attributeHandler.h 9.1 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
32
33
34
35
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __ATTRIBUTE_HANDLER_H__
#define __ATTRIBUTE_HANDLER_H__

#include <vector>
#include <map>

#include "Topology/generic/genericmap.h"

namespace CGoGN
{

36
37
38
class AttributeHandlerGen
{
protected:
39
40
41
	friend class GenericMap ;
	friend class AttribMap ;

42
	// the map that contains the linked attribute
43
44
45
	GenericMap* m_map ;
	// boolean that states the validity of the handler
	bool valid ;
46
47

public:
48
	AttributeHandlerGen(GenericMap* m, bool v) : m_map(m), valid(v)
49
50
51
52
53
54
	{}

	GenericMap* map() const
	{
		return m_map ;
	}
55

56
57
58
59
60
	bool isValid() const
	{
		return valid ;
	}

61
62
	virtual const std::string& name() const = 0;

63
protected:
64
65
	void setInvalid()
	{
66
		valid = false ;
67
	}
68
} ;
69

Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
/**
 * Class that create an access-table to an existing attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
72
 * Main available operations are:
Pierre Kraemer's avatar
Pierre Kraemer committed
73
74
75
76
 * - [ index ]
 * - [ dart ]
 * - begin / end / next to manage indexing
 */
77
template <typename T, unsigned int ORBIT>
78
class AttributeHandler : public AttributeHandlerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
{
protected:
81
	// the multi-vector that contains attribute data
82
	AttributeMultiVector<T>* m_attrib;
Pierre Kraemer's avatar
Pierre Kraemer committed
83

84
85
86
	void registerInMap() ;
	void unregisterFromMap() ;

87
88
89
public:
	typedef T DATA_TYPE ;

90
91
92
93
94
95
	/**
	 * Default constructor
	 * Constructs a non-valid AttributeHandler (i.e. not linked to any attribute)
	 */
	AttributeHandler() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
96
97
98
	/**
	 * Constructor
	 * @param m the map which belong attribute
99
	 * @param amv a pointer to the AttributeMultiVector
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	 */
101
	AttributeHandler(GenericMap* m, AttributeMultiVector<T>* amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
104
105
106

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

untereiner's avatar
untereiner committed
109
110
111
112
113
114
115
116
	/**
	 * Transmute Constructor
	 * Construct an attribute of Orbit from Orbit2
	 * @param h the table attribute
	 */
	template <unsigned int ORBIT2>
	AttributeHandler(const AttributeHandler<T, ORBIT2>& h) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
119
120
	/**
	 * affectation operator
	 * @param ta the table attribute to affect to this
	 */
121
	AttributeHandler<T, ORBIT>& operator=(const AttributeHandler<T, ORBIT>& ta) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
122

untereiner's avatar
untereiner committed
123
124
125
126
127
128
129
	/**
	 * transmuted affectation operator
	 * @param ta the table attribute to affect to this
	 */
	template <unsigned int ORBIT2>
	AttributeHandler<T, ORBIT>& operator=(const AttributeHandler<T, ORBIT2>& ta) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
130
131
132
133
134
135
136
137
	/**
	 * Destructor (empty & virtual)
	 */
	virtual ~AttributeHandler() ;

	/**
	 * get attribute data vector
	 */
138
	AttributeMultiVector<T>* getDataVector() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
139
140

	/**
141
	 * get attribute orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
142
	 */
143
144
145
146
147
148
	unsigned int getOrbit() const ;

	/**
	 * get attribute index
	 */
	unsigned int getIndex() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
149
150
151
152

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

155
156
157
158
159
	/**
	 * give the number of elements of the attribute container
	 */
	unsigned int nbElements() const;

Pierre Kraemer's avatar
Pierre Kraemer committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	/**
	 * [] operator with dart parameter
	 */
	T& operator[](Dart d) ;

	/**
	 * const [] operator with dart parameter
	 */
	const T& operator[](Dart d) const ;

	/**
	 * 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
186
	 * insert an element with default value (warning we add here a complete line in container)
Pierre Kraemer's avatar
Pierre Kraemer committed
187
188
189
	 */
	unsigned int newElt() ;

190
	/**
191
	 * initialize all the lines of the attribute with the given value
192
	 */
193
	void setAllValues(const T& v) ;
194

Pierre Kraemer's avatar
Pierre Kraemer committed
195
196
197
198
199
200
201
	/**
	 * begin of table
	 * @return the iterator of the begin of container
	 */
	unsigned int begin() const;

	/**
202
	 * end of table
Pierre Kraemer's avatar
Pierre Kraemer committed
203
204
205
206
207
	 * @return the iterator of the end of container
	 */
	unsigned int end() const;

	/**
208
	 * Next on iterator (equivalent to stl ++)
Pierre Kraemer's avatar
Pierre Kraemer committed
209
210
211
212
213
	 * @param iter iterator to
	 */
	void next(unsigned int& iter) const;
} ;

214
215
216
217
218
219
220
221
222
/**
 *  shortcut class for Vertex Attribute (Handler)
 */
template <typename T>
class DartAttribute : public AttributeHandler<T, DART>
{
public:
	DartAttribute() : AttributeHandler<T, DART>() {}
	DartAttribute(const AttributeHandler<T, DART>& ah) : AttributeHandler<T, DART>(ah) {}
223
	DartAttribute(GenericMap* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, DART>(m,amv) {}
224
225
226
	DartAttribute<T>& operator=(const AttributeHandler<T, DART>& ah) { this->AttributeHandler<T, DART>::operator=(ah); return *this; }
};

227
228
229
230
/**
 *  shortcut class for Vertex Attribute (Handler)
 */
template <typename T>
Pierre Kraemer's avatar
bla    
Pierre Kraemer committed
231
class VertexAttribute : public AttributeHandler<T, VERTEX>
232
233
{
public:
234
235
	VertexAttribute() : AttributeHandler<T, VERTEX>() {}
	VertexAttribute(const AttributeHandler<T, VERTEX>& ah) : AttributeHandler<T, VERTEX>(ah) {}
236
	VertexAttribute(GenericMap* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, VERTEX>(m,amv) {}
237
	VertexAttribute<T>& operator=(const AttributeHandler<T, VERTEX>& ah) { this->AttributeHandler<T, VERTEX>::operator=(ah); return *this; }
untereiner's avatar
untereiner committed
238
239
240
	VertexAttribute<T>& operator=(const AttributeHandler<T, EDGE>& ah) { this->AttributeHandler<T,VERTEX>::operator=(ah); return *this; }
	VertexAttribute<T>& operator=(const AttributeHandler<T, FACE>& ah) { this->AttributeHandler<T,VERTEX>::operator=(ah); return *this; }
	VertexAttribute<T>& operator=(const AttributeHandler<T, VOLUME>& ah) { this->AttributeHandler<T,VERTEX>::operator=(ah); return *this; }
241
242
243
244
245
246
};

/**
 *  shortcut class for Edge Attribute (Handler)
 */
template <typename T>
247
class EdgeAttribute : public AttributeHandler<T, EDGE>
248
249
{
public:
250
251
	EdgeAttribute() : AttributeHandler<T, EDGE>() {}
	EdgeAttribute(const AttributeHandler<T, EDGE>& ah) : AttributeHandler<T, EDGE>(ah) {}
252
	EdgeAttribute(GenericMap* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, EDGE>(m,amv) {}
253
	EdgeAttribute<T>& operator=(const AttributeHandler<T, EDGE>& ah) { this->AttributeHandler<T, EDGE>::operator=(ah); return *this; }
254
255
256
257
258
259
};

/**
 *  shortcut class for Face Attribute (Handler)
 */
template <typename T>
260
class FaceAttribute : public AttributeHandler<T, FACE>
261
262
{
public:
263
264
	FaceAttribute() : AttributeHandler<T, FACE>() {}
	FaceAttribute(const AttributeHandler<T, FACE>& ah) : AttributeHandler<T, FACE>(ah) {}
265
	FaceAttribute(GenericMap* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, FACE>(m,amv) {}
266
	FaceAttribute<T>& operator=(const AttributeHandler<T, FACE>& ah) { this->AttributeHandler<T, FACE>::operator=(ah); return *this; }
untereiner's avatar
untereiner committed
267
	FaceAttribute<T>& operator=(const AttributeHandler<T, VERTEX>& ah) { this->AttributeHandler<T,FACE>::operator=(ah); return *this; }
268
269
270
271
272
273
};

/**
 *  shortcut class for Volume Attribute (Handler)
 */
template <typename T>
274
class VolumeAttribute : public AttributeHandler<T, VOLUME>
275
276
{
public:
277
278
	VolumeAttribute() : AttributeHandler<T, VOLUME>() {}
	VolumeAttribute(const AttributeHandler<T, VOLUME>& ah) : AttributeHandler<T, VOLUME>(ah) {}
279
	VolumeAttribute(GenericMap* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, VOLUME>(m,amv) {}
280
	VolumeAttribute<T>& operator=(const AttributeHandler<T, VOLUME>& ah) { this->AttributeHandler<T, VOLUME>::operator=(ah); return *this; }
untereiner's avatar
untereiner committed
281
	VolumeAttribute<T>& operator=(const AttributeHandler<T, VERTEX>& ah) { this->AttributeHandler<T, VOLUME>::operator=(ah); return *this; }
282
283
};

untereiner's avatar
untereiner committed
284
285
286
287
288
289
290
291
292
293
294

// 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>
  inline void turn_to(FROM_T* p)
  {
    assert( sizeof(FROM_T) == sizeof(TO_T));
    ::new(p) TO_T(); // use of placement new
  }

Pierre Kraemer's avatar
Pierre Kraemer committed
295
296
297
298
299
} // namespace CGoGN

#include "Topology/generic/attributeHandler.hpp"

#endif