Plane3D.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #include "Vec3.h"
  3. namespace Framework
  4. {
  5. template<typename T>
  6. //! Stores a 3D plane in coordinate form (x*?+y*?+z*?-w=0)
  7. class Ebene3D
  8. {
  9. public:
  10. T x; //! The X component of the normal vector of the plane
  11. T y; //! The Y component of the normal vector of the plane
  12. T z; //! The Z component of the normal vector of the plane
  13. T w; //! The value the dot product of a vector with the normal
  14. //! vector must have to lie on the plane
  15. //! Constructor
  16. inline Ebene3D()
  17. : x((T)0),
  18. y((T)0),
  19. z((T)0),
  20. w((T)0)
  21. {}
  22. //! Constructor
  23. //! \param x The X component of the normal vector of the plane
  24. //! \param y The Y component of the normal vector of the plane
  25. //! \param z The Z component of the normal vector of the plane
  26. //! \param w The value the dot product of a vector with the
  27. //! normal vector must have to lie on the plane
  28. inline Ebene3D(T x, T y, T z, T w)
  29. : x(x),
  30. y(y),
  31. z(z),
  32. w(w)
  33. {}
  34. //! Constructor
  35. //! \param vect The plane whose values should be copied
  36. inline Ebene3D(const Ebene3D& vect)
  37. : Ebene3D(vect.x, vect.y, vect.z, vect.w)
  38. {}
  39. //! Normalizes the normal vector of the plane to length 1
  40. inline Ebene3D& normalize()
  41. {
  42. const T length = len();
  43. x /= length;
  44. y /= length;
  45. z /= length;
  46. w /= length;
  47. return *this;
  48. }
  49. //! Swaps the values of two planes
  50. //! \param vect The other plane
  51. inline Ebene3D& Swap(Ebene3D& vect)
  52. {
  53. const Ebene3D tmp = vect;
  54. vect = *this;
  55. *this = tmp;
  56. return *this;
  57. }
  58. //! Copies the values of another plane
  59. //! \param r The other plane
  60. inline Ebene3D operator=(const Ebene3D& r)
  61. {
  62. x = r.x;
  63. y = r.y;
  64. z = r.z;
  65. w = r.w;
  66. return *this;
  67. }
  68. //! Adds another plane
  69. //! \param r The other plane
  70. inline Ebene3D operator+=(const Ebene3D& r)
  71. {
  72. x += r.x;
  73. y += r.y;
  74. z += r.z;
  75. w += r.w;
  76. return *this;
  77. }
  78. //! Subtracts another plane
  79. //! \param r The other plane
  80. inline Ebene3D operator-=(const Ebene3D& r)
  81. {
  82. x -= r.x;
  83. y -= r.y;
  84. z -= r.z;
  85. w -= r.w;
  86. return *this;
  87. }
  88. //! Multiplies the plane by a factor
  89. //! \param r The factor
  90. inline Ebene3D operator*=(const T& r)
  91. {
  92. x *= r;
  93. y *= r;
  94. z *= r;
  95. w *= r;
  96. return *this;
  97. }
  98. //! Divides the plane by a factor
  99. //! \param r The factor
  100. inline Ebene3D operator/=(const T& r)
  101. {
  102. x /= r;
  103. y /= r;
  104. z /= r;
  105. w /= r;
  106. return *this;
  107. }
  108. template<typename T2>
  109. //! Converts the plane to another type
  110. inline operator Ebene3D<T2>() const
  111. {
  112. return {(T2)x, (T2)y, (T2)z, (T2)w};
  113. }
  114. //! Calculates the square of the length of the normal vector of the plane
  115. inline T lenSq() const
  116. {
  117. return x * x + y * y + z * z;
  118. }
  119. //! Calculates the length of the normal vector
  120. inline T len() const
  121. {
  122. return sqrt(lenSq());
  123. }
  124. //! Calculates the distance from the plane to a vector
  125. //! \param r The vector
  126. inline T operator*(const Vec3<T>& r) const
  127. {
  128. return x * r.x + y * r.y + z * r.z + w;
  129. }
  130. //! Adds two planes
  131. //! \param r The other plane
  132. inline Ebene3D operator+(const Ebene3D& r) const
  133. {
  134. return Vec4(*this) += r;
  135. }
  136. //! Subtracts two planes
  137. //! \param r The other plane
  138. inline Ebene3D operator-(const Ebene3D& r) const
  139. {
  140. return Ebene3D(*this) -= r;
  141. }
  142. //! Multiplies the plane by a factor
  143. //! r: The factor
  144. inline Ebene3D operator*(const T& r) const
  145. {
  146. return Ebene3D(*this) *= r;
  147. }
  148. //! Divides the plane by a factor
  149. //! \param r The factor
  150. inline Ebene3D operator/(const T& r) const
  151. {
  152. return Ebene3D(*this) /= r;
  153. }
  154. //! Checks two planes for equality
  155. //! \param r The other plane
  156. inline bool operator==(const Ebene3D& r) const
  157. {
  158. return x == r.x && y == r.y && z == r.z && w == r.w;
  159. }
  160. //! Checks two planes for inequality
  161. //! \param r The other plane
  162. inline bool operator!=(const Ebene3D& r) const
  163. {
  164. return !(*this == r);
  165. }
  166. inline Vec3<T> normal() const
  167. {
  168. return {x, y, z};
  169. }
  170. };
  171. } // namespace Framework