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

Revision 1020, 7.3 KB checked in by mattausch, 18 years ago (diff)

worked on view-object space partition
fixed some loading bugs
fixeds some exporting bugs using line segments
enabling other methods for view space sampling in ViewCellsManager? OBJECT_DIRECTION_BASED_DISTRIBUTION)
added class interface for a sampling strategy

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  /// list of vertex pointers
55  VertexIndexContainer mVertexIndices; 
56};
57
58
59
60/// default vertex container for Mesh
61typedef vector<Vector3> VertexContainer;
62
63/// default patch container for Mesh
64typedef std::vector<Face *> FaceContainer;
65
66/** Mesh containing polygonal patches */
67class Mesh {
68
69public:
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                                int &nearestFace,
179                                Intersectable *instance
180                                );
181       
182 
183  int
184  RayFaceIntersection(const int faceIndex,
185                                        const Ray &ray,
186                                        float &t,
187                                        const float nearestT
188                                        );
189 
190  Plane3 GetFacePlane(const int faceIndex);
191
192  AxisAlignedBox3 GetFaceBox(const int faceIndex);
193
194  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
195
196  int
197  GetRandomVisibleSurfacePoint(Vector3 &point,
198                                                           Vector3 &normal,
199                                                           const Vector3 &viewpoint,
200                                                           const int maxTries
201                                                           );
202 
203  /** Returns unique mesh id.
204  */
205  int GetId() const
206  {
207          return mId;
208  }
209
210  virtual ostream &Describe(ostream &s) {
211        return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
212  }
213 
214  /** Creates a mesh from a axis aligned bounding box.
215      The mesh is handled from the resource manager.
216  */
217  friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
218
219 
220protected:
221
222  /// Unique id of this mesh.
223  int mId;
224};
225
226
227class MeshInstance : public Intersectable {
228
229public:
230  MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
231  {
232  }
233
234  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
235
236        int
237        GetRandomVisibleSurfacePoint(Vector3 &point,
238                                                                                                                         Vector3 &normal,
239                                                                                                                         const Vector3 &viewpoint,
240                                                                                                                         const int maxTries
241                                                                                                                         );
242
243
244  Mesh *GetMesh() { return mMesh; }
245
246  virtual AxisAlignedBox3 GetBox() const {
247    return mMesh->mBox;
248  }
249
250  virtual int CastRay(Ray &ray);
251
252  virtual bool IsConvex() const { return mMesh->mIsConvex; }
253  virtual bool IsWatertight() const { return mMesh->mIsWatertight; }
254  virtual float IntersectionComplexity() {  return (float)mMesh->mFaces.size(); }
255
256  virtual int NumberOfFaces() const {  return (int)mMesh->mFaces.size(); }
257
258  virtual int Type() const { return MESH_INSTANCE; }
259
260  virtual int
261  CastRay(
262          Ray &ray,
263          const vector<int> &faces
264          );
265
266
267        virtual ostream &Describe(ostream &s) {
268                s<<"MeshInstance Id="<<GetId();
269                return mMesh->Describe(s);
270        }
271       
272        /** Sets the material. this overrides the material from
273                the mesh itself.
274        */
275        void SetMaterial(Material *mat);
276
277        /** Returns the material of this mesh instance.
278                if not defined, returns the material of the mesh itself.
279        */
280        Material *GetMaterial() const;
281
282protected:
283
284        Mesh *mMesh;
285        /** This material overrides the mesh material;
286        */
287        Material *mMaterial; 
288};
289
290
291/** This mesh instance includes a world transform. Use this
292        class if the same mesh should be instantiated on different places.
293*/
294class TransformedMeshInstance: public MeshInstance
295{
296public:
297  TransformedMeshInstance(Mesh *mesh);
298 
299  virtual AxisAlignedBox3 GetBox() const;
300   
301 
302  virtual int CastRay(Ray &ray);
303
304  virtual int CastRay(Ray &ray, const vector<int> &faces);
305
306  virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
307
308  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
309
310  /** Transforms this mesh instance by m.
311  */
312  void ApplyWorldTransform(const Matrix4x4 &m);
313
314  /** Loads the transformation matrix into this mesh instance.
315  */
316  void LoadWorldTransform(const Matrix4x4 &m);
317
318  /** The transformation is returned in m.
319  */
320  void GetWorldTransform(Matrix4x4 &m) const;
321
322  /** Transforms a mesh according to the stored world transform.
323          @param transformedMesh returns the tranformed mesh.
324  */
325  void GetTransformedMesh(Mesh &transformedMesh) const;
326
327protected:
328
329        /// the transformation matrix   
330        Matrix4x4 mWorldTransform;
331 
332};
333
334}
335
336#endif
Note: See TracBrowser for help on using the repository browser.