This commit is contained in:
Federico Ponchio 2004-06-24 14:19:20 +00:00
parent 298d7c14c6
commit ef01c54189
3 changed files with 91 additions and 3 deletions

View File

@ -24,19 +24,27 @@
History
$Log: not supported by cvs2svn $
Revision 1.1 2004/06/23 17:17:46 ponchio
Created
****************************************************************************/
#ifndef NXS_PCHAIN_H
#define NXS_PCHAIN_H
#include <stdio.h>
#include <vcg/space/point3.h>
/** Partition must be a class with a Key type, with
Levels, Locate, Priority, Save(FILE *), Load(FILE *)
as in pvoronoi.h */
namespace {
template <class Partition> class PChain {
public:
typedef typename Partition::Key Key;
std::vector<Partition> levels;
unsigned int Levels() {
return levels.size();
@ -51,8 +59,29 @@ template <class Partition> class PChain {
assert(level < levels.size());
return levels[level].Priority(level, p, key);
}
private:
std::vector<Partition> levels;
bool Save(const std::string &file) {
FILE *fp = fopen(file.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.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;
}
};
}//namespace

View File

@ -24,11 +24,17 @@
History
$Log: not supported by cvs2svn $
Revision 1.1 2004/06/23 17:17:46 ponchio
Created
****************************************************************************/
#pragma warning(disable:4786 4804 4244 4018 4267 4311)
#include <stdio.h>
#include <iostream>
#include "pvoronoi.h"
using namespace std;
using namespace vcg;
using namespace nxs;
@ -119,6 +125,11 @@ unsigned int VoronoiPartition::count(Key key) {
return key > 0 && key < size();
}
Seed &VoronoiPartition::operator[](Key key) {
assert(key < all_seeds.size());
return all_seeds[key];
}
VoronoiPartition::Key VoronoiPartition::Locate(const vcg::Point3f &p) {
int target;
Closest(p, target);
@ -131,3 +142,41 @@ float VoronoiPartition::Priority(const vcg::Point3f &p, Key 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();
}

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.1 2004/06/23 17:17:46 ponchio
Created
****************************************************************************/
@ -32,6 +35,8 @@ $Log: not supported by cvs2svn $
#include <vector>
#include <set>
#include <string>
#include <stdio.h>
#include <vcg/space/point3.h>
#include <vcg/space/box3.h>
@ -60,7 +65,7 @@ namespace nxs {
return weight * weight *vcg::SquaredDistance(p,q);
}
};
class VoronoiPartition {
public:
@ -87,10 +92,15 @@ namespace nxs {
iterator end();
int size();
unsigned int count(Key key);
Seed &operator[](Key key);
void clear();
Key Locate(const vcg::Point3f &p);
float Priority(const vcg::Point3f &p, Key key);
bool Save(const std::string &file);
bool Load(const std::string &file);
unsigned int Save(FILE *fp);
unsigned int Load(FILE *fp);
private:
vcg::Box3f bbox;
vcg::GridStaticPtr< std::vector<Seed> > ug;