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