00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _LUXRAYS_TRIANGLEMESH_H
00023 #define _LUXRAYS_TRIANGLEMESH_H
00024
00025 #include <cassert>
00026 #include <cstdlib>
00027
00028 #include "luxrays/luxrays.h"
00029 #include "luxrays/core/geometry/triangle.h"
00030 #include "geometry/transform.h"
00031
00032 namespace luxrays {
00033
00034 typedef unsigned int TriangleMeshID;
00035 typedef unsigned int TriangleID;
00036
00037 enum MeshType {
00038 TYPE_TRIANGLE, TYPE_TRIANGLE_INSTANCE,
00039 TYPE_EXT_TRIANGLE, TYPE_EXT_TRIANGLE_INSTANCE
00040 };
00041
00042 class Mesh {
00043 public:
00044 Mesh() { }
00045 virtual ~Mesh() { }
00046
00047 virtual MeshType GetType() const = 0;
00048
00049 virtual unsigned int GetTotalVertexCount() const = 0;
00050 virtual unsigned int GetTotalTriangleCount() const = 0;
00051
00052 virtual BBox GetBBox() const = 0;
00053 virtual Point GetVertex(const unsigned int vertIndex) const = 0;
00054 virtual float GetTriangleArea(const unsigned int triIndex) const = 0;
00055
00056 virtual Point *GetVertices() const = 0;
00057 virtual Triangle *GetTriangles() const = 0;
00058 };
00059
00060 class TriangleMesh : public Mesh {
00061 public:
00062
00063 TriangleMesh(const unsigned int meshVertCount, const unsigned int meshTriCount,
00064 Point *meshVertices, Triangle *meshTris) {
00065 assert (meshVertCount > 0);
00066 assert (meshTriCount > 0);
00067 assert (meshVertices != NULL);
00068 assert (meshTris != NULL);
00069
00070 vertCount = meshVertCount;
00071 triCount = meshTriCount;
00072 vertices = meshVertices;
00073 tris = meshTris;
00074 };
00075 virtual ~TriangleMesh() { };
00076 virtual void Delete() {
00077 delete[] vertices;
00078 delete[] tris;
00079 }
00080
00081 virtual MeshType GetType() const { return TYPE_TRIANGLE; }
00082 unsigned int GetTotalVertexCount() const { return vertCount; }
00083 unsigned int GetTotalTriangleCount() const { return triCount; }
00084
00085 BBox GetBBox() const;
00086 Point GetVertex(const unsigned int vertIndex) const { return vertices[vertIndex]; }
00087 float GetTriangleArea(const unsigned int triIndex) const { return tris[triIndex].Area(vertices); }
00088
00089 Point *GetVertices() const { return vertices; }
00090 Triangle *GetTriangles() const { return tris; }
00091
00092 static TriangleMesh *Merge(
00093 const std::deque<Mesh *> &meshes,
00094 TriangleMeshID **preprocessedMeshIDs = NULL,
00095 TriangleID **preprocessedMeshTriangleIDs = NULL);
00096 static TriangleMesh *Merge(
00097 const unsigned int totalVerticesCount,
00098 const unsigned int totalIndicesCount,
00099 const std::deque<Mesh *> &meshes,
00100 TriangleMeshID **preprocessedMeshIDs = NULL,
00101 TriangleID **preprocessedMeshTriangleIDs = NULL);
00102
00103 protected:
00104 unsigned int vertCount;
00105 unsigned int triCount;
00106 Point *vertices;
00107 Triangle *tris;
00108 };
00109
00110 class InstanceTriangleMesh : public Mesh {
00111 public:
00112 InstanceTriangleMesh(TriangleMesh *m, const Transform &t) {
00113 assert (mesh != NULL);
00114
00115 trans = t;
00116 invTrans = t.GetInverse();
00117 mesh = m;
00118 };
00119 virtual ~InstanceTriangleMesh() { };
00120
00121 virtual MeshType GetType() const { return TYPE_TRIANGLE_INSTANCE; }
00122 unsigned int GetTotalVertexCount() const { return mesh->GetTotalVertexCount(); }
00123 unsigned int GetTotalTriangleCount() const { return mesh->GetTotalTriangleCount(); }
00124
00125 BBox GetBBox() const {
00126 return trans(mesh->GetBBox());
00127 }
00128 Point GetVertex(const unsigned int vertIndex) const { return trans(mesh->GetVertex(vertIndex)); }
00129 float GetTriangleArea(const unsigned int triIndex) const {
00130 const Triangle &tri = mesh->GetTriangles()[triIndex];
00131
00132 return Triangle::Area(GetVertex(tri.v[0]), GetVertex(tri.v[1]), GetVertex(tri.v[2]));
00133 }
00134
00135 const Transform &GetTransformation() const { return trans; }
00136 const Transform &GetInvTransformation() const { return invTrans; }
00137 Point *GetVertices() const { return mesh->GetVertices(); }
00138 Triangle *GetTriangles() const { return mesh->GetTriangles(); }
00139 TriangleMesh *GetTriangleMesh() const { return mesh; };
00140
00141 protected:
00142 Transform trans, invTrans;
00143 TriangleMesh *mesh;
00144 };
00145
00146 }
00147
00148 #endif