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

25
26
#include "Geometry/orientation.h"

Sylvain Thery's avatar
Sylvain Thery committed
27
28
29
30
31
32
//#include <libxml/encoding.h>
//#include <libxml/xmlwriter.h>
//#include <libxml/parser.h>

//#include <tinyxml2.h>
#include "Utils/xml.h"
33

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

namespace Algo
{

40
41
42
namespace Volume
{

Pierre Kraemer's avatar
Pierre Kraemer committed
43
44
45
namespace Import
{

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
71
72
inline float floatFromNas(std::string& s_v)
{
	float x = 0.0f;

	std::size_t pos1 = s_v.find_last_of('-');
	if ((pos1!=std::string::npos) && (pos1!=0))
	{
		std::string res = s_v.substr(0,pos1) + "e" + s_v.substr(pos1,8-pos1);
		x = atof(res.c_str());
	}
	else
	{
		std::size_t pos2 = s_v.find_last_of('+');
		if ((pos2!=std::string::npos) && (pos2!=0))
		{
			std::string res = s_v.substr(0,pos2) + "e" + s_v.substr(pos2,8-pos2);
			x = atof(res.c_str());
		}
		else
		{
			x = atof(s_v.c_str());
		}
	}
	return x;
}


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

78
	attrNames.clear() ;
79

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

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

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

142
	attrNames.push_back(position.name()) ;
143

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

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

154
	std::string ligne;
155

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

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

166
	//reading vertices
167
168
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);
169

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

		std::stringstream oss(ligne);

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

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

189
		verticesID.push_back(id);
Pierre Kraemer's avatar
Pierre Kraemer committed
190
191
	}

192
193
194
	// reading volumes
	m_nbFaces.reserve(m_nbVolumes*4);
	m_emb.reserve(m_nbVolumes*12);
Pierre Kraemer's avatar
Pierre Kraemer committed
195

196
197
    unsigned int nbc = 0;

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

		std::stringstream oss(ligne);
		int n;
207
208
		oss >> n; // type of volumes

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
        if(!oss.good())
        {
            oss.clear();
            char dummy;
            oss >> dummy; // type of volumes
            oss >> dummy;

            if(dummy == 'C')// connector
            {
                ++nbc;
                m_nbFaces.push_back(3);

                int s0,s1,s2,s3;

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

                //std::cout << "connector " << s0 << " " << s1 << " " << s2 << " " << s3 << std::endl;

                m_emb.push_back(verticesID[s0]);
                m_emb.push_back(verticesID[s1]);
                m_emb.push_back(verticesID[s2]);
                m_emb.push_back(verticesID[s3]);
            }
        }
237
		//tetrahedron
238
        else if(n == 4)
239
240
241
242
243
244
245
246
247
248
249
250
251
		{
			m_nbFaces.push_back(4);

			int s0,s1,s2,s3;

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

			typename PFP::VEC3 P = position[verticesID[s0]];
			typename PFP::VEC3 A = position[verticesID[s1]];
			typename PFP::VEC3 B = position[verticesID[s2]];
252
            typename PFP::VEC3 C = position[verticesID[s3]];
253

254
            if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
			{
				int ui = s1;
				s1 = s2;
				s2 = ui;
			}

			m_emb.push_back(verticesID[s0]);
			m_emb.push_back(verticesID[s1]);
			m_emb.push_back(verticesID[s2]);
			m_emb.push_back(verticesID[s3]);
		}
		//pyramid
		else if(n == 5)
		{
			m_nbFaces.push_back(5);

			int s0,s1,s2,s3,s4;

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

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
			typename PFP::VEC3 P = position[verticesID[s4]];
			typename PFP::VEC3 A = position[verticesID[s0]];
			typename PFP::VEC3 B = position[verticesID[s1]];
			typename PFP::VEC3 C = position[verticesID[s2]];

			// 1 pyra ok avec cette partie
			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
			{
				unsigned int pt[5];
				pt[0] = s4;
				pt[1] = s0;
				pt[2] = s1;
				pt[3] = s2;
				pt[4] = s3;

				s0 = pt[0];
				s1 = pt[1];
				s2 = pt[2];
				s3 = pt[3];
				s4 = pt[4];
			}

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
			m_emb.push_back(verticesID[s0]);
			m_emb.push_back(verticesID[s1]);
			m_emb.push_back(verticesID[s2]);
			m_emb.push_back(verticesID[s3]);
			m_emb.push_back(verticesID[s4]);
		}
		//prism
		else if(n == 6)
		{
			m_nbFaces.push_back(6);

			int s0,s1,s2,s3,s4,s5;

			oss >> s0;
			oss >> s1;
			oss >> s2;
			oss >> s3;
			oss >> s4;
			oss >> s5;

321
322
323
324
325
			// 1 prism ok sans cette partie
//			typename PFP::VEC3 P = position[verticesID[s5]];
//			typename PFP::VEC3 A = position[verticesID[s0]];
//			typename PFP::VEC3 B = position[verticesID[s1]];
//			typename PFP::VEC3 C = position[verticesID[s2]];
326

327
328
329
//			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
//			{
//				int pt[6];
330

331
332
333
334
335
336
//				pt[0] = s0;
//				pt[1] = s1;
//				pt[2] = s2;
//				pt[3] = s3;
//				pt[4] = s4;
//				pt[5] = s5;
337

338
339
340
341
342
343
344
345
//				s0 = pt[0];
//				s1 = pt[1];
//				s2 = pt[2];
//				s3 = pt[3];
//				s4 = pt[4];
//				s5 = pt[5];

//			}
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

			m_emb.push_back(verticesID[s0]);
			m_emb.push_back(verticesID[s1]);
			m_emb.push_back(verticesID[s2]);
			m_emb.push_back(verticesID[s3]);
			m_emb.push_back(verticesID[s4]);
			m_emb.push_back(verticesID[s5]);
		}
		//hexahedron
		else if(n == 8)
		{
			m_nbFaces.push_back(8);

			int s0,s1,s2,s3,s4,s5,s6,s7;

			oss >> s0;
			oss >> s1;
			oss >> s2;
			oss >> s3;
			oss >> s4;
			oss >> s5;
			oss >> s6;
			oss >> s7;

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
            typename PFP::VEC3 P = position[verticesID[s4]];
            typename PFP::VEC3 A = position[verticesID[s0]];
            typename PFP::VEC3 B = position[verticesID[s1]];
            typename PFP::VEC3 C = position[verticesID[s2]];

            // 1 hexa ok avec cette partie
            if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
            {
                unsigned int pt[8];
                pt[0] = s0;
                pt[1] = s1;
                pt[2] = s3;
                pt[3] = s2;
                pt[4] = s4;
                pt[5] = s5;
                pt[6] = s7;
                pt[7] = s6;

                s0 = pt[0];
                s1 = pt[1];
                s2 = pt[2];
                s3 = pt[3];
                s4 = pt[4];
                s5 = pt[5];
                s6 = pt[6];
                s7 = pt[7];
            }
397
398
399
400
401
402
403
404
405
406
407

			m_emb.push_back(verticesID[s0]);
			m_emb.push_back(verticesID[s1]);
			m_emb.push_back(verticesID[s2]);
			m_emb.push_back(verticesID[s3]);
			m_emb.push_back(verticesID[s4]);
			m_emb.push_back(verticesID[s5]);
			m_emb.push_back(verticesID[s6]);
			m_emb.push_back(verticesID[s7]);
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
408

409
410
    std::cout << "#connectors = " << nbc << std::endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
411
412
413
414
	fp.close();
	return true;
}

415
416
417
template <typename PFP>
bool MeshTablesVolume<PFP>::importOFFWithELERegions(const std::string& filenameOFF, const std::string& filenameELE, std::vector<std::string>& attrNames)
{
418
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
419
420

	if (!position.isValid())
421
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
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

	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;

		typename PFP::VEC3 P = position[verticesID[s0]];
		typename PFP::VEC3 A = position[verticesID[s1]];
		typename PFP::VEC3 B = position[verticesID[s2]];
		typename PFP::VEC3 C = position[verticesID[s3]];

		if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
		{
			int ui= s0;
			s0 = s3;
			s3 = s2;
			s2 = s1;
			s1 = ui;
		}

		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;
554
555
556
557
558
}

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

	if (!position.isValid())
562
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688

	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;

		typename PFP::VEC3 P = position[verticesMapID[s0]];
		typename PFP::VEC3 A = position[verticesMapID[s1]];
		typename PFP::VEC3 B = position[verticesMapID[s2]];
		typename PFP::VEC3 C = position[verticesMapID[s3]];

		if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
		{
			int ui= s0;
			s0 = s3;
			s3 = s2;
			s2 = s1;
			s1 = ui;
		}

		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;
689
690
691
692
693
}

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

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

699
	attrNames.push_back(position.name()) ;
700

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

703
704
705
706
707
708
709
	//open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}
710

711
	std::string line;
712

713
	fp >> line;
714

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

718
	fp >> m_nbVertices;
719

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

722
	std::getline (fp, line);
723

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

729
730
731
732
733
734
	for(unsigned int i = 0; i < m_nbVertices; ++i)
	{
		do
		{
			std::getline (fp, line);
		} while (line.size() == 0);
735

736
		std::stringstream oss(line);
737

738
739
740
741
742
743
		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
		// TODO : if required read other vertices attributes here
		VEC3 pos(x,y,z);
744

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

748
749
		verticesID.push_back(id);
	}
750

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

755
756
	fp >> m_nbVolumes;
	std::getline (fp, line);
757

758
759
760
	// reading tetrahedra
	m_nbFaces.reserve(m_nbVolumes*4);
	m_emb.reserve(m_nbVolumes*12);
761

762
763
764
765
766
767
	for(unsigned i = 0; i < m_nbVolumes ; ++i)
	{
		do
		{
			std::getline(fp,line);
		} while(line.size() == 0);
768

769
		std::stringstream oss(line);
770

771
		m_nbFaces.push_back(4);
772

773
		int s0,s1,s2,s3;
774

775
776
777
778
		oss >> s0;
		oss >> s1;
		oss >> s2;
		oss >> s3;
779

780
781
782
783
		typename PFP::VEC3 P = position[verticesID[s0]];
		typename PFP::VEC3 A = position[verticesID[s1]];
		typename PFP::VEC3 B = position[verticesID[s2]];
		typename PFP::VEC3 C = position[verticesID[s3]];
784

785
786
787
788
789
790
791
792
793
794
795
796
		if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
		{
			int ui=s1;
			s1 = s2;
			s2 = ui;
		}

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

798
799
	fp.close();
	return true;
800
801
802
803
804
}

template <typename PFP>
bool MeshTablesVolume<PFP>::importTs(const std::string& filename, std::vector<std::string>& attrNames)
{
805
806
807
808
809
810
811
812
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}

813
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
814
815

	if (!position.isValid())
816
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
817
818
819

	attrNames.push_back(position.name()) ;

820
	VertexAttribute<REAL, MAP> scalar = m_map.template getAttribute<REAL, VERTEX, MAP>("scalar");
821
822

	if (!scalar.isValid())
823
		scalar = m_map.template addAttribute<REAL, VERTEX, MAP>("scalar") ;
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839

	attrNames.push_back(scalar.name()) ;

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

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

841
842
843
844
845
846
	for(unsigned int i = 0; i < m_nbVertices; ++i)
	{
		do
		{
			std::getline (fp, ligne);
		} while (ligne.size() == 0);
847

848
		std::stringstream oss(ligne);
849

850
851
852
853
		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
854

855
		VEC3 pos(x,y,z);
856

857
		unsigned int id = container.insertLine();
858

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

862
863
864
865
		float scal;
		oss >> scal;
		scalar[id] = scal;
	}
866

867
868
869
870
871
872
873
	//Read and embed all tetrahedrons
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
		do
		{
			std::getline(fp,ligne);
		} while(ligne.size() == 0);
874

875
		std::stringstream oss(ligne);
876

877
		m_nbFaces.push_back(4);
878

879
		int s0,s1,s2,s3,nbe;
880

881
882
883
884
		oss >> s0;
		oss >> s1;
		oss >> s2;
		oss >> s3;
885

886
887
888
889
		typename PFP::VEC3 P = position[verticesID[s0]];
		typename PFP::VEC3 A = position[verticesID[s1]];
		typename PFP::VEC3 B = position[verticesID[s2]];
		typename PFP::VEC3 C = position[verticesID[s3]];
890

891
892
893
894
895
896
		if(Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
		{
			int ui = s1;
			s1 = s2;
			s2 = ui;
		}
897
898


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

902
903
904
905
906
		m_emb.push_back(verticesID[s0]);
		m_emb.push_back(verticesID[s1]);
		m_emb.push_back(verticesID[s2]);
		m_emb.push_back(verticesID[s3]);
	}
907

908
909
910
	fp.close();
	return true;
}
911

912
913
914
915
916
917
918
919
920
921
template <typename PFP>
bool MeshTablesVolume<PFP>::importNAS(const std::string& filename, std::vector<std::string>& attrNames)
{
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}
922

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

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

928
	attrNames.push_back(position.name()) ;
929

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

932
933
	std::string ligne;
	std::string tag;
934

935
936
937
938
939
940
	std::getline (fp, ligne);
	do
	{
		std::getline (fp, ligne);
		tag = ligne.substr(0,4);
	} while (tag !="GRID");
941

942
943
944
945
946
947
948
	 m_nbVertices = 0;
	//reading vertices
	std::map<unsigned int, unsigned int> verticesMapID;
	do
	{
		std::string s_v = ligne.substr(8,8);
		unsigned int ind = atoi(s_v.c_str());
949

950
951
952
953
954
955
		s_v = ligne.substr(24,8);
		float x = floatFromNas(s_v);
		s_v = ligne.substr(32,8);
		float y = floatFromNas(s_v);
		s_v = ligne.substr(40,8);
		float z = floatFromNas(s_v);
956

957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
		VEC3 pos(x,y,z);
		unsigned int id = container.insertLine();
		position[id] = pos;
		verticesMapID.insert(std::pair<unsigned int, unsigned int>(ind,id));
		//		std::cout << "P: "<< ind << "   "<<x<<", "<<y<<", "<<z << std::endl;
		std::getline (fp, ligne);
		tag = ligne.substr(0,4);
		m_nbVertices++;
	} while (tag =="GRID");

	m_nbVolumes = 0;

	do
	{
		std::string s_v = ligne.substr(0,6);

		if (s_v == "CHEXA ")
		{
			m_nbFaces.push_back(8);
			m_nbVolumes++;

			s_v = ligne.substr(24,8);
			unsigned int ind1 = atoi(s_v.c_str());
			s_v = ligne.substr(32,8);
			unsigned int ind2 = atoi(s_v.c_str());
			s_v = ligne.substr(40,8);
			unsigned int ind3 = atoi(s_v.c_str());
			s_v = ligne.substr(48,8);
			unsigned int ind4 = atoi(s_v.c_str());
			s_v = ligne.substr(56,8);
			unsigned int ind5 = atoi(s_v.c_str());
			s_v = ligne.substr(64,8);
			unsigned int ind6 = atoi(s_v.c_str());

			std::getline (fp, ligne);
			s_v = ligne.substr(8,8);
			unsigned int ind7 = atoi(s_v.c_str());
			s_v = ligne.substr(16,8);
			unsigned int ind8 = atoi(s_v.c_str());

			typename PFP::VEC3 P = position[verticesMapID[ind5]];
			typename PFP::VEC3 A = position[verticesMapID[ind1]];
			typename PFP::VEC3 B = position[verticesMapID[ind2]];
			typename PFP::VEC3 C = position[verticesMapID[ind3]];

			unsigned int v[8];

			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
			{
				v[0] = ind4;
				v[1] = ind3;
				v[2] = ind2;
				v[3] = ind1;
				v[4] = ind8;
				v[5] = ind7;
				v[6] = ind6;
				v[7] = ind5;
			}
			else
			{
				v[0] = ind1;
				v[1] = ind2;
				v[2] = ind3;
				v[3] = ind4;
				v[4] = ind5;
				v[5] = ind6;
				v[6] = ind7;
				v[7] = ind8;
			}

			m_emb.push_back(verticesMapID[v[0]]);
			m_emb.push_back(verticesMapID[v[1]]);
			m_emb.push_back(verticesMapID[v[2]]);
			m_emb.push_back(verticesMapID[v[3]]);
			m_emb.push_back(verticesMapID[v[4]]);
			m_emb.push_back(verticesMapID[v[5]]);
			m_emb.push_back(verticesMapID[v[6]]);
			m_emb.push_back(verticesMapID[v[7]]);
		}
		if (s_v == "CTETRA")
		{
			m_nbFaces.push_back(4);
			m_nbVolumes++;

			s_v = ligne.substr(24,8);
			unsigned int ind1 = atoi(s_v.c_str());
			s_v = ligne.substr(32,8);
			unsigned int ind2 = atoi(s_v.c_str());
			s_v = ligne.substr(40,8);
			unsigned int ind3 = atoi(s_v.c_str());
			s_v = ligne.substr(48,8);
			unsigned int ind4 = atoi(s_v.c_str());

			typename PFP::VEC3 P = position[verticesMapID[ind1]];
			typename PFP::VEC3 A = position[verticesMapID[ind2]];
			typename PFP::VEC3 B = position[verticesMapID[ind3]];
			typename PFP::VEC3 C = position[verticesMapID[ind4]];

			Geom::Vec4ui v;

			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
			{
				v[0] = ind4;
				v[1] = ind3;
				v[2] = ind2;
				v[3] = ind1;
			}
			else
			{
				v[0] = ind1;
				v[1] = ind2;
				v[2] = ind3;
				v[3] = ind4;
			}

			m_emb.push_back(verticesMapID[v[0]]);
			m_emb.push_back(verticesMapID[v[1]]);
			m_emb.push_back(verticesMapID[v[2]]);
			m_emb.push_back(verticesMapID[v[3]]);
		}
		std::getline (fp, ligne);
		tag = ligne.substr(0,4);
	} while (!fp.eof());

	fp.close();
1082

1083
	return true;
1084
1085
}

1086
1087
template <typename PFP>
bool MeshTablesVolume<PFP>::importMSH(const std::string& filename, std::vector<std::string>& attrNames)
1088
{
1089
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1090
1091

	if (!position.isValid())
1092
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
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

	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 ligne;
	unsigned int nbv=0;
	//read $NODE
	std::getline (fp, ligne);

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

	//reading vertices
	//	std::vector<unsigned int> verticesID;
	std::map<unsigned int, unsigned int> verticesMapID;


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

		std::stringstream oss(ligne);
		unsigned int pipo;
		float x,y,z;
		oss >> pipo;
		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;

		verticesMapID.insert(std::pair<unsigned int, unsigned int>(pipo,id));
		//		verticesID.push_back(id);
	}

	// ENNODE
	std::getline (fp, ligne);

	m_nbVertices = nbv;

	// ELM
	std::getline (fp, ligne);

	// reading number of elements
	std::getline (fp, ligne);
	unsigned int nbe=0;
	std::stringstream oss2(ligne);
	oss2 >> nbe;

	m_nbVolumes = nbe;

	bool invertVol = false;

	for(unsigned int i=0; i<nbe; ++i)
	{
		unsigned int pipo,type_elm,nb;
		fp >> pipo;
		fp >> type_elm;
		fp >> pipo;
		fp >> pipo;
		fp >> nb;

		if ((type_elm==4) && (nb==4))
		{
			Geom::Vec4ui v;

			m_nbFaces.push_back(4);


			// test orientation of first tetra
			if (i==0)
			{
				fp >> v[0];
				fp >> v[1];
				fp >> v[2];
				fp >> v[3];

				typename PFP::VEC3 P = position[verticesMapID[v[0]]];
				typename PFP::VEC3 A = position[verticesMapID[v[1]]];
				typename PFP::VEC3 B = position[verticesMapID[v[2]]];
				typename PFP::VEC3 C = position[verticesMapID[v[3]]];

				if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
				{
					invertVol=true;
					unsigned int ui=v[0];
					v[0] = v[3];
					v[3] = v[2];
					v[2] = v[1];
					v[1] = ui;
				}
			}
			else
			{
				if (invertVol)
				{
					fp >> v[1];
					fp >> v[2];
					fp >> v[3];
					fp >> v[0];
				}
				else
				{
					fp >> v[0];
					fp >> v[1];
					fp >> v[2];
					fp >> v[3];
				}
			}
			m_emb.push_back(verticesMapID[v[0]]);
			m_emb.push_back(verticesMapID[v[1]]);
			m_emb.push_back(verticesMapID[v[2]]);
			m_emb.push_back(verticesMapID[v[3]]);
		}
		else
		{
			if ((type_elm==5) && (nb==8))
			{
				Geom::Vec4ui v;

				m_nbFaces.push_back(8);

				if (i==0)
				{
					unsigned int last;
					fp >> v[0];
					fp >> v[1];
					fp >> v[2];
					fp >> v[3];
					fp >> last;

					typename PFP::VEC3 P = position[verticesMapID[last]];
					typename PFP::VEC3 A = position[verticesMapID[v[0]]];
					typename PFP::VEC3 B = position[verticesMapID[v[1]]];
					typename PFP::VEC3 C = position[verticesMapID[v[2]]];

					if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
					{

						invertVol=true;
						unsigned int ui = v[3];
						v[3] = v[0];
						v[0] = ui;
						ui = v[2];
						v[2] = v[1];
						v[1] = ui;
						// hexa.push_back(v);
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
						v[3] = last;
						fp >> v[2];
						fp >> v[1];
						fp >> v[0];
						//hexa.push_back(v);
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
					}
					else
					{
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
						//hexa.push_back(v);
						v[0] = last;
						fp >> v[1];
						fp >> v[2];
						fp >> v[3];
						//hexa.push_back(v);
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
					}
				}
				else
				{
					if (invertVol)
					{
						fp >> v[3];
						fp >> v[2];
						fp >> v[1];
						fp >> v[0];
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
						//hexa.push_back(v);
						fp >> v[3];
						fp >> v[2];
						fp >> v[1];
						fp >> v[0];
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
						//hexa.push_back(v);

					}
					else
					{
						fp >> v[0];
						fp >> v[1];
						fp >> v[2];
						fp >> v[3];
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
						//hexa.push_back(v);
						fp >> v[0];
						fp >> v[1];
						fp >> v[2];
						fp >> v[3];
						m_emb.push_back(verticesMapID[v[0]]);
						m_emb.push_back(verticesMapID[v[1]]);
						m_emb.push_back(verticesMapID[v[2]]);
						m_emb.push_back(verticesMapID[v[3]]);
						//hexa.push_back(v);
					}
				}
			}
			else
			{
				for (unsigned int j=0; j<nb; ++j)
				{
					unsigned int v;
					fp >> v;
				}
			}
		}
	}

	fp.close();

	return true;
1352
}
1353

1354
template <typename PFP>
1355
bool MeshTablesVolume<PFP>::importVBGZ(const std::string& filename, std::vector<std::string>& attrNames)
1356
{
1357
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1358
1359

	if (!position.isValid())
1360
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470

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


	unsigned int numbers[3];

	// read nb of points
	fs.read(reinterpret_cast<char*>(numbers), 3*sizeof(unsigned int));

	VEC3* bufposi;
	bufposi = new VEC3[ numbers[0] ];
	fs.read(reinterpret_cast<char*>(bufposi), numbers[0]*sizeof(VEC3));

	std::vector<unsigned int> verticesID;
	verticesID.reserve(numbers[0]);

	for(unsigned int i = 0; i < numbers[0];++i)
	{
		unsigned int id = container.insertLine();
		position[id] = bufposi[i];
		verticesID.push_back(id);
	}
	delete bufposi;

	unsigned int* bufTetra=NULL;
	if (numbers[1] != 0)
	{
		bufTetra = new unsigned int[ 4*numbers[1] ];
		fs.read(reinterpret_cast<char*>(bufTetra), 4*numbers[1]*sizeof(unsigned int));
	}

	unsigned int* bufHexa=NULL;
	if (numbers[2] != 0)
	{
		bufHexa = new unsigned int[ 8*numbers[2] ];
		fs.read(reinterpret_cast<char*>(bufHexa), 8*numbers[2]*sizeof(unsigned int));
	}
	CGoGNout << "nb vertices = " << numbers[0];

	m_nbVertices = numbers[0];
	m_nbVolumes = numbers[1] + numbers[2];

	if (numbers[1] > 0)
	{
		//Read and embed all tetrahedrons
		for(unsigned int i = 0; i < numbers[1] ; ++i)
		{
			Geom::Vec4ui pt;

			m_nbFaces.push_back(4);

			pt[0] = bufTetra[4*i];
			pt[1] = bufTetra[4*i+1];
			pt[2] = bufTetra[4*i+2];
			pt[3] = bufTetra[4*i+3];

			m_emb.push_back(verticesID[pt[0]]);
			m_emb.push_back(verticesID[pt[1]]);
			m_emb.push_back(verticesID[pt[2]]);
			m_emb.push_back(verticesID[pt[3]]);

			//end of tetra
		}
		CGoGNout << " / nb tetra = " << numbers[1];
		delete[] bufTetra;
	}

	if (numbers[2] > 0)
	{
		//Read and embed all tetrahedrons
		for(unsigned int i = 0; i < numbers[2] ; ++i)
		{
			m_nbFaces.push_back(8);
			// one hexa
			unsigned int pt[8];
			pt[0] = bufHexa[8*i];
			pt[1] = bufHexa[8*i+1];
			pt[2] = bufHexa[8*i+2];
			pt[3] = bufHexa[8*i+3];
			pt[4] = bufHexa[8*i+4];
			pt[5] = bufHexa[8*i+5];
			pt[6] = bufHexa[8*i+6];
			pt[7] = bufHexa[8*i+7];

			m_emb.push_back(verticesID[pt[0]]);
			m_emb.push_back(verticesID[pt[1]]);
			m_emb.push_back(verticesID[pt[2]]);
			m_emb.push_back(verticesID[pt[3]]);
			m_emb.push_back(verticesID[pt[4]]);
			m_emb.push_back(verticesID[pt[5]]);
			m_emb.push_back(verticesID[pt[6]]);
			m_emb.push_back(verticesID[pt[7]]);
			//end of hexa
		}
		CGoGNout << " / nb hexa = " << numbers[2];
		delete[] bufHexa;
	}

	return true;
1471
1472
}

Sylvain Thery's avatar
Sylvain Thery committed
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
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
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
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
//template <typename PFP>
//bool MeshTablesVolume<PFP>::importVTU(const std::string& filename, std::vector<std::string>& attrNames)
//{
//	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;

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

//	attrNames.push_back(position.name()) ;

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

//	xmlDocPtr doc = xmlReadFile(filename.c_str(), NULL, 0);
//	xmlNodePtr vtu_node = xmlDocGetRootElement(doc);

//	//	std::cout << " NAME "<<vtu_node->name << std::endl;

//	xmlChar *prop = xmlGetProp(vtu_node, BAD_CAST "type");
//	//	std::cout << "type = "<< prop << std::endl;

//	xmlNode* grid_node = vtu_node->children;
//	while (strcmp((char*)(grid_node->name),(char*)"UnstructuredGrid")!=0)
//		grid_node = grid_node->next;

//	xmlNode* piece_node = grid_node->children;
//	while (strcmp((char*)(piece_node->name),(char*)"Piece")!=0)
//		piece_node = piece_node->next;

//	prop = xmlGetProp(piece_node, BAD_CAST "NumberOfPoints");
//	m_nbVertices = atoi((char*)(prop));

//	prop = xmlGetProp(piece_node, BAD_CAST "NumberOfCells");
//	m_nbVolumes = atoi((char*)(prop));

//	std::cout << "Number of points = "<< m_nbVertices<< std::endl;
//	std::cout << "Number of cells = "<< m_nbVolumes << std::endl;

//	xmlNode* points_node = piece_node->children;
//	while (strcmp((char*)(points_node->name),(char*)"Points")!=0)
//		points_node = points_node->next;

//	points_node = points_node->children;
//	while (strcmp((char*)(points_node->name),(char*)"DataArray")!=0)
//		points_node = points_node->next;

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

//	std::stringstream ss((char*)(xmlNodeGetContent(points_node->children)));
//	for (unsigned int i=0; i< m_nbVertices; ++i)
//	{
//		typename PFP::VEC3 P;
//		ss >> P[0]; ss >> P[1]; ss >> P[2];
//		unsigned int id = container.insertLine();
//		position[id] = P;
//		verticesID.push_back(id);
//	}

//	xmlNode* cell_node = piece_node->children;
//	while (strcmp((char*)(cell_node->name),(char*)"Cells")!=0)
//		cell_node = cell_node->next;

//	std::cout <<"CELL NODE = "<< cell_node->name << std::endl;


//	std::vector<unsigned char> typeVols;
//	typeVols.reserve(m_nbVolumes);
//	std::vector<unsigned int> offsets;
//	offsets.reserve(m_nbVolumes);
//	std::vector<unsigned int> indices;
//	indices.reserve(m_nbVolumes*4);

//	for (xmlNode* x_node = cell_node->children; x_node!=NULL; x_node = x_node->next)
//	{
//		while ((x_node!=NULL) && (strcmp((char*)(x_node->name),(char*)"DataArray")!=0))
//			x_node = x_node->next;

//		if (x_node == NULL)
//			break;
//		else
//		{
//			xmlChar* type = xmlGetProp(x_node, BAD_CAST "Name");

//			if (strcmp((char*)(type),(char*)"connectivity")==0)
//			{
//				std::stringstream ss((char*)(xmlNodeGetContent(x_node->children)));
//				while (!ss.eof())
//				{
//					unsigned int ind;
//					ss >> ind;
//					indices.push_back(ind);
//				}
//			}
//			if (strcmp((char*)(type),(char*)"offsets")==0)
//			{
//				std::stringstream ss((char*)(xmlNodeGetContent(x_node->children)));
//				for (unsigned int i=0; i< m_nbVolumes; ++i)
//				{
//					unsigned int o;
//					ss >> o;
//					offsets.push_back(o);
//				}
//			}
//			if (strcmp((char*)(type),(char*)"types")==0)
//			{
//				bool unsupported = false;
//				std::stringstream ss((char*)(xmlNodeGetContent(x_node->children)));
//				for (unsigned int i=0; i< m_nbVolumes; ++i)
//				{
//					unsigned int t;
//					ss >> t;
//					if ((t != 12) && (t!= 10))
//					{
//						unsupported = true;
//						typeVols.push_back(0);
//					}
//					else
//					{
//						typeVols.push_back((unsigned char)t);
//					}
//				}
//				if (unsupported)
//					CGoGNerr << "warning, some unsupported volume cell types"<< CGoGNendl;
//			}
//		}
//	}

//	xmlFreeDoc(doc);

//	unsigned int currentOffset = 0;
//	for (unsigned int i=0; i< m_nbVolumes; ++i)
//	{
//		if (typeVols[i]==12)
//		{
//			m_nbFaces.push_back(8);

//			unsigned int pt[8];
//			pt[0] = indices[currentOffset];
//			pt[1] = indices[currentOffset+1];
//			pt[2] = indices[currentOffset+2];
//			pt[3] = indices[currentOffset+3];
//			pt[4] = indices[currentOffset+4];
//			typename PFP::VEC3 P = position[verticesID[indices[currentOffset+4]]];
//			typename PFP::VEC3 A = position[verticesID[indices[currentOffset  ]]];
//			typename PFP::VEC3 B = position[verticesID[indices[currentOffset+1]]];
//			typename PFP::VEC3 C = position[verticesID[indices[currentOffset+2]]];

//			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
//			{

//				pt[0] = indices[currentOffset+3];
//				pt[1] = indices[currentOffset+2];
//				pt[2] = indices[currentOffset+1];
//				pt[3] = indices[currentOffset+0];
//				pt[4] = indices[currentOffset+7];
//				pt[5] = indices[currentOffset+6];
//				pt[6] = indices[currentOffset+5];
//				pt[7] = indices[currentOffset+4];
//			}
//			else
//			{
//				pt[0] = indices[currentOffset+0];
//				pt[1] = indices[currentOffset+1];
//				pt[2] = indices[currentOffset+2];
//				pt[3] = indices[currentOffset+3];
//				pt[4] = indices[currentOffset+4];
//				pt[5] = indices[currentOffset+5];
//				pt[6] = indices[currentOffset+6];
//				pt[7] = indices[currentOffset+7];
//			}

//			m_emb.push_back(verticesID[pt[0]]);
//			m_emb.push_back(verticesID[pt[1]]);
//			m_emb.push_back(verticesID[pt[2]]);
//			m_emb.push_back(verticesID[pt[3]]);
//			m_emb.push_back(verticesID[pt[4]]);
//			m_emb.push_back(verticesID[pt[5]]);
//			m_emb.push_back(verticesID[pt[6]]);
//			m_emb.push_back(verticesID[pt[7]]);

//		}
//		else if (typeVols[i]==10)
//		{
//			m_nbFaces.push_back(4);

//			Geom::Vec4ui pt;
//			pt[0] = indices[currentOffset];
//			pt[1] = indices[currentOffset+1];
//			pt[2] = indices[currentOffset+2];
//			pt[3] = indices[currentOffset+3];

//			typename PFP::VEC3 P = position[verticesID[pt[0]]];
//			typename PFP::VEC3 A = position[verticesID[pt[1]]];
//			typename PFP::VEC3 B = position[verticesID[pt[2]]];
//			typename PFP::VEC3 C = position[verticesID[pt[3]]];

//			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
//			{
//				unsigned int ui=pt[1];
//				pt[1] = pt[2];
//				pt[2] = ui;
//			}

//			m_emb.push_back(verticesID[pt[0]]);
//			m_emb.push_back(verticesID[pt[1]]);
//			m_emb.push_back(verticesID[pt[2]]);
//			m_emb.push_back(verticesID[pt[3]]);
//		}
//		currentOffset = offsets[i];
//	}

//	return true;
//}


//inline bool XMLisError(tinyxml2::XMLError err, const std::string& msg)
//{
//	if (err != tinyxml2::XML_NO_ERROR)
//	{
//		CGoGNerr << msg << CGoGNendl;
//		return true;
//	}
//	return false;
//}

//inline std::string XMLAttribute(tinyxml2::XMLElement* node, const char* attName)
//{
//	const char *ptr = node->Attribute(attName);
//	if (ptr == NULL)
//	{
//		CGoGNerr << "Warning attrbute "<< attName << " not found"<< CGoGNendl;
//		return "";
//	}
//	return std::string(ptr);
//}



1712
template <typename PFP>
1713
bool MeshTablesVolume<PFP>::importVTU(const std::string& filename, std::vector<std::string>& attrNames)
1714
{
Sylvain Thery's avatar
Sylvain Thery committed
1715

1716
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1717
1718

	if (!position.isValid())
1719
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
1720
1721
1722
1723
1724
1725
1726

	attrNames.push_back(position.name()) ;

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


Sylvain Thery's avatar
Sylvain Thery committed
1727
1728
	tinyxml2::XMLDocument doc;
	tinyxml2::XMLError eResult = doc.LoadFile(filename.c_str());
1729

Sylvain Thery's avatar
Sylvain Thery committed
1730
1731
	if (XMLisError(eResult,"unable loading file"+filename))
		return false;
1732

Sylvain Thery's avatar
Sylvain Thery committed
1733
	tinyxml2::XMLElement* vtu_node = doc.RootElement();
1734

Sylvain Thery's avatar
Sylvain Thery committed
1735
1736
	tinyxml2::XMLElement* grid_node = vtu_node->FirstChildElement("UnstructuredGrid");
	tinyxml2::XMLElement* piece_node = grid_node->FirstChildElement("Piece");
1737

Sylvain Thery's avatar
Sylvain Thery committed
1738
1739
1740
1741
1742
1743
	eResult = piece_node->QueryUnsignedAttribute("NumberOfPoints",&m_nbVertices);
	if (XMLisError(eResult,"unreadable VTU file: "+filename))
		return false;
	eResult = piece_node->QueryUnsignedAttribute("NumberOfCells",&m_nbVolumes);
	if (XMLisError(eResult,"unreadable VTU file: "+filename))
		return false;
1744

Sylvain Thery's avatar
Sylvain Thery committed
1745
1746
	CGoGNout << "Number of points = "<< m_nbVertices<< CGoGNendl;
	CGoGNout << "Number of cells = "<< m_nbVolumes << CGoGNendl;
1747

Sylvain Thery's avatar
Sylvain Thery committed
1748
1749
	tinyxml2::XMLElement* points_node = piece_node->FirstChildElement("Points");
	tinyxml2::XMLElement* array_node = points_node->FirstChildElement("DataArray");
1750
1751
1752
1753

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