genericmap.hpp 8.48 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) ;
		}
57
		m_markerTables[DART_ORBIT]->operator[](d.index).clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
59 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
}

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
100 101 102 103 104
	class FunctorGetLazyEmb: public FunctorType
	{
	protected:
		GenericMap& m_map;
		unsigned int m_orbit;
105
		AttributeMultiVector<unsigned int>* m_emb;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		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)
			{
132
				AttributeContainer& cont = m_map.getAttributeContainer(m_orbit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
133 134 135 136 137 138 139 140 141 142 143 144
				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
145 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
	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         *
 ****************************************/

219 220 221 222 223
inline AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit)
{
	return m_attribs[orbit] ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
224
template <typename T>
225
inline AttributeMultiVector<T>& GenericMap::getAttributeVector(unsigned int idAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
226
{
227 228
	assert(idAttr != AttributeContainer::UNKNOWN) ;
	return m_attribs[AttributeContainer::orbitAttr(idAttr)].getDataVector<T>(AttributeContainer::indexAttr(idAttr)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
229 230
}

231
inline AttributeMultiVectorGen& GenericMap::getAttributeVectorGen(unsigned int idAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
232
{
233 234
	assert(idAttr != AttributeContainer::UNKNOWN) ;
	return m_attribs[AttributeContainer::orbitAttr(idAttr)].getVirtualDataVector(AttributeContainer::indexAttr(idAttr)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
235 236
}

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

242
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector(unsigned int orbit)
Pierre Kraemer's avatar
Pierre Kraemer committed
243
{
244
	return m_embeddings[orbit] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
245 246 247 248 249 250 251 252
}

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

inline Marker GenericMap::getNewMarker(unsigned int cell)
{
253
//	assert(isOrbitEmbedded(cell) || !"Try to get a marker on non embedded cell! ") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	return m_orbMarker[cell].getNewMarker(cell) ;
}

inline void GenericMap::releaseMarker(Marker m)
{
	unsigned int cell = m.getCell();
	assert(isOrbitEmbedded(cell) || !"Try to release a marker on non embedded cell! ") ;
	m_orbMarker[cell].releaseMarker(m) ;
}

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