attribmv.hpp 9.26 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 24 25 26 27 28 29 30 31 32 33 34 35 36 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

namespace CGoGN
{

inline AttribMultiVectGen::AttribMultiVectGen(const std::string& strName, const std::string& strType):
		m_attName(strName), m_typeName(strType), m_toProcess(true)
{}

inline AttribMultiVectGen::AttribMultiVectGen(): m_toProcess(true)
{}

inline AttribMultiVectGen::~AttribMultiVectGen()
{}

inline void AttribMultiVectGen::toggleProcess()
{
	m_toProcess = true;
}

inline void AttribMultiVectGen::toggleNoProcess()
{
	m_toProcess = false;
}

inline bool AttribMultiVectGen::toProcess()
{
	return m_toProcess;
}

inline unsigned int AttribMultiVectGen::BlockSize()
{
	return _BLOCKSIZE_;
}






template <typename T>
AttribMultiVect<T>::AttribMultiVect(const std::string& strName, const std::string& strType):
AttribMultiVectGen(strName,strType)
{
	m_tableData.reserve(1024);
}

template <typename T>
AttribMultiVect<T>::AttribMultiVect()
{
	m_tableData.reserve(1024);
}

template <typename T>
AttribMultiVect<T>::~AttribMultiVect()
{
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
}

template <typename T>
AttribMultiVectGen* AttribMultiVect<T>::new_obj()
{
	return new AttribMultiVect<T>;
}

66

Pierre Kraemer's avatar
Pierre Kraemer committed
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
template <typename T>
bool AttribMultiVect<T>::copy(const AttribMultiVectGen* atmvg)
{
	const AttribMultiVect<T>* atmv = dynamic_cast<const AttribMultiVect<T>*>(atmvg);

	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>
bool AttribMultiVect<T>::swap(AttribMultiVectGen* atmvg)
{
	AttribMultiVect<T>* atmv = dynamic_cast<AttribMultiVect<T>*>(atmvg);

	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>
bool AttribMultiVect<T>::swap(AttribMultiVectGen& att)
{
	AttribMultiVect<T>* attrib = dynamic_cast< AttribMultiVect<T>* >(&att);
	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>
bool AttribMultiVect<T>::merge(const AttribMultiVectGen& att)
{
	const AttribMultiVect<T>* attrib = dynamic_cast< const AttribMultiVect<T>* >(&att);
	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>
T& AttribMultiVect<T>::operator[](unsigned int i) { return m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];}

template <typename T>
const T& AttribMultiVect<T>::operator[](unsigned int i) const { return m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];}

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

template <typename T>
void AttribMultiVect<T>::input(unsigned int i,const std::string& st)
{
	std::stringstream ss(st);
	ss >> m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_];
}

template <typename T>
void AttribMultiVect<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 AttribMultiVect<T>::initElt(unsigned int id)
{
	m_tableData[id/_BLOCKSIZE_][id%_BLOCKSIZE_] =  T(0);
}

template <typename T>
void AttribMultiVect<T>::copyElt(unsigned int dst, unsigned int src)
{
	m_tableData[dst/_BLOCKSIZE_][dst%_BLOCKSIZE_] =  m_tableData[src/_BLOCKSIZE_][src%_BLOCKSIZE_];
}

template <typename T>
void AttribMultiVect<T>::setNbBlocks(unsigned int nbb)
{
	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>
void AttribMultiVect<T>::addBlocksBefore(unsigned int nbb)
{
	std::vector< T* > tempo;
	tempo.reserve(1024);

215
	for (unsigned int i= 0; i <nbb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	{
		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>
void AttribMultiVect<T>::overwrite(unsigned int src_b, unsigned int src_id, unsigned int dst_b, unsigned int dst_id)
{
	m_tableData[dst_b][dst_id] = m_tableData[src_b][src_id];
}

template <typename T>
void AttribMultiVect<T>::free()
{
	for (typename std::vector< T* >::iterator it = m_tableData.begin(); it != m_tableData.end(); ++it)
		delete[] (*it);
	m_tableData.clear();
}

template <typename T>
unsigned int AttribMultiVect<T>::getStartAddresses(std::vector<void*>& addr, unsigned int& byteTableSize)
{
	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>
void AttribMultiVect<T>::saveBin(CGoGNostream& fs, unsigned int id)
{
	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
281
	for(unsigned int i=0; i<nbs[0]; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	{
		fs.write(reinterpret_cast<const char*>(m_tableData[i]),_BLOCKSIZE_*sizeof(T));
	}
}

inline unsigned int AttribMultiVectGen::loadBinInfos(CGoGNistream& fs, std::string& name, std::string& type)
{
	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>
bool AttribMultiVect<T>::loadBin(CGoGNistream& fs)
{
	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);
315
	for(unsigned int i=0; i<nb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317 318 319 320 321 322 323 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	{
		T* ptr = new T[_BLOCKSIZE_];
		fs.read(reinterpret_cast<char*>(ptr),_BLOCKSIZE_*sizeof(T));
		m_tableData[i] = ptr;
	}

	return true;
}

inline bool AttribMultiVectGen::skipLoadBin(CGoGNistream& fs)
{
	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>
void AttribMultiVect<T>::affect(unsigned int i, unsigned int j)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] = m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}

template <typename T>
void AttribMultiVect<T>::add(unsigned int i, unsigned int j)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] += m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}

template <typename T>
void AttribMultiVect<T>::sub(unsigned int i, unsigned int j)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] -= m_tableData[j/_BLOCKSIZE_][j%_BLOCKSIZE_];
}

template <typename T>
void AttribMultiVect<T>::mult(unsigned int i, double alpha)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] *= alpha;
}

template <typename T>
void AttribMultiVect<T>::div(unsigned int i, double alpha)
{
	if (m_toProcess)
		m_tableData[i/_BLOCKSIZE_][i%_BLOCKSIZE_] /= alpha;
}

template <typename T>
void AttribMultiVect<T>::lerp(unsigned res, unsigned int i, unsigned int j, double alpha)
{
	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