[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); |
---|
[162] | 79 | |
---|
| 80 | void AddFace(Face *face) |
---|
| 81 | { |
---|
| 82 | mFaces.push_back(face); |
---|
| 83 | } |
---|
[170] | 84 | |
---|
[162] | 85 | void Preprocess(); |
---|
| 86 | |
---|
[176] | 87 | /** Axis aligned bounding box of the mesh in local mesh coordinates */ |
---|
| 88 | AxisAlignedBox3 mBox; |
---|
[162] | 89 | |
---|
| 90 | /** Vertices forming the mesh */ |
---|
| 91 | VertexContainer mVertices; |
---|
| 92 | |
---|
| 93 | /** Patches forming the mesh */ |
---|
| 94 | FaceContainer mFaces; |
---|
[170] | 95 | |
---|
[162] | 96 | /** Global mesh material */ |
---|
| 97 | Material *mMaterial; |
---|
[170] | 98 | |
---|
[162] | 99 | /** true if the mesh is a convex mesh */ |
---|
| 100 | bool mIsConvex; |
---|
[170] | 101 | |
---|
[176] | 102 | /** true if the mesh is a convex mesh */ |
---|
| 103 | bool mIsWatertight; |
---|
| 104 | |
---|
[170] | 105 | MeshKdTree *mKdTree; |
---|
[162] | 106 | |
---|
| 107 | int |
---|
| 108 | CastRay( |
---|
[333] | 109 | Ray &ray, |
---|
| 110 | MeshInstance *instance |
---|
| 111 | ); |
---|
| 112 | |
---|
[170] | 113 | int |
---|
| 114 | CastRayToSelectedFaces( |
---|
[333] | 115 | Ray &ray, |
---|
| 116 | const vector<int> &faces, |
---|
| 117 | Intersectable *instance |
---|
| 118 | ); |
---|
| 119 | |
---|
[162] | 120 | int |
---|
[170] | 121 | CastRayToFace( |
---|
[333] | 122 | const int faceIndex, |
---|
| 123 | Ray &ray, |
---|
| 124 | float &nearestT, |
---|
| 125 | int &nearestFace, |
---|
| 126 | Intersectable *instance |
---|
| 127 | ); |
---|
| 128 | |
---|
[170] | 129 | |
---|
| 130 | int |
---|
[162] | 131 | RayFaceIntersection(const int faceIndex, |
---|
[333] | 132 | const Ray &ray, |
---|
| 133 | float &t, |
---|
| 134 | const float nearestT |
---|
| 135 | ); |
---|
[170] | 136 | |
---|
[162] | 137 | Plane3 GetFacePlane(const int faceIndex); |
---|
| 138 | |
---|
[170] | 139 | AxisAlignedBox3 GetFaceBox(const int faceIndex); |
---|
[176] | 140 | |
---|
| 141 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
| 142 | |
---|
[162] | 143 | }; |
---|
| 144 | |
---|
| 145 | class MeshInstance : public Intersectable { |
---|
| 146 | protected: |
---|
| 147 | Mesh *mMesh; |
---|
| 148 | |
---|
| 149 | public: |
---|
[176] | 150 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh) |
---|
[162] | 151 | { |
---|
| 152 | } |
---|
| 153 | |
---|
[176] | 154 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
| 155 | |
---|
| 156 | |
---|
[162] | 157 | Mesh *GetMesh() { return mMesh; } |
---|
| 158 | |
---|
| 159 | virtual AxisAlignedBox3 GetBox() { |
---|
[176] | 160 | return mMesh->mBox; |
---|
[162] | 161 | } |
---|
| 162 | |
---|
| 163 | virtual int |
---|
| 164 | CastRay( |
---|
| 165 | Ray &ray |
---|
| 166 | ); |
---|
[176] | 167 | |
---|
| 168 | virtual bool IsConvex() { return mMesh->mIsConvex; } |
---|
| 169 | virtual bool IsWatertight() { return mMesh->mIsWatertight; } |
---|
[271] | 170 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); } |
---|
[176] | 171 | |
---|
| 172 | virtual int Type() const { return MESH_INSTANCE; } |
---|
| 173 | |
---|
[170] | 174 | virtual int |
---|
| 175 | CastRay( |
---|
| 176 | Ray &ray, |
---|
| 177 | const vector<int> &faces |
---|
| 178 | ); |
---|
[162] | 179 | |
---|
[170] | 180 | |
---|
[162] | 181 | }; |
---|
| 182 | |
---|
| 183 | |
---|
[176] | 184 | class TransformedMeshInstance : public MeshInstance |
---|
[162] | 185 | { |
---|
| 186 | public: |
---|
[176] | 187 | TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh) |
---|
[162] | 188 | { |
---|
| 189 | mWorldTransform = IdentityMatrix(); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | virtual AxisAlignedBox3 GetBox() { |
---|
[176] | 193 | return Transform(mMesh->mBox, |
---|
[162] | 194 | mWorldTransform); |
---|
| 195 | } |
---|
[176] | 196 | |
---|
[162] | 197 | virtual int |
---|
| 198 | CastRay( |
---|
| 199 | Ray &ray |
---|
| 200 | ); |
---|
| 201 | |
---|
[176] | 202 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; } |
---|
| 203 | |
---|
| 204 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
| 205 | |
---|
[162] | 206 | private: |
---|
| 207 | Matrix4x4 mWorldTransform; |
---|
| 208 | |
---|
| 209 | }; |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | #endif |
---|