Simbody  3.4 (development)
Geo_Sphere.h
Go to the documentation of this file.
00001 #ifndef SimTK_SIMMATH_GEO_SPHERE_H_
00002 #define SimTK_SIMMATH_GEO_SPHERE_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                        Simbody(tm): SimTKmath                              *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK biosimulation toolkit originating from           *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody.  *
00011  *                                                                            *
00012  * Portions copyright (c) 2011-12 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
00017  * not use this file except in compliance with the License. You may obtain a  *
00018  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
00019  *                                                                            *
00020  * Unless required by applicable law or agreed to in writing, software        *
00021  * distributed under the License is distributed on an "AS IS" BASIS,          *
00022  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
00023  * See the License for the specific language governing permissions and        *
00024  * limitations under the License.                                             *
00025  * -------------------------------------------------------------------------- */
00026 
00030 #include "SimTKcommon.h"
00031 #include "simmath/internal/common.h"
00032 #include "simmath/internal/Geo.h"
00033 
00034 #include <cassert>
00035 #include <cmath>
00036 #include <algorithm>
00037 
00038 namespace SimTK {
00039 
00040 
00041 //==============================================================================
00042 //                              GEO SPHERE
00043 //==============================================================================
00046 template <class P>
00047 class Geo::Sphere_ {
00048 typedef P           RealP;
00049 typedef Vec<3,P>    Vec3P;
00050 typedef Vec<4,P>    Vec4P;
00051 public:
00054 Sphere_() {}
00056 Sphere_(const Vec3P& center, RealP radius)
00057 :   cr(center[0], center[1], center[2], radius) {assert(radius>=0);}
00059 Sphere_& setRadius(RealP radius) 
00060 {   assert(radius>=0); cr[3]=radius; return *this; }
00062 Sphere_& setCenter(const Vec3P& center) 
00063 {   Vec3P::updAs(&cr[0])=center; return *this; }
00064 
00068 Sphere_& scaleBy(RealP f)
00069 {   setRadius(f*getRadius()); return *this; }
00070 
00078 Sphere_& stretchBoundary() {
00079     const RealP tol = Geo::getDefaultTol<P>();
00080     const RealP maxdim = max(getCenter().abs());
00081     const RealP scale = std::max(maxdim, getRadius());
00082     updRadius() += std::max(scale*Geo::getEps<P>(), tol);
00083     return *this; 
00084 }
00085 
00087 RealP findVolume() const 
00088 {   return (RealP(4)/3) * NTraits<P>::getPi() * cube(getRadius()); }
00090 RealP findArea() const 
00091 {   return 4 * NTraits<P>::getPi() * square(getRadius()); }
00092 
00095 bool isPointOutside(const Vec3P& p) const {
00096     const RealP r2 = Geo::Point_<P>::findDistanceSqr(p, getCenter());
00097     return r2 > square(getRadius());
00098 }
00101 bool isPointOutside(const Vec3P& p, RealP tol) const {
00102     assert(tol >= 0);
00103     const RealP r2 = Geo::Point_<P>::findDistanceSqr(p, getCenter());
00104     return r2 > square(getRadius()+tol);
00105 }
00107 const Vec3P& getCenter() const {return Vec3P::getAs(&cr[0]);}
00109 Vec3P& updCenter() {return Vec3P::updAs(&cr[0]);}
00111 RealP getRadius() const {return cr[3];}
00113 RealP& updRadius() {return cr[3];}
00114 
00115 
00116 private:
00117 // Store together to make sure the compiler doesn't introduce any padding.
00118 // cr[0..2] is the center point, cr[3] is the radius
00119 Vec4P   cr;    
00120 };
00121 
00122 
00123 } // namespace SimTK
00124 
00125 #endif // SimTK_SIMMATH_GEO_SPHERE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines