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