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