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

#include <iostream>
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28 29 30 31 32
#include <cassert>
#include "Container/registered.h"
#include "Utils/nameTypes.h"

namespace CGoGN
{

33
inline unsigned int AttributeContainer::getOrbit() const
34 35 36
{
	return m_orbit ;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
37

38 39 40 41 42 43 44 45
inline void AttributeContainer::setOrbit(unsigned int orbit)
{
	m_orbit = orbit ;
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->setOrbit(orbit);
	}
46 47 48 49 50

	for(unsigned int i = 0; i < m_tableMarkerAttribs.size(); ++i)
	{
		m_tableMarkerAttribs[i]->setOrbit(orbit);
	}
51 52 53
}

inline void AttributeContainer::setRegistry(std::map< std::string, RegisteredBaseAttribute* >* re)
Pierre Kraemer's avatar
Pierre Kraemer committed
54
{
55 56 57 58 59 60
	m_attributes_registry_map = re;
}

/**************************************
 *          BASIC FEATURES            *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
61

62 63 64
template <typename T>
AttributeMultiVector<T>* AttributeContainer::addAttribute(const std::string& attribName)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
65
	// first check if attribute already exist
66
	unsigned int index ;
Pierre Kraemer's avatar
Pierre Kraemer committed
67 68
	if (attribName != "")
	{
69 70
		index = getAttributeIndex(attribName) ;
		if (index != UNKNOWN)
71 72
		{
			std::cout << "attribute " << attribName << " already found.." << std::endl ;
73
			return NULL ;
74
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76
	}

77 78 79
	// create the new attribute
	std::string typeName = nameOfType(T()) ;
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, typeName) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82

	if(!m_freeIndices.empty())
	{
83
		index = m_freeIndices.back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
84
		m_freeIndices.pop_back() ;
85
		m_tableAttribs[index] = amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88
	}
	else
	{
89
		index = uint32(m_tableAttribs.size()) ;
90
		m_tableAttribs.push_back(amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
92

93 94 95 96 97
	amv->setOrbit(m_orbit) ;
	amv->setIndex(index) ;

	// generate a name for the attribute if no one was given
	if (attribName == "")
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	{
99 100 101
		std::stringstream ss ;
		ss << "unknown" << m_nbUnknown++ ;
		amv->setName(ss.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103
	}

104 105
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106

107
	// resize the new attribute so that it has the same size than others
108
	amv->setNbBlocks(uint32(m_holesBlocks.size())) ;
109 110

	m_nbAttributes++ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
111

112
	return amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114 115
}

template <typename T>
116
void AttributeContainer::addAttribute(const std::string& attribName, const std::string& nametype, unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
117 118 119 120
{
	// first check if attribute already exist
	if (attribName != "")
	{
121 122 123
		unsigned int i = getAttributeIndex(attribName) ;
		if (i != UNKNOWN)
			return ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125
	}

126
	// create the new attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
127
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, nametype);
128 129 130 131 132 133 134

	m_tableAttribs[index] = amv;
	amv->setOrbit(m_orbit) ;
	amv->setIndex(index) ;

	// generate a name for the attribute if no one was given
	if (attribName == "")
Pierre Kraemer's avatar
Pierre Kraemer committed
135 136 137 138 139
	{
		std::stringstream ss;
		ss << "unknown" << m_nbUnknown++;
		amv->setName(ss.str());
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
140

141 142
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
143

144
	// resize the new attribute so that it has the same size than others
145
	amv->setNbBlocks(uint32(m_holesBlocks.size())) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149

	m_nbAttributes++;
}

150 151 152 153 154 155 156
template <typename T>
bool AttributeContainer::removeAttribute(const std::string& attribName)
{
	unsigned int index = getAttributeIndex(attribName) ;

	if (index == UNKNOWN)
	{
157
		std::cerr << "removeAttribute by name: attribute not found (" << attribName << ")"<< std::endl ;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		return false ;
	}

	// delete the attribute
	delete m_tableAttribs[index] ;
	m_tableAttribs[index] = NULL ;

	if (index == m_tableAttribs.size() - 1)
		m_tableAttribs.pop_back() ;
	else
		m_freeIndices.push_back(index) ;

	--m_nbAttributes ;
	m_lineCost -= sizeof(T);

	return true ;
}

template <typename T>
bool AttributeContainer::removeAttribute(unsigned int index)
{
	if(m_tableAttribs[index] == NULL)
	{
		std::cerr << "removeAttribute by index: attribute not found" << std::endl ;
		return false ;
	}

	// delete the attribute
	delete m_tableAttribs[index] ;
	m_tableAttribs[index] = NULL ;

	if(index == m_tableAttribs.size() - 1)
		m_tableAttribs.pop_back() ;
	else
		m_freeIndices.push_back(index) ;

	--m_nbAttributes ;
	m_lineCost -= sizeof(T);

	return true ;
}

Sylvain Thery's avatar
Sylvain Thery committed
200
template <typename T>
201
unsigned int AttributeContainer::getAttributeBlocksPointers(unsigned int attrIndex, std::vector<T*>& vect_ptr, unsigned int& byteBlockSize) const
Sylvain Thery's avatar
Sylvain Thery committed
202 203 204 205 206 207
{
	assert(attrIndex < m_tableAttribs.size() || !"getAttributeBlocksPointers: attribute index out of bounds");
	assert(m_tableAttribs[attrIndex] != NULL || !"getAttributeBlocksPointers: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getAttributeBlocksPointers: wrong type");
208
	return atm->getBlocksPointers(reinterpret_cast<std::vector<void*>&>(vect_ptr), byteBlockSize);
Sylvain Thery's avatar
Sylvain Thery committed
209 210
}

211 212 213 214 215
/**************************************
 *      INFO ABOUT THE CONTAINER      *
 **************************************/

inline unsigned int AttributeContainer::getNbAttributes() const
Pierre Kraemer's avatar
Pierre Kraemer committed
216
{
Pierre Kraemer's avatar
Pierre Kraemer committed
217
	return m_nbAttributes;
Pierre Kraemer's avatar
Pierre Kraemer committed
218 219
}

220 221 222 223
inline unsigned int AttributeContainer::size() const
{
	return m_size;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
224

225
inline unsigned int AttributeContainer::capacity() const
Pierre Kraemer's avatar
Pierre Kraemer committed
226
{
227
	return uint32(m_holesBlocks.size() * _BLOCKSIZE_);
228
}
Pierre Kraemer's avatar
Pierre Kraemer committed
229

230 231
inline unsigned int AttributeContainer::memoryTotalSize() const
{
232
	return uint32(capacity() * (m_lineCost + 8));
233 234 235 236
}

inline unsigned int AttributeContainer::memorySize() const
{
237
	return uint32(size() * (m_lineCost + 8));
238
}
Pierre Kraemer's avatar
Pierre Kraemer committed
239

240 241
inline bool AttributeContainer::used(unsigned int index) const
{
Sylvain Thery's avatar
Sylvain Thery committed
242
	return m_holesBlocks[index / _BLOCKSIZE_]->used(index % _BLOCKSIZE_) != 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
243 244
}

Sylvain Thery's avatar
Sylvain Thery committed
245 246 247 248 249
inline float AttributeContainer::fragmentation()
{
	return float(m_size) / float(m_maxSize);
}

250 251 252 253
/**************************************
 *         CONTAINER TRAVERSAL        *
 **************************************/

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
//inline unsigned int AttributeContainer::begin() const
//{
//	unsigned int it = 0;
//	while ((it < m_maxSize) && (!used(it)))
//		++it;
//	return it;
//}

//inline unsigned int AttributeContainer::end() const
//{
//	return m_maxSize;
//}

//inline void AttributeContainer::next(unsigned int &it) const
//{
//	do
//	{
//		++it;
//	} while ((it < m_maxSize) && (!used(it)));
//}

275
inline unsigned int AttributeContainer::begin() const
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
{
	if (m_currentBrowser != NULL)
		return m_currentBrowser->begin();
	return AttributeContainer::realBegin();
}

inline unsigned int AttributeContainer::end() const
{
	if (m_currentBrowser != NULL)
		return m_currentBrowser->end();
	return AttributeContainer::realEnd();
}

inline void AttributeContainer::next(unsigned int &it) const
{
	if (m_currentBrowser != NULL)
		m_currentBrowser->next(it);
	else
		AttributeContainer::realNext(it);
}

inline unsigned int AttributeContainer::realBegin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
298
{
299 300 301 302 303
	unsigned int it = 0;
	while ((it < m_maxSize) && (!used(it)))
		++it;
	return it;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
304

305
inline unsigned int AttributeContainer::realEnd() const
306 307 308
{
	return m_maxSize;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
309

310
inline void AttributeContainer::realNext(unsigned int &it) const
311 312 313 314 315
{
	do
	{
		++it;
	} while ((it < m_maxSize) && (!used(it)));
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317
}

Sylvain Thery's avatar
Sylvain Thery committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
inline unsigned int AttributeContainer::realRBegin() const
{
	unsigned int it = m_maxSize-1;
	while ((it != 0xffffffff) && (!used(it)))
		--it;
	return it;
}

inline unsigned int AttributeContainer::realREnd() const
{
	return 0xffffffff; // -1
}

inline void AttributeContainer::realRNext(unsigned int &it) const
{
	do
	{
		--it;
	} while ((it !=0xffffffff) && (!used(it)));
}

339 340 341 342 343
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/

inline void AttributeContainer::initLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
344
{
345 346 347 348 349 350
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->initElt(index);
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
351

352 353
inline void AttributeContainer::initMarkersOfLine(unsigned int index)
{
354
	for(unsigned int i = 0; i < m_tableMarkerAttribs.size(); ++i)
355
	{
356
		m_tableMarkerAttribs[i]->initElt(index);
357 358 359
	}
}

360 361 362 363 364 365 366
inline void AttributeContainer::copyLine(unsigned int dstIndex, unsigned int srcIndex)
{
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->copyElt(dstIndex, srcIndex);
	}
367 368 369 370 371

	for(unsigned int i = 0; i < m_tableMarkerAttribs.size(); ++i)
	{
		m_tableMarkerAttribs[i]->copyElt(dstIndex, srcIndex);
	}
372
}
Pierre Kraemer's avatar
Pierre Kraemer committed
373

374 375 376
inline void AttributeContainer::refLine(unsigned int index)
{
	m_holesBlocks[index / _BLOCKSIZE_]->ref(index % _BLOCKSIZE_);
Pierre Kraemer's avatar
Pierre Kraemer committed
377 378
}

379 380 381 382 383 384 385 386 387
inline bool AttributeContainer::unrefLine(unsigned int index)
{
	if (m_holesBlocks[index / _BLOCKSIZE_]->unref(index % _BLOCKSIZE_))
	{
		--m_size;
		return true;
	}
	return false;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
388

389
inline unsigned int AttributeContainer::getNbRefs(unsigned int index) const
Pierre Kraemer's avatar
Pierre Kraemer committed
390
{
391 392
	unsigned int bi = index / _BLOCKSIZE_;
	unsigned int j = index % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
393

394
	return m_holesBlocks[bi]->nbRefs(j);
Pierre Kraemer's avatar
Pierre Kraemer committed
395 396
}

397
inline void AttributeContainer::setNbRefs(unsigned int index, unsigned int nb)
Pierre Kraemer's avatar
Pierre Kraemer committed
398
{
399 400
	m_holesBlocks[index / _BLOCKSIZE_]->setNbRefs(index % _BLOCKSIZE_, nb);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
401

402 403 404
/**************************************
 *       ATTRIBUTES MANAGEMENT        *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
405

406 407 408 409
inline bool AttributeContainer::copyAttribute(unsigned int index_dst, unsigned int index_src)
{
	return m_tableAttribs[index_dst]->copy(m_tableAttribs[index_src]);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
410

411 412 413
inline bool AttributeContainer::swapAttributes(unsigned int index1, unsigned int index2)
{
	return m_tableAttribs[index1]->swap(m_tableAttribs[index2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
414 415
}

416 417 418
/**************************************
 *       ATTRIBUTES DATA ACCESS       *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
419 420

template <typename T>
421
AttributeMultiVector<T>* AttributeContainer::getDataVector(unsigned int attrIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
422
{
423 424 425 426 427
	assert(attrIndex < m_tableAttribs.size() || !"getDataVector: attribute index out of bounds");
	assert(m_tableAttribs[attrIndex] != NULL || !"getDataVector: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getDataVector: wrong type");
428
	return atm;
Pierre Kraemer's avatar
Pierre Kraemer committed
429 430
}

431
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(unsigned int attrIndex)
432
{
433
	return m_tableAttribs[attrIndex];
434
}
Pierre Kraemer's avatar
Pierre Kraemer committed
435

436
template <typename T>
437
AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
438
{
439
	unsigned int index = getAttributeIndex(attribName) ;
440 441
	if(index == UNKNOWN)
		return NULL ;
442 443 444

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[index]);
	assert((atm != NULL) || !"getDataVector: wrong type");
445
	return atm;
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447
}

448 449 450 451 452 453 454 455
inline CGoGNCodeType AttributeContainer::getTypeCode(const std::string& attribName) const
{
	unsigned int index = getAttributeIndex(attribName) ;
	if(index == UNKNOWN)
		return CGoGNUNKNOWNTYPE ;
	return m_tableAttribs[index]->getTypeCode();
}

456
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
457
{
458
	unsigned int index = getAttributeIndex(attribName) ;
459 460 461 462
	if(index == UNKNOWN)
		return NULL ;
	else
		return m_tableAttribs[index];
Pierre Kraemer's avatar
Pierre Kraemer committed
463 464
}

465 466
template <typename T>
inline T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
467
{
468 469 470 471 472 473 474 475
	assert(eltIndex < m_maxSize || !"getData: element index out of bounds");
	assert(m_holesBlocks[eltIndex / _BLOCKSIZE_]->used(eltIndex % _BLOCKSIZE_) || !"getData: element does not exist");
	assert((m_tableAttribs[attrIndex] != NULL) || !"getData: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getData: wrong type");

	return atm->operator[](eltIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
476 477
}

478 479
template <typename T>
inline const T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex) const
Pierre Kraemer's avatar
Pierre Kraemer committed
480
{
481 482 483 484 485 486 487 488
	assert(eltIndex < m_maxSize || !"getData: element index out of bounds");
	assert(m_holesBlocks[eltIndex / _BLOCKSIZE_]->used(eltIndex % _BLOCKSIZE_) || !"getData: element does not exist");
	assert((m_tableAttribs[attrIndex] != NULL) || !"getData: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getData: wrong type");

	return atm->operator[](eltIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
489 490
}

491 492
template <typename T>
inline void AttributeContainer::setData(unsigned int attrIndex, unsigned int eltIndex, const T& data)
Pierre Kraemer's avatar
Pierre Kraemer committed
493
{
494 495 496 497 498 499 500 501
	assert(eltIndex < m_maxSize || !"getData: element index out of bounds");
	assert(m_holesBlocks[eltIndex / _BLOCKSIZE_]->used(eltIndex % _BLOCKSIZE_) || !"getData: element does not exist");
	assert((m_tableAttribs[attrIndex] != NULL) || !"getData: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getData: wrong type");

	atm->operator[](eltIndex) = data;
Pierre Kraemer's avatar
Pierre Kraemer committed
502 503
}

504 505 506 507 508
inline std::vector<AttributeMultiVector<MarkerBool>*>& AttributeContainer::getMarkerAttributes()
{
	return m_tableMarkerAttribs;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
509
} // namespace CGoGN