source: trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.h @ 340

Revision 340, 4.3 KB checked in by bittner, 19 years ago (diff)

mesh cleanup added

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