2004-03-21 18:51:57 +01: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 $
|
2007-06-08 16:57:29 +02:00
|
|
|
Revision 1.9 2006/07/06 12:40:34 ganovelli
|
|
|
|
typdef ..ScalarType added
|
|
|
|
|
2006-07-06 14:40:34 +02:00
|
|
|
Revision 1.8 2005/02/22 14:18:15 ponchio
|
|
|
|
assert addded.
|
|
|
|
|
2005-02-22 15:18:15 +01:00
|
|
|
Revision 1.7 2005/02/21 17:03:03 ponchio
|
|
|
|
Added Tight creation.
|
|
|
|
|
2005-02-21 18:03:03 +01:00
|
|
|
Revision 1.6 2004/12/01 16:06:59 ponchio
|
|
|
|
Distance
|
|
|
|
|
2004-12-01 17:06:59 +01:00
|
|
|
Revision 1.5 2004/09/29 13:55:33 ponchio
|
|
|
|
Added Distance shpere - point.
|
|
|
|
|
2004-09-29 15:55:33 +02:00
|
|
|
Revision 1.4 2004/04/02 09:49:01 ponchio
|
|
|
|
Ehm... a couople of small errors.
|
|
|
|
|
2004-04-02 11:49:01 +02:00
|
|
|
Revision 1.3 2004/04/02 09:44:13 ponchio
|
|
|
|
Sphere ->Sphere3
|
|
|
|
|
2004-04-02 11:44:13 +02:00
|
|
|
Revision 1.2 2004/03/25 17:25:46 ponchio
|
|
|
|
#include sbagliato.
|
|
|
|
|
2004-03-25 18:25:46 +01:00
|
|
|
Revision 1.1 2004/03/21 17:51:57 ponchio
|
|
|
|
First version.
|
|
|
|
|
2004-03-21 18:51:57 +01:00
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef VCG_SPHERE_H
|
|
|
|
#define VCG_SPHERE_H
|
|
|
|
|
2005-02-22 15:18:15 +01:00
|
|
|
#include <assert.h>
|
2004-03-25 18:25:46 +01:00
|
|
|
#include <vcg/space/point3.h>
|
2008-10-28 16:00:16 +01:00
|
|
|
#include <vector>
|
2004-03-21 18:51:57 +01:00
|
|
|
|
|
|
|
namespace vcg {
|
|
|
|
|
|
|
|
/** \addtogroup space */
|
|
|
|
/*@{*/
|
|
|
|
/**
|
|
|
|
Templated class for 3D sphere.
|
|
|
|
This is the class for definition of a sphere in 3D space. It is stored just as a Point3 and a radius
|
|
|
|
@param T (template parameter) Specifies the type of scalar used to represent coords.
|
|
|
|
Various policy could be added to improve efficience (keeping square of radius for instance).
|
|
|
|
*/
|
|
|
|
|
2004-04-02 11:44:13 +02:00
|
|
|
template <class T> class Sphere3 {
|
2004-03-21 18:51:57 +01:00
|
|
|
protected:
|
|
|
|
Point3<T> _center;
|
2005-02-21 18:03:03 +01:00
|
|
|
T _radius;
|
|
|
|
public:
|
2006-07-06 14:40:34 +02:00
|
|
|
typedef T ScalarType;
|
2005-02-21 18:03:03 +01:00
|
|
|
Sphere3(): _radius(-1) {}
|
2004-04-02 11:49:01 +02:00
|
|
|
Sphere3(const Point3<T> ¢er, T radius): _center(center), _radius(radius) {}
|
2005-02-21 18:03:03 +01:00
|
|
|
|
2004-03-21 18:51:57 +01:00
|
|
|
T &Radius() { return _radius; }
|
|
|
|
const T &Radius() const { return _radius; }
|
|
|
|
Point3<T> &Center() { return _center; }
|
|
|
|
const Point3<T> &Center() const { return _center; }
|
|
|
|
|
|
|
|
bool IsEmpty() const { return _radius < 0; }
|
|
|
|
///return true if @param p - Center() <= Radius()
|
|
|
|
bool IsIn(const Point3<T> &p) const;
|
2010-04-22 21:05:24 +02:00
|
|
|
bool IsIn(const Sphere3<T> &p) const;
|
2004-03-21 18:51:57 +01:00
|
|
|
|
2005-02-21 18:03:03 +01:00
|
|
|
void Add(const Point3<T> &p);
|
|
|
|
void Add(const Sphere3 &sphere);
|
|
|
|
void Intersect(const Sphere3 &sphere);
|
2004-03-21 18:51:57 +01:00
|
|
|
|
2007-06-08 16:57:29 +02:00
|
|
|
|
|
|
|
int CreateFromBox(int n, const Point3<T> *points);
|
2005-02-21 18:03:03 +01:00
|
|
|
//makes 36 iterations over the data... but get good results.
|
2008-08-04 12:50:53 +02:00
|
|
|
int CreateTight(int n, const Point3<T> *points,
|
|
|
|
T threshold = 1.01, T speed = 0.6);
|
2008-10-28 16:00:16 +01:00
|
|
|
int CreateTight(const std::vector<Point3< T> > & points,
|
2005-02-21 18:03:03 +01:00
|
|
|
T threshold = 1.01, T speed = 0.6);
|
2004-03-21 18:51:57 +01:00
|
|
|
};
|
|
|
|
|
2010-10-15 17:17:15 +02:00
|
|
|
//template <class T> T Distance(const Sphere3<T> &sphere,
|
|
|
|
// const Point3<T> &point) {
|
|
|
|
// T dist = Distance(point, sphere.Center()) - sphere.Radius();
|
|
|
|
// if(dist < 0) dist = 0;
|
|
|
|
// return dist;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//template <class T> T Distance(const Sphere3<T> &sphere,
|
|
|
|
// const Sphere3<T> &s) {
|
|
|
|
// T dist = Distance(s.Center(), sphere.Center())
|
|
|
|
// - sphere.Radius() - s.Radius();
|
|
|
|
// if(dist < 0) dist = 0;
|
|
|
|
// return dist;
|
|
|
|
//}
|
2004-09-29 15:55:33 +02:00
|
|
|
|
2004-04-02 11:44:13 +02:00
|
|
|
typedef Sphere3<float> Sphere3f;
|
|
|
|
typedef Sphere3<double> Sphere3d;
|
|
|
|
|
2004-04-02 11:49:01 +02:00
|
|
|
template <class T> void Sphere3<T>::Add(const Sphere3<T> &sphere) {
|
2004-03-21 18:51:57 +01:00
|
|
|
if(IsEmpty()) {
|
|
|
|
*this = sphere;
|
|
|
|
return;
|
|
|
|
}
|
2004-04-02 11:49:01 +02:00
|
|
|
Point3<T> dist = sphere.Center() - _center;
|
2004-03-21 18:51:57 +01:00
|
|
|
float distance = dist.Norm();
|
2010-04-22 21:05:24 +02:00
|
|
|
float fartest = sphere.Radius() + distance;
|
2004-03-21 18:51:57 +01:00
|
|
|
if(fartest <= _radius)
|
|
|
|
return;
|
2010-04-22 21:05:24 +02:00
|
|
|
|
|
|
|
float nearest = sphere.Radius() - distance;
|
|
|
|
if(nearest >= _radius) {
|
|
|
|
*this = sphere;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(distance < 0.001*(_radius + sphere.Radius())) {
|
|
|
|
_radius += distance;
|
|
|
|
return;
|
2004-03-21 18:51:57 +01:00
|
|
|
}
|
2010-04-22 21:05:24 +02:00
|
|
|
|
|
|
|
_center += dist * ((fartest - _radius) / (distance * 2));
|
|
|
|
_radius = (_radius + fartest)/2;
|
2004-03-21 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2005-02-21 18:03:03 +01:00
|
|
|
template <class T> void Sphere3<T>::Add(const Point3<T> &p) {
|
2004-03-21 18:51:57 +01:00
|
|
|
if(IsEmpty()) {
|
|
|
|
_center = p;
|
|
|
|
_radius = 0;
|
|
|
|
}
|
|
|
|
Point3<T> dist = p - _center;
|
|
|
|
float fartest = dist.Norm();
|
|
|
|
if(fartest <= _radius) return;
|
|
|
|
_center += dist * ((fartest - _radius) / (fartest*2));
|
|
|
|
_radius = (_radius + fartest)/2;
|
|
|
|
}
|
|
|
|
|
2004-04-02 11:49:01 +02:00
|
|
|
template <class T> bool Sphere3<T>::IsIn(const Point3<T> &p) const {
|
2005-02-21 18:03:03 +01:00
|
|
|
if(IsEmpty()) return false;
|
|
|
|
Point3<T> dist = p - _center;
|
|
|
|
return dist.SquaredNorm() <= _radius*_radius;
|
2004-03-21 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2010-04-22 21:05:24 +02:00
|
|
|
template <class T> bool Sphere3<T>::IsIn(const Sphere3<T> &p) const {
|
|
|
|
if(IsEmpty()) return false;
|
|
|
|
Point3<T> dist = p.Center() - _center;
|
|
|
|
float distance = dist.Norm();
|
|
|
|
return distance + p.Radius() < _radius;
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:03:03 +01:00
|
|
|
template <class T> void Sphere3<T>::Intersect(const Sphere3<T> &s) {
|
|
|
|
float dist = Distance(_center, s.Center());
|
|
|
|
float r = 0.5 * (_radius + s.Radius() - dist);
|
|
|
|
if(r < 0) {
|
|
|
|
_radius = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_center = (s.Center()*(_radius - r) + _center*(s.Radius() - r))/dist;
|
|
|
|
_radius = r;
|
|
|
|
}
|
|
|
|
|
2007-06-08 16:57:29 +02:00
|
|
|
template <class T> int Sphere3<T>::CreateFromBox(int n, const Point3<T> *points) {
|
2012-06-14 11:40:26 +02:00
|
|
|
Point3f hi(-1e100, -1e100, -1e100);
|
|
|
|
Point3f low(1e100, 1e100, 1e100);
|
2007-06-08 16:57:29 +02:00
|
|
|
for(int i = 0; i < n; i++) {
|
|
|
|
for(int k = 0; k < 3; k++) {
|
2012-06-14 11:40:26 +02:00
|
|
|
if(hi[k] < points[i][k]) hi[k] = points[i][k];
|
|
|
|
if(low[k] > points[i][k]) low[k] = points[i][k];
|
2007-06-08 16:57:29 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-14 11:40:26 +02:00
|
|
|
Center() = (low + hi)/2;
|
|
|
|
Radius() = (low - hi).Norm()/2;
|
2007-06-08 16:57:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-02-21 18:03:03 +01:00
|
|
|
template <class T> int Sphere3<T>::CreateTight(int n, const Point3<T> *points,
|
|
|
|
T threshold, T speed) {
|
|
|
|
//This is quantized gradient descent... really ugly. But simple :P
|
|
|
|
//TODO step should adapt to terrain...
|
|
|
|
for(int i = 0; i < n; i++)
|
|
|
|
Add(points[i]);
|
|
|
|
Radius() *= 1.0001;
|
|
|
|
|
|
|
|
Point3<T> center;
|
|
|
|
//Test with 6 directions
|
|
|
|
|
|
|
|
Point3f pert[6];
|
|
|
|
T step = Radius()/8;
|
|
|
|
int count = 0;
|
|
|
|
while(1) {
|
|
|
|
count++;
|
|
|
|
T radius = Radius();
|
|
|
|
pert[0] = Point3f(step, 0, 0);
|
|
|
|
pert[1] = -pert[0];
|
|
|
|
pert[2] = Point3f(0, step, 0);
|
|
|
|
pert[3] = -pert[2];
|
|
|
|
pert[4] = Point3f(0, 0, step);
|
|
|
|
pert[5] = -pert[4];
|
|
|
|
|
2010-04-22 21:05:24 +02:00
|
|
|
int best = 6;
|
2005-02-21 18:03:03 +01:00
|
|
|
T best_radius = Radius()/threshold;
|
|
|
|
|
2010-04-22 21:05:24 +02:00
|
|
|
for(int k = 0; k < 6; k++) {
|
2005-02-21 18:03:03 +01:00
|
|
|
center = Center() + pert[k];
|
|
|
|
radius = 0;
|
2010-04-22 21:05:24 +02:00
|
|
|
for(int i = 0; i < n; i++) {
|
|
|
|
float r = Distance(center, points[i]);
|
|
|
|
if(r > radius)
|
|
|
|
radius = r;
|
2005-02-21 18:03:03 +01:00
|
|
|
}
|
|
|
|
if(radius < best_radius) {
|
2010-04-22 21:05:24 +02:00
|
|
|
best = k;
|
|
|
|
best_radius = radius;
|
2005-02-21 18:03:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(best != 6) {
|
|
|
|
Center() = Center() + pert[best];
|
|
|
|
Radius() = best_radius;
|
|
|
|
}
|
|
|
|
step *= speed;
|
2008-08-04 12:50:53 +02:00
|
|
|
if(step <= Radius() * (threshold - 1))
|
2005-02-21 18:03:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Radius() *= 1.01;
|
2005-02-22 15:18:15 +01:00
|
|
|
|
|
|
|
//Test we did it correctly.
|
|
|
|
for(int i = 0; i < n; i++)
|
|
|
|
assert(IsIn(points[i]));
|
|
|
|
|
2005-02-21 18:03:03 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-08-04 12:50:53 +02:00
|
|
|
template <class T> int Sphere3<T>::CreateTight(const std::vector<Point3<T> > & points,
|
|
|
|
T threshold, T speed){
|
|
|
|
return (points.empty())? -1 :CreateTight(points.size(),&(*points.begin()),threshold,speed);
|
|
|
|
}
|
|
|
|
|
2004-03-21 18:51:57 +01:00
|
|
|
} //namespace
|
|
|
|
|
|
|
|
|
2004-04-02 11:44:13 +02:00
|
|
|
#endif
|