source: GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h @ 1328

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