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