vcglib/apps/nexus/vchain.cpp

135 lines
4.9 KiB
C++
Raw Normal View History

2005-02-08 13:43:03 +01:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
2004-11-30 23:50:30 +01:00
#include "vchain.h"
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace vcg;
using namespace nxs;
2004-12-03 22:19:00 +01:00
VChain::~VChain() {
for(iterator i = begin(); i != end(); i++)
delete *i;
}
2004-11-30 23:50:30 +01:00
bool VChain::Save(const string &file) {
FILE *fp = fopen(file.c_str(), "wb+");
if(!fp) {
cerr << "Could not save vchain data.\n";
return false;
}
unsigned int nlevels = size();
fwrite(&nlevels, sizeof(unsigned int), 1, fp);
2004-12-01 04:25:58 +01:00
for(unsigned int i = 0; i < nlevels; i++) {
2004-12-03 22:19:00 +01:00
VPartition &level = *operator[](i);
2004-11-30 23:50:30 +01:00
unsigned int npoints = level.size();
fwrite(&npoints, sizeof(unsigned int), 1, fp);
fwrite(&(level[0]), sizeof(Point3f), npoints, fp);
}
//writing fragments
unsigned int nfrag = newfragments.size();
fwrite(&nfrag, sizeof(unsigned int), 1, fp);
2004-12-01 04:25:58 +01:00
std::map<unsigned int, std::set<unsigned int> >::iterator j;
for(j = newfragments.begin(); j != newfragments.end(); j++) {
unsigned int n = (*j).second.size();
fwrite(&((*j).first), sizeof(unsigned int), 1, fp);
2004-11-30 23:50:30 +01:00
fwrite(&n, sizeof(unsigned int), 1, fp);
set<unsigned int>::iterator k;
2004-12-01 04:25:58 +01:00
for(k = (*j).second.begin(); k != (*j).second.end(); k++)
2004-11-30 23:50:30 +01:00
fwrite(&*k, sizeof(unsigned int), 1, fp);
}
nfrag = oldfragments.size();
fwrite(&nfrag, sizeof(unsigned int), 1, fp);
2004-12-01 04:25:58 +01:00
for(j = oldfragments.begin(); j != oldfragments.end(); j++) {
unsigned int n = (*j).second.size();
fwrite(&((*j).first), sizeof(unsigned int), 1, fp);
2004-11-30 23:50:30 +01:00
fwrite(&n, sizeof(unsigned int), 1, fp);
set<unsigned int>::iterator k;
2004-12-01 04:25:58 +01:00
for(k = (*j).second.begin(); k != (*j).second.end(); k++)
2004-11-30 23:50:30 +01:00
fwrite(&*k, sizeof(unsigned int), 1, fp);
}
fclose(fp);
return true;
}
bool VChain::Load(const string &file) {
FILE *fp = fopen(file.c_str(), "rb");
if(!fp) {
cerr << "Could not load vchain data\n";
return false;
}
unsigned int nlevels;
fread(&nlevels, sizeof(unsigned int), 1, fp);
2004-12-01 04:25:58 +01:00
for(unsigned int i = 0; i < nlevels; i++) {
2004-12-03 22:19:00 +01:00
push_back(new VPartition());
VPartition &level = *back();
2004-11-30 23:50:30 +01:00
unsigned int npoints;
fread(&npoints, sizeof(unsigned int), 1, fp);
level.resize(npoints);
fread(&(level[0]), sizeof(Point3f), npoints, fp);
level.Init();
}
//reading fragments
unsigned int nfrag;
fread(&nfrag, sizeof(unsigned int), 1, fp);
for(unsigned int i = 0; i < nfrag; i++) {
unsigned int p, n;
fread(&p, sizeof(unsigned int), 1, fp);
set<unsigned int> &s = newfragments[p];
fread(&n, sizeof(unsigned int), 1, fp);
for(unsigned int k = 0; k < n; k++) {
unsigned int j;
fread(&j, sizeof(unsigned int), 1, fp);
s.insert(j);
}
}
fread(&nfrag, sizeof(unsigned int), 1, fp);
for(unsigned int i = 0; i < nfrag; i++) {
unsigned int p, n;
fread(&p, sizeof(unsigned int), 1, fp);
set<unsigned int> &s = oldfragments[p];
fread(&n, sizeof(unsigned int), 1, fp);
for(unsigned int k = 0; k < n; k++) {
unsigned int j;
fread(&j, sizeof(unsigned int), 1, fp);
s.insert(j);
}
}
fclose(fp);
return true;
}