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

Revision 333, 4.0 KB checked in by bittner, 19 years ago (diff)

code merge

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 AddFace(Face *face)
81  {
82    mFaces.push_back(face);
83  }
84 
85  void Preprocess();
86
87  /** Axis aligned bounding box of the mesh in local mesh coordinates */
88  AxisAlignedBox3 mBox;
89 
90  /** Vertices forming the mesh */
91  VertexContainer mVertices;
92 
93  /** Patches forming the mesh */
94  FaceContainer mFaces;
95 
96  /** Global mesh material */
97  Material *mMaterial;
98 
99  /** true if the mesh is a convex mesh */
100  bool mIsConvex;
101
102  /** true if the mesh is a convex mesh */
103  bool mIsWatertight;
104
105  MeshKdTree *mKdTree;
106 
107  int
108  CastRay(
109                                        Ray &ray,
110                                        MeshInstance *instance
111                                        );
112       
113  int
114  CastRayToSelectedFaces(
115                                                                                                 Ray &ray,
116                                                                                                 const vector<int> &faces,
117                                                                                                 Intersectable *instance
118                                                                                                 );
119       
120  int
121  CastRayToFace(
122                                                                const int faceIndex,
123                                                                Ray &ray,
124                                                                float &nearestT,
125                                                                int &nearestFace,
126                                                                Intersectable *instance
127                                                                );
128       
129 
130  int
131  RayFaceIntersection(const int faceIndex,
132                                                                                        const Ray &ray,
133                                                                                        float &t,
134                                                                                        const float nearestT
135                                                                                        );
136 
137  Plane3 GetFacePlane(const int faceIndex);
138
139  AxisAlignedBox3 GetFaceBox(const int faceIndex);
140
141  void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
142
143};
144
145class MeshInstance : public Intersectable {
146protected:
147  Mesh *mMesh;
148 
149public:
150  MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh)
151  {
152  }
153
154  void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
155
156
157  Mesh *GetMesh() { return mMesh; }
158
159  virtual AxisAlignedBox3 GetBox() {
160    return mMesh->mBox;
161  }
162
163  virtual int
164  CastRay(
165          Ray &ray
166          );
167
168  virtual bool IsConvex() { return mMesh->mIsConvex; }
169  virtual bool IsWatertight() { return mMesh->mIsWatertight; }
170  virtual float IntersectionComplexity() {  return (float)mMesh->mFaces.size(); }
171
172  virtual int Type() const { return MESH_INSTANCE; }
173
174  virtual int
175  CastRay(
176          Ray &ray,
177          const vector<int> &faces
178          );
179
180
181};
182
183
184class TransformedMeshInstance : public MeshInstance
185{
186public:
187  TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh)
188  {
189    mWorldTransform = IdentityMatrix();
190  }
191 
192  virtual AxisAlignedBox3 GetBox() {
193    return Transform(mMesh->mBox,
194                     mWorldTransform);
195  }
196 
197  virtual int
198  CastRay(
199          Ray &ray
200          );
201
202  virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
203
204  void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
205
206private:
207  Matrix4x4 mWorldTransform;
208 
209};
210
211
212
213#endif
Note: See TracBrowser for help on using the repository browser.