attributeHandler.h 8.33 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
/// Macro that checks if ATTRIBUTEHANDLER type is an AttributeHandler
#define CHECK_ATTRIBUTEHANDLER(ATTRIBUTEHANDLER)\
	static_assert(std::is_base_of<AttributeHandlerGen, ATTRIBUTEHANDLER>::value, "Error not AttributeHandler");
39

40 41 42 43
/// Macro that checks if ATTRIBUTEHANDLER type is an AttributeHandler of orbit ORBITVALUE
#define CHECK_ATTRIBUTEHANDLER_ORBIT(ATTRIBUTEHANDLER, ORBITVALUE)\
	static_assert(std::is_base_of<AttributeHandlerGen, ATTRIBUTEHANDLER>::value, "Error not AttributeHandler");\
	static_assert(ATTRIBUTEHANDLER::ORBIT == ORBITVALUE, "Error wrong orbit of AttributeHandler");
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47
namespace CGoGN
{

48 49 50
class AttributeHandlerGen
{
protected:
51
	friend class GenericMap ;
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	template <typename MAP_IMPL> friend class MapCommon;
53 54 55

	// boolean that states the validity of the handler
	bool valid ;
56 57

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	AttributeHandlerGen(bool v) :
59
		valid(v)
60 61
	{}

62
	bool isValid() const { return valid ; }
63

64 65 66 67
	virtual int getSizeOfType() const = 0;

	virtual unsigned int getOrbit() const = 0;

68
	virtual const std::string& name() const = 0;
69 70 71
	virtual const std::string& typeName() const = 0;

	virtual AttributeMultiVectorGen* getDataVectorGen() const = 0;
72

73
protected:
74
	void setInvalid() { valid = false ; }
75
} ;
76

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

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

96 97
public:
	typedef T DATA_TYPE ;
98
	static const unsigned int ORBIT = ORB;
99

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

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

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

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

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

untereiner's avatar
untereiner committed
133 134 135 136 137
	/**
	 * transmuted affectation operator
	 * @param ta the table attribute to affect to this
	 */
	template <unsigned int ORBIT2>
138
	AttributeHandler<T, ORB, MAP>& operator=(const AttributeHandler<T, ORBIT2, MAP>& ta) ;
untereiner's avatar
untereiner committed
139

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

Pierre Kraemer's avatar
Pierre Kraemer committed
145 146 147 148
	/**
	 * @brief map
	 * @return the map that contains the attribute
	 */
149
	MAP* map() const
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152 153
	{
		return m_map ;
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

248 249


250
/**
251
 *  c++11 shortcut for Dart Attribute (Handler)
252
 */
253
template <typename T, typename MAP>
254
using DartAttribute = AttributeHandler<T, DART, MAP>;
255

256
/**
257
 *  c++11 shortcut for Vertex Attribute (Handler)
258
 */
259
template <typename T, typename MAP>
260
using VertexAttribute = AttributeHandler<T, VERTEX, MAP>;
261 262

/**
263
 *  c++11 shortcut for Edge Attribute (Handler)
264
 */
265
template <typename T, typename MAP>
266
using EdgeAttribute = AttributeHandler<T, EDGE, MAP>;
267 268

/**
269
 *  c++11 shortcut for Face Attribute (Handler)
270
 */
271
template <typename T, typename MAP>
272
using FaceAttribute = AttributeHandler<T, FACE, MAP>;
273 274

/**
275
 *  c++11 shortcut for Volume Attribute (Handler)
276
 */
277
template <typename T, typename MAP>
278 279 280
using VolumeAttribute = AttributeHandler<T, VOLUME, MAP>;


281

untereiner's avatar
untereiner committed
282 283 284 285 286

// 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
287 288 289 290 291
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
292

Sylvain Thery's avatar
Sylvain Thery committed
293 294 295 296 297 298 299 300 301 302 303 304 305
/**
 * 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
{
306

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

Sylvain Thery's avatar
Sylvain Thery committed
310 311
}

Pierre Kraemer's avatar
Pierre Kraemer committed
312 313 314 315 316
} // namespace CGoGN

#include "Topology/generic/attributeHandler.hpp"

#endif