arsa  2.7
Public Member Functions | Public Attributes | List of all members
irr::core::triangle3d< T > Class Template Reference

3d triangle template class for doing collision detection and other things. More...

#include <triangle3d.h>

Public Member Functions

 triangle3d ()
 Constructor for an all 0 triangle. More...
 
 triangle3d (const vector3d< T > &v1, const vector3d< T > &v2, const vector3d< T > &v3)
 Constructor for triangle with given three vertices. More...
 
bool operator== (const triangle3d< T > &other) const
 Equality operator. More...
 
bool operator!= (const triangle3d< T > &other) const
 Inequality operator. More...
 
bool isTotalInsideBox (const aabbox3d< T > &box) const
 Determines if the triangle is totally inside a bounding box. More...
 
bool isTotalOutsideBox (const aabbox3d< T > &box) const
 Determines if the triangle is totally outside a bounding box. More...
 
core::vector3d< T > closestPointOnTriangle (const core::vector3d< T > &p) const
 Get the closest point on a triangle to a point on the same plane. More...
 
bool isPointInside (const vector3d< T > &p) const
 Check if a point is inside the triangle (border-points count also as inside) More...
 
bool isPointInsideFast (const vector3d< T > &p) const
 Check if a point is inside the triangle (border-points count also as inside) More...
 
bool getIntersectionWithLimitedLine (const line3d< T > &line, vector3d< T > &outIntersection) const
 Get an intersection with a 3d line. More...
 
bool getIntersectionWithLine (const vector3d< T > &linePoint, const vector3d< T > &lineVect, vector3d< T > &outIntersection) const
 Get an intersection with a 3d line. More...
 
bool getIntersectionOfPlaneWithLine (const vector3d< T > &linePoint, const vector3d< T > &lineVect, vector3d< T > &outIntersection) const
 Calculates the intersection between a 3d line and the plane the triangle is on. More...
 
vector3d< T > getNormal () const
 Get the normal of the triangle. More...
 
bool isFrontFacing (const vector3d< T > &lookDirection) const
 Test if the triangle would be front or backfacing from any point. More...
 
plane3d< T > getPlane () const
 Get the plane of this triangle. More...
 
getArea () const
 Get the area of the triangle. More...
 
void set (const core::vector3d< T > &a, const core::vector3d< T > &b, const core::vector3d< T > &c)
 sets the triangle's points More...
 

Public Attributes

vector3d< T > pointA
 the three points of the triangle More...
 
vector3d< T > pointB
 
vector3d< T > pointC
 

Detailed Description

template<class T>
class irr::core::triangle3d< T >

3d triangle template class for doing collision detection and other things.

Definition at line 20 of file triangle3d.h.

Constructor & Destructor Documentation

◆ triangle3d() [1/2]

template<class T>
irr::core::triangle3d< T >::triangle3d ( )
inline

Constructor for an all 0 triangle.

Definition at line 25 of file triangle3d.h.

25 {}

◆ triangle3d() [2/2]

template<class T>
irr::core::triangle3d< T >::triangle3d ( const vector3d< T > &  v1,
const vector3d< T > &  v2,
const vector3d< T > &  v3 
)
inline

Constructor for triangle with given three vertices.

Definition at line 27 of file triangle3d.h.

27 : pointA(v1), pointB(v2), pointC(v3) {}
GLfloat GLfloat GLfloat v2
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
GLfloat GLfloat GLfloat GLfloat v3
vector3d< T > pointC
Definition: triangle3d.h:241
GLfloat GLfloat v1

Member Function Documentation

◆ closestPointOnTriangle()

template<class T>
core::vector3d<T> irr::core::triangle3d< T >::closestPointOnTriangle ( const core::vector3d< T > &  p) const
inline

Get the closest point on a triangle to a point on the same plane.

Parameters
pPoint which must be on the same plane as the triangle.
Returns
The closest point of the triangle

Definition at line 68 of file triangle3d.h.

69  {
70  const core::vector3d<T> rab = line3d<T>(pointA, pointB).getClosestPoint(p);
71  const core::vector3d<T> rbc = line3d<T>(pointB, pointC).getClosestPoint(p);
72  const core::vector3d<T> rca = line3d<T>(pointC, pointA).getClosestPoint(p);
73 
74  const T d1 = rab.getDistanceFrom(p);
75  const T d2 = rbc.getDistanceFrom(p);
76  const T d3 = rca.getDistanceFrom(p);
77 
78  if (d1 < d2)
79  return d1 < d3 ? rab : rca;
80 
81  return d2 < d3 ? rbc : rca;
82  }
GLfloat GLfloat p
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ getArea()

template<class T>
T irr::core::triangle3d< T >::getArea ( ) const
inline

Get the area of the triangle.

Definition at line 224 of file triangle3d.h.

225  {
226  return (pointB - pointA).crossProduct(pointC - pointA).getLength() * 0.5f;
227 
228  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ getIntersectionOfPlaneWithLine()

template<class T>
bool irr::core::triangle3d< T >::getIntersectionOfPlaneWithLine ( const vector3d< T > &  linePoint,
const vector3d< T > &  lineVect,
vector3d< T > &  outIntersection 
) const
inline

Calculates the intersection between a 3d line and the plane the triangle is on.

Parameters
lineVectVector of the line to intersect with.
linePointPoint of the line to intersect with.
outIntersectionPlace to store the intersection point, if there is one.
Returns
True if there was an intersection, else false.

Definition at line 169 of file triangle3d.h.

171  {
172  // Work with f64 to get more precise results (makes enough difference to be worth the casts).
173  const vector3d<f64> linePointf64(linePoint.X, linePoint.Y, linePoint.Z);
174  const vector3d<f64> lineVectf64(lineVect.X, lineVect.Y, lineVect.Z);
175  vector3d<f64> outIntersectionf64;
176 
177  core::triangle3d<irr::f64> trianglef64(vector3d<f64>((f64)pointA.X, (f64)pointA.Y, (f64)pointA.Z)
178  ,vector3d<f64>((f64)pointB.X, (f64)pointB.Y, (f64)pointB.Z)
179  , vector3d<f64>((f64)pointC.X, (f64)pointC.Y, (f64)pointC.Z));
180  const vector3d<irr::f64> normalf64 = trianglef64.getNormal().normalize();
181  f64 t2;
182 
183  if ( core::iszero ( t2 = normalf64.dotProduct(lineVectf64) ) )
184  return false;
185 
186  f64 d = trianglef64.pointA.dotProduct(normalf64);
187  f64 t = -(normalf64.dotProduct(linePointf64) - d) / t2;
188  outIntersectionf64 = linePointf64 + (lineVectf64 * t);
189 
190  outIntersection.X = (T)outIntersectionf64.X;
191  outIntersection.Y = (T)outIntersectionf64.Y;
192  outIntersection.Z = (T)outIntersectionf64.Z;
193  return true;
194  }
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
Definition: irrMath.h:307
GLdouble GLdouble t
Definition: SDL_opengl.h:2071
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ getIntersectionWithLimitedLine()

template<class T>
bool irr::core::triangle3d< T >::getIntersectionWithLimitedLine ( const line3d< T > &  line,
vector3d< T > &  outIntersection 
) const
inline

Get an intersection with a 3d line.

Parameters
lineLine to intersect with.
outIntersectionPlace to store the intersection point, if there is one.
Returns
True if there was an intersection, false if not.

Definition at line 136 of file triangle3d.h.

138  {
139  return getIntersectionWithLine(line.start,
140  line.getVector(), outIntersection) &&
141  outIntersection.isBetweenPoints(line.start, line.end);
142  }
bool getIntersectionWithLine(const vector3d< T > &linePoint, const vector3d< T > &lineVect, vector3d< T > &outIntersection) const
Get an intersection with a 3d line.
Definition: triangle3d.h:154

◆ getIntersectionWithLine()

template<class T>
bool irr::core::triangle3d< T >::getIntersectionWithLine ( const vector3d< T > &  linePoint,
const vector3d< T > &  lineVect,
vector3d< T > &  outIntersection 
) const
inline

Get an intersection with a 3d line.

Please note that also points are returned as intersection which are on the line, but not between the start and end point of the line. If you want the returned point be between start and end use getIntersectionWithLimitedLine().

Parameters
linePointPoint of the line to intersect with.
lineVectVector of the line to intersect with.
outIntersectionPlace to store the intersection point, if there is one.
Returns
True if there was an intersection, false if there was not.

Definition at line 154 of file triangle3d.h.

156  {
157  if (getIntersectionOfPlaneWithLine(linePoint, lineVect, outIntersection))
158  return isPointInside(outIntersection);
159 
160  return false;
161  }
bool getIntersectionOfPlaneWithLine(const vector3d< T > &linePoint, const vector3d< T > &lineVect, vector3d< T > &outIntersection) const
Calculates the intersection between a 3d line and the plane the triangle is on.
Definition: triangle3d.h:169
bool isPointInside(const vector3d< T > &p) const
Check if a point is inside the triangle (border-points count also as inside)
Definition: triangle3d.h:89

◆ getNormal()

template<class T>
vector3d<T> irr::core::triangle3d< T >::getNormal ( ) const
inline

Get the normal of the triangle.

Please note: The normal is not always normalized.

Definition at line 199 of file triangle3d.h.

200  {
201  return (pointB - pointA).crossProduct(pointC - pointA);
202  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ getPlane()

template<class T>
plane3d<T> irr::core::triangle3d< T >::getPlane ( ) const
inline

Get the plane of this triangle.

Definition at line 218 of file triangle3d.h.

219  {
220  return plane3d<T>(pointA, pointB, pointC);
221  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ isFrontFacing()

template<class T>
bool irr::core::triangle3d< T >::isFrontFacing ( const vector3d< T > &  lookDirection) const
inline

Test if the triangle would be front or backfacing from any point.

Thus, this method assumes a camera position from which the triangle is definitely visible when looking at the given direction. Do not use this method with points as it will give wrong results!

Parameters
lookDirectionLook direction.
Returns
True if the plane is front facing and false if it is backfacing.

Definition at line 210 of file triangle3d.h.

211  {
212  const vector3d<T> n = getNormal().normalize();
213  const f32 d = (f32)n.dotProduct(lookDirection);
214  return F32_LOWER_EQUAL_0(d);
215  }
GLdouble n
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
vector3d< T > getNormal() const
Get the normal of the triangle.
Definition: triangle3d.h:199
#define F32_LOWER_EQUAL_0(n)
Definition: irrMath.h:423

◆ isPointInside()

template<class T>
bool irr::core::triangle3d< T >::isPointInside ( const vector3d< T > &  p) const
inline

Check if a point is inside the triangle (border-points count also as inside)

Definition at line 89 of file triangle3d.h.

90  {
91  vector3d<f64> af64((f64)pointA.X, (f64)pointA.Y, (f64)pointA.Z);
92  vector3d<f64> bf64((f64)pointB.X, (f64)pointB.Y, (f64)pointB.Z);
93  vector3d<f64> cf64((f64)pointC.X, (f64)pointC.Y, (f64)pointC.Z);
94  vector3d<f64> pf64((f64)p.X, (f64)p.Y, (f64)p.Z);
95  return (isOnSameSide(pf64, af64, bf64, cf64) &&
96  isOnSameSide(pf64, bf64, af64, cf64) &&
97  isOnSameSide(pf64, cf64, af64, bf64));
98  }
GLfloat GLfloat p
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ isPointInsideFast()

template<class T>
bool irr::core::triangle3d< T >::isPointInsideFast ( const vector3d< T > &  p) const
inline

Check if a point is inside the triangle (border-points count also as inside)

This method uses a barycentric coordinate system. It is faster than isPointInside but is more susceptible to floating point rounding errors. This will especially be noticeable when the FPU is in single precision mode (which is for example set on default by Direct3D).

Parameters
pPoint to test. Assumes that this point is already on the plane of the triangle.
Returns
True if point is inside the triangle, otherwise false.

Definition at line 108 of file triangle3d.h.

109  {
110  const vector3d<T> a = pointC - pointA;
111  const vector3d<T> b = pointB - pointA;
112  const vector3d<T> c = p - pointA;
113 
114  const f64 dotAA = a.dotProduct( a);
115  const f64 dotAB = a.dotProduct( b);
116  const f64 dotAC = a.dotProduct( c);
117  const f64 dotBB = b.dotProduct( b);
118  const f64 dotBC = b.dotProduct( c);
119 
120  // get coordinates in barycentric coordinate system
121  const f64 invDenom = 1/(dotAA * dotBB - dotAB * dotAB);
122  const f64 u = (dotBB * dotAC - dotAB * dotBC) * invDenom;
123  const f64 v = (dotAA * dotBC - dotAB * dotAC ) * invDenom;
124 
125  // We count border-points as inside to keep downward compatibility.
126  // Rounding-error also needed for some test-cases.
127  return (u > -ROUNDING_ERROR_f32) && (v >= 0) && (u + v < 1+ROUNDING_ERROR_f32);
128 
129  }
GLfloat GLfloat p
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
const GLdouble * v
Definition: SDL_opengl.h:2064
const f32 ROUNDING_ERROR_f32
Definition: irrMath.h:50
GLboolean GLboolean GLboolean b
vector3d< T > pointC
Definition: triangle3d.h:241
GLboolean GLboolean GLboolean GLboolean a
const GLubyte * c

◆ isTotalInsideBox()

template<class T>
bool irr::core::triangle3d< T >::isTotalInsideBox ( const aabbox3d< T > &  box) const
inline

Determines if the triangle is totally inside a bounding box.

Parameters
boxBox to check.
Returns
True if triangle is within the box, otherwise false.

Definition at line 44 of file triangle3d.h.

45  {
46  return (box.isPointInside(pointA) &&
47  box.isPointInside(pointB) &&
48  box.isPointInside(pointC));
49  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ isTotalOutsideBox()

template<class T>
bool irr::core::triangle3d< T >::isTotalOutsideBox ( const aabbox3d< T > &  box) const
inline

Determines if the triangle is totally outside a bounding box.

Parameters
boxBox to check.
Returns
True if triangle is outside the box, otherwise false.

Definition at line 54 of file triangle3d.h.

55  {
56  return ((pointA.X > box.MaxEdge.X && pointB.X > box.MaxEdge.X && pointC.X > box.MaxEdge.X) ||
57 
58  (pointA.Y > box.MaxEdge.Y && pointB.Y > box.MaxEdge.Y && pointC.Y > box.MaxEdge.Y) ||
59  (pointA.Z > box.MaxEdge.Z && pointB.Z > box.MaxEdge.Z && pointC.Z > box.MaxEdge.Z) ||
60  (pointA.X < box.MinEdge.X && pointB.X < box.MinEdge.X && pointC.X < box.MinEdge.X) ||
61  (pointA.Y < box.MinEdge.Y && pointB.Y < box.MinEdge.Y && pointC.Y < box.MinEdge.Y) ||
62  (pointA.Z < box.MinEdge.Z && pointB.Z < box.MinEdge.Z && pointC.Z < box.MinEdge.Z));
63  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ operator!=()

template<class T>
bool irr::core::triangle3d< T >::operator!= ( const triangle3d< T > &  other) const
inline

Inequality operator.

Definition at line 36 of file triangle3d.h.

37  {
38  return !(*this==other);
39  }

◆ operator==()

template<class T>
bool irr::core::triangle3d< T >::operator== ( const triangle3d< T > &  other) const
inline

Equality operator.

Definition at line 30 of file triangle3d.h.

31  {
32  return other.pointA==pointA && other.pointB==pointB && other.pointC==pointC;
33  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
vector3d< T > pointC
Definition: triangle3d.h:241

◆ set()

template<class T>
void irr::core::triangle3d< T >::set ( const core::vector3d< T > &  a,
const core::vector3d< T > &  b,
const core::vector3d< T > &  c 
)
inline

sets the triangle's points

Definition at line 231 of file triangle3d.h.

232  {
233  pointA = a;
234  pointB = b;
235  pointC = c;
236  }
vector3d< T > pointA
the three points of the triangle
Definition: triangle3d.h:239
vector3d< T > pointB
Definition: triangle3d.h:240
GLboolean GLboolean GLboolean b
vector3d< T > pointC
Definition: triangle3d.h:241
GLboolean GLboolean GLboolean GLboolean a
const GLubyte * c

Member Data Documentation

◆ pointA

template<class T>
vector3d<T> irr::core::triangle3d< T >::pointA

the three points of the triangle

Definition at line 239 of file triangle3d.h.

◆ pointB

template<class T>
vector3d<T> irr::core::triangle3d< T >::pointB

Definition at line 240 of file triangle3d.h.

◆ pointC

template<class T>
vector3d<T> irr::core::triangle3d< T >::pointC

Definition at line 241 of file triangle3d.h.


The documentation for this class was generated from the following file: