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 { return value; }
63 }
64 
69 inline void clampInPlaceAndWarn(double & value, double lower, double upper, const std::string & label)
70 {
71  value = clampAndWarn(value, lower, upper, label);
72 }
73 
88 template<typename VectorT>
89 inline VectorT clamp(const VectorT & v, double lower, double upper)
90 {
91  VectorT result(v.size());
92  for(unsigned i = 0; i < v.size(); i++) { result(i) = clamp(v(i), lower, upper); }
93  return result;
94 }
95 
102 template<typename VectorT>
103 inline VectorT clamp(const VectorT & v, const VectorT & lower, const VectorT & upper)
104 {
105  VectorT result(v.size());
106  for(unsigned i = 0; i < v.size(); i++) { result(i) = clamp(v(i), lower(i), upper(i)); }
107  return result;
108 }
109 
116 template<typename VectorT>
117 inline VectorT clampAndWarn(const VectorT & v, const VectorT & lower, const VectorT & upper, const std::string & label)
118 {
119  VectorT result(v.size());
120  for(unsigned i = 0; i < v.size(); i++)
121  {
122  result(i) = clampAndWarn(v(i), lower(i), upper(i), label + " (" + std::to_string(i) + ")");
123  }
124  return result;
125 }
126 
132 template<typename VectorT>
133 inline void clampInPlace(VectorT & v, const VectorT & lower, const VectorT & upper)
134 {
135  for(unsigned i = 0; i < v.size(); i++) { v(i) = clamp(v(i), lower(i), upper(i)); }
136 }
137 
143 template<typename VectorT>
144 inline void clampInPlace(VectorT & v, double lower, double upper)
145 {
146  for(unsigned i = 0; i < v.size(); i++) { v(i) = clamp(v(i), lower, upper); }
147 }
148 
155 template<typename VectorT>
156 inline void clampInPlaceAndWarn(VectorT & vector, double lower, double upper, const std::string & label)
157 {
158  for(unsigned i = 0; i < vector.size(); i++)
159  {
160  clampInPlaceAndWarn(vector(i), lower, upper, label + " (" + std::to_string(i) + ")");
161  }
162 }
163 
170 template<typename VectorT>
171 inline void clampInPlaceAndWarn(VectorT & vector,
172  const VectorT & lower,
173  const VectorT & upper,
174  const std::string & label)
175 {
176  for(unsigned i = 0; i < vector.size(); i++)
177  {
178  clampInPlaceAndWarn(vector(i), lower(i), upper(i), label + " (" + std::to_string(i) + ")");
179  }
180 }
181 
182 } // namespace utils
183 } // namespace mc_filter
mc_filter::utils::clampInPlaceAndWarn
void clampInPlaceAndWarn(double &value, double lower, double upper, const std::string &label)
Definition: clamp.h:69
mc_filter
Definition: ExponentialMovingAverage.h:14
mc_rtc::log::warning
void warning(Args &&... args)
Definition: logging.h:69
mc_filter::utils::clamp
double clamp(double value, double lower, double upper)
Definition: clamp.h:29
mc_filter::utils::clampInPlace
void clampInPlace(double &value, double lower, double upper)
Definition: clamp.h:38
mc_rbdyn::details::to_string
std::conditional< std::is_same< std::string, T >::value, const std::string &, std::string >::type to_string(const T &value)
Definition: RobotLoader.h:51
logging.h
mc_filter::utils::clampAndWarn
double clampAndWarn(double value, double lower, double upper, const std::string &label)
Definition: clamp.h:49