From ef01c541893ddc4a43e63afe0552a07abd2c66a5 Mon Sep 17 00:00:00 2001 From: ponchio Date: Thu, 24 Jun 2004 14:19:20 +0000 Subject: [PATCH] Debugged --- wrap/nexus/pchain.h | 33 +++++++++++++++++++++++++-- wrap/nexus/pvoronoi.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ wrap/nexus/pvoronoi.h | 12 +++++++++- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/wrap/nexus/pchain.h b/wrap/nexus/pchain.h index c139170f..4fa36864 100644 --- a/wrap/nexus/pchain.h +++ b/wrap/nexus/pchain.h @@ -24,19 +24,27 @@ History $Log: not supported by cvs2svn $ +Revision 1.1 2004/06/23 17:17:46 ponchio +Created + ****************************************************************************/ #ifndef NXS_PCHAIN_H #define NXS_PCHAIN_H +#include #include +/** Partition must be a class with a Key type, with + Levels, Locate, Priority, Save(FILE *), Load(FILE *) + as in pvoronoi.h */ namespace { template class PChain { public: typedef typename Partition::Key Key; + std::vector levels; unsigned int Levels() { return levels.size(); @@ -51,8 +59,29 @@ template class PChain { assert(level < levels.size()); return levels[level].Priority(level, p, key); } - private: - std::vector levels; + bool Save(const std::string &file) { + FILE *fp = fopen(file.c_str(), "wb+"); + if(!fp) return false; + int n = Levels(); + fwrite(&n, sizeof(int), 1, fp); + for(int i = 0; i < n; i++) + levels[i].Save(fp); + fclose(fp); + return true; + } + bool Load(const std::string &file) { + levels.clear(); + FILE *fp = fopen(file.c_str(), "rb"); + if(!fp) return false; + int n; + fread(&n, sizeof(int), 1, fp); + levels.resize(n); + for(int i = 0; i < n; i++) + levels[i].Load(fp); + + fclose(fp); + return true; + } }; }//namespace diff --git a/wrap/nexus/pvoronoi.cpp b/wrap/nexus/pvoronoi.cpp index cc9abdef..d3eae937 100644 --- a/wrap/nexus/pvoronoi.cpp +++ b/wrap/nexus/pvoronoi.cpp @@ -24,11 +24,17 @@ History $Log: not supported by cvs2svn $ +Revision 1.1 2004/06/23 17:17:46 ponchio +Created + ****************************************************************************/ #pragma warning(disable:4786 4804 4244 4018 4267 4311) +#include +#include #include "pvoronoi.h" + using namespace std; using namespace vcg; using namespace nxs; @@ -119,6 +125,11 @@ unsigned int VoronoiPartition::count(Key key) { return key > 0 && key < size(); } +Seed &VoronoiPartition::operator[](Key key) { + assert(key < all_seeds.size()); + return all_seeds[key]; +} + VoronoiPartition::Key VoronoiPartition::Locate(const vcg::Point3f &p) { int target; Closest(p, target); @@ -131,3 +142,41 @@ float VoronoiPartition::Priority(const vcg::Point3f &p, Key key) { return seed.Dist(p); } +bool VoronoiPartition::Save(const std::string &file) { + FILE *fp = fopen(file.c_str(), "wb+"); + if(!fp) return false; + Save(fp); + fclose(fp); + return true; +} + +bool VoronoiPartition::Load(const std::string &file) { + FILE *fp = fopen(file.c_str(), "rb"); + if(!fp) return false; + Load(fp); + fclose(fp); + return true; +} + +unsigned int VoronoiPartition::Save(FILE *fp) { + fwrite(&bbox, sizeof(Box3f), 1, fp); + int n = all_seeds.size(); + fwrite(&n, sizeof(int), 1, fp); + fwrite(&all_seeds[0], sizeof(Seed), all_seeds.size(), fp); + return sizeof(Box3f) + sizeof(int) + sizeof(Seed) * all_seeds.size(); +} + +unsigned int VoronoiPartition::Load(FILE *fp) { + clear(); + fread(&bbox, sizeof(Box3f), 1, fp); + int n; + fread(&n, sizeof(int), 1, fp); + all_seeds.resize(n); + fread(&all_seeds[0], sizeof(Seed), all_seeds.size(), fp); + ug_seeds.resize(n); + for(int i = 0; i < n; i++) + ug_seeds[i] = all_seeds[i]; + ug.SetBBox(bbox); + ug.Set(ug_seeds); + return sizeof(Box3f) + sizeof(int) + sizeof(Seed) * all_seeds.size(); +} diff --git a/wrap/nexus/pvoronoi.h b/wrap/nexus/pvoronoi.h index 0629fd21..4f4c89e3 100644 --- a/wrap/nexus/pvoronoi.h +++ b/wrap/nexus/pvoronoi.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.1 2004/06/23 17:17:46 ponchio +Created + ****************************************************************************/ @@ -32,6 +35,8 @@ $Log: not supported by cvs2svn $ #include #include +#include +#include #include #include @@ -60,7 +65,7 @@ namespace nxs { return weight * weight *vcg::SquaredDistance(p,q); } }; - + class VoronoiPartition { public: @@ -87,10 +92,15 @@ namespace nxs { iterator end(); int size(); unsigned int count(Key key); + Seed &operator[](Key key); void clear(); Key Locate(const vcg::Point3f &p); float Priority(const vcg::Point3f &p, Key key); + bool Save(const std::string &file); + bool Load(const std::string &file); + unsigned int Save(FILE *fp); + unsigned int Load(FILE *fp); private: vcg::Box3f bbox; vcg::GridStaticPtr< std::vector > ug;