import2tablesVolume.hpp 16.9 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-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 PURPOSE. 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
26
27
28
29
30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

31
32
33
namespace Volume
{

Pierre Kraemer's avatar
Pierre Kraemer committed
34
35
36
37
namespace Import
{


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//template<typename PFP>
//ImportType MeshTablesVolume<PFP>::getFileType(const std::string& filename)
//{
//    if ((filename.rfind(".tetmesh")!=std::string::npos) || (filename.rfind(".TETMESH")!=std::string::npos))
//        return TETMESH;

//    if ((filename.rfind(".tet")!=std::string::npos) || (filename.rfind(".TET")!=std::string::npos))
//        return TET;

//    if ((filename.rfind(".node")!=std::string::npos) || (filename.rfind(".NODE")!=std::string::npos))
//        return NODE;

//    if ((filename.rfind(".off")!=std::string::npos) || (filename.rfind(".OFF")!=std::string::npos))
//        return OFF;

//    if ((filename.rfind(".ts")!=std::string::npos) || (filename.rfind(".TS")!=std::string::npos))
//        return TS;

//    if ((filename.rfind(".msh")!=std::string::npos) || (filename.rfind(".MSH")!=std::string::npos))
//        return MSH;

//    if ((filename.rfind(".vtu")!=std::string::npos) || (filename.rfind(".VTU")!=std::string::npos))
//        return VTU;

//    if ((filename.rfind(".nas")!=std::string::npos) || (filename.rfind(".NAS")!=std::string::npos))
//        return NAS;

//    if ((filename.rfind(".vbgz")!=std::string::npos) || (filename.rfind(".VBGZ")!=std::string::npos))
//        return VBGZ;

//    return UNKNOWNVOLUME;
//}

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

76
77
    attrNames.clear() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
	switch (kind)
	{
80
	case TET:
81
        return importTet(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
82
		break;
83
	case NODE:
84
85
86
87
88
89
    {
        size_t pos = filename.rfind(".");
        std::string fileEle = filename;
        fileEle.erase(pos);
        fileEle.append(".ele");
        return importNodeWithELERegions(filename, fileEle, attrNames);
90
		break;
91
92
93
94
95
96
97
98
99
100
    }
    case OFF:
    {
        size_t pos = filename.rfind(".");
        std::string fileEle = filename;
        fileEle.erase(pos);
        fileEle.append(".ele");
        return importOFFWithELERegions(filename, fileEle, attrNames);
        break;
    }
101
	case TS:
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        return importTs(filename, attrNames);
        break;
    case MSH:
        //return importMSH(filename, attrNames);
        break;
    case VTU:
        //return importVTU(filename, attrNames);
        break;
    case NAS:
        //return importNAS(filename, attrNames);
        break;
    case VBGZ:
        //return importVBGZ>(filename, attrNames);
        break;
    case TETMESH:
        return importTetmesh(filename, attrNames);
        break;
119
120
//	case ImportVolumique::MOKA:
//		return importMoka(filename,attrNames);
121
122
123
//		break;      
//	case OVM:
//		return importOVM(filename, attrNames);
124
//		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
	default:
126
		CGoGNerr << "Not yet supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
127
128
129
130
131
132
		break;
	}
	return false;
}

template <typename PFP>
133
bool MeshTablesVolume<PFP>::importTet(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
134
{
135
    VertexAttribute<VEC3> position =  m_map.template getAttribute<VEC3, VERTEX>("position") ;
136

137
138
    if (!position.isValid())
        position = m_map.template addAttribute<VEC3, VERTEX>("position") ;
139

140
    attrNames.push_back(position.name()) ;
141

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

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

152
153
    std::string ligne;

154
	// reading number of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
157
    oss >> m_nbVertices;
Pierre Kraemer's avatar
Pierre Kraemer committed
158

159
	// reading number of tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
160
161
	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
162
    oss2 >> m_nbVolumes;
Pierre Kraemer's avatar
Pierre Kraemer committed
163

164
	//reading vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
165
	std::vector<unsigned int> verticesID;
166
167
168
    verticesID.reserve(m_nbVertices);

    for(unsigned int i = 0; i < m_nbVertices; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
169
	{
170
171
172
173
		do
		{
			std::getline (fp, ligne);
		} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
174
175
176
177
178
179
180

		std::stringstream oss(ligne);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
181
		// TODO : if required read other vertices attributes here
182
        VEC3 pos(x,y,z);
Pierre Kraemer's avatar
Pierre Kraemer committed
183

184
		unsigned int id = container.insertLine();
185
        position[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
186
187
188
189

		verticesID.push_back(id);
	}

190
191
192
    // reading tetrahedra
    m_nbFaces.reserve(m_nbVolumes*4);
    m_emb.reserve(m_nbVolumes*12);
Pierre Kraemer's avatar
Pierre Kraemer committed
193

194
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
195
196
197
198
199
200
201
202
203
204
	{
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

		std::stringstream oss(ligne);
		int n;
		oss >> n; // nb de faces d'un volume ?

205
        m_nbFaces.push_back(4);
Pierre Kraemer's avatar
Pierre Kraemer committed
206

207
        int s0,s1,s2,s3;
Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
210
211
212
213

		oss >> s0;
		oss >> s1;
		oss >> s2;
		oss >> s3;

214
215
216
217
        m_emb.push_back(verticesID[s0]);
        m_emb.push_back(verticesID[s1]);
        m_emb.push_back(verticesID[s2]);
        m_emb.push_back(verticesID[s3]);
Pierre Kraemer's avatar
Pierre Kraemer committed
218
219
220
221
222
223
	}

	fp.close();
	return true;
}

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
template <typename PFP>
bool MeshTablesVolume<PFP>::importOFFWithELERegions(const std::string& filenameOFF, const std::string& filenameELE, std::vector<std::string>& attrNames)
{
    VertexAttribute<VEC3> position =  m_map.template getAttribute<VEC3, VERTEX>("position") ;

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

    attrNames.push_back(position.name()) ;

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

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

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

    std::string line;

    //OFF reading
    std::getline(foff, line);
    if(line.rfind("OFF") == std::string::npos)
    {
        CGoGNerr << "Problem reading off file: not an off file"<<CGoGNendl;
        CGoGNerr << line << CGoGNendl;
        return false;
    }

    //Reading number of vertex/faces/edges in OFF file
    unsigned int nbe;
    {
        do
        {
            std::getline(foff,line);
        }while(line.size() == 0);

        std::stringstream oss(line);
        oss >> m_nbVertices;
        oss >> nbe;
        oss >> nbe;
        oss >> nbe;
    }

    //Reading number of tetrahedra in ELE file
    unsigned int nbv;
    {
        do
        {
            std::getline(fele,line);
        }while(line.size() == 0);

        std::stringstream oss(line);
        oss >> m_nbVolumes;
        oss >> nbv ;
        oss >> nbv;
    }

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

    for(unsigned int i = 0 ; i < m_nbVertices ; ++i)
    {
        do
        {
            std::getline(foff,line);
        }while(line.size() == 0);

        std::stringstream oss(line);

        float x,y,z;
        oss >> x;
        oss >> y;
        oss >> z;
        //we can read colors informations if exists
        VEC3 pos(x,y,z);

        unsigned int id = container.insertLine();
        position[id] = pos;
        verticesID.push_back(id);
    }

    // reading tetrahedra
    m_nbFaces.reserve(m_nbVolumes*4);
    m_emb.reserve(m_nbVolumes*12);

    for(unsigned i = 0; i < m_nbVolumes ; ++i)
    {
        do
        {
            std::getline(fele,line);
        } while(line.size() == 0);

        std::stringstream oss(line);
        oss >> nbe;

        m_nbFaces.push_back(4);

        int s0,s1,s2,s3;

        oss >> s0;
        oss >> s1;
        oss >> s2;
        oss >> s3;

        m_emb.push_back(verticesID[s0]);
        m_emb.push_back(verticesID[s1]);
        m_emb.push_back(verticesID[s2]);
        m_emb.push_back(verticesID[s3]);
    }

    foff.close();
    fele.close();

    return true;
}

template <typename PFP>
bool MeshTablesVolume<PFP>::importNodeWithELERegions(const std::string& filenameNode, const std::string& filenameELE, std::vector<std::string>& attrNames)
{
    VertexAttribute<VEC3> position =  m_map.template getAttribute<VEC3, VERTEX>("position") ;

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

    attrNames.push_back(position.name()) ;

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

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

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

    std::string line;

    //Reading NODE file
    //First line: [# of points] [dimension (must be 3)] [# of attributes] [# of boundary markers (0 or 1)]
    unsigned int nbe;
    {
        do
        {
            std::getline(fnode,line);
        }while(line.size() == 0);

        std::stringstream oss(line);
        oss >> m_nbVertices;
        oss >> nbe;
        oss >> nbe;
        oss >> nbe;
    }

    //Reading number of tetrahedra in ELE file
    unsigned int nbv;
    {
        do
        {
            std::getline(fele,line);
        }while(line.size() == 0);

        std::stringstream oss(line);
        oss >> m_nbVolumes;
        oss >> nbv ;
        oss >> nbv;
    }

    //Reading vertices
    std::map<unsigned int,unsigned int> verticesMapID;

    for(unsigned int i = 0 ; i < m_nbVertices ; ++i)
    {
        do
        {
            std::getline(fnode,line);
        }while(line.size() == 0);

        std::stringstream oss(line);

        int idv;
        oss >> idv;

        float x,y,z;
        oss >> x;
        oss >> y;
        oss >> z;
        //we can read colors informations if exists
        VEC3 pos(x,y,z);

        unsigned int id = container.insertLine();
        position[id] = pos;
        verticesMapID.insert(std::pair<unsigned int, unsigned int>(idv,id));
    }

    // reading tetrahedra
    m_nbFaces.reserve(m_nbVolumes*4);
    m_emb.reserve(m_nbVolumes*12);

    for(unsigned i = 0; i < m_nbVolumes ; ++i)
    {
        do
        {
            std::getline(fele,line);
        } while(line.size() == 0);

        std::stringstream oss(line);
        oss >> nbe;

        m_nbFaces.push_back(4);

        int s0,s1,s2,s3;

        oss >> s0;
        oss >> s1;
        oss >> s2;
        oss >> s3;

        m_emb.push_back(verticesMapID[s0]);
        m_emb.push_back(verticesMapID[s1]);
        m_emb.push_back(verticesMapID[s2]);
        m_emb.push_back(verticesMapID[s3]);
    }

    fnode.close();
    fele.close();

    return true;
}

template <typename PFP>
bool MeshTablesVolume<PFP>::importTetmesh(const std::string& filename, std::vector<std::string>& attrNames)
{
    VertexAttribute<VEC3> position =  m_map.template getAttribute<VEC3, VERTEX>("position") ;

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

    attrNames.push_back(position.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;
    }

    std::string line;

    fp >> line;

    if (line!="Vertices")
        CGoGNerr << "Warning tetmesh file problem"<< CGoGNendl;

    fp >> m_nbVertices;

    std::cout << "READ: "<< m_nbVertices << std::endl;

    std::getline (fp, line);

    //reading vertices
    std::vector<unsigned int> verticesID;
    verticesID.reserve(m_nbVertices+1);
    verticesID.push_back(0xffffffff);

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

        std::stringstream oss(line);

        float x,y,z;
        oss >> x;
        oss >> y;
        oss >> z;
        // TODO : if required read other vertices attributes here
        VEC3 pos(x,y,z);

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

        verticesID.push_back(id);
    }

    fp >> line;
    if (line!="Tetrahedra")
        CGoGNerr << "Warning tetmesh file problem"<< CGoGNendl;

    fp >> m_nbVolumes;
    std::getline (fp, line);

    // reading tetrahedra
    m_nbFaces.reserve(m_nbVolumes*4);
    m_emb.reserve(m_nbVolumes*12);

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

        std::stringstream oss(line);

        m_nbFaces.push_back(4);

        int s0,s1,s2,s3;

        oss >> s0;
        oss >> s1;
        oss >> s2;
        oss >> s3;

        m_emb.push_back(verticesID[s0]);
        m_emb.push_back(verticesID[s1]);
        m_emb.push_back(verticesID[s2]);
        m_emb.push_back(verticesID[s3]);
    }

    fp.close();
    return true;
}

template <typename PFP>
bool MeshTablesVolume<PFP>::importTs(const std::string& filename, std::vector<std::string>& attrNames)
{
    //
    VertexAttribute<VEC3> position =  m_map.template getAttribute<VEC3, VERTEX>("position") ;

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

    attrNames.push_back(position.name()) ;

    //
    VertexAttribute<REAL> scalar = m_map.template getAttribute<REAL, VERTEX>("scalar");

    if (!scalar.isValid())
        scalar = m_map.template addAttribute<REAL, VERTEX>("scalar") ;

    attrNames.push_back(scalar.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;
    }

    std::string ligne;

    // reading number of vertices/tetrahedra
    std::getline (fp, ligne);
    std::stringstream oss(ligne);
    oss >> m_nbVertices;
    oss >> m_nbVolumes;

    //reading vertices
    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;

        VEC3 pos(x,y,z);

        unsigned int id = container.insertLine();

        position[id] = pos;
        verticesID.push_back(id);

        float scal;
        oss >> scal;
        scalar[id] = scal;
    }

    //Read and embed all tetrahedrons
    for(unsigned int i = 0; i < m_nbVolumes ; ++i)
    {
        int nbe;
        do
        {
            std::getline(fp,ligne);
        } while(ligne.size() == 0);

        std::stringstream oss(ligne);

        m_nbFaces.push_back(4);

        int s0,s1,s2,s3;

        oss >> s0;
        oss >> s1;
        oss >> s2;
        oss >> s3;

        //if regions are defined use this number
        oss >> nbe; //ignored here

        m_emb.push_back(verticesID[s0]);
        m_emb.push_back(verticesID[s1]);
        m_emb.push_back(verticesID[s2]);
        m_emb.push_back(verticesID[s3]);
    }

    fp.close();
    return true;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
673
674
} // namespace Import

675
676
}

Pierre Kraemer's avatar
Pierre Kraemer committed
677
678
679
} // namespace Algo

} // namespace CGoGN