import2tablesSurface.hpp 58.5 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

Pierre Kraemer's avatar
Pierre Kraemer committed
29 30
#include "Algo/Modelisation/voxellisation.h"

31
#include "Algo/Import/AHEM.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
32

33 34
#include <algorithm>

Pierre Kraemer's avatar
Pierre Kraemer committed
35 36 37 38 39 40
namespace CGoGN
{

namespace Algo
{

41 42 43
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
44 45 46 47
namespace Import
{

template<typename PFP>
48
bool MeshTablesSurface<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
49
{
Pierre Kraemer's avatar
Pierre Kraemer committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    ImportType kind = getFileType(filename);

    attrNames.clear() ;

    switch (kind)
    {
    case TRIAN:
        CGoGNout << "TYPE: TRIAN" << CGoGNendl;
        return importTrian(filename, attrNames);
        break;
    case TRIANBGZ:
        CGoGNout << "TYPE: TRIANBGZ" << CGoGNendl;
        return importTrianBinGz(filename, attrNames);
        break;
    case OFF:
        CGoGNout << "TYPE: OFF" << CGoGNendl;
        return importOff(filename, attrNames);
        break;
    case MESHBIN:
        CGoGNout << "TYPE: MESHBIN" << CGoGNendl;
        return importMeshBin(filename, attrNames);
        break;
    case PLY:
        CGoGNout << "TYPE: PLY" << CGoGNendl;
        return importPly(filename, attrNames);
        break;
    case OBJ:
        CGoGNout << "TYPE: OBJ" << CGoGNendl;
        return importObj(filename, attrNames);
        break;
    case AHEM:
        CGoGNout << "TYPE: AHEM" << CGoGNendl;
        return importAHEM(filename, attrNames);
        break;
    case STLB:
        CGoGNout << "TYPE: STLB" << CGoGNendl;
        return importSTLBin(filename, attrNames);
        break;
    case STL:
        CGoGNout << "TYPE: STL" << CGoGNendl;
        return importSTLAscii(filename, attrNames);
        break;
    default:
    #ifdef WITH_ASSIMP
        CGoGNout << "TYPE: ASSIMP" << CGoGNendl;
        return importASSIMP(filename, attrNames);
    #else
        CGoGNout << "unsupported file type"<< CGoGNendl;
    #endif
        break;
    }
    return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
105
bool MeshTablesSurface<PFP>::importTrian(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
106
{
107
	VertexAttribute<VEC3, MAP> positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109

    if (!positions.isValid())
110
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    attrNames.push_back(positions.name()) ;

    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;

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

    // read nb of points
    fp >> m_nbVertices;

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

    for (unsigned int i = 0; i < m_nbVertices; ++i)
    {
        VEC3 pos;
        fp >> pos[0];
        fp >> pos[1];
        fp >> pos[2];
        unsigned int id = container.insertLine();
        positions[id] = pos;
        verticesID.push_back(id);
    }

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

    // read indices of faces
    for (unsigned int i = 0; i < m_nbFaces; ++i)
    {
        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;
Pierre Kraemer's avatar
Pierre Kraemer committed
169 170 171
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
172
bool MeshTablesSurface<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
173
{
174
	VertexAttribute<VEC3, MAP> positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
175 176

    if (!positions.isValid())
177
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

    attrNames.push_back(positions.name()) ;

    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;

    // open file
    igzstream fs(filename.c_str(), std::ios::in|std::ios::binary);

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

    // read nb of points
    fs.read(reinterpret_cast<char*>(&m_nbVertices), sizeof(int));

    // read points
    std::vector<unsigned int> verticesID;
    {	// juste pour limiter la portee des variables
        verticesID.reserve(m_nbVertices);
        float* buffer = new float[m_nbVertices*3];
        fs.read(reinterpret_cast<char*>(buffer), 3*m_nbVertices*sizeof(float));
        float *ptr = buffer;
        for (unsigned int i = 0; i < m_nbVertices; ++i)
        {
            VEC3 pos;
            pos[0]= *ptr++;
            pos[1]= *ptr++;
            pos[2]= *ptr++;

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

            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
    {	// juste pour limiter la portee des variables
        int* buffer = new int[m_nbFaces*6];
        fs.read(reinterpret_cast<char*>(buffer),6*m_nbFaces*sizeof(float));
        int *ptr = buffer;

        for (unsigned int i = 0; i < m_nbFaces; i++)
        {
            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;
Pierre Kraemer's avatar
Pierre Kraemer committed
239 240 241
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
242
bool MeshTablesSurface<PFP>::importOff(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
243
{
244
	VertexAttribute<VEC3, MAP> positions = m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
245

Pierre Kraemer's avatar
Pierre Kraemer committed
246
    if (!positions.isValid())
247
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
248

Pierre Kraemer's avatar
Pierre Kraemer committed
249
    attrNames.push_back(positions.name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
250

Pierre Kraemer's avatar
Pierre Kraemer committed
251
    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
252

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

    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
        CGoGNerr << "Problem reading off file: not an off file" << CGoGNendl;
        CGoGNerr << ligne << CGoGNendl;
        return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
270 271 272
    }

    // lecture des nombres de sommets/faces/aretes
Pierre Kraemer's avatar
Pierre Kraemer committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    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);
    for (unsigned int i = 0; i < m_nbVertices;++i)
    {
        do
        {
            std::getline (fp, ligne);
        } while (ligne.size() == 0);

        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);

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

        verticesID.push_back(id);
    }

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

    for (unsigned int i = 0; i < m_nbFaces; ++i)
    {
        do
        {
            std::getline (fp, ligne);
        } while (ligne.size() == 0);

        std::stringstream oss(ligne);
        unsigned int n;
        oss >> n;
        m_nbEdges.push_back(n);
        for (unsigned 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>
bool MeshTablesSurface<PFP>::importVoxellisation(Algo::Surface::Modelisation::Voxellisation& voxellisation, std::vector<std::string>& attrNames)
{
343
	VertexAttribute<VEC3, MAP> positions = m_map.template getAttribute<VEC3, VERTEX>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345

    if (!positions.isValid())
346
		positions = m_map.template addAttribute<VEC3, VERTEX>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348 349 350 351 352 353 354 355 356 357 358 359

    attrNames.push_back(positions.name()) ;

    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;

    // lecture des nombres de sommets/faces
    m_nbVertices = voxellisation.getNbSommets();
    m_nbFaces = voxellisation.getNbFaces();

    //lecture sommets
    std::vector<unsigned int> verticesID;
    verticesID.reserve(m_nbVertices);
    for (unsigned int i = 0; i < m_nbVertices;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
360
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
361 362
        unsigned int id = container.insertLine();
        positions[id] = voxellisation.m_sommets[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
363

Pierre Kraemer's avatar
Pierre Kraemer committed
364
        verticesID.push_back(id);
Pierre Kraemer's avatar
Pierre Kraemer committed
365 366
    }

Pierre Kraemer's avatar
Pierre Kraemer committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    // lecture faces
    // normalement nbVertices*8 devrait suffire largement
    m_nbEdges.reserve(m_nbFaces);
    m_emb.reserve(m_nbVertices*8);

    for (unsigned int i = 0; i < m_nbFaces*4-3; i=i+4)
    {
        m_nbEdges.push_back(4); //Toutes les faces ont 4 côtés (de par leur construction)
        for (unsigned int j = 0; j < 4; ++j)
        {
            m_emb.push_back(verticesID[voxellisation.m_faces[i+j]]);
        }
    }

    return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
template <typename PFP>
template <typename PFP3>
bool MeshTablesSurface<PFP>::import3DMap(typename PFP3::MAP& map, std::vector<std::string>& attrNames)
{
	VertexAttribute<VEC3, MAP> positions = m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;


	VertexAttribute<VEC3, typename PFP3::MAP> position_from = map.template getAttribute<VEC3, VERTEX, typename PFP3::MAP>("position") ;


	if (!positions.isValid())
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;

	attrNames.push_back(positions.name()) ;

	AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;

	// lecture des nombres de sommets / faces du bord
	m_nbVertices = 0;
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		if(map.isBoundaryVertex(v.dart))
			++m_nbVertices;
	}, FORCE_DART_MARKING);

	std::cout << "nbVertices = " << m_nbVertices << std::endl;

	m_nbFaces = 0;
	foreach_cell<FACE>(map, [&] (Face f)
	{
		if(map.isBoundaryFace(f.dart))
			++m_nbFaces;
	}, FORCE_DART_MARKING);

	std::cout << "m_nbFaces = " << m_nbFaces << std::endl;

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

//	//parcours des sommets de la 3-carte
//	foreach_cell<VERTEX>(map, [&] (Vertex v)
//	{
//		//si c'est un sommet du bord
//		if(map.isBoundaryVertex(v.dart))
//		{
//			// insert une nouvelle ligne dans le container
//			unsigned int id = container.insertLine();
//			// copie de la position
//			positions[id] = position_from[id];
//			// push_back
//			verticesID.push_back(id);
//		}
//	}, FORCE_DART_MARKING);

//	//parcours des faces de la 3-carte
//	// normalement nbVertices*8 devrait suffire largement
//	m_nbEdges.reserve(m_nbFaces);
//	m_emb.reserve(m_nbVertices*8);

//	foreach_cell<FACE>(map, [&] (Face f)
//	{
//		if(map.isBoundaryFace(f.dart))
//		{
//			Dart d = f.dart;
//			if(!map.template isBoundaryMarked<3>(d))
//				d = map.phi3(d);

//			Dart dit = d;
//			do
//			{
//				unsigned int index ; // index of embedding

//				index = map.getEmbedding(Vertex(d));

//				m_emb.push_back(verticesID[index]) ;

//				dit = map.phi1(dit);
//			}while(dit != d);


//		}
//	}, FORCE_DART_MARKING);

	return true;
}

470 471 472
template<typename PFP>
bool MeshTablesSurface<PFP>::importMeshBin(const std::string& filename, std::vector<std::string>& attrNames)
{
473
	VertexAttribute<VEC3, MAP> positions = m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
474

Pierre Kraemer's avatar
Pierre Kraemer committed
475 476
    if (!positions.isValid())
    {
477
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
478
    }
479

Pierre Kraemer's avatar
Pierre Kraemer committed
480
    attrNames.push_back(positions.name()) ;
481

Pierre Kraemer's avatar
Pierre Kraemer committed
482
    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
483

Pierre Kraemer's avatar
Pierre Kraemer committed
484 485 486 487 488 489 490
    // 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;
    }
491

Pierre Kraemer's avatar
Pierre Kraemer committed
492
    // Read header
493 494 495 496 497 498 499 500 501
    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
Pierre Kraemer's avatar
Pierre Kraemer committed
502 503
    std::vector<unsigned int> verticesID ;
    verticesID.reserve(m_nbVertices) ;
504 505 506

    for (unsigned int vxNum = 0 ; vxNum < m_nbVertices ; ++vxNum)
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
507 508 509 510
        Geom::Vec3f pos ;
        fp.read((char*) &pos[0], sizeof(float)) ;
        fp.read((char*) &pos[1], sizeof(float)) ;
        fp.read((char*) &pos[2], sizeof(float)) ;
511

Pierre Kraemer's avatar
Pierre Kraemer committed
512 513
        unsigned int id = container.insertLine() ;
        positions[id] = pos ;
514

Pierre Kraemer's avatar
Pierre Kraemer committed
515
        verticesID.push_back(id) ;
516 517 518
    }

    // Read foreach face
Pierre Kraemer's avatar
Pierre Kraemer committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    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 ;
538 539 540
}


Pierre Kraemer's avatar
Pierre Kraemer committed
541
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
542
bool MeshTablesSurface<PFP>::importObj(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
543
{
544
	VertexAttribute<VEC3, MAP> positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
545

Pierre Kraemer's avatar
Pierre Kraemer committed
546
    if (!positions.isValid())
547
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
548

Pierre Kraemer's avatar
Pierre Kraemer committed
549
    attrNames.push_back(positions.name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
550

Pierre Kraemer's avatar
Pierre Kraemer committed
551
    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
552

Pierre Kraemer's avatar
Pierre Kraemer committed
553 554 555 556 557 558 559
    // open file
    std::ifstream fp(filename.c_str(), std::ios::binary);
    if (!fp.good())
    {
        CGoGNerr << "Unable to open file " << filename << CGoGNendl;
        return false;
    }
Pierre Kraemer's avatar
Pierre Kraemer committed
560

Pierre Kraemer's avatar
Pierre Kraemer committed
561
//	fp.seekg(0, std::ios::end);
Pierre Kraemer's avatar
Pierre Kraemer committed
562
//	int ab = fp.tellg();
Pierre Kraemer's avatar
Pierre Kraemer committed
563
//	fp.seekg(0, std::ios::beg);
Pierre Kraemer's avatar
Pierre Kraemer committed
564 565 566 567 568 569 570
//	int ac = fp.tellg();

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

    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
571 572
        fp >> tag;
        std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
573 574 575
    }while (tag != std::string("v"));

    // lecture des sommets
Pierre Kraemer's avatar
Pierre Kraemer committed
576 577
    std::vector<unsigned int> verticesID;
    verticesID.reserve(102400); // on tape large (400Ko wahouuuuu !!)
Pierre Kraemer's avatar
Pierre Kraemer committed
578

Pierre Kraemer's avatar
Pierre Kraemer committed
579
    unsigned int i = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
580 581
    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
582 583 584 585 586 587 588 589
        if (tag == std::string("v"))
        {
            std::stringstream oss(ligne);

            float x,y,z;
            oss >> x;
            oss >> y;
            oss >> z;
Pierre Kraemer's avatar
Pierre Kraemer committed
590

Pierre Kraemer's avatar
Pierre Kraemer committed
591
            VEC3 pos(x,y,z);
Pierre Kraemer's avatar
Pierre Kraemer committed
592

Pierre Kraemer's avatar
Pierre Kraemer committed
593 594
            unsigned int id = container.insertLine();
            positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
595

Pierre Kraemer's avatar
Pierre Kraemer committed
596 597 598
            verticesID.push_back(id);
            i++;
        }
Pierre Kraemer's avatar
Pierre Kraemer committed
599

Pierre Kraemer's avatar
Pierre Kraemer committed
600 601
        fp >> tag;
        std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
602 603
    } while (!fp.eof());

Pierre Kraemer's avatar
Pierre Kraemer committed
604
    m_nbVertices = verticesID.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
605

Pierre Kraemer's avatar
Pierre Kraemer committed
606 607 608 609
    // close/clear/open only way to go back to beginning of file
    fp.close();
    fp.clear();
    fp.open(filename.c_str());
Pierre Kraemer's avatar
Pierre Kraemer committed
610

Pierre Kraemer's avatar
Pierre Kraemer committed
611
    do
Pierre Kraemer's avatar
Pierre Kraemer committed
612
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
613 614
        fp >> tag;
        std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
615 616
    } while (tag != std::string("f"));

Pierre Kraemer's avatar
Pierre Kraemer committed
617 618
    m_nbEdges.reserve(verticesID.size()*2);
    m_emb.reserve(verticesID.size()*8);
Pierre Kraemer's avatar
Pierre Kraemer committed
619

Pierre Kraemer's avatar
Pierre Kraemer committed
620 621 622
    std::vector<int> table;
    table.reserve(64); // NBV cotes pour une face devrait suffire
    m_nbFaces = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
623 624
    do
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
        if (tag == std::string("f")) // lecture d'une face
        {
            std::stringstream oss(ligne);
            table.clear();
            while (!oss.eof())  // lecture de tous les indices
            {
                std::string str;
                oss >> str;

                unsigned int ind = 0;

                while ((ind<str.length()) && (str[ind]!='/'))
                    ind++;

                if (ind > 0)
                {
                    long index;
                    std::stringstream iss(str.substr(0, ind));
                    iss >> index;
                    table.push_back(index);
                }
            }

            unsigned int n = table.size();
            m_nbEdges.push_back(short(n));
            for (unsigned int j = 0; j < n; ++j)
            {
                int index = table[j] - 1; // les index commencent a 1 (boufonnerie d'obj ;)
                m_emb.push_back(verticesID[index]);
            }
            m_nbFaces++;
        }
        fp >> tag;
        std::getline(fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
659 660
     } while (!fp.eof());

Pierre Kraemer's avatar
Pierre Kraemer committed
661 662
    fp.close ();
    return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
663 664 665
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
666
bool MeshTablesSurface<PFP>::importPly(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
667
{
668
	VertexAttribute<VEC3, MAP> positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
669

Pierre Kraemer's avatar
Pierre Kraemer committed
670
    if (!positions.isValid())
671
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
672

Pierre Kraemer's avatar
Pierre Kraemer committed
673
    attrNames.push_back(positions.name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
674

Pierre Kraemer's avatar
Pierre Kraemer committed
675
    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
676

Pierre Kraemer's avatar
Pierre Kraemer committed
677
    PlyImportData pid;
Pierre Kraemer's avatar
Pierre Kraemer committed
678

Pierre Kraemer's avatar
Pierre Kraemer committed
679 680 681 682 683
    if (! pid.read_file(filename) )
    {
        CGoGNerr << "Unable to open file " << filename << CGoGNendl;
        return false;
    }
684

685
	VertexAttribute<VEC3, MAP> colors = m_map.template getAttribute<VEC3, VERTEX, MAP>("color") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
686 687 688
    if (pid.hasColors())
    {
        if(!colors.isValid())
689
			colors = m_map.template addAttribute<VEC3, VERTEX, MAP>("color") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
690 691
        attrNames.push_back(colors.name()) ;
    }
692

Pierre Kraemer's avatar
Pierre Kraemer committed
693
    // lecture des nombres de sommets/aretes/faces
Pierre Kraemer's avatar
Pierre Kraemer committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    m_nbVertices = pid.nbVertices();
    m_nbFaces = pid.nbFaces();

    //lecture sommets
    std::vector<unsigned int> verticesID;
    verticesID.reserve(m_nbVertices);
    for (unsigned int i = 0; i < m_nbVertices; ++i)
    {
        VEC3 pos;
        pid.vertexPosition(i, pos);

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

        if (pid.hasColorsUint8())
        {
            Geom::Vector<3, unsigned char> col ;
            pid.vertexColorUint8(i, col) ;
            colors[id][0] = col[0] ;
            colors[id][1] = col[1] ;
            colors[id][2] = col[2] ;
            colors[id] /= 255.0 ;
        }

        if (pid.hasColorsFloat32())
        {
            Geom::Vector<3, float> col ;
            pid.vertexColorFloat32(i, col) ;
            colors[id][0] = col[0] ;
            colors[id][1] = col[1] ;
            colors[id][2] = col[2] ;
        }


        verticesID.push_back(id);
    }

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

    for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
    {
        unsigned int n = pid.getFaceValence(i);
        m_nbEdges.push_back(n);
        int* indices = pid.getFaceIndices(i);
        for (unsigned int j = 0; j < n; ++j)
        {
            m_emb.push_back(verticesID[indices[j]]);
        }
    }

    return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
746 747
}

748
/**
749 750
 * Import plySLF (K Vanhoey generic format).
 * It can handle bivariable polynomials and spherical harmonics of any degree and returns the appropriate attrNames
751 752
 * @param filename the file to import;
 * @param attrNames reference that will be filled with the attribute names
753
 * the number of attrNames returned depends on the degree of the polynomials / level of the SH :
754
 *  - 1 attrName for geometric position (VEC3) : name = "position" ;
755
 *  - 3 attrNames for local frame (3xVEC3) : names are "frameT" (Tangent), "frameB" (Binormal) and "frameN" (Normal) ;
756
 *  - 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.
757
 *  Their names are : "SLFcoefs<i>" (where <i> is a number from 0 to N-1).
758 759 760 761 762
 * 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,
763 764
 *
 * N = l*l for SH of level l,
765
 * ...
766
 *  - K remaining attrNames named "remainderNo<k>" where k is an integer from 0 to K-1.
767
 * @return bool : success.
Pierre Kraemer's avatar
Pierre Kraemer committed
768
 */
769 770 771
template <typename PFP>
bool MeshTablesSurface<PFP>::importPlySLFgeneric(const std::string& filename, std::vector<std::string>& attrNames)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
772 773 774 775 776 777 778 779 780
    // Open file
    std::ifstream fp(filename.c_str(), std::ios::in) ;
    if (!fp.good())
    {
        CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
        return false ;
    }

    // Read quantities : #vertices, #faces, #properties, degree of polynomials
781 782 783
    std::string tag ;

    fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    if (tag != std::string("ply")) // verify file type
    {
        CGoGNerr << filename << " is not a ply file !" <<  CGoGNout ;
        return false ;
    }

    do // go to #vertices
    {
        fp >> tag ;
    } while (tag != std::string("vertex")) ;
    unsigned int nbVertices ;
    fp >> nbVertices ; // Read #vertices

    bool position = false ;
    bool tangent = false ;
    bool binormal = false ;
    bool normal = false ;
    bool PTM = false ;
    bool SH = false ;
    unsigned int nbProps = 0 ;		// # properties
    unsigned int nbCoefs = 0 ; 	// # coefficients
    do // go to #faces and count #properties
    {
        fp >> tag ;
        if (tag == std::string("property"))
            ++nbProps ;
        else 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"))
        else if (tag == std::string("frameT_0") || tag == std::string("frameT_1") || tag == std::string("frameT_2"))
            tangent = true ;
        //else if (tag == std::string("bx") || tag == std::string("by") || tag == std::string("bz"))
        else if (tag == std::string("frameB_0") || tag == std::string("frameB_1") || tag == std::string("frameB_2"))
            binormal = true ;
        //else if (tag == std::string("nx") || tag == std::string("ny") || tag == std::string("nz"))
        else if (tag == std::string("frameN_0") || tag == std::string("frameN_1") || tag == std::string("frameN_2"))
            normal = true ;
        // else if (tag.substr(0,1) == std::string("C") && tag.substr(2,1) == std::string("_"))
        else if ((tag.length() > 8) && tag.find("_") != std::string::npos)
        {
            if (tag.substr(0,7) == std::string("PBcoefs"))
            {
                PTM = true ;
                ++nbCoefs ;
            }
            else if (tag.substr(0,7) == std::string("SHcoefs"))
            {
                SH = true ;
                ++nbCoefs ;
            }
        }
    } while (tag != std::string("face")) ;
    unsigned int nbRemainders = nbProps ;		// # remaining properties
    nbRemainders -= nbCoefs + 3*(position==true) + 3*(tangent==true) + 3*(binormal==true) + 3*(normal==true) ;
    nbCoefs /= 3 ;

    fp >> m_nbFaces ; // Read #vertices

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

    // Define containers
848
	VertexAttribute<VEC3, MAP> positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
849

Pierre Kraemer's avatar
Pierre Kraemer committed
850
    if (!positions.isValid())
851
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
852 853
    attrNames.push_back(positions.name()) ;

854 855 856 857
	VertexAttribute<VEC3, MAP> *frame = new VertexAttribute<VEC3, MAP>[3] ;
	frame[0] = m_map.template addAttribute<VEC3, VERTEX, MAP>("frameT") ; // Tangent
	frame[0] = m_map.template addAttribute<VEC3, VERTEX, MAP>("frameB") ; // Binormal
	frame[0] = m_map.template addAttribute<VEC3, VERTEX, MAP>("frameN") ; // Normal
Pierre Kraemer's avatar
Pierre Kraemer committed
858 859 860 861
    attrNames.push_back(frame[0].name()) ;
    attrNames.push_back(frame[1].name()) ;
    attrNames.push_back(frame[2].name()) ;

862
	VertexAttribute<VEC3, MAP> *PBcoefs = NULL, *SHcoefs = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
863 864
    if (PTM)
    {
865
		PBcoefs = new VertexAttribute<VEC3, MAP>[nbCoefs] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
866 867 868 869
        for (unsigned int i = 0 ; i < nbCoefs ; ++i)
        {
            std::stringstream name ;
            name << "PBcoefs" << i ;
870
			PBcoefs[i] = m_map.template addAttribute<VEC3, VERTEX, MAP>(name.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
871 872 873 874 875 876
            attrNames.push_back(PBcoefs[i].name()) ;
        }
    }

    if (SH)
    {
877
		SHcoefs = new VertexAttribute<VEC3, MAP>[nbCoefs] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
878 879 880 881
        for (unsigned int i = 0 ; i < nbCoefs ; ++i)
        {
            std::stringstream name ;
            name << "SHcoefs" << i ;
882
			SHcoefs[i] = m_map.template addAttribute<VEC3, VERTEX, MAP>(name.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
883 884 885 886
            attrNames.push_back(SHcoefs[i].name()) ;
        }
    }

887
	VertexAttribute<REAL, MAP> *remainders = new VertexAttribute<REAL, MAP>[nbRemainders] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
888 889 890 891
    for (unsigned int i = 0 ; i < nbRemainders ; ++i)
    {
        std::stringstream name ;
        name << "remainderNo" << i ;
892
		remainders[i] = m_map.template addAttribute<REAL, VERTEX, MAP>(name.str()) ;
893
		attrNames.push_back(remainders[i].name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 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
    }

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

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

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

        positions[id] = VEC3(properties[0],properties[1],properties[2]) ; // position
        for (unsigned int k = 0 ; k < 3 ; ++k) // frame
            for (unsigned int l = 0 ; l < 3 ; ++l)
                frame[k][id][l] = properties[3+(3*k+l)] ;
        for (unsigned int l = 0 ; l < nbCoefs ; ++l) // coefficients
            for (unsigned int k = 0 ; k < 3 ; ++k)
            {
                if (PTM)
                    PBcoefs[l][id][k] = (typename PFP::REAL)(properties[12+(3*l+k)]) ;
                else /* if SH */
                    SHcoefs[l][id][k] = (typename PFP::REAL)(properties[12+(3*l+k)]) ;
            }
        unsigned int cur = 12+3*nbCoefs ;
        for (unsigned int k = 0 ; k < nbRemainders ; ++k) // remaining data
            remainders[k][id] = properties[cur + k] ;
    }
    m_nbVertices = verticesID.size() ;
    delete[] properties ;

    // Read faces index
    m_nbEdges.reserve(m_nbFaces) ;
    m_emb.reserve(3 * m_nbFaces) ;
    for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
    {
        // read the indices of vertices for current face
        unsigned int nbEdgesForFace ;
        fp >> nbEdgesForFace ;
        m_nbEdges.push_back(nbEdgesForFace);

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

    // Close file
    fp.close() ;

    return true ;
951 952 953 954 955
}

template <typename PFP>
bool MeshTablesSurface<PFP>::importPlySLFgenericBin(const std::string& filename, std::vector<std::string>& attrNames)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
956 957 958 959 960 961 962 963 964
    // 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 quantities : #vertices, #faces, #properties, degree of polynomials
965 966 967
    std::string tag ;

    fp >> tag;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    if (tag != std::string("ply")) // verify file type
    {
        CGoGNerr << filename << " is not a ply file !" <<  CGoGNout ;
        return false ;
    }

    do // go to #vertices
    {
        fp >> tag ;
    } while (tag != std::string("vertex")) ;
    unsigned int nbVertices ;
    fp >> nbVertices ; // Read #vertices

    bool position = false ;
    bool tangent = false ;
    bool binormal = false ;
    bool normal = false ;
    bool PTM = false ;
    bool SH = false ;
    unsigned int propSize = 0 ;
    unsigned int nbProps = 0 ;		// # properties
    unsigned int nbCoefs = 0 ; 	// # coefficients
    do // go to #faces and count #properties
    {
        fp >> tag ;
        if (tag == std::string("property"))
            ++nbProps ;
        else if (tag == std::string("int8") || tag == std::string("uint8"))
        {
            if (propSize < 2)
            {
                propSize = 1 ;
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: only float64 is yet handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: only float64 is yet handled") ;			}
            else
            {
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled") ;
            }
        }
        else if (tag == std::string("int16") || tag == std::string("uint16"))
        {
            if (propSize == 0 || propSize == 2)
            {
                propSize = 2 ;
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: only float64 is yet handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: only float64 is yet handled") ;			}
            else
            {
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled") ;
            }
        }
        else if (tag == std::string("int32") || tag == std::string("float32") || tag == std::string("uint32"))
        {
            if (propSize == 0 || propSize == 4)
            {
                propSize = 4 ;
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: only float64 is yet handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: only float64 is yet handled") ;
            }
            else
            {
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled") ;
            }
        }
        else if (tag == std::string("int64") || tag == std::string("float64"))
        {
            if (propSize == 0 || propSize == 8)
            {
                propSize = 8 ;
                //std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: only float32 is yet handled" << std::endl ;
                //assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: only float32 is yet handled") ;
            }
            else
            {
                std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled" << std::endl ;
                assert(!"MeshTablesSurface<PFP>::importPlySLFgenericBin: all properties should be same size: otherwise not handled") ;
            }
        }
        else 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"))
        else if (tag == std::string("frameT_0") || tag == std::string("frameT_1") || tag == std::string("frameT_2"))
            tangent = true ;
        //else if (tag == std::string("bx") || tag == std::string("by") || tag == std::string("bz"))
        else if (tag == std::string("frameB_0") || tag == std::string("frameB_1") || tag == std::string("frameB_2"))
            binormal = true ;
        //else if (tag == std::string("nx") || tag == std::string("ny") || tag == std::string("nz"))
        else if (tag == std::string("frameN_0") || tag == std::string("frameN_1") || tag == std::string("frameN_2"))
            normal = true ;
        // else if (tag.substr(0,1) == std::string("C") && tag.substr(2,1) == std::string("_"))
        else if ((tag.length() > 8) && tag.find("_") != std::string::npos)
        {
            if (tag.substr(0,7) == std::string("PBcoefs"))
            {
                PTM = true ;
                ++nbCoefs ;
            }
            else if (tag.substr(0,7) == std::string("SHcoefs"))
            {
                SH = true ;
                ++nbCoefs ;
            }
        }
    } while (tag != std::string("face")) ;
    unsigned int nbRemainders = nbProps ;		// # remaining properties
    nbRemainders -= nbCoefs + 3*(position==true) + 3*(tangent==true) + 3*(binormal==true) + 3*(normal==true) ;
    nbCoefs /= 3 ;

    assert(!(SH && PTM) || !"MeshTablesSurface<PFP>::importPlySLFgenericBin: Confusing functional colors since both SLF and RF are defined.") ;
    if (SH && PTM)
        std::cerr << "MeshTablesSurface<PFP>::importPlySLFgenericBin: Confusing functional colors since both SLF and RF are defined." << std::endl ;

    fp >> m_nbFaces ; // Read #vertices

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

    char* endline = new char[1] ;
    fp.read(endline, sizeof(char)) ;
    if (*endline == '\r') // for windows
        fp.read(endline, sizeof(char)) ;
    assert(*endline == '\n') ;
    delete[] endline ;

    // Define containers
1098
	VertexAttribute<VEC3, MAP> positions = m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1099
    if (!positions.isValid())
1100
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1101 1102
    attrNames.push_back(positions.name()) ;

1103
	VertexAttribute<VEC3, MAP> *frame = new VertexAttribute<VEC3, MAP>[3] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1104 1105
    if (tangent)
    {
1106
		frame[0] = m_map.template addAttribute<VEC3, VERTEX, MAP>("frameT") ; // Tangent
Pierre Kraemer's avatar
Pierre Kraemer committed
1107 1108 1109 1110
        attrNames.push_back(frame[0].name()) ;
    }
    if (binormal)
    {
1111
		frame[1] = m_map.template addAttribute<VEC3, VERTEX, MAP>("frameB") ; // Bitangent
Pierre Kraemer's avatar
Pierre Kraemer committed
1112 1113 1114 1115
        attrNames.push_back(frame[0].name()) ;
    }
    if (normal)
    {
1116
		frame[2] = m_map.template addAttribute<VEC3, VERTEX, MAP>("frameN") ; // Normal
Pierre Kraemer's avatar
Pierre Kraemer committed
1117 1118 1119
        attrNames.push_back(frame[0].name()) ;
    }

1120
	VertexAttribute<VEC3, MAP> *PBcoefs = NULL, *SHcoefs = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1121 1122
    if (PTM)
    {
1123
		PBcoefs = new VertexAttribute<VEC3, MAP>[nbCoefs] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1124 1125 1126 1127
        for (unsigned int i = 0 ; i < nbCoefs ; ++i)
        {
            std::stringstream name ;
            name << "PBcoefs" << i ;
1128
			PBcoefs[i] = m_map.template addAttribute<VEC3, VERTEX, MAP>(name.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1129 1130 1131 1132 1133 1134
            attrNames.push_back(PBcoefs[i].name()) ;
        }
    }

    if (SH)
    {
1135
		SHcoefs = new VertexAttribute<VEC3, MAP>[nbCoefs] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1136 1137 1138 1139
        for (unsigned int i = 0 ; i < nbCoefs ; ++i)
        {
            std::stringstream name ;
            name << "SHcoefs" << i ;
1140
			SHcoefs[i] = m_map.template addAttribute<VEC3, VERTEX, MAP>(name.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1141 1142 1143 1144
            attrNames.push_back(SHcoefs[i].name()) ;
        }
    }

1145
	VertexAttribute<REAL, MAP> *remainders = new VertexAttribute<REAL, MAP>[nbRemainders] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1146 1147 1148 1149
    for (unsigned int i = 0 ; i < nbRemainders ; ++i)
    {
        std::stringstream name ;
        name << "remainderNo" << i ;
1150
		remainders[i] = m_map.template addAttribute<REAL, VERTEX, MAP>(name.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1151 1152 1153 1154 1155 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 1223 1224 1225 1226
        attrNames.push_back(remainders[i].name()) ;
    }

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

    double* properties = new double[nbProps] ;
    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
    for (unsigned int i = 0 ; i < nbVertices ; ++i) // Read and store properties for current vertex
    {
        unsigned int id = container.insertLine() ;
        verticesID.push_back(id) ;

        fp.read((char*)properties,nbProps * propSize) ;

        // positions
        if (nbProps > 2)
            positions[id] = VEC3(properties[0],properties[1],properties[2]) ; // position

        if (tangent && binormal && normal) // == if (nbprops > 11)
            for (unsigned int k = 0 ; k < 3 ; ++k) // frame
                for (unsigned int l = 0 ; l < 3 ; ++l)
                    frame[k][id][l] = (typename PFP::REAL)(properties[3+(3*k+l)]) ;

        for (unsigned int l = 0 ; l < nbCoefs ; ++l) // coefficients
            for (unsigned int k = 0 ; k < 3 ; ++k)
            {
                if (PTM)
                    PBcoefs[l][id][k] = (typename PFP::REAL)(properties[12+(3*l+k)]) ;
                else /* if SH */
                    SHcoefs[l][id][k] = (typename PFP::REAL)(properties[12+(3*l+k)]) ;
            }

        unsigned int cur = 12+3*nbCoefs ;
        for (unsigned int k = 0 ; k < nbRemainders ; ++k) // remaining data
            remainders[k][id] = (typename PFP::REAL)(properties[cur + k]) ;
    }
    m_nbVertices = verticesID.size() ;
    delete[] properties ;

    // Read faces index
    m_nbEdges.reserve(m_nbFaces) ;
    m_emb.reserve(3 * m_nbFaces) ;
    for (unsigned int i = 0 ; i < m_nbFaces ; ++i)
    {
        // read the indices of vertices for current face
        unsigned int nbEdgesForFace ;
        unsigned char tmp ;
        fp.read((char*)&(tmp), sizeof(unsigned char)) ;
        nbEdgesForFace = tmp ;
        m_nbEdges.push_back(nbEdgesForFace);

        unsigned int vertexID ;
        for (unsigned int j=0 ; j < nbEdgesForFace ; ++j)
        {
            fp.read((char*)&vertexID, sizeof(unsigned int)) ;
            m_emb.push_back(verticesID[vertexID]);
        }
        /*// read the indices of vertices for current face
        unsigned int nbEdgesForFace ;
        fp.read((char*)&(nbEdgesForFace), sizeof(unsigned int)) ;
        m_nbEdges.push_back(nbEdgesForFace);

        unsigned int vertexID ;
        for (unsigned int j=0 ; j < nbEdgesForFace ; ++j)
        {
            fp.read((char*)&vertexID, sizeof(unsigned int)) ;
            m_emb.push_back(verticesID[vertexID]);
        }*/
    }

    // Close file
    fp.close() ;

    return true ;
1227
}
1228

1229 1230 1231 1232
/**
 * Import plyPTM (F Larue format).
 * It handles only 2nd degree polynomials
 * @param filename : the file to import;
1233 1234 1235
 * @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
1236 1237
 *  - 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.
Pierre Kraemer's avatar
Pierre Kraemer committed
1238 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 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
 */
//template <typename PFP>
//bool MeshTablesSurface<PFP>::importPlyPTM(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()) ;
//
//	AttributeHandler<typename PFP::VEC3> frame[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
//	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, "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") ;
//
//	for (unsigned int i = 0 ; i < 6 ; ++i)
//		attrNames.push_back(colorPTM[i].name()) ;
//
//	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
//
//	std::ifstream fp(filename.c_str(), std::ios::binary);
//	if (!fp.good())
//	{
//		CGoGNerr << "Unable to open file " << filename<< CGoGNendl;
//		return false;
//	}
//
//    std::string ligne;
//    std::string tag;
//
//	fp >> tag;
//	if (tag != std::string("ply"))
//	{
//		CGoGNerr <<filename<< " is not a ply file !" <<  CGoGNendl;
//		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)
//				frame[k][id][l] = properties[3+(3*k+l)] ;
//
//		for (unsigned int k = 0 ; k < 3 ; ++k)
//			for (unsigned int l = 0 ; l < 6 ; ++l)
//				colorPTM[l][id][k] = properties[12+(6*k+l)];
//	}
//
//	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;
//}
1364 1365 1366 1367

template <typename PFP>
bool MeshTablesSurface<PFP>::importAHEM(const std::string& filename, std::vector<std::string>& attrNames)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1368 1369 1370
    // Open file

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

Pierre Kraemer's avatar
Pierre Kraemer committed
1372 1373 1374 1375 1376
    if (!fp.good())
    {
        CGoGNerr << "Unable to open file " << filename << CGoGNendl;
        return false;
    }
1377

Pierre Kraemer's avatar
Pierre Kraemer committed
1378
    // Read header
1379

Pierre Kraemer's avatar
Pierre Kraemer committed
1380
    AHEMHeader hdr;
1381

Pierre Kraemer's avatar
Pierre Kraemer committed
1382
    fp.read((char*)&hdr, sizeof(AHEMHeader));
1383

Pierre Kraemer's avatar
Pierre Kraemer committed
1384 1385
    if(hdr.magic != AHEM_MAGIC)
        CGoGNerr << "Warning: " << filename << " invalid magic" << CGoGNendl;
1386

Pierre Kraemer's avatar
Pierre Kraemer committed
1387 1388
    m_nbVertices = hdr.meshHdr.vxCount;
    m_nbFaces = hdr.meshHdr.faceCount;
1389

Pierre Kraemer's avatar
Pierre Kraemer committed
1390
    // Read attributes
1391

Pierre Kraemer's avatar
Pierre Kraemer committed
1392 1393
    AHEMAttributeDescriptor* ahemAttrDesc = new AHEMAttributeDescriptor[hdr.attributesChunkNumber];
    char** ahemAttrNames = new char*[hdr.attributesChunkNumber];
1394

Pierre Kraemer's avatar
Pierre Kraemer committed
1395 1396 1397
    for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
    {
        fp.read((char*)(ahemAttrDesc + i), sizeof(AHEMAttributeDescriptor));
1398

Pierre Kraemer's avatar
Pierre Kraemer committed
1399 1400 1401 1402
        ahemAttrNames[i] = new char[ahemAttrDesc[i].nameSize + 1];
        fp.read(ahemAttrNames[i], ahemAttrDesc[i].nameSize);
        ahemAttrNames[i][ahemAttrDesc[i].nameSize] = '\0';
    }
1403

Pierre Kraemer's avatar
Pierre Kraemer committed
1404
    // Compute buffer size for largest chunk and allocate
1405

Pierre Kraemer's avatar
Pierre Kraemer committed
1406
    unsigned int bufferSize = hdr.meshHdr.meshChunkSize;
1407

Pierre Kraemer's avatar
Pierre Kraemer committed
1408 1409 1410
    for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
        if(ahemAttrDesc[i].attributeChunkSize > bufferSize)
            bufferSize = ahemAttrDesc[i].attributeChunkSize;
1411

Pierre Kraemer's avatar
Pierre Kraemer committed
1412
    char* buffer = new char[bufferSize];
1413 1414 1415

    // Allocate vertices

Pierre Kraemer's avatar
Pierre Kraemer committed
1416
    AttributeContainer& vxContainer = m_map.template getAttributeContainer<VERTEX>();
1417

Pierre Kraemer's avatar
Pierre Kraemer committed
1418 1419
    std::vector<unsigned int> verticesId;
    verticesId.resize(hdr.meshHdr.vxCount);
1420

Pierre Kraemer's avatar
Pierre Kraemer committed
1421 1422
    for(unsigned int i = 0 ; i < hdr.meshHdr.vxCount ; i++)
        verticesId[i] = vxContainer.insertLine();
1423

Pierre Kraemer's avatar
Pierre Kraemer committed
1424
    // Read faces stream
1425

Pierre Kraemer's avatar
Pierre Kraemer committed
1426 1427
    m_nbEdges.resize(hdr.meshHdr.faceCount);
    m_emb.resize(hdr.meshHdr.heCount);
1428

Pierre Kraemer's avatar
Pierre Kraemer committed
1429 1430
    fp.read(buffer, hdr.meshHdr.meshChunkSize);
    char* batch = buffer;
1431

Pierre Kraemer's avatar
Pierre Kraemer committed
1432
    unsigned int fCount = 0;
1433

Pierre Kraemer's avatar
Pierre Kraemer committed
1434 1435
    unsigned int fId = 0;
    unsigned int j = 0;
1436

Pierre Kraemer's avatar
Pierre Kraemer committed
1437 1438 1439 1440
    while(fCount < hdr.meshHdr.faceCount)
    {
        AHEMFaceBatchDescriptor* fbd = (AHEMFaceBatchDescriptor*)batch;
        stUInt32* ix = (stUInt32*)(batch + sizeof(AHEMFaceBatchDescriptor));
1441

Pierre Kraemer's avatar
Pierre Kraemer committed
1442 1443 1444
        for(unsigned int i = 0 ; i < fbd->batchLength ; i++)
        {
            m_nbEdges[fId++] = fbd->batchFaceSize;
1445

Pierre Kraemer's avatar
Pierre Kraemer committed
1446 1447 1448
            for(unsigned int k = 0 ; k < fbd->batchFaceSize ; k++)
                m_emb[j++] = *ix++;
        }
1449

Pierre Kraemer's avatar
Pierre Kraemer committed
1450 1451 1452
        fCount += fbd->batchLength;
        batch = (char*)ix;
    }
1453

Pierre Kraemer's avatar
Pierre Kraemer committed
1454
    // Read positions
1455

1456
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1457

Pierre Kraemer's avatar
Pierre Kraemer committed
1458
    if (!position.isValid())
1459
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
1460

Pierre Kraemer's avatar
Pierre Kraemer committed
1461
    attrNames.push_back(position.name()) ;
1462

Pierre Kraemer's avatar
Pierre Kraemer committed
1463
    AHEMAttributeDescriptor* posDesc = NULL;
1464

Pierre Kraemer's avatar
Pierre Kraemer committed
1465 1466 1467 1468 1469 1470 1471 1472
    for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
    {
        if(IsEqualGUID(ahemAttrDesc[i].semantic, AHEMATTRIBUTE_POSITION))
        {
            posDesc = ahemAttrDesc + i;
            break;
        }
    }
1473

Pierre Kraemer's avatar
Pierre Kraemer committed
1474 1475
    fp.seekg(posDesc->fileStartOffset, std::ios_base::beg);
    fp.read(buffer, posDesc->attributeChunkSize);
1476

Pierre Kraemer's avatar
Pierre Kraemer committed
1477
    float* q = (float*)buffer;
1478

Pierre Kraemer's avatar
Pierre Kraemer committed
1479 1480 1481 1482 1483
    for(unsigned int i = 0 ; i < hdr.meshHdr.vxCount ; i++)
    {
        position[verticesId[i]] = VEC3(q[0], q[1], q[2]);
        q += 3;
    }
1484

Pierre Kraemer's avatar
Pierre Kraemer committed
1485
    // Close file and release allocated stuff
1486

Pierre Kraemer's avatar
Pierre Kraemer committed
1487
    fp.close();
1488

Pierre Kraemer's avatar
Pierre Kraemer committed
1489 1490
    for(unsigned int i = 0 ; i < hdr.attributesChunkNumber ; i++)
        delete[] ahemAttrNames[i];
1491

Pierre Kraemer's avatar
Pierre Kraemer committed
1492 1493 1494 1495 1496
    delete[] ahemAttrNames;
    delete[] ahemAttrDesc;
    delete[] buffer;

    return true;
1497 1498
}

1499
#ifdef WITH_ASSIMP
Pierre Kraemer's avatar
Pierre Kraemer committed
1500
template<typename PFP>
1501
void MeshTablesSurface<PFP>::extractMeshRec(AttributeContainer& container, VertexAttribute<VEC3, MAP>& positions, const struct aiScene* scene, const struct aiNode* nd, struct aiMatrix4x4* trafo)
Pierre Kraemer's avatar
Pierre Kraemer committed
1502
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
    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];
            aiTransformVecByMatrix4(&tmp, trafo);
            // now store it
            unsigned int id = container.insertLine();
            positions[id] = VEC3(tmp[0], tmp[1], tmp[2]);
            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)
    {
1547
//		CGoGNout << "Children "<<n<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
1548 1549 1550
        extractMeshRec(container, positions, scene, nd->mChildren[n], trafo);
    }
    *trafo = prev;
Pierre Kraemer's avatar
Pierre Kraemer committed
1551 1552
}

1553

Pierre Kraemer's avatar
Pierre Kraemer committed
1554
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
1555
bool MeshTablesSurface<PFP>::importASSIMP(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
1556
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1557
    AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
1558
	VertexAttribute<VEC3, MAP> positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1559
    attrNames.push_back(positions.name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1560

Pierre Kraemer's avatar
Pierre Kraemer committed
1561 1562
    m_nbVertices = 0;
    m_nbFaces = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
1563

Pierre Kraemer's avatar
Pierre Kraemer committed
1564 1565
    m_nbEdges.reserve(5000);
    m_emb.reserve(15000);
Pierre Kraemer's avatar
Pierre Kraemer committed
1566

Pierre Kraemer's avatar
Pierre Kraemer committed
1567 1568
    struct aiMatrix4x4 trafo;
    aiIdentityMatrix4(&trafo);
Pierre Kraemer's avatar
Pierre Kraemer committed
1569

Pierre Kraemer's avatar
Pierre Kraemer committed
1570 1571 1572
    m_lab = 0;
    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
1573

Pierre Kraemer's avatar
Pierre Kraemer committed
1574
    return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
1575
}
1576
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
1577

1578 1579 1580
template<typename PFP>
bool MeshTablesSurface<PFP>::mergeCloseVertices()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
    const 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;

1601
	VertexAttribute<VEC3, MAP> positions = m_map.template getAttribute<VEC3, VERTEX, MAP>("position");
Pierre Kraemer's avatar
Pierre Kraemer committed
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617

    // 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());

1618 1619
	VertexAutoAttribute<unsigned int, MAP> gridIndex(m_map, "gridIndex");
	VertexAutoAttribute<unsigned int, MAP> newIndices(m_map, "newIndices");
Pierre Kraemer's avatar
Pierre Kraemer committed
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723

    // 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 faces divide by 10000 (very very closed)
    unsigned int nbf = 100;
    if (nbf> m_nbFaces)
        nbf = m_nbFaces;

    int k=0;
    double d=0;
    for (unsigned int i=0; i< nbf;++i)
    {
        typename PFP::VEC3 e1 = positions[m_emb[k+1]] - positions[m_emb[k]];
        d += double(e1.norm());
        k += m_nbEdges[i];
    }
    d /= double(nbf);

    typename PFP::REAL epsilon = d/10000.0;


    // 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.template 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;
1724 1725
}

1726 1727 1728 1729 1730


template<typename PFP>
bool MeshTablesSurface<PFP>::importSTLAscii(const std::string& filename, std::vector<std::string>& attrNames)
{
1731
	VertexAttribute<VEC3, MAP> positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1732 1733

    if (!positions.isValid())
1734
		positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803