[162] | 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 | |
---|
[209] | 12 | struct Triangle3; |
---|
[162] | 13 | class MeshInstance; |
---|
[170] | 14 | class MeshKdTree; |
---|
[162] | 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: |
---|
[327] | 26 | Face(): mVertexIndices() {} |
---|
[162] | 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 | |
---|
[327] | 33 | Face(const int a, const int b, const int c, const int d): |
---|
| 34 | mVertexIndices(4) { |
---|
[162] | 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 |
---|
[327] | 44 | VertexIndexContainer mVertexIndices; |
---|
[162] | 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 |
---|
[170] | 56 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL) {} |
---|
[162] | 57 | |
---|
| 58 | /// Constructor with container preallocation |
---|
| 59 | Mesh(const int vertices, |
---|
[176] | 60 | const int faces): |
---|
| 61 | mFaces(), |
---|
| 62 | mMaterial(NULL), |
---|
| 63 | mKdTree(NULL), |
---|
| 64 | mVertices(), |
---|
| 65 | mIsConvex(false), |
---|
| 66 | mIsWatertight(false) |
---|
[162] | 67 | { |
---|
| 68 | mVertices.reserve(vertices); |
---|
| 69 | mFaces.reserve(faces); |
---|
| 70 | } |
---|
[170] | 71 | |
---|
[162] | 72 | ~Mesh() { |
---|
| 73 | for (int i=0; i < mFaces.size(); i++) |
---|
| 74 | delete mFaces[i]; |
---|
| 75 | } |
---|
[191] | 76 | |
---|
| 77 | void AddTriangle(const Triangle3 &triangle); |
---|
[209] | 78 | void AddRectangle(const Rectangle3 &triangle); |
---|
[340] | 79 | |
---|
| 80 | void Cleanup(); |
---|
| 81 | bool |
---|
| 82 | ValidateFace(const int face); |
---|
| 83 | |
---|
[162] | 84 | void AddFace(Face *face) |
---|
| 85 | { |
---|
| 86 | mFaces.push_back(face); |
---|
| 87 | } |
---|
[170] | 88 | |
---|
[162] | 89 | void Preprocess(); |
---|
| 90 | |
---|
[176] | 91 | /** Axis aligned bounding box of the mesh in local mesh coordinates */ |
---|
| 92 | AxisAlignedBox3 mBox; |
---|
[162] | 93 | |
---|
| 94 | /** Vertices forming the mesh */ |
---|
| 95 | VertexContainer mVertices; |
---|
| 96 | |
---|
| 97 | /** Patches forming the mesh */ |
---|
| 98 | FaceContainer mFaces; |
---|
[170] | 99 | |
---|
[162] | 100 | /** Global mesh material */ |
---|
| 101 | Material *mMaterial; |
---|
[170] | 102 | |
---|
[162] | 103 | /** true if the mesh is a convex mesh */ |
---|
| 104 | bool mIsConvex; |
---|
[170] | 105 | |
---|
[176] | 106 | /** true if the mesh is a convex mesh */ |
---|
| 107 | bool mIsWatertight; |
---|
| 108 | |
---|
[170] | 109 | MeshKdTree *mKdTree; |
---|
[162] | 110 | |
---|
| 111 | int |
---|
| 112 | CastRay( |
---|
[333] | 113 | Ray &ray, |
---|
| 114 | MeshInstance *instance |
---|
| 115 | ); |
---|
| 116 | |
---|
[170] | 117 | int |
---|
| 118 | CastRayToSelectedFaces( |
---|
[333] | 119 | Ray &ray, |
---|
| 120 | const vector<int> &faces, |
---|
| 121 | Intersectable *instance |
---|
| 122 | ); |
---|
| 123 | |
---|
[162] | 124 | int |
---|
[170] | 125 | CastRayToFace( |
---|
[333] | 126 | const int faceIndex, |
---|
| 127 | Ray &ray, |
---|
| 128 | float &nearestT, |
---|
| 129 | int &nearestFace, |
---|
| 130 | Intersectable *instance |
---|
| 131 | ); |
---|
| 132 | |
---|
[170] | 133 | |
---|
| 134 | int |
---|
[162] | 135 | RayFaceIntersection(const int faceIndex, |
---|
[333] | 136 | const Ray &ray, |
---|
| 137 | float &t, |
---|
| 138 | const float nearestT |
---|
| 139 | ); |
---|
[170] | 140 | |
---|
[162] | 141 | Plane3 GetFacePlane(const int faceIndex); |
---|
| 142 | |
---|
[170] | 143 | AxisAlignedBox3 GetFaceBox(const int faceIndex); |
---|
[176] | 144 | |
---|
[349] | 145 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
[176] | 146 | |
---|
[354] | 147 | int |
---|
| 148 | GetRandomVisibleSurfacePoint(Vector3 &point, |
---|
| 149 | Vector3 &normal, |
---|
| 150 | const Vector3 &viewpoint, |
---|
| 151 | const int maxTries |
---|
| 152 | ); |
---|
| 153 | |
---|
[340] | 154 | virtual ostream &Describe(ostream &s) { |
---|
| 155 | return s<<"Mesh #vertices="<<mVertices.size()<<" #faces="<<mFaces.size(); |
---|
| 156 | } |
---|
| 157 | |
---|
[162] | 158 | }; |
---|
| 159 | |
---|
| 160 | class MeshInstance : public Intersectable { |
---|
| 161 | protected: |
---|
| 162 | Mesh *mMesh; |
---|
| 163 | |
---|
| 164 | public: |
---|
[176] | 165 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh) |
---|
[162] | 166 | { |
---|
| 167 | } |
---|
| 168 | |
---|
[349] | 169 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
[176] | 170 | |
---|
[354] | 171 | int |
---|
| 172 | GetRandomVisibleSurfacePoint(Vector3 &point, |
---|
| 173 | Vector3 &normal, |
---|
| 174 | const Vector3 &viewpoint, |
---|
| 175 | const int maxTries |
---|
| 176 | ); |
---|
[176] | 177 | |
---|
[354] | 178 | |
---|
[162] | 179 | Mesh *GetMesh() { return mMesh; } |
---|
| 180 | |
---|
| 181 | virtual AxisAlignedBox3 GetBox() { |
---|
[176] | 182 | return mMesh->mBox; |
---|
[162] | 183 | } |
---|
| 184 | |
---|
| 185 | virtual int |
---|
| 186 | CastRay( |
---|
| 187 | Ray &ray |
---|
| 188 | ); |
---|
[176] | 189 | |
---|
| 190 | virtual bool IsConvex() { return mMesh->mIsConvex; } |
---|
| 191 | virtual bool IsWatertight() { return mMesh->mIsWatertight; } |
---|
[271] | 192 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); } |
---|
[176] | 193 | |
---|
| 194 | virtual int Type() const { return MESH_INSTANCE; } |
---|
| 195 | |
---|
[170] | 196 | virtual int |
---|
| 197 | CastRay( |
---|
| 198 | Ray &ray, |
---|
| 199 | const vector<int> &faces |
---|
| 200 | ); |
---|
[162] | 201 | |
---|
[170] | 202 | |
---|
[340] | 203 | virtual ostream &Describe(ostream &s) { |
---|
| 204 | s<<"MeshInstance Id="<<GetId(); |
---|
| 205 | return mMesh->Describe(s); |
---|
| 206 | } |
---|
| 207 | |
---|
[162] | 208 | }; |
---|
| 209 | |
---|
| 210 | |
---|
[176] | 211 | class TransformedMeshInstance : public MeshInstance |
---|
[162] | 212 | { |
---|
| 213 | public: |
---|
[176] | 214 | TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh) |
---|
[162] | 215 | { |
---|
| 216 | mWorldTransform = IdentityMatrix(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | virtual AxisAlignedBox3 GetBox() { |
---|
[176] | 220 | return Transform(mMesh->mBox, |
---|
[162] | 221 | mWorldTransform); |
---|
| 222 | } |
---|
[176] | 223 | |
---|
[162] | 224 | virtual int |
---|
| 225 | CastRay( |
---|
| 226 | Ray &ray |
---|
| 227 | ); |
---|
| 228 | |
---|
[176] | 229 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; } |
---|
| 230 | |
---|
[349] | 231 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
[176] | 232 | |
---|
[162] | 233 | private: |
---|
| 234 | Matrix4x4 mWorldTransform; |
---|
| 235 | |
---|
| 236 | }; |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | #endif |
---|