attributeHandler.hpp 5.98 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
template <typename T>
void AttributeHandler<T>::registerInMap()
{
	m_map->attributeHandlers.insert(std::pair<AttributeMultiVectorGen*, AttributeHandlerGen*>(m_attrib, this)) ;
}

template <typename T>
void AttributeHandler<T>::unregisterFromMap()
{
	typedef std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*>::iterator IT ;
	std::pair<IT, IT> bounds = m_map->attributeHandlers.equal_range(m_attrib) ;
	for(IT i = bounds.first; i != bounds.second; ++i)
	{
		if((*i).second == this)
		{
			m_map->attributeHandlers.erase(i) ;
			return ;
		}
	}
	assert(false || !"Should not get here") ;
}

// =================================================================

Pierre Kraemer's avatar
Pierre Kraemer committed
52
template <typename T>
53
AttributeHandler<T>::AttributeHandler() :
54
	AttributeHandlerGen(NULL, false), m_attrib(NULL)
55
{}
Pierre Kraemer's avatar
Pierre Kraemer committed
56

57
template <typename T>
58
AttributeHandler<T>::AttributeHandler(GenericMap* m, AttributeMultiVector<T>* amv) :
59
	AttributeHandlerGen(m, false), m_attrib(amv)
60
{
61
	if(m != NULL && amv != NULL && amv->getIndex() != AttributeContainer::UNKNOWN)
62 63 64 65 66 67
	{
		valid = true ;
		registerInMap() ;
	}
	else
		valid = false ;
68
}
69

Pierre Kraemer's avatar
Pierre Kraemer committed
70
template <typename T>
71
AttributeHandler<T>::AttributeHandler(const AttributeHandler<T>& ta) :
72
	AttributeHandlerGen(ta.m_map, ta.valid), m_attrib(ta.m_attrib)
73
{
74 75
	if(valid)
		registerInMap() ;
76
}
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80

template <typename T>
inline void AttributeHandler<T>::operator=(const AttributeHandler<T>& ta)
{
81 82 83
	if(valid)
		unregisterFromMap() ;
	m_map = ta.m_map ;
Pierre Kraemer's avatar
Pierre Kraemer committed
84
	m_attrib = ta.m_attrib ;
85 86 87
	valid = ta.valid ;
	if(valid)
		registerInMap() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91
}

template <typename T>
AttributeHandler<T>::~AttributeHandler()
92
{
93 94
	if(valid)
		unregisterFromMap() ;
95
}
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97

template <typename T>
98
AttributeMultiVector<T>* AttributeHandler<T>::getDataVector() const
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100 101 102 103
{
	return m_attrib ;
}

template <typename T>
104
unsigned int AttributeHandler<T>::getOrbit() const
Pierre Kraemer's avatar
Pierre Kraemer committed
105
{
106 107 108 109 110 111 112
	return m_attrib->getOrbit() ;
}

template <typename T>
unsigned int AttributeHandler<T>::getIndex() const
{
	return m_attrib->getIndex() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114 115 116 117
}

template <typename T>
const std::string& AttributeHandler<T>::name() const
{
118
	return m_attrib->getName() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120 121 122 123
}

template <typename T>
inline T& AttributeHandler<T>::operator[](Dart d)
{
124
	assert(valid || !"Invalid AttributeHandler") ;
125
	unsigned int orbit = m_attrib->getOrbit() ;
126
	unsigned int a = m_map->getEmbedding(orbit, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128

	if (a == EMBNULL)
129
		a = m_map->embedNewCell(orbit, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
130

131
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134 135 136
}

template <typename T>
inline const T& AttributeHandler<T>::operator[](Dart d) const
{
137 138
	assert(valid || !"Invalid AttributeHandler") ;
	unsigned int a = m_map->getEmbedding(m_attrib->getOrbit(), d) ;
139
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143 144
}

template <typename T>
inline T& AttributeHandler<T>::operator[](unsigned int a)
{
145
	assert(valid || !"Invalid AttributeHandler") ;
146
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149 150 151
}

template <typename T>
inline const T& AttributeHandler<T>::operator[](unsigned int a) const
{
152
	assert(valid || !"Invalid AttributeHandler") ;
153
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155 156 157 158
}

template <typename T>
inline unsigned int AttributeHandler<T>::insert(const T& elt)
{
159 160
	assert(valid || !"Invalid AttributeHandler") ;
	unsigned int idx = m_map->getAttributeContainer(m_attrib->getOrbit()).insertLine() ;
161 162
	m_attrib->operator[](idx) = elt ;
	return idx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164 165 166 167
}

template <typename T>
inline unsigned int AttributeHandler<T>::newElt()
{
168 169
	assert(valid || !"Invalid AttributeHandler") ;
	unsigned int idx = m_map->getAttributeContainer(m_attrib->getOrbit()).insertLine() ;
170
	return idx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
171 172
}

173
template <typename T>
174
inline void AttributeHandler<T>::setAllValues(const T& v)
175 176 177 178 179
{
	for(unsigned int i = begin(); i != end(); next(i))
		m_attrib->operator[](i) = v ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182
template <typename T>
inline unsigned int AttributeHandler<T>::begin() const
{
183 184
	assert(valid || !"Invalid AttributeHandler") ;
	return m_map->getAttributeContainer(m_attrib->getOrbit()).begin() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186 187 188 189
}

template <typename T>
inline unsigned int AttributeHandler<T>::end() const
{
190 191
	assert(valid || !"Invalid AttributeHandler") ;
	return m_map->getAttributeContainer(m_attrib->getOrbit()).end() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
192 193 194 195 196
}

template <typename T>
inline void AttributeHandler<T>::next(unsigned int& iter) const
{
197 198
	assert(valid || !"Invalid AttributeHandler") ;
	m_map->getAttributeContainer(m_attrib->getOrbit()).next(iter) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
199 200 201
}

} //namespace CGoGN