attributeHandler.hpp 6.74 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
32
33
{
	m_map->attributeHandlers.insert(std::pair<AttributeMultiVectorGen*, AttributeHandlerGen*>(m_attrib, this)) ;
}

34
35
template <typename T, unsigned int ORBIT>
inline void AttributeHandler<T, ORBIT>::unregisterFromMap()
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{
	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") ;
}

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

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

57

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

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

80
template <typename T, unsigned int ORBIT>
81
inline AttributeHandler<T, ORBIT>& AttributeHandler<T, ORBIT>::operator=(const AttributeHandler<T, ORBIT>& ta)
Pierre Kraemer's avatar
Pierre Kraemer committed
82
{
83
84
85
	if(valid)
		unregisterFromMap() ;
	m_map = ta.m_map ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	m_attrib = ta.m_attrib ;
87
88
89
	valid = ta.valid ;
	if(valid)
		registerInMap() ;
Pierre Kraemer's avatar
bla    
Pierre Kraemer committed
90
	return *this ;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
}

93
94
template <typename T, unsigned int ORBIT>
AttributeHandler<T, ORBIT>::~AttributeHandler()
95
{
96
97
	if(valid)
		unregisterFromMap() ;
98
}
Pierre Kraemer's avatar
Pierre Kraemer committed
99

100
101
template <typename T, unsigned int ORBIT>
inline AttributeMultiVector<T>* AttributeHandler<T, ORBIT>::getDataVector() const
Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
104
105
{
	return m_attrib ;
}

106
107
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::getOrbit() const
Pierre Kraemer's avatar
Pierre Kraemer committed
108
{
109
	return ORBIT ;
110
111
}

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

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

124
125
126
127
128
129
130

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

131
132
template <typename T, unsigned int ORBIT>
inline T& AttributeHandler<T, ORBIT>::operator[](Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
133
{
134
	assert(valid || !"Invalid AttributeHandler") ;
135
	unsigned int a = m_map->getEmbedding<ORBIT>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136
137

	if (a == EMBNULL)
138
		a = m_map->embedNewCell<ORBIT>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
139

140
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141
142
}

143
144
template <typename T, unsigned int ORBIT>
inline const T& AttributeHandler<T, ORBIT>::operator[](Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
145
{
146
	assert(valid || !"Invalid AttributeHandler") ;
147
	unsigned int a = m_map->getEmbedding<ORBIT>(d) ;
148
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
149
150
}

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

158
159
template <typename T, unsigned int ORBIT>
inline const T& AttributeHandler<T, ORBIT>::operator[](unsigned int a) const
Pierre Kraemer's avatar
Pierre Kraemer committed
160
{
161
	assert(valid || !"Invalid AttributeHandler") ;
162
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
163
164
}

165
166
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::insert(const T& elt)
Pierre Kraemer's avatar
Pierre Kraemer committed
167
{
168
	assert(valid || !"Invalid AttributeHandler") ;
169
	unsigned int idx = m_map->getAttributeContainer<ORBIT>().insertLine() ;
170
171
	m_attrib->operator[](idx) = elt ;
	return idx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
172
173
}

174
175
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::newElt()
Pierre Kraemer's avatar
Pierre Kraemer committed
176
{
177
	assert(valid || !"Invalid AttributeHandler") ;
178
	unsigned int idx = m_map->getAttributeContainer<ORBIT>().insertLine() ;
179
	return idx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
180
181
}

182
183
template <typename T, unsigned int ORBIT>
inline void AttributeHandler<T, ORBIT>::setAllValues(const T& v)
184
185
186
187
188
{
	for(unsigned int i = begin(); i != end(); next(i))
		m_attrib->operator[](i) = v ;
}

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

196
197
template <typename T, unsigned int ORBIT>
inline unsigned int AttributeHandler<T, ORBIT>::end() const
Pierre Kraemer's avatar
Pierre Kraemer committed
198
{
199
	assert(valid || !"Invalid AttributeHandler") ;
200
	return m_map->getAttributeContainer<ORBIT>().end() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
201
202
}

203
204
template <typename T, unsigned int ORBIT>
inline void AttributeHandler<T, ORBIT>::next(unsigned int& iter) const
Pierre Kraemer's avatar
Pierre Kraemer committed
205
{
206
	assert(valid || !"Invalid AttributeHandler") ;
207
	m_map->getAttributeContainer<ORBIT>().next(iter) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
210
}

} //namespace CGoGN