Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
plane3d.hpp
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 nirtcpp/nirtcpp.hpp
4
5#ifndef NIRT_PLANE_3D_HPP_INCLUDED
6#define NIRT_PLANE_3D_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/irrMath.hpp>
9#include <nirtcpp/core/engine/vector3d.hpp>
10
11namespace nirt
12{
13namespace core
14{
15
18{
19 ISREL3D_FRONT = 0,
20 ISREL3D_BACK,
21 ISREL3D_PLANAR,
22 ISREL3D_SPANNING,
23 ISREL3D_CLIPPED
24};
25
27
32template <class T>
34{
35 public:
36
37 // Constructors
38
39 plane3d(): Normal(0,1,0) { recalculateD(vector3d<T>(0,0,0)); }
40
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
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 {
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
77 }
78
79
81
87 const vector3d<T>& lineVect,
89 {
90 T t2 = Normal.dotProduct(lineVect);
91
92 if (t2 == 0)
93 return false;
94
95 T t =- (Normal.dotProduct(linePoint) + D) / t2;
97 return true;
98 }
99
101
108 const vector3d<T>& linePoint2) const
109 {
111 T t2 = (f32)Normal.dotProduct(vect);
112 return (f32)-((Normal.dotProduct(linePoint1) + D) / t2);
113 }
114
116
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
150 {
151 D = - MPoint.dotProduct(Normal);
152 }
153
156 {
157 return Normal * -D;
158 }
159
161
163 {
164 vector3d<T> cross = other.Normal.crossProduct(Normal);
165 return cross.getLength() > core::ROUNDING_ERROR_f32;
166 }
167
169
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 {
200 return o2.getIntersectionWithLine(linePoint, lineVect, outPoint);
201
202 return false;
203 }
204
206
215 {
216 const f32 d = Normal.dotProduct(lookDirection);
217 return F32_LOWER_EQUAL_0 ( d );
218 }
219
221
223 {
224 return point.dotProduct(Normal) + D;
225 }
226
229
232};
233
234
237
240
241} // end namespace core
242} // end namespace nirt
243
244#endif
245
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
Template plane class with some intersection testing methods.
Definition plane3d.hpp:34
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.hpp:195
T D
Distance from origin.
Definition plane3d.hpp:231
bool getIntersectionWithPlane(const plane3d< T > &other, vector3d< T > &outLinePoint, vector3d< T > &outLineVect) const
Intersects this plane with another.
Definition plane3d.hpp:173
vector3d< T > Normal
Normal vector of the plane.
Definition plane3d.hpp:228
vector3d< T > getMemberPoint() const
Gets a member point of the plane.
Definition plane3d.hpp:155
EIntersectionRelation3D classifyPointRelation(const vector3d< T > &point) const
Classifies the relation of a point to this plane.
Definition plane3d.hpp:135
void recalculateD(const vector3d< T > &MPoint)
Recalculates the distance from origin by applying a new member point to the plane.
Definition plane3d.hpp:149
bool getIntersectionWithLine(const vector3d< T > &linePoint, const vector3d< T > &lineVect, vector3d< T > &outIntersection) const
Get an intersection with a 3d line.
Definition plane3d.hpp:86
bool existsIntersection(const plane3d< T > &other) const
Tests if there is an intersection with the other plane.
Definition plane3d.hpp:162
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.hpp:107
bool isFrontFacing(const vector3d< T > &lookDirection) const
Test if the triangle would be front or backfacing from any point.
Definition plane3d.hpp:214
T getDistanceTo(const vector3d< T > &point) const
Get the distance to a point.
Definition plane3d.hpp:222
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.hpp:121
EIntersectionRelation3D
Enumeration for intersection relations of 3d objects.
Definition plane3d.hpp:18
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.hpp:243
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
double f64
64 bit floating point variable.
Definition irrTypes.hpp:114
float f32
32 bit floating point variable.
Definition irrTypes.hpp:110

Nirtcpp    @cppfx.xyz

Utxcpp    utx::print