TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::Triangle Class Reference

#include <Triangle.h>

Public Member Functions

 Triangle (class BinaryInput &b)
 
void serialize (class BinaryOutput &b)
 
void deserialize (class BinaryInput &b)
 
 Triangle ()
 
 Triangle (const Point3 &v0, const Point3 &v1, const Point3 &v2)
 
 ~Triangle ()
 
const Point3vertex (int n) const
 
const Vector3edge01 () const
 
const Vector3edge02 () const
 
float area () const
 
Vector3::Axis primaryAxis () const
 
const Vector3normal () const
 
Point3 center () const
 
const Planeplane () const
 
Point3 randomPoint () const
 
void getRandomSurfacePoint (Point3 &P, Vector3 &N=Vector3::ignore()) const
 
bool operator== (const Triangle &other) const
 
size_t hashCode () const
 
void getBounds (class AABox &) const
 
bool intersect (const class Ray &ray, float &distance, float baryCoord[3]) const
 Intersect the ray at distance less than distance. More...
 

Private Member Functions

void init (const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
 

Private Attributes

Vector3 _vertex [3]
 
Vector3 edgeDirection [3]
 
float edgeMagnitude [3]
 
Plane _plane
 
Vector3::Axis _primaryAxis
 
Vector3 _edge01
 
Vector3 _edge02
 
float _area
 

Friends

class CollisionDetection
 
class Ray
 

Detailed Description

A generic triangle representation. This should not be used as the underlying triangle for creating models; it is intended for providing fast property queries but requires a lot of storage and is mostly immutable.

Constructor & Destructor Documentation

G3D::Triangle::Triangle ( class BinaryInput b)
68  {
69  deserialize(b);
70 }
void deserialize(class BinaryInput &b)
Definition: Triangle.cpp:80

+ Here is the call graph for this function:

G3D::Triangle::Triangle ( )
54  {
56 }
static const Vector3 & zero()
Definition: Vector3.cpp:119
void init(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
Definition: Triangle.cpp:25

+ Here is the call graph for this function:

G3D::Triangle::Triangle ( const Point3 v0,
const Point3 v1,
const Point3 v2 
)
59  {
60  init(v0, v1, v2);
61 }
void init(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
Definition: Triangle.cpp:25

+ Here is the call graph for this function:

G3D::Triangle::~Triangle ( )
64  {
65 }

Member Function Documentation

float G3D::Triangle::area ( ) const
88  {
89  return _area;
90 }
float _area
Definition: Triangle.h:53
Vector3 G3D::Triangle::center ( ) const

Barycenter

103  {
104  return (_vertex[0] + _vertex[1] + _vertex[2]) / 3.0;
105 }
Vector3 _vertex[3]
Definition: Triangle.h:39
void G3D::Triangle::deserialize ( class BinaryInput b)
80  {
81  _vertex[0].deserialize(b);
82  _vertex[1].deserialize(b);
83  _vertex[2].deserialize(b);
84  init(_vertex[0], _vertex[1], _vertex[2]);
85 }
void deserialize(class BinaryInput &b)
Definition: Vector3.cpp:190
Vector3 _vertex[3]
Definition: Triangle.h:39
void init(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
Definition: Triangle.cpp:25

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const Vector3& G3D::Triangle::edge01 ( ) const
inline

vertex[1] - vertex[0]

76  {
77  return _edge01;
78  }
Vector3 _edge01
Definition: Triangle.h:48

+ Here is the caller graph for this function:

const Vector3& G3D::Triangle::edge02 ( ) const
inline

vertex[2] - vertex[0]

81  {
82  return _edge02;
83  }
Vector3 _edge02
Definition: Triangle.h:51

+ Here is the caller graph for this function:

void G3D::Triangle::getBounds ( class AABox out) const
124  {
125  Vector3 lo = _vertex[0];
126  Vector3 hi = lo;
127 
128  for (int i = 1; i < 3; ++i) {
129  lo = lo.min(_vertex[i]);
130  hi = hi.max(_vertex[i]);
131  }
132 
133  out = AABox(lo, hi);
134 }
Vector3 __fastcall max(const Vector3 &v) const
Definition: Vector3.h:794
Vector3 __fastcall min(const Vector3 &v) const
Definition: Vector3.h:789
Vector3 _vertex[3]
Definition: Triangle.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::Triangle::getRandomSurfacePoint ( Point3 P,
Vector3 N = Vector3::ignore() 
) const
inline
103  {
104  P = randomPoint();
105  N = normal();
106  }
const Vector3 & normal() const
Definition: Triangle.cpp:93
Point3 randomPoint() const
Definition: Triangle.cpp:107
uint8 const P[]
Definition: AuthenticationPackets.cpp:225

+ Here is the call graph for this function:

size_t G3D::Triangle::hashCode ( ) const
inline
123  {
124  return
125  _vertex[0].hashCode() +
126  (_vertex[1].hashCode() >> 2) +
127  (_vertex[2].hashCode() >> 3);
128  }
size_t hashCode() const
Definition: Vector3.cpp:155
size_t hashCode() const
Definition: Triangle.h:123
Vector3 _vertex[3]
Definition: Triangle.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::Triangle::init ( const Vector3 v0,
const Vector3 v1,
const Vector3 v2 
)
private
25  {
26 
27  _plane = Plane(v0, v1, v2);
28  _vertex[0] = v0;
29  _vertex[1] = v1;
30  _vertex[2] = v2;
31 
32  static int next[] = {1,2,0};
33 
34  for (int i = 0; i < 3; ++i) {
35  const Vector3& e = _vertex[next[i]] - _vertex[i];
36  edgeMagnitude[i] = e.magnitude();
37 
38  if (edgeMagnitude[i] == 0) {
40  } else {
41  edgeDirection[i] = e / (float)edgeMagnitude[i];
42  }
43  }
44 
45  _edge01 = _vertex[1] - _vertex[0];
46  _edge02 = _vertex[2] - _vertex[0];
47 
50  //0.5f * (_vertex[1] - _vertex[0]).cross(_vertex[2] - _vertex[0]).dot(_plane.normal());
51 }
const Vector3 & normal() const
Definition: Plane.h:124
float _area
Definition: Triangle.h:53
float edgeMagnitude[3]
Definition: Triangle.h:43
int next(int i, int n)
Definition: RecastContour.cpp:469
Plane _plane
Definition: Triangle.h:44
Axis primaryAxis() const
Definition: Vector3.cpp:129
Vector3 edgeDirection[3]
Definition: Triangle.h:42
static const Vector3 & zero()
Definition: Vector3.cpp:119
Vector3::Axis _primaryAxis
Definition: Triangle.h:45
float magnitude() const
Definition: Vector3.h:746
Vector3 __fastcall cross(const Vector3 &rkVector) const
Definition: Vector3.h:776
Vector3 _vertex[3]
Definition: Triangle.h:39
Vector3 _edge01
Definition: Triangle.h:48
Vector3 _edge02
Definition: Triangle.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool G3D::Triangle::intersect ( const class Ray ray,
float &  distance,
float  baryCoord[3] 
) const

Intersect the ray at distance less than distance.

Parameters
distanceSet to the maximum distance (can be G3D::inf()) to search for an intersection. On return, this is the smaller of the distance to the intersection, if one exists, and the original value.
baryCoordIf a triangle is hit before distance, a the barycentric coordinates of the hit location on the triangle. Otherwise, unmodified.
Returns
True if there was an intersection before the original distance.
137  {
138  static const float EPS = 1e-5f;
139 
140  // See RTR2 ch. 13.7 for the algorithm.
141 
142  const Vector3& e1 = edge01();
143  const Vector3& e2 = edge02();
144  const Vector3 p(ray.direction().cross(e2));
145  const float a = e1.dot(p);
146 
147  if (abs(a) < EPS) {
148  // Determinant is ill-conditioned; abort early
149  return false;
150  }
151 
152  const float f = 1.0f / a;
153  const Vector3 s(ray.origin() - vertex(0));
154  const float u = f * s.dot(p);
155 
156  if ((u < 0.0f) || (u > 1.0f)) {
157  // We hit the plane of the m_geometry, but outside the m_geometry
158  return false;
159  }
160 
161  const Vector3 q(s.cross(e1));
162  const float v = f * ray.direction().dot(q);
163 
164  if ((v < 0.0f) || ((u + v) > 1.0f)) {
165  // We hit the plane of the triangle, but outside the triangle
166  return false;
167  }
168 
169  const float t = f * e2.dot(q);
170 
171  if ((t > 0.0f) && (t < distance)) {
172  // This is a new hit, closer than the previous one
173  distance = t;
174 
175  baryCoord[0] = 1.0f - u - v;
176  baryCoord[1] = u;
177  baryCoord[2] = v;
178 
179  return true;
180  } else {
181  // This hit is after the previous hit, so ignore it
182  return false;
183  }
184 }
double abs(double fValue)
Definition: g3dmath.h:617
double distance(double x, double y)
Definition: g3dmath.h:731
const Vector3 & edge02() const
Definition: Triangle.h:81
const Vector3 & edge01() const
Definition: Triangle.h:76
const Point3 & vertex(int n) const
Definition: Triangle.h:70

+ Here is the call graph for this function:

const Vector3 & G3D::Triangle::normal ( ) const
93  {
94  return _plane.normal();
95 }
const Vector3 & normal() const
Definition: Plane.h:124
Plane _plane
Definition: Triangle.h:44

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool G3D::Triangle::operator== ( const Triangle other) const
inline

For two triangles to be equal they must have the same vertices in the same order. That is, vertex[0] == vertex[0], etc.

113  {
114  for (int i = 0; i < 3; ++i) {
115  if (_vertex[i] != other._vertex[i]) {
116  return false;
117  }
118  }
119 
120  return true;
121  }
Vector3 _vertex[3]
Definition: Triangle.h:39
const Plane & G3D::Triangle::plane ( ) const
98  {
99  return _plane;
100 }
Plane _plane
Definition: Triangle.h:44

+ Here is the caller graph for this function:

Vector3::Axis G3D::Triangle::primaryAxis ( ) const
inline
87  {
88  return _primaryAxis;
89  }
Vector3::Axis _primaryAxis
Definition: Triangle.h:45

+ Here is the caller graph for this function:

Vector3 G3D::Triangle::randomPoint ( ) const

Returns a random point in the triangle.

107  {
108  // Choose a random point in the parallelogram
109 
110  float s = uniformRandom();
111  float t = uniformRandom();
112 
113  if (t > 1.0f - s) {
114  // Outside the triangle; reflect about the
115  // diagonal of the parallelogram
116  t = 1.0f - t;
117  s = 1.0f - s;
118  }
119 
120  return _edge01 * s + _edge02 * t + _vertex[0];
121 }
float uniformRandom(float low=0.0f, float hi=1.0f)
Definition: g3dmath.h:694
Vector3 _vertex[3]
Definition: Triangle.h:39
Vector3 _edge01
Definition: Triangle.h:48
Vector3 _edge02
Definition: Triangle.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::Triangle::serialize ( class BinaryOutput b)
73  {
74  _vertex[0].serialize(b);
75  _vertex[1].serialize(b);
76  _vertex[2].serialize(b);
77 }
void serialize(class BinaryOutput &b) const
Definition: Vector3.cpp:219
Vector3 _vertex[3]
Definition: Triangle.h:39

+ Here is the call graph for this function:

const Point3& G3D::Triangle::vertex ( int  n) const
inline

0, 1, or 2

70  {
71  debugAssert((n >= 0) && (n < 3));
72  return _vertex[n];
73  }
#define debugAssert(exp)
Definition: debugAssert.h:160
Vector3 _vertex[3]
Definition: Triangle.h:39

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class CollisionDetection
friend
friend class Ray
friend

Member Data Documentation

float G3D::Triangle::_area
private
Vector3 G3D::Triangle::_edge01
private

vertex[1] - vertex[0]

Vector3 G3D::Triangle::_edge02
private

vertex[2] - vertex[0]

Plane G3D::Triangle::_plane
private
Vector3::Axis G3D::Triangle::_primaryAxis
private
Vector3 G3D::Triangle::_vertex[3]
private
Vector3 G3D::Triangle::edgeDirection[3]
private

edgeDirection[i] is the normalized vector v[i+1] - v[i]

float G3D::Triangle::edgeMagnitude[3]
private

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