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

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