Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "trimesh.h"
00035
00036 #include <fstream>
00037
00038 #include "common/wave_ex.h"
00039 #include "perf/perf.h"
00040 #include "util/token_stream.h"
00041
00042
00043 namespace trimesh {
00044
00045
00046
00047
00048
00049
00050 Trimesh::~Trimesh(void) throw() { }
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 class Mesh : public Trimesh {
00068 public:
00069
00070 Mesh(void) throw();
00071 ~Mesh(void) throw();
00072
00073
00074 void initialize(IN const char * filename);
00075
00076
00077 int getVertexCount(void) const throw() { return m_nVertices; }
00078 int getTriangleCount(void) const throw() { return m_nTriangles; }
00079 const point3d_t * getVertexArray(void) const throw() { return m_vertices; }
00080 const int * getTriangleArray(void) const throw() { return m_triangles; }
00081 void dump(IN const char * title) const throw();
00082
00083 private:
00084
00085 int m_nVertices;
00086 int m_nTriangles;
00087 point3d_t * m_vertices;
00088 int * m_triangles;
00089 };
00090
00091
00092
00093 Mesh::Mesh(void)
00094 throw()
00095 {
00096 m_nVertices = m_nTriangles = 0;
00097 m_vertices = NULL;
00098 m_triangles = NULL;
00099 }
00100
00101
00102 Mesh::~Mesh(void)
00103 throw()
00104 {
00105 if (m_vertices) {
00106 delete[] m_vertices;
00107 }
00108 if (m_triangles) {
00109 delete[] m_triangles;
00110 }
00111 }
00112
00113
00114
00115 void
00116 Mesh::initialize
00117 (
00118 IN const char * filename
00119 )
00120 {
00121 ASSERT(filename, "null");
00122
00123
00124 std::ifstream infile(filename);
00125 if (!infile.good()) {
00126 WAVE_EX(wex);
00127 wex << "Could not open triangle mesh file: " << filename;
00128 }
00129 std::string token;
00130
00131
00132 expectToken(infile, "nVertices");
00133 m_nVertices = readInteger(infile, token);
00134 if (m_nVertices < 0) {
00135 WAVE_EX(wex);
00136 wex << "Invalid vertex count (" << m_nVertices << ") in ";
00137 wex << filename;
00138 }
00139
00140 m_vertices = new point3d_t[m_nVertices];
00141 ASSERT(m_vertices, "out of memory");
00142
00143
00144 for (int i = 0; i < m_nVertices; ++i) {
00145 expectToken(infile, "v");
00146 point3d_t& p = m_vertices[i];
00147
00148
00149 p.x = readFloat(infile, token);
00150 p.y = readFloat(infile, token);
00151 p.z = readFloat(infile, token);
00152 }
00153
00154
00155 expectToken(infile, "nTriangles");
00156 m_nTriangles = readInteger(infile, token);
00157 if (m_nTriangles < 0) {
00158 WAVE_EX(wex);
00159 wex << "Invalid triangle count (" << m_nTriangles << ") in ";
00160 wex << filename;
00161 }
00162 m_triangles = new int[3 * m_nTriangles];
00163 ASSERT(m_triangles, "out of memory");
00164
00165
00166 for (int i = 0; i < m_nTriangles; ++i) {
00167 expectToken(infile, "t");
00168 int * index = m_triangles + (3 * i);
00169
00170
00171 index[0] = readInteger(infile, token);
00172 index[1] = readInteger(infile, token);
00173 index[2] = readInteger(infile, token);
00174 }
00175 }
00176
00177
00178
00179 void
00180 Mesh::dump
00181 (
00182 IN const char * title
00183 )
00184 const
00185 throw()
00186 {
00187 ASSERT(title, "null");
00188
00189 DPRINTF("Triangle mesh: %s", title);
00190 DPRINTF(" vertex count: %4d", m_nVertices);
00191 DPRINTF(" triangle count: %4d", m_nTriangles);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 smart_ptr<Trimesh>
00203 Trimesh::load
00204 (
00205 IN const char * filename
00206 )
00207 {
00208 perf::Timer timer("TriangleMesh::load");
00209 ASSERT(filename, "null");
00210
00211 smart_ptr<Mesh> local = new Mesh;
00212 ASSERT(local, "out of memory");
00213
00214 local->initialize(filename);
00215
00216 return local;
00217 }
00218
00219
00220
00221 };
00222