[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 | |
---|
[191] | 12 | class 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: |
---|
| 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):mVertexIndices(4) { |
---|
| 34 | mVertexIndices[0] = a; |
---|
| 35 | mVertexIndices[1] = b; |
---|
| 36 | mVertexIndices[2] = c; |
---|
| 37 | mVertexIndices[3] = d; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | Face(const VertexIndexContainer &vertices):mVertexIndices(vertices) {} |
---|
| 41 | |
---|
| 42 | /// list of vertex pointers |
---|
| 43 | VertexIndexContainer mVertexIndices; |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | /// default patch container for Mesh |
---|
| 47 | typedef std::vector<Face *> FaceContainer; |
---|
| 48 | |
---|
| 49 | /** Mesh containing polygonal patches */ |
---|
| 50 | class Mesh { |
---|
| 51 | |
---|
| 52 | public: |
---|
| 53 | |
---|
| 54 | /// Default constructor |
---|
[170] | 55 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL) {} |
---|
[162] | 56 | |
---|
| 57 | /// Constructor with container preallocation |
---|
| 58 | Mesh(const int vertices, |
---|
[176] | 59 | const int faces): |
---|
| 60 | mFaces(), |
---|
| 61 | mMaterial(NULL), |
---|
| 62 | mKdTree(NULL), |
---|
| 63 | mVertices(), |
---|
| 64 | mIsConvex(false), |
---|
| 65 | mIsWatertight(false) |
---|
[162] | 66 | { |
---|
| 67 | mVertices.reserve(vertices); |
---|
| 68 | mFaces.reserve(faces); |
---|
| 69 | } |
---|
[170] | 70 | |
---|
[162] | 71 | ~Mesh() { |
---|
| 72 | for (int i=0; i < mFaces.size(); i++) |
---|
| 73 | delete mFaces[i]; |
---|
| 74 | } |
---|
[191] | 75 | |
---|
| 76 | void AddTriangle(const Triangle3 &triangle); |
---|
[162] | 77 | |
---|
| 78 | void AddFace(Face *face) |
---|
| 79 | { |
---|
| 80 | mFaces.push_back(face); |
---|
| 81 | } |
---|
[170] | 82 | |
---|
[162] | 83 | void Preprocess(); |
---|
| 84 | |
---|
[176] | 85 | /** Axis aligned bounding box of the mesh in local mesh coordinates */ |
---|
| 86 | AxisAlignedBox3 mBox; |
---|
[162] | 87 | |
---|
| 88 | /** Vertices forming the mesh */ |
---|
| 89 | VertexContainer mVertices; |
---|
| 90 | |
---|
| 91 | /** Patches forming the mesh */ |
---|
| 92 | FaceContainer mFaces; |
---|
[170] | 93 | |
---|
[162] | 94 | /** Global mesh material */ |
---|
| 95 | Material *mMaterial; |
---|
[170] | 96 | |
---|
[162] | 97 | /** true if the mesh is a convex mesh */ |
---|
| 98 | bool mIsConvex; |
---|
[170] | 99 | |
---|
[176] | 100 | /** true if the mesh is a convex mesh */ |
---|
| 101 | bool mIsWatertight; |
---|
| 102 | |
---|
[170] | 103 | MeshKdTree *mKdTree; |
---|
[162] | 104 | |
---|
| 105 | int |
---|
| 106 | CastRay( |
---|
| 107 | Ray &ray, |
---|
| 108 | MeshInstance *instance |
---|
| 109 | ); |
---|
| 110 | |
---|
[170] | 111 | int |
---|
| 112 | CastRayToSelectedFaces( |
---|
| 113 | Ray &ray, |
---|
| 114 | const vector<int> &faces, |
---|
| 115 | MeshInstance *instance |
---|
| 116 | ); |
---|
[162] | 117 | |
---|
| 118 | int |
---|
[170] | 119 | CastRayToFace( |
---|
| 120 | const int faceIndex, |
---|
| 121 | Ray &ray, |
---|
| 122 | float &nearestT, |
---|
| 123 | int &nearestFace, |
---|
| 124 | MeshInstance *instance |
---|
| 125 | ); |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | int |
---|
[162] | 129 | RayFaceIntersection(const int faceIndex, |
---|
| 130 | const Ray &ray, |
---|
| 131 | float &t, |
---|
| 132 | const float nearestT |
---|
| 133 | ); |
---|
[170] | 134 | |
---|
[162] | 135 | Plane3 GetFacePlane(const int faceIndex); |
---|
| 136 | |
---|
[170] | 137 | AxisAlignedBox3 GetFaceBox(const int faceIndex); |
---|
[176] | 138 | |
---|
| 139 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
| 140 | |
---|
[162] | 141 | }; |
---|
| 142 | |
---|
| 143 | class MeshInstance : public Intersectable { |
---|
| 144 | protected: |
---|
| 145 | Mesh *mMesh; |
---|
| 146 | |
---|
| 147 | public: |
---|
[176] | 148 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh) |
---|
[162] | 149 | { |
---|
| 150 | } |
---|
| 151 | |
---|
[176] | 152 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
| 153 | |
---|
| 154 | |
---|
[162] | 155 | Mesh *GetMesh() { return mMesh; } |
---|
| 156 | |
---|
| 157 | virtual AxisAlignedBox3 GetBox() { |
---|
[176] | 158 | return mMesh->mBox; |
---|
[162] | 159 | } |
---|
| 160 | |
---|
| 161 | virtual int |
---|
| 162 | CastRay( |
---|
| 163 | Ray &ray |
---|
| 164 | ); |
---|
[176] | 165 | |
---|
| 166 | virtual bool IsConvex() { return mMesh->mIsConvex; } |
---|
| 167 | virtual bool IsWatertight() { return mMesh->mIsWatertight; } |
---|
| 168 | virtual float IntersectionComplexity() { return mMesh->mFaces.size(); } |
---|
| 169 | |
---|
| 170 | virtual int Type() const { return MESH_INSTANCE; } |
---|
| 171 | |
---|
[170] | 172 | virtual int |
---|
| 173 | CastRay( |
---|
| 174 | Ray &ray, |
---|
| 175 | const vector<int> &faces |
---|
| 176 | ); |
---|
[162] | 177 | |
---|
[170] | 178 | |
---|
[162] | 179 | }; |
---|
| 180 | |
---|
| 181 | |
---|
[176] | 182 | class TransformedMeshInstance : public MeshInstance |
---|
[162] | 183 | { |
---|
| 184 | public: |
---|
[176] | 185 | TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh) |
---|
[162] | 186 | { |
---|
| 187 | mWorldTransform = IdentityMatrix(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | virtual AxisAlignedBox3 GetBox() { |
---|
[176] | 191 | return Transform(mMesh->mBox, |
---|
[162] | 192 | mWorldTransform); |
---|
| 193 | } |
---|
[176] | 194 | |
---|
[162] | 195 | virtual int |
---|
| 196 | CastRay( |
---|
| 197 | Ray &ray |
---|
| 198 | ); |
---|
| 199 | |
---|
[176] | 200 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; } |
---|
| 201 | |
---|
| 202 | void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); |
---|
| 203 | |
---|
[162] | 204 | private: |
---|
| 205 | Matrix4x4 mWorldTransform; |
---|
| 206 | |
---|
| 207 | }; |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | #endif |
---|