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

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