attributeMultiVector.hpp 12.8 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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
24 25 26 27

namespace CGoGN
{

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

32 33
inline AttributeMultiVectorGen::AttributeMultiVectorGen():
	m_toProcess(true)
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**************************************
 *             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 ;
}

inline const std::string& AttributeMultiVectorGen::getName()
{
	return m_attrName;
}

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

inline const std::string& AttributeMultiVectorGen::getTypeName()
{
	return m_typeName;
}

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

inline unsigned int AttributeMultiVectorGen::getBlockSize()
{
	return _BLOCKSIZE_ ;
}

/**************************************
 *       ARITHMETIC OPERATIONS        *
 **************************************/

92
inline void AttributeMultiVectorGen::toggleProcess()
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96
{
	m_toProcess = true;
}

97
inline void AttributeMultiVectorGen::toggleNoProcess()
Pierre Kraemer's avatar
Pierre Kraemer committed
98 99 100 101
{
	m_toProcess = false;
}

102
inline bool AttributeMultiVectorGen::toProcess()
Pierre Kraemer's avatar
Pierre Kraemer committed
103 104 105 106 107
{
	return m_toProcess;
}


108 109
/***************************************************************************************************/
/***************************************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112


template <typename T>
113
AttributeMultiVector<T>::AttributeMultiVector(const std::string& strName, const std::string& strType):
114
	AttributeMultiVectorGen(strName, strType)
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118 119
{
	m_tableData.reserve(1024);
}

template <typename T>
120
AttributeMultiVector<T>::AttributeMultiVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
121 122 123 124 125
{
	m_tableData.reserve(1024);
}

template <typename T>
126
AttributeMultiVector<T>::~AttributeMultiVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128 129 130 131 132
{
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
}

template <typename T>
133
inline AttributeMultiVectorGen* AttributeMultiVector<T>::new_obj()
Pierre Kraemer's avatar
Pierre Kraemer committed
134
{
135
	return new AttributeMultiVector<T>;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**************************************
 *       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())
	{
		for (unsigned int i= m_tableData.size(); i <nbb; ++i)
			addBlock();
	}
	else
	{
		for (unsigned int i = nbb; i < m_tableData.size(); ++i)
			delete[] m_tableData[i];
		m_tableData.resize(nbb);
	}
}

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
184
template <typename T>
185
bool AttributeMultiVector<T>::copy(const AttributeMultiVectorGen* atmvg)
Pierre Kraemer's avatar
Pierre Kraemer committed
186
{
187
	const AttributeMultiVector<T>* atmv = dynamic_cast<const AttributeMultiVector<T>*>(atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189 190

	if (atmv == NULL)
	{
191
		CGoGNerr << "trying to copy attributes of different type" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
192 193 194 195
		return false;
	}
	if (atmv->m_typeName != m_typeName)
	{
196
		CGoGNerr << "trying to swap attributes with different type names" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
197 198 199 200
		return false;
	}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205 206
	return true;
}

template <typename T>
207
bool AttributeMultiVector<T>::swap(AttributeMultiVectorGen* atmvg)
Pierre Kraemer's avatar
Pierre Kraemer committed
208
{
209
	AttributeMultiVector<T>* atmv = dynamic_cast<AttributeMultiVector<T>*>(atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
210 211 212

	if (atmv == NULL)
	{
213
		CGoGNerr << "trying to swap attributes of different type" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
216
	if (atmv->m_typeName != m_typeName)
Pierre Kraemer's avatar
Pierre Kraemer committed
217
	{
218
		CGoGNerr << "trying to swap attributes with different type names" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221
		return false;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
222
	m_tableData.swap(atmv->m_tableData) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226
	return true;
}

template <typename T>
227
bool AttributeMultiVector<T>::merge(const AttributeMultiVectorGen& att)
Pierre Kraemer's avatar
Pierre Kraemer committed
228
{
229
	const AttributeMultiVector<T>* attrib = dynamic_cast< const AttributeMultiVector<T>* >(&att);
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231
	if (attrib==NULL)
	{
232
		CGoGNerr << "trying to merge attributes of different type" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234
		return false;
	}
235

Pierre Kraemer's avatar
Pierre Kraemer committed
236 237
	if (attrib->m_typeName != m_typeName)
	{
238
		CGoGNerr << "trying to merge attributes with different type names" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
239 240 241
		return false;
	}

242
	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
243 244 245 246 247 248
		m_tableData.push_back(*it);

	return true;
}

template <typename T>
249
inline void AttributeMultiVector<T>::clear()
Pierre Kraemer's avatar
Pierre Kraemer committed
250
{
251 252 253
	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
254
}
Pierre Kraemer's avatar
Pierre Kraemer committed
255

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

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

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

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

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

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

	return addr.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
284 285
}

286 287 288 289
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/

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

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

template <typename T>
303
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
304
{
305
	m_tableData[dst_b][dst_id] = m_tableData[src_b][src_id];
Pierre Kraemer's avatar
Pierre Kraemer committed
306 307
}

308 309 310 311
/**************************************
 *       ARITHMETIC OPERATIONS        *
 **************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
312
template <typename T>
313
void AttributeMultiVector<T>::affect(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
314
{
315 316 317
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] = m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}
Pierre Kraemer's avatar
Pierre Kraemer committed
318

319 320 321 322 323 324
template <typename T>
void AttributeMultiVector<T>::add(unsigned int i, unsigned int j)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] += m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}
Pierre Kraemer's avatar
Pierre Kraemer committed
325

326 327 328 329 330
template <typename T>
void AttributeMultiVector<T>::sub(unsigned int i, unsigned int j)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] -= m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
331 332 333
}

template <typename T>
334
void AttributeMultiVector<T>::mult(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
335
{
336 337
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] *= alpha;
Pierre Kraemer's avatar
Pierre Kraemer committed
338 339 340
}

template <typename T>
341
void AttributeMultiVector<T>::div(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
342
{
343 344
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] /= alpha;
Pierre Kraemer's avatar
Pierre Kraemer committed
345 346 347
}

template <typename T>
348
void AttributeMultiVector<T>::lerp(unsigned res, unsigned int i, unsigned int j, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
349
{
350 351 352 353
	if (m_toProcess)
	{
		T v1 = m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];
		T v2 = m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
354

355 356 357 358
		v1 *= alpha;
		v2 *= 1.0 - alpha;
		v1 += v2;
		m_tableData[res/_BLOCKSIZE_][res%_BLOCKSIZE_] = v1;
Pierre Kraemer's avatar
Pierre Kraemer committed
359

360 361
		CGoGNout << "LERP " << m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] << " & " << m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
		CGoGNout << " = " << m_tableData[res/_BLOCKSIZE_][res%_BLOCKSIZE_] << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
362
	}
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
}

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

template <typename T>
std::string AttributeMultiVector<T>::output(unsigned int i)
{
	std::stringstream ss;
	ss << m_tableData[i / _BLOCKSIZE_][i % _BLOCKSIZE_];
	return ss.str();
}

template <typename T>
void AttributeMultiVector<T>::input(unsigned int i,const std::string& st)
{
	std::stringstream ss(st);
	ss >> m_tableData[i / _BLOCKSIZE_][i % _BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
382 383 384
}

template <typename T>
385
void AttributeMultiVector<T>::saveBin(CGoGNostream& fs, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
386 387 388
{
	unsigned int nbs[3];
	nbs[0] = id;
389
	int len1 = m_attrName.size()+1;
Pierre Kraemer's avatar
Pierre Kraemer committed
390 391 392 393 394 395
	int len2 = m_typeName.size()+1;
	nbs[1] = len1;
	nbs[2] = len2;
	fs.write(reinterpret_cast<const char*>(nbs),3*sizeof(unsigned int));
	// store names
	char buffer[256];
396
	const char* s1 = m_attrName.c_str();
Pierre Kraemer's avatar
Pierre Kraemer committed
397 398 399 400 401 402 403 404 405 406 407
	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));


	nbs[0] = m_tableData.size();
	nbs[1] = nbs[0] * _BLOCKSIZE_* sizeof(T);
	fs.write(reinterpret_cast<const char*>(nbs),2*sizeof(unsigned int));

	// store data blocks
408
	for(unsigned int i=0; i<nbs[0]; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
409 410 411 412 413
	{
		fs.write(reinterpret_cast<const char*>(m_tableData[i]),_BLOCKSIZE_*sizeof(T));
	}
}

414
inline unsigned int AttributeMultiVectorGen::loadBinInfos(CGoGNistream& fs, std::string& name, std::string& type)
Pierre Kraemer's avatar
Pierre Kraemer committed
415 416 417 418 419 420 421 422 423 424 425 426
{
	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);
427
	type = std::string(buffer + len1);
Pierre Kraemer's avatar
Pierre Kraemer committed
428 429 430 431 432

	return id;
}

template <typename T>
433
bool AttributeMultiVector<T>::loadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
434 435 436 437 438 439 440 441
{
	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);
442
	for(unsigned int i = 0; i < nb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
443 444 445 446 447 448 449 450 451
	{
		T* ptr = new T[_BLOCKSIZE_];
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_*sizeof(T));
		m_tableData[i] = ptr;
	}

	return true;
}

452
inline bool AttributeMultiVectorGen::skipLoadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
453 454 455 456 457 458 459 460
{
	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
461
	if (nbb % _BLOCKSIZE_ != 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
462
	{
463
		CGoGNerr << "Error skipping wrong number of byte in attributes reading"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
464 465 466 467 468
		return false;
	}

	// skip data (no seek because of pb with gzstream)
	char* ptr = new char[_BLOCKSIZE_];
469
	while (nbb != 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
470 471 472 473 474 475 476 477 478 479
	{
		nbb -= _BLOCKSIZE_;
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_);
	}
	delete[] ptr;

	return true;
}

} // namespace CGoGN