attributeHandler.hpp 8.92 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
{

Pierre Kraemer's avatar
Pierre Kraemer committed
28 29
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline void AttributeHandler<T, ORBIT, MAP_IMPL>::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)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
35 36
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline void AttributeHandler<T, ORBIT, MAP_IMPL>::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") ;
}

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
78 79 80 81
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
AttributeHandler<T, ORBIT, MAP_IMPL>::AttributeHandler(const AttributeHandler<T, ORBIT, MAP_IMPL>& ta) :
	AttributeHandlerGen(ta.valid),
	m_map(ta.m_map),
82
	m_attrib(ta.m_attrib)
83
{
84 85
	if(valid)
		registerInMap() ;
86
}
Pierre Kraemer's avatar
Pierre Kraemer committed
87

Pierre Kraemer's avatar
Pierre Kraemer committed
88
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
untereiner's avatar
untereiner committed
89
template <unsigned int ORBIT2>
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91 92 93
AttributeHandler<T, ORBIT, MAP_IMPL>::AttributeHandler(const AttributeHandler<T, ORBIT2, MAP_IMPL>& h) :
	AttributeHandlerGen(h.valid),
	m_map(h.m_map),
	m_attrib(h.m_attrib)
untereiner's avatar
untereiner committed
94 95 96 97 98 99 100 101 102 103
{
	if(m_attrib->getOrbit() == ORBIT2)
	{
		if(valid)
			registerInMap() ;
	}
	else
		valid = false;
}

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
144 145
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline AttributeMultiVectorGen* AttributeHandler<T, ORBIT, MAP_IMPL>::getDataVectorGen() const
146 147 148 149
{
	return m_attrib ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
150 151
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline int AttributeHandler<T, ORBIT, MAP_IMPL>::getSizeOfType() const
152 153 154 155
{
	return sizeof(T) ;
}

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
174 175
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline const std::string& AttributeHandler<T, ORBIT, MAP_IMPL>::typeName() const
176 177 178
{
	return m_attrib->getTypeName();
}
179

180

Pierre Kraemer's avatar
Pierre Kraemer committed
181 182
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline unsigned int AttributeHandler<T, ORBIT, MAP_IMPL>::nbElements() const
183
{
Pierre Kraemer's avatar
Pierre Kraemer committed
184
	return m_map->template getAttributeContainer<ORBIT>().size() ;
185 186
}

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

Sylvain Thery's avatar
Sylvain Thery committed
193
	if (a == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
194
		a = m_map->template setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
195

196
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
197 198
}

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
238 239
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
inline void AttributeHandler<T, ORBIT, MAP_IMPL>::setAllValues(const T& v)
240 241 242 243 244
{
	for(unsigned int i = begin(); i != end(); next(i))
		m_attrib->operator[](i) = v ;
}

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

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

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

} //namespace CGoGN