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
28namespace 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
38 {
39 const auto nReal = static_cast<RealType>(n);
40 const auto piSqrt = std::sqrt(M_PI);
41 const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
42 return std::pow(piSqrt, nReal) / divisor;
43 }
44
45 /**
46 * @brief Returns the volume of a n dimensional hypersphere with radius radius.
47 * @param n The dimensionality of the sphere.
48 * @param radius The radius of the sphere.
49 * @return The volume of a n dimensional hypersphere with radius radius.
50 */
51 template <class RealType>
52 RealType
53 volumeOfHypersphere(std::size_t n, RealType radius)
54 {
55 const auto nReal = static_cast<RealType>(n);
56 const auto piSqrt = std::sqrt(M_PI);
57 const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
58 return std::pow(piSqrt * radius, nReal) / divisor;
59 }
60
61 /**
62 * @brief Returns the volume of an hyperellipsoid with the radii contained by the range [beginRadii, endRadii).
63 * @param beginRadii The first radius.
64 * @param endRadii One past the last radius.
65 * @return The volume of an hyperellipsoid with the radii contained by the range [beginRadii, endRadii)
66 */
67 template <class RealType, class IteratorType>
68 RealType
69 volumeOfHyperellipsoid(IteratorType beginRadii, IteratorType endRadii)
70 {
71 const auto nReal = static_cast<RealType>(std::distance(beginRadii, endRadii));
72 const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
73 auto dividend = std::pow(std::sqrt(M_PI), nReal);
74
75 for (; beginRadii != endRadii; ++beginRadii)
76 {
77 dividend *= *beginRadii;
78 }
79
80 return dividend / divisor;
81 }
82
83 /**
84 * @brief Returns the volume of a hyperspheroid with given dimensionality and radii.
85 *
86 * A hyperspheroid is a hyperellipsoid with one radius equal the polar radius and all other radii equal the equatorial radius.
87 * @param n The dimensionality of the hyperspheroid.
88 * @param polarRadius The polar radius of the hyperspheroid.
89 * @param equatorialRadius The equatorial radius of the hyperspheroid.
90 * @return The volume of the hyperspheroid.
91 */
92 template <class RealType>
93 RealType
94 volumeOfHyperspheroid(std::size_t n, RealType polarRadius, RealType equatorialRadius)
95 {
96 const auto nReal = static_cast<RealType>(n);
97 const auto divisor = std::tgamma(nReal / 2.0 + 1.0);
98 const auto piSqrt = std::sqrt(M_PI);
99 return std::pow(piSqrt, nReal) * std::pow(equatorialRadius, nReal - 1.0) * polarRadius /
100 divisor;
101 }
102} // namespace armarx
#define M_PI
Definition MathTools.h:17
This file offers overloads of toIce() and fromIce() functions for STL container types.
RealType volumeOfUnitHypersphere(std::size_t n)
Returns the volume of a n dimensional unit hypersphere.
Definition Volume.h:37
RealType volumeOfHyperellipsoid(IteratorType beginRadii, IteratorType endRadii)
Returns the volume of an hyperellipsoid with the radii contained by the range [beginRadii,...
Definition Volume.h:69
RealType volumeOfHypersphere(std::size_t n, RealType radius)
Returns the volume of a n dimensional hypersphere with radius radius.
Definition Volume.h:53
RealType volumeOfHyperspheroid(std::size_t n, RealType polarRadius, RealType equatorialRadius)
Returns the volume of a hyperspheroid with given dimensionality and radii.
Definition Volume.h:94