attributeContainer.hpp 13.5 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-2011, 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.u-strasbg.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 34 35 36
inline unsigned int AttributeContainer::getOrbit()
{
	return m_orbit ;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
37

38 39 40 41 42 43 44 45 46 47 48
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);
	}
}

inline void AttributeContainer::setRegistry(std::map< std::string, RegisteredBaseAttribute* >* re)
Pierre Kraemer's avatar
Pierre Kraemer committed
49
{
50 51 52 53 54 55
	m_attributes_registry_map = re;
}

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

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

72 73 74
	// create the new attribute
	std::string typeName = nameOfType(T()) ;
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, typeName) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77

	if(!m_freeIndices.empty())
	{
78
		index = m_freeIndices.back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
79
		m_freeIndices.pop_back() ;
80
		m_tableAttribs[index] = amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83
	}
	else
	{
84 85
		index = m_tableAttribs.size() ;
		m_tableAttribs.push_back(amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
87

88 89 90 91 92
	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
93
	{
94 95 96
		std::stringstream ss ;
		ss << "unknown" << m_nbUnknown++ ;
		amv->setName(ss.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98
	}

99 100
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
101

102 103 104 105
	// resize the new attribute so that it has the same size than others
	amv->setNbBlocks(m_holesBlocks.size()) ;

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

107
	return amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109 110
}

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

121
	// create the new attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
122
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, nametype);
123 124 125 126 127 128 129

	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
130 131 132 133 134
	{
		std::stringstream ss;
		ss << "unknown" << m_nbUnknown++;
		amv->setName(ss.str());
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
135

136 137
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138

139 140
	// resize the new attribute so that it has the same size than others
	amv->setNbBlocks(m_holesBlocks.size()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142 143 144

	m_nbAttributes++;
}

145 146 147 148 149 150 151 152 153 154 155 156 157 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
template <typename T>
bool AttributeContainer::removeAttribute(const std::string& attribName)
{
	unsigned int index = getAttributeIndex(attribName) ;

	if (index == UNKNOWN)
	{
		std::cerr << "removeAttribute by name: 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 ;
}

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


196 197 198 199 200
/**************************************
 *      INFO ABOUT THE CONTAINER      *
 **************************************/

inline unsigned int AttributeContainer::getNbAttributes() const
Pierre Kraemer's avatar
Pierre Kraemer committed
201
{
Pierre Kraemer's avatar
Pierre Kraemer committed
202
	return m_nbAttributes;
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204
}

205 206 207 208
inline unsigned int AttributeContainer::size() const
{
	return m_size;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
209

210
inline unsigned int AttributeContainer::capacity() const
Pierre Kraemer's avatar
Pierre Kraemer committed
211
{
212 213
	return m_holesBlocks.size() * _BLOCKSIZE_;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
214

215 216
inline unsigned int AttributeContainer::memoryTotalSize() const
{
217 218 219 220 221 222
	return capacity() * (m_lineCost + 8);
}

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

225 226 227
inline bool AttributeContainer::used(unsigned int index) const
{
	return m_holesBlocks[index / _BLOCKSIZE_]->used(index % _BLOCKSIZE_);
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229
}

230 231 232 233 234
/**************************************
 *         CONTAINER TRAVERSAL        *
 **************************************/

inline unsigned int AttributeContainer::begin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
235
{
236 237 238 239 240
	unsigned int it = 0;
	while ((it < m_maxSize) && (!used(it)))
		++it;
	return it;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
241

242 243 244 245
inline unsigned int AttributeContainer::end() const
{
	return m_maxSize;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
246

247 248 249 250 251 252
inline void AttributeContainer::next(unsigned int &it) const
{
	do
	{
		++it;
	} while ((it < m_maxSize) && (!used(it)));
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254
}

255 256 257 258 259
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/

inline void AttributeContainer::initLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
260
{
261 262 263 264 265 266
	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
267

268 269 270 271 272 273 274 275
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);
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
276

277 278 279
inline void AttributeContainer::refLine(unsigned int index)
{
	m_holesBlocks[index / _BLOCKSIZE_]->ref(index % _BLOCKSIZE_);
Pierre Kraemer's avatar
Pierre Kraemer committed
280 281
}

282 283 284 285 286 287 288 289 290
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
291

292
inline unsigned int AttributeContainer::getNbRefs(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
293
{
294 295
	unsigned int bi = index / _BLOCKSIZE_;
	unsigned int j = index % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
296

297
	return m_holesBlocks[bi]->nbRefs(j);
Pierre Kraemer's avatar
Pierre Kraemer committed
298 299
}

300
inline void AttributeContainer::setNbRefs(unsigned int index, unsigned int nb)
Pierre Kraemer's avatar
Pierre Kraemer committed
301
{
302 303
	m_holesBlocks[index / _BLOCKSIZE_]->setNbRefs(index % _BLOCKSIZE_, nb);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
304

305 306 307
/**************************************
 *       ATTRIBUTES MANAGEMENT        *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
308

309 310 311 312
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
313

314 315 316
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
317 318
}

319 320 321
/**************************************
 *       ATTRIBUTES DATA ACCESS       *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323

template <typename T>
324
AttributeMultiVector<T>* AttributeContainer::getDataVector(unsigned int attrIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
325
{
326 327 328 329 330
	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");
331
	return atm;
Pierre Kraemer's avatar
Pierre Kraemer committed
332 333
}

334
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(unsigned int attrIndex)
335
{
336
	return m_tableAttribs[attrIndex];
337
}
Pierre Kraemer's avatar
Pierre Kraemer committed
338

339
template <typename T>
340
AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
341
{
342
	unsigned int index = getAttributeIndex(attribName) ;
343 344
	if(index == UNKNOWN)
		return NULL ;
345 346 347

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

351
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
352
{
353
	unsigned int index = getAttributeIndex(attribName) ;
354 355 356 357
	if(index == UNKNOWN)
		return NULL ;
	else
		return m_tableAttribs[index];
Pierre Kraemer's avatar
Pierre Kraemer committed
358 359
}

360 361
template <typename T>
inline T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
362
{
363 364 365 366 367 368 369 370
	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
371 372
}

373 374
template <typename T>
inline const T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex) const
Pierre Kraemer's avatar
Pierre Kraemer committed
375
{
376 377 378 379 380 381 382 383
	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
384 385
}

386 387
template <typename T>
inline void AttributeContainer::setData(unsigned int attrIndex, unsigned int eltIndex, const T& data)
Pierre Kraemer's avatar
Pierre Kraemer committed
388
{
389 390 391 392 393 394 395 396
	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
397 398
}

399 400 401 402 403
/**************************************
 *       ARITHMETIC OPERATIONS        *
 **************************************/

inline void AttributeContainer::toggleProcess(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
404
{
405 406 407
	assert(index < m_tableAttribs.size() || !"toggleProcess: attribute index out of bounds");
	assert(m_tableAttribs[index] != NULL || !"toggleProcess: attribute does not exist");
	m_tableAttribs[index]->toggleProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
408 409
}

410
inline void AttributeContainer::toggleNoProcess(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
411
{
412 413 414
	assert(index < m_tableAttribs.size() || !"toggleNoProcess: attribute index out of bounds");
	assert(m_tableAttribs[index] != NULL || !"toggleNoProcess: attribute does not exist");
	m_tableAttribs[index]->toggleNoProcess();
Pierre Kraemer's avatar
Pierre Kraemer committed
415 416
}

417
inline void AttributeContainer::affect(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
418
{
419 420 421 422 423
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
	{
		if (*it != NULL)
			(*it)->affect(i, j);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
424 425
}

426
inline void AttributeContainer::add(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
427
{
428 429 430 431 432
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
			(*it)->add(i, j);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
433 434
}

435
inline void AttributeContainer::sub(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
436
{
437 438 439 440 441
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
			(*it)->sub(i, j);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
442 443
}

444
inline void AttributeContainer::mult(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
445
{
446
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
447
	{
448 449 450
		if ((*it) != NULL)
			(*it)->mult(i, alpha);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
451 452
}

453
inline void AttributeContainer::div(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
454
{
455 456 457 458 459
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
			(*it)->div(i, alpha);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
460 461
}

462
inline void AttributeContainer::lerp(unsigned res, unsigned int i, unsigned int j, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
463
{
464 465 466 467 468
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
			(*it)->lerp(res, i, j, alpha);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
469 470 471
}

} // namespace CGoGN