This commit is contained in:
Federico Ponchio 2004-08-27 00:39:28 +00:00
parent 9f065c7a66
commit ac705ccbbf
3 changed files with 96 additions and 63 deletions

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.3 2004/07/20 14:06:02 ponchio
Changed filename saving.
Revision 1.2 2004/06/25 16:47:13 ponchio
Various debug
@ -42,56 +45,30 @@ Created
#ifndef NXS_PCHAIN_H
#define NXS_PCHAIN_H
#include <stdio.h>
#include <vector>
#include <vcg/space/point3.h>
#include "crude.h"
/** Partition must be a class with a Key type, with
Levels, Locate, Priority, Save(FILE *), Load(FILE *)
as in pvoronoi.h */
namespace nxs {
template <class Partition> class PChain {
class PChain {
public:
typedef typename Partition::Key Key;
std::vector<Partition> levels;
unsigned int Levels() {
return levels.size();
}
// virtual void Init(Crude &crude, unsigned int max_level) = 0;
Key Locate(unsigned int level, const vcg::Point3f &p) {
assert(level < levels.size());
return levels[level].Locate(p);
}
/* Return an unique uint for couple of patch level, level+1 */
virtual unsigned int Locate(unsigned int level, const vcg::Point3f &p) = 0;
float Priority(unsigned int level, const vcg::Point3f &p, Key key) {
assert(level < levels.size());
return levels[level].Priority(level, p, key);
}
bool Save(const std::string &file) {
FILE *fp = fopen((file + ".chn").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 + ".chn").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;
}
virtual void RemapFaces(Crude &crude, VFile<unsigned int> &face_remap,
std::vector<unsigned int> &patch_faces) = 0;
/* virtual unsigned int Levels() = 0;
virtual unsigned int Locate(unsigned int level,
const vcg::Point3f &p) = 0;
virtual float Priority(unsigned int level,
const vcg::Point3f &p, unsigned int key) = 0;*/
};
}//namespace

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.2 2004/07/01 21:35:34 ponchio
int -> Key
Revision 1.1 2004/06/24 14:32:45 ponchio
Moved from wrap/nexus
@ -56,7 +59,7 @@ bool Seed::Dist(const Point3f &point, float &mindist,
return false;
}
VoronoiPartition::Key VoronoiPartition::Add(const vcg::Point3f &p,
unsigned int VoronoiPartition::Add(const vcg::Point3f &p,
float weight) {
Seed ns(p,weight);
all_seeds.push_back(ns);
@ -68,14 +71,14 @@ VoronoiPartition::Key VoronoiPartition::Add(const vcg::Point3f &p,
seedBuf.clear();
ug.Set(ug_seeds);
}
return (Key)(size());
return size();
}
float VoronoiPartition::Closest(const vcg::Point3f &p,
VoronoiPartition::Key &target, float radius) {
unsigned int &target, float radius) {
Point3f res;
float mindist = 1e20;
target = -1;
target = 0xffffffff;
if(ug_seeds.size()) {
Seed *nsp = ug.GetClosest(p, mindist, res);
@ -101,7 +104,7 @@ float VoronoiPartition::Closest(const vcg::Point3f &p,
void VoronoiPartition::iterator::operator++() {
++seed;
}
const VoronoiPartition::Key VoronoiPartition::iterator::operator*() {
const unsigned int VoronoiPartition::iterator::operator*() {
return seed;
}
bool VoronoiPartition::iterator::operator==(const VoronoiPartition::iterator &key) {
@ -129,23 +132,23 @@ void VoronoiPartition::clear() {
ug_seeds.clear();
seedBuf.clear();
}
unsigned int VoronoiPartition::count(Key key) {
return key > 0 && key < size();
unsigned int VoronoiPartition::count(unsigned int key) {
return key > 0 && key < (unsigned int)size();
}
Seed &VoronoiPartition::operator[](Key key) {
assert((unsigned int)key < all_seeds.size());
Seed &VoronoiPartition::operator[](unsigned int key) {
assert(key < all_seeds.size());
return all_seeds[key];
}
VoronoiPartition::Key VoronoiPartition::Locate(const vcg::Point3f &p) {
int target;
unsigned int VoronoiPartition::Locate(const vcg::Point3f &p) {
unsigned int target;
Closest(p, target);
assert(target != -1);
assert(target != 0xffffffff);
return target;
}
float VoronoiPartition::Priority(const vcg::Point3f &p, Key key) {
float VoronoiPartition::Priority(const vcg::Point3f &p, unsigned int key) {
Seed &seed = all_seeds[key];
return seed.Dist(p);
}
@ -188,3 +191,49 @@ unsigned int VoronoiPartition::Load(FILE *fp) {
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<vcg::Point3f> 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<unsigned int> 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;
}

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.4 2004/07/20 14:17:51 ponchio
*** empty log message ***
Revision 1.3 2004/07/01 21:34:59 ponchio
int -> Key
@ -54,6 +57,8 @@ Created
#include <vcg/space/box3.h>
#include <vcg/space/index/grid_static_ptr.h>
#include "crude.h"
//TODO provide a Sort function, to sort spatially the seeds.
namespace nxs {
@ -84,32 +89,32 @@ namespace nxs {
class VoronoiPartition {
public:
enum { MAX_BUF=25 };
typedef int Key;
VoronoiPartition() {}
void Init(vcg::Box3f &bb) { bbox=bb; ug.SetBBox(bb); }
Key Add(const vcg::Point3f &p, float weight = 1);
float Closest(const vcg::Point3f &p, Key &target, float radius = 0);
unsigned int Add(const vcg::Point3f &p, float weight = 1);
float Closest(const vcg::Point3f &p,
unsigned int &target, float radius = 0);
class iterator {
public:
void operator++();
const Key operator*();
const unsigned int operator*();
bool operator==(const iterator &key);
bool operator!=(const iterator &key);
private:
int seed;
unsigned int seed;
friend class VoronoiPartition;
};
iterator begin();
iterator end();
int size();
unsigned int count(Key key);
Seed &operator[](Key key);
unsigned int count(unsigned int key);
Seed &operator[](unsigned int key);
void clear();
Key Locate(const vcg::Point3f &p);
float Priority(const vcg::Point3f &p, Key key);
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);
@ -120,6 +125,8 @@ namespace nxs {
to estimate optimal radius.
At the moment strategy is to campion randomly the file.
*/
static float OptimalRadius(Crude &crude, unsigned int target);
template <class T>
static std::vector<float> OptimalRadii(unsigned int total,
T begin, T end,