TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Triangle.h
Go to the documentation of this file.
1 
15 #ifndef G3D_Triangle_h
16 #define G3D_Triangle_h
17 
18 #include "G3D/platform.h"
19 #include "G3D/g3dmath.h"
20 #include "G3D/Vector3.h"
21 #include "G3D/Plane.h"
22 #include "G3D/BoundsTrait.h"
23 #include "G3D/debugAssert.h"
24 #include <string>
25 
26 namespace G3D {
27 
34 class Triangle {
35 private:
36  friend class CollisionDetection;
37  friend class Ray;
38 
40 
43  float edgeMagnitude[3];
46 
49 
52 
53  float _area;
54 
55  void init(const Vector3& v0, const Vector3& v1, const Vector3& v2);
56 
57 public:
58 
59  Triangle(class BinaryInput& b);
60  void serialize(class BinaryOutput& b);
61  void deserialize(class BinaryInput& b);
62 
63  Triangle();
64 
65  Triangle(const Point3& v0, const Point3& v1, const Point3& v2);
66 
67  ~Triangle();
68 
70  inline const Point3& vertex(int n) const {
71  debugAssert((n >= 0) && (n < 3));
72  return _vertex[n];
73  }
74 
76  inline const Vector3& edge01() const {
77  return _edge01;
78  }
79 
81  inline const Vector3& edge02() const {
82  return _edge02;
83  }
84 
85  float area() const;
86 
88  return _primaryAxis;
89  }
90 
91  const Vector3& normal() const;
92 
94  Point3 center() const;
95 
96  const Plane& plane() const;
97 
99  Point3 randomPoint() const;
100 
101  inline void getRandomSurfacePoint
102  (Point3& P,
103  Vector3& N = Vector3::ignore()) const {
104  P = randomPoint();
105  N = normal();
106  }
107 
113  inline bool operator==(const Triangle& other) const {
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  }
122 
123  inline size_t hashCode() const {
124  return
125  _vertex[0].hashCode() +
126  (_vertex[1].hashCode() >> 2) +
127  (_vertex[2].hashCode() >> 3);
128  }
129 
130  void getBounds(class AABox&) const;
131 
146  bool intersect(const class Ray& ray, float& distance, float baryCoord[3]) const;
147 };
148 
149 } // namespace G3D
150 
151 template <> struct HashTrait<G3D::Triangle> {
152  static size_t hashCode(const G3D::Triangle& key) { return key.hashCode(); }
153 };
154 
155 
156 template<> struct BoundsTrait<class G3D::Triangle> {
157  static void getBounds(const G3D::Triangle& t, G3D::AABox& out) { t.getBounds(out); }
158 };
159 
160 #endif
bool operator==(const Triangle &other) const
Definition: Triangle.h:113
static size_t hashCode(const G3D::Triangle &key)
Definition: Triangle.h:152
Triangle()
Definition: Triangle.cpp:54
Definition: Plane.h:25
float _area
Definition: Triangle.h:53
Point3 center() const
Definition: Triangle.cpp:103
Definition: BinaryInput.h:69
float edgeMagnitude[3]
Definition: Triangle.h:43
Definition: HashTrait.h:105
Plane _plane
Definition: Triangle.h:44
Definition: AABox.h:25
static Vector3 & ignore()
Definition: Vector3.cpp:114
Axis
Definition: Vector3.h:122
void getRandomSurfacePoint(Point3 &P, Vector3 &N=Vector3::ignore()) const
Definition: Triangle.h:102
Definition: CollisionDetection.h:94
double distance(double x, double y)
Definition: g3dmath.h:731
Definition: Vector3.h:58
Vector3 edgeDirection[3]
Definition: Triangle.h:42
~Triangle()
Definition: Triangle.cpp:64
const Vector3 & edge02() const
Definition: Triangle.h:81
void getBounds(class AABox &) const
Definition: Triangle.cpp:124
void serialize(class BinaryOutput &b)
Definition: Triangle.cpp:73
static void getBounds(const G3D::Triangle &t, G3D::AABox &out)
Definition: Triangle.h:157
float area() const
Definition: Triangle.cpp:88
size_t hashCode() const
Definition: Vector3.cpp:155
Vector3::Axis _primaryAxis
Definition: Triangle.h:45
#define debugAssert(exp)
Definition: debugAssert.h:160
void deserialize(class BinaryInput &b)
Definition: Triangle.cpp:80
size_t hashCode() const
Definition: Triangle.h:123
Definition: Ray.h:24
Definition: AABox.h:32
const Vector3 & edge01() const
Definition: Triangle.h:76
Definition: BoundsTrait.h:17
Vector3 _vertex[3]
Definition: Triangle.h:39
Vector3 _edge01
Definition: Triangle.h:48
Vector3 _edge02
Definition: Triangle.h:51
const Plane & plane() const
Definition: Triangle.cpp:98
const Vector3 & normal() const
Definition: Triangle.cpp:93
Definition: BinaryOutput.h:52
Point3 randomPoint() const
Definition: Triangle.cpp:107
bool intersect(const class Ray &ray, float &distance, float baryCoord[3]) const
Intersect the ray at distance less than distance.
Definition: Triangle.cpp:137
uint8 const P[]
Definition: AuthenticationPackets.cpp:225
const Point3 & vertex(int n) const
Definition: Triangle.h:70
Definition: Triangle.h:34
void init(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
Definition: Triangle.cpp:25
Vector3::Axis primaryAxis() const
Definition: Triangle.h:87