[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,
|
---|
[1199] | 178 | Vector3 &nearestNormal,
|
---|
[1001] | 179 | int &nearestFace,
|
---|
| 180 | Intersectable *instance
|
---|
| 181 | );
|
---|
[372] | 182 |
|
---|
| 183 |
|
---|
| 184 | int
|
---|
| 185 | RayFaceIntersection(const int faceIndex,
|
---|
[1199] | 186 | const Ray &ray,
|
---|
| 187 | float &t,
|
---|
| 188 | Vector3 &normal,
|
---|
| 189 | const float nearestT
|
---|
| 190 | );
|
---|
[372] | 191 |
|
---|
| 192 | Plane3 GetFacePlane(const int faceIndex);
|
---|
| 193 |
|
---|
| 194 | AxisAlignedBox3 GetFaceBox(const int faceIndex);
|
---|
| 195 |
|
---|
| 196 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 197 |
|
---|
[492] | 198 | int
|
---|
| 199 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 200 | Vector3 &normal,
|
---|
| 201 | const Vector3 &viewpoint,
|
---|
| 202 | const int maxTries
|
---|
| 203 | );
|
---|
[1001] | 204 |
|
---|
| 205 | /** Returns unique mesh id.
|
---|
| 206 | */
|
---|
| 207 | int GetId() const
|
---|
| 208 | {
|
---|
| 209 | return mId;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[492] | 212 | virtual ostream &Describe(ostream &s) {
|
---|
| 213 | return s<<"Mesh #vertices="<<(int)mVertices.size()<<" #faces="<<(int)mFaces.size();
|
---|
| 214 | }
|
---|
[752] | 215 |
|
---|
[1001] | 216 | /** Creates a mesh from a axis aligned bounding box.
|
---|
| 217 | The mesh is handled from the resource manager.
|
---|
[991] | 218 | */
|
---|
| 219 | friend Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box);
|
---|
[1001] | 220 |
|
---|
| 221 |
|
---|
| 222 | protected:
|
---|
| 223 |
|
---|
| 224 | /// Unique id of this mesh.
|
---|
| 225 | int mId;
|
---|
[372] | 226 | };
|
---|
| 227 |
|
---|
[492] | 228 |
|
---|
[372] | 229 | class MeshInstance : public Intersectable {
|
---|
[1001] | 230 |
|
---|
[372] | 231 | public:
|
---|
[1001] | 232 | MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh), mMaterial(NULL)
|
---|
[372] | 233 | {
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 237 |
|
---|
| 238 | int
|
---|
| 239 | GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 240 | Vector3 &normal,
|
---|
| 241 | const Vector3 &viewpoint,
|
---|
| 242 | const int maxTries
|
---|
| 243 | );
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 | Mesh *GetMesh() { return mMesh; }
|
---|
| 247 |
|
---|
[1020] | 248 | virtual AxisAlignedBox3 GetBox() const {
|
---|
[372] | 249 | return mMesh->mBox;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[1004] | 252 | virtual int CastRay(Ray &ray);
|
---|
[372] | 253 |
|
---|
[1020] | 254 | virtual bool IsConvex() const { return mMesh->mIsConvex; }
|
---|
| 255 | virtual bool IsWatertight() const { return mMesh->mIsWatertight; }
|
---|
[372] | 256 | virtual float IntersectionComplexity() { return (float)mMesh->mFaces.size(); }
|
---|
| 257 |
|
---|
[1020] | 258 | virtual int NumberOfFaces() const { return (int)mMesh->mFaces.size(); }
|
---|
[387] | 259 |
|
---|
[372] | 260 | virtual int Type() const { return MESH_INSTANCE; }
|
---|
| 261 |
|
---|
| 262 | virtual int
|
---|
| 263 | CastRay(
|
---|
| 264 | Ray &ray,
|
---|
| 265 | const vector<int> &faces
|
---|
| 266 | );
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | virtual ostream &Describe(ostream &s) {
|
---|
| 270 | s<<"MeshInstance Id="<<GetId();
|
---|
| 271 | return mMesh->Describe(s);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[1001] | 274 | /** Sets the material. this overrides the material from
|
---|
| 275 | the mesh itself.
|
---|
| 276 | */
|
---|
| 277 | void SetMaterial(Material *mat);
|
---|
| 278 |
|
---|
| 279 | /** Returns the material of this mesh instance.
|
---|
| 280 | if not defined, returns the material of the mesh itself.
|
---|
| 281 | */
|
---|
| 282 | Material *GetMaterial() const;
|
---|
| 283 |
|
---|
| 284 | protected:
|
---|
| 285 |
|
---|
| 286 | Mesh *mMesh;
|
---|
| 287 | /** This material overrides the mesh material;
|
---|
| 288 | */
|
---|
| 289 | Material *mMaterial;
|
---|
[372] | 290 | };
|
---|
| 291 |
|
---|
| 292 |
|
---|
[1001] | 293 | /** This mesh instance includes a world transform. Use this
|
---|
| 294 | class if the same mesh should be instantiated on different places.
|
---|
| 295 | */
|
---|
[1004] | 296 | class TransformedMeshInstance: public MeshInstance
|
---|
[372] | 297 | {
|
---|
| 298 | public:
|
---|
[1001] | 299 | TransformedMeshInstance(Mesh *mesh);
|
---|
[372] | 300 |
|
---|
[1020] | 301 | virtual AxisAlignedBox3 GetBox() const;
|
---|
[1001] | 302 |
|
---|
[372] | 303 |
|
---|
[1001] | 304 | virtual int CastRay(Ray &ray);
|
---|
[372] | 305 |
|
---|
[1004] | 306 | virtual int CastRay(Ray &ray, const vector<int> &faces);
|
---|
| 307 |
|
---|
[372] | 308 | virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; }
|
---|
| 309 |
|
---|
| 310 | int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
|
---|
| 311 |
|
---|
[1001] | 312 | /** Transforms this mesh instance by m.
|
---|
| 313 | */
|
---|
| 314 | void ApplyWorldTransform(const Matrix4x4 &m);
|
---|
| 315 |
|
---|
| 316 | /** Loads the transformation matrix into this mesh instance.
|
---|
| 317 | */
|
---|
| 318 | void LoadWorldTransform(const Matrix4x4 &m);
|
---|
| 319 |
|
---|
| 320 | /** The transformation is returned in m.
|
---|
| 321 | */
|
---|
[1020] | 322 | void GetWorldTransform(Matrix4x4 &m) const;
|
---|
[1001] | 323 |
|
---|
[1004] | 324 | /** Transforms a mesh according to the stored world transform.
|
---|
[1020] | 325 | @param transformedMesh returns the tranformed mesh.
|
---|
[1002] | 326 | */
|
---|
[1020] | 327 | void GetTransformedMesh(Mesh &transformedMesh) const;
|
---|
[1002] | 328 |
|
---|
[1001] | 329 | protected:
|
---|
| 330 |
|
---|
| 331 | /// the transformation matrix
|
---|
| 332 | Matrix4x4 mWorldTransform;
|
---|
[372] | 333 |
|
---|
| 334 | };
|
---|
| 335 |
|
---|
[860] | 336 | }
|
---|
[372] | 337 |
|
---|
| 338 | #endif
|
---|