00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _LUXRAYS_EXTTRIANGLEMESH_H
00023 #define _LUXRAYS_EXTTRIANGLEMESH_H
00024
00025 #include <cassert>
00026 #include <cstdlib>
00027
00028 #include "luxrays/luxrays.h"
00029 #include "luxrays/core/geometry/triangle.h"
00030 #include "luxrays/core/trianglemesh.h"
00031
00032 namespace luxrays {
00033
00034 class ExtMesh : public Mesh {
00035 public:
00036 ExtMesh() { }
00037 virtual ~ExtMesh() { }
00038
00039 virtual bool HasNormals() const = 0;
00040 virtual bool HasColors() const = 0;
00041 virtual bool HasUVs() const = 0;
00042
00043 virtual Normal GetNormal(const unsigned int triIndex, const unsigned int vertIndex) const = 0;
00044 virtual Normal GetNormal(const unsigned int vertIndex) const = 0;
00045 virtual Spectrum GetColor(const unsigned int vertIndex) const = 0;
00046 virtual UV GetUV(const unsigned int vertIndex) const = 0;
00047
00048 virtual Normal InterpolateTriNormal(const unsigned int index, const float b1, const float b2) const = 0;
00049 virtual Spectrum InterpolateTriColor(const unsigned int index, const float b0, const float b1, const float b2) const = 0;
00050 virtual Spectrum InterpolateTriColor(const unsigned int index, const float b1, const float b2) const = 0;
00051 virtual UV InterpolateTriUV(const unsigned int index, const float b1, const float b2) const = 0;
00052
00053 virtual void Sample(const unsigned int index, const float u0, const float u1, Point *p, float *b0, float *b1, float *b2) const = 0;
00054
00055 virtual void Delete() = 0;
00056 };
00057
00058 class ExtTriangleMesh : public ExtMesh {
00059 public:
00060 ExtTriangleMesh(ExtTriangleMesh *mesh) {
00061 assert (mesh != NULL);
00062
00063 vertCount = mesh->vertCount;
00064 triCount = mesh->triCount;
00065 vertices = mesh->vertices;
00066 tris = mesh->tris;
00067
00068 normals = mesh->normals;
00069 colors = mesh->colors;
00070 uvs = mesh->uvs;
00071 }
00072 ExtTriangleMesh(const unsigned int meshVertCount, const unsigned int meshTriCount,
00073 Point *meshVertices, Triangle *meshTris, Normal *meshNormals = NULL, Spectrum *meshColors = NULL, UV *meshUV = NULL) {
00074 assert (meshVertCount > 0);
00075 assert (meshTriCount > 0);
00076 assert (meshVertices != NULL);
00077 assert (meshTris != NULL);
00078
00079 vertCount = meshVertCount;
00080 triCount = meshTriCount;
00081 vertices = meshVertices;
00082 tris = meshTris;
00083
00084 normals = meshNormals;
00085 colors = meshColors;
00086 uvs = meshUV;
00087 }
00088 ~ExtTriangleMesh() { };
00089 void Delete() {
00090 delete[] vertices;
00091 delete[] tris;
00092 delete[] normals;
00093 delete[] colors;
00094 delete[] uvs;
00095 }
00096
00097 MeshType GetType() const { return TYPE_EXT_TRIANGLE; }
00098 unsigned int GetTotalVertexCount() const { return vertCount; }
00099 unsigned int GetTotalTriangleCount() const { return triCount; }
00100 BBox GetBBox() const;
00101
00102 bool HasNormals() const { return normals != NULL; }
00103 bool HasColors() const { return colors != NULL; }
00104 bool HasUVs() const { return uvs != NULL; }
00105
00106 Point GetVertex(const unsigned int vertIndex) const { return vertices[vertIndex]; }
00107 float GetTriangleArea(const unsigned int triIndex) const { return tris[triIndex].Area(vertices); }
00108 Normal GetNormal(const unsigned int triIndex, const unsigned int vertIndex) const { return normals[tris[triIndex].v[vertIndex]]; }
00109 Normal GetNormal(const unsigned int vertIndex) const { return normals[vertIndex]; }
00110 Spectrum GetColor(const unsigned int vertIndex) const { return colors[vertIndex]; }
00111 UV GetUV(const unsigned int vertIndex) const { return uvs[vertIndex]; }
00112
00113 Normal InterpolateTriNormal(const unsigned int index, const float b1, const float b2) const {
00114 const Triangle &tri = tris[index];
00115 const float b0 = 1.f - b1 - b2;
00116 return Normalize(b0 * normals[tri.v[0]] + b1 * normals[tri.v[1]] + b2 * normals[tri.v[2]]);
00117 }
00118
00119 Spectrum InterpolateTriColor(const unsigned int index, const float b0, const float b1, const float b2) const {
00120 const Triangle &tri = tris[index];
00121 return b0 * colors[tri.v[0]] + b1 * colors[tri.v[1]] + b2 * colors[tri.v[2]];
00122 }
00123
00124 Spectrum InterpolateTriColor(const unsigned int index, const float b1, const float b2) const {
00125 const Triangle &tri = tris[index];
00126 const float b0 = 1.f - b1 - b2;
00127 return b0 * colors[tri.v[0]] + b1 * colors[tri.v[1]] + b2 * colors[tri.v[2]];
00128 }
00129
00130 UV InterpolateTriUV(const unsigned int index, const float b1, const float b2) const {
00131 const Triangle &tri = tris[index];
00132 const float b0 = 1.f - b1 - b2;
00133 return b0 * uvs[tri.v[0]] + b1 * uvs[tri.v[1]] + b2 * uvs[tri.v[2]];
00134 }
00135
00136 void Sample(const unsigned int index, const float u0, const float u1, Point *p, float *b0, float *b1, float *b2) const {
00137 const Triangle &tri = tris[index];
00138 tri.Sample(vertices, u0, u1, p, b0, b1, b2);
00139 }
00140
00141 Point *GetVertices() const { return vertices; }
00142 Triangle *GetTriangles() const { return tris; }
00143
00144 static ExtTriangleMesh *LoadExtTriangleMesh(Context *ctx, const std::string &fileName, const bool usePlyNormals = false);
00145
00146 private:
00147 unsigned int vertCount;
00148 unsigned int triCount;
00149 Point *vertices;
00150 Triangle *tris;
00151 Normal *normals;
00152 Spectrum *colors;
00153 UV *uvs;
00154 };
00155
00156 class ExtInstanceTriangleMesh : public ExtMesh {
00157 public:
00158 ExtInstanceTriangleMesh(ExtTriangleMesh *m, const Transform &t) {
00159 assert (mesh != NULL);
00160
00161 trans = t;
00162 invTrans = t.GetInverse();
00163 mesh = m;
00164 }
00165 ~ExtInstanceTriangleMesh() { };
00166 void Delete() { }
00167
00168 virtual MeshType GetType() const { return TYPE_EXT_TRIANGLE_INSTANCE; }
00169
00170 Point GetVertex(const unsigned index) const { return trans(mesh->GetVertex(index)); }
00171 float GetTriangleArea(const unsigned int triIndex) const {
00172 const Triangle &tri = mesh->GetTriangles()[triIndex];
00173
00174 return Triangle::Area(GetVertex(tri.v[0]), GetVertex(tri.v[1]), GetVertex(tri.v[2]));
00175 }
00176 unsigned int GetTotalVertexCount() const { return mesh->GetTotalVertexCount(); }
00177 unsigned int GetTotalTriangleCount() const { return mesh->GetTotalTriangleCount(); }
00178
00179 BBox GetBBox() const {
00180 return trans(mesh->GetBBox());
00181 }
00182
00183 bool HasNormals() const { return mesh->HasNormals(); }
00184 bool HasColors() const { return mesh->HasColors(); }
00185 bool HasUVs() const { return mesh->HasUVs(); }
00186
00187 Normal GetNormal(const unsigned index) const { return Normalize(trans(mesh->GetNormal(index))); }
00188 Normal GetNormal(const unsigned int triIndex, const unsigned int vertIndex) const { return Normalize(trans(mesh->GetNormal(triIndex, vertIndex))); }
00189 Spectrum GetColor(const unsigned index) const { return mesh->GetColor(index); }
00190 UV GetUV(const unsigned index) const { return mesh->GetUV(index); }
00191
00192 Normal InterpolateTriNormal(const unsigned int index, const float b1, const float b2) const {
00193 return Normalize(trans(mesh->InterpolateTriNormal(index, b1, b2)));
00194 }
00195
00196 Spectrum InterpolateTriColor(const unsigned int index, const float b0, const float b1, const float b2) const {
00197 return mesh->InterpolateTriColor(index, b0, b1, b2);
00198 }
00199
00200 Spectrum InterpolateTriColor(const unsigned int index, const float b1, const float b2) const {
00201 return mesh->InterpolateTriColor(index, b1, b2);
00202 }
00203
00204 UV InterpolateTriUV(const unsigned int index, const float b1, const float b2) const {
00205 return mesh->InterpolateTriUV(index, b1, b2);
00206 }
00207
00208 void Sample(const unsigned int index, const float u0, const float u1, Point *p, float *b0, float *b1, float *b2) const {
00209 mesh->Sample(index, u0, u1, p , b0, b1, b2);
00210 *p = trans(*p);
00211 }
00212
00213 const Transform &GetTransformation() const { return trans; }
00214 const Transform &GetInvTransformation() const { return invTrans; }
00215 void SetTransformation(const Transform &t) {
00216 trans = t;
00217 invTrans = t.GetInverse();
00218 }
00219 Point *GetVertices() const { return mesh->GetVertices(); }
00220 Triangle *GetTriangles() const { return mesh->GetTriangles(); }
00221 ExtTriangleMesh *GetExtTriangleMesh() const { return mesh; };
00222
00223 private:
00224 Transform trans, invTrans;
00225 ExtTriangleMesh *mesh;
00226 };
00227
00228 }
00229
00230 #endif