SVector3.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionConfig.h>
5 
6 #include "SVector2.h"
7 
8 #include <glm/glm.hpp>
9 #include <glm/gtc/matrix_transform.hpp>
10 
11 
14 template <typename T>
15 struct vec3
16 {
17 
18  T X;
19  T Y;
20  T Z;
21 
23  // Constructors //
25 
27  vec3()
28  : X(0), Y(0), Z(0)
29  {}
30 
32  vec3(T const in)
33  : X(in), Y(in), Z(in)
34  {}
35 
37  vec3(T const x, T const y, T const z)
38  : X(x), Y(y), Z(z)
39  {}
40 
42  template <typename U>
43  vec3(glm::tvec3<U, glm::defaultp> const & v)
44  : X(v.x), Y(v.y), Z(v.z)
45  {}
46 
48  template <typename U>
49  vec3(vec3<U> const & vec)
50  : X((T) vec.X), Y((T) vec.Y), Z((T) vec.Z)
51  {}
52 
54  template <typename U>
55  vec3(vec2<U> const & vec)
56  : X((T) vec.X), Y((T) vec.Y), Z(0)
57  {}
58 
59 
61  // Index Operators //
63 
65  T operator[] (int i) const
66  {
67  switch (i)
68  {
69  case 0:
70  return X;
71  case 1:
72  return Y;
73  case 2:
74  return Z;
75  default:
76  return 0;
77  }
78  }
79 
81  T & operator[] (int i)
82  {
83  switch (i)
84  {
85  case 0:
86  return X;
87  case 1:
88  return Y;
89  case 2:
90  return Z;
91  default:
92  {
93  static T Dummy;
94  return Dummy = 0;
95  }
96  }
97  }
98 
99 
101  // Assignment //
103 
105  template <typename U>
106  vec3<T> & operator = (vec3<U> const & vec)
107  {
108  set(vec);
109  return *this;
110  }
111 
113  void reset()
114  {
115  X = Y = Z = 0;
116  }
117 
119  void set(T const in)
120  {
121  X = Y = Z = in;
122  }
123 
125  void set(T const in[])
126  {
127  X = in[0];
128  Y = in[1];
129  Z = in[2];
130  }
131 
133  template <typename U>
134  void set(vec3<U> const & other)
135  {
136  X = (T) other.X;
137  Y = (T) other.Y;
138  Z = (T) other.Z;
139  }
140 
141 
143  // Length Operations //
145 
147  T Length() const
148  {
149  return Sqrt<T>(X*X + Y*Y + Z*Z);
150  }
151 
153  friend T Length(vec3<T> const & vec)
154  {
155  return vec.Length();
156  }
157 
159  T LengthSq() const
160  {
161  return X*X + Y*Y + Z*Z;
162  }
163 
165  friend T LengthSq(vec3<T> const & vec)
166  {
167  return vec.LengthSq();
168  }
169 
170 
172  // Dot Product //
174 
175  T Dot(vec3<T> const & other) const
176  {
177  return X * other.X + Y * other.Y + Z * other.Z;
178  }
179 
180  friend T Dot(vec3<T> const & lhs, vec3<T> const & rhs)
181  {
182  return lhs.Dot(rhs);
183  }
184 
185 
187  // Distance //
189 
190  T GetDistanceFrom(vec3<T> const & v) const
191  {
192  return (*this - v).Length();
193  }
194 
195  friend T Distance(vec3<T> const & lhs, vec3<T> const & rhs)
196  {
197  return (lhs - rhs).Length();
198  }
199 
200  T const GetDistanceSqFrom(vec3<T> const & v) const
201  {
202  return (* this - v).LengthSq();
203  }
204 
205  friend T DistanceSq(vec3<T> const & lhs, vec3<T> const & rhs)
206  {
207  return (lhs - rhs).LengthSq();
208  }
209 
210 
212  // Interpolation //
214 
215  template <typename Real>
216  vec3<T> GetInterpolated(vec3<T> const & v, Real const d) const
217  {
218  vec3<T> ret;
219  Real inv = (Real) 1 - d;
220  ret.X = (T) ((Real) v.X * inv + (Real) X * d);
221  ret.Y = (T) ((Real) v.Y * inv + (Real) Y * d);
222  ret.Z = (T) ((Real) v.Z * inv + (Real) Z * d);
223  return ret;
224  }
225 
226  template <typename Real>
227  friend vec3<T> Interpolate(vec3<T> const & lhs, vec3<T> const & rhs, Real const d)
228  {
229  return lhs.GetInterpolated(rhs, d);
230  }
231 
232 
234  // Normalization //
236 
238  {
239  vec3<T> ret = *this;
240  ret.Normalize();
241  return ret;
242  }
243 
246  {
247  T const len = Length();
248 
249  X /= len;
250  Y /= len;
251  Z /= len;
252 
253  return *this;
254  }
255 
256  friend vec3<T> Normalize(vec3<T> const & v)
257  {
258  vec3<T> ret = v;
259  ret.Normalize();
260  return ret;
261  }
262 
263 
265  // Basic Arithmetic //
267 
268  vec3<T> operator + (vec3<T> const & v) const
269  {
270  vec3<T> ret;
271  ret.X = X + v.X;
272  ret.Y = Y + v.Y;
273  ret.Z = Z + v.Z;
274  return ret;
275  }
276 
277  vec3<T> operator - (vec3<T> const & v) const
278  {
279  vec3<T> ret;
280  ret.X = X - v.X;
281  ret.Y = Y - v.Y;
282  ret.Z = Z - v.Z;
283  return ret;
284  }
285 
286  vec3<T> operator * (vec3<T> const & v) const
287  {
288  vec3<T> ret;
289  ret.X = X * v.X;
290  ret.Y = Y * v.Y;
291  ret.Z = Z * v.Z;
292  return ret;
293  }
294 
295  vec3<T> operator / (vec3<T> const & v) const
296  {
297  vec3<T> ret;
298  ret.X = X / v.X;
299  ret.Y = Y / v.Y;
300  ret.Z = Z / v.Z;
301  return ret;
302  }
303 
304 
306  // Scalar Arithmetic //
308 
309  vec3<T> operator + (T const s) const
310  {
311  vec3<T> ret;
312  ret.X = X + s;
313  ret.Y = Y + s;
314  ret.Z = Z + s;
315  return ret;
316  }
317 
318  vec3<T> operator - (T const s) const
319  {
320  vec3<T> ret;
321  ret.X = X - s;
322  ret.Y = Y - s;
323  ret.Z = Z - s;
324  return ret;
325  }
326 
327  vec3<T> operator * (T const s) const
328  {
329  vec3<T> ret;
330  ret.X = X * s;
331  ret.Y = Y * s;
332  ret.Z = Z * s;
333  return ret;
334  }
335 
336  vec3<T> operator / (T const s) const
337  {
338  vec3<T> ret;
339  ret.X = X / s;
340  ret.Y = Y / s;
341  ret.Z = Z / s;
342  return ret;
343  }
344 
345  friend vec3<T> operator + (T const lhs, vec3<T> const & rhs)
346  {
347  vec3<T> ret;
348  ret.X = lhs + rhs.X;
349  ret.Y = lhs + rhs.Y;
350  ret.Z = lhs + rhs.Z;
351  return ret;
352  }
353 
354  friend vec3<T> operator - (T const lhs, vec3<T> const & rhs)
355  {
356  vec3<T> ret;
357  ret.X = lhs - rhs.X;
358  ret.Y = lhs - rhs.Y;
359  ret.Z = lhs - rhs.Z;
360  return ret;
361  }
362 
363  friend vec3<T> operator * (T const lhs, vec3<T> const & rhs)
364  {
365  vec3<T> ret;
366  ret.X = lhs * rhs.X;
367  ret.Y = lhs * rhs.Y;
368  ret.Z = lhs * rhs.Z;
369  return ret;
370  }
371 
372  friend vec3<T> operator / (T const lhs, vec3<T> const & rhs)
373  {
374  vec3<T> ret;
375  ret.X = lhs / rhs.X;
376  ret.Y = lhs / rhs.Y;
377  ret.Z = lhs / rhs.Z;
378  return ret;
379  }
380 
381 
383  // Basic Arithmetic Assignment //
385 
387  {
388  X += v.X;
389  Y += v.Y;
390  Z += v.Z;
391 
392  return * this;
393  }
394 
396  {
397  X -= v.X;
398  Y -= v.Y;
399  Z -= v.Z;
400 
401  return * this;
402  }
403 
405  {
406  X *= v.X;
407  Y *= v.Y;
408  Z *= v.Z;
409 
410  return * this;
411  }
412 
414  {
415  X /= v.X;
416  Y /= v.Y;
417  Z /= v.Z;
418 
419  return * this;
420  }
421 
422 
424  // Scalar Arithmetic Assignment //
426 
427  vec3<T> & operator += (T const s)
428  {
429  X += s;
430  Y += s;
431  Z += s;
432 
433  return * this;
434  }
435 
436  vec3<T> & operator -= (T const s)
437  {
438  X -= s;
439  Y -= s;
440  Z -= s;
441 
442  return * this;
443  }
444 
445  vec3<T> & operator *= (T const s)
446  {
447  X *= s;
448  Y *= s;
449  Z *= s;
450 
451  return * this;
452  }
453 
454  vec3<T> & operator /= (T const s)
455  {
456  X /= s;
457  Y /= s;
458  Z /= s;
459 
460  return * this;
461  }
462 
464  // Negation //
466 
467  vec3<T> const operator - () const
468  {
469  vec3<T> ret;
470  ret.X = -X;
471  ret.Y = -Y;
472  ret.Z = -Z;
473  return ret;
474  }
475 
476 
478  // Inequality operator (for sorting) //
480 
481  bool operator < (vec3<T> const & v) const
482  {
483  return
484  (X < v.X) ||
485  (X == v.X && Y < v.Y) ||
486  (X == v.X && Y == v.Y && Z < v.Z);
487  }
488 
489 
491  // Equality operators //
493 
494  bool operator == (vec3<T> const & v) const
495  {
496  return Equals(v);
497  }
498 
499  bool operator != (vec3<T> const & v) const
500  {
501  return ! Equals(v);
502  }
503 
504  bool Equals(vec3<T> const & v, T const Epsilon = RoundingError<T>::Value()) const
505  {
506  return
507  ::Equals(X, v.X, Epsilon) &&
508  ::Equals(Y, v.Y, Epsilon) &&
509  ::Equals(Z, v.Z, Epsilon);
510  }
511 
512 
514  // Extrema //
516 
517  friend vec3<T> ComponentMinimum(vec3<T> const & lhs, vec3<T> const & rhs)
518  {
519  vec3<T> ret;
520  ret.X = Minimum(lhs.X, rhs.X);
521  ret.Y = Minimum(lhs.Y, rhs.Y);
522  ret.Z = Minimum(lhs.Z, rhs.Z);
523  return ret;
524  }
525 
526  friend vec3<T> ComponentMaximum(vec3<T> const & lhs, vec3<T> const & rhs)
527  {
528  vec3<T> ret;
529  ret.X = Maximum(lhs.X, rhs.X);
530  ret.Y = Maximum(lhs.Y, rhs.Y);
531  ret.Z = Maximum(lhs.Z, rhs.Z);
532  return ret;
533  }
534 
535 
537  // Cross Product //
539 
541  vec3<T> CrossProduct(vec3<T> const & v) const
542  {
543  return vec3<T>(Y*v.Z - v.Y*Z, v.X*Z - X*v.Z, X*v.Y - v.X*Y);
544  }
545 
547  vec3<T> Cross(vec3<T> const & v) const
548  {
549  return vec3<T>(Y*v.Z - v.Y*Z, v.X*Z - X*v.Z, X*v.Y - v.X*Y);
550  }
551 
553  friend vec3<T> Cross(vec3<T> const & lhs, vec3<T> const & rhs)
554  {
555  return lhs.Cross(rhs);
556  }
557 
558 
560  // GLM //
562 
563  glm::vec3 const ToGLM() const
564  {
565  return glm::vec3(X, Y, Z);
566  }
567 
568  static vec3<T> FromGLM(glm::vec3 const & Vec)
569  {
570  return vec3<T>(Vec.x, Vec.y, Vec.z);
571  }
572 
573  static vec3<T> FromGLM(glm::vec4 const & Vec)
574  {
575  return vec3<T>(Vec.x, Vec.y, Vec.z);
576  }
577 
578 
580  // Matrix Transform //
582 
583  void Transform(glm::mat4 const & mat, f32 const TranslateComponent = 1)
584  {
585  glm::vec4 v = glm::vec4(ToGLM(), TranslateComponent);
586  v = mat * v;
587  *this = FromGLM(v);
588  }
589 
590  vec3<T> GetTransformed(glm::mat4 const & mat, f32 const TranslateComponent = 1) const
591  {
592  glm::vec4 v = glm::vec4(ToGLM(), TranslateComponent);
593  v = mat * v;
594  return FromGLM(v);
595  }
596 
597  vec3<T> RotateAround(vec3<T> const & other, f32 const radians) const
598  {
599  return GetTransformed(glm::rotate(glm::mat4(1.f), radians, other.ToGLM()));
600  }
601 
602 
604  // Swizzle //
606 
607  vec2<T> XY() const
608  {
609  return vec2<T>(X, Y);
610  }
611 
612  vec2<T> YZ() const
613  {
614  return vec2<T>(Y, Z);
615  }
616 
617  vec2<T> XZ() const
618  {
619  return vec2<T>(X, Z);
620  }
621 
622 
624  // Formatting //
626 
627  friend std::ostream & operator << (std::ostream & stream, vec3<T> const & vec)
628  {
629  return stream << +vec.X << " " << +vec.Y << " " << +vec.Z;
630  }
631 
632 };
633 
634 typedef vec3<f32> vec3f;
635 typedef vec3<f64> vec3d;
636 typedef vec3<s32> vec3i;
637 typedef vec3<u32> vec3u;
vec3< T > & operator-=(vec3< T > const &v)
Definition: SVector3.h:395
bool Equals(vec3< T > const &v, T const Epsilon=RoundingError< T >::Value()) const
Definition: SVector3.h:504
vec3< u32 > vec3u
Definition: SVector3.h:637
friend T DistanceSq(vec3< T > const &lhs, vec3< T > const &rhs)
Definition: SVector3.h:205
bool operator==(vec3< T > const &v) const
Definition: SVector3.h:494
vec2< T > YZ() const
Definition: SVector3.h:612
T Y
Definition: SVector3.h:19
friend T Length(vec3< T > const &vec)
Length of vector.
Definition: SVector3.h:153
vec3< T > RotateAround(vec3< T > const &other, f32 const radians) const
Definition: SVector3.h:597
vec3< T > & operator=(vec3< U > const &vec)
Generic vector assignment operator.
Definition: SVector3.h:106
vec3(vec2< U > const &vec)
Generic vec2 constructor.
Definition: SVector3.h:55
static vec3< T > FromGLM(glm::vec4 const &Vec)
Definition: SVector3.h:573
vec2< T > XY() const
Definition: SVector3.h:607
3D vector
Definition: SVector3.h:15
T GetDistanceFrom(vec3< T > const &v) const
Definition: SVector3.h:190
vec3< f32 > vec3f
Definition: SVector3.h:634
T LengthSq() const
Squared-length of vector (computationally fast)
Definition: SVector3.h:159
vec3(T const x, T const y, T const z)
Explicit constructor.
Definition: SVector3.h:37
2D vector
Definition: SVector2.h:11
T X
Definition: SVector2.h:14
vec3< T > GetTransformed(glm::mat4 const &mat, f32 const TranslateComponent=1) const
Definition: SVector3.h:590
vec3< T > operator+(vec3< T > const &v) const
Definition: SVector3.h:268
vec3< T > operator*(vec3< T > const &v) const
Definition: SVector3.h:286
T Z
Definition: SVector3.h:20
vec3< s32 > vec3i
Definition: SVector3.h:636
friend vec3< T > Normalize(vec3< T > const &v)
Definition: SVector3.h:256
glm::vec3 const ToGLM() const
Definition: SVector3.h:563
static vec3< T > FromGLM(glm::vec3 const &Vec)
Definition: SVector3.h:568
friend vec3< T > ComponentMinimum(vec3< T > const &lhs, vec3< T > const &rhs)
Definition: SVector3.h:517
vec3()
Default constructor.
Definition: SVector3.h:27
vec3< T > CrossProduct(vec3< T > const &v) const
Vector cross product.
Definition: SVector3.h:541
friend T LengthSq(vec3< T > const &vec)
Squared-length of vector (computationally fast)
Definition: SVector3.h:165
bool operator!=(vec3< T > const &v) const
Definition: SVector3.h:499
T const GetDistanceSqFrom(vec3< T > const &v) const
Definition: SVector3.h:200
vec3< T > const operator-() const
Definition: SVector3.h:467
vec3< T > Cross(vec3< T > const &v) const
Vector cross product.
Definition: SVector3.h:547
friend T Dot(vec3< T > const &lhs, vec3< T > const &rhs)
Definition: SVector3.h:180
vec3< T > & operator/=(vec3< T > const &v)
Definition: SVector3.h:413
float f32
Definition: ionTypes.h:94
vec3< T > & Normalize()
Normalize this vector.
Definition: SVector3.h:245
vec3< f64 > vec3d
Definition: SVector3.h:635
friend vec3< T > ComponentMaximum(vec3< T > const &lhs, vec3< T > const &rhs)
Definition: SVector3.h:526
vec3(T const in)
Scalar constructor.
Definition: SVector3.h:32
T Length() const
Length of vector.
Definition: SVector3.h:147
vec3< T > operator/(vec3< T > const &v) const
Definition: SVector3.h:295
vec3< T > & operator+=(vec3< T > const &v)
Definition: SVector3.h:386
void reset()
Sets all values to 0.
Definition: SVector3.h:113
vec2< T > XZ() const
Definition: SVector3.h:617
vec3(glm::tvec3< U, glm::defaultp > const &v)
GLM constructor.
Definition: SVector3.h:43
friend vec3< T > Interpolate(vec3< T > const &lhs, vec3< T > const &rhs, Real const d)
Definition: SVector3.h:227
T X
Definition: SVector3.h:18
vec3< T > GetInterpolated(vec3< T > const &v, Real const d) const
Definition: SVector3.h:216
T operator[](int i) const
Note: Out-of-bounds access returns a dummy value without error.
Definition: SVector3.h:65
friend T Distance(vec3< T > const &lhs, vec3< T > const &rhs)
Definition: SVector3.h:195
T Dot(vec3< T > const &other) const
Definition: SVector3.h:175
friend vec3< T > Cross(vec3< T > const &lhs, vec3< T > const &rhs)
Vector cross product.
Definition: SVector3.h:553
vec3< T > & operator*=(vec3< T > const &v)
Definition: SVector3.h:404
vec3< T > GetNormalized() const
Definition: SVector3.h:237
void Transform(glm::mat4 const &mat, f32 const TranslateComponent=1)
Definition: SVector3.h:583
Helper template to enable template-dependant epsilon values.
Definition: ionComparison.h:30
vec3(vec3< U > const &vec)
Generic vec3 constructor.
Definition: SVector3.h:49