[372] | 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"
|
---|
[863] | 11 | #include "Containers.h"
|
---|
[372] | 12 |
|
---|
[863] | 13 |
|
---|
[860] | 14 | namespace GtpVisibilityPreprocessor {
|
---|
| 15 |
|
---|
[878] | 16 | extern bool MeshDebug;
|
---|
| 17 |
|
---|
[372] | 18 | struct Triangle3;
|
---|
| 19 | class MeshInstance;
|
---|
| 20 | class MeshKdTree;
|
---|
| 21 |
|
---|
| 22 | /// default vertex container for Mesh
|
---|
| 23 | typedef std::vector<Vector3> VertexContainer;
|
---|
| 24 |
|
---|
| 25 | /// vertex index container
|
---|
[870] | 26 | //typedef std::vector<short> VertexIndexContainer;
|
---|
| 27 | typedef std::vector<int> VertexIndexContainer;
|
---|
[372] | 28 |
|
---|
| 29 | /** Patch used as an element of the mesh */
|
---|
[752] | 30 | struct Face {
|
---|
| 31 |
|
---|
[372] | 32 | public:
|
---|
| 33 | Face(): mVertexIndices() {}
|
---|
[752] | 34 |
|
---|
[372] | 35 | Face(const int a, const int b, const int c):mVertexIndices(3) {
|
---|
| 36 | mVertexIndices[0] = a;
|
---|
| 37 | mVertexIndices[1] = b;
|
---|
| 38 | mVertexIndices[2] = c;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | Face(const int a, const int b, const int c, const int d):
|
---|
| 42 | mVertexIndices(4) {
|
---|
| 43 | mVertexIndices[0] = a;
|
---|
| 44 | mVertexIndices[1] = b;
|
---|
| 45 | mVertexIndices[2] = c;
|
---|
| 46 | mVertexIndices[3] = d;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[752] | 49 | Face(const VertexIndexContainer &vertices):mVertexIndices(vertices.size()) {
|
---|
| 50 | for (int i=0; i < vertices.size(); i++)
|
---|
| 51 | mVertexIndices[i] = vertices[i];
|
---|
| 52 | }
|
---|
[372] | 53 |
|
---|
| 54 | /// list of vertex pointers
|
---|
| 55 | VertexIndexContainer mVertexIndices;
|
---|
| 56 | };
|
---|
| 57 |
|
---|
[863] | 58 |
|
---|
| 59 |
|
---|
| 60 | /// default vertex container for Mesh
|
---|
| 61 | typedef vector<Vector3> VertexContainer;
|
---|
| 62 |
|
---|
[372] | 63 | /// default patch container for Mesh
|
---|
| 64 | typedef std::vector<Face *> FaceContainer;
|
---|
| 65 |
|
---|
| 66 | /** Mesh containing polygonal patches */
|
---|
| 67 | class Mesh {
|
---|
| 68 |
|
---|
| 69 | public:
|
---|
| 70 |
|
---|
| 71 | /// Default constructor
|
---|
[1001] | 72 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL), mId(0) {}
|
---|
| 73 |
|
---|
[372] | 74 | /// Constructor with container preallocation
|
---|
[1001] | 75 | Mesh(const int vertices, const int faces):
|
---|
[372] | 76 | mFaces(),
|
---|
| 77 | mMaterial(NULL),
|
---|
| 78 | mKdTree(NULL),
|
---|
| 79 | mVertices(),
|
---|
| 80 | mIsConvex(false),
|
---|
[1001] | 81 | mIsWatertight(false),
|
---|
| 82 | mId(0)
|
---|
[372] | 83 | {
|
---|
| 84 | mVertices.reserve(vertices);
|
---|
| 85 | mFaces.reserve(faces);
|
---|
| 86 | }
|
---|
[1001] | 87 |
|
---|
| 88 | /** Constructor setting a unqiue mesh id.
|
---|
| 89 | */
|
---|
| 90 | Mesh(const int id);
|
---|
| 91 |
|
---|
| 92 | /** Setting unique mesh id and using preallocation.
|
---|
| 93 | */
|
---|
| 94 | Mesh(const int id, const int vertices, const int faces);
|
---|
| 95 |
|
---|
| 96 | /** Copy constructor making a deep copy of the faces.
|
---|
| 97 | */
|
---|
| 98 | Mesh(const Mesh &rhs);
|
---|
| 99 |
|
---|
| 100 | /** Assignement operator.
|
---|
| 101 | @note does not copy id
|
---|
| 102 | @note preprocess may be necessary
|
---|
| 103 | */
|
---|
| 104 | Mesh& operator=(const Mesh& m);
|
---|
| 105 |
|
---|
[1005] | 106 | ~Mesh();
|
---|
| 107 | void Clear();
|
---|
[752] | 108 |
|
---|
| 109 | void IndexVertices();
|
---|
| 110 |
|
---|
| 111 | void AssignRandomMaterial();
|
---|
[372] | 112 | void AddTriangle(const Triangle3 &triangle);
|
---|
| 113 | void AddRectangle(const Rectangle3 &triangle);
|
---|
| 114 |
|
---|
[693] | 115 | void Cleanup();
|
---|
| 116 |
|
---|
| 117 | bool ValidateFace(const int face);
|
---|
[372] | 118 |
|
---|
| 119 | void AddFace(Face *face)
|
---|
| 120 | {
|
---|
| 121 | mFaces.push_back(face);
|
---|
| 122 | }
|
---|
[859] | 123 |
|
---|
| 124 | void ComputeBoundingBox();
|
---|
[1001] | 125 |
|
---|
| 126 | /** This function must be called after creating the mesh
|
---|
| 127 | because it creates the local kd tree and the bounding box.
|
---|
| 128 | */
|
---|
[372] | 129 | void Preprocess();
|
---|
| 130 |
|
---|
[1001] | 131 | /** Applies transformation to the mesh.
|
---|
| 132 | @note: meshkdtree will most likely be outdated after transformation, thus
|
---|
| 133 | another preprocess might be necessary.
|
---|
| 134 | */
|
---|
| 135 | void ApplyTransformation(const Matrix4x4 &m);
|
---|
| 136 |
|
---|
| 137 | /** Axis aligned bounding box of the mesh in local mesh coordinates.
|
---|
| 138 | */
|
---|
[372] | 139 | AxisAlignedBox3 mBox;
|
---|
| 140 |
|
---|
[1001] | 141 | /** Vertices forming the mesh.
|
---|
| 142 | */
|
---|
[372] | 143 | VertexContainer mVertices;
|
---|
| 144 |
|
---|
[1001] | 145 | /** Patches forming the mesh.
|
---|
| 146 | */
|
---|
[372] | 147 | FaceContainer mFaces;
|
---|
| 148 |
|
---|
[1001] | 149 | /** Global mesh material.
|
---|
| 150 | */
|
---|
[372] | 151 | Material *mMaterial;
|
---|
| 152 |
|
---|
[1001] | 153 | /** true if the mesh is a convex mesh.
|
---|
| 154 | */
|
---|
[372] | 155 | bool mIsConvex;
|
---|
| 156 |
|
---|
[1001] | 157 | /** true if the mesh is a convex mesh.
|
---|
| 158 | */
|
---|
[372] | 159 | bool mIsWatertight;
|
---|
| 160 |
|
---|
| 161 | MeshKdTree *mKdTree;
|
---|
[1001] | 162 |
|
---|
[372] | 163 | int
|
---|
[1001] | 164 | CastRay(Ray &ray, MeshInstance *instance);
|
---|
[372] | 165 |
|
---|
| 166 | int
|
---|
| 167 | CastRayToSelectedFaces(
|
---|
[1001] | 168 | Ray &ray,
|
---|
| 169 | const vector<int> &faces,
|
---|
| 170 | Intersectable *instance
|
---|
| 171 | );
|
---|
[372] | 172 |
|
---|
| 173 | int
|
---|
| 174 | CastRayToFace(
|
---|
[1001] | 175 | const int faceIndex,
|
---|
| 176 | Ray &ray,
|
---|
| 177 | float &nearestT,
|
---|
| 178 | int &nearestFace,
|
---|
| 179 | Intersectable *instance
|
---|
| 180 | );
|
---|
[372] | 181 |
|
---|
| 182 |
|
---|
| 183 | int
|
---|
| 184 | RayFaceIntersection(const int faceIndex,
|
---|
[1001] | 185 | const Ray &ray,
|
---|
| 186 | float &t,
|
---|
| 187 | const float nearestT
|
---|
| 188 | );
|
---|
[372] | 189 |
|
---|
| 190 | Plane3 GetFacePlane(const int faceIndex);
|
---|
| 191 |
|
---|
| 192 | AxisAlignedBox3 GetFaceBox(const int faceIndex);
|
---|
| 193 |
|
---|
| 194 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 195 |
|
---|
[492] | 196 | int
|
---|
| 197 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 198 | Vector3 &normal,
|
---|
| 199 | const Vector3 &viewpoint,
|
---|
| 200 | const int maxTries
|
---|
| 201 | );
|
---|
[1001] | 202 |
|
---|
| 203 | /** Returns unique mesh id.
|
---|
| 204 | */
|
---|
| 205 | int GetId() const
|
---|
| 206 | {
|
---|
| 207 | return mId;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[492] | 210 | virtual ostream &Describe(ostream &s) {
|
---|
| 211 | return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
|
---|
| 212 | }
|
---|
[752] | 213 |
|
---|
[1001] | 214 | /** Creates a mesh from a axis aligned bounding box.
|
---|
| 215 | The mesh is handled from the resource manager.
|
---|
[991] | 216 | */
|
---|
| 217 | friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
|
---|
[1001] | 218 |
|
---|
| 219 |
|
---|
| 220 | protected:
|
---|
| 221 |
|
---|
| 222 | /// Unique id of this mesh.
|
---|
| 223 | int mId;
|
---|
[372] | 224 | };
|
---|
| 225 |
|
---|
[492] | 226 |
|
---|
[372] | 227 | class MeshInstance : public Intersectable {
|
---|
[1001] | 228 |
|
---|
[372] | 229 | public:
|
---|
[1001] | 230 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
|
---|
[372] | 231 | {
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 235 |
|
---|
| 236 | int
|
---|
| 237 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 238 | Vector3 &normal,
|
---|
| 239 | const Vector3 &viewpoint,
|
---|
| 240 | const int maxTries
|
---|
| 241 | );
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | Mesh *GetMesh() { return mMesh; }
|
---|
| 245 |
|
---|
[1020] | 246 | virtual AxisAlignedBox3 GetBox() const {
|
---|
[372] | 247 | return mMesh->mBox;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[1004] | 250 | virtual int CastRay(Ray &ray);
|
---|
[372] | 251 |
|
---|
[1020] | 252 | virtual bool IsConvex() const { return mMesh->mIsConvex; }
|
---|
| 253 | virtual bool IsWatertight() const { return mMesh->mIsWatertight; }
|
---|
[372] | 254 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); }
|
---|
| 255 |
|
---|
[1020] | 256 | virtual int NumberOfFaces() const { return (int)mMesh->mFaces.size(); }
|
---|
[387] | 257 |
|
---|
[372] | 258 | virtual int Type() const { return MESH_INSTANCE; }
|
---|
| 259 |
|
---|
| 260 | virtual int
|
---|
| 261 | CastRay(
|
---|
| 262 | Ray &ray,
|
---|
| 263 | const vector<int> &faces
|
---|
| 264 | );
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | virtual ostream &Describe(ostream &s) {
|
---|
| 268 | s<<"MeshInstance Id="<<GetId();
|
---|
| 269 | return mMesh->Describe(s);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[1001] | 272 | /** Sets the material. this overrides the material from
|
---|
| 273 | the mesh itself.
|
---|
| 274 | */
|
---|
| 275 | void SetMaterial(Material *mat);
|
---|
| 276 |
|
---|
| 277 | /** Returns the material of this mesh instance.
|
---|
| 278 | if not defined, returns the material of the mesh itself.
|
---|
| 279 | */
|
---|
| 280 | Material *GetMaterial() const;
|
---|
| 281 |
|
---|
| 282 | protected:
|
---|
| 283 |
|
---|
| 284 | Mesh *mMesh;
|
---|
| 285 | /** This material overrides the mesh material;
|
---|
| 286 | */
|
---|
| 287 | Material *mMaterial;
|
---|
[372] | 288 | };
|
---|
| 289 |
|
---|
| 290 |
|
---|
[1001] | 291 | /** This mesh instance includes a world transform. Use this
|
---|
| 292 | class if the same mesh should be instantiated on different places.
|
---|
| 293 | */
|
---|
[1004] | 294 | class TransformedMeshInstance: public MeshInstance
|
---|
[372] | 295 | {
|
---|
| 296 | public:
|
---|
[1001] | 297 | TransformedMeshInstance(Mesh *mesh);
|
---|
[372] | 298 |
|
---|
[1020] | 299 | virtual AxisAlignedBox3 GetBox() const;
|
---|
[1001] | 300 |
|
---|
[372] | 301 |
|
---|
[1001] | 302 | virtual int CastRay(Ray &ray);
|
---|
[372] | 303 |
|
---|
[1004] | 304 | virtual int CastRay(Ray &ray, const vector<int> &faces);
|
---|
| 305 |
|
---|
[372] | 306 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
|
---|
| 307 |
|
---|
| 308 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 309 |
|
---|
[1001] | 310 | /** Transforms this mesh instance by m.
|
---|
| 311 | */
|
---|
| 312 | void ApplyWorldTransform(const Matrix4x4 &m);
|
---|
| 313 |
|
---|
| 314 | /** Loads the transformation matrix into this mesh instance.
|
---|
| 315 | */
|
---|
| 316 | void LoadWorldTransform(const Matrix4x4 &m);
|
---|
| 317 |
|
---|
| 318 | /** The transformation is returned in m.
|
---|
| 319 | */
|
---|
[1020] | 320 | void GetWorldTransform(Matrix4x4 &m) const;
|
---|
[1001] | 321 |
|
---|
[1004] | 322 | /** Transforms a mesh according to the stored world transform.
|
---|
[1020] | 323 | @param transformedMesh returns the tranformed mesh.
|
---|
[1002] | 324 | */
|
---|
[1020] | 325 | void GetTransformedMesh(Mesh &transformedMesh) const;
|
---|
[1002] | 326 |
|
---|
[1001] | 327 | protected:
|
---|
| 328 |
|
---|
| 329 | /// the transformation matrix
|
---|
| 330 | Matrix4x4 mWorldTransform;
|
---|
[372] | 331 |
|
---|
| 332 | };
|
---|
| 333 |
|
---|
[860] | 334 | }
|
---|
[372] | 335 |
|
---|
| 336 | #endif
|
---|