vector3D.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. w = 1;
  33. return *this;
  34. }
  35. //Scalar-vector operations
  36. Vector3 operator-()
  37. {return Vector3(-x, -y, -z);}
  38. Vector3 operator*(const T &rhs) const //Scalar-vector multiplication
  39. {return Vector3(x*rhs, y*rhs, z*rhs);}
  40. Vector3 operator+(const T &rhs) const //Scalar-vector multiplication
  41. {return Vector3(x+rhs, y+rhs, z+rhs);}
  42. //Vector-vector operations
  43. Vector3 operator-(const Vector3 &rhs) const
  44. {return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);}
  45. Vector3 operator+(const Vector3 &rhs) const
  46. {return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);}
  47. Vector3 operator*(const Vector3 &rhs) const
  48. {return Vector3(x * rhs.x, y * rhs.y, z * rhs.z);}
  49. Vector3 crossProduct(const Vector3 &r) const
  50. {return Vector3( (y*r.z - z*r.y), (z*r.x - x*r.z), (x*r.y - y*r.x) );}
  51. T dotProduct(const Vector3 &rhs) const
  52. {return x*rhs.x + y*rhs.y + z*rhs.z;}
  53. T dot2D(const Vector3 &rhs) const
  54. {return x*rhs.x + y*rhs.y;}
  55. T length() const
  56. {return std::sqrt(x*x + y*y + z*z);}
  57. Vector3 &normalized()
  58. {
  59. T len = length();
  60. if( len > 0){
  61. T factor = 1 / len;
  62. x *= factor;
  63. y *= factor;
  64. z *= factor;
  65. }
  66. else {
  67. printf("WARNING: Attempting to normalize a vector of length 0.\n");
  68. }
  69. return *this;
  70. }
  71. static Vector3 reflect(const Vector3 &I, const Vector3 &N){
  72. return I - ((N * I.dotProduct(N)) * 2.0f);
  73. }
  74. //Print for debugging purposes
  75. void print() const{
  76. std::string str;
  77. if(std::is_same<T,float>::value){
  78. str = "Vecf:(%f, %f, %f)\n";
  79. }
  80. else if(std::is_same<T,int>::value) {
  81. str = "Veci:(%d, %d, %d)\n";
  82. }
  83. printf(str.c_str(),x,y,z);
  84. }
  85. };
  86. //Shorthands for the common vector types we use
  87. typedef Vector3<float> Vector3f;
  88. typedef Vector3<int> Vector3i;
  89. #endif