pinocchio  3.7.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
 
Loading...
Searching...
No Matches
static-if.hpp
1//
2// Copyright (c) 2019-2020 INRIA
3//
4
5#ifndef __pinocchio_utils_static_if_hpp__
6#define __pinocchio_utils_static_if_hpp__
7
8#include "pinocchio/fwd.hpp"
9
10namespace pinocchio
11{
12 namespace internal
13 {
14
15 enum ComparisonOperators
16 {
17 LT,
18 LE,
19 EQ,
20 GE,
21 GT
22 };
23
24 template<typename LhsType, typename RhsType, typename ThenType, typename ElseType>
25 struct if_then_else_impl;
26
27 template<typename LhsType, typename RhsType>
28 struct comparison_eq_impl;
29
31 template<typename LhsType, typename RhsType, typename return_type>
32 struct traits<if_then_else_impl<LhsType, RhsType, return_type, return_type>>
33 {
34 typedef return_type ReturnType;
35 };
36
37 template<typename condition_type, typename ThenType, typename ElseType>
38 struct if_then_else_impl<condition_type, condition_type, ThenType, ElseType>
39 {
40 typedef typename internal::traits<if_then_else_impl>::ReturnType ReturnType;
41
42 static inline ReturnType run(
43 const ComparisonOperators op,
44 const condition_type & lhs_value,
45 const condition_type & rhs_value,
46 const ThenType & then_value,
47 const ElseType & else_value)
48 {
49 switch (op)
50 {
51 case EQ:
52 if (lhs_value == rhs_value)
53 return then_value;
54 else
55 return else_value;
56 break;
57 case LT:
58 if (lhs_value < rhs_value)
59 return then_value;
60 else
61 return else_value;
62 break;
63 case LE:
64 if (lhs_value <= rhs_value)
65 return then_value;
66 else
67 return else_value;
68 break;
69 case GE:
70 if (lhs_value >= rhs_value)
71 return then_value;
72 else
73 return else_value;
74 break;
75 case GT:
76 if (lhs_value > rhs_value)
77 return then_value;
78 else
79 return else_value;
80 break;
81 }
82 PINOCCHIO_THROW_PRETTY(
83 std::logic_error, "ComparisonOperators " << static_cast<int>(op) << " is not managed");
84 }
85 };
86
87 template<typename LhsType, typename RhsType, typename ThenType, typename ElseType>
88 inline typename if_then_else_impl<LhsType, RhsType, ThenType, ElseType>::ReturnType
89 if_then_else(
90 const ComparisonOperators op,
91 const LhsType & lhs_value,
92 const RhsType & rhs_value,
93 const ThenType & then_value,
94 const ElseType & else_value)
95 {
96 typedef if_then_else_impl<LhsType, RhsType, ThenType, ElseType> algo;
97 return algo::run(op, lhs_value, rhs_value, then_value, else_value);
98 }
99
100 // Generic
101 template<typename LhsType, typename RhsType>
102 struct comparison_eq_impl
103 {
104 static inline bool run(const LhsType & lhs_value, const RhsType & rhs_value)
105 {
106 return lhs_value == rhs_value;
107 }
108 };
109
110 template<typename condition_type>
111 struct comparison_eq_impl<condition_type, condition_type>
112 {
113 static inline bool run(const condition_type & lhs_value, const condition_type & rhs_value)
114 {
115 return lhs_value == rhs_value;
116 }
117 };
118
119 template<typename LhsType, typename RhsType>
120 inline bool comparison_eq(const LhsType & lhs_value, const RhsType & rhs_value)
121 {
122 typedef comparison_eq_impl<LhsType, RhsType> algo;
123 return algo::run(lhs_value, rhs_value);
124 }
125 } // namespace internal
126} // namespace pinocchio
127
128#endif
Main pinocchio namespace.
Definition treeview.dox:11