attributeHandler.h 10.6 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
28
29
30
31
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __ATTRIBUTE_HANDLER_H__
#define __ATTRIBUTE_HANDLER_H__

#include <vector>
#include <map>

#include "Topology/generic/genericmap.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
32
#include "Container/attributeContainer.h"
33
#include "Container/fakeAttribute.h"
34
#include "Topology/generic/cells.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
35
36
37
38

namespace CGoGN
{

39
40
41
class AttributeHandlerGen
{
protected:
42
	friend class GenericMap ;
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	template <typename MAP_IMPL> friend class MapCommon;
44
45
46

	// boolean that states the validity of the handler
	bool valid ;
47
48

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
49
	AttributeHandlerGen(bool v) :
50
		valid(v)
51
52
	{}

53
54
55
56
57
	bool isValid() const
	{
		return valid ;
	}

58
59
60
61
	virtual int getSizeOfType() const = 0;

	virtual unsigned int getOrbit() const = 0;

62
	virtual const std::string& name() const = 0;
63
64
65
	virtual const std::string& typeName() const = 0;

	virtual AttributeMultiVectorGen* getDataVectorGen() const = 0;
66

67
protected:
68
69
	void setInvalid()
	{
70
		valid = false ;
71
	}
72
} ;
73

Pierre Kraemer's avatar
Pierre Kraemer committed
74
75
template <typename MAP_IMPL> class MapCommon;

Pierre Kraemer's avatar
Pierre Kraemer committed
76
77
/**
 * Class that create an access-table to an existing attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
78
 * Main available operations are:
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
82
 * - [ index ]
 * - [ dart ]
 * - begin / end / next to manage indexing
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
83
template <typename T, unsigned int ORBIT, typename MAP_IMPL>
84
class AttributeHandler : public AttributeHandlerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
{
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
87
88
	// the map that contains the linked attribute
	MapCommon<MAP_IMPL>* m_map;
89
	// the multi-vector that contains attribute data
90
	AttributeMultiVector<T>* m_attrib;
Pierre Kraemer's avatar
Pierre Kraemer committed
91

92
93
94
	void registerInMap() ;
	void unregisterFromMap() ;

95
96
97
public:
	typedef T DATA_TYPE ;

98
99
100
101
102
103
	/**
	 * Default constructor
	 * Constructs a non-valid AttributeHandler (i.e. not linked to any attribute)
	 */
	AttributeHandler() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
104
105
106
	/**
	 * Constructor
	 * @param m the map which belong attribute
107
	 * @param amv a pointer to the AttributeMultiVector
Pierre Kraemer's avatar
Pierre Kraemer committed
108
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
109
	AttributeHandler(MapCommon<MAP_IMPL>* m, AttributeMultiVector<T>* amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
110
111
112
113
114

	/**
	 * Copy constructor
	 * @param ta the table attribute
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
115
	AttributeHandler(const AttributeHandler<T, ORBIT, MAP_IMPL>& ta) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116

untereiner's avatar
untereiner committed
117
118
119
120
121
122
	/**
	 * Transmute Constructor
	 * Construct an attribute of Orbit from Orbit2
	 * @param h the table attribute
	 */
	template <unsigned int ORBIT2>
Pierre Kraemer's avatar
Pierre Kraemer committed
123
	AttributeHandler(const AttributeHandler<T, ORBIT2, MAP_IMPL>& h) ;
untereiner's avatar
untereiner committed
124

Pierre Kraemer's avatar
Pierre Kraemer committed
125
126
127
128
	/**
	 * affectation operator
	 * @param ta the table attribute to affect to this
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	AttributeHandler<T, ORBIT, MAP_IMPL>& operator=(const AttributeHandler<T, ORBIT, MAP_IMPL>& ta) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
130

untereiner's avatar
untereiner committed
131
132
133
134
135
	/**
	 * transmuted affectation operator
	 * @param ta the table attribute to affect to this
	 */
	template <unsigned int ORBIT2>
Pierre Kraemer's avatar
Pierre Kraemer committed
136
	AttributeHandler<T, ORBIT, MAP_IMPL>& operator=(const AttributeHandler<T, ORBIT2, MAP_IMPL>& ta) ;
untereiner's avatar
untereiner committed
137

Pierre Kraemer's avatar
Pierre Kraemer committed
138
139
140
141
142
	/**
	 * Destructor (empty & virtual)
	 */
	virtual ~AttributeHandler() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
143
144
145
146
147
148
149
150
151
	/**
	 * @brief map
	 * @return the map that contains the attribute
	 */
	MapCommon<MAP_IMPL>* map() const
	{
		return m_map ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
152
153
154
	/**
	 * get attribute data vector
	 */
155
	AttributeMultiVector<T>* getDataVector() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156

157
158
159
160
161
162
163
164
165
166
	/**
	 * get attribute data vector (generic MultiVector)
	 */
	virtual AttributeMultiVectorGen* getDataVectorGen() const ;

	/**
	 * get size of attribute type
	 */
	virtual int getSizeOfType() const ;

Pierre Kraemer's avatar
Pierre Kraemer committed
167
	/**
168
	 * get attribute orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
169
	 */
170
	virtual unsigned int getOrbit() const ;
171
172
173
174
175

	/**
	 * get attribute index
	 */
	unsigned int getIndex() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176
177
178
179

	/**
	 * get attribute name
	 */
180
	virtual const std::string& name() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
181

182
183
184
185
	/**
	 * get attribute type name
	 */
	virtual const std::string& typeName() const ;
186

187
188
189
190
191
	/**
	 * give the number of elements of the attribute container
	 */
	unsigned int nbElements() const;

Pierre Kraemer's avatar
Pierre Kraemer committed
192
	/**
193
	 * [] operator with cell parameter
Pierre Kraemer's avatar
Pierre Kraemer committed
194
	 */
195
	T& operator[](Cell<ORBIT> c) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
196
197

	/**
198
	 * const [] operator with cell parameter
Pierre Kraemer's avatar
Pierre Kraemer committed
199
	 */
200
	const T& operator[](Cell<ORBIT> c) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

	/**
	 * at operator (same as [] but with index parameter)
	 */
	T& operator[](unsigned int a) ;

	/**
	 * const at operator (same as [] but with index parameter)
	 */
	const T& operator[](unsigned int a) const ;

	/**
	 * insert an element (warning we add here a complete line in container)
	 */
	unsigned int insert(const T& elt) ;

	/**
untereiner's avatar
ihm3  
untereiner committed
218
	 * insert an element with default value (warning we add here a complete line in container)
Pierre Kraemer's avatar
Pierre Kraemer committed
219
220
221
	 */
	unsigned int newElt() ;

222
	/**
223
	 * initialize all the lines of the attribute with the given value
224
	 */
225
	void setAllValues(const T& v) ;
226

Pierre Kraemer's avatar
Pierre Kraemer committed
227
228
229
230
231
232
233
	/**
	 * begin of table
	 * @return the iterator of the begin of container
	 */
	unsigned int begin() const;

	/**
234
	 * end of table
Pierre Kraemer's avatar
Pierre Kraemer committed
235
236
237
238
239
	 * @return the iterator of the end of container
	 */
	unsigned int end() const;

	/**
240
	 * Next on iterator (equivalent to stl ++)
Pierre Kraemer's avatar
Pierre Kraemer committed
241
242
243
244
245
	 * @param iter iterator to
	 */
	void next(unsigned int& iter) const;
} ;

246
/**
Pierre Kraemer's avatar
Pierre Kraemer committed
247
 *  shortcut class for Dart Attribute (Handler)
248
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
249
250
template <typename T, typename MAP_IMPL>
class DartAttribute : public AttributeHandler<T, DART, MAP_IMPL>
251
252
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
253
254
255
256
	DartAttribute() : AttributeHandler<T, DART, MAP_IMPL>() {}
	DartAttribute(const AttributeHandler<T, DART, MAP_IMPL>& ah) : AttributeHandler<T, DART, MAP_IMPL>(ah) {}
	DartAttribute(MapCommon<MAP_IMPL>* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, DART, MAP_IMPL>(m,amv) {}
	DartAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, DART, MAP_IMPL>& ah) { this->AttributeHandler<T, DART, MAP_IMPL>::operator=(ah); return *this; }
257
258
};

259
260
261
/**
 *  shortcut class for Vertex Attribute (Handler)
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
262
263
template <typename T, typename MAP_IMPL>
class VertexAttribute : public AttributeHandler<T, VERTEX, MAP_IMPL>
264
265
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
266
267
268
269
270
271
272
	VertexAttribute() : AttributeHandler<T, VERTEX, MAP_IMPL>() {}
	VertexAttribute(const AttributeHandler<T, VERTEX, MAP_IMPL>& ah) : AttributeHandler<T, VERTEX, MAP_IMPL>(ah) {}
	VertexAttribute(MapCommon<MAP_IMPL>* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, VERTEX, MAP_IMPL>(m,amv) {}
	VertexAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, VERTEX, MAP_IMPL>& ah) { this->AttributeHandler<T, VERTEX, MAP_IMPL>::operator=(ah); return *this; }
	VertexAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, EDGE, MAP_IMPL>& ah) { this->AttributeHandler<T,VERTEX, MAP_IMPL>::operator=(ah); return *this; }
	VertexAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, FACE, MAP_IMPL>& ah) { this->AttributeHandler<T,VERTEX, MAP_IMPL>::operator=(ah); return *this; }
	VertexAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, VOLUME, MAP_IMPL>& ah) { this->AttributeHandler<T,VERTEX, MAP_IMPL>::operator=(ah); return *this; }
273
274
275
276
277
};

/**
 *  shortcut class for Edge Attribute (Handler)
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
278
279
template <typename T, typename MAP_IMPL>
class EdgeAttribute : public AttributeHandler<T, EDGE, MAP_IMPL>
280
281
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
282
283
284
285
	EdgeAttribute() : AttributeHandler<T, EDGE, MAP_IMPL>() {}
	EdgeAttribute(const AttributeHandler<T, EDGE, MAP_IMPL>& ah) : AttributeHandler<T, EDGE, MAP_IMPL>(ah) {}
	EdgeAttribute(MapCommon<MAP_IMPL>* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, EDGE, MAP_IMPL>(m,amv) {}
	EdgeAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, EDGE, MAP_IMPL>& ah) { this->AttributeHandler<T, EDGE, MAP_IMPL>::operator=(ah); return *this; }
286
287
288
289
290
};

/**
 *  shortcut class for Face Attribute (Handler)
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
291
292
template <typename T, typename MAP_IMPL>
class FaceAttribute : public AttributeHandler<T, FACE, MAP_IMPL>
293
294
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
295
296
297
298
299
	FaceAttribute() : AttributeHandler<T, FACE, MAP_IMPL>() {}
	FaceAttribute(const AttributeHandler<T, FACE, MAP_IMPL>& ah) : AttributeHandler<T, FACE, MAP_IMPL>(ah) {}
	FaceAttribute(MapCommon<MAP_IMPL>* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, FACE, MAP_IMPL>(m,amv) {}
	FaceAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, FACE, MAP_IMPL>& ah) { this->AttributeHandler<T, FACE, MAP_IMPL>::operator=(ah); return *this; }
	FaceAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, VERTEX, MAP_IMPL>& ah) { this->AttributeHandler<T,FACE, MAP_IMPL>::operator=(ah); return *this; }
300
301
302
303
304
};

/**
 *  shortcut class for Volume Attribute (Handler)
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
305
306
template <typename T, typename MAP_IMPL>
class VolumeAttribute : public AttributeHandler<T, VOLUME, MAP_IMPL>
307
308
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
309
310
311
312
313
	VolumeAttribute() : AttributeHandler<T, VOLUME, MAP_IMPL>() {}
	VolumeAttribute(const AttributeHandler<T, VOLUME, MAP_IMPL>& ah) : AttributeHandler<T, VOLUME, MAP_IMPL>(ah) {}
	VolumeAttribute(MapCommon<MAP_IMPL>* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, VOLUME, MAP_IMPL>(m,amv) {}
	VolumeAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, VOLUME, MAP_IMPL>& ah) { this->AttributeHandler<T, VOLUME, MAP_IMPL>::operator=(ah); return *this; }
	VolumeAttribute<T, MAP_IMPL>& operator=(const AttributeHandler<T, VERTEX, MAP_IMPL>& ah) { this->AttributeHandler<T, VOLUME, MAP_IMPL>::operator=(ah); return *this; }
314
315
};

untereiner's avatar
untereiner committed
316
317
318
319
320

// turn_to<b>(A*</b> obj) changes class of the object
// that means it just replaces VTBL of the object by VTBL of another class.
// NOTE: these two classes has to be ABI compatible!
template <typename TO_T, typename FROM_T>
Pierre Kraemer's avatar
Pierre Kraemer committed
321
322
323
324
325
inline void turn_to(FROM_T* p)
{
assert(sizeof(FROM_T) == sizeof(TO_T));
::new(p) TO_T(); // use of placement new
}
untereiner's avatar
untereiner committed
326

Pierre Kraemer's avatar
Pierre Kraemer committed
327
328
329
330
331
} // namespace CGoGN

#include "Topology/generic/attributeHandler.hpp"

#endif