mc_rtc  2.14.0
clamp.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  *
4  * This file is inspired by Stephane's Caron original implementation as part of
5  * lipm_walking_controller <https://github.com/stephane-caron/lipm_walking_controller>
6  */
7 
8 #pragma once
9 
10 #include <mc_rtc/logging.h>
11 
12 #include <Eigen/Core>
13 #include <map>
14 
15 namespace mc_filter
16 {
17 
18 namespace utils
19 {
20 
29 inline double clamp(double value, double lower, double upper)
30 {
31  return std::max(lower, std::min(value, upper));
32 }
33 
38 inline void clampInPlace(double & value, double lower, double upper)
39 {
40  value = clamp(value, lower, upper);
41 }
42 
49 inline double clampAndWarn(double value, double lower, double upper, const std::string & label)
50 {
51  if(value > upper)
52  {
53  mc_rtc::log::warning("{} clamped to {}", label, upper);
54  ;
55  return upper;
56  }
57  else if(value < lower)
58  {
59  mc_rtc::log::warning("{} clamped to {}", label, lower);
60  return lower;
61  }
62  else
63  {
64  return value;
65  }
66 }
67 
72 inline void clampInPlaceAndWarn(double & value, double lower, double upper, const std::string & label)
73 {
74  value = clampAndWarn(value, lower, upper, label);
75 }
76 
91 template<typename VectorT>
92 inline VectorT clamp(const VectorT & v, double lower, double upper)
93 {
94  VectorT result(v.size());
95  for(unsigned i = 0; i < v.size(); i++) { result(i) = clamp(v(i), lower, upper); }
96  return result;
97 }
98 
105 template<typename VectorT>
106 inline VectorT clamp(const VectorT & v, const VectorT & lower, const VectorT & upper)
107 {
108  VectorT result(v.size());
109  for(unsigned i = 0; i < v.size(); i++) { result(i) = clamp(v(i), lower(i), upper(i)); }
110  return result;
111 }
112 
119 template<typename VectorT>
120 inline VectorT clampAndWarn(const VectorT & v, const VectorT & lower, const VectorT & upper, const std::string & label)
121 {
122  VectorT result(v.size());
123  for(unsigned i = 0; i < v.size(); i++)
124  {
125  result(i) = clampAndWarn(v(i), lower(i), upper(i), label + " (" + std::to_string(i) + ")");
126  }
127  return result;
128 }
129 
135 template<typename VectorT>
136 inline void clampInPlace(VectorT & v, const VectorT & lower, const VectorT & upper)
137 {
138  for(unsigned i = 0; i < v.size(); i++) { v(i) = clamp(v(i), lower(i), upper(i)); }
139 }
140 
146 template<typename VectorT>
147 inline void clampInPlace(VectorT & v, double lower, double upper)
148 {
149  for(unsigned i = 0; i < v.size(); i++) { v(i) = clamp(v(i), lower, upper); }
150 }
151 
158 template<typename VectorT>
159 inline void clampInPlaceAndWarn(VectorT & vector, double lower, double upper, const std::string & label)
160 {
161  for(unsigned i = 0; i < vector.size(); i++)
162  {
163  clampInPlaceAndWarn(vector(i), lower, upper, label + " (" + std::to_string(i) + ")");
164  }
165 }
166 
173 template<typename VectorT>
174 inline void clampInPlaceAndWarn(VectorT & vector,
175  const VectorT & lower,
176  const VectorT & upper,
177  const std::string & label)
178 {
179  for(unsigned i = 0; i < vector.size(); i++)
180  {
181  clampInPlaceAndWarn(vector(i), lower(i), upper(i), label + " (" + std::to_string(i) + ")");
182  }
183 }
184 
185 } // namespace utils
186 } // namespace mc_filter
void clampInPlace(double &value, double lower, double upper)
Definition: clamp.h:38
void clampInPlaceAndWarn(double &value, double lower, double upper, const std::string &label)
Definition: clamp.h:72
double clamp(double value, double lower, double upper)
Definition: clamp.h:29
double clampAndWarn(double value, double lower, double upper, const std::string &label)
Definition: clamp.h:49
Definition: ExponentialMovingAverage.h:15
std::conditional< std::is_same< std::string, T >::value, const std::string &, std::string >::type to_string(const T &value)
Definition: RobotLoader.h:51
void warning(Args &&... args)
Definition: logging.h:69