ihm.hpp 11.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
25
26
27
28
29
30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

31
32
33
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
34
35
36
37
38
39
40
namespace IHM
{

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

Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
template <typename T, unsigned int ORBIT>
AttributeHandler_IHM<T, ORBIT> ImplicitHierarchicalMap::addAttribute(const std::string& nameAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
43
44
{
	bool addNextLevelCell = false ;
45
	if(!isOrbitEmbedded<ORBIT>())
Pierre Kraemer's avatar
Pierre Kraemer committed
46
47
		addNextLevelCell = true ;

Pierre Kraemer's avatar
Pierre Kraemer committed
48
	AttributeHandler<T, ORBIT> h = Map2::addAttribute<T, ORBIT>(nameAttr) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51

	if(addNextLevelCell)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
52
		AttributeContainer& cellCont = m_attribs[ORBIT] ;
53
		AttributeMultiVector<unsigned int>* amv = cellCont.addAttribute<unsigned int>("nextLevelCell") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
54
		m_nextLevelCell[ORBIT] = amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
55
		for(unsigned int i = cellCont.begin(); i < cellCont.end(); cellCont.next(i))
56
			amv->operator[](i) = EMBNULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
57
58
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
59
	return AttributeHandler_IHM<T, ORBIT>(this, h.getDataVector()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
60
61
}

Pierre Kraemer's avatar
Pierre Kraemer committed
62
63
template <typename T, unsigned int ORBIT>
AttributeHandler_IHM<T, ORBIT> ImplicitHierarchicalMap::getAttribute(const std::string& nameAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
{
Pierre Kraemer's avatar
Pierre Kraemer committed
65
66
	AttributeHandler<T, ORBIT> h = Map2::getAttribute<T, ORBIT>(nameAttr) ;
	return AttributeHandler_IHM<T, ORBIT>(this, h.getDataVector()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
67
68
}

Pierre Kraemer's avatar
Pierre Kraemer committed
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
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
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************
 *                 MAP TRAVERSAL                   *
 ***************************************************/

inline Dart ImplicitHierarchicalMap::newDart()
{
	Dart d = Map2::newDart() ;
	m_dartLevel[d] = m_curLevel ;
	if(m_curLevel > m_maxLevel)			// update max level
		m_maxLevel = m_curLevel ;		// if needed
	return d ;
}

inline Dart ImplicitHierarchicalMap::phi1(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	bool finished = false ;
	unsigned int edgeId = m_edgeId[d] ;
	Dart it = d ;
	do
	{
		it = Map2::phi1(it) ;
		if(m_dartLevel[it] <= m_curLevel)
			finished = true ;
		else
		{
			while(m_edgeId[it] != edgeId)
				it = Map2::alpha_1(it) ;
		}
	} while(!finished) ;
	return it ;
}

inline Dart ImplicitHierarchicalMap::phi_1(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	bool finished = false ;
	Dart it = Map2::phi_1(d) ;
	unsigned int edgeId = m_edgeId[it] ;
	do
	{
		if(m_dartLevel[it] <= m_curLevel)
			finished = true ;
		else
		{
			it = Map2::phi_1(it) ;
			while(m_edgeId[it] != edgeId)
				it = Map2::phi_1(Map2::phi2(it)) ;
		}
	} while(!finished) ;
	return it ;
}

inline Dart ImplicitHierarchicalMap::phi2(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	if(Map2::phi2(d) == d)
		return d ;
	return Map2::alpha1(phi1(d)) ;
}

inline Dart ImplicitHierarchicalMap::alpha0(Dart d)
{
	return phi2(d) ;
}

inline Dart ImplicitHierarchicalMap::alpha1(Dart d)
{
	return Map2::alpha1(d) ;
}

inline Dart ImplicitHierarchicalMap::alpha_1(Dart d)
{
	return Map2::alpha_1(d) ;
}

145
inline Dart ImplicitHierarchicalMap::begin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
{
	Dart d = Map2::begin() ;
148
	while(d != Map2::end() && m_dartLevel[d] > m_curLevel)
Pierre Kraemer's avatar
Pierre Kraemer committed
149
150
151
152
		Map2::next(d) ;
	return d ;
}

153
inline Dart ImplicitHierarchicalMap::end() const
Pierre Kraemer's avatar
Pierre Kraemer committed
154
155
156
157
{
	return Map2::end() ;
}

158
inline void ImplicitHierarchicalMap::next(Dart& d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
159
160
161
162
{
	do
	{
		Map2::next(d) ;
163
	} while(d != Map2::end() && m_dartLevel[d] > m_curLevel) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
}

166
inline bool ImplicitHierarchicalMap::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
172
173
174
175
176
177
{
	Dart dNext = d;
	do
	{
		if (f(dNext))
			return true;
		dNext = alpha1(dNext);
 	} while (dNext != d);
 	return false;
}

178
inline bool ImplicitHierarchicalMap::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
179
180
181
182
183
184
185
186
187
188
189
{
	if (f(d))
		return true;

	Dart d2 = phi2(d);
	if (d2 != d)
		return f(d2);
	else
		return false;
}

190
inline bool ImplicitHierarchicalMap::foreach_dart_of_oriented_face(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
191
192
193
194
195
196
197
198
199
200
201
{
	Dart dNext = d ;
	do
	{
		if (f(dNext))
			return true ;
		dNext = phi1(dNext) ;
	} while (dNext != d) ;
	return false ;
}

202
inline bool ImplicitHierarchicalMap::foreach_dart_of_face(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
203
204
205
206
{
	return foreach_dart_of_oriented_face(d, f) ;
}

207
inline bool ImplicitHierarchicalMap::foreach_dart_of_oriented_volume(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{
	DartMarkerStore mark(*this);	// Lock a marker
	bool found = false;				// Last functor return value

	std::list<Dart> visitedFaces;	// Faces that are traversed
	visitedFaces.push_back(d);		// Start with the face of d
	std::list<Dart>::iterator face;

	// For every face added to the list
	for (face = visitedFaces.begin(); !found && face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))		// Face has not been visited yet
		{
			// Apply functor to the darts of the face
			found = foreach_dart_of_oriented_face(*face, f);

			// If functor returns false then mark visited darts (current face)
			// and add non visited adjacent faces to the list of face
			if (!found)
			{
				Dart dNext = *face ;
				do
				{
					mark.mark(dNext);					// Mark
					Dart adj = phi2(dNext);				// Get adjacent face
					if (adj != dNext && !mark.isMarked(adj))
						visitedFaces.push_back(adj);	// Add it
					dNext = phi1(dNext);
				} while(dNext != *face);
			}
		}
	}
	return found;
}

243
inline bool ImplicitHierarchicalMap::foreach_dart_of_volume(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
244
245
246
247
{
	return foreach_dart_of_oriented_volume(d, f) ;
}

248
inline bool ImplicitHierarchicalMap::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
249
250
251
252
{
	return foreach_dart_of_oriented_volume(d, f) ;
}

253
254
255
256
257
258
/***************************************************
 *               MAP MANIPULATION                  *
 ***************************************************/

inline void ImplicitHierarchicalMap::splitFace(Dart d, Dart e)
{
259
	EmbeddedMap2::splitFace(d, e) ;
260
	if(isOrbitEmbedded<FACE>())
261
262
263
	{
		unsigned int cur = m_curLevel ;
		m_curLevel = m_maxLevel ;
264
265
		this->setOrbitEmbedding<FACE>(d, this->getEmbedding<FACE>(d)) ;
		this->setOrbitEmbedding<FACE>(e, this->getEmbedding<FACE>(e)) ;
266
267
		m_curLevel = cur ;
	}
268
269
}

Pierre Kraemer's avatar
Pierre Kraemer committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/***************************************************
 *              LEVELS MANAGEMENT                  *
 ***************************************************/

inline unsigned int ImplicitHierarchicalMap::getCurrentLevel()
{
	return m_curLevel ;
}

inline void ImplicitHierarchicalMap::setCurrentLevel(unsigned int l)
{
	assert(l >= 0 || !"Trying to set current level to a negative value") ;
	m_curLevel = l ;
}

inline unsigned int ImplicitHierarchicalMap::getMaxLevel()
{
	return m_maxLevel ;
}

inline unsigned int ImplicitHierarchicalMap::getDartLevel(Dart d)
{
	return m_dartLevel[d] ;
}

inline void ImplicitHierarchicalMap::setDartLevel(Dart d, unsigned int l)
{
	m_dartLevel[d] = l ;
}

/***************************************************
 *             EDGE ID MANAGEMENT                  *
 ***************************************************/

inline unsigned int ImplicitHierarchicalMap::getNewEdgeId()
{
	return m_idCount++ ;
}

inline unsigned int ImplicitHierarchicalMap::getEdgeId(Dart d)
{
	return m_edgeId[d] ;
}

inline void ImplicitHierarchicalMap::setEdgeId(Dart d, unsigned int i)
{
	m_edgeId[d] = i ;
}

/***************************************************
 *               CELLS INFORMATION                 *
 ***************************************************/

inline unsigned int ImplicitHierarchicalMap::vertexInsertionLevel(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	return m_dartLevel[d] ;
}

inline unsigned int ImplicitHierarchicalMap::edgeLevel(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	unsigned int ld = m_dartLevel[d] ;
//	unsigned int ldd = m_dartLevel[phi2(d)] ;	// the level of an edge is the maximum of the
	unsigned int ldd = m_dartLevel[phi1(d)] ;
	return ld < ldd ? ldd : ld ;				// insertion levels of its two darts
}

338
339
340
341
/***************************************************
 *               ATTRIBUTE HANDLER                 *
 ***************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
342
343
template <typename T, unsigned int ORBIT>
T& AttributeHandler_IHM<T, ORBIT>::operator[](Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
344
345
346
347
348
{
	ImplicitHierarchicalMap* m = reinterpret_cast<ImplicitHierarchicalMap*>(this->m_map) ;
	assert(m->m_dartLevel[d] <= m->m_curLevel || !"Access to a dart introduced after current level") ;
	assert(m->vertexInsertionLevel(d) <= m->m_curLevel || !"Access to the embedding of a vertex inserted after current level") ;

349
	unsigned int orbit = this->getOrbit() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
350
	unsigned int nbSteps = m->m_curLevel - m->vertexInsertionLevel(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
351
	unsigned int index = m->getEmbedding<ORBIT>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
352

353
	if(index == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
354
	{
355
		index = m->setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
356
357
358
		m->m_nextLevelCell[orbit]->operator[](index) = EMBNULL ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
359
	AttributeContainer& cont = m->getAttributeContainer<ORBIT>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
360
361
362
363
364
365
366
	unsigned int step = 0 ;
	while(step < nbSteps)
	{
		step++ ;
		unsigned int nextIdx = m->m_nextLevelCell[orbit]->operator[](index) ;
		if (nextIdx == EMBNULL)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
367
368
			nextIdx = m->newCell<ORBIT>() ;
			m->copyCell<ORBIT>(nextIdx, index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
369
370
371
372
373
374
375
376
377
			m->m_nextLevelCell[orbit]->operator[](index) = nextIdx ;
			m->m_nextLevelCell[orbit]->operator[](nextIdx) = EMBNULL ;
			cont.refLine(index) ;
		}
		index = nextIdx ;
	}
	return this->m_attrib->operator[](index);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
378
379
template <typename T, unsigned int ORBIT>
const T& AttributeHandler_IHM<T, ORBIT>::operator[](Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
380
381
382
383
384
{
	ImplicitHierarchicalMap* m = reinterpret_cast<ImplicitHierarchicalMap*>(this->m_map) ;
	assert(m->m_dartLevel[d] <= m->m_curLevel || !"Access to a dart introduced after current level") ;
	assert(m->vertexInsertionLevel(d) <= m->m_curLevel || !"Access to the embedding of a vertex inserted after current level") ;

385
	unsigned int orbit = this->getOrbit() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
386
	unsigned int nbSteps = m->m_curLevel - m->vertexInsertionLevel(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
387
	unsigned int index = m->getEmbedding<ORBIT>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
388
389
390
391
392
393
394
395
396
397
398
399
400

	unsigned int step = 0 ;
	while(step < nbSteps)
	{
		step++ ;
		unsigned int next = m->m_nextLevelCell[orbit]->operator[](index) ;
		if(next != EMBNULL) index = next ;
		else break ;
	}
	return this->m_attrib->operator[](index);
}

} //namespace IHM
401
} // Surface
Pierre Kraemer's avatar
Pierre Kraemer committed
402
403
404
} //namespace Algo

} //namespace CGoGN