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 | void Clear();
|
---|
108 |
|
---|
109 | void IndexVertices();
|
---|
110 |
|
---|
111 | void AssignRandomMaterial();
|
---|
112 | void AddTriangle(const Triangle3 &triangle);
|
---|
113 | void AddRectangle(const Rectangle3 &triangle);
|
---|
114 |
|
---|
115 | void Cleanup();
|
---|
116 |
|
---|
117 | bool ValidateFace(const int face);
|
---|
118 |
|
---|
119 | void AddFace(Face *face)
|
---|
120 | {
|
---|
121 | mFaces.push_back(face);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void ComputeBoundingBox();
|
---|
125 |
|
---|
126 | /** This function must be called after creating the mesh
|
---|
127 | because it creates the local kd tree and the bounding box.
|
---|
128 | */
|
---|
129 | void Preprocess();
|
---|
130 |
|
---|
131 | /** Applies transformation to the mesh.
|
---|
132 | @note: meshkdtree will most likely be outdated after transformation, thus
|
---|
133 | another preprocess might be necessary.
|
---|
134 | */
|
---|
135 | void ApplyTransformation(const Matrix4x4 &m);
|
---|
136 |
|
---|
137 | /** Axis aligned bounding box of the mesh in local mesh coordinates.
|
---|
138 | */
|
---|
139 | AxisAlignedBox3 mBox;
|
---|
140 |
|
---|
141 | /** Vertices forming the mesh.
|
---|
142 | */
|
---|
143 | VertexContainer mVertices;
|
---|
144 |
|
---|
145 | /** Patches forming the mesh.
|
---|
146 | */
|
---|
147 | FaceContainer mFaces;
|
---|
148 |
|
---|
149 | /** Global mesh material.
|
---|
150 | */
|
---|
151 | Material *mMaterial;
|
---|
152 |
|
---|
153 | /** true if the mesh is a convex mesh.
|
---|
154 | */
|
---|
155 | bool mIsConvex;
|
---|
156 |
|
---|
157 | /** true if the mesh is a convex mesh.
|
---|
158 | */
|
---|
159 | bool mIsWatertight;
|
---|
160 |
|
---|
161 | MeshKdTree *mKdTree;
|
---|
162 |
|
---|
163 | int
|
---|
164 | CastRay(Ray &ray, MeshInstance *instance);
|
---|
165 |
|
---|
166 | int
|
---|
167 | CastRayToSelectedFaces(
|
---|
168 | Ray &ray,
|
---|
169 | const vector<int> &faces,
|
---|
170 | Intersectable *instance
|
---|
171 | );
|
---|
172 |
|
---|
173 | int
|
---|
174 | CastRayToFace(
|
---|
175 | const int faceIndex,
|
---|
176 | Ray &ray,
|
---|
177 | float &nearestT,
|
---|
178 | Vector3 &nearestNormal,
|
---|
179 | int &nearestFace,
|
---|
180 | Intersectable *instance
|
---|
181 | );
|
---|
182 |
|
---|
183 |
|
---|
184 | int
|
---|
185 | RayFaceIntersection(const int faceIndex,
|
---|
186 | const Ray &ray,
|
---|
187 | float &t,
|
---|
188 | Vector3 &normal,
|
---|
189 | const float nearestT
|
---|
190 | );
|
---|
191 |
|
---|
192 | Plane3 GetFacePlane(const int faceIndex);
|
---|
193 |
|
---|
194 | AxisAlignedBox3 GetFaceBox(const int faceIndex);
|
---|
195 |
|
---|
196 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
197 |
|
---|
198 | int
|
---|
199 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
200 | Vector3 &normal,
|
---|
201 | const Vector3 &viewpoint,
|
---|
202 | const int maxTries
|
---|
203 | );
|
---|
204 |
|
---|
205 | /** Returns unique mesh id.
|
---|
206 | */
|
---|
207 | int GetId() const
|
---|
208 | {
|
---|
209 | return mId;
|
---|
210 | }
|
---|
211 |
|
---|
212 | virtual ostream &Describe(ostream &s) {
|
---|
213 | return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** Creates a mesh from a axis aligned bounding box.
|
---|
217 | The mesh is handled from the resource manager.
|
---|
218 | */
|
---|
219 | friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
|
---|
220 |
|
---|
221 |
|
---|
222 | protected:
|
---|
223 |
|
---|
224 | /// Unique id of this mesh.
|
---|
225 | int mId;
|
---|
226 | };
|
---|
227 |
|
---|
228 |
|
---|
229 | class MeshInstance : public Intersectable {
|
---|
230 |
|
---|
231 | public:
|
---|
232 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
|
---|
233 | {
|
---|
234 | }
|
---|
235 |
|
---|
236 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
237 |
|
---|
238 | int
|
---|
239 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
240 | Vector3 &normal,
|
---|
241 | const Vector3 &viewpoint,
|
---|
242 | const int maxTries
|
---|
243 | );
|
---|
244 |
|
---|
245 |
|
---|
246 | Mesh *GetMesh() { return mMesh; }
|
---|
247 |
|
---|
248 | virtual AxisAlignedBox3 GetBox() const {
|
---|
249 | return mMesh->mBox;
|
---|
250 | }
|
---|
251 |
|
---|
252 | virtual int CastRay(Ray &ray);
|
---|
253 |
|
---|
254 | virtual bool IsConvex() const { return mMesh->mIsConvex; }
|
---|
255 | virtual bool IsWatertight() const { return mMesh->mIsWatertight; }
|
---|
256 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); }
|
---|
257 |
|
---|
258 | virtual int NumberOfFaces() const { return (int)mMesh->mFaces.size(); }
|
---|
259 |
|
---|
260 | virtual int Type() const { return MESH_INSTANCE; }
|
---|
261 |
|
---|
262 | virtual int
|
---|
263 | CastRay(
|
---|
264 | Ray &ray,
|
---|
265 | const vector<int> &faces
|
---|
266 | );
|
---|
267 |
|
---|
268 |
|
---|
269 | virtual ostream &Describe(ostream &s) {
|
---|
270 | s<<"MeshInstance Id="<<GetId();
|
---|
271 | return mMesh->Describe(s);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Sets the material. this overrides the material from
|
---|
275 | the mesh itself.
|
---|
276 | */
|
---|
277 | void SetMaterial(Material *mat);
|
---|
278 |
|
---|
279 | /** Returns the material of this mesh instance.
|
---|
280 | if not defined, returns the material of the mesh itself.
|
---|
281 | */
|
---|
282 | Material *GetMaterial() const;
|
---|
283 |
|
---|
284 | protected:
|
---|
285 |
|
---|
286 | Mesh *mMesh;
|
---|
287 | /** This material overrides the mesh material;
|
---|
288 | */
|
---|
289 | Material *mMaterial;
|
---|
290 | };
|
---|
291 |
|
---|
292 |
|
---|
293 | /** This mesh instance includes a world transform. Use this
|
---|
294 | class if the same mesh should be instantiated on different places.
|
---|
295 | */
|
---|
296 | class TransformedMeshInstance: public MeshInstance
|
---|
297 | {
|
---|
298 | public:
|
---|
299 | TransformedMeshInstance(Mesh *mesh);
|
---|
300 |
|
---|
301 | virtual AxisAlignedBox3 GetBox() const;
|
---|
302 |
|
---|
303 |
|
---|
304 | virtual int CastRay(Ray &ray);
|
---|
305 |
|
---|
306 | virtual int CastRay(Ray &ray, const vector<int> &faces);
|
---|
307 |
|
---|
308 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
|
---|
309 |
|
---|
310 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
311 |
|
---|
312 | /** Transforms this mesh instance by m.
|
---|
313 | */
|
---|
314 | void ApplyWorldTransform(const Matrix4x4 &m);
|
---|
315 |
|
---|
316 | /** Loads the transformation matrix into this mesh instance.
|
---|
317 | */
|
---|
318 | void LoadWorldTransform(const Matrix4x4 &m);
|
---|
319 |
|
---|
320 | /** The transformation is returned in m.
|
---|
321 | */
|
---|
322 | void GetWorldTransform(Matrix4x4 &m) const;
|
---|
323 |
|
---|
324 | /** Transforms a mesh according to the stored world transform.
|
---|
325 | @param transformedMesh returns the tranformed mesh.
|
---|
326 | */
|
---|
327 | void GetTransformedMesh(Mesh &transformedMesh) const;
|
---|
328 |
|
---|
329 | protected:
|
---|
330 |
|
---|
331 | /// the transformation matrix
|
---|
332 | Matrix4x4 mWorldTransform;
|
---|
333 |
|
---|
334 | };
|
---|
335 |
|
---|
336 | }
|
---|
337 |
|
---|
338 | #endif
|
---|