Save, load and other changes.

This commit is contained in:
Federico Ponchio 2004-07-20 14:11:07 +00:00
parent 5a32aed258
commit 594d17f8b9
1 changed files with 76 additions and 22 deletions

View File

@ -24,53 +24,67 @@
History
$Log: not supported by cvs2svn $
Revision 1.2 2004/07/01 21:46:36 ponchio
Added header.
****************************************************************************/
#ifndef PARTITION_INTERSECT_H
#define PARTITION_INTERSECT_H
#include <map>
#include <set>
#include <string>
#include <vcg/space/point3.h>
namespace nxs {
template <class Partition> class PIntersect {
public:
Partition &fine;
Partition &coarse;
typedef typename Partition::Key Key;
struct Pair {
Key fine;
Key coarse;
bool operator<(const Pair &p) const {
struct IPair {
unsigned int fine;
unsigned int coarse;
bool operator<(const IPair &p) const {
if(fine == p.fine) return coarse < p.coarse;
return fine < p.fine;
}
};
struct Cell {
struct ICell {
unsigned int index;
unsigned int count;
};
template <class Partition> class PIntersect {
public:
Partition *fine;
Partition *coarse;
// typedef typename Partition::Key Key;
unsigned int last_cell;
std::map<Pair, Cell> pairs;
std::map<IPair, ICell> pairs;
std::set<unsigned int> cells;
PIntersect(Partition &f, Partition &c):
PIntersect(Partition *f = NULL, Partition *c = NULL):
fine(f), coarse(c), last_cell(0) {}
void Init(Partition *f, Partition *c, unsigned int of_cell) {
fine = f;
coarse = c;
last_cell = of_cell;
}
unsigned int Locate(const vcg::Point3f &p) {
Pair pp;
pp.fine = fine.Locate(p);
pp.coarse = coarse.Locate(p);
IPair pp;
pp.fine = fine->Locate(p);
pp.coarse = coarse->Locate(p);
if(pairs.count(pp)) { //already there
Cell &cell = pairs[pp];
ICell &cell = pairs[pp];
cell.count++;
return cell.index;
} else {
Cell &cell = pairs[pp];
ICell &cell = pairs[pp];
cell.index = last_cell++;
cell.count = 1;
cells.insert(cell.index);
@ -85,11 +99,51 @@ template <class Partition> class PIntersect {
void Prune(unsigned int threshold) {
//TODO!!!!!!
}
unsigned int Count(unsigned int cell) {
return cells.count(cell);
}
bool Save(const std::string &file) {
FILE *fp = fopen((file + ".chi").c_str(), "wb+");
if(!fp)
return false;
unsigned int n = pairs.size();
fwrite(&n, sizeof(unsigned int), 1, fp);
std::map<IPair, ICell>::iterator i;
for(i = pairs.begin(); i != pairs.end(); i++) {
IPair pair = (*i).first;
ICell cell = (*i).second;
fwrite(&pair, sizeof(IPair), 1, fp);
fwrite(&cell, sizeof(ICell), 1, fp);
}
fclose(fp);
return true;
}
bool Load(const std::string &file) {
pairs.clear();
cells.clear();
FILE *fp = fopen((file + ".chi").c_str(), "rb");
if(!fp)
return false;
unsigned int n;
fread(&n, sizeof(unsigned int), 1, fp);
IPair pair;
ICell cell;
for(unsigned int i = 0; i < n; i++) {
fread(&pair, sizeof(IPair), 1, fp);
fread(&cell, sizeof(ICell), 1, fp);
pairs[pair] = cell;
cells.insert(cell.index);
}
fclose(fp);
return true;
}
};
}
}//namespace
#endif