import2tablesSurface.hpp 35.8 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-2012, 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.unistra.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
#include "Algo/Import/AHEM.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30

Sylvain Thery's avatar
Sylvain Thery committed
31
#ifdef WITH_ASSIMP
Pierre Kraemer's avatar
Pierre Kraemer committed
32 33 34
#include "assimp.h"
#include "aiPostProcess.h"
#include "aiScene.h"
Sylvain Thery's avatar
Sylvain Thery committed
35
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

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
51

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

55 56 57
	if ((filename.rfind(".meshbin")!=std::string::npos) || (filename.rfind(".MESHBIN")!=std::string::npos))
		return ImportSurfacique::MESHBIN;

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

61 62 63
	if ((filename.rfind(".plyPTMext")!=std::string::npos) || (filename.rfind(".plySHreal")!=std::string::npos))
		return ImportSurfacique::PLYSLFgeneric;

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

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

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

73 74 75
	if ((filename.rfind(".ahem")!=std::string::npos) || (filename.rfind(".AHEM")!=std::string::npos))
		return ImportSurfacique::AHEM;

Pierre Kraemer's avatar
Pierre Kraemer committed
76 77 78 79
	return ImportSurfacique::UNKNOWNSURFACE;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
80
bool MeshTablesSurface<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames, ImportSurfacique::ImportType kind)
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83 84
{
	if (kind == ImportSurfacique::UNKNOWNSURFACE)
		kind = getFileType(filename);

Pierre Kraemer's avatar
Pierre Kraemer committed
85
	attrNames.clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88 89

	switch (kind)
	{
	case ImportSurfacique::TRIAN:
90
		CGoGNout << "TYPE: TRIAN" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
		return importTrian(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93
		break;
	case ImportSurfacique::TRIANBGZ:
94
		CGoGNout << "TYPE: TRIANBGZ" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
95
		return importTrianBinGz(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97
		break;
	case ImportSurfacique::OFF:
98
		CGoGNout << "TYPE: OFF" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
99
		return importOff(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
100
		break;
101 102 103 104
	case ImportSurfacique::MESHBIN:
		CGoGNout << "TYPE: MESHBIN" << CGoGNendl;
		return importMeshBin(filename, attrNames);
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
105
	case ImportSurfacique::PLY:
106
		CGoGNout << "TYPE: PLY" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
107
		return importPly(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
108
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
109
	case ImportSurfacique::PLYPTM:
110
		CGoGNout << "TYPE: PLYPTM" << CGoGNendl;
111 112
		return importPlyPTM(filename, attrNames);
		break;
113 114 115
	case ImportSurfacique::PLYSLFgeneric:
		CGoGNout << "TYPE: PLYSLFgeneric" << CGoGNendl;
		return importPlySLFgeneric(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
116
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
117
	case ImportSurfacique::OBJ:
118
		CGoGNout << "TYPE: OBJ" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
119
		return importObj(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
120
		break;
121 122 123 124
	case ImportSurfacique::AHEM:
		CGoGNout << "TYPE: AHEM" << CGoGNendl;
		return importAHEM(filename, attrNames);
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
	default:
Sylvain Thery's avatar
Sylvain Thery committed
126
	#ifdef WITH_ASSIMP
127
		CGoGNout << "TYPE: ASSIMP" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
		return importASSIMP(filename, attrNames);
Sylvain Thery's avatar
Sylvain Thery committed
129 130 131
	#else
		CGoGNout << "unsupported file type"<< CGoGNendl;		
	#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134 135 136 137
		break;
	}
	return false;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
138
bool MeshTablesSurface<PFP>::importTrian(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
139
{
140
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
141 142

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

Pierre Kraemer's avatar
Pierre Kraemer committed
145 146
	attrNames.push_back(positions.name()) ;

147
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
148

Pierre Kraemer's avatar
Pierre Kraemer committed
149 150 151 152
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
153
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155 156
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
157
	// read nb of points
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159
	fp >> m_nbVertices;

Pierre Kraemer's avatar
Pierre Kraemer committed
160
	// read points
Pierre Kraemer's avatar
Pierre Kraemer committed
161 162 163
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

Pierre Kraemer's avatar
Pierre Kraemer committed
164
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169
	{
		VEC3 pos;
		fp >> pos[0];
		fp >> pos[1];
		fp >> pos[2];
Pierre Kraemer's avatar
Pierre Kraemer committed
170 171
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
172 173 174 175 176 177 178 179
		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
180 181
	// read indices of faces
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	{
		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
205
bool MeshTablesSurface<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
206
{
207
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
208 209

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

Pierre Kraemer's avatar
Pierre Kraemer committed
212 213
	attrNames.push_back(positions.name()) ;

214
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
215

Pierre Kraemer's avatar
Pierre Kraemer committed
216 217 218 219 220
	// open file
	igzstream fs(filename.c_str(), std::ios::in|std::ios::binary);

	if (!fs.good())
	{
221
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
222 223
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
224

Pierre Kraemer's avatar
Pierre Kraemer committed
225 226 227 228 229
	// 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
230
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
231 232
		verticesID.reserve(m_nbVertices);
		float* buffer = new float[m_nbVertices*3];
Pierre Kraemer's avatar
Pierre Kraemer committed
233
		fs.read(reinterpret_cast<char*>(buffer), 3*m_nbVertices*sizeof(float));
Pierre Kraemer's avatar
Pierre Kraemer committed
234
		float *ptr = buffer;
Pierre Kraemer's avatar
Pierre Kraemer committed
235
		for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237 238 239 240 241
		{
			VEC3 pos;
			pos[0]= *ptr++;
			pos[1]= *ptr++;
			pos[2]= *ptr++;

Pierre Kraemer's avatar
Pierre Kraemer committed
242 243
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
244 245 246 247 248 249 250 251 252 253 254 255

			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
256
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259 260
		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
261
		for (unsigned int i = 0; i < m_nbFaces; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
262 263 264 265 266 267 268 269 270 271 272 273 274
		{
			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
275
bool MeshTablesSurface<PFP>::importOff(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
276
{
277
	AttributeHandler<typename PFP::VEC3> positions = m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
278 279

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

Pierre Kraemer's avatar
Pierre Kraemer committed
282 283
	attrNames.push_back(positions.name()) ;

284
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
285

Pierre Kraemer's avatar
Pierre Kraemer committed
286 287 288 289
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
290
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
291 292 293 294 295 296 297 298 299
		return false;
	}

    std::string ligne;

    // lecture de OFF
    std::getline (fp, ligne);
    if (ligne.rfind("OFF") == std::string::npos)
    {
300 301
		CGoGNerr << "Problem reading off file: not an off file" << CGoGNendl;
		CGoGNerr << ligne << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
302
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
303 304 305 306 307 308 309 310
    }

    // lecture des nombres de sommets/faces/aretes
	int nbe;
    {
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
311
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
312 313 314 315 316 317 318 319 320 321

	    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
322
	for (unsigned int i = 0; i < m_nbVertices;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
323 324 325 326
	{
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
327
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
328 329 330 331 332 333 334 335 336 337

		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
338 339
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
340 341 342 343 344 345 346 347 348

		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
349
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
350 351 352 353
	{
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
354
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356

		std::stringstream oss(ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
357
		unsigned int n;
Pierre Kraemer's avatar
Pierre Kraemer committed
358 359
		oss >> n;
		m_nbEdges.push_back(n);
Pierre Kraemer's avatar
Pierre Kraemer committed
360
		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
361 362 363 364 365 366 367 368 369 370 371 372
		{
			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;
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
template<typename PFP>
bool MeshTablesSurface<PFP>::importMeshBin(const std::string& filename, std::vector<std::string>& attrNames)
{
	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()) ;

	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;

	// open file
	std::ifstream fp(filename.c_str(), std::ios::in | std::ios::binary);
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}

	// Read header
    unsigned int Fmin, Fmax ;
    fp.read((char*)&m_nbVertices, sizeof(unsigned int)) ;
    fp.read((char*)&m_nbFaces, sizeof(unsigned int)) ;
    fp.read((char*)&Fmin, sizeof(unsigned int)) ;
    fp.read((char*)&Fmax, sizeof(unsigned int)) ;

    assert((Fmin == 3 && Fmax == 3) || !"Only triangular faces are handled yet") ;

    // Read foreach vertex
	std::vector<unsigned int> verticesID ;
	verticesID.reserve(m_nbVertices) ;

    for (unsigned int vxNum = 0 ; vxNum < m_nbVertices ; ++vxNum)
    {
    	VEC3 pos ;
    	fp.read((char*) &pos[0], sizeof(float)) ;
    	fp.read((char*) &pos[1], sizeof(float)) ;
    	fp.read((char*) &pos[2], sizeof(float)) ;

		unsigned int id = container.insertLine() ;
		positions[id] = pos ;

		verticesID.push_back(id) ;
    }

    // Read foreach face
	m_nbEdges.reserve(m_nbFaces) ;
	m_emb.reserve(m_nbVertices * 8) ;

	for (unsigned int fNum = 0; fNum < m_nbFaces; ++fNum)
	{
		const unsigned int faceSize = 3 ;
		fp.read((char*) &faceSize, sizeof(float)) ;
		m_nbEdges.push_back(faceSize) ;

		for (unsigned int i = 0 ; i < faceSize; ++i)
		{
			unsigned int index ; // index of embedding
			fp.read((char*) &index, sizeof(unsigned int)) ;
			m_emb.push_back(verticesID[index]) ;
		}
	}

	fp.close() ;
	return true ;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
442
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
443
bool MeshTablesSurface<PFP>::importObj(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
444
{
445
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
446 447

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

Pierre Kraemer's avatar
Pierre Kraemer committed
450 451
	attrNames.push_back(positions.name()) ;

452
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
453

Pierre Kraemer's avatar
Pierre Kraemer committed
454 455 456 457
	// open file
	std::ifstream fp(filename.c_str(), std::ios::binary);
	if (!fp.good())
	{
458
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
459 460 461
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
462
//	fp.seekg(0, std::ios::end);
Pierre Kraemer's avatar
Pierre Kraemer committed
463
//	int ab = fp.tellg();
Pierre Kraemer's avatar
Pierre Kraemer committed
464
//	fp.seekg(0, std::ios::beg);
Pierre Kraemer's avatar
Pierre Kraemer committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
//	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
480
	unsigned int i = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
481 482 483 484 485 486 487 488 489 490 491 492 493
    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
494 495
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
496 497 498 499 500 501

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

		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
502
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    } 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;
522
	table.reserve(64); // NBV cotes pour une face devrait suffire
Pierre Kraemer's avatar
Pierre Kraemer committed
523 524 525
	m_nbFaces = 0;
    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
526
    	if (tag == std::string("f")) // lecture d'une face
Pierre Kraemer's avatar
Pierre Kraemer committed
527 528 529 530 531 532 533 534
    	{
    		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
535
    			unsigned int ind = 0;
Sylvain Thery's avatar
Sylvain Thery committed
536

Pierre Kraemer's avatar
Pierre Kraemer committed
537
    			while ((ind<str.length()) && (str[ind]!='/'))
Pierre Kraemer's avatar
Pierre Kraemer committed
538 539
    				ind++;

Pierre Kraemer's avatar
Pierre Kraemer committed
540
				if (ind > 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
541 542
				{
    				long index;
Pierre Kraemer's avatar
Pierre Kraemer committed
543
					std::stringstream iss(str.substr(0, ind));
Pierre Kraemer's avatar
Pierre Kraemer committed
544 545 546 547 548
					iss >> index;
		   			table.push_back(index);
				}
    		}

Pierre Kraemer's avatar
Pierre Kraemer committed
549
    		unsigned int n = table.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
550
			m_nbEdges.push_back(short(n));
Pierre Kraemer's avatar
Pierre Kraemer committed
551
    		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
552
    		{
Pierre Kraemer's avatar
Pierre Kraemer committed
553
    			int index = table[j] - 1; // les index commencent a 1 (boufonnerie d'obj ;)
Pierre Kraemer's avatar
Pierre Kraemer committed
554 555 556 557 558
				m_emb.push_back(verticesID[index]);
    		}
			m_nbFaces++;
    	}
		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
559
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
560 561 562 563 564 565 566
     } while (!fp.eof());

	fp.close ();
	return true;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
567
bool MeshTablesSurface<PFP>::importPly(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
568
{
569
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
570 571

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

Pierre Kraemer's avatar
Pierre Kraemer committed
574 575
	attrNames.push_back(positions.name()) ;

576
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
577

Pierre Kraemer's avatar
Pierre Kraemer committed
578 579 580 581
	PlyImportData pid;

	if (! pid.read_file(filename) )
	{
582
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
583 584 585 586 587 588 589 590 591 592
		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
593
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
594 595
	{
		VEC3 pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
596
		pid.vertexPosition(i, pos);
Pierre Kraemer's avatar
Pierre Kraemer committed
597

Pierre Kraemer's avatar
Pierre Kraemer committed
598 599
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
600 601 602 603 604 605 606

		verticesID.push_back(id);
	}

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

607
	for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
608
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
609
		unsigned int n = pid.getFaceValence(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
610 611
		m_nbEdges.push_back(n);
		int* indices = pid.getFaceIndices(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
612
		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
613
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
614
			m_emb.push_back(verticesID[indices[j]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
615 616 617 618 619 620
		}
	}

	return true;
}

621
/**
622 623
 * Import plySLF (K Vanhoey generic format).
 * It can handle bivariable polynomials and spherical harmonics of any degree and returns the appropriate attrNames
624 625
 * @param filename the file to import;
 * @param attrNames reference that will be filled with the attribute names
626
 * the number of attrNames returned depends on the degree of the polynomials / level of the SH :
627 628
 *  - 1 attrName for geometric position (VEC3) : name = "position" ;
 *  - 3 attrNames for local frame (3xVEC3) : names are "Frame_T" (Tangent), "Frame_B" (Binormal) and "Frame_N" (Normal) ;
629
 *  - 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.
630
 *  Their names are : "SLFcoefs_<i>" (where <i> is a number from 0 to N-1).
631 632 633 634 635
 * 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,
636 637
 *
 * N = l*l for SH of level l,
638
 * ...
639
 *  - K remaining attrNames named "remainderNo<k>" where k is an integer from 0 to K-1.
640 641
 * @return bool : success.
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
642
template <typename PFP>
643
bool MeshTablesSurface<PFP>::importPlySLFgeneric(const std::string& filename, std::vector<std::string>& attrNames)
644
{
645 646
	// Open file
	std::ifstream fp(filename.c_str(), std::ios::in) ;
647 648
	if (!fp.good())
	{
649 650
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
651 652
	}

653 654
	// Read quantities : #vertices, #faces, #properties, degree of polynomials
    std::string tag ;
655

656 657
    fp >> tag;
	if (tag != std::string("ply")) // verify file type
658
	{
659 660
		CGoGNerr << filename << " is not a ply file !" <<  CGoGNout ;
		return false ;
661 662
	}

663
	do // go to #vertices
664
	{
665 666 667 668
		fp >> tag ;
	} while (tag != std::string("vertex")) ;
	unsigned int nbVertices ;
	fp >> nbVertices ; // Read #vertices
669

670 671 672 673 674 675
	bool position = false ;
	bool tangent = false ;
	bool binormal = false ;
	bool normal = false ;
	unsigned int nbProps = 0 ;			// # properties
	unsigned int nbCoefsPerPol = 0 ; 	// # coefficients per polynomial
676
	do // go to #faces and count #properties
677
	{
678
		fp >> tag ;
679
		if (tag == std::string("property"))
680
			++nbProps ;
681 682 683 684 685 686 687 688 689

		if (tag == std::string("x") || tag == std::string("y") || tag == std::string("z"))
			position = true ;
		else if (tag == std::string("tx") || tag == std::string("ty") || tag == std::string("tz"))
			tangent = true ;
		else if (tag == std::string("bx") || tag == std::string("by") || tag == std::string("bz"))
			binormal = true ;
		else if (tag == std::string("nx") || tag == std::string("ny") || tag == std::string("nz"))
			normal = true ;
690
		if (tag.substr(0,1) == std::string("C") && tag.substr(2,1) == std::string("_"))
691
			++nbCoefsPerPol ;
692
	} while (tag != std::string("face")) ;
693 694 695 696
	unsigned int nbRemainders = nbProps ;		// # remaining properties
	nbRemainders -= nbCoefsPerPol + 3*(position==true) + 3*(tangent==true) + 3*(binormal==true) + 3*(normal==true) ;
	nbCoefsPerPol /= 3 ;

697
	fp >> m_nbFaces ; // Read #vertices
698

699 700 701 702
	do // go to end of header
	{
		fp >> tag ;
	} while (tag != std::string("end_header")) ;
703

704 705 706 707 708
	// 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()) ;
709

710 711 712 713 714 715 716
	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()) ;
717

718
	AttributeHandler<typename PFP::VEC3> *SLFcoefs = new AttributeHandler<typename PFP::VEC3>[nbCoefsPerPol] ;
719
	for (unsigned int i = 0 ; i < nbCoefsPerPol ; ++i)
720
	{
721
		std::stringstream name ;
722 723 724
		name << "SLFcoefs_" << i ;
		SLFcoefs[i] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, name.str()) ;
		attrNames.push_back(SLFcoefs[i].name()) ;
725
	}
726

727 728 729 730 731 732 733 734 735
	AttributeHandler<typename PFP::REAL> *remainders = new AttributeHandler<typename PFP::REAL>[nbRemainders] ;
	for (unsigned int i = 0 ; i < nbRemainders ; ++i)
	{
		std::stringstream name ;
		name << "remainderNo" << i ;
		remainders[i] = m_map.template addAttribute<typename PFP::REAL>(VERTEX, name.str()) ;
		attrNames.push_back(remainders[i].name()) ;
	}

736 737 738
	// Read vertices
	std::vector<unsigned int> verticesID ;
	verticesID.reserve(nbVertices) ;
739

740 741 742
	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
743
	{
744 745
		unsigned int id = container.insertLine() ;
		verticesID.push_back(id) ;
746

747 748
		for (unsigned int j = 0 ; j < nbProps ; ++j) // get all properties
			fp >> properties[j] ;
749

750 751
		positions[id] = VEC3(properties[0],properties[1],properties[2]) ; // position
		for (unsigned int k = 0 ; k < 3 ; ++k) // frame
752 753
			for (unsigned int l = 0 ; l < 3 ; ++l)
				frame[k][id][l] = properties[3+(3*k+l)] ;
754 755
		for (unsigned int k = 0 ; k < 3 ; ++k) // coefficients
			for (unsigned int l = 0 ; l < nbCoefsPerPol ; ++l)
756
				SLFcoefs[l][id][k] = properties[12+(nbCoefsPerPol*k+l)] ;
757 758 759
		unsigned int cur = 12+3*nbCoefsPerPol ;
		for (unsigned int k = 0 ; k < nbRemainders ; ++k) // remaining data
			remainders[k][id] = properties[cur + k] ;
760
	}
761 762
	m_nbVertices = verticesID.size() ;
	delete[] properties ;
763

764 765
	// Read faces index
	m_nbEdges.reserve(m_nbFaces) ;
766
	m_emb.reserve(3 * m_nbFaces) ;
767
	for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
768
	{
769 770 771 772
		// read the indices of vertices for current face
		int nbEdgesForFace ;
		fp >> nbEdgesForFace ;
		m_nbEdges.push_back(nbEdgesForFace);
773

774 775
		int vertexID ;
		for (int j=0 ; j < nbEdgesForFace ; ++j)
776
		{
777 778
			fp >> vertexID ;
			m_emb.push_back(verticesID[vertexID]);
779 780 781
		}
	}

782 783 784 785
	// Close file
	fp.close() ;

	return true ;
786 787
}

788 789 790 791
/**
 * Import plyPTM (F Larue format).
 * It handles only 2nd degree polynomials
 * @param filename : the file to import;
792 793 794
 * @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
795 796
 *  - 6 attrNames for the function coefficients (6xVEC3) : 6 RGB coefficients being successively the quadratic members, the linears and the constants (u then v) : a*u^2 + b*v^2 + c*uv + d*u + e*v +f.
  * @return bool : success.
797 798
 */
template <typename PFP>
799
bool MeshTablesSurface<PFP>::importPlyPTM(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
800
{
801
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
802 803

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

Pierre Kraemer's avatar
Pierre Kraemer committed
806
	attrNames.push_back(positions.name()) ;
807 808

	AttributeHandler<typename PFP::VEC3> frame[3] ;
809 810 811
	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
812 813 814 815
	for (unsigned int i = 0 ; i < 3 ; ++i)
		attrNames.push_back(frame[i].name()) ;

	AttributeHandler<typename PFP::VEC3> colorPTM[6] ;
816 817 818 819 820 821
	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") ;
822 823 824

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

826
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
827

Pierre Kraemer's avatar
Pierre Kraemer committed
828 829 830
	std::ifstream fp(filename.c_str(), std::ios::binary);
	if (!fp.good())
	{
831
		CGoGNerr << "Unable to open file " << filename<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
832 833 834 835 836 837 838 839 840
		return false;
	}

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

	fp >> tag;
	if (tag != std::string("ply"))
	{
841
		CGoGNerr <<filename<< " is not a ply file !" <<  CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
		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)
894
				frame[k][id][l] = properties[3+(3*k+l)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
895 896 897

		for (unsigned int k = 0 ; k < 3 ; ++k)
			for (unsigned int l = 0 ; l < 6 ; ++l)
898
				colorPTM[l][id][k] = properties[12+(6*k+l)];
Pierre Kraemer's avatar
Pierre Kraemer committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	}

	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;
922
}
Pierre Kraemer's avatar
Pierre Kraemer committed
923

924 925 926 927 928 929 930 931 932 933 934 935 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


template <typename PFP>
bool MeshTablesSurface<PFP>::importAHEM(const std::string& filename, std::vector<std::string>& attrNames)
{
	// Open file

	std::ifstream fp(filename.c_str(), std::ios::binary);

	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}


	// Read header

	AHEMHeader hdr;

	fp.read((char*)&hdr, sizeof(AHEMHeader));

	if(hdr.magic != AHEM_MAGIC)
		CGoGNerr << "Warning: " << filename << " invalid magic" << CGoGNendl;


	m_nbVertices = hdr.meshHdr.vxCount;
	m_nbFaces = hdr.meshHdr.faceCount;


	// Read attributes

	AHEMAttributeDescriptor* ahemAttrDesc = new AHEMAttributeDescriptor[hdr.attributesChunkNumber];
	char** ahemAttrNames = new char*[hdr.attributesChunkNumber];

	for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
	{
		fp.read((char*)(ahemAttrDesc + i), sizeof(AHEMAttributeDescriptor));
		
		ahemAttrNames[i] = new char[ahemAttrDesc[i].nameSize + 1];
		fp.read(ahemAttrNames[i], ahemAttrDesc[i].nameSize);
		ahemAttrNames[i][ahemAttrDesc[i].nameSize] = '\0';
	}
	

	// Compute buffer size for largest chunk and allocate

	unsigned int bufferSize = hdr.meshHdr.meshChunkSize;

	for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
		if(ahemAttrDesc[i].attributeChunkSize > bufferSize)
			bufferSize = ahemAttrDesc[i].attributeChunkSize;


	char* buffer = new char[bufferSize];


    // Allocate vertices

Pierre Kraemer's avatar
Pierre Kraemer committed
983
	AttributeContainer& vxContainer = m_map.getAttributeContainer(VERTEX);
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


	std::vector<unsigned int> verticesId;
	verticesId.resize(hdr.meshHdr.vxCount);

	for(unsigned int i = 0 ; i < hdr.meshHdr.vxCount ; i++)
		verticesId[i] = vxContainer.insertLine();



	// Read faces stream

	m_nbEdges.resize(hdr.meshHdr.faceCount);
	m_emb.resize(hdr.meshHdr.heCount);

	fp.read(buffer, hdr.meshHdr.meshChunkSize);
	char* batch = buffer;

	unsigned int fCount = 0;

	unsigned int fId = 0;
	unsigned int j = 0;

	while(fCount < hdr.meshHdr.faceCount)
	{
		AHEMFaceBatchDescriptor* fbd = (AHEMFaceBatchDescriptor*)batch;
		stUInt32* ix = (stUInt32*)(batch + sizeof(AHEMFaceBatchDescriptor));

		for(unsigned int i = 0 ; i < fbd->batchLength ; i++)
		{
			m_nbEdges[fId++] = fbd->batchFaceSize;

			for(unsigned int k = 0 ; k < fbd->batchFaceSize ; k++)
				m_emb[j++] = *ix++;
		}

		fCount += fbd->batchLength;
		batch = (char*)ix;
	}
	


	// Read positions

Pierre Kraemer's avatar
Pierre Kraemer committed
1028
	AttributeHandler<typename PFP::VEC3> position =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
1029 1030

	if (!position.isValid())
Pierre Kraemer's avatar
Pierre Kraemer committed
1031
		position = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
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

	attrNames.push_back(position.name()) ;

	AHEMAttributeDescriptor* posDesc = NULL;
	
	for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
		if(IsEqualGUID(ahemAttrDesc[i].semantic, AHEMATTRIBUTE_POSITION))
		{
			posDesc = ahemAttrDesc + i;
			break;
		}

	fp.seekg(posDesc->fileStartOffset, std::ios_base::beg);
	fp.read(buffer, posDesc->attributeChunkSize);

	float* q = (float*)buffer;

	for(unsigned int i = 0 ; i < hdr.meshHdr.vxCount ; i++)
	{
		position[verticesId[i]] = VEC3(q[0], q[1], q[2]);
		q += 3;
	}

	


	// Close file and release allocated stuff

	fp.close();

	for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
		delete[] ahemAttrNames[i];

	delete[] ahemAttrNames;
	delete[] ahemAttrDesc;
	delete[] buffer;

	return true;
}


Sylvain Thery's avatar
Sylvain Thery committed
1073
#ifdef WITH_ASSIMP
Pierre Kraemer's avatar
Pierre Kraemer committed
1074
template<typename PFP>
1075
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
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
{
	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
1096
			aiTransformVecByMatrix4(&tmp, trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
1097
			// now store it
Pierre Kraemer's avatar
Pierre Kraemer committed
1098 1099
			unsigned int id = container.insertLine();
			positions[id] = VEC3(tmp[0], tmp[1], tmp[2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
			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)
	{
1121
//		CGoGNout << "Children "<<n<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
1122
		extractMeshRec(container, positions, scene, nd->mChildren[n], trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
1123 1124 1125 1126
	}
	*trafo = prev;
}

Sylvain Thery's avatar
Sylvain Thery committed
1127

Pierre Kraemer's avatar
Pierre Kraemer committed
1128
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
1129
bool MeshTablesSurface<PFP>::importASSIMP(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
1130
{
1131 1132
	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
1133 1134
	attrNames.push_back(positions.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	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
1145 1146
	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
1147 1148 1149

	return true;
}
Sylvain Thery's avatar
Sylvain Thery committed
1150
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
1151

1152 1153 1154
template<typename PFP>
bool MeshTablesSurface<PFP>::mergeCloseVertices()
{
thery's avatar
thery committed
1155
	const int NBV=64; // seems to be good
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

	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;
	}
	
1223 1224 1225 1226 1227
	// compute EPSILON: average length of 50 of 100 first edges of faces divide by 10000 (very very closed)
	unsigned int nbf = 100;
	if (nbf> m_nbFaces)
		nbf = m_nbFaces;

1228
	int k=0;
1229 1230
	double d=0;
	for (unsigned int i=0; i< nbf;++i)
1231 1232
	{
		typename PFP::VEC3 e1 = positions[m_emb[k+1]] - positions[m_emb[k]];
1233
		d += double(e1.norm());
1234 1235
		k += m_nbEdges[i];
	}
1236
	d /= double(nbf);
1237
	
1238
	typename PFP::REAL EPSILON = d/10000.0;
1239
	
1240

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	// 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
1304 1305 1306 1307 1308
} // namespace Import

} // namespace Algo

} // namespace CGoGN