SPlane3.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "SVector3.h"
5 
6 
9 template <typename T, typename Vector = vec3<T> >
10 class SPlane3
11 {
12 
13 public:
14 
16  {
20  };
21 
23  : Normal(0, 1, 0)
24  {
25  RecalculateD(Vector(0,0,0));
26  }
27 
28  SPlane3(const Vector& MPoint, const Vector& Normal)
29  : Normal(Normal)
30  {
31  RecalculateD(MPoint);
32  }
33 
34  SPlane3(T px, T py, T pz, T nx, T ny, T nz)
35  : Normal(nx, ny, nz)
36  {
37  RecalculateD(Vector(px, py, pz));
38  }
39 
40  SPlane3(const Vector& point1, const Vector& point2, const Vector& point3)
41  {
42  SetPlane(point1, point2, point3);
43  }
44 
45  SPlane3(const Vector & normal, const T d)
46  : Normal(normal), D(d)
47  {}
48 
49  void SetPlane(const Vector& point, const Vector& nvector)
50  {
51  Normal = nvector;
52  RecalculateD(point);
53  }
54 
55  void SetPlane(const Vector& nvect, T d)
56  {
57  Normal = nvect;
58  D = d;
59  }
60 
61  void SetPlane(const Vector& point1, const Vector& point2, const Vector& point3)
62  {
63  // creates the plane from 3 memberpoints
64  Normal = (point2 - point1).crossProduct(point3 - point1);
65  Normal.normalize();
66 
67  RecalculateD(point1);
68  }
69 
70 
72 
77  bool GetIntersectionWithLine(const Vector& linePoint,
78  const Vector& lineVect,
79  Vector& outIntersection) const
80  {
81  T t2 = Normal.DotProduct(lineVect);
82 
83  if (t2 == 0)
84  return false;
85 
86  T t =- (Normal.DotProduct(linePoint) + D) / t2;
87  outIntersection = linePoint + (lineVect * t);
88  return true;
89  }
90 
92 
98  f32 GetKnownIntersectionWithLine(const Vector& linePoint1,
99  const Vector& linePoint2) const
100  {
101  Vector vect = linePoint2 - linePoint1;
102  T t2 = (f32)Normal.DotProduct(vect);
103  return (f32)-((Normal.DotProduct(linePoint1) + D) / t2);
104  }
105 
107 
113  const Vector& linePoint1,
114  const Vector& linePoint2,
115  Vector& outIntersection) const
116  {
117  return (GetIntersectionWithLine(linePoint1, linePoint2 - linePoint1, outIntersection) &&
118  outIntersection.IsBetweenPoints(linePoint1, linePoint2));
119  }
120 
122 
126  EIntersectionRelation ClassifyPointRelation(const Vector& point) const
127  {
128  const T d = Normal.DotProduct(point) + D;
129 
130  if (d < -RoundingError<T>::Value())
131  return EIntersectionRelation::Back;
132 
133  if (d > RoundingError<T>::Value())
134  return EIntersectionRelation::Front;
135 
136  return EIntersectionRelation::Plannar;
137  }
138 
140  void RecalculateD(const Vector& MPoint)
141  {
142  D = - MPoint.DotProduct(Normal);
143  }
144 
146  Vector GetMemberPoint() const
147  {
148  return Normal * -D;
149  }
150 
152 
153  bool ExistsIntersection(const SPlane3<T>& other) const
154  {
155  Vector cross = other.Normal.CrossProduct(Normal);
156  return cross.getLength() > RoundingError<T>::Value();
157  }
158 
160 
165  Vector& outLinePoint,
166  Vector& outLineVect) const
167  {
168  const T fn00 = Normal.getLength();
169  const T fn01 = Normal.DotProduct(other.Normal);
170  const T fn11 = other.Normal.getLength();
171  const f64 det = fn00*fn11 - fn01*fn01;
172 
173  if (fabs(det) < RoundingError<T>::Value())
174  return false;
175 
176  const f64 invdet = 1.0 / det;
177  const f64 fc0 = (fn11*-D + fn01*other.D) * invdet;
178  const f64 fc1 = (fn00*-other.D + fn01*D) * invdet;
179 
180  outLineVect = Normal.CrossProduct(other.Normal);
181  outLinePoint = Normal*(T)fc0 + other.Normal*(T)fc1;
182  return true;
183  }
184 
187  const SPlane3<T>& o2, Vector& outPoint) const
188  {
189  Vector linePoint, lineVect;
190  if (GetIntersectionWithPlane(o1, linePoint, lineVect))
191  return o2.GetIntersectionWithLine(linePoint, lineVect, outPoint);
192 
193  return false;
194  }
195 
197 
205  bool IsFrontFacing(const Vector& lookDirection) const
206  {
207  const f32 d = Normal.DotProduct(lookDirection);
208  return d <= 0 || Equals((T) 0, d);
209  }
210 
212 
213  T GetDistanceTo(const Vector& point) const
214  {
215  return point.DotProduct(Normal) + D;
216  }
217 
219  Vector Normal;
220 
222  T D;
223 };
224 
bool GetIntersectionWithPlane(const SPlane3< T > &other, Vector &outLinePoint, Vector &outLineVect) const
Intersects this plane with another.
Definition: SPlane3.h:164
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:186
EIntersectionRelation
Definition: SPlane3.h:15
bool ExistsIntersection(const SPlane3< T > &other) const
Tests if there is an intersection with the other plane.
Definition: SPlane3.h:153
void SetPlane(const Vector &nvect, T d)
Definition: SPlane3.h:55
bool GetIntersectionWithLine(const Vector &linePoint, const Vector &lineVect, Vector &outIntersection) const
Get an intersection with a 3d line.
Definition: SPlane3.h:77
SPlane3(const Vector &normal, const T d)
Definition: SPlane3.h:45
void SetPlane(const Vector &point1, const Vector &point2, const Vector &point3)
Definition: SPlane3.h:61
Definition: SPlane3.h:19
bool GetIntersectionWithLimitedLine(const Vector &linePoint1, const Vector &linePoint2, Vector &outIntersection) const
Get an intersection with a 3d line, limited between two 3d points.
Definition: SPlane3.h:112
SPlane3(T px, T py, T pz, T nx, T ny, T nz)
Definition: SPlane3.h:34
void SetPlane(const Vector &point, const Vector &nvector)
Definition: SPlane3.h:49
void RecalculateD(const Vector &MPoint)
Recalculates the distance from origin by applying a new member point to the plane.
Definition: SPlane3.h:140
SPlane3()
Definition: SPlane3.h:22
T GetDistanceTo(const Vector &point) const
Get the distance to a point.
Definition: SPlane3.h:213
float f32
Definition: ionTypes.h:94
f32 GetKnownIntersectionWithLine(const Vector &linePoint1, const Vector &linePoint2) const
Get percentage of line between two points where an intersection with this plane happens.
Definition: SPlane3.h:98
T D
Distance from origin.
Definition: SPlane3.h:222
static T const Value()
Definition: ionComparison.h:32
EIntersectionRelation ClassifyPointRelation(const Vector &point) const
Classifies the relation of a point to this plane.
Definition: SPlane3.h:126
Vector GetMemberPoint() const
Gets a member point of the plane.
Definition: SPlane3.h:146
SPlane3(const Vector &point1, const Vector &point2, const Vector &point3)
Definition: SPlane3.h:40
Definition: SPlane3.h:18
SPlane3< s32 > plane3i
Definition: SPlane3.h:226
double f64
Definition: ionTypes.h:95
Definition: SPlane3.h:17
Vector Normal
Normal vector of the plane.
Definition: SPlane3.h:219
SPlane3< f32 > plane3f
Definition: SPlane3.h:225
SPlane3(const Vector &MPoint, const Vector &Normal)
Definition: SPlane3.h:28
3D plane
Definition: SPlane3.h:10
bool IsFrontFacing(const Vector &lookDirection) const
Test if the triangle would be front or backfacing from any point.
Definition: SPlane3.h:205
Helper template to enable template-dependant epsilon values.
Definition: ionComparison.h:30