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): |
---|
34 | mVertexIndices(4) { |
---|
35 | mVertexIndices[0] = a; |
---|
36 | mVertexIndices[1] = b; |
---|
37 | mVertexIndices[2] = c; |
---|
38 | mVertexIndices[3] = d; |
---|
39 | } |
---|
40 | |
---|
41 | Face(const VertexIndexContainer &vertices):mVertexIndices(vertices) {} |
---|
42 | |
---|
43 | /// list of vertex pointers |
---|
44 | VertexIndexContainer mVertexIndices; |
---|
45 | }; |
---|
46 | |
---|
47 | /// default patch container for Mesh |
---|
48 | typedef std::vector<Face *> FaceContainer; |
---|
49 | |
---|
50 | /** Mesh containing polygonal patches */ |
---|
51 | class Mesh { |
---|
52 | |
---|
53 | public: |
---|
54 | |
---|
55 | /// Default constructor |
---|
56 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL) {} |
---|
57 | |
---|
58 | /// Constructor with container preallocation |
---|
59 | Mesh(const int vertices, |
---|
60 | const int faces): |
---|
61 | mFaces(), |
---|
62 | mMaterial(NULL), |
---|
63 | mKdTree(NULL), |
---|
64 | mVertices(), |
---|
65 | mIsConvex(false), |
---|
66 | mIsWatertight(false) |
---|
67 | { |
---|
68 | mVertices.reserve(vertices); |
---|
69 | mFaces.reserve(faces); |
---|
70 | } |
---|
71 | |
---|
72 | ~Mesh() { |
---|
73 | for (int i=0; i < mFaces.size(); i++) |
---|
74 | delete mFaces[i]; |
---|
75 | } |
---|
76 | |
---|
77 | void AddTriangle(const Triangle3 &triangle); |
---|
78 | void AddRectangle(const Rectangle3 &triangle); |
---|
79 | |
---|
80 | void Cleanup(); |
---|
81 | bool |
---|
82 | ValidateFace(const int face); |
---|
83 | |
---|
84 | void AddFace(Face *face) |
---|
85 | { |
---|
86 | mFaces.push_back(face); |
---|
87 | } |
---|
88 | |
---|
89 | void Preprocess(); |
---|
90 | |
---|
91 | /** Axis aligned bounding box of the mesh in local mesh coordinates */ |
---|
92 | AxisAlignedBox3 mBox; |
---|
93 | |
---|
94 | /** Vertices forming the mesh */ |
---|
95 | VertexContainer mVertices; |
---|
96 | |
---|
97 | /** Patches forming the mesh */ |
---|
98 | FaceContainer mFaces; |
---|
99 | |
---|
100 | /** Global mesh material */ |
---|
101 | Material *mMaterial; |
---|
102 | |
---|
103 | /** true if the mesh is a convex mesh */ |
---|
104 | bool mIsConvex; |
---|
105 | |
---|
106 | /** true if the mesh is a convex mesh */ |
---|
107 | bool mIsWatertight; |
---|
108 | |
---|
109 | MeshKdTree *mKdTree; |
---|
110 | |
---|
111 | int |
---|
112 | CastRay( |
---|
113 | Ray &ray, |
---|
114 | MeshInstance *instance |
---|
115 | ); |
---|
116 | |
---|
117 | int |
---|
118 | CastRayToSelectedFaces( |
---|
119 | Ray &ray, |
---|
120 | const vector<int> &faces, |
---|
121 | Intersectable *instance |
---|
122 | ); |
---|
123 | |
---|
124 | int |
---|
125 | CastRayToFace( |
---|
126 | const int faceIndex, |
---|
127 | Ray &ray, |
---|
128 | float &nearestT, |
---|
129 | int &nearestFace, |
---|
130 | Intersectable *instance |
---|
131 | ); |
---|
132 | |
---|
133 | |
---|
134 | int |
---|
135 | RayFaceIntersection(const int faceIndex, |
---|
136 | const Ray &ray, |
---|
137 | float &t, |
---|
138 | const float nearestT |
---|
139 | ); |
---|
140 | |
---|
141 | Plane3 GetFacePlane(const int faceIndex); |
---|
142 | |
---|
143 | AxisAlignedBox3 GetFaceBox(const int faceIndex); |
---|
144 | |
---|
145 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
146 | |
---|
147 | int |
---|
148 | GetRandomVisibleSurfacePoint(Vector3 &point, |
---|
149 | Vector3 &normal, |
---|
150 | const Vector3 &viewpoint, |
---|
151 | const int maxTries |
---|
152 | ); |
---|
153 | |
---|
154 | virtual ostream &Describe(ostream &s) { |
---|
155 | return s<<"Mesh #vertices="<<mVertices.size()<<" #faces="<<mFaces.size(); |
---|
156 | } |
---|
157 | |
---|
158 | }; |
---|
159 | |
---|
160 | class MeshInstance : public Intersectable { |
---|
161 | protected: |
---|
162 | Mesh *mMesh; |
---|
163 | |
---|
164 | public: |
---|
165 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh) |
---|
166 | { |
---|
167 | } |
---|
168 | |
---|
169 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
170 | |
---|
171 | int |
---|
172 | GetRandomVisibleSurfacePoint(Vector3 &point, |
---|
173 | Vector3 &normal, |
---|
174 | const Vector3 &viewpoint, |
---|
175 | const int maxTries |
---|
176 | ); |
---|
177 | |
---|
178 | |
---|
179 | Mesh *GetMesh() { return mMesh; } |
---|
180 | |
---|
181 | virtual AxisAlignedBox3 GetBox() { |
---|
182 | return mMesh->mBox; |
---|
183 | } |
---|
184 | |
---|
185 | virtual int |
---|
186 | CastRay( |
---|
187 | Ray &ray |
---|
188 | ); |
---|
189 | |
---|
190 | virtual bool IsConvex() { return mMesh->mIsConvex; } |
---|
191 | virtual bool IsWatertight() { return mMesh->mIsWatertight; } |
---|
192 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); } |
---|
193 | |
---|
194 | virtual int Type() const { return MESH_INSTANCE; } |
---|
195 | |
---|
196 | virtual int |
---|
197 | CastRay( |
---|
198 | Ray &ray, |
---|
199 | const vector<int> &faces |
---|
200 | ); |
---|
201 | |
---|
202 | |
---|
203 | virtual ostream &Describe(ostream &s) { |
---|
204 | s<<"MeshInstance Id="<<GetId(); |
---|
205 | return mMesh->Describe(s); |
---|
206 | } |
---|
207 | |
---|
208 | }; |
---|
209 | |
---|
210 | |
---|
211 | class TransformedMeshInstance : public MeshInstance |
---|
212 | { |
---|
213 | public: |
---|
214 | TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh) |
---|
215 | { |
---|
216 | mWorldTransform = IdentityMatrix(); |
---|
217 | } |
---|
218 | |
---|
219 | virtual AxisAlignedBox3 GetBox() { |
---|
220 | return Transform(mMesh->mBox, |
---|
221 | mWorldTransform); |
---|
222 | } |
---|
223 | |
---|
224 | virtual int |
---|
225 | CastRay( |
---|
226 | Ray &ray |
---|
227 | ); |
---|
228 | |
---|
229 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; } |
---|
230 | |
---|
231 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
232 | |
---|
233 | private: |
---|
234 | Matrix4x4 mWorldTransform; |
---|
235 | |
---|
236 | }; |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | #endif |
---|