vcglib/vcg/space/sphere3.h

120 lines
4.0 KiB
C
Raw Normal View History

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 $
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
2004-03-25 18:25:46 +01:00
#include <vcg/space/point3.h>
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;
T _radius;
public:
2004-04-02 11:49:01 +02:00
Sphere3(): _radius(-1) {}
Sphere3(const Point3<T> &center, T radius): _center(center), _radius(radius) {}
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;
void Add(Point3<T> &p);
2004-04-02 11:44:13 +02:00
void Add(const Sphere3 &sphere);
2004-03-21 18:51:57 +01: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();
float fartest = distance + sphere.Radius();
if(fartest <= _radius)
return;
if(distance == 0)
_radius = sphere.Radius();
else {
_center += dist * ((fartest - _radius) / (dist.Norm() * 2));
_radius = (_radius + fartest)/2;
}
}
2004-04-02 11:49:01 +02:00
template <class T> void Sphere3<T>::Add(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 {
2004-03-21 18:51:57 +01:00
Point3<T> dist = p - _center;
return dist.Norm() <= _radius;
}
} //namespace
2004-04-02 11:44:13 +02:00
#endif