attribmap.hpp 5.25 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
{

28 29
template <typename T, unsigned int ORBIT>
inline AttributeHandler<T, ORBIT> AttribMap::addAttribute(const std::string& nameAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
30
{
31 32
	if(!isOrbitEmbedded<ORBIT>())
		addEmbedding<ORBIT>() ;
33 34
	AttributeMultiVector<T>* amv = m_attribs[ORBIT].addAttribute<T>(nameAttr) ;
	return AttributeHandler<T, ORBIT>(this, amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
35 36
}

37 38
template <typename T, unsigned int ORBIT>
inline bool AttribMap::removeAttribute(AttributeHandler<T, ORBIT>& attr)
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40
{
	assert(attr.isValid() || !"Invalid attribute handler") ;
Thery Sylvain's avatar
Thery Sylvain committed
41
	if(m_attribs[attr.getOrbit()].template removeAttribute<T>(attr.getIndex()))
42 43 44 45 46
	{
		typedef std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*>::iterator IT ;
		std::pair<IT, IT> bounds = attributeHandlers.equal_range(attr.getDataVector()) ;
		for(IT i = bounds.first; i != bounds.second; ++i)
			(*i).second->setInvalid() ;
47
		attributeHandlers.erase(bounds.first, bounds.second) ;
48 49 50
		return true ;
	}
	return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52
}

53 54
template <typename T, unsigned int ORBIT>
inline AttributeHandler<T ,ORBIT> AttribMap::getAttribute(const std::string& nameAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
55
{
56 57
	AttributeMultiVector<T>* amv = m_attribs[ORBIT].getDataVector<T>(nameAttr) ;
	return AttributeHandler<T, ORBIT>(this, amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59
}

60 61
template <typename T, unsigned int ORBIT>
inline bool AttribMap::swapAttributes(AttributeHandler<T, ORBIT>& attr1, AttributeHandler<T, ORBIT>& attr2)
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63
{
	assert((attr1.isValid() && attr2.isValid()) || !"Invalid attribute handler") ;
64 65
//	assert(attr1.getOrbit() == attr2.getOrbit() || !"Cannot swap attributes of different orbits") ;
//	unsigned int orbit = attr1.getOrbit() ;
66 67
	unsigned int index1 = attr1.getIndex() ;
	unsigned int index2 = attr2.getIndex() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
68
	if(index1 != index2)
69
		return m_attribs[ORBIT].swapAttributes(index1, index2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72
	return false ;
}

73 74
template <typename T, unsigned int ORBIT>
inline bool AttribMap::copyAttribute(AttributeHandler<T, ORBIT>& dst, AttributeHandler<T, ORBIT>& src)
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76
{
	assert((dst.isValid() && src.isValid()) || !"Invalid attribute handler") ;
77 78
//	unsigned int orbit = dst.getOrbit() ;
//	assert(orbit == src.getOrbit() || !"Cannot copy attributes of different orbits") ;
79 80
	unsigned int index_dst = dst.getIndex() ;
	unsigned int index_src = src.getIndex() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81
	if(index_dst != index_src)
82
		return m_attribs[ORBIT].copyAttribute(index_dst, index_src) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
83 84 85 86 87 88 89 90
	return false ;
}

inline unsigned int AttribMap::getNbCells(unsigned int orbit)
{
	return this->m_attribs[orbit].size() ;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/****************************************
 *               UTILITIES              *
 ****************************************/

template <unsigned int ORBIT>
unsigned int AttribMap::computeIndexCells(AttributeHandler<unsigned int, ORBIT>& idx)
{
	AttributeContainer& cont = m_attribs[ORBIT] ;
	unsigned int cpt = 0 ;
	for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
		idx[i] = cpt++ ;
	return cpt ;
}

template <unsigned int ORBIT>
void AttribMap::bijectiveOrbitEmbedding()
{
108
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;
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

	AttributeHandler<int, ORBIT> counter = addAttribute<int, ORBIT>("tmpCounter") ;
	counter.setAllValues(int(0)) ;

	DartMarker mark(*this) ;
	for(Dart d = begin(); d != end(); next(d))
	{
		if(!mark.isMarked(d))
		{
			mark.markOrbit<ORBIT>(d) ;
			unsigned int emb = getEmbedding<ORBIT>(d) ;
			if (emb != EMBNULL)
			{
				if (counter[d] > 0)
				{
					unsigned int newEmb = embedNewCell<ORBIT>(d) ;
					copyCell<ORBIT>(newEmb, emb) ;
				}
				counter[d]++ ;
			}
		}
	}

	removeAttribute(counter) ;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135

} // namespace CGoGN