importInESS.hpp 14.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
/*******************************************************************************
* 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"
26
#include "Algo/Modelisation/primitives3d.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
27
#include <vector>
28 29
#include "Geometry/bounding_box.h"
#include <set>
Pierre Kraemer's avatar
Pierre Kraemer committed
30 31 32 33 34 35 36 37 38

namespace CGoGN
{
namespace Algo
{
namespace Import 
{

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
39
typename PFP::VEC3 stringToEmb(std::string s)
Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
	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);

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

	return coord;
}

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

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

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

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

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

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	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;
98

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

107 108 109 110 111

	//
	// Lecture du fichier
	//

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

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

	//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
124
	bg = line.substr(0, posData);
Pierre Kraemer's avatar
Pierre Kraemer committed
125
	posCoord = bg.find(") (");
126 127
	VEC3 c1 = stringToEmb<PFP>(bg.substr(0, posCoord));
	VEC3 c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
128
	std::cout << std::endl;
129

130 131 132 133 134
	//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]);
135

136 137
	sy.insert(c1[1]);
	sy.insert(c2[1]);
138 139 140 141 142

	// Second Column (nil)
	bg = line.substr(posData);
	//std::cout << bg << std::endl;

143
	size++;
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147 148 149 150

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

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

154 155
		VEC3 c1 = stringToEmb<PFP>(bg.substr(0, posCoord));
		VEC3 c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
156
		//std::cout << std::endl;
157 158 159 160 161 162 163 164

		coordonnees.push_back(std::pair<VEC3,VEC3>(c1,c2));

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

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

166 167 168 169 170 171 172 173 174 175 176 177 178

		//Second Column
		bg = line.substr(posData);
		posCoord = bg.find(") (");
		//std::cout << bg << std::endl;
		c1 = stringToEmb<PFP>(bg.substr(1, posCoord+1));
		//CGoGNout << c1 << CGoGNendl;
		c2 = stringToEmb<PFP>(bg.substr(posCoord+3));
		//CGoGNout << c2 << CGoGNendl;
		influences.push_back(std::pair<VEC3,VEC3>(c1,c2));



179
		size++;
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182
	}

	fp.close();
183 184 185 186 187 188 189 190 191 192 193 194 195


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

196
	//tableau des brins (un par sommet)
197 198 199 200 201 202 203 204 205 206 207 208 209
	std::vector<Dart> tableVertDarts = prim.getVertexDarts();


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

210
			objects[d] = 0;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

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

231 232
			if(objects[d] == 0)
			{
233

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

237
				//std::cout << "center=" << c << std::endl;
238

239 240 241
				int i = 1;
				//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)
242
				{
243 244 245 246 247 248 249 250 251
					//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] = i ;
					}
					++i;
252 253
				}

254
			}
255 256 257 258 259
			++sitx;
		}
		++sity;
	}

260
	std::cout << "coord size : " << coordonnees.size() << std::endl;
261 262


Pierre Kraemer's avatar
Pierre Kraemer committed
263 264 265
	return true;
}

untereiner's avatar
untereiner committed
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
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())
	{
294
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
untereiner's avatar
untereiner committed
295 296 297
		return false;
	}

298

untereiner's avatar
untereiner committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	//
	// 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;

321 322 323 324
	float x = c1[0];
	float y = c1[1];
	float z = c1[2];

untereiner's avatar
untereiner committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	//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
342 343
		posCoord = bg.find(") (");

untereiner's avatar
untereiner committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		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
359 360 361
	}

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

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

//	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::Primitive3D<PFP> prim(map, position);

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

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

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


	prim.embedHexaGrid(x,y,z);

//	std::pair<std::multimap<int, unsigned int>::iterator,std::multimap<int, unsigned int>::iterator > retX;
//	int nbs = (cx+1)*(cy+1);
//
//	for(int i=0; i<= 1; ++i) //z
//	{
//		for(int j=0; j<= cy; ++j) //y
//		{
//			std::set<int>::iterator sitx = sx.begin();
//
//			for(int k=0; k<=cx;++k)	//x
//			{
//				Dart d = tableVertDarts[ i*nbs+j*(cx+1)+k ];
//
//				map.embedNewCell(VERTEX_ORBIT,d);
//
//				position[d] = VEC3(float(k), float(j), float(i));
//
////				if(i == 0)
////					position[d] = VEC3(*sitx, *sity, 0);
////				else
////					position[d] = VEC3(*sitx,*sity, 0);
//
//				++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];
//
//			position[d] = VEC3(*sitx, *sity, 0);
//
//			position[map.phi1(map.phi1(map.phi2(d)))] = VEC3(*sitx, *sity, 14000);
//
//
//			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;
//	}

Pierre Kraemer's avatar
Pierre Kraemer committed
481 482 483
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
484
} // namespace Import
Pierre Kraemer's avatar
Pierre Kraemer committed
485

Pierre Kraemer's avatar
Pierre Kraemer committed
486
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
487

Pierre Kraemer's avatar
Pierre Kraemer committed
488
} // namespace CGoGN
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

/*
	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;
*/