From 2138bdd2f9cde61f6af3625ccb913ab1e9634ac4 Mon Sep 17 00:00:00 2001 From: ponchio Date: Wed, 23 Jun 2004 17:17:46 +0000 Subject: [PATCH] Created --- wrap/nexus/pchain.h | 60 ++++++++++++++++++ wrap/nexus/pvoronoi.cpp | 133 ++++++++++++++++++++++++++++++++++++++++ wrap/nexus/pvoronoi.h | 104 +++++++++++++++++++++++++++++++ 3 files changed, 297 insertions(+) create mode 100644 wrap/nexus/pchain.h create mode 100644 wrap/nexus/pvoronoi.cpp create mode 100644 wrap/nexus/pvoronoi.h diff --git a/wrap/nexus/pchain.h b/wrap/nexus/pchain.h new file mode 100644 index 00000000..c139170f --- /dev/null +++ b/wrap/nexus/pchain.h @@ -0,0 +1,60 @@ +/**************************************************************************** +* 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 $ + +****************************************************************************/ + +#ifndef NXS_PCHAIN_H +#define NXS_PCHAIN_H + +#include + +namespace { + +template class PChain { + public: + typedef typename Partition::Key Key; + + unsigned int Levels() { + return levels.size(); + } + + Key Locate(unsigned int level, const vcg::Point3f &p) { + assert(level < levels.size()); + return levels[level].Locate(p); + } + + float Priority(unsigned int level, const vcg::Point3f &p, Key key) { + assert(level < levels.size()); + return levels[level].Priority(level, p, key); + } + private: + std::vector levels; +}; + +}//namespace + +#endif diff --git a/wrap/nexus/pvoronoi.cpp b/wrap/nexus/pvoronoi.cpp new file mode 100644 index 00000000..cc9abdef --- /dev/null +++ b/wrap/nexus/pvoronoi.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** +* 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 $ + +****************************************************************************/ + +#pragma warning(disable:4786 4804 4244 4018 4267 4311) +#include "pvoronoi.h" +using namespace std; +using namespace vcg; +using namespace nxs; + +bool Seed::Dist(const Point3f &point, float &mindist, + Point3f &res) { + float newdist = Distance(p, point); + if(newdist < mindist) { + mindist = newdist; + res = p; + return true; + } else + return false; +} + +int VoronoiPartition::Add(vcg::Point3f p, float weight) { + Seed ns(p,weight); + all_seeds.push_back(ns); + seedBuf.push_back(p); + + if(seedBuf.size() >= MAX_BUF) { + for(unsigned int i = 0; i < seedBuf.size(); ++i) + ug_seeds.push_back(seedBuf[i]); + seedBuf.clear(); + ug.Set(ug_seeds); + } + return size(); +} + +float VoronoiPartition::Closest(const vcg::Point3f &p, int &target, float radius) { + Point3f res; + float mindist = 1e20; + target = -1; + + if(ug_seeds.size()) { + Seed *nsp = ug.GetClosest(p, mindist, res); + if(nsp) { + target = nsp-&*ug_seeds.begin(); + } + } + + for(unsigned int i=0;i dist) { + target=ug_seeds.size()+i; + mindist=dist; + } + } + + //assert(target >=0 ); + //assert (target < size()+seedBuf.size()); + return mindist; +} + + +void VoronoiPartition::iterator::operator++() { + ++seed; +} +const VoronoiPartition::Key VoronoiPartition::iterator::operator*() { + return seed; +} +bool VoronoiPartition::iterator::operator==(const VoronoiPartition::iterator &key) { + return key.seed == seed; +} +bool VoronoiPartition::iterator::operator!=(const VoronoiPartition::iterator &key) { + return key.seed != seed; +} + +VoronoiPartition::iterator VoronoiPartition::begin() { + iterator i; + i.seed = 0; + return i; +} +VoronoiPartition::iterator VoronoiPartition::end() { + iterator i; + i.seed = size(); + return i; +} +int VoronoiPartition::size() { + return all_seeds.size(); +} +void VoronoiPartition::clear() { + all_seeds.clear(); + ug_seeds.clear(); + seedBuf.clear(); +} +unsigned int VoronoiPartition::count(Key key) { + return key > 0 && key < size(); +} + +VoronoiPartition::Key VoronoiPartition::Locate(const vcg::Point3f &p) { + int target; + Closest(p, target); + assert(target != -1); + return target; +} + +float VoronoiPartition::Priority(const vcg::Point3f &p, Key key) { + Seed &seed = all_seeds[key]; + return seed.Dist(p); +} + diff --git a/wrap/nexus/pvoronoi.h b/wrap/nexus/pvoronoi.h new file mode 100644 index 00000000..0629fd21 --- /dev/null +++ b/wrap/nexus/pvoronoi.h @@ -0,0 +1,104 @@ +/**************************************************************************** +* 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 $ + +****************************************************************************/ + +#ifndef NXS_NET_GRID_H +#define NXS_NET_GRID_H + +#include +#include + +#include +#include +#include + +namespace nxs { + + class Seed { + public: + vcg::Point3f p; + float weight; + typedef float ScalarType; + bool Dist(const vcg::Point3f & point, float &mindist, vcg::Point3f &res); + void GetBBox(vcg::Box3f &b) {b.Set(p);} + bool IsD() { return false; } + + Seed(): weight(1) {} + Seed(const vcg::Point3f &point): p(point), weight(1) {} + Seed(const vcg::Point3f &point, const float w): + p(point), weight(w) {} + + inline float Dist(const vcg::Point3f &q) const { + return weight * vcg::Distance(p,q); + } + inline float SquaredDist(const vcg::Point3f &q) const { + return weight * weight *vcg::SquaredDistance(p,q); + } + }; + + + class VoronoiPartition { + public: + enum { MAX_BUF=25 }; + typedef int Key; + + VoronoiPartition() {} + + void Init(vcg::Box3f &bb) { bbox=bb; ug.SetBBox(bb); } + int Add(vcg::Point3f p, float weight = 1); + float Closest(const vcg::Point3f &p, Key &target, float radius = 0); + + class iterator { + public: + void operator++(); + const Key operator*(); + bool operator==(const iterator &key); + bool operator!=(const iterator &key); + private: + int seed; + friend class VoronoiPartition; + }; + iterator begin(); + iterator end(); + int size(); + unsigned int count(Key key); + void clear(); + Key Locate(const vcg::Point3f &p); + float Priority(const vcg::Point3f &p, Key key); + + private: + vcg::Box3f bbox; + vcg::GridStaticPtr< std::vector > ug; + std::vector all_seeds; + std::vector ug_seeds; + std::vector seedBuf; + }; + +} +#endif +