attributeMultiVector.h 9.16 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
	const std::string& getName() const;
100 101

	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
	const std::string& getTypeName() const;
107 108

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

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

	/**************************************
	 *       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 147 148
	/**
	 * get size of type
	 */
	virtual int getSizeOfType() const = 0;

149 150 151
	/**************************************
	 *             DATA ACCESS            *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
152

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

155 156 157
	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
158

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

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

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

165
	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
166

167 168 169
	/**************************************
	 *       ARITHMETIC OPERATIONS        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
170

171
 	void toggleProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
172

173
 	void toggleNoProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
174

175
 	bool toProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177 178 179

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

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

	/**
	 * sub attribute j from i
	 */
190
	virtual void sub(unsigned int i, unsigned int j) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194

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

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

	/**
203
	 * interpolate attribute i and j in res (with alpha coefficient)
Pierre Kraemer's avatar
Pierre Kraemer committed
204
	 */
205
	virtual void lerp(unsigned res, unsigned int i, unsigned int j, double alpha) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	/**************************************
	 *            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
222 223
};

224 225 226 227 228

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


Pierre Kraemer's avatar
Pierre Kraemer committed
229
template <typename T>
230
class AttributeMultiVector : public AttributeMultiVectorGen
Pierre Kraemer's avatar
Pierre Kraemer committed
231 232
{
	/**
233
	* table of blocks of data pointers: vectors!
Pierre Kraemer's avatar
Pierre Kraemer committed
234
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
235
	std::vector<T*> m_tableData;
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237

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

240
	AttributeMultiVector();
Pierre Kraemer's avatar
Pierre Kraemer committed
241

242
	~AttributeMultiVector();
Pierre Kraemer's avatar
Pierre Kraemer committed
243

244 245 246 247 248 249 250 251 252 253
	AttributeMultiVectorGen* new_obj();

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

	void addBlock();

	void setNbBlocks(unsigned int nbb);

254 255
	unsigned int getNbBlocks() const;

256 257
	void addBlocksBefore(unsigned int nbb);

258
	bool copy(const AttributeMultiVectorGen* atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
259

260
	bool swap(AttributeMultiVectorGen* atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
261

262
	bool merge(const AttributeMultiVectorGen& att);
Pierre Kraemer's avatar
Pierre Kraemer committed
263

264 265
	void clear();

266 267
	int getSizeOfType() const;

268 269 270 271
	/**************************************
	 *             DATA ACCESS            *
	 **************************************/

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

	/**
279 280
	 * get a const reference on a elt
	 * @param i index of element
Pierre Kraemer's avatar
Pierre Kraemer committed
281
	 */
282
	const T& operator[](unsigned int i) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
283 284

	/**
285
	 * Get the addresses of each block of data
Pierre Kraemer's avatar
Pierre Kraemer committed
286
	 */
287
	unsigned int getBlocksPointers(std::vector<void*>& addr, unsigned int& byteBlockSize) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
288

289 290 291
	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
292 293 294 295 296

	void initElt(unsigned int id);

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
299
	/**
300
	* swap two elements in container (useful for compact function)
Pierre Kraemer's avatar
Pierre Kraemer committed
301 302 303 304 305 306 307
	* @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);

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	/**************************************
	 *       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
328
	/**
329 330 331 332
	 *  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
333 334

	/**
335 336 337
	 *  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
338
	 */
339
	void input(unsigned int i,const std::string& st);
Pierre Kraemer's avatar
Pierre Kraemer committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

	/**
	 * 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
355
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
356

357
#include "attributeMultiVector.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
358 359

#endif