attributeHandler.hpp 8.04 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 void AttributeHandler<T, ORBIT>::registerInMap()
30
{
31
	boost::mutex::scoped_lock lockAH(m_map->attributeHandlersMutex);
32 33 34
	m_map->attributeHandlers.insert(std::pair<AttributeMultiVectorGen*, AttributeHandlerGen*>(m_attrib, this)) ;
}

35 36
template <typename T, unsigned int ORBIT>
inline void AttributeHandler<T, ORBIT>::unregisterFromMap()
37 38
{
	typedef std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*>::iterator IT ;
39 40

	boost::mutex::scoped_lock lockAH(m_map->attributeHandlersMutex);
41 42 43 44 45 46 47 48 49 50 51 52 53 54
	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") ;
}

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

55 56
template <typename T, unsigned int ORBIT>
AttributeHandler<T, ORBIT>::AttributeHandler() :
57
	AttributeHandlerGen(NULL, false), m_attrib(NULL)
58
{}
Pierre Kraemer's avatar
Pierre Kraemer committed
59

60 61
template <typename T, unsigned int ORBIT>
AttributeHandler<T, ORBIT>::AttributeHandler(GenericMap* m, AttributeMultiVector<T>* amv) :
62
	AttributeHandlerGen(m, false), m_attrib(amv)
63
{
64
	if(m != NULL && amv != NULL && amv->getIndex() != AttributeContainer::UNKNOWN)
65
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
66
		assert(ORBIT == amv->getOrbit() || !"AttributeHandler: orbit incompatibility") ;
67 68 69 70 71
		valid = true ;
		registerInMap() ;
	}
	else
		valid = false ;
72
}
73

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

untereiner's avatar
untereiner committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
//template <typename T, unsigned int ORBIT>
//template <unsigned int ORBIT2>
template <typename T, unsigned int ORBIT>
template <unsigned int ORBIT2>
AttributeHandler<T, ORBIT>::AttributeHandler(const AttributeHandler<T, ORBIT2>& h) :
	AttributeHandlerGen(h.m_map, h.valid)
{
	m_attrib = h.m_attrib;
	if(m_attrib->getOrbit() == ORBIT2)
	{
		if(valid)
			registerInMap() ;
	}
	else
		valid = false;
}

100
template <typename T, unsigned int ORBIT>
101
inline AttributeHandler<T, ORBIT>& AttributeHandler<T, ORBIT>::operator=(const AttributeHandler<T, ORBIT>& ta)
Pierre Kraemer's avatar
Pierre Kraemer committed
102
{
103 104 105
	if(valid)
		unregisterFromMap() ;
	m_map = ta.m_map ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106
	m_attrib = ta.m_attrib ;
107 108 109
	valid = ta.valid ;
	if(valid)
		registerInMap() ;
Pierre Kraemer's avatar
bla  
Pierre Kraemer committed
110
	return *this ;
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112
}

untereiner's avatar
untereiner committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
template <typename T, unsigned int ORBIT>
template <unsigned int ORBIT2>
inline AttributeHandler<T, ORBIT>& AttributeHandler<T, ORBIT>::operator=(const AttributeHandler<T, ORBIT2>& ta)
{
	if(valid)
		unregisterFromMap() ;
	m_map = ta.map() ;
	m_attrib = ta.getDataVector() ;
	valid = ta.isValid() ;
	if(valid)
		registerInMap() ;
	return *this ;
}


128 129
template <typename T, unsigned int ORBIT>
AttributeHandler<T, ORBIT>::~AttributeHandler()
130
{
131 132
	if(valid)
		unregisterFromMap() ;
133
}
Pierre Kraemer's avatar
Pierre Kraemer committed
134

135 136
template <typename T, unsigned int ORBIT>
inline AttributeMultiVector<T>* AttributeHandler<T, ORBIT>::getDataVector() const
Pierre Kraemer's avatar
Pierre Kraemer committed
137 138 139 140
{
	return m_attrib ;
}

141 142 143 144 145 146 147 148 149 150 151 152
template <typename T, unsigned int ORBIT>
inline AttributeMultiVectorGen* AttributeHandler<T, ORBIT>::getDataVectorGen() const
{
	return m_attrib ;
}

template <typename T, unsigned int ORBIT>
inline int AttributeHandler<T, ORBIT>::getSizeOfType() const
{
	return sizeof(T) ;
}

153 154
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::getOrbit() const
Pierre Kraemer's avatar
Pierre Kraemer committed
155
{
156
	return ORBIT ;
157 158
}

159 160
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::getIndex() const
161 162
{
	return m_attrib->getIndex() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164
}

165 166
template <typename T, unsigned int ORBIT>
inline const std::string& AttributeHandler<T, ORBIT>::name() const
Pierre Kraemer's avatar
Pierre Kraemer committed
167
{
168
	return m_attrib->getName() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
169 170
}

171 172 173 174 175
template <typename T, unsigned int ORBIT>
inline const std::string& AttributeHandler<T, ORBIT>::typeName() const
{
	return m_attrib->getTypeName();
}
176

177 178 179 180 181 182 183

template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::nbElements() const
{
	return m_map->getAttributeContainer<ORBIT>().size() ;
}

184 185
template <typename T, unsigned int ORBIT>
inline T& AttributeHandler<T, ORBIT>::operator[](Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
186
{
187
	assert(valid || !"Invalid AttributeHandler") ;
188
	unsigned int a = m_map->getEmbedding<ORBIT>(d) ;
189

Sylvain Thery's avatar
Sylvain Thery committed
190 191
	if (a == EMBNULL)
		a = m_map->setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
192

193
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
194 195
}

196 197
template <typename T, unsigned int ORBIT>
inline const T& AttributeHandler<T, ORBIT>::operator[](Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
198
{
199
	assert(valid || !"Invalid AttributeHandler") ;
200
	unsigned int a = m_map->getEmbedding<ORBIT>(d) ;
201
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
202 203
}

204 205
template <typename T, unsigned int ORBIT>
inline T& AttributeHandler<T, ORBIT>::operator[](unsigned int a)
Pierre Kraemer's avatar
Pierre Kraemer committed
206
{
207
	assert(valid || !"Invalid AttributeHandler") ;
208
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
209 210
}

211 212
template <typename T, unsigned int ORBIT>
inline const T& AttributeHandler<T, ORBIT>::operator[](unsigned int a) const
Pierre Kraemer's avatar
Pierre Kraemer committed
213
{
214
	assert(valid || !"Invalid AttributeHandler") ;
215
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217
}

218 219
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::insert(const T& elt)
Pierre Kraemer's avatar
Pierre Kraemer committed
220
{
221
	assert(valid || !"Invalid AttributeHandler") ;
222
	unsigned int idx = m_map->getAttributeContainer<ORBIT>().insertLine() ;
223 224
	m_attrib->operator[](idx) = elt ;
	return idx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
225 226
}

227 228
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::newElt()
Pierre Kraemer's avatar
Pierre Kraemer committed
229
{
230
	assert(valid || !"Invalid AttributeHandler") ;
231
	unsigned int idx = m_map->getAttributeContainer<ORBIT>().insertLine() ;
232
	return idx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234
}

235 236
template <typename T, unsigned int ORBIT>
inline void AttributeHandler<T, ORBIT>::setAllValues(const T& v)
237 238 239 240 241
{
	for(unsigned int i = begin(); i != end(); next(i))
		m_attrib->operator[](i) = v ;
}

242 243
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::begin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
244
{
245
	assert(valid || !"Invalid AttributeHandler") ;
246
	return m_map->getAttributeContainer<ORBIT>().begin() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248
}

249 250
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::end() const
Pierre Kraemer's avatar
Pierre Kraemer committed
251
{
252
	assert(valid || !"Invalid AttributeHandler") ;
253
	return m_map->getAttributeContainer<ORBIT>().end() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
254 255
}

256 257
template <typename T, unsigned int ORBIT>
inline void AttributeHandler<T, ORBIT>::next(unsigned int& iter) const
Pierre Kraemer's avatar
Pierre Kraemer committed
258
{
259
	assert(valid || !"Invalid AttributeHandler") ;
260
	m_map->getAttributeContainer<ORBIT>().next(iter) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263
}

} //namespace CGoGN