diff --git a/apps/nexus/pintersect.h b/apps/nexus/pintersect.h index e15010cd..08dfa88a 100644 --- a/apps/nexus/pintersect.h +++ b/apps/nexus/pintersect.h @@ -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 +#include +#include #include namespace nxs { -template 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 PIntersect { + public: + Partition *fine; + Partition *coarse; + + // typedef typename Partition::Key Key; + unsigned int last_cell; - std::map pairs; + std::map pairs; std::set 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 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::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