2 * ICE / OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 * Contains code for planes.
21 * \author Pierre Terdiman
22 * \date April, 4, 2000
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 #ifndef __ICEPLANE_H__
29 #define __ICEPLANE_H__
31 #define PLANE_EPSILON (1.0e-7f)
33 class ICEMATHS_API Plane
38 //! Constructor from a normal and a distance
39 inline_ Plane(float nx, float ny, float nz, float d) { Set(nx, ny, nz, d); }
40 //! Constructor from a point on the plane and a normal
41 inline_ Plane(const Point& p, const Point& n) { Set(p, n); }
42 //! Constructor from three points
43 inline_ Plane(const Point& p0, const Point& p1, const Point& p2) { Set(p0, p1, p2); }
44 //! Constructor from a normal and a distance
45 inline_ Plane(const Point& _n, float _d) { n = _n; d = _d; }
47 inline_ Plane(const Plane& plane) : n(plane.n), d(plane.d) { }
51 inline_ Plane& Zero() { n.Zero(); d = 0.0f; return *this; }
52 inline_ Plane& Set(float nx, float ny, float nz, float _d) { n.Set(nx, ny, nz); d = _d; return *this; }
53 inline_ Plane& Set(const Point& p, const Point& _n) { n = _n; d = - p | _n; return *this; }
54 Plane& Set(const Point& p0, const Point& p1, const Point& p2);
56 inline_ float Distance(const Point& p) const { return (p | n) + d; }
57 inline_ bool Belongs(const Point& p) const { return fabsf(Distance(p)) < PLANE_EPSILON; }
59 inline_ void Normalize()
61 float Denom = 1.0f / n.Magnitude();
69 Point n; //!< The normal to the plane
70 float d; //!< The distance from the origin
73 inline_ operator Point() const { return n; }
74 inline_ operator HPoint() const { return HPoint(n, d); }
76 // Arithmetic operators
77 inline_ Plane operator*(const Matrix4x4& m) const
79 // Old code from Irion. Kept for reference.
84 inline_ Plane& operator*=(const Matrix4x4& m)
86 // Old code from Irion. Kept for reference.
87 Point n2 = HPoint(n, 0.0f) * m;
88 d = -((Point) (HPoint( -d*n, 1.0f ) * m) | n2);
94 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96 * Transforms a plane by a 4x4 matrix. Same as Plane * Matrix4x4 operator, but faster.
97 * \param transformed [out] transformed plane
98 * \param plane [in] source plane
99 * \param transform [in] transform matrix
100 * \warning the plane normal must be unit-length
102 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 inline_ void TransformPlane(Plane& transformed, const Plane& plane, const Matrix4x4& transform)
105 // Rotate the normal using the rotation part of the 4x4 matrix
106 transformed.n = plane.n * Matrix3x3(transform);
109 transformed.d = plane.d - (Point(transform.GetTrans())|transformed.n);
112 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
114 * Transforms a plane by a 4x4 matrix. Same as Plane * Matrix4x4 operator, but faster.
115 * \param plane [in/out] source plane (transformed on return)
116 * \param transform [in] transform matrix
117 * \warning the plane normal must be unit-length
119 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120 inline_ void TransformPlane(Plane& plane, const Matrix4x4& transform)
122 // Rotate the normal using the rotation part of the 4x4 matrix
123 plane.n *= Matrix3x3(transform);
126 plane.d -= Point(transform.GetTrans())|plane.n;
129 #endif // __ICEPLANE_H__