attributeMultiVector.hpp 13 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24
#include "Geometry/vector_gen.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
25 26 27 28

namespace CGoGN
{

29
inline AttributeMultiVectorGen::AttributeMultiVectorGen(const std::string& strName, const std::string& strType):
Sylvain Thery's avatar
Sylvain Thery committed
30
	m_attrName(strName), m_typeName(strType)
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32
{}

Sylvain Thery's avatar
Sylvain Thery committed
33
inline AttributeMultiVectorGen::AttributeMultiVectorGen()
Pierre Kraemer's avatar
Pierre Kraemer committed
34 35
{}

36
inline AttributeMultiVectorGen::~AttributeMultiVectorGen()
Pierre Kraemer's avatar
Pierre Kraemer committed
37 38
{}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/**************************************
 *             ACCESSORS              *
 **************************************/

inline unsigned int AttributeMultiVectorGen::getOrbit() const
{
	return m_orbit;
}

inline void AttributeMultiVectorGen::setOrbit(unsigned int i)
{
	m_orbit = i ;
}

inline unsigned int AttributeMultiVectorGen::getIndex() const
{
	return m_index;
}

inline void AttributeMultiVectorGen::setIndex(unsigned int i)
{
	m_index = i ;
}

63
inline const std::string& AttributeMultiVectorGen::getName() const
64 65 66 67 68 69 70 71 72
{
	return m_attrName;
}

inline void AttributeMultiVectorGen::setName(const std::string& n)
{
	m_attrName = n ;
}

73
inline const std::string& AttributeMultiVectorGen::getTypeName() const
74 75 76 77 78 79 80 81 82
{
	return m_typeName;
}

inline void AttributeMultiVectorGen::setTypeName(const std::string& n)
{
	m_typeName = n ;
}

83
inline unsigned int AttributeMultiVectorGen::getBlockSize() const
84 85 86 87
{
	return _BLOCKSIZE_ ;
}

88 89 90 91
inline CGoGNCodeType AttributeMultiVectorGen::getTypeCode() const
{
	return m_typeCode;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
92

93 94
/***************************************************************************************************/
/***************************************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97


template <typename T>
98
AttributeMultiVector<T>::AttributeMultiVector(const std::string& strName, const std::string& strType):
99
	AttributeMultiVectorGen(strName, strType)
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103 104
{
	m_tableData.reserve(1024);
}

template <typename T>
105
AttributeMultiVector<T>::AttributeMultiVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109 110
{
	m_tableData.reserve(1024);
}

template <typename T>
111
AttributeMultiVector<T>::~AttributeMultiVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115 116 117
{
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
}

template <typename T>
118
inline AttributeMultiVectorGen* AttributeMultiVector<T>::new_obj()
Pierre Kraemer's avatar
Pierre Kraemer committed
119
{
120 121 122
	AttributeMultiVectorGen* ptr = new AttributeMultiVector<T>;
	ptr->setTypeName(m_typeName);
	return ptr;
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
/**************************************
 *       MULTI VECTOR MANAGEMENT      *
 **************************************/

template <typename T>
inline void AttributeMultiVector<T>::addBlock()
{
	T* ptr = new T[_BLOCKSIZE_];
	m_tableData.push_back(ptr);
	// init
//	T* endPtr = ptr + _BLOCKSIZE_;
//	while (ptr != endPtr)
//		*ptr++ = T(0);
}

template <typename T>
void AttributeMultiVector<T>::setNbBlocks(unsigned int nbb)
{
	if (nbb >= m_tableData.size())
	{
145
		for (size_t i= m_tableData.size(); i <nbb; ++i)
146 147 148 149
			addBlock();
	}
	else
	{
150
		for (size_t i = nbb; i < m_tableData.size(); ++i)
151 152 153 154 155
			delete[] m_tableData[i];
		m_tableData.resize(nbb);
	}
}

156 157 158
template <typename T>
unsigned int AttributeMultiVector<T>::getNbBlocks() const
{
159
	return uint32(m_tableData.size());
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
template <typename T>
void AttributeMultiVector<T>::addBlocksBefore(unsigned int nbb)
{
	std::vector<T*> tempo;
	tempo.reserve(1024);

	for (unsigned int i = 0; i < nbb; ++i)
		addBlock();

	for (typename std::vector<T*>::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		tempo.push_back(*it);

	m_tableData.swap(tempo);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
177
template <typename T>
178
bool AttributeMultiVector<T>::copy(const AttributeMultiVectorGen* atmvg)
Pierre Kraemer's avatar
Pierre Kraemer committed
179
{
180
	const AttributeMultiVector<T>* atmv = dynamic_cast<const AttributeMultiVector<T>*>(atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183

	if (atmv == NULL)
	{
184
		CGoGNerr << "trying to copy attributes of different type" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186 187 188
		return false;
	}
	if (atmv->m_typeName != m_typeName)
	{
189
		CGoGNerr << "trying to swap attributes with different type names" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193
		return false;
	}

	for (unsigned int i = 0; i < atmv->m_tableData.size(); ++i)
194
		std::memcpy( (void*) m_tableData[i], (void*) atmv->m_tableData[i], _BLOCKSIZE_ * sizeof(T));
195

Pierre Kraemer's avatar
Pierre Kraemer committed
196 197 198 199
	return true;
}

template <typename T>
200
bool AttributeMultiVector<T>::swap(AttributeMultiVectorGen* atmvg)
Pierre Kraemer's avatar
Pierre Kraemer committed
201
{
202
	AttributeMultiVector<T>* atmv = dynamic_cast<AttributeMultiVector<T>*>(atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205

	if (atmv == NULL)
	{
206
		CGoGNerr << "trying to swap attributes of different type" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
207 208
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
209
	if (atmv->m_typeName != m_typeName)
Pierre Kraemer's avatar
Pierre Kraemer committed
210
	{
211
		CGoGNerr << "trying to swap attributes with different type names" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
212 213 214
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
215
	m_tableData.swap(atmv->m_tableData) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217 218 219
	return true;
}

template <typename T>
220
bool AttributeMultiVector<T>::merge(const AttributeMultiVectorGen& att)
Pierre Kraemer's avatar
Pierre Kraemer committed
221
{
222
	const AttributeMultiVector<T>* attrib = dynamic_cast< const AttributeMultiVector<T>* >(&att);
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224
	if (attrib==NULL)
	{
225
		CGoGNerr << "trying to merge attributes of different type" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
226 227
		return false;
	}
228

Pierre Kraemer's avatar
Pierre Kraemer committed
229 230
	if (attrib->m_typeName != m_typeName)
	{
231
		CGoGNerr << "trying to merge attributes with different type names" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
232 233 234
		return false;
	}

235
	for (typename std::vector<T*>::const_iterator it = attrib->m_tableData.begin(); it != attrib->m_tableData.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237 238 239 240 241
		m_tableData.push_back(*it);

	return true;
}

template <typename T>
242
inline void AttributeMultiVector<T>::clear()
Pierre Kraemer's avatar
Pierre Kraemer committed
243
{
244 245 246
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
	m_tableData.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
247
}
Pierre Kraemer's avatar
Pierre Kraemer committed
248

249 250 251 252 253 254
template <typename T>
inline int AttributeMultiVector<T>::getSizeOfType() const
{
	return sizeof(T);
}

255 256 257
/**************************************
 *             DATA ACCESS            *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
258 259

template <typename T>
260
inline T& AttributeMultiVector<T>::operator[](unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
261
{
262
	return m_tableData[i / _BLOCKSIZE_][i % _BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
263 264 265
}

template <typename T>
266
inline const T& AttributeMultiVector<T>::operator[](unsigned int i) const
Pierre Kraemer's avatar
Pierre Kraemer committed
267
{
268
	return m_tableData[i / _BLOCKSIZE_][i % _BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
269 270 271
}

template <typename T>
272
unsigned int AttributeMultiVector<T>::getBlocksPointers(std::vector<void*>& addr, unsigned int& byteBlockSize) const
Pierre Kraemer's avatar
Pierre Kraemer committed
273
{
274 275 276 277 278
	byteBlockSize = _BLOCKSIZE_ * sizeof(T);

	addr.reserve(m_tableData.size());
	addr.clear();

279
	for (typename std::vector<T*>::const_iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
280 281
		addr.push_back(*it);

282
	return uint32(addr.size());
Pierre Kraemer's avatar
Pierre Kraemer committed
283 284
}

285 286 287 288
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
289
template <typename T>
Sylvain Thery's avatar
Sylvain Thery committed
290
inline void AttributeMultiVector<T>::initElt(unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
291
{
Sylvain Thery's avatar
Sylvain Thery committed
292
	m_tableData[id / _BLOCKSIZE_][id % _BLOCKSIZE_] = T(); // T(0);
Pierre Kraemer's avatar
Pierre Kraemer committed
293 294 295
}

template <typename T>
Sylvain Thery's avatar
Sylvain Thery committed
296
inline void AttributeMultiVector<T>::copyElt(unsigned int dst, unsigned int src)
Pierre Kraemer's avatar
Pierre Kraemer committed
297
{
298
	m_tableData[dst / _BLOCKSIZE_][dst % _BLOCKSIZE_] = m_tableData[src / _BLOCKSIZE_][src % _BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
299 300
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
301 302 303 304 305 306 307 308
template <typename T>
void AttributeMultiVector<T>::swapElt(unsigned int id1, unsigned int id2)
{
	T data = m_tableData[id1 / _BLOCKSIZE_][id1 % _BLOCKSIZE_] ;
	m_tableData[id1 / _BLOCKSIZE_][id1 % _BLOCKSIZE_] = m_tableData[id2 / _BLOCKSIZE_][id2 % _BLOCKSIZE_] ;
	m_tableData[id2 / _BLOCKSIZE_][id2 % _BLOCKSIZE_] = data ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
309
template <typename T>
310
void AttributeMultiVector<T>::overwrite(unsigned int src_b, unsigned int src_id, unsigned int dst_b, unsigned int dst_id)
Pierre Kraemer's avatar
Pierre Kraemer committed
311
{
312
	m_tableData[dst_b][dst_id] = m_tableData[src_b][src_id];
Pierre Kraemer's avatar
Pierre Kraemer committed
313 314
}

315 316 317 318 319

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

Pierre Kraemer's avatar
Pierre Kraemer committed
320
template <typename T>
321
void AttributeMultiVector<T>::saveBin(CGoGNostream& fs, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323 324
{
	unsigned int nbs[3];
	nbs[0] = id;
325 326
	int len1 = int(m_attrName.size()+1);
	int len2 = int(m_typeName.size()+1);
Pierre Kraemer's avatar
Pierre Kraemer committed
327 328 329 330 331
	nbs[1] = len1;
	nbs[2] = len2;
	fs.write(reinterpret_cast<const char*>(nbs),3*sizeof(unsigned int));
	// store names
	char buffer[256];
332
	const char* s1 = m_attrName.c_str();
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337
	memcpy(buffer,s1,len1);
	const char* s2 = m_typeName.c_str();
	memcpy(buffer+len1,s2,len2);
	fs.write(reinterpret_cast<const char*>(buffer),(len1+len2)*sizeof(char));

338
	nbs[0] = int(m_tableData.size());
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341 342
	nbs[1] = nbs[0] * _BLOCKSIZE_* sizeof(T);
	fs.write(reinterpret_cast<const char*>(nbs),2*sizeof(unsigned int));

	// store data blocks
343
	for(unsigned int i=0; i<nbs[0]; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346 347 348
	{
		fs.write(reinterpret_cast<const char*>(m_tableData[i]),_BLOCKSIZE_*sizeof(T));
	}
}

349
inline unsigned int AttributeMultiVectorGen::loadBinInfos(CGoGNistream& fs, std::string& name, std::string& type)
Pierre Kraemer's avatar
Pierre Kraemer committed
350 351 352 353 354 355 356 357 358 359 360 361
{
	unsigned int nbs[3];
	fs.read(reinterpret_cast<char*>(nbs), 3*sizeof(unsigned int));

	unsigned int id = nbs[0];
	unsigned int len1 = nbs[1];
	unsigned int len2 = nbs[2];

	char buffer[256];
	fs.read(buffer, (len1+len2)*sizeof(char));

	name = std::string(buffer);
362
	type = std::string(buffer + len1);
Pierre Kraemer's avatar
Pierre Kraemer committed
363 364 365 366 367

	return id;
}

template <typename T>
368
bool AttributeMultiVector<T>::loadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
369 370 371 372 373 374 375 376
{
	unsigned int nbs[2];
	fs.read(reinterpret_cast<char*>(nbs), 2*sizeof(unsigned int));

	unsigned int nb = nbs[0];

	// load data blocks
	m_tableData.resize(nb);
377
	for(unsigned int i = 0; i < nb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
378 379 380 381 382 383 384 385 386
	{
		T* ptr = new T[_BLOCKSIZE_];
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_*sizeof(T));
		m_tableData[i] = ptr;
	}

	return true;
}

387
inline bool AttributeMultiVectorGen::skipLoadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
388 389 390 391 392 393 394 395
{
	unsigned int nbs[2];
	fs.read(reinterpret_cast<char*>(nbs), 2*sizeof(unsigned int));

	// get number of byte to skip
	unsigned int nbb = nbs[1];

	// check if nbb ok
396
	if (nbb % _BLOCKSIZE_ != 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
397
	{
398
		CGoGNerr << "Error skipping wrong number of byte in attributes reading"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
399 400 401 402 403
		return false;
	}

	// skip data (no seek because of pb with gzstream)
	char* ptr = new char[_BLOCKSIZE_];
404
	while (nbb != 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407 408 409 410 411 412 413
	{
		nbb -= _BLOCKSIZE_;
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_);
	}
	delete[] ptr;

	return true;
}

414 415 416 417 418 419 420

template <typename T>
void AttributeMultiVector<T>::dump(unsigned int i) const
{
	CGoGNout << this->operator[](i);
}

421 422 423 424 425
template <typename T>
inline bool AttributeMultiVector<T>::isMarkerBool()
{
	return false;
}
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

template <typename T>
inline void AttributeMultiVector<T>::setTypeCode()
{
	m_typeCode = CGoGNUNKNOWNTYPE;
}

template <>
inline void AttributeMultiVector<int>::setTypeCode()
{
	m_typeCode = CGoGNINT;
}
template <>
inline void AttributeMultiVector<unsigned int>::setTypeCode()
{
	m_typeCode = CGoGNUINT;
}
template <>
inline void AttributeMultiVector<short>::setTypeCode()
{
	m_typeCode = CGoGNSHORT;
}
template <>
inline void AttributeMultiVector<unsigned short>::setTypeCode()
{
	m_typeCode = CGoGNUSHORT;
}
template <>
inline void AttributeMultiVector<char>::setTypeCode()
{
	m_typeCode = CGoGNCHAR;
}
template <>
inline void AttributeMultiVector<unsigned char>::setTypeCode()
{
	m_typeCode = CGoGNUCHAR;
}

template <>
inline void AttributeMultiVector<float>::setTypeCode()
{
	m_typeCode = CGoGNFLOAT;
}
template <>
inline void AttributeMultiVector<double>::setTypeCode()
{
	m_typeCode = CGoGNDOUBLE;
}

template <>
inline void AttributeMultiVector<Geom::Vec2f>::setTypeCode()
{
	m_typeCode = CGoGNVEC2F;
}

template <>
inline void AttributeMultiVector<Geom::Vec2d>::setTypeCode()
{
	m_typeCode = CGoGNVEC2D;
}

template <>
inline void AttributeMultiVector<Geom::Vec3f>::setTypeCode()
{
	m_typeCode = CGoGNVEC3F;
}

template <>
inline void AttributeMultiVector<Geom::Vec3d>::setTypeCode()
{
	m_typeCode = CGoGNVEC3D;
}

template <>
inline void AttributeMultiVector<Geom::Vec4f>::setTypeCode()
{
	m_typeCode = CGoGNVEC4F;
}

template <>
inline void AttributeMultiVector<Geom::Vec4d>::setTypeCode()
{
	m_typeCode = CGoGNVEC4D;
}


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