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

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

added pvs

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