/**************************************************************************** * 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 $ Revision 1.4 2004/04/02 09:49:01 ponchio Ehm... a couople of small errors. Revision 1.3 2004/04/02 09:44:13 ponchio Sphere ->Sphere3 Revision 1.2 2004/03/25 17:25:46 ponchio #include sbagliato. Revision 1.1 2004/03/21 17:51:57 ponchio First version. ****************************************************************************/ #ifndef VCG_SPHERE_H #define VCG_SPHERE_H #include 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). */ template class Sphere3 { protected: Point3 _center; T _radius; public: Sphere3(): _radius(-1) {} Sphere3(const Point3 ¢er, T radius): _center(center), _radius(radius) {} T &Radius() { return _radius; } const T &Radius() const { return _radius; } Point3 &Center() { return _center; } const Point3 &Center() const { return _center; } bool IsEmpty() const { return _radius < 0; } ///return true if @param p - Center() <= Radius() bool IsIn(const Point3 &p) const; void Add(Point3 &p); void Add(const Sphere3 &sphere); }; template T Distance(const Sphere3 &sphere, const Point3 &point) { T dist = Distance(point, sphere.Center()) - sphere.Radius(); if(dist < 0) dist = 0; return dist; } typedef Sphere3 Sphere3f; typedef Sphere3 Sphere3d; template void Sphere3::Add(const Sphere3 &sphere) { if(IsEmpty()) { *this = sphere; return; } Point3 dist = sphere.Center() - _center; 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; } } template void Sphere3::Add(Point3 &p) { if(IsEmpty()) { _center = p; _radius = 0; } Point3 dist = p - _center; float fartest = dist.Norm(); if(fartest <= _radius) return; _center += dist * ((fartest - _radius) / (fartest*2)); _radius = (_radius + fartest)/2; } template bool Sphere3::IsIn(const Point3 &p) const { Point3 dist = p - _center; return dist.Norm() <= _radius; } } //namespace #endif