libalmath  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unitutils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Softbank Robotics. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the COPYING file.
5  */
6 
7 #ifndef LIBALMATH_ALMATH_SCENEGRAPH_QIANIM_UTILS_H
8 #define LIBALMATH_ALMATH_SCENEGRAPH_QIANIM_UTILS_H
9 
10 #include <almath/api.h>
12 #include <boost/math/constants/constants.hpp>
13 
14 namespace AL {
15 namespace qianim {
16 
17 // example usage:
18 // float val_from = ...;
19 // auto val_to = getUnitConversionFactor<float>(to, from) * val_from;
20 template <typename Scalar>
21 typename std::enable_if<std::is_floating_point<Scalar>::value, Scalar>::type
23  if (from == to)
24  return static_cast<Scalar>(1);
25  if (to == Unit::radian && from == Unit::degree)
26  return boost::math::constants::pi<Scalar>()/180;
27  if (to == Unit::degree && from == Unit::radian)
28  return 180/boost::math::constants::pi<Scalar>();
29  // units are not commensurable
30  throw std::invalid_argument("units are not of the same dimension");
31 }
32 
33 // example usage:
34 // float val_from = ...;
35 // auto val_si = getSIConversionFactor<float>(from) * val_from;
36 template <typename Scalar>
37 typename std::enable_if<std::is_floating_point<Scalar>::value, Scalar>::type
39  return (from == Unit::degree) ? boost::math::constants::pi<Scalar>()/180
40  : static_cast<Scalar>(1);
41 }
42 }
43 }
44 
45 #endif
std::enable_if< std::is_floating_point< Scalar >::value, Scalar >::type getUnitConversionFactor(Unit to, Unit from)
Definition: unitutils.h:22
std::enable_if< std::is_floating_point< Scalar >::value, Scalar >::type getSIConversionFactor(Unit from)
Definition: unitutils.h:38