stator
A math, geometry, and utility library
sphere.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 Marcus N Campbell Bannerman <[email protected]>
3 
4  This file is part of stator.
5 
6  stator is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  stator is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with stator. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 // stator
23 #include "stator/config.hpp"
24 #include "stator/constants.hpp"
25 
26 namespace stator {
27  namespace geometry {
39  template<typename Scalar, size_t D>
40  class Ball {
41  public:
49  Ball() {}
50 
52  Ball(const Scalar& radius, const Vector<Scalar, D>& center = Vector<Scalar, D>::Zero().eval()): radius_(radius), center_(center) {}
53 
55  const Scalar& radius() const { return radius_; }
56 
58  const Vector<Scalar, D>& center() const { return center_; }
59 
60  protected:
62  Scalar radius_;
63 
66  };
67 
72  template<typename Scalar, size_t D>
73  class InverseBall: public Ball<Scalar, D> {
74  public:
76  };
77 
78  namespace detail {
89  template<typename Scalar, size_t D> class UnitBall{};
90  }// namespace detail
91 
93  template<typename Scalar>
94  constexpr Scalar volume(const detail::UnitBall<Scalar, 0>& ball) { return Scalar(1); }
95 
96  template<typename Scalar>
97  constexpr Scalar volume(const detail::UnitBall<Scalar, 1>& ball) { return Scalar(2); }
98 
99  template<typename Scalar, size_t D>
100  constexpr typename std::enable_if<(D%2) && (D>1), Scalar>::type
101  volume(const detail::UnitBall<Scalar, D>& ball) {
102  return 2 * std::tgamma((D-1)/2+1)
103  * std::pow(4 * stator::constant<Scalar>::pi(), (D-1)/2)
104  / std::tgamma(D+1);
105  }
106 
107  template<typename Scalar, size_t D>
108  constexpr typename std::enable_if<(!(D%2)) && (D>1), Scalar>::type
109  volume(const detail::UnitBall<Scalar, D>& ball) {
110  return std::pow(stator::constant<Scalar>::pi(), D/2) / std::tgamma(D/2+1);
111  }
112 
113  template<typename Scalar, size_t D>
114  Scalar area(const detail::UnitBall<Scalar, D>& ball) {
115  return D * volume(detail::UnitBall<Scalar,D>());
116  }
120  template<typename Scalar, size_t D>
121  Scalar volume(const Ball<Scalar, D>& ball) {
122  return volume(detail::UnitBall<Scalar, D>()) * std::pow(ball.radius(), D);
123  }
124 
126  template<typename Scalar, size_t D>
127  Scalar area(const Ball<Scalar, D>& sphere) {
128  return area(detail::UnitBall<Scalar, D>()) * std::pow(sphere.radius(), D-1);
129  }
130  } // namespace geometry
131 } // namespace stator
132 
auto area(const Obj &)
Compute a Hausdorf measure of the boundary/boundaries of an object.
auto volume(const Obj &)
Compute a Hausdorf measure of the interior of an object.
Scalar radius_
Radius of the ball.
Definition: sphere.hpp:62
Fundamental typedef&#39;s and macros for stator.
Physical constants in different floating point representations.
Definition: constants.hpp:40
Ball(const Scalar &radius, const Vector< Scalar, D > &center=Vector< Scalar, D >::Zero().eval())
RAII constructor.
Definition: sphere.hpp:52
An empty-class representation of a unit ball.
Definition: sphere.hpp:89
An n-ball (an n-sphere including its interior volume).
Definition: sphere.hpp:40
Vector< Scalar, D > center_
Center of the ball.
Definition: sphere.hpp:65
const Scalar & radius() const
Get function for the ball radius.
Definition: sphere.hpp:55
const Vector< Scalar, D > & center() const
Get function for the ball center.
Definition: sphere.hpp:58
The stator library namespace.
Definition: frontpage.dox:243
auto pow(const LHS &l, const RHS &r) -> STATOR_AUTORETURN(std::pow(l, r))
Ball()
Default constructor.
Definition: sphere.hpp:49
An inverse n-ball (an n-sphere including its exterior volume).
Definition: sphere.hpp:73
Matrix< Scalar, D, 1 > Vector
A convenience typedef for a non-aligned Eigen Vector.
Definition: config.hpp:58