map1.hpp 9.15 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

Pierre Kraemer's avatar
Pierre Kraemer committed
28 29
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::init()
Pierre Kraemer's avatar
Pierre Kraemer committed
30
{
Pierre Kraemer's avatar
Pierre Kraemer committed
31
	MAP_IMPL::addPermutation() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
32 33
}

Pierre Kraemer's avatar
Pierre Kraemer committed
34 35
template <typename MAP_IMPL>
inline Map1<MAP_IMPL>::Map1() : MapCommon<MAP_IMPL>()
36 37 38 39
{
	init() ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
template <typename MAP_IMPL>
inline std::string Map1<MAP_IMPL>::mapTypeName() const
Pierre Kraemer's avatar
Pierre Kraemer committed
42
{
43
	return "Map1" ;
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45
}

Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
template <typename MAP_IMPL>
inline unsigned int Map1<MAP_IMPL>::dimension() const
Pierre Kraemer's avatar
Pierre Kraemer committed
48
{
49 50 51
	return 1 ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
52 53
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::clear(bool removeAttrib)
54
{
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	ParentMap::clear(removeAttrib) ;
Sylvain Thery's avatar
Sylvain Thery committed
56 57
	if (removeAttrib)
		init() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59
}

Pierre Kraemer's avatar
Pierre Kraemer committed
60
template <typename MAP_IMPL>
Pierre Kraemer's avatar
Pierre Kraemer committed
61
inline unsigned int Map1<MAP_IMPL>::getNbInvolutions() const
Sylvain Thery's avatar
Sylvain Thery committed
62
{
Pierre Kraemer's avatar
Pierre Kraemer committed
63 64 65 66 67 68 69
	return 0;
}

template <typename MAP_IMPL>
inline unsigned int Map1<MAP_IMPL>::getNbPermutations() const
{
	return 1;
Sylvain Thery's avatar
Sylvain Thery committed
70 71
}

Pierre Kraemer's avatar
Pierre Kraemer committed
72 73 74 75
/*! @name Basic Topological Operators
 * Access and Modification
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
template <typename MAP_IMPL>
inline Dart Map1<MAP_IMPL>::phi1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
78
{
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	return MAP_IMPL::template getPermutation<0>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81
}

Pierre Kraemer's avatar
Pierre Kraemer committed
82 83
template <typename MAP_IMPL>
inline Dart Map1<MAP_IMPL>::phi_1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
84
{
Pierre Kraemer's avatar
Pierre Kraemer committed
85
	return MAP_IMPL::template getPermutationInv<0>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87
}

Pierre Kraemer's avatar
Pierre Kraemer committed
88
template <typename MAP_IMPL>
Pierre Kraemer's avatar
Pierre Kraemer committed
89
template <int N>
Pierre Kraemer's avatar
Pierre Kraemer committed
90
inline Dart Map1<MAP_IMPL>::phi(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	assert((N > 0) || !"negative parameters not allowed in template multi-phi");
	if (N < 10)
	{
		switch(N)
		{
			case 1 : return phi1(d) ;
			default : assert(!"Wrong multi-phi relation value") ; return d ;
		}
	}
	switch(N%10)
	{
		case 1 : return phi1(phi<N/10>(d)) ;
		default : assert(!"Wrong multi-phi relation value") ; return d ;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
108 109
template <typename MAP_IMPL>
inline Dart Map1<MAP_IMPL>::alpha1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113
{
	return phi1(d) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
114 115
template <typename MAP_IMPL>
inline Dart Map1<MAP_IMPL>::alpha_1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117 118 119
{
	return phi_1(d) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::phi1sew(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
{
Pierre Kraemer's avatar
Pierre Kraemer committed
123
	MAP_IMPL::template permutationSew<0>(d,e);
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125
}

Pierre Kraemer's avatar
Pierre Kraemer committed
126 127
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::phi1unsew(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
128
{
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	MAP_IMPL::template permutationUnsew<0>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131
}

132 133 134 135
/*! @name Generator and Deletor
 *  To generate or delete faces in a 1-map
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
136 137
template <typename MAP_IMPL>
Dart Map1<MAP_IMPL>::newCycle(unsigned int nbEdges)
138 139 140 141
{
	assert(nbEdges > 0 || !"Cannot create a face with no edge") ;
	Dart d = this->newDart() ;	// Create the first edge
	for (unsigned int i = 1 ; i < nbEdges ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
142
		Map1<MAP_IMPL>::cutEdge(d) ;		// Subdivide nbEdges-1 times this edge
143 144 145
	return d ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
template <typename MAP_IMPL>
void Map1<MAP_IMPL>::deleteCycle(Dart d)
148 149 150 151 152 153 154 155 156 157 158
{
	Dart e = phi1(d) ;
	while (e != d)
	{
		Dart f = phi1(e) ;
		this->deleteDart(e) ;
		e = f ;
	}
	this->deleteDart(d) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162
/*! @name Topological Operators
 *  Topological operations on 1-maps
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
163 164
template <typename MAP_IMPL>
inline Dart Map1<MAP_IMPL>::cutEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
165
{
166
	Dart e = this->newDart() ;	// Create a new dart
167
	phi1sew(d, e) ;				// Insert dart e between d and phi1(d)
Sylvain Thery's avatar
Sylvain Thery committed
168

Pierre Kraemer's avatar
Pierre Kraemer committed
169 170
	if (this->template isBoundaryMarked<2>(d))
		this->template boundaryMark<2>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
171

Pierre Kraemer's avatar
Pierre Kraemer committed
172 173
	if (this->template isBoundaryMarked<3>(d))
		this->template boundaryMark<3>(e);
174

Pierre Kraemer's avatar
Pierre Kraemer committed
175
	return e ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177
}

Pierre Kraemer's avatar
Pierre Kraemer committed
178 179
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::uncutEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181
{
	Dart d1 = phi1(d) ;
182
	phi1unsew(d) ;			// Dart d is linked to the successor of its successor
183
	this->deleteDart(d1) ;	// Dart d1 is erased
Pierre Kraemer's avatar
Pierre Kraemer committed
184 185
}

Pierre Kraemer's avatar
Pierre Kraemer committed
186 187
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::collapseEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189
{
	phi1unsew(phi_1(d)) ;	// Dart before d is linked to its successor
190
	this->deleteDart(d) ;	// Dart d is erased
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192
}

Pierre Kraemer's avatar
Pierre Kraemer committed
193 194
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::splitCycle(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
195
{
196 197
	assert(d != e && sameCycle(d, e)) ;
	phi1sew(phi_1(d), phi_1(e)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199
}

Pierre Kraemer's avatar
Pierre Kraemer committed
200 201
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::mergeCycles(Dart d, Dart e)
Thomas's avatar
Thomas committed
202
{
203 204
	assert(!sameCycle(d, e)) ;
	phi1sew(phi_1(d), phi_1(e)) ;
Thomas's avatar
Thomas committed
205 206
}

Pierre Kraemer's avatar
Pierre Kraemer committed
207 208
template <typename MAP_IMPL>
inline void Map1<MAP_IMPL>::linkCycles(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
209
{
210
	assert(d != e && !sameCycle(d, e)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212
	Map1<MAP_IMPL>::cutEdge(phi_1(d));		// cut the edge before d (insert a new dart before d)
	Map1<MAP_IMPL>::cutEdge(phi_1(e));		// cut the edge before e (insert a new dart before e)
213
	phi1sew(phi_1(d), phi_1(e)) ;	// phi1sew between the 2 new inserted darts
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215
}

Pierre Kraemer's avatar
Pierre Kraemer committed
216 217
template <typename MAP_IMPL>
void Map1<MAP_IMPL>::reverseCycle(Dart d)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
	Dart e = phi1(d) ;			// Dart e is the first edge of the new face
	if (e == d) return ;		// Only one edge: nothing to do
	if (phi1(e) == d) return ;	// Only two edges: nothing to do

	phi1unsew(d) ;				// Detach e from the face of d

	Dart dNext = phi1(d) ;		// While the face of d contains more than two edges
	while (dNext != d)
	{
		phi1unsew(d) ;			// Unsew the edge after d
		phi1sew(e, dNext) ;		// Sew it after e (thus in reverse order)
		dNext = phi1(d) ;
	}
	phi1sew(e, d) ;				// Sew the last edge
}

Pierre Kraemer's avatar
Pierre Kraemer committed
235 236 237 238
/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
239 240
template <typename MAP_IMPL>
inline bool Map1<MAP_IMPL>::sameCycle(Dart d, Dart e) const
Pierre Kraemer's avatar
Pierre Kraemer committed
241
{
242 243 244 245 246 247 248 249 250 251
	Dart it = d ;
	do
	{
		if(it == e)
			return true ;
		it = phi1(it) ;
	} while(it != d) ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
252 253
template <typename MAP_IMPL>
inline unsigned int Map1<MAP_IMPL>::cycleDegree(Dart d) const
254 255 256 257
{
	unsigned int count = 0 ;
	Dart it = d ;
	do
258
    {
259 260 261 262 263 264
		++count ;
		it = phi1(it) ;
	} while (it != d) ;
	return count ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
265 266
template <typename MAP_IMPL>
inline int Map1<MAP_IMPL>::checkCycleDegree(Dart d, unsigned int degree) const
267 268 269 270 271 272 273
{
	unsigned int count = 0 ;
	Dart it = d ;
	do
	{
		++count ;
		it = phi1(it) ;
274
	} while ((count <= degree) && (it != d)) ;
275 276 277
	return count-degree;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
278 279
template <typename MAP_IMPL>
inline bool Map1<MAP_IMPL>::isCycleTriangle(Dart d) const
280 281
{
	return (phi1(d) != d) && (phi1(phi1(phi1(d))) == d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283 284 285 286 287
}

/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/

288 289 290 291 292 293 294 295 296 297 298 299
//template <typename MAP_IMPL>
//template <unsigned int ORBIT, typename FUNC>
//void Map1<MAP_IMPL>::foreach_dart_of_orbit(Cell<ORBIT> c, FUNC 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;
//		default: 		assert(!"Cells of this dimension are not handled"); break;
//	}
//}
300 301 302

template <typename MAP_IMPL>
template <unsigned int ORBIT, typename FUNC>
303
void Map1<MAP_IMPL>::foreach_dart_of_orbit(Cell<ORBIT> c, const FUNC& f, unsigned int thread) const
304 305 306 307 308 309 310 311 312 313 314 315 316
{
	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;
		default: 		assert(!"Cells of this dimension are not handled"); break;
	}
}

template <typename MAP_IMPL>
template <typename FUNC>
inline void Map1<MAP_IMPL>::foreach_dart_of_vertex(Dart d, FUNC& f, unsigned int /*thread*/) const
Pierre Kraemer's avatar
Pierre Kraemer committed
317
{
318
	f(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
319 320
}

Pierre Kraemer's avatar
Pierre Kraemer committed
321
template <typename MAP_IMPL>
322 323
template <typename FUNC>
inline void Map1<MAP_IMPL>::foreach_dart_of_edge(Dart d, FUNC& f, unsigned int /*thread*/) const
Pierre Kraemer's avatar
Pierre Kraemer committed
324
{
325
	f(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
326 327
}

Pierre Kraemer's avatar
Pierre Kraemer committed
328
template <typename MAP_IMPL>
329 330
template <typename FUNC>
inline void Map1<MAP_IMPL>::foreach_dart_of_cc(Dart d, FUNC& f, unsigned int /*thread*/) const
331 332 333 334
{
	Dart it = d ;
	do
	{
335
		f(it);
336 337 338 339
		it = phi1(it) ;
	} while (it != d) ;
}

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