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

Revision 860, 5.3 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
12namespace GtpVisibilityPreprocessor {
13
14struct Triangle3;
15class MeshInstance;
16class MeshKdTree;
17
18/// default vertex container for Mesh
19typedef std::vector<Vector3> VertexContainer;
20
21/// vertex index container
22typedef std::vector<short> VertexIndexContainer;
23
24
25/** Patch used as an element of the mesh */
26struct Face {
27
28public:
29  Face(): mVertexIndices() {}
30
31  Face(const int a, const int b, const int c):mVertexIndices(3) {
32    mVertexIndices[0] = a;
33    mVertexIndices[1] = b;
34    mVertexIndices[2] = c;
35  }
36
37  Face(const int a, const int b, const int c, const int d):
38  mVertexIndices(4) {
39    mVertexIndices[0] = a;
40    mVertexIndices[1] = b;
41    mVertexIndices[2] = c;
42    mVertexIndices[3] = d;
43  }
44
45  Face(const VertexIndexContainer &vertices):mVertexIndices(vertices.size()) {
46        for (int i=0;  i < vertices.size(); i++)
47          mVertexIndices[i] = vertices[i];
48  }
49 
50  /// list of vertex pointers
51  VertexIndexContainer mVertexIndices; 
52};
53
54/// default patch container for Mesh
55typedef std::vector<Face *> FaceContainer;
56
57/** Mesh containing polygonal patches */
58class Mesh {
59
60public:
61
62  /// Default constructor
63  Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL) {}
64 
65  /// Constructor with container preallocation
66  Mesh(const int vertices,
67       const int faces):
68    mFaces(),
69    mMaterial(NULL),
70    mKdTree(NULL),
71    mVertices(),
72    mIsConvex(false),
73    mIsWatertight(false)
74  {
75    mVertices.reserve(vertices);
76    mFaces.reserve(faces);
77  }
78 
79  ~Mesh() {
80    for (int i=0; i < mFaces.size(); i++)
81      delete mFaces[i];
82  }
83 
84
85  void Clear() {
86        mVertices.clear();
87        mFaces.clear();
88  }
89 
90  void IndexVertices();
91 
92  void AssignRandomMaterial();
93  void AddTriangle(const Triangle3 &triangle);
94  void AddRectangle(const Rectangle3 &triangle);
95
96  void Cleanup();
97 
98  bool ValidateFace(const int face);
99
100  void AddFace(Face *face)
101  {
102    mFaces.push_back(face);
103  }
104
105  void ComputeBoundingBox();
106  void Preprocess();
107
108  /** Axis aligned bounding box of the mesh in local mesh coordinates */
109  AxisAlignedBox3 mBox;
110 
111  /** Vertices forming the mesh */
112  VertexContainer mVertices;
113 
114  /** Patches forming the mesh */
115  FaceContainer mFaces;
116 
117  /** Global mesh material */
118  Material *mMaterial;
119 
120  /** true if the mesh is a convex mesh */
121  bool mIsConvex;
122
123  /** true if the mesh is a convex mesh */
124  bool mIsWatertight;
125
126  MeshKdTree *mKdTree;
127 
128  int
129  CastRay(
130                                        Ray &ray,
131                                        MeshInstance *instance
132                                        );
133       
134  int
135  CastRayToSelectedFaces(
136                                                                                                 Ray &ray,
137                                                                                                 const vector<int> &faces,
138                                                                                                 Intersectable *instance
139                                                                                                 );
140       
141  int
142  CastRayToFace(
143                                                                const int faceIndex,
144                                                                Ray &ray,
145                                                                float &nearestT,
146                                                                int &nearestFace,
147                                                                Intersectable *instance
148                                                                );
149       
150 
151  int
152  RayFaceIntersection(const int faceIndex,
153                                                                                        const Ray &ray,
154                                                                                        float &t,
155                                                                                        const float nearestT
156                                                                                        );
157 
158  Plane3 GetFacePlane(const int faceIndex);
159
160  AxisAlignedBox3 GetFaceBox(const int faceIndex);
161
162  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
163
164  int
165  GetRandomVisibleSurfacePoint(Vector3 &point,
166                                                           Vector3 &normal,
167                                                           const Vector3 &viewpoint,
168                                                           const int maxTries
169                                                           );
170 
171  virtual ostream &Describe(ostream &s) {
172        return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
173  }
174 
175  friend Mesh *CreateBox(const AxisAlignedBox3 &box);
176};
177
178
179class MeshInstance : public Intersectable {
180protected:
181  Mesh *mMesh;
182 
183public:
184  MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh)
185  {
186  }
187
188  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
189
190        int
191        GetRandomVisibleSurfacePoint(Vector3 &point,
192                                                                                                                         Vector3 &normal,
193                                                                                                                         const Vector3 &viewpoint,
194                                                                                                                         const int maxTries
195                                                                                                                         );
196
197
198  Mesh *GetMesh() { return mMesh; }
199
200  virtual AxisAlignedBox3 GetBox() {
201    return mMesh->mBox;
202  }
203
204  virtual int
205  CastRay(
206          Ray &ray
207          );
208
209  virtual bool IsConvex() { return mMesh->mIsConvex; }
210  virtual bool IsWatertight() { return mMesh->mIsWatertight; }
211  virtual float IntersectionComplexity() {  return (float)mMesh->mFaces.size(); }
212
213        virtual int NumberOfFaces() const {  return (int)mMesh->mFaces.size(); }
214
215  virtual int Type() const { return MESH_INSTANCE; }
216
217  virtual int
218  CastRay(
219          Ray &ray,
220          const vector<int> &faces
221          );
222
223
224        virtual ostream &Describe(ostream &s) {
225                s<<"MeshInstance Id="<<GetId();
226                return mMesh->Describe(s);
227        }
228       
229};
230
231
232class TransformedMeshInstance : public MeshInstance
233{
234public:
235  TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh)
236  {
237    mWorldTransform = IdentityMatrix();
238  }
239 
240  virtual AxisAlignedBox3 GetBox() {
241    return Transform(mMesh->mBox,
242                     mWorldTransform);
243  }
244 
245  virtual int
246  CastRay(
247          Ray &ray
248          );
249
250  virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
251
252  int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
253
254private:
255  Matrix4x4 mWorldTransform;
256 
257};
258
259}
260
261#endif
Note: See TracBrowser for help on using the repository browser.