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

Revision 991, 5.6 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  /// 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) {}
73 
74  /// Constructor with container preallocation
75  Mesh(const int vertices,
76       const int faces):
77    mFaces(),
78    mMaterial(NULL),
79    mKdTree(NULL),
80    mVertices(),
81    mIsConvex(false),
82    mIsWatertight(false)
83  {
84    mVertices.reserve(vertices);
85    mFaces.reserve(faces);
86  }
87 
88  ~Mesh() {
89    for (int i=0; i < mFaces.size(); i++)
90      delete mFaces[i];
91  }
92 
93
94  void Clear() {
95        mVertices.clear();
96        mFaces.clear();
97  }
98 
99  void IndexVertices();
100 
101  void AssignRandomMaterial();
102  void AddTriangle(const Triangle3 &triangle);
103  void AddRectangle(const Rectangle3 &triangle);
104
105  void Cleanup();
106 
107  bool ValidateFace(const int face);
108
109  void AddFace(Face *face)
110  {
111    mFaces.push_back(face);
112  }
113
114  void ComputeBoundingBox();
115  void Preprocess();
116
117  /** Axis aligned bounding box of the mesh in local mesh coordinates */
118  AxisAlignedBox3 mBox;
119 
120  /** Vertices forming the mesh */
121  VertexContainer mVertices;
122 
123  /** Patches forming the mesh */
124  FaceContainer mFaces;
125 
126  /** Global mesh material */
127  Material *mMaterial;
128 
129  /** true if the mesh is a convex mesh */
130  bool mIsConvex;
131
132  /** true if the mesh is a convex mesh */
133  bool mIsWatertight;
134
135  MeshKdTree *mKdTree;
136 
137  int
138  CastRay(
139                                        Ray &ray,
140                                        MeshInstance *instance
141                                        );
142       
143  int
144  CastRayToSelectedFaces(
145                                                                                                 Ray &ray,
146                                                                                                 const vector<int> &faces,
147                                                                                                 Intersectable *instance
148                                                                                                 );
149       
150  int
151  CastRayToFace(
152                                                                const int faceIndex,
153                                                                Ray &ray,
154                                                                float &nearestT,
155                                                                int &nearestFace,
156                                                                Intersectable *instance
157                                                                );
158       
159 
160  int
161  RayFaceIntersection(const int faceIndex,
162                                                                                        const Ray &ray,
163                                                                                        float &t,
164                                                                                        const float nearestT
165                                                                                        );
166 
167  Plane3 GetFacePlane(const int faceIndex);
168
169  AxisAlignedBox3 GetFaceBox(const int faceIndex);
170
171  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
172
173  int
174  GetRandomVisibleSurfacePoint(Vector3 &point,
175                                                           Vector3 &normal,
176                                                           const Vector3 &viewpoint,
177                                                           const int maxTries
178                                                           );
179 
180  virtual ostream &Describe(ostream &s) {
181        return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
182  }
183 
184  /** Creates a mesh from a axis aligned bounding box
185  */
186  friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
187};
188
189
190class MeshInstance : public Intersectable {
191protected:
192  Mesh *mMesh;
193 
194public:
195  MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh)
196  {
197  }
198
199  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
200
201        int
202        GetRandomVisibleSurfacePoint(Vector3 &point,
203                                                                                                                         Vector3 &normal,
204                                                                                                                         const Vector3 &viewpoint,
205                                                                                                                         const int maxTries
206                                                                                                                         );
207
208
209  Mesh *GetMesh() { return mMesh; }
210
211  virtual AxisAlignedBox3 GetBox() {
212    return mMesh->mBox;
213  }
214
215  virtual int
216  CastRay(
217          Ray &ray
218          );
219
220  virtual bool IsConvex() { return mMesh->mIsConvex; }
221  virtual bool IsWatertight() { return mMesh->mIsWatertight; }
222  virtual float IntersectionComplexity() {  return (float)mMesh->mFaces.size(); }
223
224        virtual int NumberOfFaces() const {  return (int)mMesh->mFaces.size(); }
225
226  virtual int Type() const { return MESH_INSTANCE; }
227
228  virtual int
229  CastRay(
230          Ray &ray,
231          const vector<int> &faces
232          );
233
234
235        virtual ostream &Describe(ostream &s) {
236                s<<"MeshInstance Id="<<GetId();
237                return mMesh->Describe(s);
238        }
239       
240};
241
242
243class TransformedMeshInstance : public MeshInstance
244{
245public:
246  TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh)
247  {
248    mWorldTransform = IdentityMatrix();
249  }
250 
251  virtual AxisAlignedBox3 GetBox() {
252    return Transform(mMesh->mBox,
253                     mWorldTransform);
254  }
255 
256  virtual int
257  CastRay(
258          Ray &ray
259          );
260
261  virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
262
263  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
264
265private:
266  Matrix4x4 mWorldTransform;
267 
268};
269
270}
271
272#endif
Note: See TracBrowser for help on using the repository browser.