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 ComputeBoundingBox();
|
---|
104 | void Preprocess();
|
---|
105 |
|
---|
106 | /** Axis aligned bounding box of the mesh in local mesh coordinates */
|
---|
107 | AxisAlignedBox3 mBox;
|
---|
108 |
|
---|
109 | /** Vertices forming the mesh */
|
---|
110 | VertexContainer mVertices;
|
---|
111 |
|
---|
112 | /** Patches forming the mesh */
|
---|
113 | FaceContainer mFaces;
|
---|
114 |
|
---|
115 | /** Global mesh material */
|
---|
116 | Material *mMaterial;
|
---|
117 |
|
---|
118 | /** true if the mesh is a convex mesh */
|
---|
119 | bool mIsConvex;
|
---|
120 |
|
---|
121 | /** true if the mesh is a convex mesh */
|
---|
122 | bool mIsWatertight;
|
---|
123 |
|
---|
124 | MeshKdTree *mKdTree;
|
---|
125 |
|
---|
126 | int
|
---|
127 | CastRay(
|
---|
128 | Ray &ray,
|
---|
129 | MeshInstance *instance
|
---|
130 | );
|
---|
131 |
|
---|
132 | int
|
---|
133 | CastRayToSelectedFaces(
|
---|
134 | Ray &ray,
|
---|
135 | const vector<int> &faces,
|
---|
136 | Intersectable *instance
|
---|
137 | );
|
---|
138 |
|
---|
139 | int
|
---|
140 | CastRayToFace(
|
---|
141 | const int faceIndex,
|
---|
142 | Ray &ray,
|
---|
143 | float &nearestT,
|
---|
144 | int &nearestFace,
|
---|
145 | Intersectable *instance
|
---|
146 | );
|
---|
147 |
|
---|
148 |
|
---|
149 | int
|
---|
150 | RayFaceIntersection(const int faceIndex,
|
---|
151 | const Ray &ray,
|
---|
152 | float &t,
|
---|
153 | const float nearestT
|
---|
154 | );
|
---|
155 |
|
---|
156 | Plane3 GetFacePlane(const int faceIndex);
|
---|
157 |
|
---|
158 | AxisAlignedBox3 GetFaceBox(const int faceIndex);
|
---|
159 |
|
---|
160 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
161 |
|
---|
162 | int
|
---|
163 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
164 | Vector3 &normal,
|
---|
165 | const Vector3 &viewpoint,
|
---|
166 | const int maxTries
|
---|
167 | );
|
---|
168 |
|
---|
169 | virtual ostream &Describe(ostream &s) {
|
---|
170 | return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
|
---|
171 | }
|
---|
172 |
|
---|
173 | friend Mesh *CreateBox(const AxisAlignedBox3 &box);
|
---|
174 | };
|
---|
175 |
|
---|
176 |
|
---|
177 | class MeshInstance : public Intersectable {
|
---|
178 | protected:
|
---|
179 | Mesh *mMesh;
|
---|
180 |
|
---|
181 | public:
|
---|
182 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh)
|
---|
183 | {
|
---|
184 | }
|
---|
185 |
|
---|
186 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
187 |
|
---|
188 | int
|
---|
189 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
190 | Vector3 &normal,
|
---|
191 | const Vector3 &viewpoint,
|
---|
192 | const int maxTries
|
---|
193 | );
|
---|
194 |
|
---|
195 |
|
---|
196 | Mesh *GetMesh() { return mMesh; }
|
---|
197 |
|
---|
198 | virtual AxisAlignedBox3 GetBox() {
|
---|
199 | return mMesh->mBox;
|
---|
200 | }
|
---|
201 |
|
---|
202 | virtual int
|
---|
203 | CastRay(
|
---|
204 | Ray &ray
|
---|
205 | );
|
---|
206 |
|
---|
207 | virtual bool IsConvex() { return mMesh->mIsConvex; }
|
---|
208 | virtual bool IsWatertight() { return mMesh->mIsWatertight; }
|
---|
209 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); }
|
---|
210 |
|
---|
211 | virtual int NumberOfFaces() const { return (int)mMesh->mFaces.size(); }
|
---|
212 |
|
---|
213 | virtual int Type() const { return MESH_INSTANCE; }
|
---|
214 |
|
---|
215 | virtual int
|
---|
216 | CastRay(
|
---|
217 | Ray &ray,
|
---|
218 | const vector<int> &faces
|
---|
219 | );
|
---|
220 |
|
---|
221 |
|
---|
222 | virtual ostream &Describe(ostream &s) {
|
---|
223 | s<<"MeshInstance Id="<<GetId();
|
---|
224 | return mMesh->Describe(s);
|
---|
225 | }
|
---|
226 |
|
---|
227 | };
|
---|
228 |
|
---|
229 |
|
---|
230 | class TransformedMeshInstance : public MeshInstance
|
---|
231 | {
|
---|
232 | public:
|
---|
233 | TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh)
|
---|
234 | {
|
---|
235 | mWorldTransform = IdentityMatrix();
|
---|
236 | }
|
---|
237 |
|
---|
238 | virtual AxisAlignedBox3 GetBox() {
|
---|
239 | return Transform(mMesh->mBox,
|
---|
240 | mWorldTransform);
|
---|
241 | }
|
---|
242 |
|
---|
243 | virtual int
|
---|
244 | CastRay(
|
---|
245 | Ray &ray
|
---|
246 | );
|
---|
247 |
|
---|
248 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
|
---|
249 |
|
---|
250 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
251 |
|
---|
252 | private:
|
---|
253 | Matrix4x4 mWorldTransform;
|
---|
254 |
|
---|
255 | };
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 | #endif
|
---|