genericmap.hpp 9.48 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
#include "Topology/generic/dartmarker.h"
26 27
#include "Topology/generic/traversor/traversorCell.h"
#include "Topology/generic/traversor/traversorFactory.h"
28

Pierre Kraemer's avatar
Pierre Kraemer committed
29 30
namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
31

Pierre Kraemer's avatar
Pierre Kraemer committed
32 33 34 35 36 37
/****************************************
 *           DARTS MANAGEMENT           *
 ****************************************/

inline Dart GenericMap::newDart()
{
38
	unsigned int di = m_attribs[DART].insertLine();		// insert a new dart line
Pierre Kraemer's avatar
Pierre Kraemer committed
39
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
40
	{
41 42
		if (m_embeddings[i])							// set all its embeddings
			(*m_embeddings[i])[di] = EMBNULL ;			// to EMBNULL
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45
	return Dart::create(di) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
}

Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51 52
inline void GenericMap::deleteDartLine(unsigned int index)
{
	m_attribs[DART].removeLine(index) ;	// free the dart line

	for (unsigned int t = 0; t < m_nbThreads; ++t)	// clear markers of
53
		(*m_markTables[DART][t])[index].clear() ;	// the removed dart
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
54

Pierre Kraemer's avatar
Pierre Kraemer committed
55 56
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
57
		if (m_embeddings[orbit])									// for each embedded orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
58
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
59
			unsigned int emb = (*m_embeddings[orbit])[index] ;		// get the embedding of the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
60
			if(emb != EMBNULL)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
61
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
62
				if(m_attribs[orbit].unrefLine(emb))					// unref the pointed embedding line
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
63
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
64
					for (unsigned int t = 0; t < m_nbThreads; ++t)	// and clear its markers if it was
65
						(*m_markTables[orbit][t])[emb].clear() ;	// its last unref (and was thus freed)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
66 67
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
70
}
Pierre Kraemer's avatar
Pierre Kraemer committed
71

Pierre Kraemer's avatar
Pierre Kraemer committed
72
inline unsigned int GenericMap::copyDartLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
73
{
74 75
	unsigned int newindex = m_attribs[DART].insertLine() ;	// create a new dart line
	m_attribs[DART].copyLine(newindex, index) ;				// copy the given dart line
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
78 79 80 81 82 83
		if (m_embeddings[orbit])
		{
			unsigned int emb = (*m_embeddings[orbit])[newindex] ;	// add a ref to the cells pointed
			if(emb != EMBNULL)										// by the new dart line
				m_attribs[orbit].refLine(emb) ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85
	}
	return newindex ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87
}

88 89 90 91
//inline bool GenericMap::isDartValid(Dart d)
//{
//	return !d.isNil() && m_attribs[DART].used(dartIndex(d)) ;
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
92

Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96
/****************************************
 *         EMBEDDING MANAGEMENT         *
 ****************************************/

97 98
template <unsigned int ORBIT>
inline bool GenericMap::isOrbitEmbedded() const
Pierre Kraemer's avatar
Pierre Kraemer committed
99
{
100
	return (ORBIT == DART) || (m_embeddings[ORBIT] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
101 102
}

103
inline bool GenericMap::isOrbitEmbedded(unsigned int orbit) const
Pierre Kraemer's avatar
Pierre Kraemer committed
104
{
105
	return (orbit == DART) || (m_embeddings[orbit] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107
}

108 109
template <unsigned int ORBIT>
inline unsigned int GenericMap::newCell()
Pierre Kraemer's avatar
Pierre Kraemer committed
110
{
111
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
112
	return m_attribs[ORBIT].insertLine();
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114
}

115 116
template <unsigned int ORBIT>
inline void GenericMap::copyCell(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
117
{
118
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
119
	m_attribs[ORBIT].copyLine(i, j) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
}

122 123
template <unsigned int ORBIT>
inline void GenericMap::initCell(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
124
{
125
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
126
	m_attribs[ORBIT].initLine(i) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128
}

Pierre Kraemer's avatar
Pierre Kraemer committed
129
/****************************************
Pierre Kraemer's avatar
Pierre Kraemer committed
130
 *   ATTRIBUTES CONTAINERS MANAGEMENT   *
Pierre Kraemer's avatar
Pierre Kraemer committed
131 132
 ****************************************/

133 134 135 136 137
inline unsigned int GenericMap::getNbCells(unsigned int orbit)
{
	return m_attribs[orbit].size() ;
}

138 139
template <unsigned int ORBIT>
inline AttributeContainer& GenericMap::getAttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
140
{
141
	return m_attribs[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143
}

Sylvain Thery's avatar
Sylvain Thery committed
144 145 146 147 148 149
template <unsigned int ORBIT>
inline const AttributeContainer& GenericMap::getAttributeContainer() const
{
	return m_attribs[ORBIT] ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152 153 154
inline AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit)
{
	return m_attribs[orbit] ;
}

Sylvain Thery's avatar
Sylvain Thery committed
155 156 157 158 159
inline const AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit) const
{
	return m_attribs[orbit] ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
160 161 162 163 164
inline AttributeMultiVectorGen* GenericMap::getAttributeVectorGen(unsigned int orbit, const std::string& nameAttr)
{
	return m_attribs[orbit].getVirtualDataVector(nameAttr) ;
}

165 166
template <unsigned int ORBIT>
inline AttributeMultiVector<Mark>* GenericMap::getMarkVector(unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
167
{
168
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;
169
	return m_markTables[ORBIT][thread] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
170 171
}

172 173
template <unsigned int ORBIT>
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
174
{
175
	return m_embeddings[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
template <typename R>
bool GenericMap::registerAttribute(const std::string &nameType)
{
	RegisteredBaseAttribute* ra = new RegisteredAttribute<R>;
	if (ra == NULL)
	{
		CGoGNerr << "Erreur enregistrement attribut" << CGoGNendl;
		return false;
	}

	ra->setTypeName(nameType);

	m_attributes_registry_map->insert(std::pair<std::string, RegisteredBaseAttribute*>(nameType,ra));
	return true;
}

194 195 196 197 198 199 200
/****************************************
 *   EMBEDDING ATTRIBUTES MANAGEMENT    *
 ****************************************/

template <unsigned int ORBIT>
void GenericMap::addEmbedding()
{
201 202 203 204
	if (!isOrbitEmbedded<ORBIT>())
	{
		std::ostringstream oss;
		oss << "EMB_" << ORBIT;
205

206 207 208
		AttributeContainer& dartCont = m_attribs[DART] ;
		AttributeMultiVector<unsigned int>* amv = dartCont.addAttribute<unsigned int>(oss.str()) ;
		m_embeddings[ORBIT] = amv ;
209

210 211 212 213
		// set new embedding to EMBNULL for all the darts of the map
		for(unsigned int i = dartCont.begin(); i < dartCont.end(); dartCont.next(i))
			(*amv)[i] = EMBNULL ;
	}
214 215
}

Pierre Kraemer's avatar
Pierre Kraemer committed
216
/****************************************
Pierre Kraemer's avatar
Pierre Kraemer committed
217
 *          ORBITS TRAVERSALS           *
Pierre Kraemer's avatar
Pierre Kraemer committed
218 219
 ****************************************/

220
template <unsigned int ORBIT>
221
void GenericMap::foreach_dart_of_orbit(Cell<ORBIT> c, std::function<void (Dart)> f, unsigned int thread) const
222 223 224
{
	switch(ORBIT)
	{
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
		case DART:		f(c); break;
		case VERTEX: 	foreach_dart_of_vertex(c, f, thread); break;
		case EDGE: 		foreach_dart_of_edge(c, f, thread); break;
		case FACE: 		foreach_dart_of_face(c, f, thread); break;
		case VOLUME: 	foreach_dart_of_volume(c, f, thread); break;
		case VERTEX1: 	foreach_dart_of_vertex1(c, f, thread); break;
		case EDGE1: 	foreach_dart_of_edge1(c, f, thread); break;
		case VERTEX2: 	foreach_dart_of_vertex2(c, f, thread); break;
		case EDGE2:		foreach_dart_of_edge2(c, f, thread); break;
		case FACE2:		foreach_dart_of_face2(c, f, thread); break;
		default: 		assert(!"Cells of this dimension are not handled"); break;
	}
}

template <unsigned int ORBIT>
void GenericMap::foreach_dart_of_orbit(Cell<ORBIT> c, std::function<void (Dart)>& f, unsigned int thread) const
{
	switch(ORBIT)
	{
		case DART:		f(c); break;
		case VERTEX: 	foreach_dart_of_vertex(c, f, thread); break;
		case EDGE: 		foreach_dart_of_edge(c, f, thread); break;
		case FACE: 		foreach_dart_of_face(c, f, thread); break;
		case VOLUME: 	foreach_dart_of_volume(c, f, thread); break;
		case VERTEX1: 	foreach_dart_of_vertex1(c, f, thread); break;
		case EDGE1: 	foreach_dart_of_edge1(c, f, thread); break;
		case VERTEX2: 	foreach_dart_of_vertex2(c, f, thread); break;
		case EDGE2:		foreach_dart_of_edge2(c, f, thread); break;
		case FACE2:		foreach_dart_of_face2(c, f, thread); break;
Pierre Kraemer's avatar
Pierre Kraemer committed
254
		default: 		assert(!"Cells of this dimension are not handled"); break;
255 256 257
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
258 259 260 261 262 263 264 265 266 267 268
/****************************************
 *  TOPOLOGICAL ATTRIBUTES MANAGEMENT   *
 ****************************************/

inline AttributeMultiVector<Dart>* GenericMap::addRelation(const std::string& name)
{
	AttributeContainer& cont = m_attribs[DART] ;
	AttributeMultiVector<Dart>* amv = cont.addAttribute<Dart>(name) ;

	// set new relation to fix point for all the darts of the map
	for(unsigned int i = cont.begin(); i < cont.end(); cont.next(i))
269
		(*amv)[i] = Dart(i) ;
Sylvain Thery's avatar
Sylvain Thery committed
270 271 272 273

	return amv ;
}

Sylvain Thery's avatar
Sylvain Thery committed
274 275 276 277 278 279 280
inline AttributeMultiVector<Dart>* GenericMap::getRelation(const std::string& name)
{
	AttributeContainer& cont = m_attribs[DART] ;
	AttributeMultiVector<Dart>* amv = cont.getDataVector<Dart>(cont.getAttributeIndex(name)) ;
	return amv ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
281
} //namespace CGoGN