From f9dd9b04167da487e4e0a9cdcc858a19c1180777 Mon Sep 17 00:00:00 2001 From: ponchio Date: Wed, 3 Nov 2004 16:31:38 +0000 Subject: [PATCH] Trying to fix big patches. --- apps/nexus/pvoronoi.cpp | 29 +++++++--- apps/nexus/pvoronoi.h | 8 ++- apps/nexus/voronoichain.cpp | 105 ++++++++++++++++++++++++------------ apps/nexus/voronoinxs.cpp | 6 ++- 4 files changed, 104 insertions(+), 44 deletions(-) diff --git a/apps/nexus/pvoronoi.cpp b/apps/nexus/pvoronoi.cpp index dc061fc4..48a8a087 100644 --- a/apps/nexus/pvoronoi.cpp +++ b/apps/nexus/pvoronoi.cpp @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.7 2004/10/30 20:17:03 ponchio +Fixed big patches problem. + Revision 1.6 2004/10/15 11:41:03 ponchio Tests and small changes. @@ -52,15 +55,17 @@ Created ****************************************************************************/ #include -#include + #include "pvoronoi.h" #include +#include + using namespace std; using namespace vcg; using namespace nxs; -void VoronoiPartition::Init() { +void VoronoiPartition::Init() { if(bd) delete bd; buffer.resize(size() * 3); for(unsigned int i = 0; i < size(); i++) { @@ -71,29 +76,39 @@ void VoronoiPartition::Init() { for(unsigned int i = 0; i < size(); i++) { points[i] = &buffer[i*3]; } + /*FILE *ft = fopen("points.txt", "wb+"); + if(!ft) { + std::cerr <<" AHOI!" << endl; + exit(0); + } + for(unsigned int i = 0; i < size(); i++) { + fprintf(ft, "%f\t%f\t%f\n", operator[](i)[0], operator[](i)[1], operator[](i)[2]); + } + fclose(ft);*/ + std::cerr << "Building kd!\n"; bd = new ANNkd_tree(&*points.begin(), size(), 3); - + std::cerr << "Done!\n"; } void VoronoiPartition::Closest(const vcg::Point3f &p, unsigned int nsize, - vector &near, + vector &nears, vector &dist) { double point[3]; point[0] = p[0]; point[1] = p[1]; point[2] = p[2]; - near.resize(nsize); + nears.resize(nsize); dist.resize(nsize); vector dists; dists.resize(nsize); - bd->annkSearch(&point[0], nsize, &*near.begin(), &*dists.begin()); + bd->annkSearch(&point[0], nsize, &*nears.begin(), &*dists.begin()); for(unsigned int i = 0; i < nsize; i++) dist[i] = (float)dists[i]; } void VoronoiPartition::Closest(const vcg::Point3f &p, int &target, float &dist) { - double point[3]; + double point[3]; point[0] = p[0]; point[1] = p[1]; point[2] = p[2]; diff --git a/apps/nexus/pvoronoi.h b/apps/nexus/pvoronoi.h index ccd85dce..1bf9226d 100644 --- a/apps/nexus/pvoronoi.h +++ b/apps/nexus/pvoronoi.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.9 2004/10/30 20:17:03 ponchio +Fixed big patches problem. + Revision 1.8 2004/10/15 11:41:03 ponchio Tests and small changes. @@ -71,7 +74,8 @@ Created //TODO provide a Sort function, to sort spatially the seeds. class ANNkd_tree; - +class ANNbd_tree; +class ANNbruteForce; namespace nxs { @@ -81,7 +85,7 @@ namespace nxs { VoronoiPartition(): bd(NULL) {} void Init(); void Closest(const vcg::Point3f &p, unsigned int nsize, - std::vector &near, + std::vector &nears, std::vector &dist); void Closest(const vcg::Point3f &p, int &target, float &dist); diff --git a/apps/nexus/voronoichain.cpp b/apps/nexus/voronoichain.cpp index 6393eec5..07b29dda 100644 --- a/apps/nexus/voronoichain.cpp +++ b/apps/nexus/voronoichain.cpp @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.18 2004/10/30 20:17:03 ponchio +Fixed big patches problem. + Revision 1.17 2004/10/29 16:33:29 ponchio Trying to fix big patches. @@ -92,36 +95,47 @@ void print(Point3f p) { cerr << p[0] << " " << p[1] << " " << p[2] << endl; } +//return first non zero distance point. float getClosest(const Point3f &seed, VoronoiPartition &part) { - vector near; - vector dist; - part.Closest(seed, 2, near, dist); - for(int k = 0; k < 2; k++) { - int c = near[k]; - assert(c >= 0); - assert(c < part.size()); - if(part[c] == seed) continue; - return Distance(seed, part[c]); + vector nears; + vector dists; + float dist = 0; + int count = 1; + while(dist == 0) { + if(count > part.size()) { + cerr << "This should never happen!!!!\n"; + exit(0); + } + part.Closest(seed, count, nears, dists); + for(int k = 0; k < count; k++) { + int c = nears[k]; + assert(c >= 0); + assert(c < part.size()); + if(dists[k] > 0 && (dist == 0 || dists[k] < dist)) { + dist = dists[k]; + } + } + count++; } - return -1; + return sqrt(dist); } int getBest(const Point3f &seed, VoronoiPartition &part, vector &mark, vector &counts) { - vector near; + vector nears; vector dist; int nnear = 7; if(part.size() < 7) nnear = part.size()/2; if(!nnear) return -1; - part.Closest(seed, nnear, near, dist); + part.Closest(seed, nnear, nears, dist); int best = -1; int bestcount = -1; int bestdist = -1; for(int k = 0; k < nnear; k++) { - int c = near[k]; + int c = nears[k]; assert(c >= 0); assert(c < part.size()); if(mark[c]) continue; if(part[c] == seed) continue; @@ -154,38 +168,57 @@ bool VoronoiChain::Optimize(int mean, VoronoiPartition &part, //first pass we check only big ones for(unsigned int i = 0; i < part.size(); i++) { if(counts[i] > max_size || counts[i] > 2 * mean) { - failed++; - cerr << "Failed> " << counts[i] << endl; - float radius= getClosest(part[i], part); - radius /= 3; - if(radius < 0) continue; - seeds.push_back(part[i] + Point3f(1, 0, 0) * radius); - seeds.push_back(part[i] + Point3f(0, 1, 0) * radius); - seeds.push_back(part[i] + Point3f(0, 0, 1) * radius); - - seeds.push_back(part[i] - Point3f(1, 0, 0) * radius); - seeds.push_back(part[i] - Point3f(0, 1, 0) * radius); - seeds.push_back(part[i] - Point3f(0, 0, 1) * radius); - mark[i]; + failed++; + cerr << "Failed> " << counts[i] << endl; + float radius= getClosest(part[i], part); + cerr << "RADIUS: " << radius << endl; + if(radius == 0) { + cerr << "Radius zero???\n"; + exit(0); + } + radius /= 3; + if(radius < 0) continue; + seeds.push_back(part[i] + Point3f(1, 0, 0) * radius); + seeds.push_back(part[i] + Point3f(0, 1, 0) * radius); + seeds.push_back(part[i] + Point3f(0, 0, 1) * radius); + + seeds.push_back(part[i] - Point3f(1, 0, 0) * radius); + seeds.push_back(part[i] - Point3f(0, 1, 0) * radius); + seeds.push_back(part[i] - Point3f(0, 0, 1) * radius); + mark[i]; } } + cerr << "Join now!" << endl; for(unsigned int i = 0; i < part.size(); i++) { if(mark[i]) continue; if(join && counts[i] < min_size) { failed++; - int best = getBest(part[i], part, mark, counts); - if(best < 0) continue; - assert(mark[best] == false); - mark[best] = true; - mark[i] = true; - seeds.push_back((part[i] + part[best])/2); + int best = getBest(part[i], part, mark, counts); + if(best < 0) { + cerr << "Best not found! how strange!\n"; + continue; + } + if(best >= part.size()) { + cerr << "Invalid best!!!\n"; + exit(0); + } + assert(mark[best] == false); + mark[best] = true; + mark[i] = true; + seeds.push_back((part[i] + part[best])/2); } } for(unsigned int i = 0; i < part.size(); i++) { if(mark[i]) continue; - if(join) part[i] = centroids[i]/(float)counts[i]; + if(join) { + if(counts[i] < min_size) { + cerr << "Qualche problema serio!\n"; + } else { + part[i] = centroids[i]/(float)counts[i]; + } + } seeds.push_back(part[i]); } @@ -194,7 +227,9 @@ bool VoronoiChain::Optimize(int mean, VoronoiPartition &part, part.push_back(seeds[i]); if(part.size() == 0) part.push_back(Point3f(0,0,0)); + cerr << "Initing!\n"; part.Init(); + cerr << "Inited!\n"; return failed == 0; } @@ -255,6 +290,7 @@ void VoronoiChain::Init(Crude &crude, float scaling, int steps) { } else Optimize(fine_vmean, fine, centroids, counts, true); } + cerr << "Optimized (fine)!\n"; //here goes some optimization pass. //Coarse optimization. //vector radius; @@ -280,10 +316,11 @@ void VoronoiChain::Init(Crude &crude, float scaling, int steps) { } if(step == steps-1) { if(!Optimize(coarse_vmean, coarse, centroids, counts, false)) - step --; + step --; } else Optimize(coarse_vmean, coarse, centroids, counts, true); } + cerr << "Optimized coarse!\n"; } unsigned int VoronoiChain::Locate(unsigned int level, diff --git a/apps/nexus/voronoinxs.cpp b/apps/nexus/voronoinxs.cpp index f42aab25..114aaaec 100644 --- a/apps/nexus/voronoinxs.cpp +++ b/apps/nexus/voronoinxs.cpp @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.20 2004/10/30 20:17:03 ponchio +Fixed big patches problem. + Revision 1.19 2004/10/22 10:37:32 ponchio Split is now in fragment. @@ -91,6 +94,7 @@ First draft. #endif #include +#include using namespace std; #include "crude.h" @@ -485,7 +489,7 @@ void SaveFragment(Nexus &nexus, VoronoiChain &chain, } void ReverseHistory(vector &history) { - reverse(history.begin(), history.end()); + std::reverse(history.begin(), history.end()); vector::iterator i; for(i = history.begin(); i != history.end(); i++) swap((*i).erased, (*i).created);