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 29.9 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Import/importPlyData.h"
26
27
#include "Algo/Geometry/boundingbox.h"
#include "Topology/generic/autoAttributeHandler.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#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
50

Pierre Kraemer's avatar
Pierre Kraemer committed
51
52
53
54
55
56
57
58
	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
59

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
66
67
68
69
70
71
72
	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
73
bool MeshTablesSurface<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames, ImportSurfacique::ImportType kind)
Pierre Kraemer's avatar
Pierre Kraemer committed
74
75
76
77
{
	if (kind == ImportSurfacique::UNKNOWNSURFACE)
		kind = getFileType(filename);

Pierre Kraemer's avatar
Pierre Kraemer committed
78
	attrNames.clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
82

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
130
131
	attrNames.push_back(positions.name()) ;

132
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
133

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
149
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
150
151
152
153
154
	{
		VEC3 pos;
		fp >> pos[0];
		fp >> pos[1];
		fp >> pos[2];
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
157
158
159
160
161
162
163
164
		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
165
166
	// read indices of faces
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
	{
		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
190
bool MeshTablesSurface<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
191
{
192
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
193
194

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

Pierre Kraemer's avatar
Pierre Kraemer committed
197
198
	attrNames.push_back(positions.name()) ;

199
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
200

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

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

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

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

			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
241
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
242
243
244
245
		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
246
		for (unsigned int i = 0; i < m_nbFaces; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
247
248
249
250
251
252
253
254
255
256
257
258
259
		{
			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
260
bool MeshTablesSurface<PFP>::importOff(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
261
{
262
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
263
264

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

Pierre Kraemer's avatar
Pierre Kraemer committed
267
268
	attrNames.push_back(positions.name()) ;

269
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
270

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

    std::string ligne;

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

    // 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
307
	for (unsigned int i = 0; i < m_nbVertices;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
308
309
310
311
	{
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
312
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
313
314
315
316
317
318
319
320
321
322

		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
323
324
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
325
326
327
328
329
330
331
332
333

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
366
367
	attrNames.push_back(positions.name()) ;

368
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
369

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

Pierre Kraemer's avatar
Pierre Kraemer committed
378
//	fp.seekg(0, std::ios::end);
Pierre Kraemer's avatar
Pierre Kraemer committed
379
//	int ab = fp.tellg();
Pierre Kraemer's avatar
Pierre Kraemer committed
380
//	fp.seekg(0, std::ios::beg);
Pierre Kraemer's avatar
Pierre Kraemer committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//	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
396
	unsigned int i = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
397
398
399
400
401
402
403
404
405
406
407
408
409
    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
410
411
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
412
413
414
415
416
417

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

		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
418
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
    } 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;
438
	table.reserve(64); // NBV cotes pour une face devrait suffire
Pierre Kraemer's avatar
Pierre Kraemer committed
439
440
441
	m_nbFaces = 0;
    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
442
    	if (tag == std::string("f")) // lecture d'une face
Pierre Kraemer's avatar
Pierre Kraemer committed
443
444
445
446
447
448
449
450
    	{
    		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
451
    			unsigned int ind = 0;
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
452
    			while ( (ind<str.length()) &&  (str[ind]!='/'))
Pierre Kraemer's avatar
Pierre Kraemer committed
453
454
    				ind++;

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

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

	fp.close ();
	return true;
}

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
489
490
	attrNames.push_back(positions.name()) ;

491
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
492

Pierre Kraemer's avatar
Pierre Kraemer committed
493
494
495
496
	PlyImportData pid;

	if (! pid.read_file(filename) )
	{
497
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
498
499
500
501
502
503
504
505
506
507
		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
508
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
509
510
	{
		VEC3 pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
511
		pid.vertexPosition(i, pos);
Pierre Kraemer's avatar
Pierre Kraemer committed
512

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

		verticesID.push_back(id);
	}

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

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

	return true;
}

536
537
/**
 * Import plyPTM (K Vanhoey generic format).
538
539
540
541
542
543
544
 * 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.
545
546
547
548
549
550
551
 * 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,
 * ...
 * Hint : N = attrNames.size() - 4 ;
552
553
 * @return bool : success.
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
554
template <typename PFP>
555
bool MeshTablesSurface<PFP>::importPlyPTMgeneric(const std::string& filename, std::vector<std::string>& attrNames)
556
{
557
558
	// Open file
	std::ifstream fp(filename.c_str(), std::ios::in) ;
559
560
	if (!fp.good())
	{
561
562
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
563
564
	}

565
566
	// Read quantities : #vertices, #faces, #properties, degree of polynomials
    std::string tag ;
567

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

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

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

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

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

598
599
600
601
602
	// 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()) ;
603

604
605
606
607
608
609
610
	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()) ;
611

612
613
	AttributeHandler<typename PFP::VEC3> *colorPTM = new AttributeHandler<typename PFP::VEC3>[nbCoefsPerPol] ;
	for (unsigned int i = 0 ; i < nbCoefsPerPol ; ++i)
614
	{
615
616
617
618
619
		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()) ;
	}
620

621
622
623
	// Read vertices
	std::vector<unsigned int> verticesID ;
	verticesID.reserve(nbVertices) ;
624

625
626
627
	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
628
	{
629
630
		unsigned int id = container.insertLine() ;
		verticesID.push_back(id) ;
631

632
633
		for (unsigned int j = 0 ; j < nbProps ; ++j) // get all properties
			fp >> properties[j] ;
634

635
636
		positions[id] = VEC3(properties[0],properties[1],properties[2]) ; // position
		for (unsigned int k = 0 ; k < 3 ; ++k) // frame
637
638
			for (unsigned int l = 0 ; l < 3 ; ++l)
				frame[k][id][l] = properties[3+(3*k+l)] ;
639
640
641
		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)] ;
642
	}
643
644
	m_nbVertices = verticesID.size() ;
	delete[] properties ;
645

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

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

664
665
666
667
	// Close file
	fp.close() ;

	return true ;
668
669
}

670
671
672
673
/**
 * Import plyPTM (F Larue format).
 * It handles only 2nd degree polynomials
 * @param filename : the file to import;
674
675
676
677
678
 * @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.
679
680
681
 * @return bool : success.
 */
template <typename PFP>
682
bool MeshTablesSurface<PFP>::importPlyPTM(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
683
{
684
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
685
686

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

Pierre Kraemer's avatar
Pierre Kraemer committed
689
	attrNames.push_back(positions.name()) ;
690
691

	AttributeHandler<typename PFP::VEC3> frame[3] ;
692
693
694
	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
695
696
697
698
	for (unsigned int i = 0 ; i < 3 ; ++i)
		attrNames.push_back(frame[i].name()) ;

	AttributeHandler<typename PFP::VEC3> colorPTM[6] ;
699
700
701
702
703
704
	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") ;
705
706
707

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

709
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
710

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

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

	fp >> tag;
	if (tag != std::string("ply"))
	{
724
		CGoGNerr <<filename<< " is not a ply file !" <<  CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
774
775
776
		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)
777
				frame[k][id][l] = properties[3+(3*k+l)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
778
779
780

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

	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;
805
}
Pierre Kraemer's avatar
Pierre Kraemer committed
806
807

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
815
816
	attrNames.push_back(positions.name()) ;

817
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
818

Pierre Kraemer's avatar
Pierre Kraemer committed
819
820
821
822
823
824
825
	// 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
826
 	// read points
Pierre Kraemer's avatar
Pierre Kraemer committed
827
828
829
830
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

	const CTMfloat* vertices = ctm.GetFloatArray(CTM_VERTICES);
Pierre Kraemer's avatar
Pierre Kraemer committed
831
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
832
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
833
834
835
836
		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
837
838
839
840
841
842
843
844
845
		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
846
847

	// read indices of faces
Pierre Kraemer's avatar
Pierre Kraemer committed
848
849
850
851
852
853
854
855
856
857
858
859
860
	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>
861
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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
{
	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
882
			aiTransformVecByMatrix4(&tmp, trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
883
			// now store it
Pierre Kraemer's avatar
Pierre Kraemer committed
884
885
			unsigned int id = container.insertLine();
			positions[id] = VEC3(tmp[0], tmp[1], tmp[2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
			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)
	{
907
//		CGoGNout << "Children "<<n<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
908
		extractMeshRec(container, positions, scene, nd->mChildren[n], trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
909
910
911
912
913
	}
	*trafo = prev;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
914
bool MeshTablesSurface<PFP>::importASSIMP(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
915
{
916
917
	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
918
919
	attrNames.push_back(positions.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
920
921
922
923
924
925
926
927
928
929
	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
930
931
	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
932
933
934
935

	return true;
}

936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
template<typename PFP>
bool MeshTablesSurface<PFP>::mergeCloseVertices()
{
	const unsigned int NBV=64; // seems to be good

	const int NEIGH[27]={
	-NBV*NBV - NBV - 1, 	-NBV*NBV - NBV, 	-NBV*NBV - NBV + 1,
	-NBV*NBV - 1, 	-NBV*NBV, 	-NBV*NBV + 1,
	-NBV*NBV + NBV - 1,	-NBV*NBV + NBV,	- NBV*NBV + NBV + 1,
	-NBV - 1,	- NBV,	-NBV + 1,
	-1,	0,	1,
	NBV - 1,	NBV,	NBV + 1,
	NBV*NBV - NBV - 1,	NBV*NBV - NBV,	NBV*NBV - NBV + 1,
	NBV*NBV - 1,	NBV*NBV,	NBV*NBV + 1,
	NBV*NBV + NBV - 1,	NBV*NBV + NBV,	NBV*NBV + NBV + 1};

	std::vector<unsigned int>** grid;
	grid = new std::vector<unsigned int>*[NBV*NBV*NBV];

	// init grid with null ptrs	
	for (unsigned int i=0; i<NBV*NBV*NBV; ++i)
		grid[i]=NULL;
	
	AttributeHandler<typename PFP::VEC3> positions = m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position");
	
	// compute BB
	Geom::BoundingBox<typename PFP::VEC3> bb(positions[ positions.begin() ]) ;
	for (unsigned int i = positions.begin(); i != positions.end(); positions.next(i))
	{
		bb.addPoint(positions[i]) ;
	}

	// add one voxel around to avoid testing		
	typename PFP::VEC3 bbsize = (bb.max() - bb.min());
	typename PFP::VEC3 one = bbsize/(NBV-2);
	one*= 1.001f;
	bb.addPoint( bb.min() - one);
	bb.addPoint( bb.max() + one);
	bbsize = (bb.max() - bb.min());
	

	AutoAttributeHandler<unsigned int> gridIndex(m_map,VERTEX, "gridIndex");
	AutoAttributeHandler<unsigned int> newIndices(m_map,VERTEX, "newIndices");
	
	
	// Store each vertex in the grid and store voxel index in vertex attribute
	for (unsigned int i = positions.begin(); i != positions.end(); positions.next(i))
	{
		typename PFP::VEC3 P = positions[i];
		P -= bb.min();
		float pz = floor((P[2]/bbsize[2])*NBV);
		float py = floor((P[1]/bbsize[1])*NBV);
		float px = floor((P[0]/bbsize[0])*NBV);

		unsigned int index = NBV*NBV*pz + NBV*py + px;
		
		if (pz==63) 
			std::cout << "z 63 bb:"<<bb<<"  P="<<positions[i]<< std::endl;
		
		std::vector<unsigned int>* vox = grid[index];
		if (vox==NULL)
		{
			grid[index] = new std::vector<unsigned int>();
			grid[index]->reserve(8);
			vox = grid[index];
		}
		vox->push_back(i);
		gridIndex[i] = index;
		newIndices[i] = 0xffffffff;
	}
	
	// compute EPSILON: average length of 50 of 100 first edges of first faces divide by 10000
	int nb = 100;
	if (m_nbEdges.size()< 100) 
		nb = m_nbEdges.size();
		
	int k=0;
	typename PFP::REAL d=0;
	for (int i=0; i< nb; i+=2)
	{
		typename PFP::VEC3 e1 = positions[m_emb[k+1]] - positions[m_emb[k]];
		d += e1.norm();
		k += m_nbEdges[i];
	}
	d /= float(nb/2);
	
	typename PFP::REAL EPSILON = d/10000.0f;
	
	
	// traverse vertices
	for (unsigned int i = positions.begin(); i != positions.end(); positions.next(i))
	{
		if (newIndices[i] == 0xffffffff) 
		{
			const typename PFP::VEC3& P = positions[i];
			
			for (unsigned int n=0; n<27; ++n)
			{
				std::vector<unsigned int>* voxel = grid[gridIndex[i]+NEIGH[n]];
				if (voxel != NULL)
				{
					for (std::vector<unsigned int>::iterator v = voxel->begin(); v != voxel->end(); ++v)
					{
						if ((*v != i) && (*v != 0xffffffff))
						{
							typename PFP::VEC3 Q = positions[*v];
							Q -= P;
							typename PFP::REAL d2 = Q*Q;
							if (d2 < EPSILON*EPSILON)
							{
								newIndices[*v] = i;
								*v = 0xffffffff;
							}
						}
					}
				}
			}
		}
	}

	// update faces indices
	for	(std::vector<unsigned int>::iterator it = m_emb.begin(); it != m_emb.end(); ++it)
	{
		if (newIndices[*it] != 0xffffffff)
		{
			*it = newIndices[*it];
		}
	}

	// delete embeddings
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;

	for (unsigned int i = positions.begin(); i != positions.end(); positions.next(i))
	{
		if (newIndices[i] != 0xffffffff)
		{
			container.removeLine(i);
		}
	}

	// release grid memory	
	for (unsigned int i=0; i<NBV*NBV*NBV; ++i)
		if (grid[i]!=NULL)
			delete grid[i];

	delete[] grid;

	return true;
}



Pierre Kraemer's avatar
Pierre Kraemer committed
1088
1089
1090
1091
1092
} // namespace Import

} // namespace Algo

} // namespace CGoGN