attributeMultiVector.hpp 9.54 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4

namespace CGoGN
{

5
inline AttributeMultiVectorGen::AttributeMultiVectorGen(const std::string& strName, const std::string& strType):
Pierre Kraemer's avatar
Pierre Kraemer committed
6 7 8
		m_attName(strName), m_typeName(strType), m_toProcess(true)
{}

9
inline AttributeMultiVectorGen::AttributeMultiVectorGen(): m_toProcess(true)
Pierre Kraemer's avatar
Pierre Kraemer committed
10 11
{}

12
inline AttributeMultiVectorGen::~AttributeMultiVectorGen()
Pierre Kraemer's avatar
Pierre Kraemer committed
13 14
{}

15
inline void AttributeMultiVectorGen::toggleProcess()
Pierre Kraemer's avatar
Pierre Kraemer committed
16 17 18 19
{
	m_toProcess = true;
}

20
inline void AttributeMultiVectorGen::toggleNoProcess()
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
{
	m_toProcess = false;
}

25
inline bool AttributeMultiVectorGen::toProcess()
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28 29
{
	return m_toProcess;
}

30
inline unsigned int AttributeMultiVectorGen::BlockSize()
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39 40
{
	return _BLOCKSIZE_;
}






template <typename T>
41 42
AttributeMultiVector<T>::AttributeMultiVector(const std::string& strName, const std::string& strType):
AttributeMultiVectorGen(strName,strType)
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47
{
	m_tableData.reserve(1024);
}

template <typename T>
48
AttributeMultiVector<T>::AttributeMultiVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51 52 53
{
	m_tableData.reserve(1024);
}

template <typename T>
54
AttributeMultiVector<T>::~AttributeMultiVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58 59 60
{
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
}

template <typename T>
61
AttributeMultiVectorGen* AttributeMultiVector<T>::new_obj()
Pierre Kraemer's avatar
Pierre Kraemer committed
62
{
63
	return new AttributeMultiVector<T>;
Pierre Kraemer's avatar
Pierre Kraemer committed
64 65 66
}

template <typename T>
67
bool AttributeMultiVector<T>::copy(const AttributeMultiVectorGen* atmvg)
Pierre Kraemer's avatar
Pierre Kraemer committed
68
{
69
	const AttributeMultiVector<T>* atmv = dynamic_cast<const AttributeMultiVector<T>*>(atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	if (atmv == NULL)
	{
		std::cerr<< "copy attribute of different type"<<std::endl;
		return false;
	}

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

template <typename T>
85
bool AttributeMultiVector<T>::swap(AttributeMultiVectorGen* atmvg)
Pierre Kraemer's avatar
Pierre Kraemer committed
86
{
87
	AttributeMultiVector<T>* atmv = dynamic_cast<AttributeMultiVector<T>*>(atmvg);
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91 92 93 94 95 96 97 98 99

	if (atmv == NULL)
	{
		std::cerr<< "swap attribute of different type"<<std::endl;
		return false;
	}

	m_tableData.swap(atmv->m_tableData) ;
	return true;
}

template <typename T>
100
bool AttributeMultiVector<T>::swap(AttributeMultiVectorGen& att)
Pierre Kraemer's avatar
Pierre Kraemer committed
101
{
102
	AttributeMultiVector<T>* attrib = dynamic_cast< AttributeMultiVector<T>* >(&att);
Pierre Kraemer's avatar
Pierre Kraemer committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	if (attrib==NULL)
	{
		std::cerr << "Erreur, swap de container de types differents"<<std::endl;
		return false;
	}
	if (attrib->m_typeName != m_typeName)
	{
		std::cerr << "Erreur, swap de container avec noms de type differents"<<std::endl;
		return false;
	}

	m_tableData.swap(attrib->m_tableData);
	m_typeName.swap(attrib->m_typeName);

	return true;
}

template <typename T>
121
bool AttributeMultiVector<T>::merge(const AttributeMultiVectorGen& att)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
{
123
	const AttributeMultiVector<T>* attrib = dynamic_cast< const AttributeMultiVector<T>* >(&att);
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	if (attrib==NULL)
	{
		std::cerr << "Erreur, merge de container de types differents"<<std::endl;
		return false;
	}
	if (attrib->m_typeName != m_typeName)
	{
		std::cerr << "Erreur, merge de container avec noms de type differents"<<std::endl;
		return false;
	}

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

	return true;
}

template <typename T>
142
T& AttributeMultiVector<T>::operator[](unsigned int i) { return m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];}
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144

template <typename T>
145
const T& AttributeMultiVector<T>::operator[](unsigned int i) const { return m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];}
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147

template <typename T>
148
std::string AttributeMultiVector<T>::output(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
149 150 151 152 153 154 155
{
	std::stringstream ss;
	ss << m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];
	return ss.str();
}

template <typename T>
156
void AttributeMultiVector<T>::input(unsigned int i,const std::string& st)
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160 161 162
{
	std::stringstream ss(st);
	ss >> m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];
}

template <typename T>
163
void AttributeMultiVector<T>::addBlock()
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166 167 168 169 170 171 172 173
{
	T* ptr = new T[_BLOCKSIZE_];
	m_tableData.push_back( ptr );
	// init
//	T* endPtr = ptr + _BLOCKSIZE_;
//	while (ptr != endPtr)
//		*ptr++ = T(0);
}

template <typename T>
174
void AttributeMultiVector<T>::initElt(unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
175
{
176
	m_tableData[id/_BLOCKSIZE_][id%_BLOCKSIZE_] = T(0);
Pierre Kraemer's avatar
Pierre Kraemer committed
177 178 179
}

template <typename T>
180
void AttributeMultiVector<T>::copyElt(unsigned int dst, unsigned int src)
Pierre Kraemer's avatar
Pierre Kraemer committed
181
{
182
	m_tableData[dst/_BLOCKSIZE_][dst%_BLOCKSIZE_] = m_tableData[src/_BLOCKSIZE_][src%_BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
183 184 185
}

template <typename T>
186
void AttributeMultiVector<T>::setNbBlocks(unsigned int nbb)
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
{
	if (nbb >=m_tableData.size())
	{
		for (unsigned int i= m_tableData.size(); i <nbb; ++i)
		{
//			T* ptr = new T[_BLOCKSIZE_];
//			m_tableData.push_back( ptr );
			addBlock();
		}
	}
	else
	{
		for (unsigned int i= nbb; i <m_tableData.size(); ++i)
		{
			delete[] m_tableData[i];
		}
		m_tableData.resize(nbb);
	}
}

template <typename T>
208
void AttributeMultiVector<T>::addBlocksBefore(unsigned int nbb)
Pierre Kraemer's avatar
Pierre Kraemer committed
209 210 211 212
{
	std::vector< T* > tempo;
	tempo.reserve(1024);

213
	for (unsigned int i= 0; i <nbb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221 222 223 224 225 226
	{
		T* ptr = new T[_BLOCKSIZE_];
		tempo.push_back( ptr );
	}

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

template <typename T>
227
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
228 229 230 231 232
{
	m_tableData[dst_b][dst_id] = m_tableData[src_b][src_id];
}

template <typename T>
233
void AttributeMultiVector<T>::free()
Pierre Kraemer's avatar
Pierre Kraemer committed
234 235 236 237 238 239 240
{
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
	m_tableData.clear();
}

template <typename T>
241
unsigned int AttributeMultiVector<T>::getStartAddresses(std::vector<void*>& addr, unsigned int& byteTableSize)
Pierre Kraemer's avatar
Pierre Kraemer committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255
{
	byteTableSize = _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();
}

template <typename T>
256
void AttributeMultiVector<T>::saveBin(CGoGNostream& fs, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
{
	unsigned int nbs[3];
	nbs[0] = id;
	int len1 = m_attName.size()+1;
	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];
	const char* s1 = m_attName.c_str();
	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
279
	for(unsigned int i=0; i<nbs[0]; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
280 281 282 283 284
	{
		fs.write(reinterpret_cast<const char*>(m_tableData[i]),_BLOCKSIZE_*sizeof(T));
	}
}

285
inline unsigned int AttributeMultiVectorGen::loadBinInfos(CGoGNistream& fs, std::string& name, std::string& type)
Pierre Kraemer's avatar
Pierre Kraemer committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
{
	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);
	type = std::string(buffer+len1);

	return id;
}

template <typename T>
304
bool AttributeMultiVector<T>::loadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
305 306 307 308 309 310 311 312
{
	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);
313
	for(unsigned int i=0; i<nb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
314 315 316 317 318 319 320 321 322
	{
		T* ptr = new T[_BLOCKSIZE_];
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_*sizeof(T));
		m_tableData[i] = ptr;
	}

	return true;
}

323
inline bool AttributeMultiVectorGen::skipLoadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
{
	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
	if (nbb%_BLOCKSIZE_ != 0)
	{
		std::cerr << "Error skipping wrong number of byte in attributes reading"<< std::endl;
		return false;
	}

	// skip data (no seek because of pb with gzstream)
	char* ptr = new char[_BLOCKSIZE_];
	while (nbb!=0)
	{
		nbb -= _BLOCKSIZE_;
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_);
	}
	delete[] ptr;

	return true;
}

template <typename T>
351
void AttributeMultiVector<T>::affect(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
352 353 354 355 356 357
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] = m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}

template <typename T>
358
void AttributeMultiVector<T>::add(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
359 360 361 362 363 364
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] += m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}

template <typename T>
365
void AttributeMultiVector<T>::sub(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
366 367 368 369 370 371
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] -= m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}

template <typename T>
372
void AttributeMultiVector<T>::mult(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
373 374 375 376 377 378
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] *= alpha;
}

template <typename T>
379
void AttributeMultiVector<T>::div(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
380 381 382 383 384 385
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] /= alpha;
}

template <typename T>
386
void AttributeMultiVector<T>::lerp(unsigned res, unsigned int i, unsigned int j, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
{
	if (m_toProcess)
	{
		T v1 = m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];
		T v2 = m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];

		v1 *= alpha;
		v2 *= 1.0-alpha;
		v1+=v2;
		m_tableData[res/_BLOCKSIZE_][res%_BLOCKSIZE_] = v1;

		std::cout << "LERP "<< m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_]<< " & " << m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
		std::cout << " = "<< m_tableData[res/_BLOCKSIZE_][res%_BLOCKSIZE_]<< std::endl;
	}
}

} // namespace CGoGN