source: GTP/trunk/App/Games/Jungle_Rumble/src/physic/foundation/include/NxPlane.h @ 1378

Revision 1378, 4.3 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

Line 
1#ifndef NX_FOUNDATION_NXPLANE
2#define NX_FOUNDATION_NXPLANE
3/*----------------------------------------------------------------------------*\
4|
5|                                               Public Interface to NovodeX Technology
6|
7|                                                            www.novodex.com
8|
9\*----------------------------------------------------------------------------*/
10/** \addtogroup foundation
11  @{
12*/
13
14#include "Nxf.h"
15#include "NxVec3.h"
16#include "NxMat34.h"
17
18/**
19\brief Representation of a plane.
20
21 Plane equation used: a*x + b*y + c*z + d = 0
22*/
23class NxPlane
24        {
25        public:
26        /**
27        \brief Constructor
28        */
29        NX_INLINE NxPlane()
30                {
31                }
32
33        /**
34        \brief Constructor from a normal and a distance
35        */
36        NX_INLINE NxPlane(NxF32 nx, NxF32 ny, NxF32 nz, NxF32 d)
37                {
38                set(nx, ny, nz, d);
39                }
40
41        /**
42        \brief Constructor from a point on the plane and a normal
43        */
44        NX_INLINE NxPlane(const NxVec3& p, const NxVec3& n)
45                {
46                set(p, n);
47                }
48
49        /**
50        \brief Constructor from three points
51        */
52        NX_INLINE NxPlane(const NxVec3& p0, const NxVec3& p1, const NxVec3& p2)
53                {
54                set(p0, p1, p2);
55                }
56
57        /**
58        \brief Constructor from a normal and a distance
59        */
60        NX_INLINE NxPlane(const NxVec3& _n, NxF32 _d) : normal(_n), d(_d)
61                {
62                }
63
64        /**
65        \brief Copy constructor
66        */
67        NX_INLINE NxPlane(const NxPlane& plane) : normal(plane.normal), d(plane.d)
68                {
69                }
70
71        /**
72        \brief Destructor
73        */
74        NX_INLINE ~NxPlane()
75                {
76                }
77
78        /**
79        \brief Sets plane to zero.
80        */
81        NX_INLINE NxPlane& zero()
82                {
83                normal.zero();
84                d = 0.0f;
85                return *this;
86                }
87
88        NX_INLINE NxPlane& set(NxF32 nx, NxF32 ny, NxF32 nz, NxF32 _d)
89                {
90                normal.set(nx, ny, nz);
91                d = _d;
92                return *this;
93                }
94
95        NX_INLINE NxPlane& set(const NxVec3& _normal, NxF32 _d)
96                {
97                normal = _normal;
98                d = _d;
99                return *this;
100                }
101
102        NX_INLINE NxPlane& set(const NxVec3& p, const NxVec3& _n)
103                {
104                normal = _n;
105                // Plane equation: a*x + b*y + c*z + d = 0
106                // p belongs to plane so:
107                //     a*p.x + b*p.y + c*p.z + d = 0
108                // <=> (n|p) + d = 0
109                // <=> d = - (n|p)
110                d = - p.dot(_n);
111                return *this;
112                }
113
114        /**
115         \brief Computes the plane equation from 3 points.
116         */
117        NxPlane& set(const NxVec3& p0, const NxVec3& p1, const NxVec3& p2)
118                {
119                NxVec3 Edge0 = p1 - p0;
120                NxVec3 Edge1 = p2 - p0;
121
122                normal = Edge0.cross(Edge1);
123                normal.normalize();
124
125                // See comments in set() for computation of d
126                d = -p0.dot(normal);
127
128                return  *this;
129                }
130
131        NX_INLINE NxF32 distance(const NxVec3& p) const
132                {
133                // Valid for plane equation a*x + b*y + c*z + d = 0
134                return p.dot(normal) + d;
135                }
136
137        NX_INLINE bool belongs(const NxVec3& p) const
138                {
139                return fabsf(distance(p)) < (1.0e-7f);
140                }
141
142        /**
143        \brief projects p into the plane
144        */
145        NX_INLINE NxVec3 project(const NxVec3 & p) const
146                {
147                // Pretend p is on positive side of plane, i.e. plane.distance(p)>0.
148                // To project the point we have to go in a direction opposed to plane's normal, i.e.:
149                return p - normal * distance(p);
150//              return p + normal * distance(p);
151                }
152
153        /**
154        \brief find an arbitrary point in the plane
155        */
156        NX_INLINE NxVec3 pointInPlane() const
157                {
158                // Project origin (0,0,0) to plane:
159                // (0) - normal * distance(0) = - normal * ((p|(0)) + d) = -normal*d
160                return - normal * d;
161//              return normal * d;
162                }
163
164        NX_INLINE void normalize()
165                {
166                        NxF32 Denom = 1.0f / normal.magnitude();
167                        normal.x        *= Denom;
168                        normal.y        *= Denom;
169                        normal.z        *= Denom;
170                        d                       *= Denom;
171                }
172
173        NX_INLINE operator NxVec3() const
174                {
175                return normal;
176                }
177
178        NX_INLINE void transform(const NxMat34 & transform, NxPlane & transformed) const
179                {
180                transformed.normal = transform.M * normal;
181                transformed.d = d - (transform.t | transformed.normal);
182                }
183
184        NX_INLINE void inverseTransform(const NxMat34 & transform, NxPlane & transformed) const
185                {
186                transformed.normal = transform.M % normal;
187                NxVec3 it = transform.M %  transform.t;
188                transformed.d = d + (it | transformed.normal);
189                }
190
191        NxVec3  normal;         //!< The normal to the plane
192        NxF32   d;                      //!< The distance from the origin
193        };
194
195 /** @} */
196#endif
197
198
199//AGCOPYRIGHTBEGIN
200///////////////////////////////////////////////////////////////////////////
201// Copyright © 2005 AGEIA Technologies.
202// All rights reserved. www.ageia.com
203///////////////////////////////////////////////////////////////////////////
204//AGCOPYRIGHTEND
205
Note: See TracBrowser for help on using the repository browser.