1313#include < vistle/core/lines.h>
1414#include < vistle/core/points.h>
1515#include < vistle/core/normals.h>
16+ #include < vistle/core/unstr.h>
1617
1718#include " ReadModel.h"
1819
@@ -100,7 +101,6 @@ bool ReadModel::prepareRead()
100101
101102Object::ptr ReadModel::load (const std::string &name) const
102103{
103- Object::ptr ret;
104104 Assimp::Importer importer;
105105 bool indexed = false ;
106106 bool readNormals = getIntParameter (" normals" );
@@ -120,92 +120,178 @@ Object::ptr ReadModel::load(const std::string &name) const
120120 std::string s = str.str ();
121121 sendError (" %s" , s.c_str ());
122122 }
123- return ret ;
123+ return {} ;
124124 }
125125
126+ enum OutputType {
127+ Unknown,
128+ Point,
129+ Line,
130+ Triangle,
131+ Polygon,
132+ Unstructured,
133+ };
134+ OutputType outputType = Unknown;
135+
136+ size_t totNumVert = 0 , totNumIndex = 0 , totNumFace = 0 ;
137+ bool haveNormals = false ;
138+ for (unsigned int m = 0 ; m < scene->mNumMeshes ; ++m) {
139+ const aiMesh *mesh = scene->mMeshes [m];
140+ if (!mesh->HasPositions ()) {
141+ continue ;
142+ }
143+ if (mesh->HasNormals () && readNormals) {
144+ haveNormals = true ;
145+ }
146+
147+ if (mesh->HasFaces ()) {
148+ if (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON) {
149+ if (outputType == Polygon || outputType == Triangle || outputType == Unknown)
150+ outputType = Polygon;
151+ else
152+ outputType = Unstructured;
153+ totNumFace += mesh->mNumFaces ;
154+ totNumVert += mesh->mNumVertices ;
155+ Index numIndex = 0 ;
156+ for (unsigned int f = 0 ; f < mesh->mNumFaces ; ++f) {
157+ numIndex += mesh->mFaces [f].mNumIndices ;
158+ }
159+ totNumIndex += numIndex;
160+ }
161+ if (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE) {
162+ if (outputType == Triangle || outputType == Unknown)
163+ outputType = Triangle;
164+ else if (outputType != Polygon)
165+ outputType = Unstructured;
166+ totNumFace += mesh->mNumFaces ;
167+ totNumVert += mesh->mNumVertices ;
168+ Index numIndex = indexed ? mesh->mNumFaces * 3 : 0 ;
169+ totNumIndex += numIndex;
170+ }
171+ } else {
172+ if (outputType == Point || outputType == Unknown)
173+ outputType = Point;
174+ else
175+ outputType = Unstructured;
176+ totNumVert += mesh->mNumVertices ;
177+ }
178+ }
179+
180+ Normals::ptr normals;
181+
182+ Coords::ptr coords;
183+ Points::ptr points;
184+ Triangles::ptr tri;
185+ Polygons::ptr poly;
186+ UnstructuredGrid::ptr unstr;
187+ Index *el = nullptr , *cl = nullptr ;
188+ Byte *tl = nullptr ;
189+ Scalar *x[3 ] = {};
190+ Scalar *norm[3 ] = {};
191+ switch (outputType) {
192+ case Unknown:
193+ return {};
194+ break ;
195+ case Point:
196+ coords = points = std::make_shared<Points>(totNumVert);
197+ break ;
198+ case Triangle:
199+ coords = tri = std::make_shared<Triangles>(totNumIndex, totNumVert);
200+ if (indexed)
201+ cl = tri->cl ().data ();
202+ break ;
203+ case Polygon:
204+ coords = poly = std::make_shared<Polygons>(totNumFace, totNumIndex, totNumVert);
205+ el = poly->el ().data ();
206+ if (indexed)
207+ cl = poly->cl ().data ();
208+ break ;
209+ case Unstructured:
210+ coords = unstr = std::make_shared<UnstructuredGrid>(totNumFace, totNumIndex, totNumVert);
211+ tl = unstr->tl ().data ();
212+ el = unstr->el ().data ();
213+ if (indexed)
214+ cl = poly->cl ().data ();
215+ break ;
216+ }
217+ if (coords) {
218+ for (int c = 0 ; c < 3 ; ++c) {
219+ x[c] = coords->x (c).data ();
220+ }
221+ }
222+ if (haveNormals) {
223+ normals = std::make_shared<Normals>(totNumVert);
224+ for (int c = 0 ; c < 3 ; ++c) {
225+ norm[c] = normals->x (c).data ();
226+ }
227+ }
228+
229+ Index vertCount = 0 , coordCount = 0 ;
230+ Index idx = 0 ;
126231 for (unsigned int m = 0 ; m < scene->mNumMeshes ; ++m) {
127232 const aiMesh *mesh = scene->mMeshes [m];
128233 if (mesh->HasPositions ()) {
129- Coords::ptr coords;
130234 if (mesh->HasFaces ()) {
131- auto numVert = mesh->mNumVertices ;
132235 auto numFace = mesh->mNumFaces ;
133236 if (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON) {
134- Index numIndex = 0 ;
135- for (unsigned int f = 0 ; f < numFace; ++f) {
136- numIndex += mesh->mFaces [f].mNumIndices ;
137- }
138- Polygons::ptr poly (new Polygons (numFace, numIndex, numVert));
139- coords = poly;
140- auto *el = poly->el ().data ();
141- auto *cl = indexed ? poly->cl ().data () : nullptr ;
142- Index idx = 0 , vertCount = 0 ;
143237 for (unsigned int f = 0 ; f < numFace; ++f) {
144- el[idx++] = vertCount;
145238 if (indexed) {
146239 const auto &face = mesh->mFaces [f];
147240 for (unsigned int i = 0 ; i < face.mNumIndices ; ++i) {
148241 cl[vertCount++] = face.mIndices [i];
149242 }
150243 }
244+ if (tl)
245+ tl[idx] = UnstructuredGrid::POLYGON ;
246+ el[idx++] = vertCount;
151247 }
152- el[idx] = vertCount;
153248 } else if (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE) {
154- Index numIndex = indexed ? mesh->mNumFaces * 3 : 0 ;
155- Triangles::ptr tri (new Triangles (numIndex, numVert));
156- coords = tri;
157249 if (indexed) {
158- auto *cl = tri->cl ().data ();
159- Index vertCount = 0 ;
160250 for (unsigned int f = 0 ; f < numFace; ++f) {
161251 const auto &face = mesh->mFaces [f];
162252 assert (face.mNumIndices == 3 );
163253 for (unsigned int i = 0 ; i < face.mNumIndices ; ++i) {
164254 cl[vertCount++] = face.mIndices [i];
165255 }
256+ if (tl)
257+ tl[idx] = UnstructuredGrid::TRIANGLE ;
258+ if (el)
259+ el[idx++] = vertCount;
166260 }
167261 }
168262 }
169263 } else {
170- Points::ptr points (new Points (mesh->mNumVertices ));
171- coords = points;
264+ ++vertCount;
265+ if (tl)
266+ tl[idx] = UnstructuredGrid::POINT ;
267+ if (el)
268+ el[idx++] = vertCount;
172269 }
173270 if (coords) {
174- Scalar *x[3 ] = {nullptr , nullptr , nullptr };
175- for (int c = 0 ; c < 3 ; ++c) {
176- x[c] = coords->x (c).data ();
177- }
178271 for (Index i = 0 ; i < mesh->mNumVertices ; ++i) {
179272 const auto &vert = mesh->mVertices [i];
180273 for (unsigned int c = 0 ; c < 3 ; ++c) {
181- x[c][i] = vert[c];
274+ x[c][coordCount + i] = vert[c];
182275 }
183276 }
184- ret = coords;
185277 if (mesh->HasNormals () && readNormals) {
186- Normals::ptr normals (new Normals (mesh->mNumVertices ));
187- Scalar *n[3 ] = {nullptr , nullptr , nullptr };
188- for (int c = 0 ; c < 3 ; ++c) {
189- n[c] = normals->x (c).data ();
190- }
191278 for (Index i = 0 ; i < mesh->mNumVertices ; ++i) {
192- const auto &norm = mesh->mNormals [i];
279+ const auto &n = mesh->mNormals [i];
193280 for (unsigned int c = 0 ; c < 3 ; ++c) {
194- n [c][i] = norm [c];
281+ norm [c][coordCount + i] = n [c];
195282 }
196283 }
197- updateMeta (normals);
198- coords->setNormals (normals);
199284 }
285+
286+ coordCount += mesh->mNumVertices ;
200287 }
201288 }
202-
203- break ;
204289 }
205290
206- if (scene->mNumMeshes > 1 ) {
207- sendInfo (" file %s contains %d meshes, all but the first have been ignored" , name.c_str (), scene->mNumMeshes );
291+ if (normals) {
292+ updateMeta (normals);
293+ coords->setNormals (normals);
208294 }
209295
210- return ret ;
296+ return coords ;
211297}
0 commit comments