genericmap.hpp 10.8 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
{
Sylvain Thery's avatar
Sylvain Thery committed
31
/****************************************
Sylvain Thery's avatar
Sylvain Thery committed
32
 *         THREAD ID MANAGEMENT         *
Sylvain Thery's avatar
Sylvain Thery committed
33
 ****************************************/
Sylvain Thery's avatar
Sylvain Thery committed
34 35 36 37 38 39 40
inline unsigned int GenericMap::getCurrentThreadIndex() const
{
	std::thread::id id = std::this_thread::get_id();
	unsigned int i=0;
	while (id != m_thread_ids[i])
	{
		i++;
Sylvain Thery's avatar
Sylvain Thery committed
41
		assert(i<m_thread_ids.size());
Sylvain Thery's avatar
Sylvain Thery committed
42 43 44 45
	}
	return i;
}

Sylvain Thery's avatar
Sylvain Thery committed
46 47 48 49 50 51
//inline void GenericMap::addThreadId(const std::thread::id& id)
//{
//	m_thread_ids.push_back(id);
//}

inline unsigned int GenericMap::addEmptyThreadIds(unsigned int n)
Sylvain Thery's avatar
Sylvain Thery committed
52
{
Sylvain Thery's avatar
Sylvain Thery committed
53 54 55
	unsigned int nb = m_thread_ids.size();
	m_thread_ids.resize(nb + n);
	return nb;
Sylvain Thery's avatar
Sylvain Thery committed
56 57 58 59 60 61 62 63
}

inline void GenericMap::popThreadIds(unsigned int nb)
{
	assert(nb<m_thread_ids.size());
	for ( unsigned int i=0; i<nb; ++i)
		m_thread_ids.pop_back();
}
Sylvain Thery's avatar
Sylvain Thery committed
64

Sylvain Thery's avatar
Sylvain Thery committed
65 66 67 68 69
inline std::thread::id& GenericMap::getThreadId(unsigned int j)
{
	return m_thread_ids[j];
}

Sylvain Thery's avatar
Sylvain Thery committed
70 71 72 73 74 75

/****************************************
 *         BUFFERS MANAGEMENT           *
 ****************************************/

inline std::vector<Dart>* GenericMap::askDartBuffer() const
Sylvain Thery's avatar
Sylvain Thery committed
76
{
Sylvain Thery's avatar
Sylvain Thery committed
77 78
	unsigned int thread = getCurrentThreadIndex();

Sylvain Thery's avatar
Sylvain Thery committed
79 80 81 82 83 84 85 86 87 88 89 90
	if (s_vdartsBuffers[thread].empty())
	{
		std::vector<Dart>* vd = new std::vector<Dart>;
		vd->reserve(128);
		return vd;
	}

	std::vector<Dart>* vd = s_vdartsBuffers[thread].back();
	s_vdartsBuffers[thread].pop_back();
	return vd;
}

Sylvain Thery's avatar
Sylvain Thery committed
91
inline void GenericMap::releaseDartBuffer(std::vector<Dart>* vd) const
Sylvain Thery's avatar
Sylvain Thery committed
92
{
Sylvain Thery's avatar
Sylvain Thery committed
93 94
	unsigned int thread = getCurrentThreadIndex();

Sylvain Thery's avatar
Sylvain Thery committed
95 96 97 98 99 100 101 102 103 104 105
	if (vd->capacity()>1024)
	{
		std::vector<Dart> v;
		vd->swap(v);
		vd->reserve(128);
	}
	vd->clear();
	s_vdartsBuffers[thread].push_back(vd);
}


Sylvain Thery's avatar
Sylvain Thery committed
106
inline std::vector<unsigned int>* GenericMap::askUIntBuffer() const
Sylvain Thery's avatar
Sylvain Thery committed
107
{
Sylvain Thery's avatar
Sylvain Thery committed
108 109
	unsigned int thread = getCurrentThreadIndex();

Sylvain Thery's avatar
Sylvain Thery committed
110 111 112 113 114 115 116 117 118 119 120 121
	if (s_vintsBuffers[thread].empty())
	{
		std::vector<unsigned int>* vui = new std::vector<unsigned int>;
		vui->reserve(128);
		return vui;
	}

	std::vector<unsigned int>* vui = s_vintsBuffers[thread].back();
	s_vintsBuffers[thread].pop_back();
	return vui;
}

Sylvain Thery's avatar
Sylvain Thery committed
122
inline void GenericMap::releaseUIntBuffer(std::vector<unsigned int>* vui) const
Sylvain Thery's avatar
Sylvain Thery committed
123
{
Sylvain Thery's avatar
Sylvain Thery committed
124 125
	unsigned int thread = getCurrentThreadIndex();

Sylvain Thery's avatar
Sylvain Thery committed
126 127 128 129 130 131 132 133 134 135 136
	if (vui->capacity()>1024)
	{
		std::vector<unsigned int> v;
		vui->swap(v);
		vui->reserve(128);
	}
	vui->clear();
	s_vintsBuffers[thread].push_back(vui);
}


Pierre Kraemer's avatar
Pierre Kraemer committed
137

Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141 142 143
/****************************************
 *           DARTS MANAGEMENT           *
 ****************************************/

inline Dart GenericMap::newDart()
{
144
	unsigned int di = m_attribs[DART].insertLine();		// insert a new dart line
Pierre Kraemer's avatar
Pierre Kraemer committed
145
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
146
	{
147 148
		if (m_embeddings[i])							// set all its embeddings
			(*m_embeddings[i])[di] = EMBNULL ;			// to EMBNULL
Pierre Kraemer's avatar
Pierre Kraemer committed
149
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
150

Pierre Kraemer's avatar
Pierre Kraemer committed
151
	return Dart::create(di) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
152 153
}

Pierre Kraemer's avatar
Pierre Kraemer committed
154 155 156 157
inline void GenericMap::deleteDartLine(unsigned int index)
{
	m_attribs[DART].removeLine(index) ;	// free the dart line

Pierre Kraemer's avatar
Pierre Kraemer committed
158 159
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
160
		if (m_embeddings[orbit])									// for each embedded orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
161
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
162
			unsigned int emb = (*m_embeddings[orbit])[index] ;		// get the embedding of the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
163
			if(emb != EMBNULL)
Sylvain Thery's avatar
Sylvain Thery committed
164
				m_attribs[orbit].unrefLine(emb);					// and unref the corresponding line
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
167
}
Pierre Kraemer's avatar
Pierre Kraemer committed
168

Pierre Kraemer's avatar
Pierre Kraemer committed
169
inline unsigned int GenericMap::copyDartLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
170
{
171 172
	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
173 174
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
175 176 177 178 179 180
		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
181 182
	}
	return newindex ;
Pierre Kraemer's avatar
Pierre Kraemer committed
183 184
}

185 186 187 188
//inline bool GenericMap::isDartValid(Dart d)
//{
//	return !d.isNil() && m_attribs[DART].used(dartIndex(d)) ;
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
189

Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193
/****************************************
 *         EMBEDDING MANAGEMENT         *
 ****************************************/

194 195
template <unsigned int ORBIT>
inline bool GenericMap::isOrbitEmbedded() const
Pierre Kraemer's avatar
Pierre Kraemer committed
196
{
197
	return (ORBIT == DART) || (m_embeddings[ORBIT] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199
}

200
inline bool GenericMap::isOrbitEmbedded(unsigned int orbit) const
Pierre Kraemer's avatar
Pierre Kraemer committed
201
{
202
	return (orbit == DART) || (m_embeddings[orbit] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204
}

205 206
template <unsigned int ORBIT>
inline unsigned int GenericMap::newCell()
Pierre Kraemer's avatar
Pierre Kraemer committed
207
{
208
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
209
	return m_attribs[ORBIT].insertLine();
Pierre Kraemer's avatar
Pierre Kraemer committed
210 211
}

212 213
template <unsigned int ORBIT>
inline void GenericMap::copyCell(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
214
{
215
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
216
	m_attribs[ORBIT].copyLine(i, j) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
217 218
}

219 220
template <unsigned int ORBIT>
inline void GenericMap::initCell(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
221
{
222
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
223
	m_attribs[ORBIT].initLine(i) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
224 225
}

Pierre Kraemer's avatar
Pierre Kraemer committed
226
/****************************************
Pierre Kraemer's avatar
Pierre Kraemer committed
227
 *   ATTRIBUTES CONTAINERS MANAGEMENT   *
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229
 ****************************************/

230 231 232 233 234
inline unsigned int GenericMap::getNbCells(unsigned int orbit)
{
	return m_attribs[orbit].size() ;
}

235 236
template <unsigned int ORBIT>
inline AttributeContainer& GenericMap::getAttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
237
{
238
	return m_attribs[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
239 240
}

Sylvain Thery's avatar
Sylvain Thery committed
241 242 243 244 245 246
template <unsigned int ORBIT>
inline const AttributeContainer& GenericMap::getAttributeContainer() const
{
	return m_attribs[ORBIT] ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250 251
inline AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit)
{
	return m_attribs[orbit] ;
}

Sylvain Thery's avatar
Sylvain Thery committed
252 253 254 255 256
inline const AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit) const
{
	return m_attribs[orbit] ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259 260 261
inline AttributeMultiVectorGen* GenericMap::getAttributeVectorGen(unsigned int orbit, const std::string& nameAttr)
{
	return m_attribs[orbit].getVirtualDataVector(nameAttr) ;
}

Sylvain Thery's avatar
Sylvain Thery committed
262 263

template <unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
264
AttributeMultiVector<MarkerBool>* GenericMap::askMarkVector()
Sylvain Thery's avatar
Sylvain Thery committed
265 266 267
{
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;

Sylvain Thery's avatar
Sylvain Thery committed
268 269 270
	// get current thread index for table of markers
	unsigned int thread = getCurrentThreadIndex();

Sylvain Thery's avatar
Sylvain Thery committed
271
	if (!m_markVectors_free[ORBIT][thread].empty())
Sylvain Thery's avatar
Sylvain Thery committed
272
	{
Sylvain Thery's avatar
Sylvain Thery committed
273 274 275 276 277 278
		AttributeMultiVector<MarkerBool>* amv = m_markVectors_free[ORBIT][thread].back();
		m_markVectors_free[ORBIT][thread].pop_back();
		return amv;
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
279
		std::lock_guard<std::mutex> lockMV(m_MarkerStorageMutex[ORBIT]);
280 281 282 283 284 285 286 287 288 289

		unsigned int x=m_nextMarkerId++;
		std::string number("___");
		number[2]= '0'+x%10;
		x = x/10;
		number[1]= '0'+x%10;
		x = x/10;
		number[0]= '0'+x%10;

		AttributeMultiVector<MarkerBool>* amv = m_attribs[ORBIT].addAttribute<MarkerBool>("marker_" + orbitName(ORBIT) + number);
Sylvain Thery's avatar
Sylvain Thery committed
290 291 292 293 294
		return amv;
	}
}


295
template <unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
296
inline void GenericMap::releaseMarkVector(AttributeMultiVector<MarkerBool>* amv)
Pierre Kraemer's avatar
Pierre Kraemer committed
297
{
298
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;
Sylvain Thery's avatar
Sylvain Thery committed
299

Sylvain Thery's avatar
Sylvain Thery committed
300 301
	unsigned int thread = getCurrentThreadIndex();

Sylvain Thery's avatar
Sylvain Thery committed
302
	m_markVectors_free[ORBIT][thread].push_back(amv);
Pierre Kraemer's avatar
Pierre Kraemer committed
303 304
}

Sylvain Thery's avatar
Sylvain Thery committed
305 306


307 308
template <unsigned int ORBIT>
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
309
{
310
	return m_embeddings[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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;
}

329 330 331 332 333 334 335
/****************************************
 *   EMBEDDING ATTRIBUTES MANAGEMENT    *
 ****************************************/

template <unsigned int ORBIT>
void GenericMap::addEmbedding()
{
336 337 338 339
	if (!isOrbitEmbedded<ORBIT>())
	{
		std::ostringstream oss;
		oss << "EMB_" << ORBIT;
340

341 342 343
		AttributeContainer& dartCont = m_attribs[DART] ;
		AttributeMultiVector<unsigned int>* amv = dartCont.addAttribute<unsigned int>(oss.str()) ;
		m_embeddings[ORBIT] = amv ;
344

345 346 347 348
		// 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 ;
	}
349 350
}

Pierre Kraemer's avatar
Pierre Kraemer committed
351
/****************************************
Pierre Kraemer's avatar
Pierre Kraemer committed
352
 *          ORBITS TRAVERSALS           *
Pierre Kraemer's avatar
Pierre Kraemer committed
353 354
 ****************************************/

Sylvain Thery's avatar
Sylvain Thery committed
355 356 357 358 359 360 361 362 363 364 365
/****************************************
 *  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))
366
		(*amv)[i] = Dart(i) ;
Sylvain Thery's avatar
Sylvain Thery committed
367 368 369 370

	return amv ;
}

Sylvain Thery's avatar
Sylvain Thery committed
371 372 373 374 375 376 377
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 ;
}

Sylvain Thery's avatar
Sylvain Thery committed
378 379 380 381 382 383 384
inline float GenericMap::fragmentation(unsigned int orbit)
{
	if (isOrbitEmbedded(orbit))
		return m_attribs[orbit].fragmentation();
	return 1.0f;
}

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