Volume.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <cmath>
27 
28 namespace armarx
29 {
30  /**
31  * @brief Returns the volume of a n dimensional unit hypersphere.
32  * @param n The dimensionality of the sphere.
33  * @return The volume of a n dimensional unit hypersphere.
34  */
35  template<class RealType>
36  RealType volumeOfUnitHypersphere(std::size_t n)
37  {
38  const auto nReal = static_cast<RealType>(n);
39  const auto piSqrt = std::sqrt(M_PI);
40  const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
41  return std::pow(piSqrt, nReal) / divisor;
42  }
43 
44  /**
45  * @brief Returns the volume of a n dimensional hypersphere with radius radius.
46  * @param n The dimensionality of the sphere.
47  * @param radius The radius of the sphere.
48  * @return The volume of a n dimensional hypersphere with radius radius.
49  */
50  template<class RealType>
51  RealType volumeOfHypersphere(std::size_t n, RealType radius)
52  {
53  const auto nReal = static_cast<RealType>(n);
54  const auto piSqrt = std::sqrt(M_PI);
55  const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
56  return std::pow(piSqrt * radius, nReal) / divisor;
57  }
58 
59  /**
60  * @brief Returns the volume of an hyperellipsoid with the radii contained by the range [beginRadii, endRadii).
61  * @param beginRadii The first radius.
62  * @param endRadii One past the last radius.
63  * @return The volume of an hyperellipsoid with the radii contained by the range [beginRadii, endRadii)
64  */
65  template<class RealType, class IteratorType>
66  RealType volumeOfHyperellipsoid(IteratorType beginRadii, IteratorType endRadii)
67  {
68  const auto nReal = static_cast<RealType>(std::distance(beginRadii, endRadii));
69  const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
70  auto dividend = std::pow(std::sqrt(M_PI), nReal);
71 
72  for (; beginRadii != endRadii; ++beginRadii)
73  {
74  dividend *= *beginRadii;
75  }
76 
77  return dividend / divisor;
78  }
79 
80  /**
81  * @brief Returns the volume of a hyperspheroid with given dimensionality and radii.
82  *
83  * A hyperspheroid is a hyperellipsoid with one radius equal the polar radius and all other radii equal the equatorial radius.
84  * @param n The dimensionality of the hyperspheroid.
85  * @param polarRadius The polar radius of the hyperspheroid.
86  * @param equatorialRadius The equatorial radius of the hyperspheroid.
87  * @return The volume of the hyperspheroid.
88  */
89  template<class RealType>
90  RealType volumeOfHyperspheroid(std::size_t n, RealType polarRadius, RealType equatorialRadius)
91  {
92  const auto nReal = static_cast<RealType>(n);
93  const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
94  const auto piSqrt = std::sqrt(M_PI);
95  return std::pow(piSqrt, nReal) * std::pow(equatorialRadius, nReal - 1.0) * polarRadius / divisor;
96  }
97 }
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::volumeOfUnitHypersphere
RealType volumeOfUnitHypersphere(std::size_t n)
Returns the volume of a n dimensional unit hypersphere.
Definition: Volume.h:36
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::volumeOfHyperspheroid
RealType volumeOfHyperspheroid(std::size_t n, RealType polarRadius, RealType equatorialRadius)
Returns the volume of a hyperspheroid with given dimensionality and radii.
Definition: Volume.h:90
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
armarx::volumeOfHyperellipsoid
RealType volumeOfHyperellipsoid(IteratorType beginRadii, IteratorType endRadii)
Returns the volume of an hyperellipsoid with the radii contained by the range [beginRadii,...
Definition: Volume.h:66
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::volumeOfHypersphere
RealType volumeOfHypersphere(std::size_t n, RealType radius)
Returns the volume of a n dimensional hypersphere with radius radius.
Definition: Volume.h:51