Simbody  3.7
Geo_Sphere.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATH_GEO_SPHERE_H_
2 #define SimTK_SIMMATH_GEO_SPHERE_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKmath *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2011-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
30 #include "SimTKcommon.h"
32 #include "simmath/internal/Geo.h"
33 
34 #include <cassert>
35 #include <cmath>
36 #include <algorithm>
37 
38 namespace SimTK {
39 
40 
41 //==============================================================================
42 // GEO SPHERE
43 //==============================================================================
46 template <class P>
47 class Geo::Sphere_ {
48 typedef P RealP;
49 typedef Vec<3,P> Vec3P;
50 typedef Vec<4,P> Vec4P;
51 public:
54 Sphere_() {}
56 Sphere_(const Vec3P& center, RealP radius)
57 : cr(center[0], center[1], center[2], radius) {assert(radius>=0);}
59 Sphere_& setRadius(RealP radius)
60 { assert(radius>=0); cr[3]=radius; return *this; }
62 Sphere_& setCenter(const Vec3P& center)
63 { Vec3P::updAs(&cr[0])=center; return *this; }
64 
68 Sphere_& scaleBy(RealP f)
69 { setRadius(f*getRadius()); return *this; }
70 
79  const RealP tol = Geo::getDefaultTol<P>();
80  const RealP maxdim = max(getCenter().abs());
81  const RealP scale = std::max(maxdim, getRadius());
82  updRadius() += std::max(scale*Geo::getEps<P>(), tol);
83  return *this;
84 }
85 
87 RealP findVolume() const
88 { return (RealP(4)/3) * NTraits<P>::getPi() * cube(getRadius()); }
90 RealP findArea() const
91 { return 4 * NTraits<P>::getPi() * square(getRadius()); }
92 
95 bool isPointOutside(const Vec3P& p) const {
96  const RealP r2 = Geo::Point_<P>::findDistanceSqr(p, getCenter());
97  return r2 > square(getRadius());
98 }
101 bool isPointOutside(const Vec3P& p, RealP tol) const {
102  assert(tol >= 0);
103  const RealP r2 = Geo::Point_<P>::findDistanceSqr(p, getCenter());
104  return r2 > square(getRadius()+tol);
105 }
107 const Vec3P& getCenter() const {return Vec3P::getAs(&cr[0]);}
109 Vec3P& updCenter() {return Vec3P::updAs(&cr[0]);}
111 RealP getRadius() const {return cr[3];}
113 RealP& updRadius() {return cr[3];}
114 
115 
116 private:
117 // Store together to make sure the compiler doesn't introduce any padding.
118 // cr[0..2] is the center point, cr[3] is the radius
119 Vec4P cr;
120 };
121 
122 
123 } // namespace SimTK
124 
125 #endif // SimTK_SIMMATH_GEO_SPHERE_H_
RealP & updRadius()
Get a writable reference to the sphere&#39;s radius.
Definition: Geo_Sphere.h:113
Sphere_ & stretchBoundary()
Stretch this sphere in place by a small amount to ensure that there will be no roundoff problems if t...
Definition: Geo_Sphere.h:78
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
static Vec & updAs(P *p)
Recast a writable ordinary C++ array E[] to a writable Vec<M,E,S>; assumes compatible length...
Definition: Vec.h:908
Sphere_(const Vec3P &center, RealP radius)
Construct a sphere from its center location and radius.
Definition: Geo_Sphere.h:56
bool isPointOutside(const Vec3P &p, RealP tol) const
Return true if a given point is more than a given tolerance outside the sphere.
Definition: Geo_Sphere.h:101
Sphere_ & scaleBy(RealP f)
Modify this sphere to scale its radius by a fractional amount f, that is we set radius to f*radius...
Definition: Geo_Sphere.h:68
Sphere_()
Construct an uninitialized Sphere object; the center point and radius will be garbage.
Definition: Geo_Sphere.h:54
unsigned char square(unsigned char u)
Definition: Scalar.h:349
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
Vec3P & updCenter()
Get a writable reference to the sphere&#39;s center point.
Definition: Geo_Sphere.h:109
RealP findVolume() const
Return the volume of this sphere (4/3 pi r^3).
Definition: Geo_Sphere.h:87
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Defines geometric primitive shapes and algorthms.
Sphere_ & setRadius(RealP radius)
Change the radius of this sphere.
Definition: Geo_Sphere.h:59
bool isPointOutside(const Vec3P &p) const
Return true if a given point is strictly outside this sphere.
Definition: Geo_Sphere.h:95
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
RealP findDistanceSqr(const Vec3P &p2) const
Find the square of the distance between this point and another one whose location is expressed in the...
Definition: Geo_Point.h:75
This is the header file that every Simmath compilation unit should include first. ...
A geometric primitive representing a sphere by its radius and center point, and a collection of spher...
Definition: Geo.h:56
RealP findArea() const
Return the surface area of this sphere (4 pi r^2).
Definition: Geo_Sphere.h:90
Sphere_ & setCenter(const Vec3P &center)
Change the center location of this sphere.
Definition: Geo_Sphere.h:62
Definition: negator.h:64
RealP getRadius() const
Get the sphere&#39;s radius.
Definition: Geo_Sphere.h:111
unsigned char cube(unsigned char u)
Definition: Scalar.h:420
const Vec3P & getCenter() const
Get the location of the sphere&#39;s center point.
Definition: Geo_Sphere.h:107
static const Vec & getAs(const P *p)
Recast an ordinary C++ array E[] to a const Vec<M,E,S>; assumes compatible length, stride, and packing.
Definition: Vec.h:904