importInESS.hpp 17.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Modelisation/polyhedron.h"
#include <vector>
27 28
#include "Geometry/bounding_box.h"
#include <set>
Pierre Kraemer's avatar
Pierre Kraemer committed
29 30 31 32 33 34 35 36 37

namespace CGoGN
{
namespace Algo
{
namespace Import 
{

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
38
typename PFP::VEC3 stringToEmb(std::string s)
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
{
	if(s[0] == '(')
		s.erase(0,2);
	if(s[s.size()-1] == ')')
		s.erase(s.size()-2,2);

	std::stringstream oss(s);

	int x,y,z;
	oss >> x;
	oss >> y;
	oss >> z;

	typename PFP::VEC3 coord(x,y,z);

54
	CGoGNout << coord << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58

	return coord;
}

59
inline unsigned int gcd(unsigned int a, unsigned int b)
60 61 62 63 64 65 66 67 68 69 70 71 72 73
{

	while ( true )
	{
		if ( a == 0 )
			return b;
		b %= a;

		if ( b == 0 )
			return a;
		a %= b;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
74
template <typename PFP>
untereiner's avatar
untereiner committed
75
bool importInESSSurfacique(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77 78
{
	typedef typename PFP::VEC3 VEC3;

Pierre Kraemer's avatar
Pierre Kraemer committed
79 80
	AttributeHandler<VEC3> position = map.template addAttribute<VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(position.name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	AttributeHandler<int> objects = map.template addAttribute<int>(FACE_ORBIT, "objects") ;
	attrNames.push_back(objects.name()) ;


	//stocker les boundingbox contenu dans le fichier
	std::vector<std::pair<VEC3,VEC3> > coordonnees; //tableau de paires (x,y)
	coordonnees.reserve(50);

	//classer les coordonnees en x
	std::multimap<int, unsigned int>  coordonneesX;
	//classer les coordonnees en y
	std::multimap<int, unsigned int>  coordonneesY;

	std::set<int> sx;
	std::set<int> sy;
97

Pierre Kraemer's avatar
Pierre Kraemer committed
98
	// open file
Pierre Kraemer's avatar
Pierre Kraemer committed
99
	std::ifstream fp(filename.c_str(), std::ios::in);
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101
	if (!fp.good())
	{
102
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
103 104 105
		return false;
	}

106 107 108 109 110

	//
	// Lecture du fichier
	//

Pierre Kraemer's avatar
Pierre Kraemer committed
111 112 113
	std::string line;
	size_t posData, posCoord;
	std::string bg;
114 115

	unsigned int size = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117 118 119 120 121 122

	//two columns cutted
	std::getline(fp, line);
	posData = line.find("\t");

	// First column
	//Bounding box : first coord & second coord
Pierre Kraemer's avatar
Pierre Kraemer committed
123
	bg = line.substr(0, posData);
Pierre Kraemer's avatar
Pierre Kraemer committed
124
	posCoord = bg.find(") (");
125 126
	VEC3 c1 = stringToEmb<PFP>(bg.substr(0, posCoord));
	VEC3 c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
127
	std::cout << std::endl;
128

129 130 131 132 133
	//coordonnees.push_back(std::pair<VEC3,VEC3>(c1,c2));
//	coordonneesX.insert(std::pair<int, unsigned int >(c1[0],size));
//	coordonneesY.insert(std::pair<int, unsigned int >(c1[1],size));
	sx.insert(c1[0]);
	sx.insert(c2[0]);
134

135 136 137
	sy.insert(c1[1]);
	sy.insert(c2[1]);
	size++;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141 142 143 144

	while ( std::getline( fp, line ) )
	{
		posData = line.find("\t");

		// First column
		//Bounding box : first coord & second coord
Pierre Kraemer's avatar
Pierre Kraemer committed
145
		bg = line.substr(0, posData);
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
		posCoord = bg.find(") (");

148 149
		VEC3 c1 = stringToEmb<PFP>(bg.substr(0, posCoord));
		VEC3 c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
150 151 152 153 154 155 156 157 158 159 160
		std::cout << std::endl;

		coordonnees.push_back(std::pair<VEC3,VEC3>(c1,c2));
//		coordonneesX.insert(std::pair<int, unsigned int >(c1[0],size));
//		coordonneesY.insert(std::pair<int, unsigned int >(c1[1],size));

		sx.insert(c1[0]);
		sx.insert(c2[0]);

		sy.insert(c1[1]);
		sy.insert(c2[1]);
161

162
		size++;
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164 165
	}

	fp.close();
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453


//	std::cout << "setx" << std::endl;
//	 for (std::set<int>::iterator it = sx.begin() ; it != sx.end(); ++it)
//	    std::cout << " " << *it;
//	 std::cout << std::endl;
//
//	 std::cout << "sety" << std::endl;
//	 for (std::set<int>::iterator it = sy.begin() ; it != sy.end(); ++it)
//	 	    std::cout << " " << *it;
//	 std::cout << std::endl;

	//
	// Creation de la grille
	//
	Algo::Modelisation::Polyhedron<PFP> prim(map,position);

	std::set<int>::iterator sity = sy.begin();

	int cx = sx.size();
	int cy = sy.size();
	prim.grid_topo(cx-1,cy-1);

	std::vector<Dart> tableVertDarts = prim.getVertexDarts();


	std::pair<std::multimap<int, unsigned int>::iterator,std::multimap<int, unsigned int>::iterator > retX;

	for(int i=0; i<cy; ++i)
	{
		//std::cout << "cood y =" << *sity << std::endl;
		std::set<int>::iterator sitx = sx.begin();

		for(int j=0; j<cx;++j)
		{
			Dart d = tableVertDarts[i*(cx)+j];
			position[d] = VEC3(*sitx, *sity, 0);

			objects[d] = -1;

			++sitx;
		}
		++sity;
	}

	sity = sy.begin();

	//
	// Coloriage
	//

	for(int i=0; i<cy; ++i)
	{
		//std::cout << "cood y =" << *sity << std::endl;
		std::set<int>::iterator sitx = sx.begin();

		for(int j=0; j<cx;++j)
		{
			Dart d = tableVertDarts[i*(cx)+j];

			//Calcul du centre de ce quad
			VEC3 c = Algo::Geometry::faceCentroid<PFP>(map, d, position);

			//std::cout << "center=" << c << std::endl;
			int place = 0;

			//Comparaison avec toutes les coordonnees si le point se trouve dedans
			for(typename std::vector<std::pair<VEC3,VEC3> >::iterator it  = coordonnees.begin(); it != coordonnees.end() ; ++it)
			{
				//comparaison si la coord du centre se situe dans l'intervalle de la boite anglobante
				//std::cout << "c1=" << (*it).first << " c2=" << (*it).second  << std::endl;

				if((*it).first[0] < c[0] && c[0] < (*it).second[0] && (*it).first[1] < c[1] && c[1] < (*it).second[1])
				{
					objects[d] = place;
					//std::cout << "i=" << place << std::endl;
					std::cout << "place=" << place << std::endl;
				}
				++place;
			}

			++sitx;
		}
		++sity;
	}



//	for(int i=0; i<cy; ++i)
//	{
//		std::cout << "cood y =" << *sity << std::endl;
//		std::set<int>::iterator sitx = sx.begin();
//
//		for(int j=0; j<cx;++j)
//		{
//			Dart d = tableVertDarts[i*(cx)+j];
//
//			//Amelioration : tester si le brin est marque d'abord
//			if(!fm.isMarked(d))
//			{
//				//chercher dans la multimap X le début des sommets dont la cle vaut *sitx
////				retX = coordonneesX.lower_bound(*sitx);
//				bool end = false;
//
//				std::cout << "sitx=" << *sitx << std::endl;
//				std::cout << "sity=" << *sity << std::endl;
//				//chercher dans ce sous ensemble avec une indirection vers les coordonnnes le couple de coordonnees dans le first a une valeur Y qui vaut *sitY
//				for(std::multimap<int, unsigned int>::iterator retX = coordonneesX.lower_bound(*sitx) ; !end && retX != coordonneesX.end() ; ++retX)
//				{
//					std::cout << (*retX).second << std::endl;
//					std::cout << coordonnees[(*retX).second].first << std::endl;
//
//					//si ce plongement est le debut d'une boite englobante
//					if(coordonnees[(*retX).second].first[1] == *sity)
//					{
//						std::cout << "plop" << std::endl;
//						//on plonge un brin de cette face
//						//objects[d] = (*retX).second;
//
//						//on marque les brins de la face
//						//fm.markOrbit(FACE_ORBIT, d);
//
//
////						Dart e = d;
////						do
////						{
////							//se balader dans la carte vers l'autre extremite de la boite englobante tout en marquant les faces
////
////							//Tout en incrementant les y
////
////							Dart f = e;
////							do
////							{
////								//on plonge un brin de cette face
////								objects[f] = (*retX).second;
////
////								//on marque les brins de la face
////								fm.markOrbit(FACE_ORBIT, f);
////
////								//d'abord sur les x tant que le x de la fin de la boite englobante n'a pas ete atteint
////
////								f = map.phi1(map.phi2(map.phi1(f)));
////							}
////							while(position[f][1] <= coordonnees[(*retX).second].second[1]);
////
////							e = map.phi2(map.phi_1(map.phi_1(e)));
////						}
////						while(position[e][0] <= coordonnees[(*retX).second].second[0]);
//
//					}
//				}
//
//				std::cout << std::endl << std::endl;
//			}
//
//			++sitx;
//		}
//		++sity;
//	}


//	std::pair<std::multimap<int, unsigned int>::iterator,std::multimap<int, unsigned int>::iterator > retX;
//	std::pair<std::multimap<int, unsigned int>::iterator,std::multimap<int, unsigned int>::iterator > retY;
//
//	for(int i=0; i<cy; ++i)
//	{
//		std::cout << "cood y =" << *sity << std::endl;
//		std::set<int>::iterator sitx = sx.begin();
//
//		for(int j=0; j<cx;++j)
//		{
//			Dart d = tableVertDarts[i*(cx)+j];
//
//			std::cout << "sitx=" << *sitx << std::endl;
//			std::cout << "sity=" << *sity << std::endl;
//
//			//chercher dans la multimap X le sous ensemble de sommets dont la cle vaut *sitx
//			retX = coordonneesX.equal_range(*sitx);
//
//			//chercher dans ce sous ensemble avec une indirection vers les coordonnnes le couple de coordonnees dans le first a une valeur Y qui vaut *sitY
//			typename std::multimap<int, unsigned int>::iterator it;
//			for (it=retX.first; it!=retX.second; ++it)
//			{
//				std::cout << (*it).second << std::endl;
//				std::cout << coordonnees[(*it).second].first << std::endl << std::endl;
//
//				//plonger le brin de cette face avec la valeur de l'indirection
//				if(coordonnees[(*it).second].first[1] == *sity)
//					objects[d] = (*it).second;
//			}
//			std::cout << std::endl;
//			++sitx;
//		}
//		++sity;
//	}

	//						Dart e = d;
	//						do
	//						{
	//							//se balader dans la carte vers l'autre extremite de la boite englobante tout en marquant les faces
	//
	//							//Tout en incrementant les y
	//
	//							Dart f = e;
	//							do
	//							{
	//								//on plonge un brin de cette face
	//								objects[f] = (*retX).second;
	//
	//								//on marque les brins de la face
	//								fm.mark(FACE_ORBIT, f);
	//
	//								//d'abord sur les x tant que le x de la fin de la boite englobante n'a pas ete atteint
	//
	//								f = map.phi1(map.phi2(map.phi1(f)));
	//							}
	//							while(position[f][1] < coordonnees[(*retX).second].second[1]);
	//
	//							e = map.phi2(map.phi_1(map.phi_1(e)));
	//
	//						}
	//						while(position[e][0] < coordonnees[(*retX).second].second[0]);


//	unsigned int em;
//	AttributeContainer& container = map.getAttributeContainer(VERTEX_CELL) ;
//	typename std::vector<std::pair<VEC3,VEC3> >::iterator it  = coordonnees.begin();
//	//	++it;
//
//    while( it != coordonnees.end())
//    {
//            std::cout << "x=" << (*it).first << "y=" << (*it).second << std::endl;
//
//
//            Dart d = map.newFace(4);
//
//            std::pair<VEC3,VEC3> c = *it;
//            VEC3 c1 = c.first;
//            VEC3 c2 = c.second;
//
//              em = container.insertLine();
//              position[em] = c1;
//              map.setDartEmbedding(VERTEX_ORBIT, d, em);
//              d = map.phi1(d);
//
//              em = container.insertLine();
//              position[em] = VEC3(c2[0],c1[1],c2[2]);
//              map.setDartEmbedding(VERTEX_ORBIT, d, em);
//              d = map.phi1(d);
//
//              em = container.insertLine();
//              position[em] = c2;
//              map.setDartEmbedding(VERTEX_ORBIT, d, em);
//              d = map.phi1(d);
//
//              em = container.insertLine();
//              position[em] = VEC3(c1[0],c2[1],c1[2]);
//              map.setDartEmbedding(VERTEX_ORBIT, d, em);
//              d = map.phi1(d);
//
//              ++it;
//    }



	//
	// Coloriage des cases de la grille en fonction des boites englobantes
	//
//	Dart d = map.begin();
//	Dart e = d;
//	do
//	{
//
//
//	}while(e != d);

//	DartMarkerStore mark(map);
//
//	for(Dart d = map.begin() ; d != map.end() ; map.next(d))
//	{
//		VEC3 a = map.getEmbedding(VERTEX_ORBIT);
//
//
//	}

	//IDEE : marquage des brins lors du plongement en fonction


Pierre Kraemer's avatar
Pierre Kraemer committed
454 455 456
	return true;
}

untereiner's avatar
untereiner committed
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
template <typename PFP>
bool importInESSVolumique(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames)
{
	typedef typename PFP::VEC3 VEC3;

	AttributeHandler<VEC3> position = map.template addAttribute<VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(position.name()) ;

	AttributeHandler<int> objects = map.template addAttribute<int>(FACE_ORBIT, "objects") ;
	attrNames.push_back(objects.name()) ;


	//stocker les boundingbox contenu dans le fichier
	std::vector<std::pair<VEC3,VEC3> > coordonnees; //tableau de paires (x,y)
	coordonnees.reserve(50);

	//classer les coordonnees en x
	std::multimap<int, unsigned int>  coordonneesX;
	//classer les coordonnees en y
	std::multimap<int, unsigned int>  coordonneesY;

	std::set<int> sx;
	std::set<int> sy;

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

	//
	// Lecture du fichier
	//

	std::string line;
	size_t posData, posCoord;
	std::string bg;

	unsigned int size = 0;

	//two columns cutted
	std::getline(fp, line);
	posData = line.find("\t");

	// First column
	//Bounding box : first coord & second coord
	bg = line.substr(0, posData);
	posCoord = bg.find(") (");
	VEC3 c1 = stringToEmb<PFP>(bg.substr(0, posCoord));
	VEC3 c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
	std::cout << std::endl;

	//coordonnees.push_back(std::pair<VEC3,VEC3>(c1,c2));
//	coordonneesX.insert(std::pair<int, unsigned int >(c1[0],size));
//	coordonneesY.insert(std::pair<int, unsigned int >(c1[1],size));
	sx.insert(c1[0]);
	sx.insert(c2[0]);

	sy.insert(c1[1]);
	sy.insert(c2[1]);
	size++;

	while ( std::getline( fp, line ) )
	{
		posData = line.find("\t");

		// First column
		//Bounding box : first coord & second coord
		bg = line.substr(0, posData);
Pierre Kraemer's avatar
Pierre Kraemer committed
528 529
		posCoord = bg.find(") (");

untereiner's avatar
untereiner committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		VEC3 c1 = stringToEmb<PFP>(bg.substr(0, posCoord));
		VEC3 c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
		std::cout << std::endl;

		coordonnees.push_back(std::pair<VEC3,VEC3>(c1,c2));
//		coordonneesX.insert(std::pair<int, unsigned int >(c1[0],size));
//		coordonneesY.insert(std::pair<int, unsigned int >(c1[1],size));

		sx.insert(c1[0]);
		sx.insert(c2[0]);

		sy.insert(c1[1]);
		sy.insert(c2[1]);

		size++;
Pierre Kraemer's avatar
Pierre Kraemer committed
545 546 547
	}

	fp.close();
untereiner's avatar
untereiner committed
548

Pierre Kraemer's avatar
Pierre Kraemer committed
549 550 551
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
552
} // namespace Import
Pierre Kraemer's avatar
Pierre Kraemer committed
553

Pierre Kraemer's avatar
Pierre Kraemer committed
554
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
555

Pierre Kraemer's avatar
Pierre Kraemer committed
556
} // namespace CGoGN
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

/*
	unsigned int diffX=std::numeric_limits<unsigned int>::max();

	unsigned int highestX = coordonneesX.rbegin()->first;
	unsigned int lowestX = coordonneesX.begin()->first;

	unsigned int id = (*(coordonneesX.begin())).first;

	//calcul de la min diff en x et min diff en y
	for(typename std::multimap<unsigned int, unsigned int>::iterator it = coordonneesX.begin() ; it != coordonneesX.end() ; ++it)
	{
		std::cout << "x=" << (*it).first << " VEC3(a) =" << coordonnees[(*it).second].first << " VEC3(b) =" << coordonnees[(*it).second].second << std::endl;
		if((*it).first != id)
		{
		//typename std::multimap<unsigned int, unsigned int>::iterator itup = coordonneesX.upper_bound(id);
		//unsigned int idup = (*itup).first;

		//if(id != highestX)
		//{
			//diffX = std::min(idup - id, diffX);
			diffX = std::min(gcd((*it).first,id), diffX);
			//std::cout << idup << " - " <<  id << " = " << (idup- id) << std::endl;
			std::cout << (*it).first << "pgcd" <<  id << " = " << gcd((*it).first, id) << std::endl;
			std::cout << "diffX = " << diffX << std::endl;
		//}

			id = (*it).first;
		}
	}
	std::cout << "diffX=" << diffX << std::endl;


	//
	//
	//
	unsigned int diffY=std::numeric_limits<unsigned int>::max();

	unsigned int highestY = coordonneesY.rbegin()->first;
	unsigned int lowestY = coordonneesY.begin()->first;

	id = (*(coordonneesY.begin())).first;
	//calcul de la min diff en x et min diff en y
	for(typename std::multimap<unsigned int, unsigned int>::iterator it = coordonneesY.begin() ; it != coordonneesY.end() ; ++it)
	{
		std::cout << "y=" << (*it).first << " VEC3(a) =" << coordonnees[(*it).second].first << " VEC3(b) =" << coordonnees[(*it).second].second << std::endl;

		if((*it).first != id)
		{
			//typename std::multimap<unsigned int, unsigned int>::iterator itup = coordonneesY.upper_bound(id);
			//unsigned int idup = (*itup).first;

			//if(id != highestY)
			//{
				//diffY = std::min(idup - id, diffY);
				diffY = std::min(gcd((*it).first,id), diffY);
				//std::cout << idup << " - " <<  id << " = " << (idup- id) << std::endl;
				std::cout << (*it).first << " pgcd " <<  id << " = " << gcd((*it).first, id) << std::endl;
				std::cout << "diffY = " << diffY << std::endl;
			//}

			id = (*it).first;
		}
	}
	std::cout << "diffY=" << diffY << std::endl;
*/