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

Revision 863, 5.5 KB checked in by mattausch, 18 years ago (diff)

working on preprocessor integration
added iv stuff

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