import2tablesSurface.hpp 20.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Import/importPlyData.h"

#include "openctm.h"

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


namespace CGoGN
{

namespace Algo
{

namespace Import
{

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

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

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

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

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

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

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

	return ImportSurfacique::UNKNOWNSURFACE;
}

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

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

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

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
117
bool MeshTablesSurface<PFP>::importTrian(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
118
{
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120 121
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

122
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
123

Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127 128 129 130 131
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		std::cerr << "Unable to open file " << filename << std::endl;
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
132
	// read nb of points
Pierre Kraemer's avatar
Pierre Kraemer committed
133 134
	fp >> m_nbVertices;

Pierre Kraemer's avatar
Pierre Kraemer committed
135
	// read points
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137 138
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

Pierre Kraemer's avatar
Pierre Kraemer committed
139
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143 144
	{
		VEC3 pos;
		fp >> pos[0];
		fp >> pos[1];
		fp >> pos[2];
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149 150 151 152 153 154
		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
155 156
	// read indices of faces
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	{
		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
180
bool MeshTablesSurface<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
181
{
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

185
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
186

Pierre Kraemer's avatar
Pierre Kraemer committed
187 188 189 190 191 192 193 194
	// open file
	igzstream fs(filename.c_str(), std::ios::in|std::ios::binary);

	if (!fs.good())
	{
		std::cerr << "Unable to open file " << filename << std::endl;
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
195

Pierre Kraemer's avatar
Pierre Kraemer committed
196 197 198 199 200
	// 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
201
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
202 203
		verticesID.reserve(m_nbVertices);
		float* buffer = new float[m_nbVertices*3];
Pierre Kraemer's avatar
Pierre Kraemer committed
204
		fs.read(reinterpret_cast<char*>(buffer), 3*m_nbVertices*sizeof(float));
Pierre Kraemer's avatar
Pierre Kraemer committed
205
		float *ptr = buffer;
Pierre Kraemer's avatar
Pierre Kraemer committed
206
		for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
207 208 209 210 211 212
		{
			VEC3 pos;
			pos[0]= *ptr++;
			pos[1]= *ptr++;
			pos[2]= *ptr++;

Pierre Kraemer's avatar
Pierre Kraemer committed
213 214
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217 218 219 220 221 222 223 224 225 226

			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
227
	{	// juste pour limiter la portee des variables
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229 230 231
		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
232
		for (unsigned int i = 0; i < m_nbFaces; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234 235 236 237 238 239 240 241 242 243 244 245
		{
			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
246
bool MeshTablesSurface<PFP>::importOff(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
247
{
Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

251
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
252

Pierre Kraemer's avatar
Pierre Kraemer committed
253 254 255 256
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
257
		std::cerr << "Unable to open file " << filename << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
258 259 260 261 262 263 264 265 266
		return false;
	}

    std::string ligne;

    // lecture de OFF
    std::getline (fp, ligne);
    if (ligne.rfind("OFF") == std::string::npos)
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269
		std::cerr << "Problem reading off file: not an off file" << std::endl;
		std::cerr << ligne << std::endl;
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    }

    // 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
289
	for (unsigned int i = 0; i < m_nbVertices;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
290 291 292 293
	{
    	do
    	{
    		std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
294
    	} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
295 296 297 298 299 300 301 302 303 304

		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
305 306
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
307 308 309 310 311 312 313 314 315

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

		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
341
bool MeshTablesSurface<PFP>::importObj(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
342
{
Pierre Kraemer's avatar
Pierre Kraemer committed
343 344 345
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

346
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
347

Pierre Kraemer's avatar
Pierre Kraemer committed
348 349 350 351
	// open file
	std::ifstream fp(filename.c_str(), std::ios::binary);
	if (!fp.good())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
352
		std::cerr << "Unable to open file " << filename << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
353 354 355
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
356
//	fp.seekg(0, std::ios::end);
Pierre Kraemer's avatar
Pierre Kraemer committed
357
//	int ab = fp.tellg();
Pierre Kraemer's avatar
Pierre Kraemer committed
358
//	fp.seekg(0, std::ios::beg);
Pierre Kraemer's avatar
Pierre Kraemer committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
//	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
374
	unsigned int i = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
375 376 377 378 379 380 381 382 383 384 385 386 387
    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
388 389
			unsigned int id = container.insertLine();
			positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
390 391 392 393 394 395

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

		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
396
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    } while (!fp.eof());

	m_nbVertices = verticesID.size();

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

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

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

	std::vector<int> table;
Pierre Kraemer's avatar
Pierre Kraemer committed
416
	table.reserve(64); // 64 cotes pour une face devrait suffire
Pierre Kraemer's avatar
Pierre Kraemer committed
417 418 419
	m_nbFaces = 0;
    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
420
    	if (tag == std::string("f")) // lecture d'une face
Pierre Kraemer's avatar
Pierre Kraemer committed
421 422 423 424 425 426 427 428
    	{
    		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
429
    			unsigned int ind = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
430 431 432
    			while ( (str[ind]!='/')&& (ind<str.length()) )
    				ind++;

Pierre Kraemer's avatar
Pierre Kraemer committed
433
				if (ind > 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
434 435
				{
    				long index;
Pierre Kraemer's avatar
Pierre Kraemer committed
436
					std::stringstream iss(str.substr(0, ind));
Pierre Kraemer's avatar
Pierre Kraemer committed
437 438 439 440 441
					iss >> index;
		   			table.push_back(index);
				}
    		}

Pierre Kraemer's avatar
Pierre Kraemer committed
442
    		unsigned int n = table.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
443
			m_nbEdges.push_back(short(n));
Pierre Kraemer's avatar
Pierre Kraemer committed
444
    		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
445
    		{
Pierre Kraemer's avatar
Pierre Kraemer committed
446
    			int index = table[j] - 1; // les index commencent a 1 (boufonnerie d'obj ;)
Pierre Kraemer's avatar
Pierre Kraemer committed
447 448 449 450 451
				m_emb.push_back(verticesID[index]);
    		}
			m_nbFaces++;
    	}
		fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
452
    	std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
453 454 455 456 457 458 459
     } while (!fp.eof());

	fp.close ();
	return true;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
460
bool MeshTablesSurface<PFP>::importPly(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
461
{
Pierre Kraemer's avatar
Pierre Kraemer committed
462 463 464
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

465
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
466

Pierre Kraemer's avatar
Pierre Kraemer committed
467 468 469 470
	PlyImportData pid;

	if (! pid.read_file(filename) )
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
471
		std::cerr << "Unable to open file " << filename << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
472 473 474 475 476 477 478 479 480 481
		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
482
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
483 484
	{
		VEC3 pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
485
		pid.vertexPosition(i, pos);
Pierre Kraemer's avatar
Pierre Kraemer committed
486

Pierre Kraemer's avatar
Pierre Kraemer committed
487 488
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
489 490 491 492 493 494 495

		verticesID.push_back(id);
	}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
496
	for (unsigned int i = 0; i < m_nbFaces; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
497
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
498
		unsigned int n = pid.getFaceValence(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
499 500
		m_nbEdges.push_back(n);
		int* indices = pid.getFaceIndices(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
501
		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
502
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
503
			m_emb.push_back(verticesID[indices[j]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
504 505 506 507 508 509
		}
	}

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
510 511 512 513 514
template <typename PFP>
bool MeshTablesSurface<PFP>::importPlyPTM(const std::string& filename, std::vector<std::string>& attrNames)
{
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

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

	AttributeHandler<typename PFP::VEC3> colorPTM[6] ;
	colorPTM[0] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_a") ;
	colorPTM[1] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_b") ;
	colorPTM[2] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_c") ;
	colorPTM[3] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_d") ;
	colorPTM[4] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_e") ;
	colorPTM[5] = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_f") ;

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

534
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
535

Pierre Kraemer's avatar
Pierre Kraemer committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	std::ifstream fp(filename.c_str(), std::ios::binary);
	if (!fp.good())
	{
		std::cerr << "Unable to open file " << filename<< std::endl;
		return false;
	}

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

	fp >> tag;
	if (tag != std::string("ply"))
	{
		std::cerr <<filename<< " is not a ply file !" <<  std::endl;
		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)
602
				frame[k][id][l] = properties[3+(3*k+l)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
603 604 605

		for (unsigned int k = 0 ; k < 3 ; ++k)
			for (unsigned int l = 0 ; l < 6 ; ++l)
606
				colorPTM[l][id][k] = properties[12+(6*k+l)];
Pierre Kraemer's avatar
Pierre Kraemer committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	}

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

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
633
bool MeshTablesSurface<PFP>::importCTM(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
634
{
Pierre Kraemer's avatar
Pierre Kraemer committed
635 636 637
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

638
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
639

Pierre Kraemer's avatar
Pierre Kraemer committed
640 641 642 643 644 645 646
	// 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
647
 	// read points
Pierre Kraemer's avatar
Pierre Kraemer committed
648 649 650 651
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

	const CTMfloat* vertices = ctm.GetFloatArray(CTM_VERTICES);
Pierre Kraemer's avatar
Pierre Kraemer committed
652
	for (unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
653
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
654 655 656 657
		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
658 659 660 661 662 663 664 665 666
		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
667 668

	// read indices of faces
Pierre Kraemer's avatar
Pierre Kraemer committed
669 670 671 672 673 674 675 676 677 678 679 680 681
	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>
682
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
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
{
	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
703
			aiTransformVecByMatrix4(&tmp, trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
704
			// now store it
Pierre Kraemer's avatar
Pierre Kraemer committed
705 706
			unsigned int id = container.insertLine();
			positions[id] = VEC3(tmp[0], tmp[1], tmp[2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
			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)
	{
//		std::cout << "Children "<<n<< std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
729
		extractMeshRec(container, positions, scene, nd->mChildren[n], trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
730 731 732 733 734
	}
	*trafo = prev;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
735
bool MeshTablesSurface<PFP>::importASSIMP(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
736
{
737
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
738 739 740
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
741 742 743 744 745 746 747 748 749 750
	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
751 752
	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
753 754 755 756 757 758 759 760 761

	return true;
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN