genericmap.hpp 9.04 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
merge..  
Pierre Kraemer committed
49 50 51
	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
52 53 54 55 56 57
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		if (m_embeddings[orbit])
		{
			unsigned int emb = (*m_embeddings[orbit])[d.index] ;
			if(emb != EMBNULL)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
58 59 60 61 62 63 64
			{
				if(m_attribs[orbit].unrefLine(emb))
				{
					for (unsigned int t = 0; t < m_nbThreads; ++t)
						m_markerTables[orbit][t]->operator[](emb).clear() ;
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
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 101 102 103 104 105 106 107
}

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
108 109 110 111 112
	class FunctorGetLazyEmb: public FunctorType
	{
	protected:
		GenericMap& m_map;
		unsigned int m_orbit;
113
		AttributeMultiVector<unsigned int>* m_emb;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		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)
			{
140
				AttributeContainer& cont = m_map.getAttributeContainer(m_orbit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142 143 144 145 146 147 148 149 150 151 152
				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
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
	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");
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
183
	FunctorSetEmb<GenericMap> fsetemb(*this, orbit, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	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         *
 ****************************************/

227
inline AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit)
Pierre Kraemer's avatar
Pierre Kraemer committed
228
{
229
	return m_attribs[orbit] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231
}

232
inline AttributeMultiVector<Mark>* GenericMap::getMarkerVector(unsigned int orbit, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
233
{
234
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded") ;
Sylvain Thery's avatar
Sylvain Thery committed
235
	return m_markerTables[orbit][thread] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237
}

238
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector(unsigned int orbit)
Pierre Kraemer's avatar
Pierre Kraemer committed
239
{
240
	return m_embeddings[orbit] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242
}

Pierre Kraemer's avatar
Pierre Kraemer committed
243
inline void GenericMap::swapEmbeddingContainers(unsigned int orbit1, unsigned int orbit2)
Pierre Kraemer's avatar
Pierre Kraemer committed
244
{
Pierre Kraemer's avatar
Pierre Kraemer committed
245 246
	assert(orbit1 != orbit2 || !"Cannot swap a container with itself") ;
	assert((orbit1 != DART_ORBIT && orbit2 != DART_ORBIT) || !"Cannot swap the darts container") ;
247

Pierre Kraemer's avatar
Pierre Kraemer committed
248
	m_attribs[orbit1].swap(m_attribs[orbit2]) ;
249 250 251
	m_attribs[orbit1].setOrbit(orbit1) ;	// to update the orbit information
	m_attribs[orbit2].setOrbit(orbit2) ;	// in the contained AttributeMultiVectors

252
	m_embeddings[orbit1]->swap(m_embeddings[orbit2]) ;
253

254 255 256 257 258
	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 ;
259

260 261 262 263
		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
264 265 266 267 268 269
}

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

Sylvain Thery's avatar
Sylvain Thery committed
270
inline Marker GenericMap::getNewMarker(unsigned int cell, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
271
{
Sylvain Thery's avatar
Sylvain Thery committed
272
	return m_orbMarker[cell][thread].getNewMarker(cell) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
273 274
}

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

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