1 | /** |
---|
2 | @file Triangle.h |
---|
3 | |
---|
4 | @maintainer Morgan McGuire, matrix@graphics3d.com |
---|
5 | |
---|
6 | @created 2003-04-05 |
---|
7 | @edited 2004-03-14 |
---|
8 | |
---|
9 | @cite Random point method by Greg Turk, Generating random points in triangles. In A. S. Glassner, ed., Graphics Gems, pp. 24-28. Academic Press, 1990 |
---|
10 | |
---|
11 | Copyright 2000-2006, Morgan McGuire. |
---|
12 | All rights reserved. |
---|
13 | */ |
---|
14 | |
---|
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 <string> |
---|
23 | |
---|
24 | namespace G3D { |
---|
25 | |
---|
26 | /** |
---|
27 | A generic triangle representation. This should not be used |
---|
28 | as the underlying triangle for creating models; it is intended |
---|
29 | for providing fast property queries but requires a lot of |
---|
30 | storage and is mostly immutable. |
---|
31 | */ |
---|
32 | class Triangle { |
---|
33 | private: |
---|
34 | friend class CollisionDetection; |
---|
35 | friend class Ray; |
---|
36 | |
---|
37 | Vector3 _vertex[3]; |
---|
38 | |
---|
39 | /** edgeDirection[i] is the normalized vector v[i+1] - v[i] */ |
---|
40 | Vector3 edgeDirection[3]; |
---|
41 | double edgeMagnitude[3]; |
---|
42 | Plane _plane; |
---|
43 | Vector3::Axis _primaryAxis; |
---|
44 | |
---|
45 | /** vertex[1] - vertex[0] */ |
---|
46 | Vector3 edge01; |
---|
47 | /** vertex[2] - vertex[0] */ |
---|
48 | Vector3 edge02; |
---|
49 | |
---|
50 | float _area; |
---|
51 | |
---|
52 | void init(const Vector3& v0, const Vector3& v1, const Vector3& v2); |
---|
53 | |
---|
54 | public: |
---|
55 | |
---|
56 | Triangle(); |
---|
57 | |
---|
58 | Triangle(const Vector3& v0, const Vector3& v1, const Vector3& v2); |
---|
59 | |
---|
60 | ~Triangle(); |
---|
61 | |
---|
62 | /** 0, 1, or 2 */ |
---|
63 | inline const Vector3& vertex(int n) const { |
---|
64 | debugAssert((n >= 0) && (n < 3)); |
---|
65 | return _vertex[n]; |
---|
66 | } |
---|
67 | |
---|
68 | double area() const; |
---|
69 | |
---|
70 | Vector3::Axis primaryAxis() const { |
---|
71 | return _primaryAxis; |
---|
72 | } |
---|
73 | |
---|
74 | const Vector3& normal() const; |
---|
75 | |
---|
76 | /** Barycenter */ |
---|
77 | Vector3 center() const; |
---|
78 | |
---|
79 | const Plane& plane() const; |
---|
80 | |
---|
81 | /** Returns a random point in the triangle. */ |
---|
82 | Vector3 randomPoint() const; |
---|
83 | |
---|
84 | /** |
---|
85 | For two triangles to be equal they must have |
---|
86 | the same vertices <I>in the same order</I>. |
---|
87 | That is, vertex[0] == vertex[0], etc. |
---|
88 | */ |
---|
89 | inline bool operator==(const Triangle& other) const { |
---|
90 | for (int i = 0; i < 3; ++i) { |
---|
91 | if (_vertex[i] != other._vertex[i]) { |
---|
92 | return false; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | return true; |
---|
97 | } |
---|
98 | |
---|
99 | inline unsigned int hashCode() const { |
---|
100 | return |
---|
101 | _vertex[0].hashCode() + |
---|
102 | (_vertex[1].hashCode() >> 2) + |
---|
103 | _vertex[2].hashCode(); |
---|
104 | } |
---|
105 | |
---|
106 | void getBounds(class AABox&) const; |
---|
107 | |
---|
108 | }; |
---|
109 | |
---|
110 | } // namespace |
---|
111 | |
---|
112 | inline unsigned int hashCode(const G3D::Triangle& t) { |
---|
113 | return t.hashCode(); |
---|
114 | } |
---|
115 | |
---|
116 | #endif |
---|