attributeMultiVector.h 7.22 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* 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 44 45 46 47 48 49 50 51
{
protected:
	std::string m_attName;

	std::string m_typeName;

	bool m_toProcess;

public:

52
	AttributeMultiVectorGen(const std::string& strName, const std::string& strType);
Pierre Kraemer's avatar
Pierre Kraemer committed
53

54
	AttributeMultiVectorGen();
Pierre Kraemer's avatar
Pierre Kraemer committed
55

56
 	virtual ~AttributeMultiVectorGen();
Pierre Kraemer's avatar
Pierre Kraemer committed
57 58 59 60 61 62 63 64 65 66 67 68

 	void toggleProcess();

 	void toggleNoProcess();

 	bool toProcess();

 	unsigned int BlockSize();

 	/**
 	 * construct a new object using this type
 	 */
69
 	virtual AttributeMultiVectorGen* new_obj()=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
70

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

73
	virtual bool swap(AttributeMultiVectorGen* atmvg)=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

	/**
	* add a vector in table
	*/
	virtual void addBlock()=0;

	/**
	* set the number of block
	*/
	virtual void setNbBlocks(unsigned int nbt)=0;

	/**
	* swap two element in the attribute container
	*/
	virtual void overwrite(unsigned int src_b, unsigned int src_id, unsigned int dst_b, unsigned int dst_id)=0;

	/**
	* Free the used memory
	*/
	virtual void free()=0;

	virtual void initElt(unsigned int id)=0;

	virtual void copyElt(unsigned int dst, unsigned int src)=0;

	/**
	* get name of the type of attribute
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
102
	const std::string& getTypeName() { return m_typeName; }
Pierre Kraemer's avatar
Pierre Kraemer committed
103

Pierre Kraemer's avatar
Pierre Kraemer committed
104 105 106 107 108
	void setTypeName(const std::string& n) { m_typeName = n ; }

	const std::string& getName() { return m_attName; }

	void setName(const std::string& n) { m_attName = n ; }
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110 111 112 113 114 115 116 117 118 119 120 121

	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);

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

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

122
	virtual bool merge(const AttributeMultiVectorGen& att)=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	virtual void addBlocksBefore(unsigned int nbb)=0;

	virtual unsigned int getStartAddresses(std::vector<void*>& addr, unsigned int& byteTableSize)=0;

	/**
	 * copy attribute j on i
	 */
	virtual void affect(unsigned int i, unsigned int j)=0;

	/**
	 * add attribute j to i
	 */
	virtual void add(unsigned int i, unsigned int j)=0;

	/**
	 * sub attribute j from i
	 */
	virtual void sub(unsigned int i, unsigned int j)=0;

	/**
	 * multiply attribute i by alpha
	 */
	virtual void mult(unsigned int i, double alpha)=0;

	/**
	 * divide attribute i by alpha
	 */
	virtual void div(unsigned int i, double alpha)=0;

	/**
	 * interpole attribute i and j in res (with alpha coef)
	 */
	virtual void lerp(unsigned res, unsigned int i, unsigned int j, double alpha)=0;

	/**
	 * load info of binary stream
	 * @param fs filestream
	 * @param name [out] name of attribute
	 * @param type [out] type of attribute
	 */
};

template <typename T>
Pierre Kraemer's avatar
Pierre Kraemer committed
167
class AttributeMultiVector: public AttributeMultiVectorGen
Pierre Kraemer's avatar
Pierre Kraemer committed
168 169 170 171
{
	/**
	* table of blocks of data ptr: vectors!
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
172
	std::vector<T*> m_tableData;
Pierre Kraemer's avatar
Pierre Kraemer committed
173 174 175

public:

176
	AttributeMultiVector(const std::string& strName, const std::string& strType);
Pierre Kraemer's avatar
Pierre Kraemer committed
177

178
	AttributeMultiVector();
Pierre Kraemer's avatar
Pierre Kraemer committed
179

180
	~AttributeMultiVector();
Pierre Kraemer's avatar
Pierre Kraemer committed
181

182
	bool copy(const AttributeMultiVectorGen* atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
183

184
	bool swap(AttributeMultiVectorGen* atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
185

186
	AttributeMultiVectorGen* new_obj();
Pierre Kraemer's avatar
Pierre Kraemer committed
187

188
	bool merge(const AttributeMultiVectorGen& att);
Pierre Kraemer's avatar
Pierre Kraemer committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	/**
	* get a reference on a elt
	* @param i index of element
	*/
	T& operator[](unsigned int i);
	/**
	* get a const reference on a elt
	* @param i index of element
	*/
	const T& operator[](unsigned int i) const ;

	/**
	 *  export en string d'un element
	 *  @param i index de l'element a sortie
	 */
	std::string output(unsigned int i);

	/**
	 *  import d'un element depuis une string
	 *  @param i index de l'element a importer
	 *  @param st string contenant l'element a importer
	 */
	void input(unsigned int i,const std::string& st);

	/**
	* add a block of data in the attribute container
	*/
	void addBlock();

	void initElt(unsigned int id);

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

	/**
	* set the number of block of the container
	* Usefull when we add a new attribute in not empty container
	* @param nbb the number of block to set
	*/
	void setNbBlocks(unsigned int nbb);

	void addBlocksBefore(unsigned int nbb);

	/**
	* swap to elements in container (usefull for compact function)
	* @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);

	/**
	* Free the used memory
	*/
	void free();

	/**
	 * Get the addresses of each block of data
	 */
	unsigned int getStartAddresses(std::vector<void*>& addr, unsigned int& byteTableSize);

	/**
	 * 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);

	// Processing attributes

	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);
};

Pierre Kraemer's avatar
Pierre Kraemer committed
279
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
280

281
#include "attributeMultiVector.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283

#endif