basic.h 4.74 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __ALGO_TOPO_BASIC__
#define __ALGO_TOPO_BASIC__

#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/traversor/traversorCell.h"

namespace CGoGN
{

namespace Algo
{

namespace Topo
{

template <unsigned int ORBIT, typename MAP>
unsigned int getNbOrbits(const MAP& map)
{
	unsigned int cpt = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
44
	foreach_cell<ORBIT>(map, [&] (Dart) { ++cpt; }, true);
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	return cpt;
}

template <typename MAP>
unsigned int getNbOrbits(const MAP& map, unsigned int orbit)
{
	switch(orbit)
	{
		case DART:		return getNbOrbits<DART, MAP>(map);
		case VERTEX: 	return getNbOrbits<VERTEX, MAP>(map);
		case EDGE: 		return getNbOrbits<EDGE, MAP>(map);
		case FACE: 		return getNbOrbits<FACE, MAP>(map);
		case VOLUME: 	return getNbOrbits<VOLUME, MAP>(map);
		case VERTEX1: 	return getNbOrbits<VERTEX1, MAP>(map);
		case EDGE1: 	return getNbOrbits<EDGE1, MAP>(map);
		case VERTEX2: 	return getNbOrbits<VERTEX2, MAP>(map);
		case EDGE2:		return getNbOrbits<EDGE2, MAP>(map);
		case FACE2:		return getNbOrbits<FACE2, MAP>(map);
		default: 		assert(!"Cells of this dimension are not handled"); break;
	}
	return 0;
}

/**
 * Traverse the map and embed all orbits of the given dimension with a new cell
 * @param realloc if true -> all the orbits are embedded on new cells, if false -> already embedded orbits are not impacted
 */
template <unsigned int ORBIT, typename MAP>
void initAllOrbitsEmbedding(MAP& map, bool realloc = false)
{
	if(!map.template isOrbitEmbedded<ORBIT>())
		map.template addEmbedding<ORBIT>() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
78
	foreach_cell<ORBIT>(map, [&] (Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80 81
	{
		if(realloc || map.template getEmbedding<ORBIT>(d) == EMBNULL)
			map.template setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
82
	});
Pierre Kraemer's avatar
Pierre Kraemer committed
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
}

/**
 * use the given attribute to store the indices of the cells of the corresponding orbit
 * @return the number of cells of the orbit
 */
template <unsigned int ORBIT, typename MAP>
unsigned int computeIndexCells(MAP& map, AttributeHandler<unsigned int, ORBIT, typename MAP::IMPL>& idx)
{
	AttributeContainer& cont = map.template getAttributeContainer<ORBIT>();
	unsigned int cpt = 0 ;
	for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
		idx[i] = cpt++ ;
	return cpt ;
}

/**
 * ensure that each embedding is pointed by only one orbit
 */
template <unsigned int ORBIT, typename MAP>
void bijectiveOrbitEmbedding(MAP& map)
{
	if(!map.template isOrbitEmbedded<ORBIT>())
		map.template addEmbedding<ORBIT>() ;

	AttributeHandler<int, ORBIT, typename MAP::IMPL> counter = map.template addAttribute<int, ORBIT>("tmpCounter") ;
	counter.setAllValues(int(0)) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
111
	foreach_cell<ORBIT>(map, [&] (Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115 116 117 118 119 120 121 122
	{
		unsigned int emb = map.template getEmbedding<ORBIT>(d) ;
		if (emb != EMBNULL)
		{
			if (counter[d] > 0)
			{
				unsigned int newEmb = map.template setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
				map.template copyCell<ORBIT>(newEmb, emb) ;
			}
			counter[d]++ ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
123 124
	},
	true);
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128 129 130 131 132 133 134 135

	map.removeAttribute(counter) ;
}

} // namespace Topo

} // namespace Algo

} // namespace CGoGN

#endif