attributeContainer.h 14 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_CONTAINER__
#define __ATTRIBUTE_CONTAINER__
Pierre Kraemer's avatar
Pierre Kraemer committed
27
28
29

#include "Container/sizeblock.h"
#include "Container/holeblockref.h"
30
#include "Container/attributeMultiVector.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
31
32
33
34
35
36
37
38

#include <vector>
#include <map>

namespace CGoGN
{

class RegisteredBaseAttribute;
39
40
41
42
43
44
45
46
47
48
49
class AttributeContainer;

class ContainerBrowser
{
public:
	virtual unsigned int begin() const = 0;
	virtual unsigned int end() const = 0;
	virtual void next(unsigned int &it) const = 0;
	virtual void enable() = 0;
	virtual void disable() = 0;
};
Pierre Kraemer's avatar
Pierre Kraemer committed
50
51

/**
52
53
54
55
 * Container for AttributeMultiVectors
 * All the attributes always have the same size and
 * the management of holes is shared by all attributes
 */
56
class AttributeContainer
Pierre Kraemer's avatar
Pierre Kraemer committed
57
{
58
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
59
	/**
60
	* constante d'attribut inconnu
Pierre Kraemer's avatar
Pierre Kraemer committed
61
	*/
62
	static const unsigned int UNKNOWN = 0xffffffff;
Pierre Kraemer's avatar
Pierre Kraemer committed
63
64

	/**
65
	* Taille du bloc
Pierre Kraemer's avatar
Pierre Kraemer committed
66
	*/
Sylvain Thery's avatar
Sylvain Thery committed
67
//	static const unsigned int BlockSize = _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
68

69
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
70
	/**
71
	* vector of pointers to AttributeMultiVectors
Pierre Kraemer's avatar
Pierre Kraemer committed
72
	*/
73
	std::vector<AttributeMultiVectorGen*> m_tableAttribs;
Pierre Kraemer's avatar
Pierre Kraemer committed
74

75
76
77
78
79
	/**
	* vector of pointers to AttributeMultiVectors of MarkerBool
	*/
	std::vector<AttributeMultiVector<MarkerBool>*> m_tableMarkerAttribs;

80
81
82
	/**
	 * vector of free indices in the vector of AttributeMultiVectors
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
	std::vector<unsigned int> m_freeIndices;

Pierre Kraemer's avatar
Pierre Kraemer committed
85
	/**
86
	* vector of pointers to HoleBlockRef -> structure that manages holes and refs
Pierre Kraemer's avatar
Pierre Kraemer committed
87
	*/
88
	std::vector<HoleBlockRef*> m_holesBlocks;
Pierre Kraemer's avatar
Pierre Kraemer committed
89

90
91
92
93
94
95
96
97
98
	/**
	* vector of indices of blocks that have free space
	*/
	std::vector<unsigned int> m_tableBlocksWithFree;

	/**
	* vector of indices of blocks that are empty
	*/
	std::vector<unsigned int> m_tableBlocksEmpty;
Pierre Kraemer's avatar
Pierre Kraemer committed
99

100
101
	ContainerBrowser* m_currentBrowser;

Pierre Kraemer's avatar
Pierre Kraemer committed
102
	/**
103
104
105
106
107
108
	 * orbit of the container
	 */
	unsigned int m_orbit;

	/**
	* number of attributes
Pierre Kraemer's avatar
Pierre Kraemer committed
109
110
111
	*/ 
	unsigned int m_nbAttributes;

112
113
114
115
116
	/**
	 * counter for attributes without name
	 */
	unsigned int m_nbUnknown;

Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
119
120
121
122
123
124
125
126
127
	/**
	* size (number of elts) of the container
	*/
	unsigned int m_size;

	/**
	* size of the container with holes
	*/
	unsigned int m_maxSize;

	/**
128
	* memory cost of each line
Pierre Kraemer's avatar
Pierre Kraemer committed
129
130
131
132
	*/
	unsigned int m_lineCost;

	/**
133
	 * map pointer (shared for all container of the same map) for attribute registration
Pierre Kraemer's avatar
Pierre Kraemer committed
134
	 */
135
	std::map<std::string, RegisteredBaseAttribute*>* m_attributes_registry_map;
Pierre Kraemer's avatar
Pierre Kraemer committed
136

137
138
public:
	AttributeContainer();
Pierre Kraemer's avatar
Pierre Kraemer committed
139

140
141
	~AttributeContainer();

142
143
	unsigned int getOrbit() const;

144
145
146
147
	void setOrbit(unsigned int orbit);

	void setRegistry(std::map<std::string, RegisteredBaseAttribute*>* re);

148
149
	void setContainerBrowser(ContainerBrowser* bro) { m_currentBrowser = bro;}

150
151
	bool hasBrowser() { return m_currentBrowser != NULL; }

152
153
154
	/**************************************
	 *          BASIC FEATURES            *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156

	/**
157
158
159
160
	 * add a new attribute to the container
	 * @param T (template) type of the new attribute
	 * @param attribName name of the new attribute
	 * @return pointer to the new AttributeMultiVector
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	 */
162
163
	template <typename T>
	AttributeMultiVector<T>* addAttribute(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
164

165
166
167
168
	/// special version for marker
	AttributeMultiVector<MarkerBool>* addMarkerAttribute(const std::string& attribName);


169
170
171
172
173
174
175
176
177
	/**
	 * add a new attribute to the container
	 * @param typeName type of the new attribute in a string
	 * @param attribName name of the new attribute
	 * @return pointer to the new AttributeMultiVectorGen (unknown type inside)
	 */
	AttributeMultiVectorGen* addAttribute(const std::string& typeName, const std::string& attribName);


178
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
179
	/**
180
181
182
183
184
	 * add a new attribute with a given index (for load only)
	 * @param T (template) type of the new attribute
	 * @param attribName name of the new attribute
	 * @param typeName name of the new attribute's type
	 * @param index index of the new attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
185
	 */
186
187
	template <typename T>
	void addAttribute(const std::string& attribName, const std::string& typeName, unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
188
189
190

public:
	/**
191
192
193
	* Remove an attribute (destroys data)
	* @param attribName name of the attribute to remove
	* @return removed or not
Pierre Kraemer's avatar
Pierre Kraemer committed
194
	*/
195
	template <typename T>
196
	bool removeAttribute(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
197

198
199
	bool removeMarkerAttribute(const std::string& attribName);

Pierre Kraemer's avatar
Pierre Kraemer committed
200
	/**
201
202
203
	* Remove an attribute (destroys data)
	* @param index index of the attribute to remove
	* @return removed or not
Pierre Kraemer's avatar
Pierre Kraemer committed
204
	*/
205
	template <typename T>
206
207
208
209
210
	bool removeAttribute(unsigned int index);

	/**************************************
	 *      INFO ABOUT THE CONTAINER      *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
211
212

	/**
213
214
215
	 * Number of attributes of the container
	 */
	unsigned int getNbAttributes() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
216
217

	/**
218
	* Size of the container (number of lines)
Pierre Kraemer's avatar
Pierre Kraemer committed
219
	*/
220
	unsigned int size() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
221
222

	/**
223
	* Capacity of the container (number of lines including holes)
Pierre Kraemer's avatar
Pierre Kraemer committed
224
	*/
225
	unsigned int capacity() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
226
227

	/**
228
229
230
	* Total memory cost of container
	*/
	unsigned int memoryTotalSize() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
231

232
233
234
235
236
	/**
	* Memory cost of every used line
	*/
	unsigned int memorySize() const;

Pierre Kraemer's avatar
Pierre Kraemer committed
237
	/**
238
239
240
	* is the line used in the container
	*/
	inline bool used(unsigned int index) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
241

242
243
244
245
246
	/**
	 * @brief check if container contain marker attribute
	 */
	bool hasMarkerAttribute() const;

247
248
249
	/**************************************
	 *         CONTAINER TRAVERSAL        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
250
251

	/**
252
	 * return the index of the first line of the container
Pierre Kraemer's avatar
Pierre Kraemer committed
253
	 */
254
	unsigned int begin() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
255
256

	/**
Sylvain Thery's avatar
Sylvain Thery committed
257
	 * return the index after the last line of the container
Pierre Kraemer's avatar
Pierre Kraemer committed
258
	 */
259
	unsigned int end() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
260
261

	/**
262
263
	 * get the index of the line after it in the container
	 * MUST BE USED INSTEAD OF ++ !
Pierre Kraemer's avatar
Pierre Kraemer committed
264
	 */
265
	void next(unsigned int &it) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
266

267
268
269
270
271
272
273
274


	/**
	 * return the index of the first line of the container
	 */
	unsigned int realBegin() const;

	/**
Sylvain Thery's avatar
Sylvain Thery committed
275
	 * return the index after the last line of the container
276
277
278
279
280
281
282
283
284
285
	 */
	unsigned int realEnd() const;

	/**
	 * get the index of the line after it in the container
	 * MUST BE USED INSTEAD OF ++ !
	 */
	void realNext(unsigned int &it) const;


Sylvain Thery's avatar
Sylvain Thery committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	/**
	 * return the index of the last line of the container
	 */
	unsigned int realRBegin() const;

	/**
	 * return the index before the first line of the container
	 */
	unsigned int realREnd() const;

	/**
	 * get the index of the line before "it" in the container
	 * MUST BE USED INSTEAD OF ++ !
	 */
	void realRNext(unsigned int &it) const;

302
303
304
	/**************************************
	 *       INFO ABOUT ATTRIBUTES        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
305
306
307
308

	/**
	* recuperation du code d'un attribut
	* @param attribName nom de l'attribut
309
	* @return l'indice de l'attribut
Pierre Kraemer's avatar
Pierre Kraemer committed
310
	*/
311
	unsigned int getAttributeIndex(const std::string& attribName) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
312
313
314
315

	/**
	 * get the name of an attribute, given its index in the container
	 */
316
	const std::string& getAttributeName(unsigned int attrIndex) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
317

Pierre Kraemer's avatar
Pierre Kraemer committed
318
	/**
319
320
321
322
323
	 * fill a vector with pointers to the blocks of the given attribute
	 * @param attrIndex index of the attribute
	 * @param vect_addr (OUT) vector of pointers
	 * @param byteBlockSize (OUT) size in bytes of each block
	 * @return number of blocks
Pierre Kraemer's avatar
Pierre Kraemer committed
324
	 */
325
326
	template<typename T>
	unsigned int getAttributeBlocksPointers(unsigned int attrIndex, std::vector<T*>& vect_ptr, unsigned int& byteBlockSize);
Pierre Kraemer's avatar
Pierre Kraemer committed
327
328

	/**
Sylvain Thery's avatar
Sylvain Thery committed
329
330
331
	 * fill a vector with attributes names
	 * @param names vector of names
	 * @return number of attributes
Pierre Kraemer's avatar
Pierre Kraemer committed
332
	 */
333
	unsigned int getAttributesNames(std::vector<std::string>& names) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
334

335

Sylvain Thery's avatar
Sylvain Thery committed
336
337
338
339
340
341
342
	/**
	 * fill a vector with attribute type names
	 * @param types vector of type names
	 * @return number of attributes
	 */
	unsigned int getAttributesTypes(std::vector<std::string>& types);

343
344
	std::vector<AttributeMultiVector<MarkerBool>*>& getMarkerAttributes();

345
346
347
	/**************************************
	 *        CONTAINER MANAGEMENT        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
348
349

	/**
350
	 * swap two containers
Pierre Kraemer's avatar
Pierre Kraemer committed
351
	 */
352
	void swap(AttributeContainer& cont);
Pierre Kraemer's avatar
Pierre Kraemer committed
353
354

	/**
355
356
357
358
	 * clear the container
	 * @param removeAttrib remove the attributes (not only their data)
	 */
	void clear(bool clearAttrib = false);
Pierre Kraemer's avatar
Pierre Kraemer committed
359
360

	/**
361
362
363
364
	 * container compacting
	 * @param mapOldNew table that contains a map from old indices to new indices (holes -> 0xffffffff)
	 */
	void compact(std::vector<unsigned int>& mapOldNew);
Pierre Kraemer's avatar
Pierre Kraemer committed
365

Sylvain Thery's avatar
Sylvain Thery committed
366
367
368
369
370
371
372
	/**
	 * Test the fragmentation of container,
	 * in fact just size/max_size
	 * @return 1 if full filled - 0 is lots of holes
	 */
	inline float fragmentation();

373
374
375
	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
376
377

	/**
378
379
	* insert a line in the container
	* @return index of the line
Pierre Kraemer's avatar
Pierre Kraemer committed
380
	*/
381
	unsigned int insertLine();
Pierre Kraemer's avatar
Pierre Kraemer committed
382
383

	/**
384
385
	* remove a line in the container
	* @param index index of the line to remove
Pierre Kraemer's avatar
Pierre Kraemer committed
386
	*/
387
	void removeLine(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
388
389

	/**
390
391
392
	 * initialize a line of the container (an element of each attribute)
	 */
	void initLine(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
393

394
395
396
397
398
	/**
	 * initialize all markers of a line of the container
	 */
	void initMarkersOfLine(unsigned int index);

Pierre Kraemer's avatar
Pierre Kraemer committed
399
	/**
400
401
402
	 * copy the content of line src in line dst
	 */
	void copyLine(unsigned int dstIndex, unsigned int srcIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
403
404

	/**
405
406
	* increment the ref counter of the given line
	* @param index index of the line
Pierre Kraemer's avatar
Pierre Kraemer committed
407
	*/
408
	void refLine(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
409
410

	/**
411
412
413
	* decrement the ref counter of the given line
	* @param index index of the line
	* @return true if the line was removed
Pierre Kraemer's avatar
Pierre Kraemer committed
414
	*/
415
	bool unrefLine(unsigned int eltIdx);
Pierre Kraemer's avatar
Pierre Kraemer committed
416
417

	/**
418
419
420
	* get the number of refs of the given line
	* @param index index of the line
	* @return number of refs of the line
Pierre Kraemer's avatar
Pierre Kraemer committed
421
	*/
422
	unsigned int getNbRefs(unsigned int index) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
423
424

	/**
425
426
427
	* set the number of refs of the given line
	* @param index index of the line
	* @param nb number of refs
Pierre Kraemer's avatar
Pierre Kraemer committed
428
	*/
429
	void setNbRefs(unsigned int eltIdx, unsigned int nb);
Pierre Kraemer's avatar
Pierre Kraemer committed
430

431
432
433
	/**************************************
	 *       ATTRIBUTES MANAGEMENT        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
434
435

	/**
436
437
438
	 * copy the data of attribute src in attribute dst (type has to be the same)
	 */
	bool copyAttribute(unsigned int dstIndex, unsigned int srcIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
439
440

	/**
441
442
443
	 * swap the data of attribute 1 with attribute 2 (type has to be the same)
	 */
	bool swapAttributes(unsigned int index1, unsigned int index2);
Pierre Kraemer's avatar
Pierre Kraemer committed
444

445
446
447
	/**************************************
	 *       ATTRIBUTES DATA ACCESS       *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
448

449
450
	inline CGoGNCodeType getTypeCode(const std::string& attribName) const;

Pierre Kraemer's avatar
Pierre Kraemer committed
451
	/**
452
453
	* get an AttributeMultiVector
	* @param attrIndex index of the attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
454
455
	*/
	template<typename T>
456
	AttributeMultiVector<T>* getDataVector(unsigned int attrIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
457

458
	AttributeMultiVectorGen* getVirtualDataVector(unsigned int attrIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
459

460

Pierre Kraemer's avatar
Pierre Kraemer committed
461
	/**
462
463
	* get an AttributeMultiVector
	* @param attribName name of the attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
464
	*/
465
	template<typename T>
466
	AttributeMultiVector<T>* getDataVector(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
467

468
	AttributeMultiVectorGen* getVirtualDataVector(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
469
470

	/**
471
472
473
474
475
	* get a given element of a given attribute
	* @param T type of the attribute
	* @param attrIndex index of the attribute
	* @param eltIndex index of the element
	* @return a reference on the element
Pierre Kraemer's avatar
Pierre Kraemer committed
476
	*/
477
478
	template <typename T>
	T& getData(unsigned int attrIndex, unsigned int eltIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
479
480

	/**
481
482
483
484
485
	* get a given const element of a given attribute
	* @param T type of the attribute
	* @param attrIndex index of the attribute
	* @param eltIndex index of the element
	* @return a const reference on the element
Pierre Kraemer's avatar
Pierre Kraemer committed
486
	*/
487
488
	template <typename T>
	const T& getData(unsigned int attrIndex, unsigned int eltIndex) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
489
490

	/**
491
492
493
494
495
	* set a given element of a given attribute
	* @param T type of the attribute
	* @param attrIndex index of the attribute
	* @param eltIndex index of the element
	* @param data data to insert
Pierre Kraemer's avatar
Pierre Kraemer committed
496
	*/
497
498
	template <typename T>
	void setData(unsigned int attrIndex, unsigned int eltIndex, const T& data);
Pierre Kraemer's avatar
Pierre Kraemer committed
499
500


501
502
503
504
505
506
507
508
509
510
511

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

public:
	/**
	* save binary file
	* @param fs a file stream
	* @param id the id to save
	*/
Sylvain Thery's avatar
Sylvain Thery committed
512
	void saveBin(CGoGNostream& fs, unsigned int id) const;
513
514
515
516
517
518
519
520
521
522
523
524
525
526

	/**
	* get id from file binary stream
	* @param fs file stream
	* @return the id of attribute container
	*/
	static unsigned int loadBinId(CGoGNistream& fs);

	/**
	* load from binary file
	* @param fs a file stream
	* @param id  ??
	*/
	bool loadBin(CGoGNistream& fs);
527
528
529
530
531
532
533

	/**
	 * copy container
	 * TODO a version that compact on the fly ?
	 */
	void copyFrom(const AttributeContainer& cont);

534
535
536
537
538
539
540
541
	/**
	 * dump the container in CSV format (; separated columns)
	 */
	void dumpCSV() const;

	void dumpByLines() const;


Pierre Kraemer's avatar
Pierre Kraemer committed
542
543
544
545
};

} // namespace CGoGN

546
#include "attributeContainer.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
547
548

#endif