attributeHandler.hpp 6.54 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
template <typename T, unsigned int ORBIT>
AttributeHandler<T, ORBIT>::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
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
63
		assert(ORBIT == amv->getOrbit() || !"AttributeHandler: orbit incompatibility") ;
64
65
66
67
68
		valid = true ;
		registerInMap() ;
	}
	else
		valid = false ;
69
}
70

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

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

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

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

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

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

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

122
123
template <typename T, unsigned int ORBIT>
inline T& AttributeHandler<T, ORBIT>::operator[](Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
124
{
125
	assert(valid || !"Invalid AttributeHandler") ;
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
template <typename T, unsigned int ORBIT>
inline const T& AttributeHandler<T, ORBIT>::operator[](Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
136
{
137
	assert(valid || !"Invalid AttributeHandler") ;
138
	unsigned int a = m_map->getEmbedding<ORBIT>(d) ;
139
	return m_attrib->operator[](a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140
141
}

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

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

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

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

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

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

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

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

} //namespace CGoGN