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

90 91 92
	void registerInMap() ;
	void unregisterFromMap() ;

93 94 95
public:
	typedef T DATA_TYPE ;

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

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

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
141 142 143 144
	/**
	 * @brief map
	 * @return the map that contains the attribute
	 */
145
	MAP* map() const
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149
	{
		return m_map ;
	}

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

155 156 157 158 159 160 161 162 163 164
	/**
	 * 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
165
	/**
166
	 * get attribute orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
167
	 */
168
	virtual unsigned int getOrbit() const ;
169 170 171 172 173

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
 *  shortcut class for Edge Attribute (Handler)
 */
276 277
template <typename T, typename MAP>
class EdgeAttribute : public AttributeHandler<T, EDGE, MAP>
278 279
{
public:
280 281 282 283
	EdgeAttribute() : AttributeHandler<T, EDGE, MAP>() {}
	EdgeAttribute(const AttributeHandler<T, EDGE, MAP>& ah) : AttributeHandler<T, EDGE, MAP>(ah) {}
	EdgeAttribute(MAP* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, EDGE, MAP>(m,amv) {}
	EdgeAttribute<T, MAP>& operator=(const AttributeHandler<T, EDGE, MAP>& ah) { this->AttributeHandler<T, EDGE, MAP>::operator=(ah); return *this; }
284 285 286 287 288
};

/**
 *  shortcut class for Face Attribute (Handler)
 */
289 290
template <typename T, typename MAP>
class FaceAttribute : public AttributeHandler<T, FACE, MAP>
291 292
{
public:
293 294 295 296 297
	FaceAttribute() : AttributeHandler<T, FACE, MAP>() {}
	FaceAttribute(const AttributeHandler<T, FACE, MAP>& ah) : AttributeHandler<T, FACE, MAP>(ah) {}
	FaceAttribute(MAP* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, FACE, MAP>(m,amv) {}
	FaceAttribute<T, MAP>& operator=(const AttributeHandler<T, FACE, MAP>& ah) { this->AttributeHandler<T, FACE, MAP>::operator=(ah); return *this; }
	FaceAttribute<T, MAP>& operator=(const AttributeHandler<T, VERTEX, MAP>& ah) { this->AttributeHandler<T,FACE, MAP>::operator=(ah); return *this; }
298 299 300 301 302
};

/**
 *  shortcut class for Volume Attribute (Handler)
 */
303 304
template <typename T, typename MAP>
class VolumeAttribute : public AttributeHandler<T, VOLUME, MAP>
305 306
{
public:
307 308 309 310 311
	VolumeAttribute() : AttributeHandler<T, VOLUME, MAP>() {}
	VolumeAttribute(const AttributeHandler<T, VOLUME, MAP>& ah) : AttributeHandler<T, VOLUME, MAP>(ah) {}
	VolumeAttribute(MAP* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, VOLUME, MAP>(m,amv) {}
	VolumeAttribute<T, MAP>& operator=(const AttributeHandler<T, VOLUME, MAP>& ah) { this->AttributeHandler<T, VOLUME, MAP>::operator=(ah); return *this; }
	VolumeAttribute<T, MAP>& operator=(const AttributeHandler<T, VERTEX, MAP>& ah) { this->AttributeHandler<T, VOLUME, MAP>::operator=(ah); return *this; }
312 313
};

untereiner's avatar
untereiner committed
314 315 316 317 318

// 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
319 320 321 322 323
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
324

Sylvain Thery's avatar
Sylvain Thery committed
325 326 327 328 329 330 331 332 333 334 335 336 337
/**
 * apply function on each element of attribute
 * Warning attr must also be captured by lambda funct
 */
template <typename ATTR, typename FUNC>
inline void foreach_attribute(ATTR& attr, FUNC func)
{
	for (unsigned int id=attr.begin(); id != attr.end(); attr.next(id))
		func(id);
}

namespace Parallel
{
338

Sylvain Thery's avatar
Sylvain Thery committed
339
template <typename ATTR, typename FUNC>
340 341
void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbth = NumberOfThreads);

Sylvain Thery's avatar
Sylvain Thery committed
342 343
}

Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346 347 348
} // namespace CGoGN

#include "Topology/generic/attributeHandler.hpp"

#endif