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