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

Revision 1328, 7.4 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[372]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"
[863]11#include "Containers.h"
[372]12
[863]13
[860]14namespace GtpVisibilityPreprocessor {
15
[878]16extern bool MeshDebug;
17
[372]18struct Triangle3;
19class MeshInstance;
20class MeshKdTree;
21
22/// default vertex container for Mesh
23typedef std::vector<Vector3> VertexContainer;
24
25/// vertex index container
[870]26//typedef std::vector<short> VertexIndexContainer;
27typedef std::vector<int> VertexIndexContainer;
[372]28
29/** Patch used as an element of the mesh */
[752]30struct Face {
31
[372]32public:
33  Face(): mVertexIndices() {}
[752]34
[372]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
[752]49  Face(const VertexIndexContainer &vertices):mVertexIndices(vertices.size()) {
50        for (int i=0;  i < vertices.size(); i++)
51          mVertexIndices[i] = vertices[i];
52  }
[372]53 
[1328]54  ////////////////////////////
[1327]55
[372]56  /// list of vertex pointers
57  VertexIndexContainer mVertexIndices; 
58};
59
[863]60
61
62/// default vertex container for Mesh
63typedef vector<Vector3> VertexContainer;
64
[372]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
[1001]74  Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL), mId(0) {}
75
[372]76  /// Constructor with container preallocation
[1001]77  Mesh(const int vertices, const int faces):
[372]78    mFaces(),
79    mMaterial(NULL),
80    mKdTree(NULL),
81    mVertices(),
82    mIsConvex(false),
[1001]83    mIsWatertight(false),
84        mId(0)
[372]85  {
86    mVertices.reserve(vertices);
87    mFaces.reserve(faces);
88  }
[1001]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
[1005]108  ~Mesh();
109  void Clear();
[752]110 
111  void IndexVertices();
112 
113  void AssignRandomMaterial();
[372]114  void AddTriangle(const Triangle3 &triangle);
115  void AddRectangle(const Rectangle3 &triangle);
116
[693]117  void Cleanup();
118 
119  bool ValidateFace(const int face);
[372]120
121  void AddFace(Face *face)
122  {
123    mFaces.push_back(face);
124  }
[859]125
126  void ComputeBoundingBox();
[1001]127
128  /** This function must be called after creating the mesh
129        because it creates the local kd tree and the bounding box.
130        */
[1292]131  void Preprocess(const bool cleanup = true);
[372]132
[1001]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  */
[372]141  AxisAlignedBox3 mBox;
142 
[1001]143  /** Vertices forming the mesh.
144  */
[372]145  VertexContainer mVertices;
146 
[1001]147  /** Patches forming the mesh.
148  */
[372]149  FaceContainer mFaces;
150 
[1001]151  /** Global mesh material.
152  */
[372]153  Material *mMaterial;
154 
[1001]155  /** true if the mesh is a convex mesh.
156  */
[372]157  bool mIsConvex;
158
[1001]159  /** true if the mesh is a convex mesh.
160  */
[372]161  bool mIsWatertight;
162
163  MeshKdTree *mKdTree;
[1001]164
[372]165  int
[1001]166  CastRay(Ray &ray, MeshInstance *instance);
[372]167       
168  int
169  CastRayToSelectedFaces(
[1001]170                                                 Ray &ray,
171                                                 const vector<int> &faces,
172                                                 Intersectable *instance
173                                                 );
[372]174       
175  int
176  CastRayToFace(
[1001]177                                const int faceIndex,
178                                Ray &ray,
179                                float &nearestT,
[1199]180                                Vector3 &nearestNormal,
[1001]181                                int &nearestFace,
182                                Intersectable *instance
183                                );
[372]184       
185 
186  int
187  RayFaceIntersection(const int faceIndex,
[1199]188                                          const Ray &ray,
189                                          float &t,
190                                          Vector3 &normal,
191                                          const float nearestT
192                                          );
[372]193 
194  Plane3 GetFacePlane(const int faceIndex);
195
196  AxisAlignedBox3 GetFaceBox(const int faceIndex);
197
198  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
199
[492]200  int
201  GetRandomVisibleSurfacePoint(Vector3 &point,
202                                                           Vector3 &normal,
203                                                           const Vector3 &viewpoint,
204                                                           const int maxTries
205                                                           );
[1001]206 
207  /** Returns unique mesh id.
208  */
209  int GetId() const
210  {
211          return mId;
212  }
213
[492]214  virtual ostream &Describe(ostream &s) {
215        return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
216  }
[752]217 
[1001]218  /** Creates a mesh from a axis aligned bounding box.
219      The mesh is handled from the resource manager.
[991]220  */
221  friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
[1001]222
223 
224protected:
225
226  /// Unique id of this mesh.
227  int mId;
[372]228};
229
[492]230
[372]231class MeshInstance : public Intersectable {
[1001]232
[372]233public:
[1001]234  MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
[372]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
[1020]250  virtual AxisAlignedBox3 GetBox() const {
[372]251    return mMesh->mBox;
252  }
253
[1004]254  virtual int CastRay(Ray &ray);
[372]255
[1020]256  virtual bool IsConvex() const { return mMesh->mIsConvex; }
257  virtual bool IsWatertight() const { return mMesh->mIsWatertight; }
[372]258  virtual float IntersectionComplexity() {  return (float)mMesh->mFaces.size(); }
259
[1020]260  virtual int NumberOfFaces() const {  return (int)mMesh->mFaces.size(); }
[387]261
[372]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       
[1001]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; 
[372]292};
293
294
[1001]295/** This mesh instance includes a world transform. Use this
296        class if the same mesh should be instantiated on different places.
297*/
[1004]298class TransformedMeshInstance: public MeshInstance
[372]299{
300public:
[1001]301  TransformedMeshInstance(Mesh *mesh);
[372]302 
[1020]303  virtual AxisAlignedBox3 GetBox() const;
[1001]304   
[372]305 
[1001]306  virtual int CastRay(Ray &ray);
[372]307
[1004]308  virtual int CastRay(Ray &ray, const vector<int> &faces);
309
[372]310  virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
311
312  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
313
[1001]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  */
[1020]324  void GetWorldTransform(Matrix4x4 &m) const;
[1001]325
[1004]326  /** Transforms a mesh according to the stored world transform.
[1020]327          @param transformedMesh returns the tranformed mesh.
[1002]328  */
[1020]329  void GetTransformedMesh(Mesh &transformedMesh) const;
[1002]330
[1001]331protected:
332
333        /// the transformation matrix   
334        Matrix4x4 mWorldTransform;
[372]335 
336};
337
[860]338}
[372]339
340#endif
Note: See TracBrowser for help on using the repository browser.