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

Revision 878, 5.5 KB checked in by bittner, 18 years ago (diff)

mesh inntersection bug fixed

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  friend Mesh *CreateBox(const AxisAlignedBox3 &box);
185};
186
187
188class MeshInstance : public Intersectable {
189protected:
190  Mesh *mMesh;
191 
192public:
193  MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh)
194  {
195  }
196
197  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
198
199        int
200        GetRandomVisibleSurfacePoint(Vector3 &point,
201                                                                                                                         Vector3 &normal,
202                                                                                                                         const Vector3 &viewpoint,
203                                                                                                                         const int maxTries
204                                                                                                                         );
205
206
207  Mesh *GetMesh() { return mMesh; }
208
209  virtual AxisAlignedBox3 GetBox() {
210    return mMesh->mBox;
211  }
212
213  virtual int
214  CastRay(
215          Ray &ray
216          );
217
218  virtual bool IsConvex() { return mMesh->mIsConvex; }
219  virtual bool IsWatertight() { return mMesh->mIsWatertight; }
220  virtual float IntersectionComplexity() {  return (float)mMesh->mFaces.size(); }
221
222        virtual int NumberOfFaces() const {  return (int)mMesh->mFaces.size(); }
223
224  virtual int Type() const { return MESH_INSTANCE; }
225
226  virtual int
227  CastRay(
228          Ray &ray,
229          const vector<int> &faces
230          );
231
232
233        virtual ostream &Describe(ostream &s) {
234                s<<"MeshInstance Id="<<GetId();
235                return mMesh->Describe(s);
236        }
237       
238};
239
240
241class TransformedMeshInstance : public MeshInstance
242{
243public:
244  TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh)
245  {
246    mWorldTransform = IdentityMatrix();
247  }
248 
249  virtual AxisAlignedBox3 GetBox() {
250    return Transform(mMesh->mBox,
251                     mWorldTransform);
252  }
253 
254  virtual int
255  CastRay(
256          Ray &ray
257          );
258
259  virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
260
261  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
262
263private:
264  Matrix4x4 mWorldTransform;
265 
266};
267
268}
269
270#endif
Note: See TracBrowser for help on using the repository browser.