vector3D.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef VECTOR3D_H
  2. #define VECTOR3D_H
  3. #include <string>
  4. #include <type_traits>
  5. #include "math.h"
  6. //Basic 3D vector class for general calculations
  7. template<typename T>
  8. struct Vector3{
  9. //I know this is undefined behaviour and really not recommended but
  10. //Using an operator overload for looping was slowing down my program 20%
  11. union {
  12. //Anonymous struct and array union hack
  13. T data[3];
  14. struct {
  15. T x;
  16. T y;
  17. T z;
  18. };
  19. };
  20. //W component only ever used in perspective projection and clipping,
  21. //Just imagine it isn't here
  22. T w;
  23. //Constructors
  24. Vector3(): x(0), y(0), z(0), w(1) {};
  25. Vector3(T val): x(val), y(val), z(val), w(1) {};
  26. Vector3(T x1, T y1, T z1) : x(x1), y(y1), z(z1), w(1) {};
  27. //Homogenous coordinates to cartesian transformation
  28. Vector3 &perspectiveDivide(){
  29. x /= w;
  30. y /= w;
  31. z /= w;
  32. return *this;
  33. }
  34. //Scalar-vector operations
  35. Vector3 operator-() const
  36. {return Vector3(-x, -y, -z);}
  37. Vector3 operator*(const T &rhs) const //Scalar-vector multiplication
  38. {return Vector3(x*rhs, y*rhs, z*rhs);}
  39. Vector3 operator+(const T &rhs) const //Scalar-vector addition
  40. {return Vector3(x+rhs, y+rhs, z+rhs);}
  41. //Vector-vector operations
  42. Vector3 operator-(const Vector3 &rhs) const
  43. {return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);}
  44. Vector3 operator+(const Vector3 &rhs) const
  45. {return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);}
  46. void operator+=(const Vector3 &rhs)
  47. {(*this) = (*this) + rhs ;}
  48. Vector3 operator*(const Vector3 &rhs) const
  49. {return Vector3(x * rhs.x, y * rhs.y, z * rhs.z);}
  50. Vector3 crossProduct(const Vector3 &r) const
  51. {return Vector3( (y*r.z - z*r.y), (z*r.x - x*r.z), (x*r.y - y*r.x) );}
  52. T dotProduct(const Vector3 &rhs) const
  53. {return x*rhs.x + y*rhs.y + z*rhs.z;}
  54. T dot2D(const Vector3 &rhs) const
  55. {return x*rhs.x + y*rhs.y;}
  56. T length() const
  57. {return std::sqrt(x*x + y*y + z*z);}
  58. Vector3 &normalized()
  59. {
  60. T len = length();
  61. if( len > 0){
  62. T factor = 1 / len;
  63. x *= factor;
  64. y *= factor;
  65. z *= factor;
  66. }
  67. else {
  68. printf("WARNING: Attempting to normalize a vector of length 0.\n");
  69. }
  70. return *this;
  71. }
  72. static Vector3 reflect(const Vector3 &I, const Vector3 &N){
  73. return I - ((N * I.dotProduct(N)) * 2.0f);
  74. }
  75. //Print for debugging purposes
  76. void print() const{
  77. std::string str;
  78. if(std::is_same<T,float>::value){
  79. str = "Vecf:(%f, %f, %f)\n";
  80. }
  81. else if(std::is_same<T,int>::value) {
  82. str = "Veci:(%d, %d, %d)\n";
  83. }
  84. printf(str.c_str(),x,y,z);
  85. }
  86. void zero() {
  87. x = 0;
  88. y = 0;
  89. z = 0;
  90. }
  91. };
  92. //Shorthands for the common vector types we use
  93. typedef Vector3<float> Vector3f;
  94. typedef Vector3<int> Vector3i;
  95. #endif