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 | #include "Containers.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | namespace GtpVisibilityPreprocessor {
|
---|
15 |
|
---|
16 | extern bool MeshDebug;
|
---|
17 |
|
---|
18 | struct Triangle3;
|
---|
19 | class MeshInstance;
|
---|
20 | class MeshKdTree;
|
---|
21 |
|
---|
22 | /// default vertex container for Mesh
|
---|
23 | typedef std::vector<Vector3> VertexContainer;
|
---|
24 |
|
---|
25 | /// vertex index container
|
---|
26 | //typedef std::vector<short> VertexIndexContainer;
|
---|
27 | typedef std::vector<int> VertexIndexContainer;
|
---|
28 |
|
---|
29 | /** Patch used as an element of the mesh */
|
---|
30 | struct Face {
|
---|
31 |
|
---|
32 | public:
|
---|
33 | Face(): mVertexIndices() {}
|
---|
34 |
|
---|
35 | Face(const int a, const int b, const int c):mVertexIndices(3) {
|
---|
36 | mVertexIndices[0] = a;
|
---|
37 | mVertexIndices[1] = b;
|
---|
38 | mVertexIndices[2] = c;
|
---|
39 | }
|
---|
40 |
|
---|
41 | Face(const int a, const int b, const int c, const int d):
|
---|
42 | mVertexIndices(4) {
|
---|
43 | mVertexIndices[0] = a;
|
---|
44 | mVertexIndices[1] = b;
|
---|
45 | mVertexIndices[2] = c;
|
---|
46 | mVertexIndices[3] = d;
|
---|
47 | }
|
---|
48 |
|
---|
49 | Face(const VertexIndexContainer &vertices):mVertexIndices(vertices.size()) {
|
---|
50 | for (int i=0; i < vertices.size(); i++)
|
---|
51 | mVertexIndices[i] = vertices[i];
|
---|
52 | }
|
---|
53 |
|
---|
54 | /// list of vertex pointers
|
---|
55 | VertexIndexContainer mVertexIndices;
|
---|
56 | };
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | /// default vertex container for Mesh
|
---|
61 | typedef vector<Vector3> VertexContainer;
|
---|
62 |
|
---|
63 | /// default patch container for Mesh
|
---|
64 | typedef std::vector<Face *> FaceContainer;
|
---|
65 |
|
---|
66 | /** Mesh containing polygonal patches */
|
---|
67 | class Mesh {
|
---|
68 |
|
---|
69 | public:
|
---|
70 |
|
---|
71 | /// Default constructor
|
---|
72 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL), mId(0) {}
|
---|
73 |
|
---|
74 | /// Constructor with container preallocation
|
---|
75 | Mesh(const int vertices, const int faces):
|
---|
76 | mFaces(),
|
---|
77 | mMaterial(NULL),
|
---|
78 | mKdTree(NULL),
|
---|
79 | mVertices(),
|
---|
80 | mIsConvex(false),
|
---|
81 | mIsWatertight(false),
|
---|
82 | mId(0)
|
---|
83 | {
|
---|
84 | mVertices.reserve(vertices);
|
---|
85 | mFaces.reserve(faces);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Constructor setting a unqiue mesh id.
|
---|
89 | */
|
---|
90 | Mesh(const int id);
|
---|
91 |
|
---|
92 | /** Setting unique mesh id and using preallocation.
|
---|
93 | */
|
---|
94 | Mesh(const int id, const int vertices, const int faces);
|
---|
95 |
|
---|
96 | /** Copy constructor making a deep copy of the faces.
|
---|
97 | */
|
---|
98 | Mesh(const Mesh &rhs);
|
---|
99 |
|
---|
100 | /** Assignement operator.
|
---|
101 | @note does not copy id
|
---|
102 | @note preprocess may be necessary
|
---|
103 | */
|
---|
104 | Mesh& operator=(const Mesh& m);
|
---|
105 |
|
---|
106 | ~Mesh() {
|
---|
107 | for (int i=0; i < mFaces.size(); i++)
|
---|
108 | delete mFaces[i];
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | void Clear() {
|
---|
113 | mVertices.clear();
|
---|
114 | mFaces.clear();
|
---|
115 | }
|
---|
116 |
|
---|
117 | void IndexVertices();
|
---|
118 |
|
---|
119 | void AssignRandomMaterial();
|
---|
120 | void AddTriangle(const Triangle3 &triangle);
|
---|
121 | void AddRectangle(const Rectangle3 &triangle);
|
---|
122 |
|
---|
123 | void Cleanup();
|
---|
124 |
|
---|
125 | bool ValidateFace(const int face);
|
---|
126 |
|
---|
127 | void AddFace(Face *face)
|
---|
128 | {
|
---|
129 | mFaces.push_back(face);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void ComputeBoundingBox();
|
---|
133 |
|
---|
134 | /** This function must be called after creating the mesh
|
---|
135 | because it creates the local kd tree and the bounding box.
|
---|
136 | */
|
---|
137 | void Preprocess();
|
---|
138 |
|
---|
139 | /** Applies transformation to the mesh.
|
---|
140 | @note: meshkdtree will most likely be outdated after transformation, thus
|
---|
141 | another preprocess might be necessary.
|
---|
142 | */
|
---|
143 | void ApplyTransformation(const Matrix4x4 &m);
|
---|
144 |
|
---|
145 | /** Axis aligned bounding box of the mesh in local mesh coordinates.
|
---|
146 | */
|
---|
147 | AxisAlignedBox3 mBox;
|
---|
148 |
|
---|
149 | /** Vertices forming the mesh.
|
---|
150 | */
|
---|
151 | VertexContainer mVertices;
|
---|
152 |
|
---|
153 | /** Patches forming the mesh.
|
---|
154 | */
|
---|
155 | FaceContainer mFaces;
|
---|
156 |
|
---|
157 | /** Global mesh material.
|
---|
158 | */
|
---|
159 | Material *mMaterial;
|
---|
160 |
|
---|
161 | /** true if the mesh is a convex mesh.
|
---|
162 | */
|
---|
163 | bool mIsConvex;
|
---|
164 |
|
---|
165 | /** true if the mesh is a convex mesh.
|
---|
166 | */
|
---|
167 | bool mIsWatertight;
|
---|
168 |
|
---|
169 | MeshKdTree *mKdTree;
|
---|
170 |
|
---|
171 | int
|
---|
172 | CastRay(Ray &ray, MeshInstance *instance);
|
---|
173 |
|
---|
174 | int
|
---|
175 | CastRayToSelectedFaces(
|
---|
176 | Ray &ray,
|
---|
177 | const vector<int> &faces,
|
---|
178 | Intersectable *instance
|
---|
179 | );
|
---|
180 |
|
---|
181 | int
|
---|
182 | CastRayToFace(
|
---|
183 | const int faceIndex,
|
---|
184 | Ray &ray,
|
---|
185 | float &nearestT,
|
---|
186 | int &nearestFace,
|
---|
187 | Intersectable *instance
|
---|
188 | );
|
---|
189 |
|
---|
190 |
|
---|
191 | int
|
---|
192 | RayFaceIntersection(const int faceIndex,
|
---|
193 | const Ray &ray,
|
---|
194 | float &t,
|
---|
195 | const float nearestT
|
---|
196 | );
|
---|
197 |
|
---|
198 | Plane3 GetFacePlane(const int faceIndex);
|
---|
199 |
|
---|
200 | AxisAlignedBox3 GetFaceBox(const int faceIndex);
|
---|
201 |
|
---|
202 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
203 |
|
---|
204 | int
|
---|
205 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
206 | Vector3 &normal,
|
---|
207 | const Vector3 &viewpoint,
|
---|
208 | const int maxTries
|
---|
209 | );
|
---|
210 |
|
---|
211 | /** Returns unique mesh id.
|
---|
212 | */
|
---|
213 | int GetId() const
|
---|
214 | {
|
---|
215 | return mId;
|
---|
216 | }
|
---|
217 |
|
---|
218 | virtual ostream &Describe(ostream &s) {
|
---|
219 | return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
|
---|
220 | }
|
---|
221 |
|
---|
222 | /** Creates a mesh from a axis aligned bounding box.
|
---|
223 | The mesh is handled from the resource manager.
|
---|
224 | */
|
---|
225 | friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
|
---|
226 |
|
---|
227 |
|
---|
228 | protected:
|
---|
229 |
|
---|
230 | /// Unique id of this mesh.
|
---|
231 | int mId;
|
---|
232 | };
|
---|
233 |
|
---|
234 |
|
---|
235 | class MeshInstance : public Intersectable {
|
---|
236 |
|
---|
237 | public:
|
---|
238 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
|
---|
239 | {
|
---|
240 | }
|
---|
241 |
|
---|
242 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
243 |
|
---|
244 | int
|
---|
245 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
246 | Vector3 &normal,
|
---|
247 | const Vector3 &viewpoint,
|
---|
248 | const int maxTries
|
---|
249 | );
|
---|
250 |
|
---|
251 |
|
---|
252 | Mesh *GetMesh() { return mMesh; }
|
---|
253 |
|
---|
254 | virtual AxisAlignedBox3 GetBox() {
|
---|
255 | return mMesh->mBox;
|
---|
256 | }
|
---|
257 |
|
---|
258 | virtual int
|
---|
259 | CastRay(
|
---|
260 | Ray &ray
|
---|
261 | );
|
---|
262 |
|
---|
263 | virtual bool IsConvex() { return mMesh->mIsConvex; }
|
---|
264 | virtual bool IsWatertight() { return mMesh->mIsWatertight; }
|
---|
265 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); }
|
---|
266 |
|
---|
267 | virtual int NumberOfFaces() const { return (int)mMesh->mFaces.size(); }
|
---|
268 |
|
---|
269 | virtual int Type() const { return MESH_INSTANCE; }
|
---|
270 |
|
---|
271 | virtual int
|
---|
272 | CastRay(
|
---|
273 | Ray &ray,
|
---|
274 | const vector<int> &faces
|
---|
275 | );
|
---|
276 |
|
---|
277 |
|
---|
278 | virtual ostream &Describe(ostream &s) {
|
---|
279 | s<<"MeshInstance Id="<<GetId();
|
---|
280 | return mMesh->Describe(s);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /** Sets the material. this overrides the material from
|
---|
284 | the mesh itself.
|
---|
285 | */
|
---|
286 | void SetMaterial(Material *mat);
|
---|
287 |
|
---|
288 | /** Returns the material of this mesh instance.
|
---|
289 | if not defined, returns the material of the mesh itself.
|
---|
290 | */
|
---|
291 | Material *GetMaterial() const;
|
---|
292 |
|
---|
293 | protected:
|
---|
294 |
|
---|
295 | Mesh *mMesh;
|
---|
296 | /** This material overrides the mesh material;
|
---|
297 | */
|
---|
298 | Material *mMaterial;
|
---|
299 | };
|
---|
300 |
|
---|
301 |
|
---|
302 | /** This mesh instance includes a world transform. Use this
|
---|
303 | class if the same mesh should be instantiated on different places.
|
---|
304 | */
|
---|
305 | class TransformedMeshInstance : public MeshInstance
|
---|
306 | {
|
---|
307 | public:
|
---|
308 | TransformedMeshInstance(Mesh *mesh);
|
---|
309 |
|
---|
310 | virtual AxisAlignedBox3 GetBox();
|
---|
311 |
|
---|
312 |
|
---|
313 | virtual int CastRay(Ray &ray);
|
---|
314 |
|
---|
315 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
|
---|
316 |
|
---|
317 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
318 |
|
---|
319 | /** Transforms this mesh instance by m.
|
---|
320 | */
|
---|
321 | void ApplyWorldTransform(const Matrix4x4 &m);
|
---|
322 |
|
---|
323 | /** Loads the transformation matrix into this mesh instance.
|
---|
324 | */
|
---|
325 | void LoadWorldTransform(const Matrix4x4 &m);
|
---|
326 |
|
---|
327 | /** The transformation is returned in m.
|
---|
328 | */
|
---|
329 | void GetWorldTransform(Matrix4x4 &m);
|
---|
330 |
|
---|
331 | /** Transforms mesh-
|
---|
332 | @param transformedMesh returns the tranformed mesh.
|
---|
333 | */
|
---|
334 | void GetTransformedMesh(Mesh &transformedMesh);
|
---|
335 |
|
---|
336 | protected:
|
---|
337 |
|
---|
338 | /// the transformation matrix
|
---|
339 | Matrix4x4 mWorldTransform;
|
---|
340 |
|
---|
341 | };
|
---|
342 |
|
---|
343 | }
|
---|
344 |
|
---|
345 | #endif
|
---|