Aether3D Game Engine
Vec3.hpp
1 #ifndef VEC3_H
2 #define VEC3_H
3 
4 #include <cmath>
5 
6 namespace ae3d
7 {
12  struct Vec3
13  {
15  float x = 0;
17  float y = 0;
19  float z = 0;
20 
28  static Vec3 Cross( const Vec3& v1, const Vec3& v2 )
29  {
30  return Vec3( v1.y * v2.z - v1.z * v2.y,
31  v1.z * v2.x - v1.x * v2.z,
32  v1.x * v2.y - v1.y * v2.x );
33  }
34 
44  static Vec3 Min2( const Vec3& v1, const Vec3& v2 )
45  {
46  return Vec3( v1.x < v2.x ? v1.x : v2.x,
47  v1.y < v2.y ? v1.y : v2.y,
48  v1.z < v2.z ? v1.z : v2.z );
49  }
50 
59  static Vec3 Max2( const Vec3& v1, const Vec3& v2 )
60  {
61  return Vec3( v1.x > v2.x ? v1.x : v2.x,
62  v1.y > v2.y ? v1.y : v2.y,
63  v1.z > v2.z ? v1.z : v2.z );
64  }
65 
74  static float Distance( const Vec3& v1, const Vec3& v2 )
75  {
76  return (v2 - v1).Length();
77  }
78 
87  static float DistanceSquared( const Vec3& v1, const Vec3& v2 )
88  {
89  const Vec3 sub = v2 - v1;
90  return sub.x * sub.x + sub.y * sub.y + sub.z * sub.z;
91  }
92 
100  static float Dot( const Vec3& v1, const Vec3& v2 )
101  {
102  return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
103  }
104 
112  static Vec3 Reflect( const Vec3& vec, const Vec3& normal ) { return vec - normal * Dot( normal, vec ) * 2; }
113 
114  // Default constructor
115  Vec3() {}
116 
118  Vec3( Vec3&& v ) noexcept = default;
119 
121  Vec3( const Vec3& other ) noexcept = default;
122 
130  Vec3( float ax, float ay, float az ) noexcept : x(ax), y(ay), z(az) {}
131 
133  Vec3& operator=( Vec3&& ) noexcept = default;
134 
141  Vec3 operator/( float f ) const
142  {
143  const float inv = 1.0f / f;
144  return Vec3(x * inv, y * inv, z * inv);
145  }
146 
153  Vec3 operator/( const Vec3& v ) const
154  {
155  return Vec3( x / v.x, y / v.y, z / v.z );
156  }
157 
163  Vec3 operator-() const
164  {
165  return Vec3( -x, -y, -z );
166  }
167 
174  Vec3 operator-( const Vec3& v ) const
175  {
176  return Vec3( x - v.x, y - v.y, z - v.z );
177  }
178 
185  Vec3 operator+( const Vec3& v ) const
186  {
187  return Vec3( x + v.x, y + v.y, z + v.z );
188  }
189 
196  Vec3 operator+( float f ) const
197  {
198  return Vec3( x + f, y + f, z + f );
199  }
200 
207  Vec3& operator-=( const Vec3& v )
208  {
209  x -= v.x;
210  y -= v.y;
211  z -= v.z;
212 
213  return *this;
214  }
215 
222  Vec3& operator+=( const Vec3& v )
223  {
224  x += v.x;
225  y += v.y;
226  z += v.z;
227 
228  return *this;
229  }
230 
237  Vec3& operator*=( float f )
238  {
239  x *= f;
240  y *= f;
241  z *= f;
242 
243  return *this;
244  }
245 
252  Vec3& operator/=( float f )
253  {
254  const float inv = 1.0f / f;
255 
256  x *= inv;
257  y *= inv;
258  z *= inv;
259 
260  return *this;
261  }
262 
269  Vec3& operator*=( const Vec3& v )
270  {
271  x *= v.x;
272  y *= v.y;
273  z *= v.z;
274 
275  return *this;
276  }
277 
284  Vec3& operator=( const Vec3& v ) { x = v.x; y = v.y; z = v.z; return *this; }
285 
292  Vec3 operator*(const Vec3& v) const
293  {
294  return Vec3( x * v.x, y * v.y, z * v.z );
295  }
296 
303  Vec3 operator*( float f ) const
304  {
305  return Vec3( x * f, y * f, z * f );
306  }
307 
311  Vec3 Normalized() const
312  {
313  //const float len = Length();
314  //return *this * (1.0f / len);
315  Vec3 out = *this;
316 
317  const float len = Length();
318 
319  if( std::abs( len ) < 0.0001f )
320  {
321  out.x = 1.0f;
322  out.y = 0.0f;
323  out.z = 0.0f;
324  return out;
325  }
326 
327  out *= (1.0f / len);
328  return out;
329 
330  }
331 
335  bool IsAlmost( const Vec3& v2 ) const
336  {
337  const float epsilon = 0.0001f;
338  return std::abs( x - v2.x ) < epsilon &&
339  std::abs( y - v2.y ) < epsilon &&
340  std::abs( z - v2.z ) < epsilon;
341  }
342 
344  float Length() const { return std::sqrt( x * x + y * y + z * z ); }
345 
347  void Zero() { x = y = z = 0; }
348  };
349 
351  struct Vec4
352  {
354  float x = 0;
356  float y = 0;
358  float z = 0;
360  float w = 0;
361 
362  Vec4() {}
363 
372  Vec4( float ax, float ay, float az, float aw ) : x( ax ), y( ay ), z( az ), w( aw ) {}
373 
375  Vec4( Vec4&& v ) noexcept = default;
376 
378  Vec4( const Vec4& other ) noexcept = default;
379 
381  explicit Vec4(const Vec3& v) : x(v.x), y(v.y), z(v.z), w(1) {}
382 
389  Vec4( const Vec3& v, float aW ) : x( v.x ), y( v.y ), z( v.z ), w(aW) {}
390 
394  bool IsAlmost( const Vec4& v2 ) const
395  {
396  const float epsilon = 0.0001f;
397  return std::abs( x - v2.x ) < epsilon &&
398  std::abs( y - v2.y ) < epsilon &&
399  std::abs( z - v2.z ) < epsilon &&
400  std::abs( w - v2.w ) < epsilon;
401  }
402 
409  Vec4& operator=( const Vec4& v ) { x = v.x; y = v.y; z = v.z; w = v.w; return *this; }
410 
417  Vec4 operator*( float f ) const
418  {
419  return Vec4(x * f, y * f, z * f, w * f);
420  }
421 
427  Vec4& operator+=( const Vec4& v )
428  {
429  x += v.x;
430  y += v.y;
431  z += v.z;
432 
433  return *this;
434  }
435 
441  Vec4& operator-=( const Vec4& v )
442  {
443  x -= v.x;
444  y -= v.y;
445  z -= v.z;
446 
447  return *this;
448  }
449 
456  float Dot( const Vec4& v ) const
457  {
458  return x * v.x + y * v.y + z * v.z + w * v.w;
459  }
460 
462  float Length() const { return std::sqrt( x * x + y * y + z * z ); }
463 
467  void Normalize()
468  {
469  const float len = Length();
470 
471  if (std::abs( len ) < 0.0001f)
472  {
473  x = 1;
474  y = 0;
475  z = 0;
476  return;
477  }
478 
479  const float iLength = 1 / len;
480 
481  x *= iLength;
482  y *= iLength;
483  z *= iLength;
484  }
485  };
486 }
487 #endif
Vec3 & operator+=(const Vec3 &v)
Definition: Vec3.hpp:222
static Vec3 Reflect(const Vec3 &vec, const Vec3 &normal)
Definition: Vec3.hpp:112
float w
W coordinate.
Definition: Vec3.hpp:360
Vec3(float ax, float ay, float az) noexcept
Definition: Vec3.hpp:130
Vec4 operator*(float f) const
Definition: Vec3.hpp:417
float x
X coordinate.
Definition: Vec3.hpp:15
Vec3 operator+(float f) const
Definition: Vec3.hpp:196
static float DistanceSquared(const Vec3 &v1, const Vec3 &v2)
Definition: Vec3.hpp:87
Vec3 operator/(float f) const
Definition: Vec3.hpp:141
Definition: AudioClip.hpp:4
static Vec3 Min2(const Vec3 &v1, const Vec3 &v2)
Definition: Vec3.hpp:44
4-component vector.
Definition: Vec3.hpp:351
Vec3 & operator=(const Vec3 &v)
Definition: Vec3.hpp:284
Vec3 operator*(const Vec3 &v) const
Definition: Vec3.hpp:292
float x
X coordinate.
Definition: Vec3.hpp:354
static Vec3 Max2(const Vec3 &v1, const Vec3 &v2)
Definition: Vec3.hpp:59
Vec3 operator+(const Vec3 &v) const
Definition: Vec3.hpp:185
float Dot(const Vec4 &v) const
Definition: Vec3.hpp:456
Vec3 & operator*=(float f)
Definition: Vec3.hpp:237
Vec3 & operator*=(const Vec3 &v)
Definition: Vec3.hpp:269
float y
Y coordinate.
Definition: Vec3.hpp:356
float Length() const
Definition: Vec3.hpp:462
static float Distance(const Vec3 &v1, const Vec3 &v2)
Definition: Vec3.hpp:74
3-component vector.
Definition: Vec3.hpp:12
Vec3 operator*(float f) const
Definition: Vec3.hpp:303
Vec3 & operator/=(float f)
Definition: Vec3.hpp:252
Vec3 operator-(const Vec3 &v) const
Definition: Vec3.hpp:174
Vec4 & operator-=(const Vec4 &v)
Definition: Vec3.hpp:441
float z
Z coordinate.
Definition: Vec3.hpp:19
Vec4(const Vec3 &v, float aW)
Definition: Vec3.hpp:389
static Vec3 Cross(const Vec3 &v1, const Vec3 &v2)
Cross product.
Definition: Vec3.hpp:28
Vec3 operator/(const Vec3 &v) const
Definition: Vec3.hpp:153
Vec3 & operator-=(const Vec3 &v)
Definition: Vec3.hpp:207
Vec3 operator-() const
Definition: Vec3.hpp:163
Vec4(const Vec3 &v)
Definition: Vec3.hpp:381
bool IsAlmost(const Vec3 &v2) const
Definition: Vec3.hpp:335
Vec4(float ax, float ay, float az, float aw)
Definition: Vec3.hpp:372
Vec4 & operator=(const Vec4 &v)
Definition: Vec3.hpp:409
static float Dot(const Vec3 &v1, const Vec3 &v2)
Definition: Vec3.hpp:100
Vec4 & operator+=(const Vec4 &v)
Definition: Vec3.hpp:427
void Normalize()
Definition: Vec3.hpp:467
Vec3 & operator=(Vec3 &&) noexcept=default
Move assignment.
Vec3 Normalized() const
Definition: Vec3.hpp:311
float y
Y coordinate.
Definition: Vec3.hpp:17
float Length() const
Definition: Vec3.hpp:344
bool IsAlmost(const Vec4 &v2) const
Definition: Vec3.hpp:394
float z
Z coordinate.
Definition: Vec3.hpp:358
void Zero()
Resets this vector&#39;s values to 0.
Definition: Vec3.hpp:347