genericmap.hpp 8.91 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

inline bool GenericMap::chechXmlNode(xmlNodePtr node, const std::string& name)
{
	return (strcmp((char*)(node->name),(char*)(name.c_str())) == 0);
}

/****************************************
 *           DARTS MANAGEMENT           *
 ****************************************/

inline Dart GenericMap::newDart()
{
	Dart d = Dart::create(m_attribs[DART_ORBIT].insertLine());
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
		if (m_embeddings[i])
			(*m_embeddings[i])[d.index] = EMBNULL ;
	return d ;
}

inline void GenericMap::deleteDart(Dart d)
{
	m_attribs[DART_ORBIT].removeLine(d.index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51 52 53 54 55 56
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		if (m_embeddings[orbit])
		{
			unsigned int emb = (*m_embeddings[orbit])[d.index] ;
			if(emb != EMBNULL)
				m_attribs[orbit].unrefLine(emb) ;
		}
sylvain thery's avatar
sylvain thery committed
57 58
		for (unsigned int t=0; t<m_nbThreads; ++t)
			m_markerTables[DART_ORBIT][t]->operator[](d.index).clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
59
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

inline bool GenericMap::isDartValid(Dart d)
{
	return !d.isNil() && m_attribs[DART_ORBIT].used(d.index) ;
}

inline unsigned int GenericMap::getNbDarts()
{
	return m_attribs[DART_ORBIT].size() ;
}

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

inline bool GenericMap::isOrbitEmbedded(unsigned int orbit) const
{
	return (orbit == DART_ORBIT) || (m_embeddings[orbit] != NULL) ;
}

inline unsigned int GenericMap::nbEmbeddings() const
{
	unsigned int nb = 0;
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
		if (isOrbitEmbedded(i))
			++nb;
	return nb ;
}

inline unsigned int GenericMap::getEmbedding(Dart d, unsigned int orbit)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");

	if (orbit == DART_ORBIT)
		return d.index;

	unsigned int emb = (*m_embeddings[orbit])[d.index] ;
	if(emb != EMBNULL)
		return emb ;

Pierre Kraemer's avatar
Pierre Kraemer committed
101 102 103 104 105
	class FunctorGetLazyEmb: public FunctorType
	{
	protected:
		GenericMap& m_map;
		unsigned int m_orbit;
106
		AttributeMultiVector<unsigned int>* m_emb;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		unsigned int m_val;
		std::vector<Dart> m_darts;

	public:
		FunctorGetLazyEmb(GenericMap& map, unsigned int orbit):
			m_map(map), m_orbit(orbit), m_val(EMBNULL)
		{
			m_emb = m_map.getEmbeddingAttributeVector(orbit) ;
			m_darts.reserve(8) ;
		}

		bool operator()(Dart d)
		{
			if ((*m_emb)[d.index] != EMBNULL)
			{
				m_val = (*m_emb)[d.index];
				return true;
			}
			m_darts.push_back(d);
			return false;
		}

		unsigned int getVal()
		{
			if(m_val != EMBNULL)
			{
133
				AttributeContainer& cont = m_map.getAttributeContainer(m_orbit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136 137 138 139 140 141 142 143 144 145
				for(std::vector<Dart>::iterator it = m_darts.begin(); it != m_darts.end(); ++it)
				{
					(*m_emb)[it->index] = m_val;
					cont.refLine(m_val) ;
				}
			}

			return m_val;
		}
	};

	FunctorGetLazyEmb fle(*this, orbit);
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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
	foreach_dart_of_orbit(orbit, d, fle);

	return fle.getVal();
}

inline unsigned int GenericMap::getDartEmbedding(unsigned int orbit, Dart d)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");

	if (orbit == DART_ORBIT)
		return d.index;

	return (*m_embeddings[orbit])[d.index] ;
}

inline void GenericMap::copyDartEmbedding(unsigned int orbit, Dart d, Dart e)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	setDartEmbedding(orbit, d, getDartEmbedding(orbit, e));
}

inline unsigned int GenericMap::newCell(unsigned int orbit)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	return m_attribs[orbit].insertLine();
}

inline void GenericMap::embedOrbit(unsigned int orbit, Dart d, unsigned int em)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	FunctorSetEmb<GenericMap> fsetemb(*this,orbit,em);
	foreach_dart_of_orbit(orbit, d, fsetemb);
}

inline unsigned int GenericMap::embedNewCell(unsigned int orbit, Dart d)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	unsigned int em = newCell(orbit);
//	setDartEmbedding(orbit, d, em) ; // possible ?? utile ??
	embedOrbit(orbit, d, em);
	return em;
}

inline void GenericMap::copyCell(unsigned int orbit, Dart d, Dart e)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	unsigned int dE = getEmbedding(d, orbit) ;
	unsigned int eE = getEmbedding(e, orbit) ;
	if(eE != EMBNULL)	// if the source is NULL, nothing to copy
	{
		if(dE == EMBNULL)	// if the dest is NULL, create a new cell
		{
			dE = embedNewCell(orbit, d) ;
		}
		m_attribs[orbit].copyLine(dE, eE) ;	// copy the data
	}
}

inline void GenericMap::copyCell(unsigned int orbit, unsigned int i, unsigned int j)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	m_attribs[orbit].copyLine(i,j) ;
}

inline void GenericMap::initCell(unsigned int orbit, unsigned int i)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	m_attribs[orbit].initLine(i) ;
}

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

220
inline AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit)
Pierre Kraemer's avatar
Pierre Kraemer committed
221
{
222
	return m_attribs[orbit] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224
}

225
inline AttributeMultiVector<Mark>* GenericMap::getMarkerVector(unsigned int orbit, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
226
{
227
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded") ;
Sylvain Thery's avatar
Sylvain Thery committed
228
	return m_markerTables[orbit][thread] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
229 230
}

231
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector(unsigned int orbit)
Pierre Kraemer's avatar
Pierre Kraemer committed
232
{
233
	return m_embeddings[orbit] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
234 235
}

Pierre Kraemer's avatar
Pierre Kraemer committed
236
inline void GenericMap::swapEmbeddingContainers(unsigned int orbit1, unsigned int orbit2)
Pierre Kraemer's avatar
Pierre Kraemer committed
237
{
Pierre Kraemer's avatar
Pierre Kraemer committed
238 239
	assert(orbit1 != orbit2 || !"Cannot swap a container with itself") ;
	assert((orbit1 != DART_ORBIT && orbit2 != DART_ORBIT) || !"Cannot swap the darts container") ;
240

Pierre Kraemer's avatar
Pierre Kraemer committed
241
	m_attribs[orbit1].swap(m_attribs[orbit2]) ;
242 243 244
	m_attribs[orbit1].setOrbit(orbit1) ;	// to update the orbit information
	m_attribs[orbit2].setOrbit(orbit2) ;	// in the contained AttributeMultiVectors

245
	m_embeddings[orbit1]->swap(m_embeddings[orbit2]) ;
246

247 248 249 250 251
	for(unsigned int t = 0; t < m_nbThreads; ++t)
	{
		AttributeMultiVector<Mark>* m = m_markerTables[orbit1][t] ;
		m_markerTables[orbit1][t] = m_markerTables[orbit2][t] ;
		m_markerTables[orbit2][t] = m ;
252

253 254 255 256
		MarkerSet ms = m_orbMarker[orbit1][t] ;
		m_orbMarker[orbit1][t] = m_orbMarker[orbit2][t] ;
		m_orbMarker[orbit2][t] = ms ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259 260 261 262
}

/****************************************
 *          MARKERS MANAGEMENT          *
 ****************************************/

Sylvain Thery's avatar
Sylvain Thery committed
263
inline Marker GenericMap::getNewMarker(unsigned int cell, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
264
{
Sylvain Thery's avatar
Sylvain Thery committed
265
	return m_orbMarker[cell][thread].getNewMarker(cell) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
266 267
}

Sylvain Thery's avatar
Sylvain Thery committed
268
inline void GenericMap::releaseMarker(Marker m, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
269 270 271
{
	unsigned int cell = m.getCell();
	assert(isOrbitEmbedded(cell) || !"Try to release a marker on non embedded cell! ") ;
Sylvain Thery's avatar
Sylvain Thery committed
272
	m_orbMarker[cell][thread].releaseMarker(m) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
}

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

inline Dart GenericMap::begin()
{
	return Dart::create(m_attribs[DART_ORBIT].begin());
}

inline Dart GenericMap::end()
{
	return Dart::create(m_attribs[DART_ORBIT].end()) ;
}

inline void GenericMap::next(Dart& d)
{
	m_attribs[DART_ORBIT].next(d.index) ;
}

} //namespace CGoGN