Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / Ice / IcePlane.h
1 /*
2  *      ICE / OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
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:
12
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.
16 */
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18 /**
19  *      Contains code for planes.
20  *      \file           IcePlane.h
21  *      \author         Pierre Terdiman
22  *      \date           April, 4, 2000
23  */
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 // Include Guard
28 #ifndef __ICEPLANE_H__
29 #define __ICEPLANE_H__
30
31         #define PLANE_EPSILON           (1.0e-7f)
32
33         class ICEMATHS_API Plane
34         {
35                 public:
36                 //! Constructor
37                 inline_                 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;                                                               }
46                 //! Copy constructor
47                 inline_                 Plane(const Plane& plane) : n(plane.n), d(plane.d)                              {                                                                                               }
48                 //! Destructor
49                 inline_                 ~Plane()                                                                                                                {                                                                                               }
50
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);
55
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;    }
58
59                 inline_ void    Normalize()
60                                                 {
61                                                         float Denom = 1.0f / n.Magnitude();
62                                                         n.x     *= Denom;
63                                                         n.y     *= Denom;
64                                                         n.z     *= Denom;
65                                                         d       *= Denom;
66                                                 }
67                 public:
68                 // Members
69                                 Point   n;              //!< The normal to the plane
70                                 float   d;              //!< The distance from the origin
71
72                 // Cast operators
73                 inline_                 operator Point()                                        const                                           { return n;                                                                             }
74                 inline_                 operator HPoint()                                       const                                           { return HPoint(n, d);                                                  }
75
76                 // Arithmetic operators
77                 inline_ Plane   operator*(const Matrix4x4& m)           const
78                                                 {
79                                                         // Old code from Irion. Kept for reference.
80                                                         Plane Ret(*this);
81                                                         return Ret *= m;
82                                                 }
83
84                 inline_ Plane&  operator*=(const Matrix4x4& m)
85                                                 {
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);
89                                                         n = n2;
90                                                         return *this;
91                                                 }
92         };
93
94         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
95         /**
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
101          */
102         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103         inline_ void TransformPlane(Plane& transformed, const Plane& plane, const Matrix4x4& transform)
104         {
105                 // Rotate the normal using the rotation part of the 4x4 matrix
106                 transformed.n = plane.n * Matrix3x3(transform);
107
108                 // Compute new d
109                 transformed.d = plane.d - (Point(transform.GetTrans())|transformed.n);
110         }
111
112         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113         /**
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
118          */
119         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120         inline_ void TransformPlane(Plane& plane, const Matrix4x4& transform)
121         {
122                 // Rotate the normal using the rotation part of the 4x4 matrix
123                 plane.n *= Matrix3x3(transform);
124
125                 // Compute new d
126                 plane.d -= Point(transform.GetTrans())|plane.n;
127         }
128
129 #endif // __ICEPLANE_H__