Save, load and other changes.
This commit is contained in:
parent
5a32aed258
commit
594d17f8b9
|
@ -24,53 +24,67 @@
|
||||||
History
|
History
|
||||||
|
|
||||||
$Log: not supported by cvs2svn $
|
$Log: not supported by cvs2svn $
|
||||||
|
Revision 1.2 2004/07/01 21:46:36 ponchio
|
||||||
|
Added header.
|
||||||
|
|
||||||
|
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
#ifndef PARTITION_INTERSECT_H
|
#ifndef PARTITION_INTERSECT_H
|
||||||
#define PARTITION_INTERSECT_H
|
#define PARTITION_INTERSECT_H
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
#include <vcg/space/point3.h>
|
#include <vcg/space/point3.h>
|
||||||
|
|
||||||
namespace nxs {
|
namespace nxs {
|
||||||
|
|
||||||
template <class Partition> class PIntersect {
|
struct IPair {
|
||||||
public:
|
unsigned int fine;
|
||||||
Partition &fine;
|
unsigned int coarse;
|
||||||
Partition &coarse;
|
bool operator<(const IPair &p) const {
|
||||||
|
|
||||||
typedef typename Partition::Key Key;
|
|
||||||
|
|
||||||
struct Pair {
|
|
||||||
Key fine;
|
|
||||||
Key coarse;
|
|
||||||
bool operator<(const Pair &p) const {
|
|
||||||
if(fine == p.fine) return coarse < p.coarse;
|
if(fine == p.fine) return coarse < p.coarse;
|
||||||
return fine < p.fine;
|
return fine < p.fine;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct Cell {
|
|
||||||
|
struct ICell {
|
||||||
unsigned int index;
|
unsigned int index;
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <class Partition> class PIntersect {
|
||||||
|
public:
|
||||||
|
Partition *fine;
|
||||||
|
Partition *coarse;
|
||||||
|
|
||||||
|
// typedef typename Partition::Key Key;
|
||||||
|
|
||||||
unsigned int last_cell;
|
unsigned int last_cell;
|
||||||
std::map<Pair, Cell> pairs;
|
std::map<IPair, ICell> pairs;
|
||||||
std::set<unsigned int> cells;
|
std::set<unsigned int> cells;
|
||||||
|
|
||||||
PIntersect(Partition &f, Partition &c):
|
PIntersect(Partition *f = NULL, Partition *c = NULL):
|
||||||
fine(f), coarse(c), last_cell(0) {}
|
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) {
|
unsigned int Locate(const vcg::Point3f &p) {
|
||||||
Pair pp;
|
IPair pp;
|
||||||
pp.fine = fine.Locate(p);
|
pp.fine = fine->Locate(p);
|
||||||
pp.coarse = coarse.Locate(p);
|
pp.coarse = coarse->Locate(p);
|
||||||
if(pairs.count(pp)) { //already there
|
if(pairs.count(pp)) { //already there
|
||||||
Cell &cell = pairs[pp];
|
ICell &cell = pairs[pp];
|
||||||
cell.count++;
|
cell.count++;
|
||||||
return cell.index;
|
return cell.index;
|
||||||
} else {
|
} else {
|
||||||
Cell &cell = pairs[pp];
|
ICell &cell = pairs[pp];
|
||||||
cell.index = last_cell++;
|
cell.index = last_cell++;
|
||||||
cell.count = 1;
|
cell.count = 1;
|
||||||
cells.insert(cell.index);
|
cells.insert(cell.index);
|
||||||
|
@ -85,11 +99,51 @@ template <class Partition> class PIntersect {
|
||||||
void Prune(unsigned int threshold) {
|
void Prune(unsigned int threshold) {
|
||||||
//TODO!!!!!!
|
//TODO!!!!!!
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Count(unsigned int cell) {
|
unsigned int Count(unsigned int cell) {
|
||||||
return cells.count(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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue