attribmap.hpp 4.89 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

template <typename T>
inline AttributeHandler<T> AttribMap::addAttribute(unsigned int orbit, const std::string& nameAttr)
{
	if(!isOrbitEmbedded(orbit))
		addEmbedding(orbit) ;
33
	AttributeContainer& cellCont = m_attribs[orbit] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
34
	unsigned int index = cellCont.addAttribute<T>(nameAttr) ;
35
	unsigned int idAttr = AttributeContainer::attrId(orbit, index) ; // assume here that we have less than 16M attributes and 255 orbits!!
Pierre Kraemer's avatar
Pierre Kraemer committed
36
37
38
39
40
41
42
43
	return AttributeHandler<T>(this, idAttr) ;
}

template <typename T>
inline bool AttribMap::removeAttribute(AttributeHandler<T>& attr)
{
	assert(attr.isValid() || !"Invalid attribute handler") ;
	unsigned int idAttr = attr.id() ;
44
	return m_attribs[AttributeContainer::orbitAttr(idAttr)].removeAttribute(AttributeContainer::indexAttr(idAttr)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
47
48
49
50
51
}

template <typename T>
inline AttributeHandler<T> AttribMap::getAttribute(unsigned int orbit, const std::string& nameAttr)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	unsigned int index = m_attribs[orbit].getAttribute(nameAttr) ;
52
	return AttributeHandler<T>(this, AttributeContainer::attrId(orbit, index)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
53
54
55
56
57
58
}

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

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

inline unsigned int AttribMap::getNbCells(unsigned int orbit)
{
	assert(isOrbitEmbedded(orbit) || !"Invalid parameter: orbit not embedded");
	return this->m_attribs[orbit].size() ;
}

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

91
inline AttributeMultiVector<Dart>* AttribMap::addRelation(const std::string& name)
Pierre Kraemer's avatar
Pierre Kraemer committed
92
{
93
	AttributeContainer& cont = m_attribs[DART_ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
94
95

	unsigned int index = cont.addAttribute<Dart>(name) ;
96
	AttributeMultiVector<Dart>& amv = cont.getDataVector<Dart>(index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97
98
99
100
101
102
103
104
105

	// 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))
		amv[i] = i ;

	return &amv ;
}

} // namespace CGoGN