00001 #pragma once 00002 /**************************************************************************** 00003 * VCGLib o o * 00004 * Visual and Computer Graphics Library o o * 00005 * _ O _ * 00006 * Copyright(C) 2004 \/)\/ * 00007 * Visual Computing Lab /\/| * 00008 * ISTI - Italian National Research Council | * 00009 * \ * 00010 * All rights reserved. * 00011 * * 00012 * This program is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU General Public License as published by * 00014 * the Free Software Foundation; either version 2 of the License, or * 00015 * (at your option) any later version. * 00016 * * 00017 * This program is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * 00021 * for more details. * 00022 * * 00023 ****************************************************************************/ 00024 /**************************************************************************** 00025 History 00026 00027 $Log: not supported by cvs2svn $ 00028 Revision 1.5 2007/11/07 09:37:20 ganovelli 00029 added draft for sphereofsphres enclosing 00030 00031 Revision 1.4 2006/09/14 08:46:00 ganovelli 00032 added inclusion of sphere3 00033 00034 Revision 1.3 2006/07/12 12:13:23 zifnab1974 00035 static keyword only in declaration not in implementation 00036 00037 Revision 1.2 2006/07/10 10:38:16 turini 00038 minor changes in SphereOfTetra() 00039 00040 Revision 1.1 2006/07/06 12:37:18 ganovelli 00041 draft version. For the triangle is not tehe smallest enclosing sphere and for the set of spheres works only for two spheres 00042 00043 00044 00045 ****************************************************************************/ 00046 00047 #include <vcg/space/triangle3.h> 00048 #include <vcg/space/tetra3.h> 00049 #include <vcg/space/sphere3.h> 00050 #include <assert.h> 00051 namespace vcg{ 00058 struct SmallestEnclosing { 00059 00061 template <class TriangleType> 00062 static Sphere3<typename TriangleType::ScalarType> SphereOfTriangle(const TriangleType & t); 00063 00065 template <class TetraType> 00066 static Sphere3<typename TetraType::ScalarType> SphereOfTetra(const TetraType & t); 00067 00069 template <class SphereType> 00070 static SphereType SphereOfSpheres( const SphereType & s0,const SphereType & s1); 00071 00073 template <class SphereContType> 00074 static typename SphereContType::value_type SphereOfSpheres( const SphereContType & t); 00075 }; 00078 template <class TriangleType> 00079 Sphere3<typename TriangleType::ScalarType> 00080 SmallestEnclosing::SphereOfTriangle(const TriangleType & t){ 00081 return Sphere3<typename TriangleType::ScalarType>(t.Barycenter(),(t.Barycenter()-t.cP(0)).Norm() ); 00082 } 00083 00084 template <class TetraType> 00085 Sphere3<typename TetraType::ScalarType> 00086 SmallestEnclosing::SphereOfTetra(const TetraType & t){ 00087 return Sphere3<typename TetraType::ScalarType>( t.Barycenter(),( t.Barycenter() - t.cP(0) ).Norm() ); 00088 } 00089 00090 template <class SphereType> 00091 SphereType 00092 SmallestEnclosing:: 00093 SphereOfSpheres( const SphereType & s0, const SphereType & s1) 00094 { 00095 typename SphereType::ScalarType radius; 00096 vcg::Point3f center; 00097 00098 if(s0.Radius()==-1.0) return s1; else if(s1.Radius()==-1.0) return s0; 00099 00100 float dst = (s1.Center()-s0.Center()).Norm() ; 00101 radius = (dst+s1.Radius()+s0.Radius())/2; 00102 Point3f a=s0.Center(); 00103 Point3f b=s1.Center(); 00104 Point3f dir = (b-a).Normalize(); 00105 a = a - dir*s0.Radius(); 00106 b = b + dir*s1.Radius(); 00107 center = (a+b)/2.0; 00108 00109 return SphereType(center,radius); 00110 } 00111 00112 template <typename SphereContType> 00113 typename SphereContType::value_type 00114 SmallestEnclosing:: 00115 SphereOfSpheres( const SphereContType & spheres) 00116 { 00117 typename SphereContType::value_type::ScalarType radius; 00118 typename SphereContType::value_type res; 00119 typename SphereContType::const_iterator si; 00120 00121 for(si = spheres.begin(); si != spheres.end(); ++si){ 00122 res = SphereOfSpheres(res,*si); 00123 } 00124 00125 return res; 00126 } 00127 } 00128 00129