2
0

IceHPoint.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for homogeneous points.
  4. * \file IceHPoint.h
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICEHPOINT_H__
  12. #define __ICEHPOINT_H__
  13. class ICEMATHS_API HPoint : public Point
  14. {
  15. public:
  16. //! Empty constructor
  17. inline_ HPoint() {}
  18. //! Constructor from floats
  19. inline_ HPoint(float _x, float _y, float _z, float _w=0.0f) : Point(_x, _y, _z), w(_w) {}
  20. //! Constructor from array
  21. inline_ HPoint(const float f[4]) : Point(f), w(f[3]) {}
  22. //! Constructor from a Point
  23. inline_ HPoint(const Point& p, float _w=0.0f) : Point(p), w(_w) {}
  24. //! Destructor
  25. inline_ ~HPoint() {}
  26. //! Clear the point
  27. inline_ HPoint& Zero() { x = y = z = w = 0.0f; return *this; }
  28. //! Assignment from values
  29. inline_ HPoint& Set(float _x, float _y, float _z, float _w ) { x = _x; y = _y; z = _z; w = _w; return *this; }
  30. //! Assignment from array
  31. inline_ HPoint& Set(const float f[4]) { x = f[_X]; y = f[_Y]; z = f[_Z]; w = f[_W]; return *this; }
  32. //! Assignment from another h-point
  33. inline_ HPoint& Set(const HPoint& src) { x = src.x; y = src.y; z = src.z; w = src.w; return *this; }
  34. //! Add a vector
  35. inline_ HPoint& Add(float _x, float _y, float _z, float _w ) { x += _x; y += _y; z += _z; w += _w; return *this; }
  36. //! Add a vector
  37. inline_ HPoint& Add(const float f[4]) { x += f[_X]; y += f[_Y]; z += f[_Z]; w += f[_W]; return *this; }
  38. //! Subtract a vector
  39. inline_ HPoint& Sub(float _x, float _y, float _z, float _w ) { x -= _x; y -= _y; z -= _z; w -= _w; return *this; }
  40. //! Subtract a vector
  41. inline_ HPoint& Sub(const float f[4]) { x -= f[_X]; y -= f[_Y]; z -= f[_Z]; w -= f[_W]; return *this; }
  42. //! Multiplies by a scalar
  43. inline_ HPoint& Mul(float s) { x *= s; y *= s; z *= s; w *= s; return *this; }
  44. //! Returns MIN(x, y, z, w);
  45. float Min() const { return MIN(x, MIN(y, MIN(z, w))); }
  46. //! Returns MAX(x, y, z, w);
  47. float Max() const { return MAX(x, MAX(y, MAX(z, w))); }
  48. //! Sets each element to be componentwise minimum
  49. HPoint& Min(const HPoint& p) { x = MIN(x, p.x); y = MIN(y, p.y); z = MIN(z, p.z); w = MIN(w, p.w); return *this; }
  50. //! Sets each element to be componentwise maximum
  51. HPoint& Max(const HPoint& p) { x = MAX(x, p.x); y = MAX(y, p.y); z = MAX(z, p.z); w = MAX(w, p.w); return *this; }
  52. //! Computes square magnitude
  53. inline_ float SquareMagnitude() const { return x*x + y*y + z*z + w*w; }
  54. //! Computes magnitude
  55. inline_ float Magnitude() const { return sqrtf(x*x + y*y + z*z + w*w); }
  56. //! Normalize the vector
  57. inline_ HPoint& Normalize()
  58. {
  59. float M = Magnitude();
  60. if(M)
  61. {
  62. M = 1.0f / M;
  63. x *= M;
  64. y *= M;
  65. z *= M;
  66. w *= M;
  67. }
  68. return *this;
  69. }
  70. // Arithmetic operators
  71. //! Operator for HPoint Negate = - HPoint;
  72. inline_ HPoint operator-() const { return HPoint(-x, -y, -z, -w); }
  73. //! Operator for HPoint Plus = HPoint + HPoint;
  74. inline_ HPoint operator+(const HPoint& p) const { return HPoint(x + p.x, y + p.y, z + p.z, w + p.w); }
  75. //! Operator for HPoint Minus = HPoint - HPoint;
  76. inline_ HPoint operator-(const HPoint& p) const { return HPoint(x - p.x, y - p.y, z - p.z, w - p.w); }
  77. //! Operator for HPoint Mul = HPoint * HPoint;
  78. inline_ HPoint operator*(const HPoint& p) const { return HPoint(x * p.x, y * p.y, z * p.z, w * p.w); }
  79. //! Operator for HPoint Scale = HPoint * float;
  80. inline_ HPoint operator*(float s) const { return HPoint(x * s, y * s, z * s, w * s); }
  81. //! Operator for HPoint Scale = float * HPoint;
  82. inline_ friend HPoint operator*(float s, const HPoint& p) { return HPoint(s * p.x, s * p.y, s * p.z, s * p.w); }
  83. //! Operator for HPoint Div = HPoint / HPoint;
  84. inline_ HPoint operator/(const HPoint& p) const { return HPoint(x / p.x, y / p.y, z / p.z, w / p.w); }
  85. //! Operator for HPoint Scale = HPoint / float;
  86. inline_ HPoint operator/(float s) const { s = 1.0f / s; return HPoint(x * s, y * s, z * s, w * s); }
  87. //! Operator for HPoint Scale = float / HPoint;
  88. inline_ friend HPoint operator/(float s, const HPoint& p) { return HPoint(s / p.x, s / p.y, s / p.z, s / p.w); }
  89. //! Operator for float DotProd = HPoint | HPoint;
  90. inline_ float operator|(const HPoint& p) const { return x*p.x + y*p.y + z*p.z + w*p.w; }
  91. // No cross-product in 4D
  92. //! Operator for HPoint += HPoint;
  93. inline_ HPoint& operator+=(const HPoint& p) { x += p.x; y += p.y; z += p.z; w += p.w; return *this; }
  94. //! Operator for HPoint += float;
  95. inline_ HPoint& operator+=(float s) { x += s; y += s; z += s; w += s; return *this; }
  96. //! Operator for HPoint -= HPoint;
  97. inline_ HPoint& operator-=(const HPoint& p) { x -= p.x; y -= p.y; z -= p.z; w -= p.w; return *this; }
  98. //! Operator for HPoint -= float;
  99. inline_ HPoint& operator-=(float s) { x -= s; y -= s; z -= s; w -= s; return *this; }
  100. //! Operator for HPoint *= HPoint;
  101. inline_ HPoint& operator*=(const HPoint& p) { x *= p.x; y *= p.y; z *= p.z; w *= p.w; return *this; }
  102. //! Operator for HPoint *= float;
  103. inline_ HPoint& operator*=(float s) { x*=s; y*=s; z*=s; w*=s; return *this; }
  104. //! Operator for HPoint /= HPoint;
  105. inline_ HPoint& operator/=(const HPoint& p) { x /= p.x; y /= p.y; z /= p.z; w /= p.w; return *this; }
  106. //! Operator for HPoint /= float;
  107. inline_ HPoint& operator/=(float s) { s = 1.0f / s; x*=s; y*=s; z*=s; w*=s; return *this; }
  108. // Arithmetic operators
  109. //! Operator for Point Mul = HPoint * Matrix3x3;
  110. Point operator*(const Matrix3x3& mat) const;
  111. //! Operator for HPoint Mul = HPoint * Matrix4x4;
  112. HPoint operator*(const Matrix4x4& mat) const;
  113. // HPoint *= Matrix3x3 doesn't exist, the matrix is first casted to a 4x4
  114. //! Operator for HPoint *= Matrix4x4
  115. HPoint& operator*=(const Matrix4x4& mat);
  116. // Logical operators
  117. //! Operator for "if(HPoint==HPoint)"
  118. inline_ bool operator==(const HPoint& p) const { return ( (x==p.x)&&(y==p.y)&&(z==p.z)&&(w==p.w)); }
  119. //! Operator for "if(HPoint!=HPoint)"
  120. inline_ bool operator!=(const HPoint& p) const { return ( (x!=p.x)||(y!=p.y)||(z!=p.z)||(w!=p.w)); }
  121. // Cast operators
  122. //! Cast a HPoint to a Point. w is discarded.
  123. inline_ operator Point() const { return Point(x, y, z); }
  124. public:
  125. float w;
  126. };
  127. #endif // __ICEHPOINT_H__