attributeContainer.cpp 19.3 KB
Newer Older
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           *
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/                                         *
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
24 25 26 27 28 29 30 31

#include <typeinfo>
#include <stdio.h>
#include <string.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <iostream>

32
#include "Container/attributeContainer.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35 36

namespace CGoGN
{

37 38 39 40 41 42 43 44
AttributeContainer::AttributeContainer() :
	m_orbit(0),
	m_nbAttributes(0),
	m_nbUnknown(0),
	m_size(0),
	m_maxSize(0),
	m_lineCost(0),
	m_attributes_registry_map(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48
{
	m_holesBlocks.reserve(512);
}

49
AttributeContainer::~AttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
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
/**************************************
 *       INFO ABOUT ATTRIBUTES        *
 **************************************/

unsigned int AttributeContainer::getAttributeIndex(const std::string& attribName)
{
	unsigned int index ;
	bool found = false ;
	for (index = 0; index < m_tableAttribs.size() && !found; ++index)
	{
		if(m_tableAttribs[index] != NULL && m_tableAttribs[index]->getName() == attribName)
			found = true ;
	}

	if (!found)
		return UNKNOWN;
	else
		return index - 1 ;
}

const std::string& AttributeContainer::getAttributeName(unsigned int index)
{
	assert(index < m_tableAttribs.size() || !"getAttributeName: attribute index out of bounds");
	assert(m_tableAttribs[index] != NULL || !"getAttributeName: attribute does not exist");

78
	return m_tableAttribs[index]->getName() ;
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
}

template <typename T>
unsigned int AttributeContainer::getAttributeBlocksPointers(unsigned int attrIndex, std::vector<T*>& vect_ptr, unsigned int& byteBlockSize)
{
	assert(attrIndex < m_tableAttribs.size() || !"getAttributeBlocksPointers: attribute index out of bounds");
	assert(m_tableAttribs[attrIndex] != NULL || !"getAttributeBlocksPointers: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getAttributeBlocksPointers: wrong type");
	return atm->getBlocksPointers(vect_ptr, byteBlockSize);
}

unsigned int AttributeContainer::getAttributesNames(std::vector<std::string>& names)
{
	names.clear() ;
	names.reserve(m_nbAttributes) ;

	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if(m_tableAttribs[i] != NULL)
			names.push_back(m_tableAttribs[i]->getName()) ;
	}

	return m_nbAttributes ;
}

/**************************************
 *        CONTAINER MANAGEMENT        *
 **************************************/

110
void AttributeContainer::swap(AttributeContainer& cont)
Pierre Kraemer's avatar
Pierre Kraemer committed
111
{
112 113
	// swap everything but the orbit

Pierre Kraemer's avatar
Pierre Kraemer committed
114
	m_tableAttribs.swap(cont.m_tableAttribs);
Pierre Kraemer's avatar
Pierre Kraemer committed
115
	m_freeIndices.swap(cont.m_freeIndices);
116 117 118
	m_holesBlocks.swap(cont.m_holesBlocks);
	m_tableBlocksWithFree.swap(cont.m_tableBlocksWithFree);
	m_tableBlocksEmpty.swap(cont.m_tableBlocksEmpty);
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120 121 122 123

	unsigned int temp = m_nbAttributes;
	m_nbAttributes = cont.m_nbAttributes;
	cont.m_nbAttributes = temp;

124 125 126 127
	temp = m_nbUnknown ;
	m_nbUnknown = cont.m_nbUnknown ;
	cont.m_nbUnknown = temp ;

Pierre Kraemer's avatar
Pierre Kraemer committed
128 129 130 131 132 133 134 135 136 137 138 139 140
	temp = m_size;
	m_size = cont.m_size;
	cont.m_size = temp;

	temp = m_maxSize;
	m_maxSize = cont.m_maxSize;
	cont.m_maxSize = temp;

	temp = m_lineCost;
	m_lineCost = cont.m_lineCost;
	cont.m_lineCost = temp;
}

141
void AttributeContainer::clear(bool removeAttrib)
Pierre Kraemer's avatar
Pierre Kraemer committed
142
{
143 144
	m_size = 0;
 	m_maxSize = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
145

146 147
	std::vector<HoleBlockRef*> bf;
	std::vector<unsigned int> bwf;
Pierre Kraemer's avatar
Pierre Kraemer committed
148

149 150 151
	// raz des cases libres
	for (std::vector<HoleBlockRef*>::iterator it = m_holesBlocks.begin(); it != m_holesBlocks.end(); ++it)
		delete (*it);
Pierre Kraemer's avatar
Pierre Kraemer committed
152

153 154
	m_holesBlocks.swap(bf);
	m_tableBlocksWithFree.swap(bwf);
Pierre Kraemer's avatar
Pierre Kraemer committed
155

156 157 158 159 160 161
	// detruit les données
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
			(*it)->clear();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	// on enleve les attributs ?
	if (removeAttrib)
	{
		// nb a zero
		m_nbAttributes = 0;

		// detruit tous les attributs
		std::vector<AttributeMultiVectorGen*> amg;
		for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
		{
			if ((*it) != NULL)
				delete (*it);
		}
		m_tableAttribs.swap(amg);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
178 179
}

180
void AttributeContainer::compact(std::vector<unsigned int>& mapOldNew)
Pierre Kraemer's avatar
Pierre Kraemer committed
181
{
182
	unsigned int nbe = _BLOCKSIZE_ * m_holesBlocks.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
183

184 185 186 187 188 189 190
	unsigned int nbb = m_holesBlocks.size() - 1;
	while ((m_holesBlocks[nbb])->empty())
	{
		--nbb;
		nbe -= _BLOCKSIZE_;
	}
	++nbb;
Pierre Kraemer's avatar
Pierre Kraemer committed
191

192
	mapOldNew.reserve(nbe);
Pierre Kraemer's avatar
Pierre Kraemer committed
193

194 195 196 197 198
	// now get the holes
	unsigned int baseAdr = 0;
	for (unsigned int i = 0; i < nbb; ++i)
	{
		HoleBlockRef* block = m_holesBlocks[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213
		for (unsigned int j = 0; j < _BLOCKSIZE_; ++j)
		{
			if (j < block->sizeTable())
			{
				if (block->used(j))
					mapOldNew.push_back(baseAdr);
				else
					mapOldNew.push_back(0xffffffff);
			}
			else
				mapOldNew.push_back(0xffffffff);
			baseAdr++;
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
214

215
	unsigned int last = mapOldNew.size() - 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
216

217 218 219 220 221 222 223 224
 	for (unsigned int i = 0 ; i < last; ++i)
	{
		unsigned int val = mapOldNew[i];
		if (val == 0xffffffff)
		{
			// first find last element
			while (mapOldNew[last] == 0xffffffff)
				--last;
Pierre Kraemer's avatar
Pierre Kraemer committed
225

226 227 228 229 230 231
			// store it in the hole
			// find the blocks and indices
			unsigned int bi = i / _BLOCKSIZE_;
			unsigned int ib = i % _BLOCKSIZE_;
			unsigned int bj = last / _BLOCKSIZE_;
			unsigned int jb = last % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
232

233 234 235 236 237 238
			//overwrite attributes
			for(unsigned int j = 0; j < m_tableAttribs.size(); ++j)
			{
				if (m_tableAttribs[j] != NULL)
					m_tableAttribs[j]->overwrite(bj, jb, bi, ib);
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
239

240 241
			// overwrite emptyLine with last line in free blocks
			m_holesBlocks[bi]->overwrite(ib, m_holesBlocks[bj], jb);
Pierre Kraemer's avatar
Pierre Kraemer committed
242

243 244 245
			// set the map value
			mapOldNew[last] = i;
			--last;
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248
		}
	}

249
	for (int i = m_holesBlocks.size() - 1; i >= 0; --i)
Pierre Kraemer's avatar
Pierre Kraemer committed
250
	{
251 252
		HoleBlockRef* ptr = m_holesBlocks[i];
		if (ptr->compressFree())
Pierre Kraemer's avatar
Pierre Kraemer committed
253
		{
254 255
			delete ptr;
			m_holesBlocks.pop_back();
Pierre Kraemer's avatar
Pierre Kraemer committed
256 257 258
		}
	}

259 260 261 262 263 264 265 266
	// maj de la table de block libre
	m_tableBlocksWithFree.clear();
	HoleBlockRef* block = m_holesBlocks.back();
	if (!block->full())
		m_tableBlocksWithFree.push_back(m_holesBlocks.size() - 1);

	// detruit les blocks de donnees inutiles
	for(unsigned int j = 0; j < m_tableAttribs.size(); ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
267
	{
268 269
		if (m_tableAttribs[j] != NULL)
			m_tableAttribs[j]->setNbBlocks(m_holesBlocks.size());
Pierre Kraemer's avatar
Pierre Kraemer committed
270 271
	}

272 273 274 275 276 277
	m_maxSize = (m_holesBlocks.back())->sizeTable() + (m_holesBlocks.size() - 1) * _BLOCKSIZE_;
}

/**************************************
 *          LINES MANAGEMENT          *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
278

279 280 281 282
unsigned int AttributeContainer::insertLine()
{
	// if no more rooms
	if (m_tableBlocksWithFree.empty())
Pierre Kraemer's avatar
Pierre Kraemer committed
283
	{
284 285 286 287 288 289 290 291 292
		HoleBlockRef* ptr = new HoleBlockRef();					// new block
		m_tableBlocksWithFree.push_back(m_holesBlocks.size());	// add its future position to block_free
		m_holesBlocks.push_back(ptr);							// and add it to block table

		for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
		{
			if (m_tableAttribs[i] != NULL)
				m_tableAttribs[i]->addBlock();					// add a block to every attribute
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
293 294
	}

295 296 297 298 299 300 301 302 303 304 305 306 307 308
	// get the first free block index (last in vector)
	unsigned int bf = m_tableBlocksWithFree.back();
	// get the block
	HoleBlockRef* block = m_holesBlocks[bf];

	// add new element in block and compute index
	unsigned int ne = block->newRefElt(m_maxSize);
	unsigned int index = _BLOCKSIZE_ * bf + ne;

	// if no more room in block remove it from free_blocks
	if (block->full())
		m_tableBlocksWithFree.pop_back();

	++m_size;
Pierre Kraemer's avatar
Pierre Kraemer committed
309

310
//	initLine(index);
Pierre Kraemer's avatar
Pierre Kraemer committed
311

312
	return index;
Pierre Kraemer's avatar
Pierre Kraemer committed
313 314
}

315
void AttributeContainer::removeLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
316
{
317 318
	unsigned int bi = index / _BLOCKSIZE_;
	unsigned int j = index % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
319

320
	HoleBlockRef* block = m_holesBlocks[bi];
Pierre Kraemer's avatar
Pierre Kraemer committed
321

322 323 324 325
	if (block->used(j))
	{
		if (block->full())		// block has no free elements before removal
			m_tableBlocksWithFree.push_back(bi);
Pierre Kraemer's avatar
Pierre Kraemer committed
326

327
		block->removeElt(j);
Pierre Kraemer's avatar
Pierre Kraemer committed
328

329
		--m_size;
Pierre Kraemer's avatar
Pierre Kraemer committed
330

331 332
		if (block->empty())		// block is empty after removal
			m_tableBlocksEmpty.push_back(bi);
Pierre Kraemer's avatar
Pierre Kraemer committed
333
	}
334
	else
335
		CGoGNerr << "Error removing non existing index " << index << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
336 337
}

338 339 340 341
/**************************************
 *            SAVE & LOAD             *
 **************************************/

342
bool AttributeContainer::loadXmlBWF(xmlNodePtr node)
Pierre Kraemer's avatar
Pierre Kraemer committed
343 344 345 346 347 348
{
	xmlChar* prop = xmlGetProp(node, BAD_CAST "nb");
	unsigned int nb = atoi((char*)prop);
	m_tableBlocksWithFree.clear();

	// charge et cree les  attributs
349
	for (xmlNode* x_node = node->children; x_node != NULL; x_node = x_node->next)
Pierre Kraemer's avatar
Pierre Kraemer committed
350 351 352 353 354 355
	{
		unsigned int ind = atoi((char*)(xmlNodeGetContent(x_node)));
		m_tableBlocksWithFree.push_back(ind);
	}
	if (m_tableBlocksWithFree.size() != nb)
	{
356
		CGoGNerr <<"Erreur lecture fichier XML incoherent"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
357 358 359 360 361
		return false;
	}
	return true;
}

362
bool AttributeContainer::loadXmlAN(xmlNodePtr node, unsigned int nbb)
Pierre Kraemer's avatar
Pierre Kraemer committed
363 364 365 366 367 368 369 370 371 372 373 374
{
	xmlChar* prop = xmlGetProp(node, BAD_CAST "nb");
//	unsigned int nb = atoi((char*)prop);
	prop = xmlGetProp(node, BAD_CAST "sv");
//	unsigned int sv = atoi((char*)prop);

	// Noooooooo!!!!
//	m_tableAttribs.resize(sv);
//	for (unsigned int i=0; i< sv; ++i)
//			m_tableAttribs[i]=NULL;

	// charge et cree les  attributs
375
	for (xmlNode* x_node = node->children; x_node != NULL; x_node = x_node->next)
Pierre Kraemer's avatar
Pierre Kraemer committed
376 377 378 379 380 381 382 383 384 385 386
	{
		prop = xmlGetProp(x_node, BAD_CAST "id");
//		unsigned int id = unsigned int(atoi((char*)prop);

		prop = xmlGetProp(x_node, BAD_CAST "type");
		// recupere l'attribut enregistrer par son type
		if (m_attributes_registry_map !=NULL)
		{
			std::map<std::string, RegisteredBaseAttribute*>::iterator itAtt = m_attributes_registry_map->find(std::string((char*)prop));
			if (itAtt == m_attributes_registry_map->end())
			{
387
				CGoGNout << "Skipping non registred attribute "<< std::string((char*)prop)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
388 389 390 391 392 393
			}
			else
			{
				RegisteredBaseAttribute* ra = itAtt->second;
				prop = xmlGetProp(x_node, BAD_CAST "name");
//				ra->addAttribute(*this, std::string((char*)prop), id);
394 395
				AttributeMultiVectorGen* amvg = ra->addAttribute(*this, std::string((char*)prop));
				amvg->setNbBlocks(nbb);
Pierre Kraemer's avatar
Pierre Kraemer committed
396 397 398 399
			}
		}
		else
		{
400
			CGoGNerr << "Attribute Registry non initialized"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
401 402 403 404 405
			return false;
		}
	}
//	if (m_attribNameMap.size() != nb)
//	{
406
//		CGoGNerr << "Pb lecture attributs"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
407 408 409 410 411
//		return false;
//	}
	return true;
}

412
bool AttributeContainer::loadXmlDL(xmlNodePtr node)
Pierre Kraemer's avatar
Pierre Kraemer committed
413 414
{
	// charge et cree les  attributs
415
	for (xmlNode* x_node = node->children; x_node != NULL; x_node = x_node->next)
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419 420 421 422
	{
		// get index
		xmlChar* prop = xmlGetProp(x_node, BAD_CAST "id");
		unsigned int id = atoi((char*)prop);
		// get & set nbref
		prop = xmlGetProp(x_node, BAD_CAST "refs");
		unsigned int nbr = atoi((char*)prop);
423
		setNbRefs(id, nbr);
Pierre Kraemer's avatar
Pierre Kraemer committed
424

Pierre Kraemer's avatar
Pierre Kraemer committed
425
		if (nbr > 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
426
		{
427 428 429 430 431 432
//			for (MapNameId::iterator it = m_attribNameMap.begin(); it != m_attribNameMap.end(); ++it)
//			{
//				prop = xmlGetProp(x_node, BAD_CAST (it->first).c_str());
//				// if name of data unkwown then error
//				if (prop == NULL)
//				{
433
//					CGoGNerr << "inconsistent xml file"<<CGoGNendl;
434 435 436 437
//					return false;
//				}
//				m_tableAttribs[it->second]->input(id, std::string((char*)prop));
//			}
Pierre Kraemer's avatar
Pierre Kraemer committed
438 439 440 441 442
		}
	}
	return true;
}

443
void AttributeContainer::saveXml(xmlTextWriterPtr writer, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
444
{
445 446
	if (m_nbAttributes == 0)
		return;
Pierre Kraemer's avatar
Pierre Kraemer committed
447

448 449 450 451 452
	// noeud du container
	int rc = xmlTextWriterStartElement(writer, BAD_CAST "Attributes_Container");
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "id","%u",id);
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "BlockSize","%u",_BLOCKSIZE_);
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "size","%u",m_maxSize);
Pierre Kraemer's avatar
Pierre Kraemer committed
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 512 513 514 515 516 517
	// recuperer le nombre d'attributs
	unsigned int nbAtt = m_nbAttributes;
	unsigned int sizeVectAtt = m_tableAttribs.size();

	// noeud avec la liste de attributs
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Attributes_Names");
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "nb","%u",nbAtt);
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "sv","%u",sizeVectAtt);

	// recuperer les attributs dans la map et les sauver
//	for (std::map<std::string, unsigned int>::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
//	{
//		int rc = xmlTextWriterStartElement(writer, BAD_CAST "Attribute");
//		rc = xmlTextWriterWriteAttribute(writer,  BAD_CAST "name",BAD_CAST (it->first).c_str());
//		const std::string& str_type = m_tableAttribs[it->second]->getTypeName();
//		rc = xmlTextWriterWriteAttribute(writer,  BAD_CAST "type",BAD_CAST str_type.c_str());
//		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "id","%u",it->second);
//		rc = xmlTextWriterEndElement(writer);
//	}
	// fin du noeud
	rc = xmlTextWriterEndElement(writer);

	// parcourir le container et sauver les lignes
	rc = xmlTextWriterStartElement(writer, BAD_CAST "Data_Lines");
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "size","%u",m_maxSize);
	for (unsigned int i = 0; i != m_maxSize; ++i)
	{
		unsigned int nbr = getNbRefs(i);
		rc = xmlTextWriterStartElement(writer, BAD_CAST "Line");
		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "id","%u",i);
		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "refs","%u",nbr);
		if (nbr > 0)
		{
			// tous les attributs de la ligne
//			for (MapNameId::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
//			{
//				std::string st_att = m_tableAttribs[it->second]->output(i);
//				rc = xmlTextWriterWriteAttribute(writer,(xmlChar*)( (it->first).c_str()), (xmlChar*)( st_att.c_str()));
//			}
		}
		// fin du noeud Line
		rc = xmlTextWriterEndElement(writer);
	}
	// fin du noeud Data Lines
	rc = xmlTextWriterEndElement(writer);

	// fin du noeud Container
	rc = xmlTextWriterEndElement(writer);
}

unsigned int AttributeContainer::getIdXmlNode(xmlNodePtr node)
{
	xmlChar *prop = xmlGetProp(node, BAD_CAST "id");
	unsigned int id = atoi((char*)prop);
	return id;
}

bool AttributeContainer::loadXml(xmlNodePtr node)
{
	xmlChar *prop = xmlGetProp(node, BAD_CAST "BlockSize");
	unsigned int bs = atoi((char*)prop);

	if (bs != _BLOCKSIZE_)
	{
518
		CGoGNerr << "Chargement impossible, tailles de block differentes: "<<_BLOCKSIZE_<<" / " << bs << CGoGNendl;
519 520
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

//	prop = xmlGetProp(node, BAD_CAST "id");
//	unsigned int id = atoi((char*)prop);

	prop = xmlGetProp(node, BAD_CAST "size");
	m_maxSize = atoi((char*)prop);

	char* ANnode = (char*)"Attributes_Names";
	char* DLnode= (char*)"Data_Lines";

	// calcul le nombre de block et les alloue
	unsigned int nbb = m_maxSize/_BLOCKSIZE_;
	if (m_maxSize%_BLOCKSIZE_)
			nbb++;

	m_holesBlocks.resize(nbb);
	for (unsigned int i=0; i<nbb; ++i)
		m_holesBlocks[i] = new HoleBlockRef;

	//load Attributes
	xmlNode* cur = node->children;
	while ( strcmp((char*)(cur->name),ANnode))
		cur = cur->next;
	loadXmlAN(cur,nbb);

	cur = node->children;
	while ( strcmp((char*)(cur->name),DLnode))
		cur = cur->next;
	loadXmlDL(cur);

	// recreate free holes in blocks
	nbb--;
553
	for (unsigned int i = 0; i < nbb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
554
	{
555
		if (m_holesBlocks[i]->updateHoles(_BLOCKSIZE_))
Pierre Kraemer's avatar
Pierre Kraemer committed
556 557
			m_tableBlocksWithFree.push_back(i);
	}
558
	m_holesBlocks[nbb]->updateHoles(m_maxSize - nbb * _BLOCKSIZE_);
Pierre Kraemer's avatar
Pierre Kraemer committed
559 560 561 562

	return true;
}

563
void AttributeContainer::saveBin(CGoGNostream& fs, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
564
{
Pierre Kraemer's avatar
Pierre Kraemer committed
565
	// en ascii id et taille les tailles
Pierre Kraemer's avatar
Pierre Kraemer committed
566 567 568 569 570 571 572 573 574 575 576 577

	std::vector<unsigned int> bufferui;
	bufferui.reserve(6);

	bufferui.push_back(id);
	bufferui.push_back(_BLOCKSIZE_);
	bufferui.push_back(m_holesBlocks.size());
	bufferui.push_back(m_tableBlocksWithFree.size());
	bufferui.push_back(m_nbAttributes);
	bufferui.push_back(m_size);
	bufferui.push_back(m_maxSize);

578
	CGoGNout << "Save Container: id:" <<id <<"  nbAtt:"<<m_nbAttributes << "  size:"<<m_size<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
579 580 581

	fs.write(reinterpret_cast<const char*>(&bufferui[0]) ,bufferui.size()*sizeof(unsigned int));

Pierre Kraemer's avatar
Pierre Kraemer committed
582
	unsigned int i = 0;
583
	for(std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
584
	{
585
		if (*it != NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
586
			(*it)->saveBin(fs, i++);
Pierre Kraemer's avatar
Pierre Kraemer committed
587 588 589
	}

	//en binaire les blocks de ref
Pierre Kraemer's avatar
Pierre Kraemer committed
590 591
	for (std::vector<HoleBlockRef*>::iterator it = m_holesBlocks.begin(); it != m_holesBlocks.end(); ++it)
		(*it)->saveBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
592 593

	// les indices des blocks libres
594
	fs.write(reinterpret_cast<const char*>(&m_tableBlocksWithFree[0]), m_tableBlocksWithFree.size() * sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
595 596
}

597
unsigned int AttributeContainer::loadBinId(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
598 599 600 601 602 603
{
	unsigned int id;
	fs.read(reinterpret_cast<char*>(&id), sizeof(unsigned int));
	return id;
}

604
bool AttributeContainer::loadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
605
{
606
	if (m_attributes_registry_map == NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
607
	{
608
		CGoGNerr << "Attribute Registry non initialized"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
609 610 611 612 613 614 615 616
		return false;
	}

	std::vector<unsigned int> bufferui;
	bufferui.resize(256);

	fs.read(reinterpret_cast<char*>(&(bufferui[0])), 6*sizeof(unsigned int));

Pierre Kraemer's avatar
Pierre Kraemer committed
617
	unsigned int bs, szHB, szBWF, nbAtt;
Pierre Kraemer's avatar
Pierre Kraemer committed
618 619 620 621 622 623 624 625 626
	bs = bufferui[0];
	szHB = bufferui[1];
	szBWF = bufferui[2];
	nbAtt = bufferui[3];
	m_size = bufferui[4];
	m_maxSize = bufferui[5];

	if (bs != _BLOCKSIZE_)
	{
627
		CGoGNerr << "Chargement impossible, tailles de block differentes: "<<_BLOCKSIZE_<<" / " << bs << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
628 629 630
		return false;
	}

631
	for (unsigned int j = 0; j < nbAtt; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
632 633 634
	{
		std::string nameAtt;
		std::string typeAtt;
635
		/*unsigned int id = */AttributeMultiVectorGen::loadBinInfos(fs,nameAtt, typeAtt);
Pierre Kraemer's avatar
Pierre Kraemer committed
636 637 638 639

		std::map<std::string, RegisteredBaseAttribute*>::iterator itAtt = m_attributes_registry_map->find(typeAtt);
		if (itAtt == m_attributes_registry_map->end())
		{
640
			CGoGNout << "Skipping non registred attribute of type name"<< typeAtt <<CGoGNendl;
641
			AttributeMultiVectorGen::skipLoadBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
642 643 644 645
		}
		else
		{
			RegisteredBaseAttribute* ra = itAtt->second;
646
			AttributeMultiVectorGen* amvg = ra->addAttribute(*this, nameAtt);
647
			CGoGNout << "loading attribute " << nameAtt << " : " << typeAtt << CGoGNendl;
648
			amvg->loadBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
649 650 651 652 653 654 655 656
			// no need the set the nb of block (done by binary read of attribmv)
			m_nbAttributes++;
		}
	}

	m_holesBlocks.resize(szHB);

	// blocks
Pierre Kraemer's avatar
Pierre Kraemer committed
657
	for (unsigned int i = 0; i < szHB; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
658 659 660 661 662 663 664
	{
		m_holesBlocks[i] = new HoleBlockRef;
		m_holesBlocks[i]->loadBin(fs);
	}

	// les indices des blocks libres
	m_tableBlocksWithFree.resize(szBWF);
665
	fs.read(reinterpret_cast<char*>(&(m_tableBlocksWithFree[0])), szBWF*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
666 667 668 669 670

	return true;
}

}