forked from cemyuksel/cyCodeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyVector.h
678 lines (587 loc) · 45.4 KB
/
cyVector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// cyCodeBase by Cem Yuksel
// [www.cemyuksel.com]
//-------------------------------------------------------------------------------
//! \file cyVector.h
//! \author Cem Yuksel
//!
//! \brief 2D, 3D, 4D, and ND vector classes.
//!
//-------------------------------------------------------------------------------
//
// Copyright (c) 2016, Cem Yuksel <[email protected]>
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//-------------------------------------------------------------------------------
#ifndef _CY_VECTOR_H_INCLUDED_
#define _CY_VECTOR_H_INCLUDED_
//-------------------------------------------------------------------------------
#include "cyCore.h"
//-------------------------------------------------------------------------------
namespace cy {
//-------------------------------------------------------------------------------
// Forward declarations
//! \cond HIDDEN_SYMBOLS
template <typename T> class Vec2;
template <typename T> class Vec3;
template <typename T> class Vec4;
//! \endcond
//-------------------------------------------------------------------------------
//! A general class for N-dimensional vectors.
template <typename T, int N>
class Vec
{
CY_NODISCARD friend Vec operator - ( T v, Vec const &p ) { return Vec<T,N>(v)-p; } //!< Subtraction from a constant
CY_NODISCARD friend Vec operator + ( T v, Vec const &p ) { return p+v; } //!< Addition with a constant
CY_NODISCARD friend Vec operator * ( T v, Vec const &p ) { return p*v; } //!< Multiplication with a constant
public:
//!@name Components of the vector
T elem[N];
//!@name Constructors
Vec() CY_CLASS_FUNCTION_DEFAULT
explicit Vec( T const * restrict p ) { MemCopy(elem,p,N); }
explicit Vec( T v ) { for ( int i=0; i<N; ++i ) elem[i]=v; }
template <typename S> explicit Vec( Vec<S,N> const &p ) { MemConvert(elem,p.elem,N); }
template <int M> explicit Vec( Vec<T,M> const &p )
{
if ( N <= M ) { MemCopy(elem,p.elem,N); }
else { MemCopy(elem,p.elem,M); MemClear(elem,N-M); }
}
template <typename S, int M> explicit Vec( Vec<S,M> const &p )
{
if ( N <= M ) { MemConvert(elem,p.elem,N); }
else { MemConvert(elem,p.elem,M); MemClear(elem,N-M); }
}
explicit Vec( Vec2<T> const &p );
explicit Vec( Vec3<T> const &p );
explicit Vec( Vec4<T> const &p );
template <typename S> explicit Vec( Vec2<S> const &p );
template <typename S> explicit Vec( Vec3<S> const &p );
template <typename S> explicit Vec( Vec4<S> const &p );
template <typename P> explicit Vec( P const &p ) { for ( int i=0; i<N; ++i ) elem[i]=(T)p[i]; }
//!@name Set & Get value methods
void Zero() { MemClear(elem,N); } //!< Sets the coordinates as zero
void Get( T * restrict p ) const { MemCopy(p,elem,N); } //!< Puts the coordinate values into the array
void Set( T const * restrict p ) { MemCopy(elem,p,N); } //!< Sets the coordinates using the values in the given array
void Set( T v ) { for ( int i=0; i<N; ++i ) elem[i] = v; } //!< Sets all coordinates using the given value
template <int M> void CopyData( T * restrict p ) { if ( M <= N ) { MemCopy(p,elem,M); } else { MemCopy(p,elem,N); MemClear(p+N,M-N); } }
template <typename S, int M> void ConvertData( S * restrict p ) { if ( M <= N ) { MemConvert(p,elem,M); } else { MemConvert(p,elem,N); MemClear(p+N,M-N); } }
void Normalize() { *this /= Length(); } //!< Normalizes the vector, such that its length becomes 1.
//!@name General methods
CY_NODISCARD Vec GetNormalized() const { return *this / Length(); } //!< Returns a normalized copy of the vector.
CY_NODISCARD T LengthSquared() const { Vec p=operator*(*this); return p.Sum(); } //!< Returns the square of the length. Effectively, this is the dot product of the vector with itself.
CY_NODISCARD T Length () const { return cy::Sqrt(LengthSquared()); } //!< Returns the length of the vector.
CY_NODISCARD T Sum () const { T v=elem[0]; for ( int i=1; i<N; ++i ) v+=elem[i]; return v; } //!< Returns the sum of its components
CY_NODISCARD bool IsZero () const { for ( int i=0; i<N; ++i ) if ( elem[i] != T(0) ) return false; return true; } //!< Returns true if all components are exactly zero
CY_NODISCARD T Min () const { T m = elem[0]; for ( int i=1; i<N; ++i ) if ( m > elem[i] ) m = elem[i]; return m; } //!< Returns the minimum component of the vector.
CY_NODISCARD T Max () const { T m = elem[0]; for ( int i=1; i<N; ++i ) if ( m < elem[i] ) m = elem[i]; return m; } //!< Returns the maximum component of the vector.
CY_NODISCARD int MinComp () const { T m = elem[0]; int ix=0; for ( int i=1; i<N; ++i ) if ( m > elem[i] ) { m = elem[i]; ix = i; } return ix; } //!< Returns the index of the minimum component of the vector.
CY_NODISCARD int MaxComp () const { T m = elem[0]; int ix=0; for ( int i=1; i<N; ++i ) if ( m < elem[i] ) { m = elem[i]; ix = i; } return ix; } //!< Returns the index of the maximum component of the vector.
CY_NODISCARD bool IsFinite () const { for ( int i=0; i<N; ++i ) if ( ! cy::IsFinite(elem[i]) ) return false; return true; } //!< Returns true if all components are finite real numbers.
CY_NODISCARD bool IsUnit () const { return std::abs(LengthSquared()-T(1)) < T(0.001); } //!< Returns true if the length of the vector is close to 1.
CY_NODISCARD Vec Sqrt () const { Vec v; for ( int i=0; i<N; ++i ) v.elem[i] = cy::Sqrt(elem[i]); return v; } //!< Returns the square root of the vector.
CY_NODISCARD Vec Abs () const { Vec v; for ( int i=0; i<N; ++i ) v.elem[i] = std::abs(elem[i]); return v; } //!< Returns a vector containing the absolute values of all components.
//!@name Limit methods
void Clamp ( T minLimit, T maxLimit ) { ClampMin(minLimit); ClampMax(maxLimit); } //!< Ensures that all components of the vector are within the given limits.
void ClampMin( T v ) { for ( int i=0; i<N; ++i ) elem[i] = (elem[i]<v) ? v : elem[i]; } //!< Ensures that all components of the vector are greater than or equal to the given limit.
void ClampMax( T v ) { for ( int i=0; i<N; ++i ) elem[i] = (elem[i]>v) ? v : elem[i]; } //!< Ensures that all components of the vector are smaller than or equal to the given limit.
void SetAbs () { for ( int i=0; i<N; i++ ) elem[i] = std::abs(elem[i]); } //!< Converts all negative components to positive values
//!@name Unary operators
CY_NODISCARD Vec operator - () const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i]=-elem[i]; return r; }
//!@name Binary operators
CY_NODISCARD Vec operator + ( Vec const &p ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] + p.elem[i]; return r; }
CY_NODISCARD Vec operator - ( Vec const &p ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] - p.elem[i]; return r; }
CY_NODISCARD Vec operator * ( Vec const &p ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] * p.elem[i]; return r; }
CY_NODISCARD Vec operator / ( Vec const &p ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] / p.elem[i]; return r; }
CY_NODISCARD Vec operator + ( T const v ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] + v; return r; }
CY_NODISCARD Vec operator - ( T const v ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] - v; return r; }
CY_NODISCARD Vec operator * ( T const v ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] * v; return r; }
CY_NODISCARD Vec operator / ( T const v ) const { Vec r; for ( int i=0; i<N; ++i ) r.elem[i] = elem[i] / v; return r; }
//!@name Assignment operators
Vec const& operator += ( Vec const &p ) { for ( int i=0; i<N; ++i ) elem[i] += p.elem[i]; return *this; }
Vec const& operator -= ( Vec const &p ) { for ( int i=0; i<N; ++i ) elem[i] -= p.elem[i]; return *this; }
Vec const& operator *= ( Vec const &p ) { for ( int i=0; i<N; ++i ) elem[i] *= p.elem[i]; return *this; }
Vec const& operator /= ( Vec const &p ) { for ( int i=0; i<N; ++i ) elem[i] /= p.elem[i]; return *this; }
Vec const& operator += ( T const v ) { for ( int i=0; i<N; ++i ) elem[i] += v; return *this; }
Vec const& operator -= ( T const v ) { for ( int i=0; i<N; ++i ) elem[i] -= v; return *this; }
Vec const& operator *= ( T const v ) { for ( int i=0; i<N; ++i ) elem[i] *= v; return *this; }
Vec const& operator /= ( T const v ) { for ( int i=0; i<N; ++i ) elem[i] /= v; return *this; }
//!@name Test operators
CY_NODISCARD bool operator == ( Vec const& p ) const { for ( int i=0; i<N; ++i ) if ( elem[i] != p.elem[i] ) return false; return true; }
CY_NODISCARD bool operator != ( Vec const& p ) const { for ( int i=0; i<N; ++i ) if ( elem[i] != p.elem[i] ) return true; return false; }
//!@name Access operators
CY_NODISCARD T& operator [] ( int i ) { return Element(i); }
CY_NODISCARD T operator [] ( int i ) const { return Element(i); }
CY_NODISCARD T& Element ( int i ) { assert(i>=0 && i<N); return elem[i]; }
CY_NODISCARD T const& Element ( int i ) const { assert(i>=0 && i<N); return elem[i]; }
CY_NODISCARD T* Elements () { return elem; }
CY_NODISCARD T const* Elements () const { return elem; }
//!@name Dot product
CY_NODISCARD T Dot ( Vec const &p ) const { Vec r=operator*(p); return r.Sum(); } //!< Dot product
CY_NODISCARD T operator % ( Vec const &p ) const { return Dot(p); } //!< Dot product operator
};
//-------------------------------------------------------------------------------
//! 2D vector class
template <typename T>
class Vec2
{
CY_NODISCARD friend Vec2 operator - ( T v, Vec2 const &p ) { return Vec2<T>(v)-p; } //!< Subtraction from a constant
CY_NODISCARD friend Vec2 operator + ( T v, Vec2 const &p ) { return p+v; } //!< Addition with a constant
CY_NODISCARD friend Vec2 operator * ( T v, Vec2 const &p ) { return p*v; } //!< Multiplication with a constant
public:
//!@name Components of the vector
union {
struct { T x, y; };
T elem[2]; //!< Array-type access to the vector elements x and y
};
//!@name Constructors
Vec2() CY_CLASS_FUNCTION_DEFAULT
Vec2( T _x, T _y ) : x( _x), y( _y) {}
explicit Vec2( T v ) : x(v ), y(v ) {}
explicit Vec2( Vec3<T> const &p );
explicit Vec2( Vec4<T> const &p );
explicit Vec2( T const * restrict v ) { Set( v ); }
template <typename S> explicit Vec2( Vec2<S> const &p ) : x(T(p.x)), y(T(p.y)) {}
template <typename S> explicit Vec2( Vec3<S> const &p );
template <typename S> explicit Vec2( Vec4<S> const &p );
template <int N > explicit Vec2( Vec<T,N> const &p ) { p.template CopyData<2>(elem); }
template <int N, typename S> explicit Vec2( Vec<S,N> const &p ) { p.template ConvertData<T,2>(elem); }
//!@name Set & Get value methods
void Zero() { MemClear(elem,2); } //!< Sets the coordinates as zero.
void Get( T * restrict p ) const { ((Vec2*)p)->operator=(*this); } //!< Puts the coordinate values into the array.
void Set( T const * restrict p ) { operator=(*((Vec2*)p)); } //!< Sets the coordinates using the values in the given array.
void Set( T v ) { x=v; y=v; } //!< Sets all coordinates using the given value
void Set( T _x, T _y ) { x=_x; y=_y; } //!< Sets the coordinates using the given values
void Normalize() { *this /= Length(); } //!< Normalizes the vector, such that its length becomes 1.
//!@name General methods
CY_NODISCARD Vec2 GetNormalized () const { return *this / Length(); } //!< Returns a normalized copy of the vector.
CY_NODISCARD T LengthSquared () const { Vec2 p=operator*(*this); return p.Sum(); } //!< Returns the square of the length. Effectively, this is the dot product of the vector with itself.
CY_NODISCARD T Length () const { return cy::Sqrt(LengthSquared()); } //!< Returns the length of the vector.
CY_NODISCARD T Sum () const { return x+y; } //!< Returns the sum of its components
CY_NODISCARD bool IsZero () const { return x==T(0) && y==T(0); } //!< Returns true if all components are exactly zero
CY_NODISCARD T Min () const { return cy::Min(x,y); } //!< Returns the minimum component of the vector.
CY_NODISCARD T Max () const { return cy::Max(x,y); } //!< Returns the maximum component of the vector.
CY_NODISCARD int MinComp () const { return x>y; } //!< Returns the index of the minimum component of the vector.
CY_NODISCARD int MaxComp () const { return x<y; } //!< Returns the index of the maximum component of the vector.
CY_NODISCARD bool IsFinite () const { return cy::IsFinite(x) && cy::IsFinite(y); } //!< Returns true if all components are finite real numbers.
CY_NODISCARD bool IsUnit () const { return std::abs(LengthSquared()-T(1)) < T(0.001); } //!< Returns true if the length of the vector is close to 1.
CY_NODISCARD Vec2 Sqrt () const { return Vec2(cy::Sqrt(x),cy::Sqrt(y)); } //!< Returns the square root of the vector.
CY_NODISCARD Vec2 Abs () const { return Vec2(std::abs(x),std::abs(y)); } //!< Returns a vector containing the absolute values of all components.
CY_NODISCARD Vec2 SortAsc () const { Vec2 v; Sort2<true >( v.elem, elem ); return v; } //!< Returns a vector with components sorted in ascending order.
CY_NODISCARD Vec2 SortDesc () const { Vec2 v; Sort2<false>( v.elem, elem ); return v; } //!< Returns a vector with components sorted in descending order.
CY_NODISCARD Vec2 GetPerpendicular() const { return Vec2(-y,x); } //!< Returns a perpendicular vector (rotated by 90 degrees in counter clockwise direction).
//!@name Limit methods
void Clamp ( T minLimit, T maxLimit ) { ClampMin(minLimit); ClampMax(maxLimit); } //!< Ensures that all components of the vector are within the given limits.
void ClampMin( T v ) { x=(x<v)?v:x; y=(y<v)?v:y; } //!< Ensures that all components of the vector are greater than or equal to the given limit.
void ClampMax( T v ) { x=(x>v)?v:x; y=(y>v)?v:y; } //!< Ensures that all components of the vector are smaller than or equal to the given limit.
void SetAbs () { x=std::abs(x); y=std::abs(y); } //!< Converts all negative components to positive values
//!@name Unary operators
CY_NODISCARD Vec2 operator - () const { Vec2 r; r.x=-x; r.y=-y; return r; }
//!@name Binary operators
CY_NODISCARD Vec2 operator + ( Vec2 const &p ) const { Vec2 r; r.x=x+p.x; r.y=y+p.y; return r; }
CY_NODISCARD Vec2 operator - ( Vec2 const &p ) const { Vec2 r; r.x=x-p.x; r.y=y-p.y; return r; }
CY_NODISCARD Vec2 operator * ( Vec2 const &p ) const { Vec2 r; r.x=x*p.x; r.y=y*p.y; return r; }
CY_NODISCARD Vec2 operator / ( Vec2 const &p ) const { Vec2 r; r.x=x/p.x; r.y=y/p.y; return r; }
CY_NODISCARD Vec2 operator + ( T const v ) const { Vec2 r; r.x=x+v; r.y=y+v; return r; }
CY_NODISCARD Vec2 operator - ( T const v ) const { Vec2 r; r.x=x-v; r.y=y-v; return r; }
CY_NODISCARD Vec2 operator * ( T const v ) const { Vec2 r; r.x=x*v; r.y=y*v; return r; }
CY_NODISCARD Vec2 operator / ( T const v ) const { Vec2 r; r.x=x/v; r.y=y/v; return r; }
//!@name Assignment operators
Vec2 const& operator += ( Vec2 const &p ) { x+=p.x; y+=p.y; return *this; }
Vec2 const& operator -= ( Vec2 const &p ) { x-=p.x; y-=p.y; return *this; }
Vec2 const& operator *= ( Vec2 const &p ) { x*=p.x; y*=p.y; return *this; }
Vec2 const& operator /= ( Vec2 const &p ) { x/=p.x; y/=p.y; return *this; }
Vec2 const& operator += ( T const v ) { x+=v; y+=v; return *this; }
Vec2 const& operator -= ( T const v ) { x-=v; y-=v; return *this; }
Vec2 const& operator *= ( T const v ) { x*=v; y*=v; return *this; }
Vec2 const& operator /= ( T const v ) { x/=v; y/=v; return *this; }
//!@name Test operators
CY_NODISCARD bool operator == ( Vec2 const& p ) const { return x==p.x && y==p.y; }
CY_NODISCARD bool operator != ( Vec2 const& p ) const { return x!=p.x && y!=p.y; }
//!@name Access operators
CY_NODISCARD T& operator [] ( int i ) { return Element(i); }
CY_NODISCARD T const& operator [] ( int i ) const { return Element(i); }
CY_NODISCARD T& Element ( int i ) { assert(i>=0 && i<2); return elem[i]; }
CY_NODISCARD T const& Element ( int i ) const { assert(i>=0 && i<2); return elem[i]; }
CY_NODISCARD T* Elements () { return elem; }
CY_NODISCARD T const* Elements () const { return elem; }
//!@name Cross product and dot product
CY_NODISCARD T Cross ( Vec2 const &p ) const { Vec2 r(-y,x); return r.Dot(p); } //!< Cross product
CY_NODISCARD T operator ^ ( Vec2 const &p ) const { return Cross(p); } //!< Cross product operator
CY_NODISCARD T Dot ( Vec2 const &p ) const { return x*p.x + y*p.y; } //!< Dot product
CY_NODISCARD T operator % ( Vec2 const &p ) const { return Dot(p); } //!< Dot product operator
//!@name Swizzling Methods
CY_NODISCARD Vec2<T> XX() const { return Vec2<T>(x,x); }
CY_NODISCARD Vec2<T> XY() const { return *this; }
CY_NODISCARD Vec2<T> YX() const { return Vec2<T>(y,x); }
CY_NODISCARD Vec2<T> YY() const { return Vec2<T>(y,y); }
};
//-------------------------------------------------------------------------------
//! 3D vector class
template <typename T>
class Vec3
{
CY_NODISCARD friend Vec3 operator - ( T v, Vec3 const &p ) { return Vec3<T>(v)-p; } //!< Subtraction from a constant
CY_NODISCARD friend Vec3 operator + ( T v, Vec3 const &p ) { return p+v; } //!< Addition with a constant
CY_NODISCARD friend Vec3 operator * ( T v, Vec3 const &p ) { return p*v; } //!< Multiplication with a constant
public:
//!@name Components of the vector
union {
struct { T x, y, z; };
T elem[3]; //!< Array-type access to the vector elements x, y, and z
};
//!@name Constructors
Vec3() CY_CLASS_FUNCTION_DEFAULT
Vec3( T _x, T _y, T _z ) : x( _x), y( _y), z( _z) {}
explicit Vec3( T v ) : x(v ), y(v ), z(v ) {}
explicit Vec3( Vec2<T> const &p, T _z=0 ) : x(p.x), y(p.y), z( _z) {}
explicit Vec3( Vec4<T> const &p );
explicit Vec3( T const * restrict v ) { Set( v ); }
template <typename S> explicit Vec3( Vec3<S> const &p ) : x(T(p.x)), y(T(p.y)), z(T(p.z)) {}
template <typename S> explicit Vec3( Vec2<S> const &p, T _z=0 ) : x(T(p.x)), y(T(p.y)), z( _z ) {}
template <typename S> explicit Vec3( Vec4<S> const &p );
template <int N > explicit Vec3( Vec<T,N> const &p ) { p.template CopyData<3>(elem); }
template <int N, typename S> explicit Vec3( Vec<S,N> const &p ) { p.template ConvertData<T,3>(elem); }
//!@name Set & Get value methods
void Zero() { MemClear(elem,3); } //!< Sets the coordinates as zero.
void Get( T * restrict p ) const { ((Vec3*)p)->operator=(*this); } //!< Puts the coordinate values into the array.
void Set( T const * restrict p ) { operator=(*((Vec3*)p)); } //!< Sets the coordinates using the values in the given array.
void Set( T v ) { x=v; y=v; z=v; } //!< Sets all coordinates using the given value.
void Set( T _x, T _y, T _z ) { x= _x; y= _y; z=_z; } //!< Sets the coordinates using the given values.
void Set( Vec2<T> const &p, T _z ) { x=p.x; y=p.y; z=_z; } //!< Sets the coordinates using the given values.
void Normalize() { *this /= Length(); } //!< Normalizes the vector, such that its length becomes 1.
//!@name General methods
CY_NODISCARD Vec3 GetNormalized () const { return *this / Length(); } //!< Returns a normalized copy of the vector.
CY_NODISCARD T LengthSquared () const { Vec3 p=operator*(*this); return p.Sum(); } //!< Returns the square of the length. Effectively, this is the dot product of the vector with itself.
CY_NODISCARD T Length () const { return cy::Sqrt(LengthSquared()); } //!< Returns the length of the vector.
CY_NODISCARD T Sum () const { return x+y+z; } //!< Returns the sum of its components.
CY_NODISCARD bool IsZero () const { return x==T(0) && y==T(0) && z==T(0); } //!< Returns true if all components are exactly zero.
CY_NODISCARD T Min () const { return cy::Min(x,y,z); } //!< Returns the minimum component of the vector.
CY_NODISCARD T Max () const { return cy::Max(x,y,z); } //!< Returns the maximum component of the vector.
CY_NODISCARD int MinComp () const { int yx=y<x; int zx=z<x; int zy=z<y; return (yx|zx)+(zx&zy); } //!< Returns the index of the minimum component of the vector.
CY_NODISCARD int MaxComp () const { int xy=x<y; int xz=x<z; int yz=y<z; return (xy|xz)+(xz&yz); } //!< Returns the index of the maximum component of the vector.
CY_NODISCARD bool IsFinite () const { return cy::IsFinite(x) && cy::IsFinite(y) && cy::IsFinite(z); } //!< Returns true if all components are finite real numbers.
CY_NODISCARD bool IsUnit () const { return std::abs(LengthSquared()-T(1)) < T(0.001); } //!< Returns true if the length of the vector is close to 1.
CY_NODISCARD Vec3 Sqrt () const { return Vec3(cy::Sqrt(x),cy::Sqrt(y),cy::Sqrt(z)); } //!< Returns the square root of the vector.
CY_NODISCARD Vec3 Abs () const { return Vec3(std::abs(x),std::abs(y),std::abs(z)); } //!< Returns a vector containing the absolute values of all components.
CY_NODISCARD Vec3 SortAsc () const { Vec3 v; Sort3<true >( v.elem, elem ); return v; } //!< Returns a vector with components sorted in ascending order.
CY_NODISCARD Vec3 SortDesc () const { Vec3 v; Sort3<false>( v.elem, elem ); return v; } //!< Returns a vector with components sorted in descending order.
CY_NODISCARD Vec3 GetPerpendicular() const { Vec3 v0,v1; GetOrthonormals(v0,v1); return v0; } //!< Returns a perpendicular vector
void GetOrthonormals ( Vec3 &v0, Vec3 &v1 ) const //!< Returns two orthogonal vectors to this vector, forming an orthonormal basis
{
if ( z >= y ) {
T const a = T(1)/(1 + z);
T const b = -x*y*a;
v0.Set( 1 - x*x*a, b, -x );
v1.Set( b, 1 - y*y*a, -y );
} else {
T const a = T(1)/(1 + y);
T const b = -x*z*a;
v0.Set( b, -z, 1 - z*z*a );
v1.Set( 1 - x*x*a, -x, b );
}
}
//!@name Limit methods
void Clamp ( T minLimit, T maxLimit ) { ClampMin(minLimit); ClampMax(maxLimit); } //!< Ensures that all components of the vector are within the given limits.
void ClampMin( T v ) { x=(x<v)?v:x; y=(y<v)?v:y; z=(z<v)?v:z; } //!< Ensures that all components of the vector are greater than or equal to the given limit.
void ClampMax( T v ) { x=(x>v)?v:x; y=(y>v)?v:y; z=(z>v)?v:z; } //!< Ensures that all components of the vector are smaller than or equal to the given limit.
void SetAbs () { x=std::abs(x); y=std::abs(y); z=std::abs(z); } //!< Converts all negative components to positive values
//!@name Unary operators
CY_NODISCARD Vec3 operator - () const { Vec3 r; r.x=-x; r.y=-y; r.z=-z; return r; }
//!@name Binary operators
CY_NODISCARD Vec3 operator + ( Vec3 const &p ) const { Vec3 r; r.x=x+p.x; r.y=y+p.y; r.z=z+p.z; return r; }
CY_NODISCARD Vec3 operator - ( Vec3 const &p ) const { Vec3 r; r.x=x-p.x; r.y=y-p.y; r.z=z-p.z; return r; }
CY_NODISCARD Vec3 operator * ( Vec3 const &p ) const { Vec3 r; r.x=x*p.x; r.y=y*p.y; r.z=z*p.z; return r; }
CY_NODISCARD Vec3 operator / ( Vec3 const &p ) const { Vec3 r; r.x=x/p.x; r.y=y/p.y; r.z=z/p.z; return r; }
CY_NODISCARD Vec3 operator + ( T const v ) const { Vec3 r; r.x=x+v; r.y=y+v; r.z=z+v; return r; }
CY_NODISCARD Vec3 operator - ( T const v ) const { Vec3 r; r.x=x-v; r.y=y-v; r.z=z-v; return r; }
CY_NODISCARD Vec3 operator * ( T const v ) const { Vec3 r; r.x=x*v; r.y=y*v; r.z=z*v; return r; }
CY_NODISCARD Vec3 operator / ( T const v ) const { Vec3 r; r.x=x/v; r.y=y/v; r.z=z/v; return r; }
//!@name Assignment operators
Vec3 const& operator += ( Vec3 const &p ) { x+=p.x; y+=p.y; z+=p.z; return *this; }
Vec3 const& operator -= ( Vec3 const &p ) { x-=p.x; y-=p.y; z-=p.z; return *this; }
Vec3 const& operator *= ( Vec3 const &p ) { x*=p.x; y*=p.y; z*=p.z; return *this; }
Vec3 const& operator /= ( Vec3 const &p ) { x/=p.x; y/=p.y; z/=p.z; return *this; }
Vec3 const& operator += ( T const v ) { x+=v; y+=v; z+=v; return *this; }
Vec3 const& operator -= ( T const v ) { x-=v; y-=v; z-=v; return *this; }
Vec3 const& operator *= ( T const v ) { x*=v; y*=v; z*=v; return *this; }
Vec3 const& operator /= ( T const v ) { x/=v; y/=v; z/=v; return *this; }
//!@name Test operators
CY_NODISCARD bool operator == ( Vec3 const& p ) const { return x==p.x && y==p.y && z==p.z; }
CY_NODISCARD bool operator != ( Vec3 const& p ) const { return x!=p.x && y!=p.y && z!=p.z; }
//!@name Access operators
CY_NODISCARD T& operator [] ( int i ) { return Element(i); }
CY_NODISCARD T const& operator [] ( int i ) const { return Element(i); }
CY_NODISCARD T& Element ( int i ) { assert(i>=0 && i<3); return elem[i]; }
CY_NODISCARD T const& Element ( int i ) const { assert(i>=0 && i<3); return elem[i]; }
CY_NODISCARD T* Elements () { return elem; }
CY_NODISCARD T const* Elements () const { return elem; }
//!@name Cross product and dot product
CY_NODISCARD Vec3 Cross ( Vec3 const &p ) const { return Vec3(y*p.z-z*p.y, z*p.x-x*p.z, x*p.y-y*p.x); } //!< Cross product
CY_NODISCARD Vec3 operator ^ ( Vec3 const &p ) const { return Cross(p); } //!< Cross product
CY_NODISCARD T Dot ( Vec3 const &p ) const { return x*p.x + y*p.y + z*p.z; } //!< Dot product
CY_NODISCARD T operator % ( Vec3 const &p ) const { return Dot(p); } //!< Dot product
//!@name Swizzling Methods
CY_NODISCARD Vec2<T> XX() const { return Vec2<T>(x,x); }
CY_NODISCARD Vec2<T> XY() const { return Vec2<T>(*this); }
CY_NODISCARD Vec2<T> XZ() const { return Vec2<T>(x,z); }
CY_NODISCARD Vec2<T> YX() const { return Vec2<T>(y,x); }
CY_NODISCARD Vec2<T> YY() const { return Vec2<T>(y,y); }
CY_NODISCARD Vec2<T> YZ() const { return Vec2<T>(y,z); }
CY_NODISCARD Vec2<T> ZX() const { return Vec2<T>(z,x); }
CY_NODISCARD Vec2<T> ZY() const { return Vec2<T>(z,y); }
CY_NODISCARD Vec2<T> ZZ() const { return Vec2<T>(z,z); }
CY_NODISCARD Vec3<T> XXX() const { return Vec3<T>(x,x,x); }
CY_NODISCARD Vec3<T> XXY() const { return Vec3<T>(x,x,y); }
CY_NODISCARD Vec3<T> XXZ() const { return Vec3<T>(x,x,z); }
CY_NODISCARD Vec3<T> XYX() const { return Vec3<T>(x,y,x); }
CY_NODISCARD Vec3<T> XYY() const { return Vec3<T>(x,y,y); }
CY_NODISCARD Vec3<T> XYZ() const { return *this; }
CY_NODISCARD Vec3<T> XZX() const { return Vec3<T>(x,z,x); }
CY_NODISCARD Vec3<T> XZY() const { return Vec3<T>(x,z,y); }
CY_NODISCARD Vec3<T> XZZ() const { return Vec3<T>(x,z,z); }
CY_NODISCARD Vec3<T> YXX() const { return Vec3<T>(y,x,x); }
CY_NODISCARD Vec3<T> YXY() const { return Vec3<T>(y,x,y); }
CY_NODISCARD Vec3<T> YXZ() const { return Vec3<T>(y,x,z); }
CY_NODISCARD Vec3<T> YYX() const { return Vec3<T>(y,y,x); }
CY_NODISCARD Vec3<T> YYY() const { return Vec3<T>(y,y,y); }
CY_NODISCARD Vec3<T> YYZ() const { return Vec3<T>(y,y,z); }
CY_NODISCARD Vec3<T> YZX() const { return Vec3<T>(y,z,x); }
CY_NODISCARD Vec3<T> YZY() const { return Vec3<T>(y,z,y); }
CY_NODISCARD Vec3<T> YZZ() const { return Vec3<T>(y,z,z); }
CY_NODISCARD Vec3<T> ZXX() const { return Vec3<T>(z,x,x); }
CY_NODISCARD Vec3<T> ZXY() const { return Vec3<T>(z,x,y); }
CY_NODISCARD Vec3<T> ZXZ() const { return Vec3<T>(z,x,z); }
CY_NODISCARD Vec3<T> ZYX() const { return Vec3<T>(z,y,x); }
CY_NODISCARD Vec3<T> ZYY() const { return Vec3<T>(z,y,y); }
CY_NODISCARD Vec3<T> ZYZ() const { return Vec3<T>(z,y,z); }
CY_NODISCARD Vec3<T> ZZX() const { return Vec3<T>(z,z,x); }
CY_NODISCARD Vec3<T> ZZY() const { return Vec3<T>(z,z,y); }
CY_NODISCARD Vec3<T> ZZZ() const { return Vec3<T>(z,z,z); }
};
//-------------------------------------------------------------------------------
//! 4D vector class
template <typename T>
class Vec4
{
CY_NODISCARD friend Vec4 operator - ( T v, Vec4 const &p ) { return Vec4<T>(v)-p; } //!< Subtraction from a constant
CY_NODISCARD friend Vec4 operator + ( T v, Vec4 const &p ) { return p+v; } //!< Addition with a constant
CY_NODISCARD friend Vec4 operator * ( T v, Vec4 const &p ) { return p*v; } //!< Multiplication with a constant
public:
//!@name Components of the vector
union {
struct { T x, y, z, w; };
T elem[4]; //!< Array-type access to the vector elements x, y, z, and w
};
//!@name Constructors
Vec4() CY_CLASS_FUNCTION_DEFAULT
Vec4( T _x, T _y, T _z, T _w ) : x( _x), y( _y), z( _z), w( _w) {}
explicit Vec4( T v ) : x(v ), y(v ), z(v ), w(v ) {}
explicit Vec4( Vec2<T> const &p, T _z=0, T _w=1 ) : x(p.x), y(p.y), z( _z), w( _w) {}
explicit Vec4( Vec3<T> const &p, T _w=1 ) : x(p.x), y(p.y), z(p.z), w( _w) {}
explicit Vec4( T const * restrict v ) { Set( v ); }
template <typename S> explicit Vec4( Vec2<S> const &p, T _z=0, T _w=1 ) : x(T(p.x)), y(T(p.y)), z( _z ), w( _w ) {}
template <typename S> explicit Vec4( Vec3<S> const &p, T _w=1 ) : x(T(p.x)), y(T(p.y)), z(T(p.z)), w( _w ) {}
template <typename S> explicit Vec4( Vec4<S> const &p ) : x(T(p.x)), y(T(p.y)), z(T(p.z)), w(T(p.w)) {}
template <int N > explicit Vec4( Vec<T,N> const &p ) { p.template CopyData<4>(elem); }
template <int N, typename S> explicit Vec4( Vec<S,N> const &p ) { p.template ConvertData<T,4>(elem); }
//!@name Set & Get value methods
void Zero() { MemClear(elem,4); } //!< Sets the coordinates as zero
void Get( T * restrict p ) const { ((Vec4*)p)->operator=(*this); } //!< Puts the coordinate values into the array
void Set( T const * restrict p ) { operator=(*((Vec4*)p)); } //!< Sets the coordinates using the values in the given array
void Set( T v ) { x=v; y=v; z=v; w=v; } //!< Sets all coordinates using the given value
void Set( T _x, T _y, T _z, T _w=1 ) { x= _x; y= _y; z= _z; w=_w; } //!< Sets the coordinates using the given values
void Set( Vec2<T> const &p, T _z, T _w=1 ) { x=p.x; y=p.y; z= _z; w=_w; } //!< Sets the coordinates using the given values
void Set( Vec3<T> const &p, T _w=1 ) { x=p.x; y=p.y; z=p.z; w=_w; } //!< Sets the coordinates using the given values
void Normalize() { *this /= Length(); } //!< Normalizes the vector, such that its length becomes 1.
//!@name General methods
CY_NODISCARD Vec4 GetNormalized() const { return *this / Length(); } //!< Returns a normalized copy of the vector.
CY_NODISCARD T LengthSquared() const { Vec4 p=operator*(*this); return p.Sum(); } //!< Returns the square of the length. Effectively, this is the dot product of the vector with itself.
CY_NODISCARD T Length () const { return cy::Sqrt(LengthSquared()); } //!< Returns the length of the vector.
CY_NODISCARD T Sum () const { return x+y+z+w; } //!< Returns the sum of its components
CY_NODISCARD bool IsZero () const { return x==T(0) && y==T(0) && z==T(0) && w==T(0); } //!< Returns true if all components are exactly zero
CY_NODISCARD T Min () const { return cy::Min(x,y,z,w); } //!< Returns the minimum component of the vector.
CY_NODISCARD T Max () const { return cy::Max(x,y,z,w); } //!< Returns the maximum component of the vector.
CY_NODISCARD int MinComp () const { int xy=x>y; int zw=(z>w)+2; return elem[xy]<elem[zw]?xy:zw; } //!< Returns the index of the minimum component of the vector.
CY_NODISCARD int MaxComp () const { int xy=x<y; int zw=(z<w)+2; return elem[xy]>elem[zw]?xy:zw; } //!< Returns the index of the maximum component of the vector.
CY_NODISCARD bool IsFinite () const { return cy::IsFinite(x) && cy::IsFinite(y) && cy::IsFinite(z) && cy::IsFinite(w); } //!< Returns true if all components are finite real numbers.
CY_NODISCARD bool IsUnit () const { return std::abs(LengthSquared()-T(1)) < T(0.001); } //!< Returns true if the length of the vector is close to 1.
CY_NODISCARD Vec4 Sqrt () const { return Vec4(cy::Sqrt(x),cy::Sqrt(y),cy::Sqrt(z),cy::Sqrt(w)); } //!< Returns the square root of the vector.
CY_NODISCARD Vec4 Abs () const { return Vec4(std::abs(x),std::abs(y),std::abs(z),std::abs(w)); } //!< Returns a vector containing the absolute values of all components.
CY_NODISCARD Vec4 SortAsc () const { Vec4 v; Sort4<true >( v.elem, elem ); return v; } //!< Returns a vector with components sorted in ascending order.
CY_NODISCARD Vec4 SortDesc () const { Vec4 v; Sort4<false>( v.elem, elem ); return v; } //!< Returns a vector with components sorted in descending order.
//!@name Limit methods
void Clamp ( T minLimit, T maxLimit ) { ClampMin(minLimit); ClampMax(maxLimit); } //!< Ensures that all components of the vector are within the given limits.
void ClampMin( T v ) { x=(x<v)?v:x; y=(y<v)?v:y; z=(z<v)?v:z; w=(w<v)?v:w; } //!< Ensures that all components of the vector are greater than or equal to the given limit.
void ClampMax( T v ) { x=(x>v)?v:x; y=(y>v)?v:y; z=(z>v)?v:z; w=(w>v)?v:w; } //!< Ensures that all components of the vector are smaller than or equal to the given limit.
void SetAbs () { x=std::abs(x); y=std::abs(y); z=std::abs(z); w=std::abs(w); } //!< Converts all negative components to positive values
//!@name Unary operators
Vec4 operator - () const { Vec4 r; r.x=-x; r.y=-y; r.z=-z; r.w=-w; return r; }
//!@name Binary operators
CY_NODISCARD Vec4 operator + ( Vec4 const &p ) const { Vec4 r; r.x=x+p.x; r.y=y+p.y; r.z=z+p.z; r.w=w+p.w; return r; }
CY_NODISCARD Vec4 operator - ( Vec4 const &p ) const { Vec4 r; r.x=x-p.x; r.y=y-p.y; r.z=z-p.z; r.w=w-p.w; return r; }
CY_NODISCARD Vec4 operator * ( Vec4 const &p ) const { Vec4 r; r.x=x*p.x; r.y=y*p.y; r.z=z*p.z; r.w=w*p.w; return r; }
CY_NODISCARD Vec4 operator / ( Vec4 const &p ) const { Vec4 r; r.x=x/p.x; r.y=y/p.y; r.z=z/p.z; r.w=w/p.w; return r; }
CY_NODISCARD Vec4 operator + ( T const v ) const { Vec4 r; r.x=x+v; r.y=y+v; r.z=z+v; r.w=w+v; return r; }
CY_NODISCARD Vec4 operator - ( T const v ) const { Vec4 r; r.x=x-v; r.y=y-v; r.z=z-v; r.w=w-v; return r; }
CY_NODISCARD Vec4 operator * ( T const v ) const { Vec4 r; r.x=x*v; r.y=y*v; r.z=z*v; r.w=w*v; return r; }
CY_NODISCARD Vec4 operator / ( T const v ) const { Vec4 r; r.x=x/v; r.y=y/v; r.z=z/v; r.w=w/v; return r; }
//!@name Assignment operators
Vec4 const& operator += ( Vec4 const &p ) { x+=p.x; y+=p.y; z+=p.z; w+=p.w; return *this; }
Vec4 const& operator -= ( Vec4 const &p ) { x-=p.x; y-=p.y; z-=p.z; w-=p.w; return *this; }
Vec4 const& operator *= ( Vec4 const &p ) { x*=p.x; y*=p.y; z*=p.z; w*=p.w; return *this; }
Vec4 const& operator /= ( Vec4 const &p ) { x/=p.x; y/=p.y; z/=p.z; w/=p.w; return *this; }
Vec4 const& operator += ( T const v ) { x+=v; y+=v; z+=v; w+=v; return *this; }
Vec4 const& operator -= ( T const v ) { x-=v; y-=v; z-=v; w-=v; return *this; }
Vec4 const& operator *= ( T const v ) { x*=v; y*=v; z*=v; w*=v; return *this; }
Vec4 const& operator /= ( T const v ) { x/=v; y/=v; z/=v; w/=v; return *this; }
//!@name Test operators
CY_NODISCARD bool operator == ( Vec4 const& p ) const { return x==p.x && y==p.y && z==p.z && w==p.w; }
CY_NODISCARD bool operator != ( Vec4 const& p ) const { return x!=p.x && y!=p.y && z!=p.z && w!=p.w; }
//!@name Access operators
CY_NODISCARD T& operator [] ( int i ) { return Element(i); }
CY_NODISCARD T const& operator [] ( int i ) const { return Element(i); }
CY_NODISCARD T& Element ( int i ) { assert(i>=0 && i<4); return elem[i]; }
CY_NODISCARD T const& Element ( int i ) const { assert(i>=0 && i<4); return elem[i]; }
CY_NODISCARD T* Elements () { return elem; }
CY_NODISCARD T const* Elements () const { return elem; }
//!@name Dot product
CY_NODISCARD T Dot ( Vec4 const &p ) const { return x*p.x + y*p.y + z*p.z + w*p.w; } //!< Dot product
CY_NODISCARD T operator % ( Vec4 const &p ) const { return Dot(p); } //!< Dot product
//!@name Swizzling Methods
CY_NODISCARD Vec2<T> XX() const { return Vec2<T>(x,x); }
CY_NODISCARD Vec2<T> XY() const { return Vec2<T>(*this); }
CY_NODISCARD Vec2<T> XZ() const { return Vec2<T>(x,z); }
CY_NODISCARD Vec2<T> XW() const { return Vec2<T>(x,w); }
CY_NODISCARD Vec2<T> YX() const { return Vec2<T>(y,x); }
CY_NODISCARD Vec2<T> YY() const { return Vec2<T>(y,y); }
CY_NODISCARD Vec2<T> YZ() const { return Vec2<T>(y,z); }
CY_NODISCARD Vec2<T> YW() const { return Vec2<T>(y,w); }
CY_NODISCARD Vec2<T> ZX() const { return Vec2<T>(z,x); }
CY_NODISCARD Vec2<T> ZY() const { return Vec2<T>(z,y); }
CY_NODISCARD Vec2<T> ZZ() const { return Vec2<T>(z,z); }
CY_NODISCARD Vec2<T> ZW() const { return Vec2<T>(z,w); }
CY_NODISCARD Vec2<T> WX() const { return Vec2<T>(w,x); }
CY_NODISCARD Vec2<T> WY() const { return Vec2<T>(w,y); }
CY_NODISCARD Vec2<T> WZ() const { return Vec2<T>(w,z); }
CY_NODISCARD Vec2<T> WW() const { return Vec2<T>(w,w); }
CY_NODISCARD Vec3<T> XXX() const { return Vec3<T>(x,x,x); }
CY_NODISCARD Vec3<T> XXY() const { return Vec3<T>(x,x,y); }
CY_NODISCARD Vec3<T> XXZ() const { return Vec3<T>(x,x,z); }
CY_NODISCARD Vec3<T> XXW() const { return Vec3<T>(x,x,w); }
CY_NODISCARD Vec3<T> XYX() const { return Vec3<T>(x,y,x); }
CY_NODISCARD Vec3<T> XYY() const { return Vec3<T>(x,y,y); }
CY_NODISCARD Vec3<T> XYZ() const { return Vec3<T>(*this); }
CY_NODISCARD Vec3<T> XYW() const { return Vec3<T>(x,y,w); }
CY_NODISCARD Vec3<T> XZX() const { return Vec3<T>(x,z,x); }
CY_NODISCARD Vec3<T> XZY() const { return Vec3<T>(x,z,y); }
CY_NODISCARD Vec3<T> XZZ() const { return Vec3<T>(x,z,z); }
CY_NODISCARD Vec3<T> XZW() const { return Vec3<T>(x,z,w); }
CY_NODISCARD Vec3<T> XWX() const { return Vec3<T>(x,w,x); }
CY_NODISCARD Vec3<T> XWY() const { return Vec3<T>(x,w,y); }
CY_NODISCARD Vec3<T> XWZ() const { return Vec3<T>(x,w,z); }
CY_NODISCARD Vec3<T> XWW() const { return Vec3<T>(x,w,w); }
CY_NODISCARD Vec3<T> YXX() const { return Vec3<T>(y,x,x); }
CY_NODISCARD Vec3<T> YXY() const { return Vec3<T>(y,x,y); }
CY_NODISCARD Vec3<T> YXZ() const { return Vec3<T>(y,x,z); }
CY_NODISCARD Vec3<T> YXW() const { return Vec3<T>(y,x,w); }
CY_NODISCARD Vec3<T> YYX() const { return Vec3<T>(y,y,x); }
CY_NODISCARD Vec3<T> YYY() const { return Vec3<T>(y,y,y); }
CY_NODISCARD Vec3<T> YYZ() const { return Vec3<T>(y,y,z); }
CY_NODISCARD Vec3<T> YYW() const { return Vec3<T>(y,y,w); }
CY_NODISCARD Vec3<T> YZX() const { return Vec3<T>(y,z,x); }
CY_NODISCARD Vec3<T> YZY() const { return Vec3<T>(y,z,y); }
CY_NODISCARD Vec3<T> YZZ() const { return Vec3<T>(y,z,z); }
CY_NODISCARD Vec3<T> YZW() const { return Vec3<T>(y,z,w); }
CY_NODISCARD Vec3<T> YWX() const { return Vec3<T>(y,w,x); }
CY_NODISCARD Vec3<T> YWY() const { return Vec3<T>(y,w,y); }
CY_NODISCARD Vec3<T> YWZ() const { return Vec3<T>(y,w,z); }
CY_NODISCARD Vec3<T> YWW() const { return Vec3<T>(y,w,w); }
CY_NODISCARD Vec3<T> ZXX() const { return Vec3<T>(z,x,x); }
CY_NODISCARD Vec3<T> ZXY() const { return Vec3<T>(z,x,y); }
CY_NODISCARD Vec3<T> ZXZ() const { return Vec3<T>(z,x,z); }
CY_NODISCARD Vec3<T> ZXW() const { return Vec3<T>(z,x,w); }
CY_NODISCARD Vec3<T> ZYX() const { return Vec3<T>(z,y,x); }
CY_NODISCARD Vec3<T> ZYY() const { return Vec3<T>(z,y,y); }
CY_NODISCARD Vec3<T> ZYZ() const { return Vec3<T>(z,y,z); }
CY_NODISCARD Vec3<T> ZYW() const { return Vec3<T>(z,y,w); }
CY_NODISCARD Vec3<T> ZZX() const { return Vec3<T>(z,z,x); }
CY_NODISCARD Vec3<T> ZZY() const { return Vec3<T>(z,z,y); }
CY_NODISCARD Vec3<T> ZZZ() const { return Vec3<T>(z,z,z); }
CY_NODISCARD Vec3<T> ZZW() const { return Vec3<T>(z,z,w); }
CY_NODISCARD Vec3<T> ZWX() const { return Vec3<T>(z,w,x); }
CY_NODISCARD Vec3<T> ZWY() const { return Vec3<T>(z,w,y); }
CY_NODISCARD Vec3<T> ZWZ() const { return Vec3<T>(z,w,z); }
CY_NODISCARD Vec3<T> ZWW() const { return Vec3<T>(z,w,w); }
CY_NODISCARD Vec3<T> WXX() const { return Vec3<T>(w,x,x); }
CY_NODISCARD Vec3<T> WXY() const { return Vec3<T>(w,x,y); }
CY_NODISCARD Vec3<T> WXZ() const { return Vec3<T>(w,x,z); }
CY_NODISCARD Vec3<T> WXW() const { return Vec3<T>(w,x,w); }
CY_NODISCARD Vec3<T> WYX() const { return Vec3<T>(w,y,x); }
CY_NODISCARD Vec3<T> WYY() const { return Vec3<T>(w,y,y); }
CY_NODISCARD Vec3<T> WYZ() const { return Vec3<T>(w,y,z); }
CY_NODISCARD Vec3<T> WYW() const { return Vec3<T>(w,y,w); }
CY_NODISCARD Vec3<T> WZX() const { return Vec3<T>(w,z,x); }
CY_NODISCARD Vec3<T> WZY() const { return Vec3<T>(w,z,y); }
CY_NODISCARD Vec3<T> WZZ() const { return Vec3<T>(w,z,z); }
CY_NODISCARD Vec3<T> WZW() const { return Vec3<T>(w,z,w); }
CY_NODISCARD Vec3<T> WWX() const { return Vec3<T>(w,w,x); }
CY_NODISCARD Vec3<T> WWY() const { return Vec3<T>(w,w,y); }
CY_NODISCARD Vec3<T> WWZ() const { return Vec3<T>(w,w,z); }
CY_NODISCARD Vec3<T> WWW() const { return Vec3<T>(w,w,w); }
CY_NODISCARD Vec3<T> GetNonHomogeneous() const { return Vec3<T>(*this)/w; }
};
//-------------------------------------------------------------------------------
// Definitions of the conversion constructors
template <typename T, int N> Vec<T,N>::Vec( Vec2<T> const &p ) { if (N<=2) { MemCopy (elem,&p.x,N); } else { MemCopy (elem,&p.x,2); MemClear(elem,N-2); } }
template <typename T, int N> Vec<T,N>::Vec( Vec3<T> const &p ) { if (N<=3) { MemCopy (elem,&p.x,N); } else { MemCopy (elem,&p.x,3); MemClear(elem,N-3); } }
template <typename T, int N> Vec<T,N>::Vec( Vec4<T> const &p ) { if (N<=4) { MemCopy (elem,&p.x,N); } else { MemCopy (elem,&p.x,4); MemClear(elem,N-4); } }
template <typename T, int N> template <typename S> Vec<T,N>::Vec( Vec2<S> const &p ) { if (N<=2) { MemConvert(elem,&p.x,N); } else { MemConvert(elem,&p.x,2); MemClear(elem,N-2); } }
template <typename T, int N> template <typename S> Vec<T,N>::Vec( Vec3<S> const &p ) { if (N<=3) { MemConvert(elem,&p.x,N); } else { MemConvert(elem,&p.x,3); MemClear(elem,N-3); } }
template <typename T, int N> template <typename S> Vec<T,N>::Vec( Vec4<S> const &p ) { if (N<=4) { MemConvert(elem,&p.x,N); } else { MemConvert(elem,&p.x,4); MemClear(elem,N-4); } }
template <typename T> Vec2<T>::Vec2( Vec3<T> const &p ) : x( p.x ), y( p.y ) {}
template <typename T> Vec2<T>::Vec2( Vec4<T> const &p ) : x( p.x ), y( p.y ) {}
template <typename T> Vec3<T>::Vec3( Vec4<T> const &p ) : x( p.x ), y( p.y ), z( p.z ) {}
template <typename T> template <typename S> Vec2<T>::Vec2( Vec3<S> const &p ) : x(T(p.x)), y(T(p.y)) {}
template <typename T> template <typename S> Vec2<T>::Vec2( Vec4<S> const &p ) : x(T(p.x)), y(T(p.y)) {}
template <typename T> template <typename S> Vec3<T>::Vec3( Vec4<S> const &p ) : x(T(p.x)), y(T(p.y)), z(T(p.z)) {}
//-------------------------------------------------------------------------------
/// !@name Support functions
template <typename T> inline Vec2<T> Normalize( Vec2<T> const &v ) { return v.GetNormalized(); }
template <typename T> inline Vec3<T> Normalize( Vec3<T> const &v ) { return v.GetNormalized(); }
template <typename T> inline Vec4<T> Normalize( Vec4<T> const &v ) { return v.GetNormalized(); }
//-------------------------------------------------------------------------------
typedef Vec2<float> Vec2f; //!< 2D vector class with float type elements
typedef Vec3<float> Vec3f; //!< 3D vector class with float type elements
typedef Vec4<float> Vec4f; //!< 4D vector class with float type elements
typedef Vec2<double> Vec2d; //!< 2D vector class with double type elements
typedef Vec3<double> Vec3d; //!< 3D vector class with double type elements
typedef Vec4<double> Vec4d; //!< 4D vector class with double type elements
//-------------------------------------------------------------------------------
} // namespace cy
//-------------------------------------------------------------------------------
typedef cy::Vec2f cyVec2f; //!< 2D vector class with float type elements
typedef cy::Vec3f cyVec3f; //!< 3D vector class with float type elements
typedef cy::Vec4f cyVec4f; //!< 4D vector class with float type elements
typedef cy::Vec2d cyVec2d; //!< 2D vector class with double type elements
typedef cy::Vec3d cyVec3d; //!< 3D vector class with double type elements
typedef cy::Vec4d cyVec4d; //!< 4D vector class with double type elements
//-------------------------------------------------------------------------------
#endif