Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

import2tablesSurface.hpp 26 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURVEC3E. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Import/importPlyData.h"

#include "openctm.h"

#include "assimp.h"
#include "aiPostProcess.h"
#include "aiScene.h"


namespace CGoGN
{

namespace Algo
{

namespace Import
{

template<typename PFP>
ImportSurfacique::ImportType MeshTablesSurface<PFP>::getFileType(const std::string& filename)
{
	if ((filename.rfind(".trianbgz")!=std::string::npos) || (filename.rfind(".TRIANBGZ")!=std::string::npos))
		return ImportSurfacique::TRIANBGZ;
Pierre Kraemer's avatar
Pierre Kraemer committed
48

Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
52
53
54
55
56
	if ((filename.rfind(".trian")!=std::string::npos) || (filename.rfind(".TRIAN")!=std::string::npos))
		return ImportSurfacique::TRIAN;

	if ((filename.rfind(".plyptm")!=std::string::npos) || (filename.rfind(".PLYGEN")!=std::string::npos))
		return ImportSurfacique::PLYPTM;

	if ((filename.rfind(".ply")!=std::string::npos) || (filename.rfind(".PLY")!=std::string::npos))
		return ImportSurfacique::PLY;
Pierre Kraemer's avatar
Pierre Kraemer committed
57

Pierre Kraemer's avatar
Pierre Kraemer committed
58
59
	if ((filename.rfind(".off")!=std::string::npos) || (filename.rfind(".OFF")!=std::string::npos))
		return ImportSurfacique::OFF;
Pierre Kraemer's avatar
Pierre Kraemer committed
60

Pierre Kraemer's avatar
Pierre Kraemer committed
61
62
	if ((filename.rfind(".obj")!=std::string::npos) || (filename.rfind(".OBJ")!=std::string::npos))
		return ImportSurfacique::OBJ;
Pierre Kraemer's avatar
Pierre Kraemer committed
63

Pierre Kraemer's avatar
Pierre Kraemer committed
64
65
66
67
68
69
70
	if ((filename.rfind(".ctm")!=std::string::npos) || (filename.rfind(".OBJ")!=std::string::npos))
		return ImportSurfacique::CTM;

	return ImportSurfacique::UNKNOWNSURFACE;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
71
bool MeshTablesSurface<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames, ImportSurfacique::ImportType kind)
Pierre Kraemer's avatar
Pierre Kraemer committed
72
73
74
75
{
	if (kind == ImportSurfacique::UNKNOWNSURFACE)
		kind = getFileType(filename);

Pierre Kraemer's avatar
Pierre Kraemer committed
76
	attrNames.clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
77
78
79
80

	switch (kind)
	{
	case ImportSurfacique::TRIAN:
81
		CGoGNout << "TYPE: TRIAN" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
82
		return importTrian(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
		break;
	case ImportSurfacique::TRIANBGZ:
85
		CGoGNout << "TYPE: TRIANBGZ" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
86
		return importTrianBinGz(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
87
88
		break;
	case ImportSurfacique::CTM:
89
		CGoGNout << "TYPE: CTM" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
90
		return importCTM(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
		break;
	case ImportSurfacique::OFF:
93
		CGoGNout << "TYPE: OFF" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
94
		return importOff(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
95
96
		break;
	case ImportSurfacique::PLY:
97
		CGoGNout << "TYPE: PLY" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
98
		return importPly(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
99
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	case ImportSurfacique::PLYPTM:
101
		CGoGNout << "TYPE: PLYPTM" << CGoGNendl;
102
103
104
105
		return importPlyPTM(filename, attrNames);
		break;
	case ImportSurfacique::PLYPTMgeneric:
		CGoGNout << "TYPE: PLYPTMgeneric" << CGoGNendl;
106
		return importPlyPTMgeneric(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
107
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
108
	case ImportSurfacique::OBJ:
109
		CGoGNout << "TYPE: OBJ" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
110
		return importObj(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
		break;
	default:
113
		CGoGNout << "TYPE: ASSIMP" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
114
		return importASSIMP(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
115
116
117
118
119
120
		break;
	}
	return false;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
121
bool MeshTablesSurface<PFP>::importTrian(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
{
123
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
124
125

	if (!positions.isValid())
126
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
127

Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
	attrNames.push_back(positions.name()) ;

130
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
131

Pierre Kraemer's avatar
Pierre Kraemer committed
132
133
134
135
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
136
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
137
138
139
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
140
	// read nb of points
Pierre Kraemer's avatar
Pierre Kraemer committed
141
142
	fp >> m_nbVertices;

Pierre Kraemer's avatar
Pierre Kraemer committed
143
	// read points
Pierre Kraemer's avatar
Pierre Kraemer committed
144
145
146
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

Pierre Kraemer's avatar
Pierre Kraemer committed
147
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
148
149
150
151
152
	{
		VEC3 pos;
		fp >> pos[0];
		fp >> pos[1];
		fp >> pos[2];
Pierre Kraemer's avatar
Pierre Kraemer committed
153
154
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156
157
158
159
160
161
162
		verticesID.push_back(id);
	}

	// read nb of faces
	fp >> m_nbFaces;
	m_nbEdges.reserve(m_nbFaces);
	m_emb.reserve(3*m_nbFaces);

Pierre Kraemer's avatar
Pierre Kraemer committed
163
164
	// read indices of faces
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
	{
		m_nbEdges.push_back(3);
		// read the three vertices of triangle
		int pt;
		fp >> pt;
		m_emb.push_back(verticesID[pt]);
		fp >> pt;
		m_emb.push_back(verticesID[pt]);
		fp >> pt;
		m_emb.push_back(verticesID[pt]);

		// neighbour not always good in files !!
		int neigh;
		fp >> neigh;
		fp >> neigh;
		fp >> neigh;
	}

	fp.close();
	return true;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
188
bool MeshTablesSurface<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
189
{
190
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
191
192

	if (!positions.isValid())
193
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
194

Pierre Kraemer's avatar
Pierre Kraemer committed
195
196
	attrNames.push_back(positions.name()) ;

197
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
198

Pierre Kraemer's avatar
Pierre Kraemer committed
199
200
201
202
203
	// open file
	igzstream fs(filename.c_str(), std::ios::in|std::ios::binary);

	if (!fs.good())
	{
204
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
207

Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
210
211
212
	// read nb of points
	fs.read(reinterpret_cast<char*>(&m_nbVertices), sizeof(int));

	// read points
	std::vector<unsigned int> verticesID;
Pierre Kraemer's avatar
Pierre Kraemer committed
213
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
214
215
		verticesID.reserve(m_nbVertices);
		float* buffer = new float[m_nbVertices*3];
Pierre Kraemer's avatar
Pierre Kraemer committed
216
		fs.read(reinterpret_cast<char*>(buffer), 3*m_nbVertices*sizeof(float));
Pierre Kraemer's avatar
Pierre Kraemer committed
217
		float *ptr = buffer;
Pierre Kraemer's avatar
Pierre Kraemer committed
218
		for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
219
220
221
222
223
224
		{
			VEC3 pos;
			pos[0]= *ptr++;
			pos[1]= *ptr++;
			pos[2]= *ptr++;

Pierre Kraemer's avatar
Pierre Kraemer committed
225
226
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
227
228
229
230
231
232
233
234
235
236
237
238

			verticesID.push_back(id);
		}
		delete[] buffer;
	}

	// read nb of faces
	fs.read(reinterpret_cast<char*>(&m_nbFaces), sizeof(int));
	m_nbEdges.reserve(m_nbFaces);
	m_emb.reserve(3*m_nbFaces);

	// read indices of faces
Pierre Kraemer's avatar
Pierre Kraemer committed
239
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
240
241
242
243
		int* buffer = new int[m_nbFaces*6];
		fs.read(reinterpret_cast<char*>(buffer),6*m_nbFaces*sizeof(float));
		int *ptr = buffer;

Pierre Kraemer's avatar
Pierre Kraemer committed
244
		for (unsigned int i = 0; i < m_nbFaces; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
245
246
247
248
249
250
251
252
253
254
255
256
257
		{
			m_nbEdges.push_back(3);
			m_emb.push_back(verticesID[*ptr++]);
			m_emb.push_back(verticesID[*ptr++]);
			m_emb.push_back(verticesID[*ptr++]);
		}
	}
	
	fs.close();
	return true;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
258
bool MeshTablesSurface<PFP>::importOff(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
259
{
260
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
261
262

	if (!positions.isValid())
263
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
264

Pierre Kraemer's avatar
Pierre Kraemer committed
265
266
	attrNames.push_back(positions.name()) ;

267
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
268

Pierre Kraemer's avatar
Pierre Kraemer committed
269
270
271
272
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
273
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
274
275
276
277
278
279
280
281
282
		return false;
	}

    std::string ligne;

    // lecture de OFF
    std::getline (fp, ligne);
    if (ligne.rfind("OFF") == std::string::npos)
    {
283
284
		CGoGNerr << "Problem reading off file: not an off file" << CGoGNendl;
		CGoGNerr << ligne << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
285
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
    }

    // lecture des nombres de sommets/faces/aretes
	int nbe;
    {
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

	    std::stringstream oss(ligne);
		oss >> m_nbVertices;
		oss >> m_nbFaces;
		oss >> nbe;
    }

	//lecture sommets
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);
Pierre Kraemer's avatar
Pierre Kraemer committed
305
	for (unsigned int i = 0; i < m_nbVertices;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
306
307
308
309
	{
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
310
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
311
312
313
314
315
316
317
318
319
320

		std::stringstream oss(ligne);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
		// on peut ajouter ici la lecture de couleur si elle existe
		VEC3 pos(x,y,z);

Pierre Kraemer's avatar
Pierre Kraemer committed
321
322
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
323
324
325
326
327
328
329
330
331

		verticesID.push_back(id);
	}

	// lecture faces
	// normalement nbVertices*8 devrait suffire largement
	m_nbEdges.reserve(m_nbFaces);
	m_emb.reserve(m_nbVertices*8);

Pierre Kraemer's avatar
Pierre Kraemer committed
332
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
333
334
335
336
	{
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
337
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

		std::stringstream oss(ligne);
		int n;
		oss >> n;
		m_nbEdges.push_back(n);
		for (int j=0;j<n; ++j)
		{
			int index; // index du plongement
			oss >> index;
			m_emb.push_back(verticesID[index]);
		}
		// on peut ajouter ici la lecture de couleur si elle existe
	}

	fp.close();
	return true;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
357
bool MeshTablesSurface<PFP>::importObj(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
358
{
359
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
360
361

	if (!positions.isValid())
362
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
363

Pierre Kraemer's avatar
Pierre Kraemer committed
364
365
	attrNames.push_back(positions.name()) ;

366
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
367

Pierre Kraemer's avatar
Pierre Kraemer committed
368
369
370
371
	// open file
	std::ifstream fp(filename.c_str(), std::ios::binary);
	if (!fp.good())
	{
372
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
373
374
375
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
376
//	fp.seekg(0, std::ios::end);
Pierre Kraemer's avatar
Pierre Kraemer committed
377
//	int ab = fp.tellg();
Pierre Kraemer's avatar
Pierre Kraemer committed
378
//	fp.seekg(0, std::ios::beg);
Pierre Kraemer's avatar
Pierre Kraemer committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//	int ac = fp.tellg();

    std::string ligne;
    std::string tag;

    do
    {
    	fp >> tag;
    	std::getline (fp, ligne);
    }while (tag != std::string("v"));

    // lecture des sommets
	std::vector<unsigned int> verticesID;
	verticesID.reserve(102400); // on tape large (400Ko wahouuuuu !!)

Pierre Kraemer's avatar
Pierre Kraemer committed
394
	unsigned int i = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
395
396
397
398
399
400
401
402
403
404
405
406
407
    do
    {
		if (tag == std::string("v"))
		{
			std::stringstream oss(ligne);
		
			float x,y,z;
			oss >> x;
			oss >> y;
			oss >> z;

			VEC3 pos(x,y,z);

Pierre Kraemer's avatar
Pierre Kraemer committed
408
409
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
410
411
412
413
414
415

			verticesID.push_back(id);
			i++;
		}

		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
416
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
    } while (!fp.eof());

	m_nbVertices = verticesID.size();

	// close/clear/open only way to go back to beginning of file
	fp.close();
	fp.clear();
	fp.open(filename.c_str());

	do
    {
    	fp >> tag;
    	std::getline (fp, ligne);
    } while (tag != std::string("f"));

	m_nbEdges.reserve(verticesID.size()*2);
	m_emb.reserve(verticesID.size()*8);

	std::vector<int> table;
Pierre Kraemer's avatar
Pierre Kraemer committed
436
	table.reserve(64); // 64 cotes pour une face devrait suffire
Pierre Kraemer's avatar
Pierre Kraemer committed
437
438
439
	m_nbFaces = 0;
    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
440
    	if (tag == std::string("f")) // lecture d'une face
Pierre Kraemer's avatar
Pierre Kraemer committed
441
442
443
444
445
446
447
448
    	{
    		std::stringstream oss(ligne);
     		table.clear();
    		while (!oss.eof())  // lecture de tous les indices
    		{
    			std::string str;
    			oss >> str;

Pierre Kraemer's avatar
Pierre Kraemer committed
449
    			unsigned int ind = 0;
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
450
    			while ( (ind<str.length()) &&  (str[ind]!='/'))
Pierre Kraemer's avatar
Pierre Kraemer committed
451
452
    				ind++;

Pierre Kraemer's avatar
Pierre Kraemer committed
453
				if (ind > 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
454
455
				{
    				long index;
Pierre Kraemer's avatar
Pierre Kraemer committed
456
					std::stringstream iss(str.substr(0, ind));
Pierre Kraemer's avatar
Pierre Kraemer committed
457
458
459
460
461
					iss >> index;
		   			table.push_back(index);
				}
    		}

Pierre Kraemer's avatar
Pierre Kraemer committed
462
    		unsigned int n = table.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
463
			m_nbEdges.push_back(short(n));
Pierre Kraemer's avatar
Pierre Kraemer committed
464
    		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
465
    		{
Pierre Kraemer's avatar
Pierre Kraemer committed
466
    			int index = table[j] - 1; // les index commencent a 1 (boufonnerie d'obj ;)
Pierre Kraemer's avatar
Pierre Kraemer committed
467
468
469
470
471
				m_emb.push_back(verticesID[index]);
    		}
			m_nbFaces++;
    	}
		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
472
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
473
474
475
476
477
478
479
     } while (!fp.eof());

	fp.close ();
	return true;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
480
bool MeshTablesSurface<PFP>::importPly(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
481
{
482
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
483
484

	if (!positions.isValid())
485
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
486

Pierre Kraemer's avatar
Pierre Kraemer committed
487
488
	attrNames.push_back(positions.name()) ;

489
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
490

Pierre Kraemer's avatar
Pierre Kraemer committed
491
492
493
494
	PlyImportData pid;

	if (! pid.read_file(filename) )
	{
495
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
496
497
498
499
500
501
502
503
504
505
		return false;
	}
	
    // lecture des nombres de sommets/aretes/faces
	m_nbVertices = pid.nbVertices();
	m_nbFaces = pid.nbFaces();
 
	//lecture sommets
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);
Pierre Kraemer's avatar
Pierre Kraemer committed
506
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
507
508
	{
		VEC3 pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
509
		pid.vertexPosition(i, pos);
Pierre Kraemer's avatar
Pierre Kraemer committed
510

Pierre Kraemer's avatar
Pierre Kraemer committed
511
512
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
513
514
515
516
517
518
519

		verticesID.push_back(id);
	}

	m_nbEdges.reserve(m_nbFaces);
	m_emb.reserve(m_nbVertices*8);

520
	for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
521
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
522
		unsigned int n = pid.getFaceValence(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
523
524
		m_nbEdges.push_back(n);
		int* indices = pid.getFaceIndices(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
525
		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
526
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
527
			m_emb.push_back(verticesID[indices[j]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
528
529
530
531
532
533
		}
	}

	return true;
}

534
535
/**
 * Import plyPTM (K Vanhoey generic format).
536
537
538
539
540
541
542
543
544
545
546
547
548
 * It can handle bivariable polynomials of any degree and returns the appropriate attrNames
 * @param filename the file to import;
 * @param attrNames reference that will be filled with the attribute names
 * the number of attrNames returned depends on the degree of the polynomials :
 *  - 1 attrName for geometric position (VEC3)
 *  - 3 attrNames for local frame (3xVEC3) : Tangent, Bitangent and Normal vector
 *  - N attrNames for the function coefficients (NxVEC3) : N RGB coefficients being successively the constants, the linears (v then u), the quadratics, etc. :  : a0 + a1*v + a2*u + a3*u*v + a4*v^2 + a5*u^2.
 *  N = 1 for constant polynomial,
 *  N = 3 for linear polynomial,
 *  N = 6 for quadratic polynomial,
 *  N = 10 for cubic degree polynomial,
 *  N = 15 for 4th degree polynomial,
 *  ...
549
550
 * @return bool : success.
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
551
template <typename PFP>
552
bool MeshTablesSurface<PFP>::importPlyPTMgeneric(const std::string& filename, std::vector<std::string>& attrNames)
553
{
554
555
	// Open file
	std::ifstream fp(filename.c_str(), std::ios::in) ;
556
557
	if (!fp.good())
	{
558
559
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
560
561
	}

562
563
	// Read quantities : #vertices, #faces, #properties, degree of polynomials
    std::string tag ;
564

565
566
    fp >> tag;
	if (tag != std::string("ply")) // verify file type
567
	{
568
569
		CGoGNerr << filename << " is not a ply file !" <<  CGoGNout ;
		return false ;
570
571
	}

572
	do // go to #vertices
573
	{
574
575
576
577
		fp >> tag ;
	} while (tag != std::string("vertex")) ;
	unsigned int nbVertices ;
	fp >> nbVertices ; // Read #vertices
578

579
580
	unsigned int nbProps = 0 ;
	do // go to #faces and count #properties
581
	{
582
		fp >> tag ;
583
		if (tag == std::string("property"))
584
585
586
			++nbProps ;
	} while (tag != std::string("face")) ;
	fp >> m_nbFaces ; // Read #vertices
587

588
589
590
591
	do // go to end of header
	{
		fp >> tag ;
	} while (tag != std::string("end_header")) ;
592

593
	unsigned int nbCoefsPerPol = (nbProps - 12) / 3 ; // get #coefficients per polynomial
594

595
596
597
598
599
	// Define containers
	AttributeHandler<typename PFP::VEC3> positions = m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
	if (!positions.isValid())
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
	attrNames.push_back(positions.name()) ;
600

601
602
603
604
605
606
607
	AttributeHandler<typename PFP::VEC3> *frame = new AttributeHandler<typename PFP::VEC3>[3] ;
	frame[0] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "frame_T") ; // Tangent
	frame[1] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "frame_B") ; // Bitangent
	frame[2] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "frame_N") ; // Normal
	attrNames.push_back(frame[0].name()) ;
	attrNames.push_back(frame[1].name()) ;
	attrNames.push_back(frame[2].name()) ;
608

609
610
	AttributeHandler<typename PFP::VEC3> *colorPTM = new AttributeHandler<typename PFP::VEC3>[nbCoefsPerPol] ;
	for (unsigned int i = 0 ; i < nbCoefsPerPol ; ++i)
611
	{
612
613
614
615
616
		std::stringstream name ;
		name << "colorPTM_a" << i ;
		colorPTM[i] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, name.str()) ;
		attrNames.push_back(colorPTM[i].name()) ;
	}
617

618
619
620
	// Read vertices
	std::vector<unsigned int> verticesID ;
	verticesID.reserve(nbVertices) ;
621

622
623
624
	float* properties = new float[nbProps] ;
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
	for (unsigned int i = 0 ; i < nbVertices ; ++i) // Read and store properties for current vertex
625
	{
626
627
		unsigned int id = container.insertLine() ;
		verticesID.push_back(id) ;
628

629
630
		for (unsigned int j = 0 ; j < nbProps ; ++j) // get all properties
			fp >> properties[j] ;
631

632
633
		positions[id] = VEC3(properties[0],properties[1],properties[2]) ; // position
		for (unsigned int k = 0 ; k < 3 ; ++k) // frame
634
635
			for (unsigned int l = 0 ; l < 3 ; ++l)
				frame[k][id][l] = properties[3+(3*k+l)] ;
636
637
638
		for (unsigned int k = 0 ; k < 3 ; ++k) // coefficients
			for (unsigned int l = 0 ; l < nbCoefsPerPol ; ++l)
				colorPTM[l][id][k] = properties[12+(nbCoefsPerPol*k+l)] ;
639
	}
640
641
	m_nbVertices = verticesID.size() ;
	delete[] properties ;
642

643
644
645
646
	// Read faces index
	m_nbEdges.reserve(m_nbFaces) ;
	m_emb.reserve(3*m_nbFaces) ;
	for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
647
	{
648
649
650
651
		// read the indices of vertices for current face
		int nbEdgesForFace ;
		fp >> nbEdgesForFace ;
		m_nbEdges.push_back(nbEdgesForFace);
652

653
654
		int vertexID ;
		for (int j=0 ; j < nbEdgesForFace ; ++j)
655
		{
656
657
			fp >> vertexID ;
			m_emb.push_back(verticesID[vertexID]);
658
659
660
		}
	}

661
662
663
664
	// Close file
	fp.close() ;

	return true ;
665
666
}

667
668
669
670
/**
 * Import plyPTM (F Larue format).
 * It handles only 2nd degree polynomials
 * @param filename : the file to import;
671
672
673
674
675
 * @param attrNames : reference that will be filled with the attribute names ;
 *  - 1 attrName for geometric position (VEC3)
 *  - 3 attrNames for local frame (3xVEC3) : Tangent, Bitangent and Normal vector
 *  - 6 attrNames for the function coefficients (6xVEC3) : 6 RGB coefficients being successively the constants, the linears (v then u) and the quadratics : a0 + a1*v + a2*u + a3*u*v + a4*v^2 + a5*u^2.
 * @return bool : success.
676
677
678
 * @return bool : success.
 */
template <typename PFP>
679
bool MeshTablesSurface<PFP>::importPlyPTM(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
680
{
681
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
682
683

	if (!positions.isValid())
684
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
685

Pierre Kraemer's avatar
Pierre Kraemer committed
686
	attrNames.push_back(positions.name()) ;
687
688

	AttributeHandler<typename PFP::VEC3> frame[3] ;
689
690
691
	frame[0] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "frame_T") ; // Tangent
	frame[1] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "frame_B") ; // Bitangent
	frame[2] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "frame_N") ; // Normal
692
693
694
695
	for (unsigned int i = 0 ; i < 3 ; ++i)
		attrNames.push_back(frame[i].name()) ;

	AttributeHandler<typename PFP::VEC3> colorPTM[6] ;
696
697
698
699
700
701
	colorPTM[0] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "colorPTM_a") ;
	colorPTM[1] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "colorPTM_b") ;
	colorPTM[2] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "colorPTM_c") ;
	colorPTM[3] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "colorPTM_d") ;
	colorPTM[4] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "colorPTM_e") ;
	colorPTM[5] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "colorPTM_f") ;
702
703
704

	for (unsigned int i = 0 ; i < 6 ; ++i)
		attrNames.push_back(colorPTM[i].name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
705

706
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
707

Pierre Kraemer's avatar
Pierre Kraemer committed
708
709
710
	std::ifstream fp(filename.c_str(), std::ios::binary);
	if (!fp.good())
	{
711
		CGoGNerr << "Unable to open file " << filename<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
712
713
714
715
716
717
718
719
720
		return false;
	}

    std::string ligne;
    std::string tag;

	fp >> tag;
	if (tag != std::string("ply"))
	{
721
		CGoGNerr <<filename<< " is not a ply file !" <<  CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
		return false;
	}

	// va au nombre de sommets
	do
	{
		fp >> tag;
	} while (tag != std::string("vertex"));

	unsigned int nbp;
	fp >> nbp;
	// read points
	std::vector<unsigned int> verticesID;
	verticesID.reserve(nbp);

	// va au nombre de faces en comptant le nombre de "property"
	unsigned int nb_props = 0;
	do
	{
		fp >> tag;
		if (tag == std::string("property"))
			nb_props++;
	} while (tag != std::string("face"));

	fp >> m_nbFaces;
 	m_nbEdges.reserve(m_nbFaces);
	m_emb.reserve(3*m_nbFaces);

	// lecture des sommets

	// saute à la fin du header
	do
	{
		fp >> tag;
	} while (tag != std::string("end_header"));

	float* properties = new float[nb_props];

	for (unsigned int i = 0; i < nbp; ++i)
	{
		unsigned int id = container.insertLine();
		verticesID.push_back(id);

		for (unsigned int j = 0; j < nb_props; ++j)
		{
			fp >> properties[j];
		}

		positions[id] = VEC3(properties[0],properties[1],properties[2]);

		for (unsigned int k = 0 ; k < 3 ; ++k)
			for (unsigned int l = 0 ; l < 3 ; ++l)
774
				frame[k][id][l] = properties[3+(3*k+l)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
775
776
777

		for (unsigned int k = 0 ; k < 3 ; ++k)
			for (unsigned int l = 0 ; l < 6 ; ++l)
778
				colorPTM[l][id][k] = properties[12+(6*k+l)];
Pierre Kraemer's avatar
Pierre Kraemer committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
	}

	m_nbVertices = verticesID.size();
	delete[] properties;

// read indices of faces
	for (unsigned int i = 0; i < m_nbFaces; i++)
	{
		// read the indices vertices of face
		int nbe;
		fp >> nbe;
		m_nbEdges.push_back(nbe);

		int pt;
		for (int j=0; j<nbe; ++j)
		{
			fp >> pt;
			m_emb.push_back(verticesID[pt]);
		}
	}

	fp.close();
	return true;
802
}
Pierre Kraemer's avatar
Pierre Kraemer committed
803
804

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
805
bool MeshTablesSurface<PFP>::importCTM(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
806
{
807
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
808
809

	if (!positions.isValid())
810
		positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
811

Pierre Kraemer's avatar
Pierre Kraemer committed
812
813
	attrNames.push_back(positions.name()) ;

814
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
815

Pierre Kraemer's avatar
Pierre Kraemer committed
816
817
818
819
820
821
822
	// Load the file using the OpenCTM API
	CTMimporter ctm;
	// Load the file
	ctm.Load(filename.c_str());

 	m_nbVertices = ctm.GetInteger(CTM_VERTEX_COUNT);

Pierre Kraemer's avatar
Pierre Kraemer committed
823
 	// read points
Pierre Kraemer's avatar
Pierre Kraemer committed
824
825
826
827
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

	const CTMfloat* vertices = ctm.GetFloatArray(CTM_VERTICES);
Pierre Kraemer's avatar
Pierre Kraemer committed
828
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
829
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
830
831
832
833
		VEC3 pos(vertices[0], vertices[1], vertices[2]);
		vertices += 3; // next vertex in float*
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
834
835
836
837
838
839
840
841
842
		verticesID.push_back(id);
	}

	// read nb of faces
	m_nbFaces = ctm.GetInteger(CTM_TRIANGLE_COUNT);
	m_nbEdges.reserve(m_nbFaces);
	m_emb.reserve(3*m_nbFaces);

	const CTMuint * indices = ctm.GetIntegerArray(CTM_INDICES);
Pierre Kraemer's avatar
Pierre Kraemer committed
843
844

	// read indices of faces
Pierre Kraemer's avatar
Pierre Kraemer committed
845
846
847
848
849
850
851
852
853
854
855
856
857
	for (unsigned i=0; i<m_nbFaces; i++)
	{
		m_nbEdges.push_back(3);
		// read the three vertices of triangle
		m_emb.push_back(verticesID[*indices++]);
		m_emb.push_back(verticesID[*indices++]);
		m_emb.push_back(verticesID[*indices++]);
	}

	return true;
}

template<typename PFP>
858
void MeshTablesSurface<PFP>::extractMeshRec(AttributeContainer& container, AttributeHandler<typename PFP::VEC3>& positions, const struct aiScene* scene, const struct aiNode* nd, struct aiMatrix4x4* trafo)
Pierre Kraemer's avatar
Pierre Kraemer committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
{
	struct aiMatrix4x4 prev;

	prev = *trafo;
	aiMultiplyMatrix4(trafo,&nd->mTransformation);

	std::vector<unsigned int> verticesID;

	// foreach mesh of node
	for (unsigned int n = 0; n < nd->mNumMeshes; ++n)
	{
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];

		verticesID.clear();
		verticesID.reserve(mesh->mNumVertices);
		//read positions
		for (unsigned int t = 0; t < mesh->mNumVertices; ++t)
		{
			// transform position
			struct aiVector3D tmp = mesh->mVertices[t];
Pierre Kraemer's avatar
Pierre Kraemer committed
879
			aiTransformVecByMatrix4(&tmp, trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
880
			// now store it
Pierre Kraemer's avatar
Pierre Kraemer committed
881
882
			unsigned int id = container.insertLine();
			positions[id] = VEC3(tmp[0], tmp[1], tmp[2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
			verticesID.push_back(id);
		}
		m_nbVertices += mesh->mNumVertices;

		// read faces indices
		for (unsigned int t = 0; t < mesh->mNumFaces; ++t)
		{
			const struct aiFace* face = &mesh->mFaces[t];
			m_nbEdges.push_back(face->mNumIndices);
			for(unsigned int i = 0; i < face->mNumIndices; i++)
			{
				unsigned int pt = face->mIndices[i];
				m_emb.push_back(verticesID[pt]);
			}
		}
		m_nbFaces += mesh->mNumFaces;
	}

	// recurse on all children of node
	for (unsigned int n = 0; n < nd->mNumChildren; ++n)
	{
904
//		CGoGNout << "Children "<<n<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
905
		extractMeshRec(container, positions, scene, nd->mChildren[n], trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
906
907
908
909
910
	}
	*trafo = prev;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
911
bool MeshTablesSurface<PFP>::importASSIMP(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
912
{
913
914
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
915
916
	attrNames.push_back(positions.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
917
918
919
920
921
922
923
924
925
926
	m_nbVertices = 0;
	m_nbFaces = 0;

 	m_nbEdges.reserve(5000);
	m_emb.reserve(15000);

	struct aiMatrix4x4 trafo;
	aiIdentityMatrix4(&trafo);

	m_lab = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
927
928
	const struct aiScene* scene = aiImportFile(filename.c_str(), aiProcess_FindDegenerates | aiProcess_JoinIdenticalVertices);
	extractMeshRec(container, positions, scene, scene->mRootNode, &trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
929
930
931
932
933
934
935
936
937

	return true;
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN