genericmap.cpp 15.5 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 27 28
/*******************************************************************************
* 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 "Topology/generic/genericmap.h"
#include "Topology/generic/dartmarker.h"
#include "Geometry/vector_gen.h"
#include "Geometry/matrix.h"
29
#include "Container/registered.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30 31 32

namespace CGoGN
{
33
std::map< std::string, RegisteredBaseAttribute* >* GenericMap::m_attributes_registry_map = NULL;
34

Pierre Kraemer's avatar
Pierre Kraemer committed
35 36
GenericMap::GenericMap()
{
37

38 39
	if (m_attributes_registry_map ==NULL)
		m_attributes_registry_map = new std::map< std::string, RegisteredBaseAttribute* >;
Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	// register all known types
	registerAttribute<Dart>("Dart");
	registerAttribute<Mark>("Mark");

	registerAttribute<long>("long");
	registerAttribute<int>("int");
	registerAttribute<short>("short");
	registerAttribute<char>("char");

	registerAttribute<unsigned long>("unsigned long");
	registerAttribute<unsigned int>("unsigned int");
	registerAttribute<unsigned short>("unsigned short");
	registerAttribute<unsigned char>("unsigned char");

	registerAttribute<Geom::Vec2f>(Geom::Vec2f::CGoGNnameOfType());
	registerAttribute<Geom::Vec3f>(Geom::Vec3f::CGoGNnameOfType());
	registerAttribute<Geom::Vec4f>(Geom::Vec4f::CGoGNnameOfType());

	registerAttribute<Geom::Vec2d>(Geom::Vec2d::CGoGNnameOfType());
	registerAttribute<Geom::Vec3d>(Geom::Vec3d::CGoGNnameOfType());
	registerAttribute<Geom::Vec4d>(Geom::Vec4d::CGoGNnameOfType());

	registerAttribute<Geom::Matrix33f>(Geom::Matrix33f::CGoGNnameOfType());
	registerAttribute<Geom::Matrix44f>(Geom::Matrix44f::CGoGNnameOfType());

	registerAttribute<Geom::Matrix33d>(Geom::Matrix33d::CGoGNnameOfType());
	registerAttribute<Geom::Matrix44d>(Geom::Matrix44d::CGoGNnameOfType());

	for (unsigned int i = 0; i < NB_ORBITS; ++i)
	{
70
		m_attribs[i].setRegistry(m_attributes_registry_map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
		m_embeddings[i] = NULL ;
		m_markerTables[i] = NULL ;
	}
}

GenericMap::~GenericMap()
{
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
	{
		if(isOrbitEmbedded(i))
		{
			m_attribs[i].clear(true) ;
		}
	}
85 86
	if (m_attributes_registry_map)
		delete m_attributes_registry_map;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 130 131 132 133 134 135 136 137 138 139 140 141 142
}

/****************************************
 *         EMBEDDING MANAGEMENT         *
 ****************************************/

void GenericMap::setDartEmbedding(unsigned int orbit, Dart d, unsigned int emb)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	unsigned int old = getDartEmbedding(orbit, d);

	// if same emb nothing to do
	if (old == emb)
		return;
	// if different then unref the old emb
	if (old != EMBNULL)
		m_attribs[orbit].unrefLine(old);
	// ref the new emb
	if (emb != EMBNULL)
		m_attribs[orbit].refLine(emb);
	// affect the embedding to the dart
	(*m_embeddings[orbit])[d.index] = emb ;
}


/****************************************
 *        ATTRIBUTES MANAGEMENT         *
 ****************************************/

void GenericMap::initOrbitEmbedding(unsigned int orbit, bool realloc)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded") ;
	DartMarker mark(*this) ;
	for(Dart d = begin(); d != end(); next(d))
	{
		if(!mark.isMarked(d))
		{
			mark.markOrbit(orbit, d) ;
			if(realloc || getEmbedding(d, orbit) == EMBNULL)
				embedNewCell(orbit, d) ;
		}
	}
}

template <typename R>
bool GenericMap::registerAttribute(const std::string &nameType)
{
	RegisteredBaseAttribute* ra = new RegisteredAttribute<R>;
	if (ra == NULL)
	{
		std::cerr << "Erreur enregistrement attribut" << std::endl;
		return false;
	}

	ra->setTypeName(nameType);

143
	m_attributes_registry_map->insert(std::pair<std::string, RegisteredBaseAttribute*>(nameType,ra));
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	return true;
}


/****************************************
 *             SAVE & LOAD              *
 ****************************************/

void GenericMap::update_m_emb_afterLoad()
{
	// get container of dart orbit
	AttribContainer& cont = m_attribs[DART_ORBIT] ;

	// get the list of attributes
	std::vector<std::string> listeNames;
	cont.getAttributesStrings(listeNames);

Pierre Kraemer's avatar
Pierre Kraemer committed
161
	// check if there are EMB_X attributes
Pierre Kraemer's avatar
Pierre Kraemer committed
162 163 164 165 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 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
	for (unsigned int i = 0;  i < listeNames.size(); ++i)
	{
		std::string sub = listeNames[i].substr(0,listeNames[i].size()-1);
		if (sub == "EMB_")
		{
			unsigned int orb = listeNames[i][4]-'0'; // easy atoi computation for one char;

			AttribMultiVect<unsigned int>& amv = cont.getDataVector<unsigned int>(i);
			m_embeddings[orb] = &amv ;
			std::cout << "Ajoute m_emb["<<orb<<"]:"<< i <<std::endl;
		}
	}
}

bool GenericMap::saveMapXml(const std::string& filename, bool compress)
{
	xmlTextWriterPtr writer = xmlNewTextWriterFilename(filename.c_str(), 0);
	xmlTextWriterStartDocument(writer,NULL,NULL,NULL);

	// Entete
	int rc = xmlTextWriterStartElement(writer, BAD_CAST "CGoGN_Map");
	rc = xmlTextWriterWriteAttribute(writer,  BAD_CAST "type", BAD_CAST this->mapTypeName().c_str());
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "nb_max_orbits","%u",NB_ORBITS);

	// save m_attribs
	for (unsigned int i=0; i<NB_ORBITS; ++i)
	{
		m_attribs[i].saveXml(writer,i);
	}


	// save m_orbMarker
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Orbit_MarkerSet");
	for (unsigned int i=0; i<NB_ORBITS; ++i)
	{
		int rc = xmlTextWriterStartElement(writer, BAD_CAST "MarkerSet");
		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "orbit", "%u", i);
		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "val", "%u", m_orbMarker[i].getMarkVal());
//		rc = xmlTextWriterWriteAttribute(writer,  BAD_CAST "bin", BAD_CAST m_orbMarker[i].getMarkerAsBinaryString().c_str());
//		m_orbMarker[i] ;
		rc = xmlTextWriterEndElement(writer);
	}
	rc = xmlTextWriterEndElement(writer);

	// save m_BoundaryMarkerVal
//	rc = xmlTextWriterStartElement(writer, BAD_CAST "Boundary_Marker");
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "val", "%u", m_BoundaryMarkerVal.getMarkVal());
//	rc = xmlTextWriterEndElement(writer);

	rc = xmlTextWriterEndElement(writer); // of map

	xmlTextWriterEndDocument(writer);
	xmlFreeTextWriter(writer);

	return true;
}


bool GenericMap::loadMapXml(const std::string& filename, bool compress)
{

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

	// Check if it is a CGoGN_Map file
//	if (strcmp((char*)(map_node->name),(char*)"CGoGN_Map")!=0)
	if (!chechXmlNode(map_node,"CGoGN_Map"))
	{
		std::cerr << "Wrong xml format: Root node != CGoGN_Map"<< std::endl;
		return false;
	}

	// check the file type
	xmlChar *prop = xmlGetProp(map_node, BAD_CAST "type");
	std::cout << "Loading "<< prop <<" xml file"<< std::endl;

	// check the nb max orbits
	prop = xmlGetProp(map_node, BAD_CAST "nb_max_orbits");
	unsigned int nbo = atoi((char*)prop);
	if (nbo != NB_ORBITS)
	{
		std::cerr << "Wrong nb max orbits in xml map"<< std::endl;
		return false;
	}

	/***********************************************
	*	 			 load attributs
	************************************************/
	for (xmlNode* cur_node = map_node->children; cur_node; cur_node = cur_node->next)
	{
		// for each attribute
//		if (strcmp((char*)(cur_node->name),(char*)"Attributes_Container")==0)
		if (chechXmlNode(cur_node, "Attributes_Container"))
		{
			std::cout << "LOAD ATTRIBUT"<< std::endl;
			// get the orbit id
			unsigned int id = AttribContainer::getIdXmlNode(cur_node);
			// and load container
			unsigned int nba = m_attribs[id].getNbAttributes();


			std::cout << "attribut "<<id<<" size="<< m_attribs[id].size()<< "  nbatt="<< nba<< std::endl;

			m_attribs[id].loadXml(cur_node);
		}
	}

	/***********************************************
	*   creation of the m_embeddings pointers table
	************************************************/
//	// get attribute names of dart orbit
//	AttribContainer& contDart = m_attribs[DART_ORBIT] ;
//	std::vector< std::string > tableNames;
//	contDart.getAttributesStrings(tableNames);
//
//	// find orbit frome name and store pointer in right place
//	for (unsigned int i = 0; i< tableNames.size(); ++i)
//	{
////		std::cout << i <<" : "<< tableNames[i]<<std::endl;
//
//		std::string& name = tableNames[i];
//		std::string is_an_emb = name.substr(0,4);
//		if (is_an_emb == "EMB_")
//		{
//			AttribMultiVect<unsigned int>& amv = contDart.getDataVector<unsigned int>(i) ;
//
//			std::string orbitname = name.substr(4, name.size()-4);
//			std::istringstream iss(orbitname);
//			unsigned int orbit;
//			iss >> orbit;
//
//			m_embeddings[orbit] = &amv ;
//		}
//	}
//
//
	update_m_emb_afterLoad();

	/***********************************************
	*	 load Orbit_MarkerSet & BoundaryMarker
	************************************************/
	xmlNode* cur_node = map_node->children;
	bool read1=false;
	bool read2=false;
	while (!(read1 || read2) && cur_node)// scan nodes to find the one with right name
	{
//		if (strcmp((char*)(cur_node->name),(char*)"Orbit_MarkerSet") == 0)
		if (chechXmlNode(cur_node, "Orbit_MarkerSet"))
		{
			for (xmlNode* mark_node = cur_node->children; mark_node; mark_node = mark_node->next)
			{
				xmlChar* prop = xmlGetProp(mark_node, BAD_CAST "orbit");
				unsigned int orb = atoi((char*)prop);
				prop = xmlGetProp(mark_node, BAD_CAST "val");
				unsigned int val = atoi((char*)prop);
				m_orbMarker[orb].setMarkVal(val);
			}
			read1 =true;
		}
		else
		{
//			if (strcmp((char*)(cur_node->name),(char*)"Boundary_Marker") == 0)
			std::cout << "Orbit_MarkerSet"<<std::endl;
//			if (chechXmlNode(cur_node, "Boundary_Marker"))
//			{
//				xmlChar* prop = xmlGetProp(cur_node, BAD_CAST "val");
//				unsigned int val = atoi((char*)prop);
//				m_BoundaryMarkerVal.setMarkVal(val);
//				read2 =true;
//			}
		}
		// next node
		cur_node = cur_node->next;
	}

	if (!(read1 && read2))
	{
		std::cerr <<"Error reading Marker in xml node"<<std::endl;
		return false;
	}

	xmlFreeDoc(doc);

	update_m_emb_afterLoad();

	return true ;
}

bool GenericMap::saveMapBin(const std::string& filename)
{
	CGoGNostream fs(filename.c_str(), std::ios::out|std::ios::binary);
	if (!fs)
	{
		std::cerr << "Unable to open file for writing: " << filename << std::endl;
		return false;
	}

	// Entete
	char* buff = new char[256];
	for (int i=0; i< 256; ++i)
		buff[i] = char(255);
	const char* cgogn = "CGoGN_Map";
	memcpy(buff,cgogn,10);
	std::string mt = mapTypeName();
	const char* mtc = mt.c_str();
	memcpy(buff+32,mtc,mt.size()+1);
	unsigned int *buffi = reinterpret_cast<unsigned int*>(buff+64);
	*buffi = NB_ORBITS;
	fs.write(reinterpret_cast<const char*>(buff), 256);

	delete buff;

	// save m_attribs
	for (unsigned int i=0; i<NB_ORBITS; ++i)
	{
		m_attribs[i].saveBin(fs,i);
	}


	std::vector<unsigned int> buffer;
	buffer.reserve(NB_ORBITS+1);
	for (unsigned int i=0; i<NB_ORBITS; ++i)
	{
		buffer.push_back(m_orbMarker[i].getMarkVal());
	}
//	buffer.push_back(m_BoundaryMarkerVal.getMarkVal());

	fs.write(reinterpret_cast<const char*>(&(buffer[0])), buffer.size()*sizeof(unsigned int));

	return true;
}

bool GenericMap::loadMapBin(const std::string& filename)
{
	CGoGNistream fs(filename.c_str(), std::ios::in|std::ios::binary);
	if (!fs)
	{
		std::cerr << "Unable to open file for loading" << std::endl;
		return false;
	}

	// entete
	char* buff = new char[256];
	fs.read(reinterpret_cast<char*>(buff), 256);

	std::string buff_str(buff);
	// Check file type
	if (buff_str != "CGoGN_Map")
	{
		std::cerr<< "Wrong binary file format"<< std::endl;
		return false;
	}

	// Check map type
	buff_str = std::string(buff+32);
	std::cout << "Map type file = "<< buff_str<< std::endl;

	std::string localType = this->mapTypeName();
	localType = localType.substr(0,localType.size()-1);

	std::string fileType = buff_str.substr(0,buff_str.size()-1);

	if (fileType != localType)
	{
		std::cerr << "Not possible to load "<< fileType<< " into "<< localType << " object"<<std::endl;
		return false;
	}

	// Check max nb orbit
	unsigned int *ptr_nbo = reinterpret_cast<unsigned int*>(buff+64);
	unsigned int nbo = *ptr_nbo;
	if (nbo != NB_ORBITS)
	{
		std::cerr << "Wrond max orbit number in file" << std::endl;
		return  false;
	}

	// load attrib container
	for (unsigned int i=0; i<NB_ORBITS; ++i)
	{
		unsigned int id = AttribContainer::loadBinId(fs);
		m_attribs[id].loadBin(fs);
	}

	// load MarkerSet & boundary
	std::vector<unsigned int> buffer;
	buffer.resize(NB_ORBITS);
	fs.read(reinterpret_cast<char*>(&(buffer[0])), (NB_ORBITS+1)*sizeof(unsigned int));
	for (unsigned int i=0; i<NB_ORBITS; ++i)
	{
		m_orbMarker[i].setMarkVal(buffer[i]);
	}
//	m_BoundaryMarkerVal.setMarkVal(buffer[NB_ORBITS]);

	update_m_emb_afterLoad();

	return true;
}


/****************************************
 *           DARTS TRAVERSALS           *
 ****************************************/

bool GenericMap::foreach_dart(FunctorType& f, const FunctorSelect& good)
{
	for (Dart d = begin(); d != end(); next(d))
	{
		if(good(d))
			if (f(d))
				return true;
	}
	return false;
}

bool GenericMap::foreach_dart_of_orbit(unsigned int orbit, Dart d, FunctorType& f)
{
	switch(orbit)
	{
		case  DART_ORBIT: return f(d);
		case  VERTEX_ORBIT: return foreach_dart_of_vertex(d, f);
		case  EDGE_ORBIT: return foreach_dart_of_edge(d, f);
		case  FACE_ORBIT: return foreach_dart_of_face(d, f);
		case  VOLUME_ORBIT: return foreach_dart_of_volume(d, f);
//		case -1: return foreach_dart_of_cc(d,f);
		default: assert(!"Cells of this dimension are not handled");
	}
	return false;
}

bool GenericMap::foreach_orbit(unsigned int orbit, FunctorType& fonct, const FunctorSelect& good)
{
	DartMarker marker(*this);	// Lock a marker
	bool found = false;			// Store the result

	// Scan all darts of the map
	for (Dart d = begin(); !found && d != end(); next(d))
	{
		if (good(d))							// If d is selected
		{
			if (!marker.isMarked(d))			// If d is in a not yet visited cell
			{
				if ((fonct)(d))					// Apply functor
					found = true;
				else
					marker.markOrbit(orbit, d);	// Mark the cell as visited
			}
		}
	}
	return found;
}

unsigned int GenericMap::getNbOrbits(unsigned int orbit, const FunctorSelect& good)
{
	FunctorCount fcount;
	foreach_orbit(orbit, fcount, good);
	return fcount.getNb();
}

} // namespace CGoGN