[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 |
|
---|
[372] | 106 | ~Mesh() {
|
---|
| 107 | for (int i=0; i < mFaces.size(); i++)
|
---|
| 108 | delete mFaces[i];
|
---|
| 109 | }
|
---|
[752] | 110 |
|
---|
[372] | 111 |
|
---|
[752] | 112 | void Clear() {
|
---|
| 113 | mVertices.clear();
|
---|
| 114 | mFaces.clear();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | void IndexVertices();
|
---|
| 118 |
|
---|
| 119 | void AssignRandomMaterial();
|
---|
[372] | 120 | void AddTriangle(const Triangle3 &triangle);
|
---|
| 121 | void AddRectangle(const Rectangle3 &triangle);
|
---|
| 122 |
|
---|
[693] | 123 | void Cleanup();
|
---|
| 124 |
|
---|
| 125 | bool ValidateFace(const int face);
|
---|
[372] | 126 |
|
---|
| 127 | void AddFace(Face *face)
|
---|
| 128 | {
|
---|
| 129 | mFaces.push_back(face);
|
---|
| 130 | }
|
---|
[859] | 131 |
|
---|
| 132 | void ComputeBoundingBox();
|
---|
[1001] | 133 |
|
---|
| 134 | /** This function must be called after creating the mesh
|
---|
| 135 | because it creates the local kd tree and the bounding box.
|
---|
| 136 | */
|
---|
[372] | 137 | void Preprocess();
|
---|
| 138 |
|
---|
[1001] | 139 | /** Applies transformation to the mesh.
|
---|
| 140 | @note: meshkdtree will most likely be outdated after transformation, thus
|
---|
| 141 | another preprocess might be necessary.
|
---|
| 142 | */
|
---|
| 143 | void ApplyTransformation(const Matrix4x4 &m);
|
---|
| 144 |
|
---|
| 145 | /** Axis aligned bounding box of the mesh in local mesh coordinates.
|
---|
| 146 | */
|
---|
[372] | 147 | AxisAlignedBox3 mBox;
|
---|
| 148 |
|
---|
[1001] | 149 | /** Vertices forming the mesh.
|
---|
| 150 | */
|
---|
[372] | 151 | VertexContainer mVertices;
|
---|
| 152 |
|
---|
[1001] | 153 | /** Patches forming the mesh.
|
---|
| 154 | */
|
---|
[372] | 155 | FaceContainer mFaces;
|
---|
| 156 |
|
---|
[1001] | 157 | /** Global mesh material.
|
---|
| 158 | */
|
---|
[372] | 159 | Material *mMaterial;
|
---|
| 160 |
|
---|
[1001] | 161 | /** true if the mesh is a convex mesh.
|
---|
| 162 | */
|
---|
[372] | 163 | bool mIsConvex;
|
---|
| 164 |
|
---|
[1001] | 165 | /** true if the mesh is a convex mesh.
|
---|
| 166 | */
|
---|
[372] | 167 | bool mIsWatertight;
|
---|
| 168 |
|
---|
| 169 | MeshKdTree *mKdTree;
|
---|
[1001] | 170 |
|
---|
[372] | 171 | int
|
---|
[1001] | 172 | CastRay(Ray &ray, MeshInstance *instance);
|
---|
[372] | 173 |
|
---|
| 174 | int
|
---|
| 175 | CastRayToSelectedFaces(
|
---|
[1001] | 176 | Ray &ray,
|
---|
| 177 | const vector<int> &faces,
|
---|
| 178 | Intersectable *instance
|
---|
| 179 | );
|
---|
[372] | 180 |
|
---|
| 181 | int
|
---|
| 182 | CastRayToFace(
|
---|
[1001] | 183 | const int faceIndex,
|
---|
| 184 | Ray &ray,
|
---|
| 185 | float &nearestT,
|
---|
| 186 | int &nearestFace,
|
---|
| 187 | Intersectable *instance
|
---|
| 188 | );
|
---|
[372] | 189 |
|
---|
| 190 |
|
---|
| 191 | int
|
---|
| 192 | RayFaceIntersection(const int faceIndex,
|
---|
[1001] | 193 | const Ray &ray,
|
---|
| 194 | float &t,
|
---|
| 195 | const float nearestT
|
---|
| 196 | );
|
---|
[372] | 197 |
|
---|
| 198 | Plane3 GetFacePlane(const int faceIndex);
|
---|
| 199 |
|
---|
| 200 | AxisAlignedBox3 GetFaceBox(const int faceIndex);
|
---|
| 201 |
|
---|
| 202 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 203 |
|
---|
[492] | 204 | int
|
---|
| 205 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 206 | Vector3 &normal,
|
---|
| 207 | const Vector3 &viewpoint,
|
---|
| 208 | const int maxTries
|
---|
| 209 | );
|
---|
[1001] | 210 |
|
---|
| 211 | /** Returns unique mesh id.
|
---|
| 212 | */
|
---|
| 213 | int GetId() const
|
---|
| 214 | {
|
---|
| 215 | return mId;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[492] | 218 | virtual ostream &Describe(ostream &s) {
|
---|
| 219 | return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
|
---|
| 220 | }
|
---|
[752] | 221 |
|
---|
[1001] | 222 | /** Creates a mesh from a axis aligned bounding box.
|
---|
| 223 | The mesh is handled from the resource manager.
|
---|
[991] | 224 | */
|
---|
| 225 | friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
|
---|
[1001] | 226 |
|
---|
| 227 |
|
---|
| 228 | protected:
|
---|
| 229 |
|
---|
| 230 | /// Unique id of this mesh.
|
---|
| 231 | int mId;
|
---|
[372] | 232 | };
|
---|
| 233 |
|
---|
[492] | 234 |
|
---|
[372] | 235 | class MeshInstance : public Intersectable {
|
---|
[1001] | 236 |
|
---|
[372] | 237 | public:
|
---|
[1001] | 238 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
|
---|
[372] | 239 | {
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 243 |
|
---|
| 244 | int
|
---|
| 245 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 246 | Vector3 &normal,
|
---|
| 247 | const Vector3 &viewpoint,
|
---|
| 248 | const int maxTries
|
---|
| 249 | );
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 | Mesh *GetMesh() { return mMesh; }
|
---|
| 253 |
|
---|
| 254 | virtual AxisAlignedBox3 GetBox() {
|
---|
| 255 | return mMesh->mBox;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | virtual int
|
---|
| 259 | CastRay(
|
---|
| 260 | Ray &ray
|
---|
| 261 | );
|
---|
| 262 |
|
---|
| 263 | virtual bool IsConvex() { return mMesh->mIsConvex; }
|
---|
| 264 | virtual bool IsWatertight() { return mMesh->mIsWatertight; }
|
---|
| 265 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); }
|
---|
| 266 |
|
---|
[406] | 267 | virtual int NumberOfFaces() const { return (int)mMesh->mFaces.size(); }
|
---|
[387] | 268 |
|
---|
[372] | 269 | virtual int Type() const { return MESH_INSTANCE; }
|
---|
| 270 |
|
---|
| 271 | virtual int
|
---|
| 272 | CastRay(
|
---|
| 273 | Ray &ray,
|
---|
| 274 | const vector<int> &faces
|
---|
| 275 | );
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 | virtual ostream &Describe(ostream &s) {
|
---|
| 279 | s<<"MeshInstance Id="<<GetId();
|
---|
| 280 | return mMesh->Describe(s);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[1001] | 283 | /** Sets the material. this overrides the material from
|
---|
| 284 | the mesh itself.
|
---|
| 285 | */
|
---|
| 286 | void SetMaterial(Material *mat);
|
---|
| 287 |
|
---|
| 288 | /** Returns the material of this mesh instance.
|
---|
| 289 | if not defined, returns the material of the mesh itself.
|
---|
| 290 | */
|
---|
| 291 | Material *GetMaterial() const;
|
---|
| 292 |
|
---|
| 293 | protected:
|
---|
| 294 |
|
---|
| 295 | Mesh *mMesh;
|
---|
| 296 | /** This material overrides the mesh material;
|
---|
| 297 | */
|
---|
| 298 | Material *mMaterial;
|
---|
[372] | 299 | };
|
---|
| 300 |
|
---|
| 301 |
|
---|
[1001] | 302 | /** This mesh instance includes a world transform. Use this
|
---|
| 303 | class if the same mesh should be instantiated on different places.
|
---|
| 304 | */
|
---|
[372] | 305 | class TransformedMeshInstance : public MeshInstance
|
---|
| 306 | {
|
---|
| 307 | public:
|
---|
[1001] | 308 | TransformedMeshInstance(Mesh *mesh);
|
---|
[372] | 309 |
|
---|
[1001] | 310 | virtual AxisAlignedBox3 GetBox();
|
---|
| 311 |
|
---|
[372] | 312 |
|
---|
[1001] | 313 | virtual int CastRay(Ray &ray);
|
---|
[372] | 314 |
|
---|
| 315 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
|
---|
| 316 |
|
---|
| 317 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 318 |
|
---|
[1001] | 319 | /** Transforms this mesh instance by m.
|
---|
| 320 | */
|
---|
| 321 | void ApplyWorldTransform(const Matrix4x4 &m);
|
---|
| 322 |
|
---|
| 323 | /** Loads the transformation matrix into this mesh instance.
|
---|
| 324 | */
|
---|
| 325 | void LoadWorldTransform(const Matrix4x4 &m);
|
---|
| 326 |
|
---|
| 327 | /** The transformation is returned in m.
|
---|
| 328 | */
|
---|
| 329 | void GetWorldTransform(Matrix4x4 &m);
|
---|
| 330 |
|
---|
[1002] | 331 | /** Transforms mesh-
|
---|
| 332 | @param transformedMesh returns the tranformed mesh.
|
---|
| 333 | */
|
---|
| 334 | void GetTransformedMesh(Mesh &transformedMesh);
|
---|
| 335 |
|
---|
[1001] | 336 | protected:
|
---|
| 337 |
|
---|
| 338 | /// the transformation matrix
|
---|
| 339 | Matrix4x4 mWorldTransform;
|
---|
[372] | 340 |
|
---|
| 341 | };
|
---|
| 342 |
|
---|
[860] | 343 | }
|
---|
[372] | 344 |
|
---|
| 345 | #endif
|
---|