diff --git a/apps/nexus/pvoronoi.cpp b/apps/nexus/pvoronoi.cpp index 56792b3b..6b4ab3af 100644 --- a/apps/nexus/pvoronoi.cpp +++ b/apps/nexus/pvoronoi.cpp @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.4 2004/09/21 00:53:23 ponchio +Lotsa changes. + Revision 1.3 2004/08/27 00:39:28 ponchio Rewrote. @@ -62,19 +65,13 @@ bool Seed::Dist(const Point3f &point, float &mindist, return false; } -unsigned int VoronoiPartition::Add(const vcg::Point3f &p, - float weight) { - Seed ns(p,weight); - all_seeds.push_back(ns); - seedBuf.push_back(p); - - if(seedBuf.size() >= MAX_BUF) { - for(unsigned int i = 0; i < seedBuf.size(); ++i) - ug_seeds.push_back(seedBuf[i]); - seedBuf.clear(); - ug.Set(ug_seeds); - } - return size(); +void VoronoiPartition::Init() { + assert(size() > 0); + for(iterator i = begin(); i != end(); i++) + box.Add((*i).p); + + ug.SetBBox(box); + ug.Set(*(vector *)this); } float VoronoiPartition::Closest(const vcg::Point3f &p, @@ -83,160 +80,10 @@ float VoronoiPartition::Closest(const vcg::Point3f &p, float mindist = 1e20; target = 0xffffffff; - if(ug_seeds.size()) { - Seed *nsp = ug.GetClosest(p, mindist, res); - if(nsp) { - target = nsp-&*ug_seeds.begin(); - } - } - - for(unsigned int i=0;i dist) { - target=ug_seeds.size()+i; - mindist=dist; - } - } - - //assert(target >=0 ); - //assert (target < size()+seedBuf.size()); + Seed *nsp = ug.GetClosest(p, mindist, res); + if(nsp) + target = nsp-&*begin(); + return mindist; } - -void VoronoiPartition::iterator::operator++() { - ++seed; -} -const unsigned int VoronoiPartition::iterator::operator*() { - return seed; -} -bool VoronoiPartition::iterator::operator==(const VoronoiPartition::iterator &key) { - return key.seed == seed; -} -bool VoronoiPartition::iterator::operator!=(const VoronoiPartition::iterator &key) { - return key.seed != seed; -} - -VoronoiPartition::iterator VoronoiPartition::begin() { - iterator i; - i.seed = 0; - return i; -} -VoronoiPartition::iterator VoronoiPartition::end() { - iterator i; - i.seed = size(); - return i; -} -int VoronoiPartition::size() { - return all_seeds.size(); -} -void VoronoiPartition::clear() { - all_seeds.clear(); - ug_seeds.clear(); - seedBuf.clear(); -} -unsigned int VoronoiPartition::count(unsigned int key) { - return key > 0 && key < (unsigned int)size(); -} - -Seed &VoronoiPartition::operator[](unsigned int key) { - assert(key < all_seeds.size()); - return all_seeds[key]; -} - -unsigned int VoronoiPartition::Locate(const vcg::Point3f &p) { - unsigned int target; - Closest(p, target); - assert(target != 0xffffffff); - return target; -} - -float VoronoiPartition::Priority(const vcg::Point3f &p, unsigned int key) { - Seed &seed = all_seeds[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(); -} - -float VoronoiPartition::OptimalRadius(Crude &crude, unsigned int target) { - - //TODO this goes into voronoichain.cpp! - - //number of samples - unsigned int samplerate = crude.Vertices()/20; - std::vector samples; - - for(unsigned int i = 0; i < crude.Vertices(); i+= samplerate) - samples.push_back(crude.GetVertex(i)); - - cerr << "sample.size(): " << samples.size() << endl; - float step = crude.GetBox().Diag()/10000; - - cerr << "step: " << step << endl; - - //for every sample i need to record function distance -> number of points - vector scale; - scale.resize(10001, 0); - - //for every point we check distance from samples - for(unsigned int i = 0; i < crude.Vertices(); i++) { - vcg::Point3f &vp = crude.GetVertex(i);; - for(unsigned int k = 0; k < samples.size(); k++) { - float dist = (vp - samples[k]).Norm(); - unsigned int pos = (int)(dist/step); - if(pos < 10000) - scale[pos]++; - } - } - - - float count =0; - int counting; - for(int j = 0; j < 10000; j++) { - count += scale[j]; - if(count > samples.size() * target) { - counting = j; - break; - } - } - cerr << "Counting: " << counting << endl; - cerr << "radius: " << 2 * step * counting << endl; - return 2 * step * counting; -} diff --git a/apps/nexus/pvoronoi.h b/apps/nexus/pvoronoi.h index 44f7f923..53afc7ae 100644 --- a/apps/nexus/pvoronoi.h +++ b/apps/nexus/pvoronoi.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.6 2004/09/21 00:53:23 ponchio +Lotsa changes. + Revision 1.5 2004/08/27 00:39:28 ponchio Rewrote. @@ -89,114 +92,24 @@ namespace nxs { }; - class VoronoiPartition { + class VoronoiPartition: public std::vector { public: - enum { MAX_BUF=25 }; - - VoronoiPartition() {} - - void Init(vcg::Box3f &bb) { bbox=bb; ug.SetBBox(bb); } - unsigned int Add(const vcg::Point3f &p, float weight = 1); + vcg::GridStaticPtr< std::vector > ug; + + void SetBox(const vcg::Box3f &b) { box = b; } + //call this before starting queries. + void Init(); float Closest(const vcg::Point3f &p, unsigned int &target, float radius = 0); - class iterator { - public: - void operator++(); - const unsigned int operator*(); - bool operator==(const iterator &key); - bool operator!=(const iterator &key); - private: - unsigned int seed; - friend class VoronoiPartition; - }; - iterator begin(); - iterator end(); - int size(); - unsigned int count(unsigned int key); - Seed &operator[](unsigned int key); - void clear(); - void reload() { ug_seeds = all_seeds; ug.Set(ug_seeds); } - unsigned int Locate(const vcg::Point3f &p); - float Priority(const vcg::Point3f &p, unsigned int key); - - bool Save(const std::string &file); - bool Load(const std::string &file); - unsigned int Save(FILE *fp); - unsigned int Load(FILE *fp); - - /** Pass iterators to Point3f container and size - to estimate optimal radius. - At the moment strategy is to campion randomly the file. - */ - - static float OptimalRadius(Crude &crude, unsigned int target); - template - static std::vector OptimalRadii(unsigned int total, - T begin, T end, - vcg::Box3f &box, - std::vector target) { - - //number of samples - unsigned int n_points = 20; - std::vector samples; - - T i; - unsigned int h; - for(i = begin, h =0; i != end; ++i, h++) - if(!((h+1)%(total/n_points))) - samples.push_back(*i); - - - float step = box.Diag()/10000; - - //for every sample i need to record function distance -> number of points - std::vector< std::vector > scale; - scale.resize(samples.size()); - for(unsigned int i = 0; i < samples.size(); i++) - scale[i].resize(10001, 0); - - - //for every point we check distance from samples - for(i = begin; i != end; ++i) { - vcg::Point3f &vp = *i; - for(unsigned int k = 0; k < samples.size(); k++) { - float dist = (vp - samples[k]).Norm(); - unsigned int pos = (int)(dist/step); - if(pos < 10000) - scale[k][pos]++; - } - } - - - float count =0; - unsigned int tcount = 0; - std::vector counting; - for(int j = 0; j < 10000; j++) { - for(unsigned int k = 0; k < samples.size(); k++) - count += scale[k][j]; - if(count > samples.size() * target[tcount]) { - counting.push_back(j); - tcount ++; - if(tcount >= target.size()) - j = 10000; - } - } - std::vector radius; - for(unsigned int i = 0; i < counting.size(); i++) - radius.push_back(2 * step * (counting[i])); - return radius; - } - - // private: - vcg::Box3f bbox; - vcg::GridStaticPtr< std::vector > ug; - std::vector all_seeds; - std::vector ug_seeds; - std::vector seedBuf; + unsigned int Locate(const vcg::Point3f &p) { + unsigned int target; + Closest(p, target); + return target; + } + vcg::Box3f box; }; - } #endif