attributeMultiVector.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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25 26
#ifndef __ATTRIBUTE_MULTI_VECTOR__
#define __ATTRIBUTE_MULTI_VECTOR__
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29 30 31 32 33 34 35

#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>

#include <typeinfo>

36
#include "Container/sizeblock.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
37 38 39 40

namespace CGoGN
{

41
class AttributeMultiVectorGen
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43
{
protected:
44 45 46 47
	/**
	 * Name of the attribute
	 */
	std::string m_attrName;
Pierre Kraemer's avatar
Pierre Kraemer committed
48

49 50 51
	/**
	 * Name of the type of the attribute
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53
	std::string m_typeName;

54 55 56 57 58 59 60 61 62 63 64 65 66
	/**
	 * orbit of the attribute
	 */
	unsigned int m_orbit;

	/**
	 * index of the attribute in its container
	 */
	unsigned int m_index;

	/**
	 * Process or not the attribute in arithmetic operations
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
67 68 69
	bool m_toProcess;

public:
70
	AttributeMultiVectorGen(const std::string& strName, const std::string& strType);
Pierre Kraemer's avatar
Pierre Kraemer committed
71

72
	AttributeMultiVectorGen();
Pierre Kraemer's avatar
Pierre Kraemer committed
73

74
 	virtual ~AttributeMultiVectorGen();
Pierre Kraemer's avatar
Pierre Kraemer committed
75

76
 	virtual AttributeMultiVectorGen* new_obj() = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
77

78 79 80
	/**************************************
	 *             ACCESSORS              *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
81

82 83 84 85
	/**
	* get / set orbit of the attribute
	*/
 	unsigned int getOrbit() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86

87
 	void setOrbit(unsigned int id) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
88

89 90 91 92
	/**
	* get / set index of the attribute
	*/
 	unsigned int getIndex() const ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93

94
 	void setIndex(unsigned int id) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96

	/**
97
	* get / set name of the attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	*/
99 100 101
	const std::string& getName();

	void setName(const std::string& n);
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103

	/**
104
	* get / set name of the type of the attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
105
	*/
106 107 108
	const std::string& getTypeName();

	void setTypeName(const std::string& n);
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110

	/**
111 112 113 114 115 116 117 118 119 120
	 * get block size
	 */
 	unsigned int getBlockSize();

	/**************************************
	 *       MULTI VECTOR MANAGEMENT      *
	 **************************************/

	/**
	* add a block of data in the multi vector
Pierre Kraemer's avatar
Pierre Kraemer committed
121
	*/
122
	virtual void addBlock() = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
123 124

	/**
125
	* set the number of blocks
Pierre Kraemer's avatar
Pierre Kraemer committed
126
	*/
127 128
	virtual void setNbBlocks(unsigned int nbb) = 0;

129 130
	virtual unsigned int getNbBlocks() const = 0;

131
	virtual void addBlocksBefore(unsigned int nbb) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
132

133
	virtual bool copy(const AttributeMultiVectorGen* atmvg) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
134

135 136 137
	virtual bool swap(AttributeMultiVectorGen* atmvg) = 0;

	virtual bool merge(const AttributeMultiVectorGen& att) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139

	/**
140
	* free the used memory
Pierre Kraemer's avatar
Pierre Kraemer committed
141
	*/
142
	virtual void clear() = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
143

144 145 146
	/**************************************
	 *             DATA ACCESS            *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
147

148
	virtual unsigned int getBlocksPointers(std::vector<void*>& addr, unsigned int& byteBlockSize) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
149

150 151 152
	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
153

154
	virtual void initElt(unsigned int id) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
155

156
	virtual void copyElt(unsigned int dst, unsigned int src) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
157

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
158 159
	virtual void swapElt(unsigned int id1, unsigned int id2) = 0;

160
	virtual void overwrite(unsigned int src_b, unsigned int src_id, unsigned int dst_b, unsigned int dst_id) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
161

162 163 164
	/**************************************
	 *       ARITHMETIC OPERATIONS        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
165

166
 	void toggleProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
167

168
 	void toggleNoProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
169

170
 	bool toProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
171 172 173 174

	/**
	 * copy attribute j on i
	 */
175
	virtual void affect(unsigned int i, unsigned int j) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177 178 179

	/**
	 * add attribute j to i
	 */
180
	virtual void add(unsigned int i, unsigned int j) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184

	/**
	 * sub attribute j from i
	 */
185
	virtual void sub(unsigned int i, unsigned int j) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187 188 189

	/**
	 * multiply attribute i by alpha
	 */
190
	virtual void mult(unsigned int i, double alpha) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194

	/**
	 * divide attribute i by alpha
	 */
195
	virtual void div(unsigned int i, double alpha) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
196 197

	/**
198
	 * interpolate attribute i and j in res (with alpha coefficient)
Pierre Kraemer's avatar
Pierre Kraemer committed
199
	 */
200
	virtual void lerp(unsigned res, unsigned int i, unsigned int j, double alpha) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	/**************************************
	 *            SAVE & LOAD             *
	 **************************************/

	virtual std::string output(unsigned int i) = 0;

	virtual void input(unsigned int i,const std::string& st) = 0;

	virtual void saveBin(CGoGNostream& fs, unsigned int id) = 0;

	static unsigned int loadBinInfos(CGoGNistream& fs, std::string& name, std::string& type);

	virtual bool loadBin(CGoGNistream& fs) = 0;

	static bool skipLoadBin(CGoGNistream& fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
217 218
};

219 220 221 222 223

/***************************************************************************************************/
/***************************************************************************************************/


Pierre Kraemer's avatar
Pierre Kraemer committed
224
template <typename T>
225
class AttributeMultiVector : public AttributeMultiVectorGen
Pierre Kraemer's avatar
Pierre Kraemer committed
226 227
{
	/**
228
	* table of blocks of data pointers: vectors!
Pierre Kraemer's avatar
Pierre Kraemer committed
229
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
230
	std::vector<T*> m_tableData;
Pierre Kraemer's avatar
Pierre Kraemer committed
231 232

public:
233
	AttributeMultiVector(const std::string& strName, const std::string& strType);
Pierre Kraemer's avatar
Pierre Kraemer committed
234

235
	AttributeMultiVector();
Pierre Kraemer's avatar
Pierre Kraemer committed
236

237
	~AttributeMultiVector();
Pierre Kraemer's avatar
Pierre Kraemer committed
238

239 240 241 242 243 244 245 246 247 248
	AttributeMultiVectorGen* new_obj();

	/**************************************
	 *       MULTI VECTOR MANAGEMENT      *
	 **************************************/

	void addBlock();

	void setNbBlocks(unsigned int nbb);

249 250
	unsigned int getNbBlocks() const;

251 252
	void addBlocksBefore(unsigned int nbb);

253
	bool copy(const AttributeMultiVectorGen* atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
254

255
	bool swap(AttributeMultiVectorGen* atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
256

257
	bool merge(const AttributeMultiVectorGen& att);
Pierre Kraemer's avatar
Pierre Kraemer committed
258

259 260 261 262 263 264
	void clear();

	/**************************************
	 *             DATA ACCESS            *
	 **************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
265
	/**
266 267 268
	 * get a reference on a elt
	 * @param i index of element
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
269 270 271
	T& operator[](unsigned int i);

	/**
272 273
	 * get a const reference on a elt
	 * @param i index of element
Pierre Kraemer's avatar
Pierre Kraemer committed
274
	 */
275
	const T& operator[](unsigned int i) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
276 277

	/**
278
	 * Get the addresses of each block of data
Pierre Kraemer's avatar
Pierre Kraemer committed
279
	 */
280
	unsigned int getBlocksPointers(std::vector<void*>& addr, unsigned int& byteBlockSize);
Pierre Kraemer's avatar
Pierre Kraemer committed
281

282 283 284
	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
285 286 287 288 289

	void initElt(unsigned int id);

	void copyElt(unsigned int dst, unsigned int src);

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
290 291
	void swapElt(unsigned int id1, unsigned int id2);

Pierre Kraemer's avatar
Pierre Kraemer committed
292
	/**
293
	* swap two elements in container (useful for compact function)
Pierre Kraemer's avatar
Pierre Kraemer committed
294 295 296 297 298 299 300
	* @param src_b  block index of source element
	* @param src_id index in block of source element
	* @param dst_b  block index of destination element
	* @param dst_id index in block of destination element
	*/
	void overwrite(unsigned int src_b, unsigned int src_id, unsigned int dst_b, unsigned int dst_id);

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	/**************************************
	 *       ARITHMETIC OPERATIONS        *
	 **************************************/

	void affect(unsigned int i, unsigned int j);

	void add(unsigned int i, unsigned int j);

	void sub(unsigned int i, unsigned int j);

	void mult(unsigned int i, double alpha);

	void div(unsigned int i, double alpha);

	void lerp(unsigned res, unsigned int i, unsigned int j, double alpha);

	/**************************************
	 *            SAVE & LOAD             *
	 **************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
321
	/**
322 323 324 325
	 *  export en string d'un element
	 *  @param i index de l'element a sortie
	 */
	std::string output(unsigned int i);
Pierre Kraemer's avatar
Pierre Kraemer committed
326 327

	/**
328 329 330
	 *  import d'un element depuis une string
	 *  @param i index de l'element a importer
	 *  @param st string contenant l'element a importer
Pierre Kraemer's avatar
Pierre Kraemer committed
331
	 */
332
	void input(unsigned int i,const std::string& st);
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

	/**
	 * Sauvegarde binaire
	 * @param fs filestream
	 * @param id id of mv
	 */
	void saveBin(CGoGNostream& fs, unsigned int id);

	/**
	 * lecture binaire
	 * @param fs filestream
	 */
	bool loadBin(CGoGNistream& fs);
};

Pierre Kraemer's avatar
Pierre Kraemer committed
348
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
349

350
#include "attributeMultiVector.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
351 352

#endif