Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Public Member Functions | Public Attributes | Static Public Attributes | Friends | List of all members
Eule::Vector3< T > Class Template Reference

Representation of a 3d vector. More...

#include <Matrix4x4.h>

Collaboration diagram for Eule::Vector3< T >:
Collaboration graph
[legend]

Public Member Functions

 Vector3 ()
 
 Vector3 (T _x, T _y, T _z)
 
 Vector3 (const Vector3< T > &other)=default
 
 Vector3 (Vector3< T > &&other) noexcept=default
 
double DotProduct (const Vector3< T > &other) const
 Will compute the dot product to another Vector3. More...
 
Vector3< double > CrossProduct (const Vector3< T > &other) const
 Will compute the cross product to another Vector3. More...
 
double SqrMagnitude () const
 Will compute the square magnitude. More...
 
double Magnitude () const
 Will compute the magnitude. More...
 
Vector3< double > Normalize () const
 Will return the normalization of this vector. More...
 
void NormalizeSelf ()
 Will normalize this vector. More...
 
Vector3< T > VectorScale (const Vector3< T > &scalar) const
 Will scale self.n by scalar.n. More...
 
void LerpSelf (const Vector3< T > &other, double t)
 Will lerp itself towards other by t. More...
 
Vector3< double > Lerp (const Vector3< T > &other, double t) const
 Will return a lerp result between this and another vector. More...
 
bool Similar (const Vector3< T > &other, double epsilon=0.00001) const
 Will compare if two vectors are similar to a certain epsilon value. More...
 
Vector3< int > ToInt () const
 Will convert this vector to a Vector3i. More...
 
Vector3< double > ToDouble () const
 Will convert this vector to a Vector3d. More...
 
T & operator[] (std::size_t idx)
 
const T & operator[] (std::size_t idx) const
 
Vector3< T > operator+ (const Vector3< T > &other) const
 
void operator+= (const Vector3< T > &other)
 
Vector3< T > operator- (const Vector3< T > &other) const
 
void operator-= (const Vector3< T > &other)
 
Vector3< T > operator* (const T scale) const
 
void operator*= (const T scale)
 
Vector3< T > operator/ (const T scale) const
 
void operator/= (const T scale)
 
Vector3< T > operator* (const Matrix4x4 &mat) const
 
void operator*= (const Matrix4x4 &mat)
 
Vector3< T > operator- () const
 
 operator Vector2< T > () const
 
 operator Vector4< T > () const
 Conversion method. More...
 
void operator= (const Vector3< T > &other)
 Conversion method. More...
 
void operator= (Vector3< T > &&other) noexcept
 
bool operator== (const Vector3< T > &other) const
 
bool operator!= (const Vector3< T > &other) const
 

Public Attributes

x
 
y
 
z
 

Static Public Attributes

static const Vector3< double > up
 
static const Vector3< double > down
 
static const Vector3< double > right
 
static const Vector3< double > left
 
static const Vector3< double > forward
 
static const Vector3< double > backward
 
static const Vector3< double > one
 
static const Vector3< double > zero
 

Friends

std::ostream & operator<< (std::ostream &os, const Vector3< T > &v)
 
std::wostream & operator<< (std::wostream &os, const Vector3< T > &v)
 

Detailed Description

template<typename T>
class Eule::Vector3< T >

Representation of a 3d vector.

Contains a lot of utility methods.

Definition at line 9 of file Matrix4x4.h.

Constructor & Destructor Documentation

◆ Vector3() [1/4]

template<typename T >
Eule::Vector3< T >::Vector3 ( )
inline

Definition at line 20 of file Vector3.h.

20 : x{ 0 }, y{ 0 }, z{ 0 } {}

◆ Vector3() [2/4]

template<typename T >
Eule::Vector3< T >::Vector3 ( _x,
_y,
_z 
)
inline

Definition at line 21 of file Vector3.h.

21 : x{ _x }, y{ _y }, z{ _z } {}

◆ Vector3() [3/4]

template<typename T >
Eule::Vector3< T >::Vector3 ( const Vector3< T > &  other)
default

◆ Vector3() [4/4]

template<typename T >
Eule::Vector3< T >::Vector3 ( Vector3< T > &&  other)
defaultnoexcept

Member Function Documentation

◆ CrossProduct()

template<typename T >
Vector3< double > Vector3::CrossProduct ( const Vector3< T > &  other) const

Will compute the cross product to another Vector3.

Definition at line 68 of file Vector3.cpp.

69 {
70  Vector3<double> cp;
71  cp.x = ((double)y * (double)other.z) - ((double)z * (double)other.y);
72  cp.y = ((double)z * (double)other.x) - ((double)x * (double)other.z);
73  cp.z = ((double)x * (double)other.y) - ((double)y * (double)other.x);
74 
75  return cp;
76 }

◆ DotProduct()

template<typename T >
double Vector3::DotProduct ( const Vector3< T > &  other) const

Will compute the dot product to another Vector3.

Definition at line 48 of file Vector3.cpp.

49 {
50  int iDot = (x * other.x) + (y * other.y) + (z * other.z);
51  return (double)iDot;
52 }

◆ Lerp()

template<typename T >
Vector3< double > Vector3::Lerp ( const Vector3< T > &  other,
double  t 
) const

Will return a lerp result between this and another vector.

Definition at line 330 of file Vector3.cpp.

331 {
332  Vector3d copy(this->ToDouble());
333  copy.LerpSelf(other.ToDouble(), t);
334 
335  return copy;
336 }

◆ LerpSelf()

template<typename T >
void Vector3::LerpSelf ( const Vector3< T > &  other,
double  t 
)

Will lerp itself towards other by t.

Definition at line 311 of file Vector3.cpp.

312 {
313  const double it = 1.0 - t; // Inverse t
314 
315  x = (int)(it * (double)x + t * (double)other.x);
316  y = (int)(it * (double)y + t * (double)other.y);
317  z = (int)(it * (double)z + t * (double)other.z);
318 
319  return;
320 }

◆ Magnitude()

template<typename T >
double Vector3::Magnitude

Will compute the magnitude.

Definition at line 95 of file Vector3.cpp.

96 {
97  return sqrt(SqrMagnitude());
98 }

◆ Normalize()

template<typename T >
Vector3< double > Vector3::Normalize

Will return the normalization of this vector.

Definition at line 147 of file Vector3.cpp.

148 {
149  Vector3<double> norm(x, y, z);
150  norm.NormalizeSelf();
151 
152  return norm;
153 }

◆ NormalizeSelf()

template<typename T >
void Vector3::NormalizeSelf ( )

Will normalize this vector.

Definition at line 200 of file Vector3.cpp.

201 {
202  std::cerr << "Stop normalizing int-vectors!!" << std::endl;
203  x = 0;
204  y = 0;
205  z = 0;
206 
207  return;
208 }

◆ operator Vector2< T >()

template<typename T >
Vector3::operator Vector2< T >

Definition at line 873 of file Vector3.cpp.

874 {
875  return Vector2<T>(x, y);
876 }

◆ operator Vector4< T >()

template<typename T >
Vector3::operator Vector4< T >

Conversion method.

Definition at line 879 of file Vector3.cpp.

880 {
881  return Vector4<T>(x, y, z, 0);
882 }

◆ operator!=()

template<typename T >
bool Vector3::operator!= ( const Vector3< T > &  other) const

Definition at line 864 of file Vector3.cpp.

865 {
866  return !operator==(other);
867 }

◆ operator*() [1/2]

template<typename T >
Vector3< int > Vector3::operator* ( const Matrix4x4 mat) const

Definition at line 731 of file Vector3.cpp.

732 {
733  Vector3<double> newVec;
734 
735  // Rotation, Scaling
736  newVec.x = ((mat[0][0] * x) + (mat[1][0] * y) + (mat[2][0] * z));
737  newVec.y = ((mat[0][1] * x) + (mat[1][1] * y) + (mat[2][1] * z));
738  newVec.z = ((mat[0][2] * x) + (mat[1][2] * y) + (mat[2][2] * z));
739 
740  // Translation
741  newVec.x += mat[0][3];
742  newVec.y += mat[1][3];
743  newVec.z += mat[2][3];
744 
745  return Vector3<int>(
746  (int)newVec.x,
747  (int)newVec.y,
748  (int)newVec.z
749  );
750 }

◆ operator*() [2/2]

template<typename T >
Vector3< T > Vector3::operator* ( const T  scale) const

Definition at line 541 of file Vector3.cpp.

542 {
543  return Vector3<T>(
544  x * scale,
545  y * scale,
546  z * scale
547  );
548 }

◆ operator*=() [1/2]

template<typename T >
void Vector3::operator*= ( const Matrix4x4 mat)

Definition at line 836 of file Vector3.cpp.

837 {
838  Vector3<double> buffer(x, y, z);
839 
840  x = (int)((mat[0][0] * buffer.x) + (mat[0][1] * buffer.y) + (mat[0][2] * buffer.z));
841  y = (int)((mat[1][0] * buffer.x) + (mat[1][1] * buffer.y) + (mat[1][2] * buffer.z));
842  z = (int)((mat[2][0] * buffer.x) + (mat[2][1] * buffer.y) + (mat[2][2] * buffer.z));
843 
844  // Translation
845  x += (int)mat[0][3];
846  y += (int)mat[1][3];
847  z += (int)mat[2][3];
848 
849  return;
850 }

◆ operator*=() [2/2]

template<typename T >
void Vector3::operator*= ( const T  scale)

Definition at line 583 of file Vector3.cpp.

584 {
585  x *= scale;
586  y *= scale;
587  z *= scale;
588  return;
589 }

◆ operator+()

template<typename T >
Vector3< T > Vector3::operator+ ( const Vector3< T > &  other) const

Definition at line 372 of file Vector3.cpp.

373 {
374  return Vector3<T>(
375  x + other.x,
376  y + other.y,
377  z + other.z
378  );
379 }

◆ operator+=()

template<typename T >
void Vector3::operator+= ( const Vector3< T > &  other)

Definition at line 414 of file Vector3.cpp.

415 {
416  x += other.x;
417  y += other.y;
418  z += other.z;
419  return;
420 }

◆ operator-() [1/2]

template<typename T >
Vector3< T > Vector3::operator-

Definition at line 806 of file Vector3.cpp.

807 {
808  return Vector3<T>(
809  -x,
810  -y,
811  -z
812  );
813 }

◆ operator-() [2/2]

template<typename T >
Vector3< T > Vector3::operator- ( const Vector3< T > &  other) const

Definition at line 456 of file Vector3.cpp.

457 {
458  return Vector3<T>(
459  x - other.x,
460  y - other.y,
461  z - other.z
462  );
463 }

◆ operator-=()

template<typename T >
void Vector3::operator-= ( const Vector3< T > &  other)

Definition at line 498 of file Vector3.cpp.

499 {
500  x -= other.x;
501  y -= other.y;
502  z -= other.z;
503  return;
504 }

◆ operator/()

template<typename T >
Vector3< T > Vector3::operator/ ( const T  scale) const

Definition at line 626 of file Vector3.cpp.

627 {
628  return Vector3<T>(
629  x / scale,
630  y / scale,
631  z / scale
632  );
633 }

◆ operator/=()

template<typename T >
void Vector3::operator/= ( const T  scale)

Definition at line 667 of file Vector3.cpp.

668 {
669  x /= scale;
670  y /= scale;
671  z /= scale;
672  return;
673 }

◆ operator=() [1/2]

template<typename T >
void Vector3::operator= ( const Vector3< T > &  other)

Conversion method.

Definition at line 816 of file Vector3.cpp.

817 {
818  x = other.x;
819  y = other.y;
820  z = other.z;
821 
822  return;
823 }

◆ operator=() [2/2]

template<typename T >
void Vector3::operator= ( Vector3< T > &&  other)
noexcept

Definition at line 826 of file Vector3.cpp.

827 {
828  x = std::move(other.x);
829  y = std::move(other.y);
830  z = std::move(other.z);
831 
832  return;
833 }

◆ operator==()

template<typename T >
bool Vector3::operator== ( const Vector3< T > &  other) const

Definition at line 855 of file Vector3.cpp.

856 {
857  return
858  (x == other.x) &&
859  (y == other.y) &&
860  (z == other.z);
861 }

◆ operator[]() [1/2]

template<typename T >
T & Vector3::operator[] ( std::size_t  idx)

Definition at line 235 of file Vector3.cpp.

236 {
237  switch (idx)
238  {
239  case 0:
240  return x;
241  case 1:
242  return y;
243  case 2:
244  return z;
245  default:
246  throw std::out_of_range("Array descriptor on Vector3<T> out of range!");
247  }
248 }

◆ operator[]() [2/2]

template<typename T >
const T & Vector3::operator[] ( std::size_t  idx) const

Definition at line 251 of file Vector3.cpp.

252 {
253  switch (idx)
254  {
255  case 0:
256  return x;
257  case 1:
258  return y;
259  case 2:
260  return z;
261  default:
262  throw std::out_of_range("Array descriptor on Vector3<T> out of range!");
263  }
264 }

◆ Similar()

template<typename T >
bool Vector3::Similar ( const Vector3< T > &  other,
double  epsilon = 0.00001 
) const

Will compare if two vectors are similar to a certain epsilon value.

Definition at line 213 of file Vector3.cpp.

214 {
215  return
216  (::Math::Similar(x, other.x, epsilon)) &&
217  (::Math::Similar(y, other.y, epsilon)) &&
218  (::Math::Similar(z, other.z, epsilon))
219  ;
220 }

◆ SqrMagnitude()

template<typename T >
double Vector3::SqrMagnitude ( ) const

Will compute the square magnitude.

Definition at line 88 of file Vector3.cpp.

89 {
90  int iSqrMag = x*x + y*y + z*z;
91  return (double)iSqrMag;
92 }

◆ ToDouble()

template<typename T >
Vector3< double > Vector3::ToDouble

Will convert this vector to a Vector3d.

Definition at line 229 of file Vector3.cpp.

230 {
231  return Vector3<double>((double)x, (double)y, (double)z);
232 }

◆ ToInt()

template<typename T >
Vector3< int > Vector3::ToInt

Will convert this vector to a Vector3i.

Definition at line 223 of file Vector3.cpp.

224 {
225  return Vector3<int>((int)x, (int)y, (int)z);
226 }

◆ VectorScale()

template<typename T >
Vector3< int > Vector3::VectorScale ( const Vector3< T > &  scalar) const

Will scale self.n by scalar.n.

Definition at line 135 of file Vector3.cpp.

136 {
137  return Vector3<int>(
138  x * scalar.x,
139  y * scalar.y,
140  z * scalar.z
141  );
142 }

Friends And Related Function Documentation

◆ operator<< [1/2]

template<typename T >
std::ostream& operator<< ( std::ostream &  os,
const Vector3< T > &  v 
)
friend

Definition at line 85 of file Vector3.h.

86  {
87  return os << "[x: " << v.x << " y: " << v.y << " z: " << v.z << "]";
88  }

◆ operator<< [2/2]

template<typename T >
std::wostream& operator<< ( std::wostream &  os,
const Vector3< T > &  v 
)
friend

Definition at line 89 of file Vector3.h.

90  {
91  return os << L"[x: " << v.x << L" y: " << v.y << L" z: " << v.z << L"]";
92  }

Member Data Documentation

◆ backward

template<typename T >
const Vector3< double > Vector3::backward
static

Definition at line 104 of file Vector3.h.

◆ down

template<typename T >
const Vector3< double > Vector3::down
static

Definition at line 100 of file Vector3.h.

◆ forward

template<typename T >
const Vector3< double > Vector3::forward
static

Definition at line 103 of file Vector3.h.

◆ left

template<typename T >
const Vector3< double > Vector3::left
static

Definition at line 102 of file Vector3.h.

◆ one

template<typename T >
const Vector3< double > Vector3::one
static

Definition at line 105 of file Vector3.h.

◆ right

template<typename T >
const Vector3< double > Vector3::right
static

Definition at line 101 of file Vector3.h.

◆ up

template<typename T >
const Vector3< double > Vector3::up
static

Definition at line 99 of file Vector3.h.

◆ x

template<typename T >
T Eule::Vector3< T >::x

Definition at line 94 of file Vector3.h.

◆ y

template<typename T >
T Eule::Vector3< T >::y

Definition at line 95 of file Vector3.h.

◆ z

template<typename T >
T Eule::Vector3< T >::z

Definition at line 96 of file Vector3.h.

◆ zero

template<typename T >
const Vector3< double > Vector3::zero
static

Definition at line 106 of file Vector3.h.


The documentation for this class was generated from the following files:
Eule::Vector3< double >
Eule::Vector3::ToDouble
Vector3< double > ToDouble() const
Will convert this vector to a Vector3d.
Definition: Vector3.cpp:229
Eule::Vector3::operator==
bool operator==(const Vector3< T > &other) const
Definition: Vector3.cpp:855
Eule::Vector3::z
T z
Definition: Vector3.h:96
Eule::Vector3::x
T x
Definition: Vector3.h:94
Eule::Vector2
Representation of a 2d vector.
Definition: Vector2.h:14
Eule::Vector3::SqrMagnitude
double SqrMagnitude() const
Will compute the square magnitude.
Definition: Vector3.cpp:88
Eule::Math::Similar
static constexpr bool Similar(const double a, const double b, const double epsilon=0.00001)
Compares two double values with a given accuracy.
Eule::Vector3::y
T y
Definition: Vector3.h:95
Eule::Vector4
Representation of a 4d vector.
Definition: Vector2.h:8