genericmap.h 14.6 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __GENERIC_MAP__
#define __GENERIC_MAP__

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <list>
#include <vector>
#include <map>

36
#include "Container/attributeContainer.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
37

Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40 41 42 43 44
#include "Topology/generic/dart.h"
#include "Topology/generic/marker.h"
#include "Topology/generic/functor.h"

namespace CGoGN
{

CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
const unsigned int EMBNULL = 0xffffffff;

const unsigned int NB_ORBITS = 6;
const unsigned int NB_THREAD = 16;

const unsigned int VERTEX	= 0;
const unsigned int EDGE		= 1;
const unsigned int ORIENTED_FACE= 2;
const unsigned int FACE		= 3;
const unsigned int VOLUME	= 4;
const unsigned int DART		= 5;
*/

Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/**
 * Class that allows to browse a map
 * This is a pure virtual class that can be overloaded in:
 * - a map (generic and so on)
 * - a MapBrowserSelector
 * - a MapBrowserLinked (Attr or Auto)
 */
class MapBrowser
{
public:
	virtual Dart begin() = 0;
	virtual Dart end() = 0;
	virtual void next(Dart& d) = 0;
};

74 75
class AttributeHandlerGen ;

Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
class GenericMap : public MapBrowser
{
78
	template<typename T> friend class AttributeHandler ;
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	template<typename T> friend class AutoAttributeHandler ;
Sylvain Thery's avatar
Sylvain Thery committed
80
	friend class CellMarkerGen ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83 84 85

protected:
	/**
	 * Attributes Containers
	 */
86
	AttributeContainer m_attribs[NB_ORBITS] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
87

88
	static std::map<std::string, RegisteredBaseAttribute*>* m_attributes_registry_map ;
Pierre Kraemer's avatar
Pierre Kraemer committed
89 90 91 92 93

	/**
	 * Direct access to the Dart attributes that store the orbits embeddings
	 * (only initialized when necessary)
	 */
94
	AttributeMultiVector<unsigned int>* m_embeddings[NB_ORBITS] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96

	/**
Pierre Kraemer's avatar
Pierre Kraemer committed
97
	 * Marks manager
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
99
	MarkSet m_marksets[NB_ORBITS][NB_THREAD] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103

	/**
	 * Direct access to the attributes that store Marks
	 */
104 105 106
	AttributeMultiVector<Mark>* m_markTables[NB_ORBITS][NB_THREAD] ;

	unsigned int m_nbThreads ;
Sylvain Thery's avatar
Sylvain Thery committed
107

108
	std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*> attributeHandlers ;
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110

public:
111
	static const unsigned int UNKNOWN_ATTRIB = AttributeContainer::UNKNOWN ;
Pierre Kraemer's avatar
Pierre Kraemer committed
112

113
	GenericMap() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
114

115
	~GenericMap() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116

117 118 119 120 121 122 123 124 125
	virtual std::string mapTypeName() = 0 ;

	/**
	 * Clear the map
	 * @param removeAttrib
	 *   if false -> data is deleted but all attributes remain (all AttributeHandlers are still valid)
	 *   if true -> data and attributes are deleted (AttributeHandlers are invalid)
	 */
	virtual void clear(bool removeAttrib) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
126

Sylvain Thery's avatar
Sylvain Thery committed
127 128 129 130 131
	/**
	 * get the marker_set of an orbit and thread (used for Cell & Dart Marker)
	 */
	MarkSet& getMarkerSet(unsigned int orbit, unsigned int thread) { return m_marksets[orbit][thread]; }

Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134 135 136 137 138
	/****************************************
	 *           DARTS MANAGEMENT           *
	 ****************************************/
protected:
	/**
	 * Add a dart to the map
	 */
139
	virtual Dart newDart() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143

	/**
	 * Erase a dart of the map
	 */
144
	void deleteDart(Dart d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146 147 148 149 150 151 152 153 154

public:
	/**
	 * return true if the dart d refers to a valid index
	 */
	bool isDartValid(Dart d) ;

	/**
	 * @return the number of darts in the map
	 */
155
	unsigned int getNbDarts() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159 160 161 162 163

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

	/**
	 * tell if an orbit is embedded or not
	 */
164
	bool isOrbitEmbedded(unsigned int orbit) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166

	/**
167
	 * return the number of embedded orbits (including DART)
Pierre Kraemer's avatar
Pierre Kraemer committed
168
	 */
169
	unsigned int nbEmbeddings() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
170 171 172 173 174 175

	/**
	 * get the cell index of the given dimension associated to dart d
	 * (can go through the whole orbit due to lazy embedding)
	 * @return EMBNULL if the orbit of d is not attached to any cell
	 */
176
	unsigned int getEmbedding(unsigned int orbit, Dart d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
177 178 179 180

	/**
	 * Set the cell index of the given dimension associated to dart d
	 */
181
	void setDartEmbedding(unsigned int orbit, Dart d, unsigned int emb) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184 185 186 187 188

	/**
	 * Copy the index of the cell associated to a dart over an other dart
	 * @param d the dart to overwrite (dest)
	 * @param e the dart to copy (src)
	 * @param orbit the id of orbit embedding
	 */
189
	void copyDartEmbedding(unsigned int orbit, Dart d, Dart e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195

	/**
	 * Allocation of some place in attrib table
	 * @param orbit the orbit of embedding
	 * @return the index to use as embedding
	 */
196
	unsigned int newCell(unsigned int orbit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
197 198 199 200 201 202 203

	/**
	* Set the index of the associated cell to all the darts of an orbit
	* @param orbit orbit to embed
	* @param d a dart of the topological vertex
	* @param em index of attribute to store as embedding
	*/
204
	void embedOrbit(unsigned int orbit, Dart d, unsigned int em) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206

	/**
untereiner's avatar
untereiner committed
207
	* Associate an new embedding to all darts of an orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
208 209 210 211
	* @param orbit orbit to embed
	* @param d a dart of the topological cell
	* @return index of the attribute in table
	*/
212
	unsigned int embedNewCell(unsigned int orbit, Dart d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
213 214

	/**
untereiner's avatar
untereiner committed
215
	 * Copy the cell associated to a dart over an other dart
Pierre Kraemer's avatar
Pierre Kraemer committed
216
	 * @param orbit attribute orbit to use
untereiner's avatar
untereiner committed
217 218
	 * @param d the dart to overwrite (dest)
	 * @param e the dart to copy (src)
Pierre Kraemer's avatar
Pierre Kraemer committed
219
	 */
220
	void copyCell(unsigned int orbit, Dart d, Dart e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
221 222 223 224 225 226 227

	/**
	 * Line of attributes i is overwritten with line j
	 * @param orbit attribute orbit to use
	 * @param i line destination of copy
	 * @param j line source of copy
	 */
228
	void copyCell(unsigned int orbit, unsigned int i, unsigned int j) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
229 230 231 232 233 234

	/**
	 * Line of attributes i is initialized
	 * @param orbit attribute orbit to use
	 * @param i line to init
	 */
235
	void initCell(unsigned int orbit, unsigned int i) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237 238 239 240 241

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

	/**
242 243
	 * get the attrib container of a given orbit
	 * @param orbit the orbit !!! (bilbo the orbit !)
Pierre Kraemer's avatar
Pierre Kraemer committed
244
	 */
245
	AttributeContainer& getAttributeContainer(unsigned int orbit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247

	/**
Pierre Kraemer's avatar
Pierre Kraemer committed
248
	 * get a multi vector of mark attribute (direct access with [i])
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250
	 * @param orbit code
	 */
251
	AttributeMultiVector<Mark>* getMarkVector(unsigned int orbit, unsigned int thread = 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
252 253

	/**
254 255
	 * return a pointer to the Dart attribute vector that store the embedding of the given orbit
	 * (may be NULL if the orbit is not embedded)
Pierre Kraemer's avatar
Pierre Kraemer committed
256
	 */
257
	AttributeMultiVector<unsigned int>* getEmbeddingAttributeVector(unsigned int orbit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
258 259

	/**
260
	 * swap two attribute containers
Pierre Kraemer's avatar
Pierre Kraemer committed
261
	 */
262
	void swapEmbeddingContainers(unsigned int orbit1, unsigned int orbit2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
263 264 265 266 267 268 269

	/**
	 * static function for type registration
	 */
	template <typename R>
	static bool registerAttribute(const std::string &nameType) ;

270 271 272 273
	/**
	 * Traverse the map and embed all orbits of the given dimension with a new cell
	 * @param realloc if true -> all the orbits are embedded on new cells, if false -> already embedded orbits are not impacted
	 */
274
	void initOrbitEmbedding(unsigned int orbit, bool realloc = false) ;
Sylvain Thery's avatar
Sylvain Thery committed
275

Sylvain Thery's avatar
Sylvain Thery committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
protected:
	/****************************************
	 *   EMBEDDING ATTRIBUTES MANAGEMENT    *
	 ****************************************/
	/**
	 * Create the dart attribute to store the embedding of this orbit (for internal use only)
	 * Also adds a Marker attribute to the container
	 */
	void addEmbedding(unsigned int orbit) ;

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

	/**
	 * Add a toological relation in the map
	 * @param name name of relation
	 */
	AttributeMultiVector<Dart>* addRelation(const std::string& name) ;

Sylvain Thery's avatar
Sylvain Thery committed
296
	/****************************************
297
	 *          THREAD MANAGEMENT           *
Sylvain Thery's avatar
Sylvain Thery committed
298 299 300 301 302 303 304
	 ****************************************/
public:
	/**
	 * add  threads (a table of Marker per orbit for each thread)
	 * to allow MT
	 * @param nb thread to add
	 */
305
	void addThreadMarker(unsigned int nb) ;
Sylvain Thery's avatar
Sylvain Thery committed
306 307 308 309 310

	/**
	 * return allowed threads
	 * @return the number of threads (including principal)
	 */
311
	unsigned int getNbThreadMarkers() ;
Sylvain Thery's avatar
Sylvain Thery committed
312 313

	/**
Pierre Kraemer's avatar
Pierre Kraemer committed
314
	 * Remove some added threads
Sylvain Thery's avatar
Sylvain Thery committed
315 316
	 * @return remaining number of threads (including principal)
	 */
317
	void removeThreadMarker(unsigned int nb) ;
Sylvain Thery's avatar
Sylvain Thery committed
318

Pierre Kraemer's avatar
Pierre Kraemer committed
319 320 321 322 323 324 325 326 327 328
	/****************************************
	 *             SAVE & LOAD              *
	 ****************************************/
public:
	/**
	 * check if an xml node has a given name
	 * @param node the xml node
	 * @param name the name
	 * @ return true if node has the good name
	 */
329
	bool chechXmlNode(xmlNodePtr node, const std::string& name) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
330 331 332 333 334 335 336 337 338 339 340

	/**
	 * update the pointer of embedding vector after loading
	 */
	void update_m_emb_afterLoad();

	/**
	 * Save map in a XML file
	 * @param filename the file name
	 * @return true if OK
	 */
341
	bool saveMapXml(const std::string& filename, bool compress = false) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
342 343 344 345 346 347

	 /**
	 * Load map from a XML file
	 * @param filename the file name
	 * @return true if OK
	 */
348
	bool loadMapXml(const std::string& filename, bool compress = false) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
349 350 351 352 353 354

	/**
	 * Save map in a binary file
	 * @param filename the file name
	 * @return true if OK
	 */
355
	bool saveMapBin(const std::string& filename) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
356 357 358 359 360 361

	/**
	 * Load map from a binary file
	 * @param filename the file name
	 * @return true if OK
	 */
362
	bool loadMapBin(const std::string& filename) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
363 364 365 366 367 368 369 370 371

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

	/**
	 * Begin of map
	 * @return the first dart of the map
	 */
372
	Dart begin() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
373 374 375 376 377

	/**
	 * End of map
	 * @return the end iterator (next of last) of the map
	 */
378
	Dart end() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
379 380 381 382 383 384

	/**
	 * allow to go from a dart to the next
	 * in the order of storage
	 * @param d reference to the dart to be modified
	 */
385
	void next(Dart& d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
386 387 388 389 390

	/**
	 * Apply a functor on each dart of the map
	 * @param f a ref to the functor obj
	 */
391
	bool foreach_dart(FunctorType& f, const FunctorSelect& good = SelectorTrue()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
392 393 394 395 396 397

	//! Apply a functor on every dart of an orbit
	/*! @param dim dimension of orbit
	 *  @param d a dart of the orbit
	 *  @param f a functor obj
	 */
398
	bool foreach_dart_of_orbit(unsigned int orbit, Dart d, FunctorType& f, unsigned int thread = 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
399

400 401 402 403 404 405
	virtual bool foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread = 0) = 0 ;
	virtual bool foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread = 0) = 0 ;
	virtual bool foreach_dart_of_oriented_face(Dart d, FunctorType& f, unsigned int thread = 0) = 0 ;
	virtual bool foreach_dart_of_face(Dart d, FunctorType& f, unsigned int thread = 0) = 0 ;
	virtual bool foreach_dart_of_volume(Dart d, FunctorType& f, unsigned int thread = 0) = 0 ;
	virtual bool foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread = 0) = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
406 407 408 409 410 411 412

	/**
	* execute functor for each orbit
	* @param dim the dimension of the orbit
	* @param f the functor
	* @param good the selector of darts
	*/
413
	bool foreach_orbit(unsigned int orbit, FunctorType& f, const FunctorSelect& good = SelectorTrue(), unsigned int thread = 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
414 415 416 417 418 419

	//! Count the number of orbits of dimension dim in the map
	/*! @param dim the dimension of the orbit
	 *	@param good the selector of darts
	 * 	@return the number of orbits
	 */
420
	unsigned int getNbOrbits(unsigned int orbit, const FunctorSelect& good = SelectorTrue()) ;
Sylvain Thery's avatar
Sylvain Thery committed
421 422 423 424 425

	/**
	 * print attributes name of map in std::cout (for debugging)
	 */
	void viewAttributesTables();
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

protected:
	/// boundary marker
	Mark m_boundaryMarker;

	/**
	 * mark a dart as  belonging to boundary
	 */
	void boundaryMark(Dart d);

	/**
	 * unmark a dart from the boundary
	 */
	void boundaryUnmark(Dart d);

Pierre Kraemer's avatar
Pierre Kraemer committed
441
public:
442 443 444 445 446
	/**
	 * test if a dart belong to the boundary
	 */
	bool isBoundaryMarked(Dart d);

Pierre Kraemer's avatar
Pierre Kraemer committed
447
protected:
448 449 450 451 452 453 454 455 456 457 458 459 460 461
	/**
	 * mark an orbit of dart as belonging to boundary
	 */
	void boundaryMarkOrbit(unsigned int orbit, Dart d);

	/**
	 * unmark an orbit of dart from the boundary
	 */
	void boundaryUnmarkOrbit(unsigned int orbit, Dart d);

	/**
	 * clear all boundary markers
	 */
	void boundaryUnmarkAll();
Sylvain Thery's avatar
Sylvain Thery committed
462

Pierre Kraemer's avatar
Pierre Kraemer committed
463 464
} ;

465 466

template <typename MAP>
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
467
bool foreach_dart_of_orbit_in_parent(MAP* ptrMap, unsigned int orbit, Dart d, FunctorType& f, unsigned int thread = 0)
468 469 470
{
	switch(orbit)
	{
471
		case  DART: return f(d);
472 473 474 475 476 477
		case  VERTEX: return ptrMap->MAP::ParentMap::foreach_dart_of_vertex(d, f, thread) ;
		case  EDGE: return ptrMap->MAP::ParentMap::foreach_dart_of_edge(d, f, thread) ;
		case  ORIENTED_FACE: return ptrMap->MAP::ParentMap::foreach_dart_of_oriented_face(d, f, thread) ;
		case  FACE: return ptrMap->MAP::ParentMap::foreach_dart_of_face(d, f, thread) ;
		case  VOLUME: return ptrMap->MAP::ParentMap::foreach_dart_of_volume(d, f, thread) ;
		default: assert(!"Cells of this dimension are not handled") ;
478
	}
479
	return false ;
480 481 482
}

template <typename MAP>
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
483
bool foreach_dart_of_orbit_in_parent2(MAP* ptrMap, unsigned int orbit, Dart d, FunctorType& f, unsigned int thread = 0)
484 485 486
{
	switch(orbit)
	{
487
		case  DART: return f(d);
488 489 490 491 492 493
		case  VERTEX: return ptrMap->MAP::ParentMap::ParentMap::foreach_dart_of_vertex(d, f,thread) ;
		case  EDGE: return ptrMap->MAP::ParentMap::ParentMap::foreach_dart_of_edge(d, f, thread) ;
		case  ORIENTED_FACE: return ptrMap->MAP::ParentMap::ParentMap::foreach_dart_of_oriented_face(d, f, thread) ;
		case  FACE: return ptrMap->MAP::ParentMap::ParentMap::foreach_dart_of_face(d, f, thread) ;
		case  VOLUME: return ptrMap->MAP::ParentMap::ParentMap::foreach_dart_of_volume(d, f, thread) ;
		default: assert(!"Cells of this dimension are not handled") ;
494
	}
495
	return false ;
496 497
}

Pierre Kraemer's avatar
Pierre Kraemer committed
498 499 500 501 502
} //namespace CGoGN

#include "Topology/generic/genericmap.hpp"

#endif