Irrlicht 3D Engine
plane3d.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __IRR_PLANE_3D_H_INCLUDED__
6 #define __IRR_PLANE_3D_H_INCLUDED__
7 
8 #include "irrMath.h"
9 #include "vector3d.h"
10 
11 namespace irr
12 {
13 namespace core
14 {
15 
18 {
24 };
25 
27 
32 template <class T>
33 class plane3d
34 {
35  public:
36 
37  // Constructors
38 
39  plane3d(): Normal(0,1,0) { recalculateD(vector3d<T>(0,0,0)); }
40 
41  plane3d(const vector3d<T>& MPoint, const vector3d<T>& Normal) : Normal(Normal) { recalculateD(MPoint); }
42 
43  plane3d(T px, T py, T pz, T nx, T ny, T nz) : Normal(nx, ny, nz) { recalculateD(vector3d<T>(px, py, pz)); }
44 
45  plane3d(const vector3d<T>& point1, const vector3d<T>& point2, const vector3d<T>& point3)
46  { setPlane(point1, point2, point3); }
47 
48  plane3d(const vector3d<T> & normal, const T d) : Normal(normal), D(d) { }
49 
50  // operators
51 
52  inline bool operator==(const plane3d<T>& other) const { return (equals(D, other.D) && Normal==other.Normal);}
53 
54  inline bool operator!=(const plane3d<T>& other) const { return !(*this == other);}
55 
56  // functions
57 
58  void setPlane(const vector3d<T>& point, const vector3d<T>& nvector)
59  {
60  Normal = nvector;
61  recalculateD(point);
62  }
63 
64  void setPlane(const vector3d<T>& nvect, T d)
65  {
66  Normal = nvect;
67  D = d;
68  }
69 
70  void setPlane(const vector3d<T>& point1, const vector3d<T>& point2, const vector3d<T>& point3)
71  {
72  // creates the plane from 3 memberpoints
73  Normal = (point2 - point1).crossProduct(point3 - point1);
74  Normal.normalize();
75 
76  recalculateD(point1);
77  }
78 
79 
81 
86  bool getIntersectionWithLine(const vector3d<T>& linePoint,
87  const vector3d<T>& lineVect,
88  vector3d<T>& outIntersection) const
89  {
90  T t2 = Normal.dotProduct(lineVect);
91 
92  if (t2 == 0)
93  return false;
94 
95  T t =- (Normal.dotProduct(linePoint) + D) / t2;
96  outIntersection = linePoint + (lineVect * t);
97  return true;
98  }
99 
101 
108  const vector3d<T>& linePoint2) const
109  {
110  vector3d<T> vect = linePoint2 - linePoint1;
111  T t2 = (f32)Normal.dotProduct(vect);
112  return (f32)-((Normal.dotProduct(linePoint1) + D) / t2);
113  }
114 
116 
122  const vector3d<T>& linePoint1,
123  const vector3d<T>& linePoint2,
124  vector3d<T>& outIntersection) const
125  {
126  return (getIntersectionWithLine(linePoint1, linePoint2 - linePoint1, outIntersection) &&
127  outIntersection.isBetweenPoints(linePoint1, linePoint2));
128  }
129 
131 
136  {
137  const T d = Normal.dotProduct(point) + D;
138 
139  if (d < -ROUNDING_ERROR_f32)
140  return ISREL3D_BACK;
141 
142  if (d > ROUNDING_ERROR_f32)
143  return ISREL3D_FRONT;
144 
145  return ISREL3D_PLANAR;
146  }
147 
149  void recalculateD(const vector3d<T>& MPoint)
150  {
151  D = - MPoint.dotProduct(Normal);
152  }
153 
156  {
157  return Normal * -D;
158  }
159 
161 
162  bool existsIntersection(const plane3d<T>& other) const
163  {
164  vector3d<T> cross = other.Normal.crossProduct(Normal);
165  return cross.getLength() > core::ROUNDING_ERROR_f32;
166  }
167 
169 
174  vector3d<T>& outLinePoint,
175  vector3d<T>& outLineVect) const
176  {
177  const T fn00 = Normal.getLength();
178  const T fn01 = Normal.dotProduct(other.Normal);
179  const T fn11 = other.Normal.getLength();
180  const f64 det = fn00*fn11 - fn01*fn01;
181 
182  if (fabs(det) < ROUNDING_ERROR_f64 )
183  return false;
184 
185  const f64 invdet = 1.0 / det;
186  const f64 fc0 = (fn11*-D + fn01*other.D) * invdet;
187  const f64 fc1 = (fn00*-other.D + fn01*D) * invdet;
188 
189  outLineVect = Normal.crossProduct(other.Normal);
190  outLinePoint = Normal*(T)fc0 + other.Normal*(T)fc1;
191  return true;
192  }
193 
196  const plane3d<T>& o2, vector3d<T>& outPoint) const
197  {
198  vector3d<T> linePoint, lineVect;
199  if (getIntersectionWithPlane(o1, linePoint, lineVect))
200  return o2.getIntersectionWithLine(linePoint, lineVect, outPoint);
201 
202  return false;
203  }
204 
206 
214  bool isFrontFacing(const vector3d<T>& lookDirection) const
215  {
216  const f32 d = Normal.dotProduct(lookDirection);
217  return F32_LOWER_EQUAL_0 ( d );
218  }
219 
221 
222  T getDistanceTo(const vector3d<T>& point) const
223  {
224  return point.dotProduct(Normal) + D;
225  }
226 
229 
231  T D;
232 };
233 
234 
237 
240 
241 } // end namespace core
242 } // end namespace irr
243 
244 #endif
245 
bool operator!=(const plane3d< T > &other) const
Definition: plane3d.h:54
T D
Distance from origin.
Definition: plane3d.h:231
vector3d< T > getMemberPoint() const
Gets a member point of the plane.
Definition: plane3d.h:155
plane3d(const vector3d< T > &point1, const vector3d< T > &point2, const vector3d< T > &point3)
Definition: plane3d.h:45
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
T getDistanceTo(const vector3d< T > &point) const
Get the distance to a point.
Definition: plane3d.h:222
void setPlane(const vector3d< T > &point1, const vector3d< T > &point2, const vector3d< T > &point3)
Definition: plane3d.h:70
f32 getKnownIntersectionWithLine(const vector3d< T > &linePoint1, const vector3d< T > &linePoint2) const
Get percentage of line between two points where an intersection with this plane happens.
Definition: plane3d.h:107
vector3d< T > Normal
Normal vector of the plane.
Definition: plane3d.h:228
plane3d(const vector3d< T > &MPoint, const vector3d< T > &Normal)
Definition: plane3d.h:41
EIntersectionRelation3D
Enumeration for intersection relations of 3d objects.
Definition: plane3d.h:17
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
3d vector template class with lots of operators and methods.
Definition: vector3d.h:22
void setPlane(const vector3d< T > &nvect, T d)
Definition: plane3d.h:64
plane3d(const vector3d< T > &normal, const T d)
Definition: plane3d.h:48
bool existsIntersection(const plane3d< T > &other) const
Tests if there is an intersection with the other plane.
Definition: plane3d.h:162
bool getIntersectionWithPlanes(const plane3d< T > &o1, const plane3d< T > &o2, vector3d< T > &outPoint) const
Get the intersection point with two other planes if there is one.
Definition: plane3d.h:195
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
plane3d< s32 > plane3di
Typedef for an integer 3d plane.
Definition: plane3d.h:239
const f32 ROUNDING_ERROR_f32
Definition: irrMath.h:50
plane3d(T px, T py, T pz, T nx, T ny, T nz)
Definition: plane3d.h:43
bool getIntersectionWithLine(const vector3d< T > &linePoint, const vector3d< T > &lineVect, vector3d< T > &outIntersection) const
Get an intersection with a 3d line.
Definition: plane3d.h:86
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:246
const f64 ROUNDING_ERROR_f64
Definition: irrMath.h:51
void setPlane(const vector3d< T > &point, const vector3d< T > &nvector)
Definition: plane3d.h:58
Template plane class with some intersection testing methods.
Definition: plane3d.h:33
bool getIntersectionWithPlane(const plane3d< T > &other, vector3d< T > &outLinePoint, vector3d< T > &outLineVect) const
Intersects this plane with another.
Definition: plane3d.h:173
bool getIntersectionWithLimitedLine(const vector3d< T > &linePoint1, const vector3d< T > &linePoint2, vector3d< T > &outIntersection) const
Get an intersection with a 3d line, limited between two 3d points.
Definition: plane3d.h:121
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition: vector3d.h:125
bool isFrontFacing(const vector3d< T > &lookDirection) const
Test if the triangle would be front or backfacing from any point.
Definition: plane3d.h:214
bool isBetweenPoints(const vector3d< T > &begin, const vector3d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition: vector3d.h:157
T getLength() const
Get length of the vector.
Definition: vector3d.h:117
void recalculateD(const vector3d< T > &MPoint)
Recalculates the distance from origin by applying a new member point to the plane.
Definition: plane3d.h:149
EIntersectionRelation3D classifyPointRelation(const vector3d< T > &point) const
Classifies the relation of a point to this plane.
Definition: plane3d.h:135
bool operator==(const plane3d< T > &other) const
Definition: plane3d.h:52
plane3d< f32 > plane3df
Typedef for a f32 3d plane.
Definition: plane3d.h:236
#define F32_LOWER_EQUAL_0(n)
Definition: irrMath.h:423