attributeHandler.h 9.03 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
	void setValid() { valid = true ; }
76
} ;
77

78 79 80 81 82 83 84 85 86 87 88
template <unsigned int ORB>
class AttributeHandlerOrbit: public AttributeHandlerGen
{
public:
	AttributeHandlerOrbit(bool v) :
		AttributeHandlerGen(v)
	{}

	static const unsigned int ORBIT = ORB;
};

Pierre Kraemer's avatar
Pierre Kraemer committed
89 90
/**
 * Class that create an access-table to an existing attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
91
 * Main available operations are:
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93 94 95
 * - [ index ]
 * - [ dart ]
 * - begin / end / next to manage indexing
 */
96
template <typename T, unsigned int ORB, typename MAP>
97
class AttributeHandler : public AttributeHandlerOrbit<ORB>
Pierre Kraemer's avatar
Pierre Kraemer committed
98 99
{
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	// the map that contains the linked attribute
101
	MAP* m_map;
102
	// the multi-vector that contains attribute data
103
	AttributeMultiVector<T>* m_attrib;
Pierre Kraemer's avatar
Pierre Kraemer committed
104

105 106 107
	void registerInMap() ;
	void unregisterFromMap() ;

108 109
public:
	typedef T DATA_TYPE ;
110
//	static const unsigned int ORBIT = ORB;
111

112 113 114 115 116 117
	/**
	 * Default constructor
	 * Constructs a non-valid AttributeHandler (i.e. not linked to any attribute)
	 */
	AttributeHandler() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
118 119 120
	/**
	 * Constructor
	 * @param m the map which belong attribute
121
	 * @param amv a pointer to the AttributeMultiVector
Pierre Kraemer's avatar
Pierre Kraemer committed
122
	 */
123
	AttributeHandler(MAP* m, AttributeMultiVector<T>* amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127 128

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

untereiner's avatar
untereiner committed
131 132 133 134 135 136
	/**
	 * Transmute Constructor
	 * Construct an attribute of Orbit from Orbit2
	 * @param h the table attribute
	 */
	template <unsigned int ORBIT2>
137
	AttributeHandler(const AttributeHandler<T, ORBIT2, MAP>& h) ;
untereiner's avatar
untereiner committed
138

Pierre Kraemer's avatar
Pierre Kraemer committed
139 140 141 142
	/**
	 * affectation operator
	 * @param ta the table attribute to affect to this
	 */
143
	AttributeHandler<T, ORB, MAP>& operator=(const AttributeHandler<T, ORB, MAP>& ta) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144

untereiner's avatar
untereiner committed
145 146 147 148 149
	/**
	 * transmuted affectation operator
	 * @param ta the table attribute to affect to this
	 */
	template <unsigned int ORBIT2>
150
	AttributeHandler<T, ORB, MAP>& operator=(const AttributeHandler<T, ORBIT2, MAP>& ta) ;
untereiner's avatar
untereiner committed
151

Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154 155 156
	/**
	 * Destructor (empty & virtual)
	 */
	virtual ~AttributeHandler() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160
	/**
	 * @brief map
	 * @return the map that contains the attribute
	 */
161
	MAP* map() const
Pierre Kraemer's avatar
Pierre Kraemer committed
162 163 164 165
	{
		return m_map ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
166 167 168
	/**
	 * get attribute data vector
	 */
169
	AttributeMultiVector<T>* getDataVector() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
170

171 172 173 174 175 176 177 178 179 180
	/**
	 * 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
181
	/**
182
	 * get attribute orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
183
	 */
184
	virtual unsigned int getOrbit() const ;
185 186 187 188 189

	/**
	 * get attribute index
	 */
	unsigned int getIndex() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193

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

196 197 198 199
	/**
	 * get attribute type name
	 */
	virtual const std::string& typeName() const ;
200

201 202 203 204 205
	/**
	 * give the number of elements of the attribute container
	 */
	unsigned int nbElements() const;

Pierre Kraemer's avatar
Pierre Kraemer committed
206
	/**
207
	 * [] operator with cell parameter
Pierre Kraemer's avatar
Pierre Kraemer committed
208
	 */
209
	T& operator[](Cell<ORB> c) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
210 211

	/**
212
	 * const [] operator with cell parameter
Pierre Kraemer's avatar
Pierre Kraemer committed
213
	 */
214
	const T& operator[](Cell<ORB> c) const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

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

236
	/**
237
	 * initialize all the lines of the attribute with the given value
238
	 */
239
	void setAllValues(const T& v) ;
240

Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245 246 247
	/**
	 * begin of table
	 * @return the iterator of the begin of container
	 */
	unsigned int begin() const;

	/**
248
	 * end of table
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250 251 252 253
	 * @return the iterator of the end of container
	 */
	unsigned int end() const;

	/**
254
	 * Next on iterator (equivalent to stl ++)
Pierre Kraemer's avatar
Pierre Kraemer committed
255 256 257 258 259
	 * @param iter iterator to
	 */
	void next(unsigned int& iter) const;
} ;

260 261


262
/**
263
 *  c++11 shortcut for Dart Attribute (Handler)
264
 */
265
template <typename T, typename MAP>
266
using DartAttribute = AttributeHandler<T, DART, MAP>;
267

268 269 270
typedef AttributeHandlerOrbit<DART> DartAttributeGen;


271
/**
272
 *  c++11 shortcut for Vertex Attribute (Handler)
273
 */
274
template <typename T, typename MAP>
275
using VertexAttribute = AttributeHandler<T, VERTEX, MAP>;
276

277 278 279
typedef AttributeHandlerOrbit<VERTEX> VertexAttributeGen;


280
/**
281
 *  c++11 shortcut for Edge Attribute (Handler)
282
 */
283
template <typename T, typename MAP>
284
using EdgeAttribute = AttributeHandler<T, EDGE, MAP>;
285

286 287
typedef AttributeHandlerOrbit<EDGE> EdgeAttributeGen;

288
/**
289
 *  c++11 shortcut for Face Attribute (Handler)
290
 */
291
template <typename T, typename MAP>
292
using FaceAttribute = AttributeHandler<T, FACE, MAP>;
293

294 295
typedef AttributeHandlerOrbit<FACE> FaceAttributeGen;

296
/**
297
 *  c++11 shortcut for Volume Attribute (Handler)
298
 */
299
template <typename T, typename MAP>
300 301
using VolumeAttribute = AttributeHandler<T, VOLUME, MAP>;

302
typedef AttributeHandlerOrbit<VOLUME> VolumeAttributeGen;
303

304

untereiner's avatar
untereiner committed
305 306 307 308 309

// 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
310 311 312 313 314
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
315

Sylvain Thery's avatar
Sylvain Thery committed
316 317 318 319 320 321 322 323 324 325 326 327 328
/**
 * 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
{
329

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

Sylvain Thery's avatar
Sylvain Thery committed
333 334
}

335 336 337
template<unsigned int ORBIT>
inline bool checkAttributeHandlerOrbit(const AttributeHandlerGen& att)
{
Sylvain Thery's avatar
Sylvain Thery committed
338
	return dynamic_cast<const AttributeHandlerOrbit<ORBIT>*>(&att) != NULL;
339 340
}

Pierre Kraemer's avatar
Pierre Kraemer committed
341 342 343 344 345
} // namespace CGoGN

#include "Topology/generic/attributeHandler.hpp"

#endif