exportVol.hpp 25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/autoAttributeHandler.h"
27 28
#include "Topology/generic/traversor/traversorCell.h"
#include "Topology/generic/traversor/traversor2.h"
29
#include "Topology/generic/cellmarker.h"
30
#include "Algo/Import/importFileTypes.h"
31 32 33 34 35 36 37 38 39 40 41 42 43

namespace CGoGN
{

namespace Algo
{

namespace Volume
{

namespace Export
{

Sylvain Thery's avatar
Sylvain Thery committed
44
inline std::string truncFloatTO8(float f)
45 46 47 48
{
	std::stringstream ss;
	ss << f;
	std::string res = ss.str();
Sylvain Thery's avatar
Sylvain Thery committed
49 50 51 52 53 54 55 56 57

	size_t expo = res.find('e');
	if (expo != std::string::npos)
	{
		if ( res[expo+2] == '0')
			return res.substr(0,6) + res[expo+1] + res[expo+3];

		return res.substr(0,5) + res.substr(expo+1);
	}
58 59 60
	return res.substr(0,8);
}

61
template <typename PFP>
62
bool exportNAS(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	// open file
	std::ofstream fout ;
	fout.open(filename, std::ios::out) ;

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

77
	VertexAutoAttribute<unsigned int, MAP> indices(map,"indices_vert");
78 79 80 81

	fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
	fout << "$$      NASTRAN MEsh File Generated by CGoGN (ICube/IGG)                        $"<< std::endl;
	fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
82 83
	fout << "CEND" << std::endl;;
	fout << "BEGIN BULK" << std::endl;
84 85 86
	fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
	fout << "$$      Vertices position                                                       $"<< std::endl;
	fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
87 88 89 90 91 92
	unsigned int count=1;
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
		fout << "GRID    ";
		fout << std::right;
93
		fout.width(8);
94 95 96
		fout << count;
		fout << "        ";
		fout << std::left;
97
		fout << std::setw(8)<<truncFloatTO8(P[0]) << std::setw(8)<<truncFloatTO8(P[1]) << std::setw(8)<<truncFloatTO8(P[2]) << std::endl;
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
		indices[i] = count++;
	}

	std::vector<unsigned int> hexa;
	std::vector<unsigned int> tetra;
	hexa.reserve(2048);
	tetra.reserve(2048);

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
		unsigned int degree = 0 ;

		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 8)
		{
			//CAS HEXAEDRIQUE (ordre 2 quad superposes, le premier en CCW)
			Dart e = d;
			Dart f = map.template phi<21121>(d);
			hexa.push_back(indices[f]);
Sylvain Thery's avatar
Sylvain Thery committed
123
			f = map.phi_1(f);
124
			hexa.push_back(indices[f]);
Sylvain Thery's avatar
Sylvain Thery committed
125
			f = map.phi_1(f);
126
			hexa.push_back(indices[f]);
Sylvain Thery's avatar
Sylvain Thery committed
127
			f = map.phi_1(f);
128
			hexa.push_back(indices[f]);
Sylvain Thery's avatar
Sylvain Thery committed
129 130 131 132 133 134 135
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
136 137 138 139
		}

		if (degree == 4)
		{
Sylvain Thery's avatar
Sylvain Thery committed
140
			//CAS TETRAEDRIQUE
141 142 143 144 145 146 147 148 149 150 151 152 153
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	unsigned int countCell=1;
	unsigned int nbhexa = hexa.size()/8;
154 155 156 157
	unsigned int nbtetra = tetra.size()/4;

	fout << std::right;
	if (nbhexa!=0)
158
	{
159 160 161 162 163 164 165 166
		fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
		fout << "$$      Hexa indices                                                            $"<< std::endl;
		fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
		for (unsigned int i=0; i<nbhexa; ++i)
		{
			fout << "CHEXA   ";
			fout << std::setw(8) << countCell++ << std::setw(8)<< 0;
			fout <<  std::setw(8) << hexa[8*i] <<  std::setw(8) << hexa[8*i+1] <<  std::setw(8) << hexa[8*i+2];
Sylvain Thery's avatar
Sylvain Thery committed
167
			fout <<  std::setw(8) << hexa[8*i+3] <<  std::setw(8) << hexa[8*i+4] <<  std::setw(8) << hexa[8*i+5] << "+"<< std::endl;
168 169
			fout << "+       " <<  std::setw(8) << hexa[8*i+6] <<  std::setw(8) << hexa[8*i+7] << std::endl;
		}
170 171
	}

172
	if (nbtetra != 0)
173
	{
174 175 176 177 178 179 180 181 182
		fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
		fout << "$$      Tetra indices                                                           $"<< std::endl;
		fout << "$$ ---------------------------------------------------------------------------- $"<< std::endl;
		for (unsigned int i=0; i<nbtetra; ++i)
		{
			fout << "CTETRA  ";
			fout << std::setw(8) << countCell++ << std::setw(8)<< 0;
			fout << std::setw(8) << tetra[4*i] << std::setw(8) << tetra[4*i+1] << std::setw(8) << tetra[4*i+2] << std::setw(8) << tetra[4*i+3] << std::endl;
		}
183 184 185
	}

	fout << "ENDDATA" << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
186 187

	fout.close();
188
	return true;
189 190
}

Sylvain Thery's avatar
Sylvain Thery committed
191
template <typename PFP>
192
bool exportVTU(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
Sylvain Thery's avatar
Sylvain Thery committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	// open file
	std::ofstream fout ;
	fout.open(filename, std::ios::out) ;

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

207
	VertexAutoAttribute<unsigned int, MAP> indices(map, "indices_vert");
Sylvain Thery's avatar
Sylvain Thery committed
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	unsigned int count=0;
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		indices[i] = count++;
	}

	std::vector<unsigned int> hexa;
	std::vector<unsigned int> tetra;
	hexa.reserve(2048);
	tetra.reserve(2048);

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
		unsigned int degree = 0 ;

		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 8)
		{
			//CAS HEXAEDRIQUE (ordre 2 quad superposes, le premier en CW)
			Dart e = d;
			Dart f = map.template phi<21121>(d);
			hexa.push_back(indices[f]);
			f = map.phi_1(f);
			hexa.push_back(indices[f]);
			f = map.phi_1(f);
			hexa.push_back(indices[f]);
			f = map.phi_1(f);
			hexa.push_back(indices[f]);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
		}

		if (degree == 4)
		{
			//CAS TETRAEDRIQUE
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	unsigned int nbhexa = hexa.size()/8;
	unsigned int nbtetra = tetra.size()/4;

	fout << "<?xml version=\"1.0\"?>" << std::endl;
	fout << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << std::endl;
	fout << "  <UnstructuredGrid>" <<  std::endl;
	fout << "    <Piece NumberOfPoints=\"" << position.nbElements() << "\" NumberOfCells=\""<< (nbhexa+nbtetra) << "\">" << std::endl;
	fout << "      <Points>" << std::endl;
	fout << "        <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;

	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
		fout << "          " << P[0]<< " " << P[1]<< " " << P[2] << std::endl;
	}

	fout << "        </DataArray>" << std::endl;
	fout << "      </Points>" << std::endl;
	fout << "      <Cells>" << std::endl;
	fout << "        <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">" << std::endl;

	if (nbhexa!=0)
	{
		for (unsigned int i=0; i<nbhexa; ++i)
		{
			fout << "          ";
			fout << hexa[8*i]   << " " << hexa[8*i+1] << " " << hexa[8*i+2] << " " << hexa[8*i+3] << " " << hexa[8*i+4] << " ";
			fout << hexa[8*i+5] << " " << hexa[8*i+6] << " " << hexa[8*i+7] << std::endl;
		}
	}

	if (nbtetra != 0)
	{

		for (unsigned int i=0; i<nbtetra; ++i)
		{
			fout << "          ";
			fout << tetra[4*i] << " " << tetra[4*i+1] << " " << tetra[4*i+2] << " " << tetra[4*i+3] << std::endl;
		}
	}

	fout << "        </DataArray>" << std::endl;
	fout << "        <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">" << std::endl;
	fout << "         ";
	unsigned int offset = 0;
	for (unsigned int i=0; i<nbhexa; ++i)
	{
		offset += 8;
		fout << " " << offset;
	}
	for (unsigned int i=0; i<nbtetra; ++i)
	{
		offset += 4;
		fout << " "<< offset;
	}

	fout << std::endl << "        </DataArray>" << std::endl;
	fout << "        <DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">" << std::endl;
	fout << "         ";
	for (unsigned int i=0; i<nbhexa; ++i)
	{
		fout << " 12";
	}
	for (unsigned int i=0; i<nbtetra; ++i)
	{
		fout << " 10";
	}

	fout << std::endl << "        </DataArray>" << std::endl;
	fout << "      </Cells>" << std::endl;
	fout << "    </Piece>" << std::endl;
	fout << "  </UnstructuredGrid>" << std::endl;
	fout << "</VTKFile>" << std::endl;

	fout.close();
	return true;
}

344
template <typename PFP>
345
bool exportMSH(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
346 347 348 349 350 351 352 353 354 355 356 357 358
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	// open file
	std::ofstream fout ;
	fout.open(filename, std::ios::out) ;

	if (!fout.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
359

360
	VertexAutoAttribute<unsigned int, MAP> indices(map,"indices_vert");
Sylvain Thery's avatar
Sylvain Thery committed
361 362 363

	fout << "$NOD" << std::endl;
	fout << position.nbElements()<< std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
364
	unsigned int count = 1;
Sylvain Thery's avatar
Sylvain Thery committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
		fout << count << " " << P[0]<< " " << P[1]<< " " << P[2] << std::endl;
		indices[i] = count++;
	}

	std::vector<unsigned int> hexa;
	std::vector<unsigned int> tetra;
	hexa.reserve(2048);
	tetra.reserve(2048);

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
		unsigned int degree = 0 ;

		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 8)
		{
			//CAS HEXAEDRIQUE (ordre 2 quad superposes, le premier en CW)
			Dart e = d;
			Dart f = map.template phi<21121>(d);
			hexa.push_back(indices[f]);
			f = map.phi_1(f);
			hexa.push_back(indices[f]);
			f = map.phi_1(f);
			hexa.push_back(indices[f]);
			f = map.phi_1(f);
			hexa.push_back(indices[f]);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
		}

		if (degree == 4)
		{
			//CAS TETRAEDRIQUE
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	fout << "$ENDNOD" << std::endl;
	fout << "$ELM" << std::endl;

	unsigned int countCell=1;
	unsigned int nbhexa = hexa.size()/8;
	unsigned int nbtetra = tetra.size()/4;

	fout << (nbhexa+nbtetra) << std::endl;
	if (nbhexa!=0)
	{
		for (unsigned int i=0; i<nbhexa; ++i)
		{
			fout << countCell++ << " 5 1 1 8 ";
			fout << hexa[8*i]   << " " << hexa[8*i+1] << " " << hexa[8*i+2] << " " << hexa[8*i+3] << " " << hexa[8*i+4] << " ";
			fout << hexa[8*i+5] << " " << hexa[8*i+6] << " " << hexa[8*i+7] << std::endl;
		}
	}

	if (nbtetra != 0)
	{

		for (unsigned int i=0; i<nbtetra; ++i)
		{
			fout << countCell++ << " 4 1 1 4 ";
			fout << tetra[4*i] << " " << tetra[4*i+1] << " " << tetra[4*i+2] << " " << tetra[4*i+3] << std::endl;
		}
	}

	fout << "$ENDELM" << std::endl;

	fout.close();
	return true;
455 456 457
}

template <typename PFP>
458
bool exportTet(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
459 460 461 462 463 464 465 466 467 468 469 470 471
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	// open file
	std::ofstream fout ;
	fout.open(filename, std::ios::out) ;

	if (!fout.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
472

473
	VertexAutoAttribute<unsigned int, MAP> indices(map,"indices_vert");
Sylvain Thery's avatar
Sylvain Thery committed
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

	fout << position.nbElements()<< " vertices" <<std::endl;

	std::vector<unsigned int> tetra;
	tetra.reserve(2048);


	unsigned int count=0;
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		indices[i] = count++;
	}

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
		unsigned int degree = 0 ;

		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 4)
		{
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi_1(e);
			tetra.push_back(indices[e]);
			e = map.phi_1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	unsigned int nbtetra = tetra.size()/4;
	fout << nbtetra << " tets" << std::endl;

	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
		fout << P[0]<< " " << P[1]<< " " << P[2] << std::endl;
	}

	for (unsigned int i=0; i<nbtetra; ++i)
	{
		fout << "4 " << tetra[4*i] << " " << tetra[4*i+1] << " " << tetra[4*i+2] << " " << tetra[4*i+3] << std::endl;
	}

	fout.close();
	return true;
527 528 529 530
}


template <typename PFP>
531
bool exportNodeEle(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
532 533 534 535
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

Sylvain Thery's avatar
Sylvain Thery committed
536
	std::string base(filename);
537 538
	size_t pos = base.rfind(".");
	base.erase(pos);
Sylvain Thery's avatar
Sylvain Thery committed
539 540 541 542
	std::string fnNode = base + ".node";
	std::string fnEle = base + ".ele";

	// open files
543
	std::ofstream fout ;
Sylvain Thery's avatar
Sylvain Thery committed
544 545
	fout.open(fnNode.c_str(), std::ios::out) ;

546 547 548 549 550 551 552 553

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

	std::ofstream foutEle ;
Sylvain Thery's avatar
Sylvain Thery committed
554
	foutEle.open(fnEle.c_str(), std::ios::out) ;
555

Sylvain Thery's avatar
Sylvain Thery committed
556
	if (!foutEle.good())
557 558 559 560
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
561

562
	VertexAutoAttribute<unsigned int, MAP> indices(map,"indices_vert");
Sylvain Thery's avatar
Sylvain Thery committed
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

	fout << position.nbElements()<< " 3  0  0"<<std::endl;
	unsigned int count=0;
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
		fout << count << " " << P[0]<< " " << P[1]<< " " << P[2] << std::endl;
		indices[i] = count++;
	}

	fout.close();

	std::vector<unsigned int> hexa;
	std::vector<unsigned int> tetra;
	hexa.reserve(2048);
	tetra.reserve(2048);

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
		unsigned int degree = 0 ;

		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 4)
		{
			//CAS TETRAEDRIQUE
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	unsigned int countCell=0;
	unsigned int nbtetra = tetra.size()/4;

	foutEle << nbtetra << " 4  0" << std::endl;

	if (nbtetra != 0)
	{

		for (unsigned int i=0; i<nbtetra; ++i)
		{
			foutEle << countCell++ << "  " << tetra[4*i] << " " << tetra[4*i+1] << " " << tetra[4*i+2] << " " << tetra[4*i+3] << std::endl;
		}
	}

	foutEle.close();

	return true;
622 623
}

Sylvain Thery's avatar
Sylvain Thery committed
624
template <typename PFP>
625
bool exportVolBinGz(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
Sylvain Thery's avatar
Sylvain Thery committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	// open file
//	std::ofstream fout ;
//	fout.open(filename, std::ios::out) ;

	ogzstream fout(filename, std::ios::out|std::ios::binary);

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

642
	VertexAutoAttribute<unsigned int, MAP> indices(map,"indices_vert");
Sylvain Thery's avatar
Sylvain Thery committed
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 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

	std::vector<typename PFP::VEC3> bufposi;
	bufposi.reserve(position.nbElements());

	unsigned int count=0;
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
		bufposi.push_back(P);
//		fout << count << " " << P[0]<< " " << P[1]<< " " << P[2] << std::endl;
		indices[i] = count++;
	}
	if (count != bufposi.size())
		CGoGNerr << "Warning problem wrong nbElements in position attributes ?" << CGoGNendl;

	std::vector<unsigned int> hexa;
	std::vector<unsigned int> tetra;

	hexa.reserve(2048);
	tetra.reserve(2048);

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
		unsigned int degree = 0 ;

		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 8)
		{
			//CAS HEXAEDRIQUE (ordre 2 quad superposes, le premier en CW)
			Dart e = d;
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);

			e = map.template phi<2112>(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);
			e = map.phi1(e);
			hexa.push_back(indices[e]);

		}

		if (degree == 4)
		{
			//CAS TETRAEDRIQUE
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.phi1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	unsigned int nbhexa = hexa.size()/8;
	unsigned int nbtetra = tetra.size()/4;

	unsigned int buffer[3];
	buffer[0] = position.nbElements();
	buffer[1] = nbtetra;
	buffer[2] = nbhexa;

	fout.write((char*)(buffer),3*sizeof(unsigned int));

	// write positions
	fout.write((char*)(&(bufposi[0])),sizeof(VEC3)*bufposi.size());

	// write tetra indices if necessary
	if (nbtetra != 0)
	{

		long int nbo = (long int)&(tetra.back()) -(long int)&(tetra.front());
		nbo /= sizeof(unsigned int);
		if (nbo != (long int)(tetra.size()-1))
			CGoGNerr << "Memory vector problem"<< CGoGNendl;
		else
			fout.write((char*)(&(tetra[0])),sizeof(unsigned int)*tetra.size());
	}

	// write hexa indices if necessary
	if (nbhexa!=0)
	{
		long int nbo = (long int)&(hexa.back()) -(long int)&(hexa.front());
		nbo /= sizeof(unsigned int);
		if (nbo != (long int)(hexa.size()-1))
			CGoGNerr << "Memory vector problem"<< CGoGNendl;
		else
			fout.write((char*)(&(hexa[0])),sizeof(unsigned int)*hexa.size());
	}

	fout.close();
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
752
template <typename PFP>
753
bool exportTetmesh(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
Sylvain Thery's avatar
Sylvain Thery committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	// open file
	std::ofstream fout ;
	fout.open(filename, std::ios::out) ;

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

768
	VertexAutoAttribute<unsigned int, MAP> indices(map,"indices_vert");
Sylvain Thery's avatar
Sylvain Thery committed
769

770
	fout << "Vertices" << std::endl<< position.nbElements() << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
771 772 773 774 775 776 777 778

	std::vector<unsigned int> tetra;
	tetra.reserve(2048);

	unsigned int count=1;
	for (unsigned int i = position.begin(); i != position.end(); position.next(i))
	{
		const VEC3& P = position[i];
779
		fout << P[0]<< " " << P[1]<< " " << P[2] << " " << "0" << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
		indices[i] = count++;
	}

	TraversorW<MAP> trav(map) ;
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
	{
#ifndef _OPTIMIZED_FOR_TETRA_ONLY_
		unsigned int degree = 0 ;
		Traversor3WV<typename PFP::MAP> twv(map, d) ;
		for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
		{
			degree++;
		}

		if (degree == 4)
#endif
		{
			Dart e = d;
			tetra.push_back(indices[e]);
			e = map.phi_1(e);
			tetra.push_back(indices[e]);
			e = map.phi_1(e);
			tetra.push_back(indices[e]);
			e = map.template phi<211>(e);
			tetra.push_back(indices[e]);
		}
	}

	unsigned int nbtetra = tetra.size()/4;
809
	fout << "Tetrahedra" << std::endl << nbtetra << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
810 811 812 813 814 815 816 817 818 819

	for (unsigned int i=0; i<nbtetra; ++i)
	{
		fout << tetra[4*i] << " " << tetra[4*i+1] << " " << tetra[4*i+2] << " " << tetra[4*i+3] << std::endl;
	}

	fout.close();
	return true;
}

820
template <typename PFP>
821
bool exportMesh(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const std::string& filename)
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
{
	Import::ImportType kind = Import::getFileType(filename);

// manque TS, OFF ?
	switch (kind)
	{
	case Import::TET:
		return exportTet<PFP>(map, position, filename.c_str());
		break;
	case Import::NODE:
		return exportNodeEle<PFP>(map, position, filename.c_str());
		break;
	case Import::MSH:
		return exportMSH<PFP>(map, position, filename.c_str());
		break;
	case Import::VTU:
		return exportVTU<PFP>(map, position, filename.c_str());
		break;
	case Import::NAS:
		return exportNAS<PFP>(map, position, filename.c_str());
		break;
	case Import::VBGZ:
		return exportVolBinGz<PFP>(map, position, filename.c_str());
		break;
Sylvain Thery's avatar
Sylvain Thery committed
846 847 848
	case Import::TETMESH:
		return exportTetmesh<PFP>(map, position, filename.c_str());
		break;
849 850
	default:
		CGoGNerr << "unknown file format for " << filename << CGoGNendl;
851
		return false;
852 853 854
		break;
	}
}
Sylvain Thery's avatar
Sylvain Thery committed
855

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

template <typename PFP>
bool exportBoundaryOFF(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const char* filename)
{
	typedef typename PFP::MAP MAP;
	typedef typename PFP::VEC3 VEC3;

	std::ofstream out(filename, std::ios::out) ;
	if (!out.good())
	{
		CGoGNerr << "Unable to open file " << CGoGNendl ;
		return false ;
	}

	unsigned int nbDarts = map.getNbDarts() ;
	std::vector<unsigned int> facesSize ;
	std::vector<std::vector<unsigned int> > facesIdx ;
	facesSize.reserve(nbDarts/3) ;
	facesIdx.reserve(nbDarts/3) ;
	std::map<unsigned int, unsigned int> vIndex ;
	unsigned int vCpt = 0 ;
	std::vector<unsigned int> vertices ;
	vertices.reserve(nbDarts/6) ;

	CellMarker<typename PFP::MAP,VERTEX> markV(map) ;
	TraversorF<MAP> t(map) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
	{
		if(map.isBoundaryFace(d))
		{
			Dart db = d;
			if(!map.template isBoundaryMarked<3>(db))
				db = map.phi3(db);

			std::vector<unsigned int> fidx ;
			fidx.reserve(8) ;
			unsigned int degree = 0 ;
			Traversor2FV<typename PFP::MAP> tfv(map, db) ;
			for(Dart it = tfv.begin(); it != tfv.end(); it = tfv.next())
			{
				++degree ;
				unsigned int vNum = map.template getEmbedding<VERTEX>(it) ;
				if(!markV.isMarked(it))
				{
					markV.mark(it) ;
					vIndex[vNum] = vCpt++ ;
					vertices.push_back(vNum) ;
				}
				fidx.push_back(vIndex[vNum]) ;
			}
			facesSize.push_back(degree) ;
			facesIdx.push_back(fidx) ;
		}
	}

	out << "OFF" << std::endl ;
	out << vertices.size() << " " << facesSize.size() << " " << 0 << std::endl ;

	for(unsigned int i = 0; i < vertices.size(); ++i)
	{
		const VEC3& v = position[vertices[i]] ;
		out << v[0] << " " << v[1] << " " << v[2] << std::endl ;
	}
	for(unsigned int i = 0; i < facesSize.size(); ++i)
	{
		out << facesSize[i] ;
		for(unsigned int j = 0; j < facesIdx[i].size(); ++j)
			out << " " << facesIdx[i][j] ;
		out << std::endl ;
	}

	out.close() ;
	return true ;
}

931 932
} // namespace Export

Pierre Kraemer's avatar
Pierre Kraemer committed
933
} // namespace Volume
934 935 936 937

} // namespace Algo

} // namespace CGoGN