1 | #ifndef _Mesh_H__ |
---|
2 | #define _Mesh_H__ |
---|
3 | |
---|
4 | #include <vector> |
---|
5 | using namespace std; |
---|
6 | #include "Intersectable.h" |
---|
7 | #include "Plane3.h" |
---|
8 | #include "Matrix4x4.h" |
---|
9 | #include "AxisAlignedBox3.h" |
---|
10 | #include "Material.h" |
---|
11 | |
---|
12 | struct Triangle3; |
---|
13 | class MeshInstance; |
---|
14 | class MeshKdTree; |
---|
15 | |
---|
16 | /// default vertex container for Mesh |
---|
17 | typedef std::vector<Vector3> VertexContainer; |
---|
18 | |
---|
19 | /// vertex index container |
---|
20 | typedef std::vector<int> VertexIndexContainer; |
---|
21 | |
---|
22 | |
---|
23 | /** Patch used as an element of the mesh */ |
---|
24 | class Face { |
---|
25 | public: |
---|
26 | Face():mVertexIndices() {} |
---|
27 | Face(const int a, const int b, const int c):mVertexIndices(3) { |
---|
28 | mVertexIndices[0] = a; |
---|
29 | mVertexIndices[1] = b; |
---|
30 | mVertexIndices[2] = c; |
---|
31 | } |
---|
32 | |
---|
33 | Face(const int a, const int b, const int c, const int d):mVertexIndices(4) { |
---|
34 | mVertexIndices[0] = a; |
---|
35 | mVertexIndices[1] = b; |
---|
36 | mVertexIndices[2] = c; |
---|
37 | mVertexIndices[3] = d; |
---|
38 | } |
---|
39 | |
---|
40 | Face(const VertexIndexContainer &vertices):mVertexIndices(vertices) {} |
---|
41 | |
---|
42 | /// list of vertex pointers |
---|
43 | VertexIndexContainer mVertexIndices; |
---|
44 | }; |
---|
45 | |
---|
46 | /// default patch container for Mesh |
---|
47 | typedef std::vector<Face *> FaceContainer; |
---|
48 | |
---|
49 | /** Mesh containing polygonal patches */ |
---|
50 | class Mesh { |
---|
51 | |
---|
52 | public: |
---|
53 | |
---|
54 | /// Default constructor |
---|
55 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL) {} |
---|
56 | |
---|
57 | /// Constructor with container preallocation |
---|
58 | Mesh(const int vertices, |
---|
59 | const int faces): |
---|
60 | mFaces(), |
---|
61 | mMaterial(NULL), |
---|
62 | mKdTree(NULL), |
---|
63 | mVertices(), |
---|
64 | mIsConvex(false), |
---|
65 | mIsWatertight(false) |
---|
66 | { |
---|
67 | mVertices.reserve(vertices); |
---|
68 | mFaces.reserve(faces); |
---|
69 | } |
---|
70 | |
---|
71 | ~Mesh() { |
---|
72 | for (int i=0; i < mFaces.size(); i++) |
---|
73 | delete mFaces[i]; |
---|
74 | } |
---|
75 | |
---|
76 | void AddTriangle(const Triangle3 &triangle); |
---|
77 | void AddRectangle(const Rectangle3 &triangle); |
---|
78 | |
---|
79 | void AddFace(Face *face) |
---|
80 | { |
---|
81 | mFaces.push_back(face); |
---|
82 | } |
---|
83 | |
---|
84 | void Preprocess(); |
---|
85 | |
---|
86 | /** Axis aligned bounding box of the mesh in local mesh coordinates */ |
---|
87 | AxisAlignedBox3 mBox; |
---|
88 | |
---|
89 | /** Vertices forming the mesh */ |
---|
90 | VertexContainer mVertices; |
---|
91 | |
---|
92 | /** Patches forming the mesh */ |
---|
93 | FaceContainer mFaces; |
---|
94 | |
---|
95 | /** Global mesh material */ |
---|
96 | Material *mMaterial; |
---|
97 | |
---|
98 | /** true if the mesh is a convex mesh */ |
---|
99 | bool mIsConvex; |
---|
100 | |
---|
101 | /** true if the mesh is a convex mesh */ |
---|
102 | bool mIsWatertight; |
---|
103 | |
---|
104 | MeshKdTree *mKdTree; |
---|
105 | |
---|
106 | int |
---|
107 | CastRay( |
---|
108 | Ray &ray, |
---|
109 | MeshInstance *instance |
---|
110 | ); |
---|
111 | |
---|
112 | int |
---|
113 | CastRayToSelectedFaces( |
---|
114 | Ray &ray, |
---|
115 | const vector<int> &faces, |
---|
116 | MeshInstance *instance |
---|
117 | ); |
---|
118 | |
---|
119 | int |
---|
120 | CastRayToFace( |
---|
121 | const int faceIndex, |
---|
122 | Ray &ray, |
---|
123 | float &nearestT, |
---|
124 | int &nearestFace, |
---|
125 | MeshInstance *instance |
---|
126 | ); |
---|
127 | |
---|
128 | |
---|
129 | int |
---|
130 | RayFaceIntersection(const int faceIndex, |
---|
131 | const Ray &ray, |
---|
132 | float &t, |
---|
133 | const float nearestT |
---|
134 | ); |
---|
135 | |
---|
136 | Plane3 GetFacePlane(const int faceIndex); |
---|
137 | |
---|
138 | AxisAlignedBox3 GetFaceBox(const int faceIndex); |
---|
139 | |
---|
140 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
141 | |
---|
142 | }; |
---|
143 | |
---|
144 | class MeshInstance : public Intersectable { |
---|
145 | protected: |
---|
146 | Mesh *mMesh; |
---|
147 | |
---|
148 | public: |
---|
149 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh) |
---|
150 | { |
---|
151 | } |
---|
152 | |
---|
153 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
154 | |
---|
155 | |
---|
156 | Mesh *GetMesh() { return mMesh; } |
---|
157 | |
---|
158 | virtual AxisAlignedBox3 GetBox() { |
---|
159 | return mMesh->mBox; |
---|
160 | } |
---|
161 | |
---|
162 | virtual int |
---|
163 | CastRay( |
---|
164 | Ray &ray |
---|
165 | ); |
---|
166 | |
---|
167 | virtual bool IsConvex() { return mMesh->mIsConvex; } |
---|
168 | virtual bool IsWatertight() { return mMesh->mIsWatertight; } |
---|
169 | virtual float IntersectionComplexity() { return mMesh->mFaces.size(); } |
---|
170 | |
---|
171 | virtual int Type() const { return MESH_INSTANCE; } |
---|
172 | |
---|
173 | virtual int |
---|
174 | CastRay( |
---|
175 | Ray &ray, |
---|
176 | const vector<int> &faces |
---|
177 | ); |
---|
178 | |
---|
179 | |
---|
180 | }; |
---|
181 | |
---|
182 | |
---|
183 | class TransformedMeshInstance : public MeshInstance |
---|
184 | { |
---|
185 | public: |
---|
186 | TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh) |
---|
187 | { |
---|
188 | mWorldTransform = IdentityMatrix(); |
---|
189 | } |
---|
190 | |
---|
191 | virtual AxisAlignedBox3 GetBox() { |
---|
192 | return Transform(mMesh->mBox, |
---|
193 | mWorldTransform); |
---|
194 | } |
---|
195 | |
---|
196 | virtual int |
---|
197 | CastRay( |
---|
198 | Ray &ray |
---|
199 | ); |
---|
200 | |
---|
201 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; } |
---|
202 | |
---|
203 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
204 | |
---|
205 | private: |
---|
206 | Matrix4x4 mWorldTransform; |
---|
207 | |
---|
208 | }; |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | #endif |
---|