2
0

IcePlane.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for planes.
  4. * \file IcePlane.h
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICEPLANE_H__
  12. #define __ICEPLANE_H__
  13. #define PLANE_EPSILON (1.0e-7f)
  14. class ICEMATHS_API Plane
  15. {
  16. public:
  17. //! Constructor
  18. inline_ Plane() { }
  19. //! Constructor from a normal and a distance
  20. inline_ Plane(float nx, float ny, float nz, float d) { Set(nx, ny, nz, d); }
  21. //! Constructor from a point on the plane and a normal
  22. inline_ Plane(const Point& p, const Point& n) { Set(p, n); }
  23. //! Constructor from three points
  24. inline_ Plane(const Point& p0, const Point& p1, const Point& p2) { Set(p0, p1, p2); }
  25. //! Constructor from a normal and a distance
  26. inline_ Plane(const Point& _n, float _d) { n = _n; d = _d; }
  27. //! Copy constructor
  28. inline_ Plane(const Plane& plane) : n(plane.n), d(plane.d) { }
  29. //! Destructor
  30. inline_ ~Plane() { }
  31. inline_ Plane& Zero() { n.Zero(); d = 0.0f; return *this; }
  32. inline_ Plane& Set(float nx, float ny, float nz, float _d) { n.Set(nx, ny, nz); d = _d; return *this; }
  33. inline_ Plane& Set(const Point& p, const Point& _n) { n = _n; d = - p | _n; return *this; }
  34. Plane& Set(const Point& p0, const Point& p1, const Point& p2);
  35. inline_ float Distance(const Point& p) const { return (p | n) + d; }
  36. inline_ bool Belongs(const Point& p) const { return fabsf(Distance(p)) < PLANE_EPSILON; }
  37. inline_ void Normalize()
  38. {
  39. float Denom = 1.0f / n.Magnitude();
  40. n.x *= Denom;
  41. n.y *= Denom;
  42. n.z *= Denom;
  43. d *= Denom;
  44. }
  45. public:
  46. // Members
  47. Point n; //!< The normal to the plane
  48. float d; //!< The distance from the origin
  49. // Cast operators
  50. inline_ operator Point() const { return n; }
  51. inline_ operator HPoint() const { return HPoint(n, d); }
  52. // Arithmetic operators
  53. inline_ Plane operator*(const Matrix4x4& m) const
  54. {
  55. // Old code from Irion. Kept for reference.
  56. Plane Ret(*this);
  57. return Ret *= m;
  58. }
  59. inline_ Plane& operator*=(const Matrix4x4& m)
  60. {
  61. // Old code from Irion. Kept for reference.
  62. Point n2 = HPoint(n, 0.0f) * m;
  63. d = -((Point) (HPoint( -d*n, 1.0f ) * m) | n2);
  64. n = n2;
  65. return *this;
  66. }
  67. };
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. /**
  70. * Transforms a plane by a 4x4 matrix. Same as Plane * Matrix4x4 operator, but faster.
  71. * \param transformed [out] transformed plane
  72. * \param plane [in] source plane
  73. * \param transform [in] transform matrix
  74. * \warning the plane normal must be unit-length
  75. */
  76. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. inline_ void TransformPlane(Plane& transformed, const Plane& plane, const Matrix4x4& transform)
  78. {
  79. // Rotate the normal using the rotation part of the 4x4 matrix
  80. transformed.n = plane.n * Matrix3x3(transform);
  81. // Compute new d
  82. transformed.d = plane.d - (Point(transform.GetTrans())|transformed.n);
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. /**
  86. * Transforms a plane by a 4x4 matrix. Same as Plane * Matrix4x4 operator, but faster.
  87. * \param plane [in/out] source plane (transformed on return)
  88. * \param transform [in] transform matrix
  89. * \warning the plane normal must be unit-length
  90. */
  91. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. inline_ void TransformPlane(Plane& plane, const Matrix4x4& transform)
  93. {
  94. // Rotate the normal using the rotation part of the 4x4 matrix
  95. plane.n *= Matrix3x3(transform);
  96. // Compute new d
  97. plane.d -= Point(transform.GetTrans())|plane.n;
  98. }
  99. #endif // __ICEPLANE_H__