import2tablesVolume.hpp 44.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 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
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
197
	{
198 199 200 201
		do
		{
			std::getline (fp, ligne);
		} while (ligne.size()==0);
Pierre Kraemer's avatar
Pierre Kraemer committed
202 203 204

		std::stringstream oss(ligne);
		int n;
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 239 240 241 242 243 244 245 246 247 248
		oss >> n; // type of volumes

		//tetrahedron
		if(n == 4)
		{
			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 = 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;

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			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];
			}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
			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;

291 292 293 294 295
			// 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]];
296

297 298 299
//			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::OVER)
//			{
//				int pt[6];
300

301 302 303 304 305 306
//				pt[0] = s0;
//				pt[1] = s1;
//				pt[2] = s2;
//				pt[3] = s3;
//				pt[4] = s4;
//				pt[5] = s5;
307

308 309 310 311 312 313 314 315
//				s0 = pt[0];
//				s1 = pt[1];
//				s2 = pt[2];
//				s3 = pt[3];
//				s4 = pt[4];
//				s5 = pt[5];

//			}
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

			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;

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

345 346
			// 1 hexa ok avec cette partie
			if (Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
347
			{
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
				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];
366 367 368 369 370 371 372 373 374 375 376 377 378
			}

			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
379 380 381 382 383

	fp.close();
	return true;
}

384 385 386
template <typename PFP>
bool MeshTablesVolume<PFP>::importOFFWithELERegions(const std::string& filenameOFF, const std::string& filenameELE, std::vector<std::string>& attrNames)
{
387
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
388 389

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

	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;
523 524 525 526 527
}

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

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

	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;
658 659 660 661 662
}

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

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

668
	attrNames.push_back(position.name()) ;
669

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

672 673 674 675 676 677 678
	//open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}
679

680
	std::string line;
681

682
	fp >> line;
683

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

687
	fp >> m_nbVertices;
688

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

691
	std::getline (fp, line);
692

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

698 699 700 701 702 703
	for(unsigned int i = 0; i < m_nbVertices; ++i)
	{
		do
		{
			std::getline (fp, line);
		} while (line.size() == 0);
704

705
		std::stringstream oss(line);
706

707 708 709 710 711 712
		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
		// TODO : if required read other vertices attributes here
		VEC3 pos(x,y,z);
713

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

717 718
		verticesID.push_back(id);
	}
719

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

724 725
	fp >> m_nbVolumes;
	std::getline (fp, line);
726

727 728 729
	// reading tetrahedra
	m_nbFaces.reserve(m_nbVolumes*4);
	m_emb.reserve(m_nbVolumes*12);
730

731 732 733 734 735 736
	for(unsigned i = 0; i < m_nbVolumes ; ++i)
	{
		do
		{
			std::getline(fp,line);
		} while(line.size() == 0);
737

738
		std::stringstream oss(line);
739

740
		m_nbFaces.push_back(4);
741

742
		int s0,s1,s2,s3;
743

744 745 746 747
		oss >> s0;
		oss >> s1;
		oss >> s2;
		oss >> s3;
748

749 750 751 752
		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]];
753

754 755 756 757 758 759 760 761 762 763 764 765
		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]);
	}
766

767 768
	fp.close();
	return true;
769 770 771 772 773
}

template <typename PFP>
bool MeshTablesVolume<PFP>::importTs(const std::string& filename, std::vector<std::string>& attrNames)
{
774 775 776 777 778 779 780 781
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}

782
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
783 784

	if (!position.isValid())
785
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
786 787 788

	attrNames.push_back(position.name()) ;

789
	VertexAttribute<REAL, MAP> scalar = m_map.template getAttribute<REAL, VERTEX, MAP>("scalar");
790 791

	if (!scalar.isValid())
792
		scalar = m_map.template addAttribute<REAL, VERTEX, MAP>("scalar") ;
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

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

810 811 812 813 814 815
	for(unsigned int i = 0; i < m_nbVertices; ++i)
	{
		do
		{
			std::getline (fp, ligne);
		} while (ligne.size() == 0);
816

817
		std::stringstream oss(ligne);
818

819 820 821 822
		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
823

824
		VEC3 pos(x,y,z);
825

826
		unsigned int id = container.insertLine();
827

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

831 832 833 834
		float scal;
		oss >> scal;
		scalar[id] = scal;
	}
835

836 837 838 839 840 841 842
	//Read and embed all tetrahedrons
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
		do
		{
			std::getline(fp,ligne);
		} while(ligne.size() == 0);
843

844
		std::stringstream oss(ligne);
845

846
		m_nbFaces.push_back(4);
847

848
		int s0,s1,s2,s3,nbe;
849

850 851 852 853
		oss >> s0;
		oss >> s1;
		oss >> s2;
		oss >> s3;
854

855 856 857 858
		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]];
859

860 861 862 863 864 865
		if(Geom::testOrientation3D<typename PFP::VEC3>(P,A,B,C) == Geom::UNDER)
		{
			int ui = s1;
			s1 = s2;
			s2 = ui;
		}
866 867


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

871 872 873 874 875
		m_emb.push_back(verticesID[s0]);
		m_emb.push_back(verticesID[s1]);
		m_emb.push_back(verticesID[s2]);
		m_emb.push_back(verticesID[s3]);
	}
876

877 878 879
	fp.close();
	return true;
}
880

881 882 883 884 885 886 887 888 889 890
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;
	}
891

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

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

897
	attrNames.push_back(position.name()) ;
898

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

901 902
	std::string ligne;
	std::string tag;
903

904 905 906 907 908 909
	std::getline (fp, ligne);
	do
	{
		std::getline (fp, ligne);
		tag = ligne.substr(0,4);
	} while (tag !="GRID");
910

911 912 913 914 915 916 917
	 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());
918

919 920 921 922 923 924
		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);
925

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 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
		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();
1051

1052
	return true;
1053 1054
}

1055 1056
template <typename PFP>
bool MeshTablesVolume<PFP>::importMSH(const std::string& filename, std::vector<std::string>& attrNames)
1057
{
1058
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1059 1060

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

	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;
1321
}
1322

1323
template <typename PFP>
1324
bool MeshTablesVolume<PFP>::importVBGZ(const std::string& filename, std::vector<std::string>& attrNames)
1325
{
1326
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1327 1328

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

	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;
1440 1441
}

Sylvain Thery's avatar
Sylvain Thery committed
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 1471 1472 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
//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);
//}



1681
template <typename PFP>
1682
bool MeshTablesVolume<PFP>::importVTU(const std::string& filename, std::vector<std::string>& attrNames)
1683
{
Sylvain Thery's avatar
Sylvain Thery committed
1684

1685
	VertexAttribute<VEC3, MAP> position =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
1686 1687

	if (!position.isValid())
1688
		position = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
1689 1690 1691 1692 1693 1694 1695

	attrNames.push_back(position.name()) ;

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


Sylvain Thery's avatar
Sylvain Thery committed
1696 1697
	tinyxml2::XMLDocument doc;
	tinyxml2::XMLError eResult = doc.LoadFile(filename.c_str());
1698

Sylvain Thery's avatar
Sylvain Thery committed
1699 1700
	if (XMLisError(eResult,"unable loading file"+filename))
		return false;
1701

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

Sylvain Thery's avatar
Sylvain Thery committed
1704 1705
	tinyxml2::XMLElement* grid_node = vtu_node->FirstChildElement("UnstructuredGrid");
	tinyxml2::XMLElement* piece_node = grid_node->FirstChildElement("Piece");
1706

Sylvain Thery's avatar
Sylvain Thery committed
1707 1708 1709 1710 1711 1712
	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;
1713

Sylvain Thery's avatar
Sylvain Thery committed
1714 1715
	CGoGNout << "Number of points = "<< m_nbVertices<< CGoGNendl;
	CGoGNout << "Number of cells = "<< m_nbVolumes << CGoGNendl;
1716

Sylvain Thery's avatar
Sylvain Thery committed
1717 1718
	tinyxml2::XMLElement* points_node = piece_node->FirstChildElement("Points");
	tinyxml2::XMLElement* array_node = points_node->FirstChildElement("DataArray");
1719 1720 1721 1722

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

Sylvain Thery's avatar
Sylvain Thery committed
1723 1724
	std::stringstream ss(array_node->GetText());

1725 1726 1727 1728 1729 1730 1731 1732 1733
	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);
	}

Sylvain Thery's avatar
Sylvain Thery committed
1734 1735
	tinyxml2::XMLElement* cell_node = piece_node->FirstChildElement("Cells");
	array_node = cell_node->FirstChildElement("DataArray");
1736 1737 1738 1739 1740 1741 1742 1743

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

Sylvain Thery's avatar
Sylvain Thery committed
1744
	while (array_node)
1745
	{
Sylvain Thery's avatar
Sylvain Thery committed
1746
		std::string propName = XMLAttribute(array_node,"Name");
1747

Sylvain Thery's avatar
Sylvain Thery committed
1748
		if (propName == "")
1749
		{
Sylvain Thery's avatar
Sylvain Thery committed
1750 1751 1752
			CGoGNerr << "Error reading VTU unreadable file: "<<filename<< CGoGNendl;
			return false;
		}
1753

Sylvain Thery's avatar
Sylvain Thery committed
1754 1755 1756 1757
		if (propName == "connectivity")
		{
			std::stringstream ss(array_node->GetText());
			while (!ss.eof())
1758
			{
Sylvain Thery's avatar
Sylvain Thery committed
1759 1760 1761
				unsigned int ind;
				ss >> ind;
				indices.push_back(ind);
1762
			}
Sylvain Thery's avatar
Sylvain Thery committed
1763 1764 1765 1766 1767
		}
		if (propName == "offsets")
		{
			std::stringstream ss(array_node->GetText());
			for (unsigned int i=0; i< m_nbVolumes; ++i)
1768
			{
Sylvain Thery's avatar
Sylvain Thery committed
1769 1770 1771
				unsigned int o;
				ss >> o;
				offsets.push_back(o);
1772
			}
Sylvain Thery's avatar
Sylvain Thery committed
1773 1774 1775 1776 1777 1778
		}
		if (propName == "types")
		{
			bool unsupported = false;
			std::stringstream ss(array_node->GetText());
			for (unsigned int i=0; i< m_nbVolumes; ++i)
1779
			{
Sylvain Thery's avatar
Sylvain Thery committed
1780 1781 1782
				unsigned int t;
				ss >> t;
				if ((t != 12) && (t!= 10))
1783
				{
Sylvain Thery's avatar
Sylvain Thery committed
1784 1785 1786 1787 1788 1789
					unsupported = true;
					typeVols.push_back(0);
				}
				else
				{
					typeVols.push_back((unsigned char)t);
1790 1791
				}
			}
Sylvain Thery's avatar
Sylvain Thery committed
1792 1793
			if (unsupported)
				CGoGNerr << "warning, some unsupported volume cell types"<< CGoGNendl;
1794
		}
Sylvain Thery's avatar
Sylvain Thery committed
1795
		array_node = array_node->NextSiblingElement("DataArray");
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869