Trying to fix big patches.

This commit is contained in:
Federico Ponchio 2004-11-03 16:31:38 +00:00
parent 2a8d132abd
commit f9dd9b0416
4 changed files with 104 additions and 44 deletions

View File

@ -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 <stdio.h>
#include <iostream>
#include "pvoronoi.h"
#include <ANN/ANN.h>
#include <iostream>
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<int> &near,
vector<int> &nears,
vector<float> &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<double> 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];

View File

@ -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<int> &near,
std::vector<int> &nears,
std::vector<float> &dist);
void Closest(const vcg::Point3f &p,
int &target, float &dist);

View File

@ -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<int> near;
vector<float> 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<int> nears;
vector<float> 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<bool> &mark,
vector<unsigned int> &counts) {
vector<int> near;
vector<int> nears;
vector<float> 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<float> 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,

View File

@ -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 <iostream>
#include <algorithm>
using namespace std;
#include "crude.h"
@ -485,7 +489,7 @@ void SaveFragment(Nexus &nexus, VoronoiChain &chain,
}
void ReverseHistory(vector<Nexus::Update> &history) {
reverse(history.begin(), history.end());
std::reverse(history.begin(), history.end());
vector<Nexus::Update>::iterator i;
for(i = history.begin(); i != history.end(); i++)
swap((*i).erased, (*i).created);