vcglib/apps/nexus/voronoichain.cpp

528 lines
16 KiB
C++
Raw Normal View History

2004-08-26 20:03:48 +02:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
2004-10-21 14:22:21 +02:00
Revision 1.14 2004/10/19 04:23:29 ponchio
*** empty log message ***
2004-10-19 06:23:29 +02:00
Revision 1.13 2004/10/15 16:45:27 ponchio
Vbo added.
2004-10-15 18:45:27 +02:00
Revision 1.12 2004/10/15 11:41:03 ponchio
Tests and small changes.
2004-10-15 13:41:03 +02:00
Revision 1.11 2004/10/10 17:19:42 ponchio
Added compression and debugged.
2004-10-10 19:19:42 +02:00
Revision 1.10 2004/10/09 14:46:47 ponchio
Windows porting small changes.
2004-10-09 16:46:47 +02:00
Revision 1.9 2004/10/08 15:12:04 ponchio
Working version (maybe)
2004-10-08 17:12:04 +02:00
Revision 1.8 2004/10/04 16:49:54 ponchio
Daily backup. Preparing for compression.
Revision 1.7 2004/10/01 16:54:57 ponchio
Daily backup.
2004-10-01 18:54:57 +02:00
Revision 1.6 2004/09/30 00:27:42 ponchio
Lot of changes. Backup.
2004-09-30 02:27:42 +02:00
Revision 1.5 2004/09/28 10:26:07 ponchio
Voronoi partition changes.
2004-09-28 12:26:07 +02:00
Revision 1.4 2004/09/21 00:53:23 ponchio
Lotsa changes.
2004-09-21 02:53:23 +02:00
Revision 1.3 2004/09/17 15:25:09 ponchio
First working (hopefully) release.
2004-09-17 17:25:59 +02:00
Revision 1.2 2004/09/16 14:25:16 ponchio
Backup. (lot of changes).
2004-09-16 16:25:16 +02:00
Revision 1.1 2004/08/26 18:03:47 ponchio
First draft.
2004-08-26 20:03:48 +02:00
****************************************************************************/
#include <iostream>
#include "voronoichain.h"
2004-10-21 14:22:21 +02:00
#include "watch.h"
2004-08-26 20:03:48 +02:00
using namespace std;
using namespace vcg;
using namespace nxs;
2004-09-21 02:53:23 +02:00
void VoronoiChain::Init(Crude &crude, float scaling, int steps) {
unsigned int f_cells = crude.Faces() / mean_size;
unsigned int c_cells = (unsigned int)(scaling * f_cells);
2004-10-09 16:46:47 +02:00
//cerr << "mean size: " << mean_size << endl;
//cerr << "f cells: " << f_cells << endl;
//cerr << "c_cells: " << c_cells << endl;
2004-08-26 20:03:48 +02:00
levels.push_back(VoronoiPartition());
levels.push_back(VoronoiPartition());
2004-09-21 02:53:23 +02:00
VoronoiPartition &fine = levels[0];
VoronoiPartition &coarse = levels[1];
2004-09-28 12:26:07 +02:00
fine.SetBox(crude.GetBox());
coarse.SetBox(crude.GetBox());
2004-09-21 02:53:23 +02:00
srand(0);
2004-10-09 16:46:47 +02:00
float fine_vmean = mean_size/2.0f;
2004-09-21 02:53:23 +02:00
float coarse_vmean = (mean_size/scaling)/2;
for(unsigned int i = 0; i < crude.Vertices(); i++) {
int f = (int)(fine_vmean*rand()/(RAND_MAX + 1.0));
int c = (int)(coarse_vmean *rand()/(RAND_MAX + 1.0));
if(f == 1) {
Point3f &point = crude.GetVertex(i);
2004-09-28 12:26:07 +02:00
fine.push_back(Seed(point, 1));
2004-09-21 02:53:23 +02:00
}
if(c == 1) {
Point3f &point = crude.GetVertex(i);
2004-09-28 12:26:07 +02:00
coarse.push_back(Seed(point, 1));
2004-09-21 02:53:23 +02:00
}
2004-08-26 20:03:48 +02:00
}
2004-09-28 12:26:07 +02:00
//TODO! Check for duplicates (use the closest :P)
2004-10-09 16:46:47 +02:00
//cerr << "fine_seeds.size: " << fine.size() << endl;
//cerr << "coarse_seeds.size: " << coarse.size() << endl;
2004-09-28 12:26:07 +02:00
fine.Init();
coarse.Init();
2004-08-26 20:03:48 +02:00
2004-09-28 12:26:07 +02:00
//here goes some optimization pass.
//Fine optimization.
2004-10-21 14:22:21 +02:00
Report report;
2004-09-21 02:53:23 +02:00
vector<Point3f> fcentroids;
vector<unsigned int> fcount;
2004-10-09 16:46:47 +02:00
for(int i = 0; i < steps; i++) {
2004-10-21 14:22:21 +02:00
cerr << "Optimization step: " << i+1 << "/" << steps << endl;
2004-09-21 02:53:23 +02:00
fcentroids.clear();
fcount.clear();
fcentroids.resize(fine.size(), Point3f(0, 0, 0));
fcount.resize(fine.size(), 0);
2004-10-21 14:22:21 +02:00
report.Init(crude.Vertices());
2004-09-21 02:53:23 +02:00
for(unsigned int v = 0; v < crude.Vertices(); v++) {
2004-10-21 14:22:21 +02:00
if(v & 0xffff) report.Step(v);
2004-09-21 02:53:23 +02:00
unsigned int ftarget;
float dist = fine.Closest(crude.vert[v], ftarget);
assert(ftarget != -1);
fcentroids[ftarget] += crude.vert[v];
fcount[ftarget]++;
}
for(unsigned int v = 0; v < fine.size(); v++) {
2004-09-30 02:27:42 +02:00
if(fcount[v] == 0) continue;
2004-10-09 16:46:47 +02:00
fine[v].p = fcentroids[v]/(float)fcount[v];
2004-10-15 18:45:27 +02:00
fine[v].weight = (float)pow(fcount[v]/(float)fine_vmean, 0.2f);
2004-09-21 02:53:23 +02:00
}
2004-09-28 12:26:07 +02:00
fine.Init();
}
2004-09-30 02:27:42 +02:00
//remove small or zero patches.
vector<Seed> seeds;
for(unsigned int i = 0; i < fine.size(); i++) {
if(fcount[i] > min_size)
seeds.push_back(fine[i]);
}
2004-10-15 18:45:27 +02:00
fine.clear();
for(unsigned int i = 0; i < seeds.size(); i++)
fine.push_back(seeds[i]);
2004-10-01 18:54:57 +02:00
if(fine.size() == 0) fine.push_back(Point3f(0,0,0));
2004-09-30 02:27:42 +02:00
fine.Init();
2004-09-28 12:26:07 +02:00
//here goes some optimization pass.
//Coarse optimization.
vector<Point3f> ccentroids;
vector<unsigned int> ccount;
2004-10-15 13:41:03 +02:00
vector<float> radius;
2004-10-09 16:46:47 +02:00
for(int i = 0; i < steps; i++) {
2004-10-21 14:22:21 +02:00
cerr << "Optimization step: " << i+1 << "/" << steps << endl;
2004-09-28 12:26:07 +02:00
ccentroids.clear();
ccount.clear();
2004-10-15 13:41:03 +02:00
ccentroids.resize(coarse.size(), Point3f(0, 0, 0));
ccount.resize(coarse.size(), 0);
radius.resize(coarse.size(), 0);
2004-09-28 12:26:07 +02:00
2004-10-21 14:22:21 +02:00
report.Init(crude.Vertices());
2004-09-28 12:26:07 +02:00
for(unsigned int v = 0; v < crude.Vertices(); v++) {
2004-10-21 14:22:21 +02:00
if(v & 0xffff) report.Step(v);
2004-09-28 12:26:07 +02:00
unsigned int ctarget = 0xffffffff;
float dist = coarse.Closest(crude.vert[v], ctarget);
assert(ctarget != 0xffffffff);
ccentroids[ctarget] += crude.vert[v];
ccount[ctarget]++;
2004-10-15 13:41:03 +02:00
if(dist > radius[ctarget]) radius[ctarget] = dist;
2004-09-28 12:26:07 +02:00
}
for(unsigned int v = 0; v < coarse.size(); v++) {
2004-09-30 02:27:42 +02:00
if(ccount[v] == 0) continue;
2004-10-15 13:41:03 +02:00
2004-10-09 16:46:47 +02:00
coarse[v].p = ccentroids[v]/(float)ccount[v];
2004-10-15 18:45:27 +02:00
coarse[v].weight = (float)pow(ccount[v]/(float)coarse_vmean, 0.2f);
2004-10-15 13:41:03 +02:00
if(radius[v] == 0) continue;
//repel from fine seeds
2004-10-15 18:45:27 +02:00
// coarse[v].p = fine.FindBorder(coarse[v].p, radius[v]);
2004-09-28 12:26:07 +02:00
}
coarse.Init();
2004-09-21 02:53:23 +02:00
}
2004-09-28 12:26:07 +02:00
2004-09-30 02:27:42 +02:00
//remove small or zero patches.
seeds.clear();
for(unsigned int i = 0; i < coarse.size(); i++) {
if(ccount[i] > (int)min_size)
seeds.push_back(coarse[i]);
}
2004-10-15 18:45:27 +02:00
coarse.clear();
for(unsigned int i = 0; i < seeds.size(); i++)
coarse.push_back(seeds[i]);
2004-10-01 18:54:57 +02:00
if(coarse.size() == 0) coarse.push_back(Point3f(0,0,0));
2004-09-30 02:27:42 +02:00
coarse.Init();
2004-09-28 12:26:07 +02:00
2004-09-21 02:53:23 +02:00
//Coarse optimization
2004-09-28 12:26:07 +02:00
/* vector< map<unsigned int, Point3f> > ccentroids;
2004-09-21 02:53:23 +02:00
vector< map<unsigned int, unsigned int> > ccount;
for(unsigned int i = 0; i < steps; i++) {
cerr << "Optimization step 1: " << i << "/" << steps << endl;
ccentroids.clear();
ccount.clear();
ccentroids.resize(coarse.size());
ccount.resize(coarse.size());
for(unsigned int v = 0; v < crude.Vertices(); v++) {
unsigned int ftarget;
float dist = fine.Closest(crude.vert[v], ftarget);
assert(ftarget != -1);
unsigned int ctarget;
dist = coarse.Closest(crude.vert[v], ctarget);
assert(ctarget != -1);
map<unsigned int, Point3f> &centroids = ccentroids[ctarget];
map<unsigned int, unsigned int> &count = ccount[ctarget];
if(!centroids.count(ftarget))
centroids[ftarget]= Point3f(0, 0, 0);
if(!count.count(ftarget))
count[ftarget] = 0;
centroids[ftarget] += crude.vert[v];
count[ftarget]++;
}
for(unsigned int v = 0; v < coarse.size(); v++) {
map<unsigned int, Point3f> &centroids = ccentroids[v];
map<unsigned int, unsigned int> &count = ccount[v];
coarse[v].p = Point3f(0, 0, 0);
float weight = 0;
unsigned int tot_size =0;
map<unsigned int, Point3f>::iterator k;
for(k = centroids.begin();k != centroids.end(); k++) {
unsigned int size = count[(*k).first];
tot_size += size;
//coarse[v].p += (*k).second / (size * size);
//weight += 1/(float)size;
coarse[v].p += (*k).second / size;
weight += 1;
// coarse[v].p += (*k).second;
// weight += size;
}
assert(weight > 0);
coarse[v].p /= weight;
//TODO find a solution
// coarse[v].weight = pow(tot_size/coarse_vmean, 0.25f);
}
2004-09-28 12:26:07 +02:00
coarse.Init();
}*/
2004-08-26 20:03:48 +02:00
}
unsigned int VoronoiChain::Locate(unsigned int level,
const vcg::Point3f &p) {
return levels[level].Locate(p);
/* assert(levels.size() > level+1);
unsigned int fine = levels[level].Locate(p);
unsigned int coarse = levels[level+1].Locate(p);
return fine + coarse * levels[level].size();*/
}
//TODO move this to nxsbuild
2004-09-21 02:53:23 +02:00
void VoronoiChain::RemapFaces(Crude &crude, VFile<unsigned int> &face_remap,
vector<unsigned int> &patch_faces,
float scaling, int steps) {
2004-08-26 20:03:48 +02:00
2004-09-21 02:53:23 +02:00
Init(crude, scaling, steps);
2004-08-26 20:03:48 +02:00
//TODO: improve quality of patches and implement threshold.
typedef map<pair<unsigned int, unsigned int>, unsigned int> FragIndex;
// map<pair<unsigned int, unsigned int>, unsigned int> patches;
FragIndex patches;
unsigned int totpatches = 0;
Point3f bari;
for(unsigned int i = 0; i < crude.Faces(); i++) {
bari = crude.GetBari(i);
// unsigned int patch = Locate(0, bari);
unsigned int fine = Locate(0, bari);
unsigned int coarse = Locate(1, bari);
unsigned int patch;
if(!patches.count(make_pair(coarse, fine))) {
patch = totpatches;
patches[make_pair(coarse, fine)] = totpatches++;
} else
patch = patches[make_pair(coarse, fine)];
face_remap[i] = patch;
2004-10-19 06:23:29 +02:00
//face_remap[i] = fine;
2004-08-26 20:03:48 +02:00
if(patch_faces.size() <= patch)
patch_faces.resize(patch+1, 0);
patch_faces[patch]++;
}
//prune faces (now only 0 faces);
unsigned int tot_patches = 0;
vector<int> patch_remap;
for(unsigned int i = 0; i < patch_faces.size(); i++) {
//if below threshold (and can join faces)
if(patch_faces[i] == 0)
patch_remap.push_back(-1);
else
patch_remap.push_back(tot_patches++);
}
//building fragments
FragIndex::iterator f;
for(f = patches.begin(); f != patches.end(); f++) {
unsigned int coarse = (*f).first.first;
unsigned int fine = (*f).first.second;
unsigned int patch = (*f).second;
oldfragments[coarse].insert(patch_remap[patch]);
}
//remapping faces
for(unsigned int i = 0; i < face_remap.Size(); i++) {
unsigned int patch = face_remap[i];
assert(patch != 0xffffffff);
assert(patch_remap[patch] != -1);
face_remap[i] = patch_remap[patch];
}
//remapping patch_faces
for(unsigned int i = 0; i < patch_faces.size(); i++) {
assert(patch_remap[i] <= (int)i);
if(patch_remap[i] != -1) {
assert(patch_faces[i] > 0);
patch_faces[patch_remap[i]] = patch_faces[i];
}
}
patch_faces.resize(tot_patches);
}
2004-09-21 02:53:23 +02:00
void VoronoiChain::BuildLevel(Nexus &nexus, unsigned int offset,
float scaling, int steps) {
unsigned int totface = 0;
2004-09-30 02:27:42 +02:00
unsigned int totvert = 0;
for(unsigned int idx = offset; idx < nexus.index.size(); idx++) {
2004-09-21 02:53:23 +02:00
totface += nexus.index[idx].nface;
2004-09-30 02:27:42 +02:00
totvert += nexus.index[idx].nvert;
}
2004-09-21 02:53:23 +02:00
2004-08-26 20:03:48 +02:00
levels.push_back(VoronoiPartition());
2004-09-21 02:53:23 +02:00
VoronoiPartition &coarse = levels[levels.size()-1];
VoronoiPartition &fine = levels[levels.size()-2];
2004-09-28 12:26:07 +02:00
coarse.SetBox(fine.box);
2004-10-15 13:41:03 +02:00
fine.Init();
2004-09-21 02:53:23 +02:00
2004-09-30 02:27:42 +02:00
unsigned int tot_coarse = (unsigned int)(fine.size() * scaling);
2004-10-21 14:22:21 +02:00
//TODO this method for selecting the seeds is ugly!
2004-09-30 02:27:42 +02:00
float ratio = tot_coarse/(float)(nexus.index.size() - offset);
float cratio = 0;
2004-08-26 20:03:48 +02:00
for(unsigned int idx = offset; idx < nexus.index.size(); idx++) {
2004-09-30 02:27:42 +02:00
cratio += ratio;
if(cratio > 1) {
Patch patch = nexus.GetPatch(idx);
Point3f &v = patch.Vert(0);
coarse.push_back(v);
cratio -= 1;
2004-09-21 02:53:23 +02:00
}
}
2004-09-30 02:27:42 +02:00
2004-09-28 12:26:07 +02:00
if(coarse.size() == 0) {
Patch patch = nexus.GetPatch(0);
coarse.push_back(patch.Vert(0));
}
2004-09-30 02:27:42 +02:00
float coarse_vmean = totvert/(float)coarse.size();
2004-09-28 12:26:07 +02:00
coarse.Init();
cerr << "Coarse size: " << coarse.size() << endl;
2004-09-30 02:27:42 +02:00
cerr << "Coarse mean: " << coarse_vmean << " mean_size: " << mean_size << endl;
2004-10-21 14:22:21 +02:00
Report report;
2004-09-28 12:26:07 +02:00
//here goes some optimization pass.
//Coarse optimization.
vector<Point3f> ccentroids;
vector<unsigned int> ccount;
2004-10-15 13:41:03 +02:00
vector<float> radius;
2004-10-09 16:46:47 +02:00
for(int i = 0; i < steps; i++) {
2004-10-21 14:22:21 +02:00
cerr << "Optimization step: " << i+1 << "/" << steps << endl;
2004-09-28 12:26:07 +02:00
ccentroids.clear();
ccount.clear();
2004-10-15 13:41:03 +02:00
ccentroids.resize(coarse.size(), Point3f(0, 0, 0));
ccount.resize(coarse.size(), 0);
radius.resize(coarse.size(), 0);
2004-10-21 14:22:21 +02:00
report.Init(nexus.index.size());
2004-09-28 12:26:07 +02:00
for(unsigned int idx = offset; idx < nexus.index.size(); idx++) {
2004-10-21 14:22:21 +02:00
report.Step(idx);
2004-09-28 12:26:07 +02:00
Patch patch = nexus.GetPatch(idx);
for(unsigned int i = 0; i < patch.nv; i++) {
unsigned int ctarget = 0xffffffff;
float dist = coarse.Closest(patch.Vert(i), ctarget);
assert(ctarget != 0xffffffff);
ccentroids[ctarget] += patch.Vert(i);
ccount[ctarget]++;
2004-10-15 13:41:03 +02:00
if(dist > radius[ctarget]) radius[ctarget] = dist;
2004-09-28 12:26:07 +02:00
}
}
for(unsigned int v = 0; v < coarse.size(); v++) {
2004-09-30 02:27:42 +02:00
if(ccount[v] == 0) continue;
2004-09-28 12:26:07 +02:00
2004-10-09 16:46:47 +02:00
coarse[v].p = ccentroids[v]/(float)ccount[v];
2004-09-28 12:26:07 +02:00
//0.3 is related to the fact is doubled the box size.
2004-10-15 18:45:27 +02:00
coarse[v].weight = (float)pow(ccount[v]/(float)coarse_vmean, 0.2f);
2004-10-15 13:41:03 +02:00
if(radius[v] == 0) continue;
2004-10-15 18:45:27 +02:00
//coarse[v].p = fine.FindBorder(coarse[v].p, radius[v]);
2004-09-28 12:26:07 +02:00
}
coarse.Init();
}
2004-09-30 02:27:42 +02:00
vector<Seed> seeds;
for(unsigned int i = 0; i < coarse.size(); i++) {
if(ccount[i] > (int)min_size)
seeds.push_back(coarse[i]);
}
2004-10-15 18:45:27 +02:00
coarse.clear();
for(unsigned int i = 0; i < seeds.size(); i++)
coarse.push_back(seeds[i]);
2004-10-01 18:54:57 +02:00
if(coarse.size() == 0) coarse.push_back(Point3f(0,0,0));
2004-09-30 02:27:42 +02:00
coarse.Init();
2004-09-21 02:53:23 +02:00
//Coarse optimization
2004-09-28 12:26:07 +02:00
/* vector< map<unsigned int, Point3f> > ccentroids;
2004-09-21 02:53:23 +02:00
vector< map<unsigned int, unsigned int> > ccount;
for(unsigned int step = 0; step < steps; step++) {
cerr << "Optimization step " << levels.size()-1 << ":"
<< step << "/" << steps << endl;
ccentroids.clear();
ccount.clear();
ccentroids.resize(coarse.size());
ccount.resize(coarse.size());
for(unsigned int idx = offset; idx < nexus.index.size(); idx++) {
Patch patch = nexus.GetPatch(idx);
for(unsigned int i = 0; i < patch.nv; i++) {
Point3f &v = patch.Vert(i);
unsigned int ftarget;
2004-09-28 12:26:07 +02:00
float dist = fine.Closest(v, ftarget);
2004-09-21 02:53:23 +02:00
dist = fine.Closest(v, ftarget);
assert(ftarget != -1);
unsigned int ctarget;
dist = coarse.Closest(v, ctarget);
assert(ctarget != -1);
map<unsigned int, Point3f> &centroids = ccentroids[ctarget];
map<unsigned int, unsigned int> &count = ccount[ctarget];
2004-09-28 12:26:07 +02:00
if(!centroids.count(ftarget))
2004-09-21 02:53:23 +02:00
centroids[ftarget]= Point3f(0, 0, 0);
if(!count.count(ftarget))
count[ftarget] = 0;
centroids[ftarget] += v;
count[ftarget]++;
}
}
2004-08-26 20:03:48 +02:00
2004-09-21 02:53:23 +02:00
cerr << "recentring" << endl;
for(unsigned int v = 0; v < coarse.size(); v++) {
map<unsigned int, Point3f> &centroids = ccentroids[v];
map<unsigned int, unsigned int> &count = ccount[v];
coarse[v].p = Point3f(0, 0, 0);
float weight = 0;
unsigned int tot_size =0;
map<unsigned int, Point3f>::iterator k;
for(k = centroids.begin();k != centroids.end(); k++) {
unsigned int size = count[(*k).first];
tot_size += size;
coarse[v].p += (*k).second / size;
weight += 1;
}
assert(weight > 0);
coarse[v].p /= weight;
//TODO find a solution!
// coarse[v].weight = pow(tot_size/coarse_vmean, 0.25f);
2004-08-26 20:03:48 +02:00
}
2004-09-28 12:26:07 +02:00
coarse.Init();
}*/
2004-09-16 16:25:16 +02:00
newfragments.clear();
2004-08-26 20:03:48 +02:00
//TODO add some optimization
}