SPlane3.h
Go to the documentation of this file.
1 
2 /* irrlicht.h -- interface of the 'Irrlicht Engine'
3 
4 Copyright Original Work (C) 2002-2012 Nikolaus Gebhardt
5 Copyright Modified Work (C) 2017 Ian Dunn
6 
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10 
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14 
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22 
23 Please note that the Irrlicht Engine is based in part on the work of the
24 Independent JPEG Group, the zlib and the libPng. This means that if you use
25 the Irrlicht Engine in your product, you must acknowledge somewhere in your
26 documentation that you've used the IJG code. It would also be nice to mention
27 that you use the Irrlicht Engine, the zlib and libPng. See the README files
28 in the jpeglib, the zlib and libPng for further informations.
29 */
30 
31 #pragma once
32 
33 #include <ionCore.h>
34 
35 
36 namespace ion
37 {
38 
41  template <typename T, typename Vector = vec3<T> >
42  class SPlane3
43  {
44 
45  public:
46 
48  {
52  };
53 
55  : Normal(0, 1, 0)
56  {
57  RecalculateD(Vector(0, 0, 0));
58  }
59 
60  SPlane3(const Vector& MPoint, const Vector& Normal)
61  : Normal(Normal)
62  {
63  RecalculateD(MPoint);
64  }
65 
66  SPlane3(T px, T py, T pz, T nx, T ny, T nz)
67  : Normal(nx, ny, nz)
68  {
69  RecalculateD(Vector(px, py, pz));
70  }
71 
72  SPlane3(Vector const & point1, Vector const & point2, Vector const & point3)
73  {
74  SetPlane(point1, point2, point3);
75  }
76 
77  SPlane3(Vector const & normal, const T d)
78  : Normal(normal), D(d)
79  {}
80 
81  void SetPlane(Vector const & point, Vector const & nvector)
82  {
83  Normal = nvector;
84  RecalculateD(point);
85  }
86 
87  void SetPlane(Vector const & nvect, T d)
88  {
89  Normal = nvect;
90  D = d;
91  }
92 
93  void SetPlane(Vector const & point1, Vector const & point2, Vector const & point3)
94  {
95  // creates the plane from 3 memberpoints
96  Normal = (point2 - point1).crossProduct(point3 - point1);
97  Normal.normalize();
98 
99  RecalculateD(point1);
100  }
101 
102 
104 
109  bool GetIntersectionWithLine(Vector const & linePoint, Vector const & lineVect, Vector & outIntersection) const
110  {
111  T t2 = Normal.DotProduct(lineVect);
112 
113  if (t2 == 0)
114  return false;
115 
116  T t = - (Normal.DotProduct(linePoint) + D) / t2;
117  outIntersection = linePoint + (lineVect * t);
118  return true;
119  }
120 
122 
128  T GetKnownIntersectionWithLine(Vector const & linePoint1, Vector const & linePoint2) const
129  {
130  Vector vect = linePoint2 - linePoint1;
131  T t2 = (T) Normal.DotProduct(vect);
132  return (T) -((Normal.DotProduct(linePoint1) + D) / t2);
133  }
134 
136 
141  bool GetIntersectionWithLimitedLine(Vector const & linePoint1, Vector const & linePoint2, Vector & outIntersection) const
142  {
143  return (GetIntersectionWithLine(linePoint1, linePoint2 - linePoint1, outIntersection) &&
144  outIntersection.IsBetweenPoints(linePoint1, linePoint2));
145  }
146 
148 
152  EIntersectionRelation ClassifyPointRelation(Vector const & point) const
153  {
154  const T d = Normal.DotProduct(point) + D;
155 
156  if (d < -RoundingError<T>::Value())
158 
159  if (d > RoundingError<T>::Value())
160  return EIntersectionRelation::Front;
161 
162  return EIntersectionRelation::Plannar;
163  }
164 
166  void RecalculateD(const Vector& MPoint)
167  {
168  D = - MPoint.DotProduct(Normal);
169  }
170 
172  Vector GetMemberPoint() const
173  {
174  return Normal * -D;
175  }
176 
178 
179  bool ExistsIntersection(const SPlane3<T>& other) const
180  {
181  Vector cross = other.Normal.CrossProduct(Normal);
182  return cross.getLength() > RoundingError<T>::Value();
183  }
184 
186 
191  Vector& outLinePoint,
192  Vector& outLineVect) const
193  {
194  const T fn00 = Normal.getLength();
195  const T fn01 = Normal.DotProduct(other.Normal);
196  const T fn11 = other.Normal.getLength();
197  const T det = fn00*fn11 - fn01*fn01;
198 
199  if (fabs(det) < RoundingError<T>::Value())
200  return false;
201 
202  const T invdet = 1 / det;
203  const T fc0 = (fn11*-D + fn01*other.D) * invdet;
204  const T fc1 = (fn00*-other.D + fn01*D) * invdet;
205 
206  outLineVect = Normal.CrossProduct(other.Normal);
207  outLinePoint = Normal*(T) fc0 + other.Normal*(T) fc1;
208  return true;
209  }
210 
213  const SPlane3<T>& o2, Vector& outPoint) const
214  {
215  Vector linePoint, lineVect;
216  if (GetIntersectionWithPlane(o1, linePoint, lineVect))
217  return o2.GetIntersectionWithLine(linePoint, lineVect, outPoint);
218 
219  return false;
220  }
221 
223 
231  bool IsFrontFacing(const Vector& lookDirection) const
232  {
233  const T d = Normal.DotProduct(lookDirection);
234  return d <= 0 || Equals((T) 0, d);
235  }
236 
238 
239  T GetDistanceTo(const Vector& point) const
240  {
241  return point.DotProduct(Normal) + D;
242  }
243 
245  Vector Normal;
246 
248  T D;
249  };
250 
253 
254 }
SPlane3(T px, T py, T pz, T nx, T ny, T nz)
Definition: SPlane3.h:66
void RecalculateD(const Vector &MPoint)
Recalculates the distance from origin by applying a new member point to the plane.
Definition: SPlane3.h:166
static T const Value()
Definition: ionComparison.h:34
EIntersectionRelation ClassifyPointRelation(Vector const &point) const
Classifies the relation of a point to this plane.
Definition: SPlane3.h:152
SPlane3< int > plane3i
Definition: SPlane3.h:252
Vector Normal
Normal vector of the plane.
Definition: SPlane3.h:245
T D
Distance from origin.
Definition: SPlane3.h:248
Vector GetMemberPoint() const
Gets a member point of the plane.
Definition: SPlane3.h:172
SPlane3(const Vector &MPoint, const Vector &Normal)
Definition: SPlane3.h:60
void SetPlane(Vector const &point1, Vector const &point2, Vector const &point3)
Definition: SPlane3.h:93
void SetPlane(Vector const &nvect, T d)
Definition: SPlane3.h:87
3D plane
Definition: SPlane3.h:42
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
bool GetIntersectionWithPlane(const SPlane3< T > &other, Vector &outLinePoint, Vector &outLineVect) const
Intersects this plane with another.
Definition: SPlane3.h:190
Definition: SPlane3.h:49
bool ExistsIntersection(const SPlane3< T > &other) const
Tests if there is an intersection with the other plane.
Definition: SPlane3.h:179
bool GetIntersectionWithLimitedLine(Vector const &linePoint1, Vector const &linePoint2, Vector &outIntersection) const
Get an intersection with a 3d line, limited between two 3d points.
Definition: SPlane3.h:141
SPlane3(Vector const &normal, const T d)
Definition: SPlane3.h:77
bool GetIntersectionWithLine(Vector const &linePoint, Vector const &lineVect, Vector &outIntersection) const
Get an intersection with a 3d line.
Definition: SPlane3.h:109
Helper template to enable template-dependant epsilon values.
Definition: ionComparison.h:32
T GetDistanceTo(const Vector &point) const
Get the distance to a point.
Definition: SPlane3.h:239
SPlane3(Vector const &point1, Vector const &point2, Vector const &point3)
Definition: SPlane3.h:72
T GetKnownIntersectionWithLine(Vector const &linePoint1, Vector const &linePoint2) const
Get percentage of line between two points where an intersection with this plane happens.
Definition: SPlane3.h:128
Definition: SPlane3.h:51
SPlane3()
Definition: SPlane3.h:54
EIntersectionRelation
Definition: SPlane3.h:47
bool IsFrontFacing(const Vector &lookDirection) const
Test if the triangle would be front or backfacing from any point.
Definition: SPlane3.h:231
SPlane3< float > plane3f
Definition: SPlane3.h:251
bool GetIntersectionWithPlanes(const SPlane3< T > &o1, const SPlane3< T > &o2, Vector &outPoint) const
Get the intersection point with two other planes if there is one.
Definition: SPlane3.h:212
void SetPlane(Vector const &point, Vector const &nvector)
Definition: SPlane3.h:81
Definition: SPlane3.h:50