mc_rtc  2.16.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 { return std::max(lower, std::min(value, upper)); }
31 
36 inline void clampInPlace(double & value, double lower, double upper)
37 { value = clamp(value, lower, upper); }
38 
45 inline double clampAndWarn(double value, double lower, double upper, const std::string & label)
46 {
47  if(value > upper)
48  {
49  mc_rtc::log::warning("{} clamped to {}", label, upper);
50  ;
51  return upper;
52  }
53  else if(value < lower)
54  {
55  mc_rtc::log::warning("{} clamped to {}", label, lower);
56  return lower;
57  }
58  else
59  {
60  return value;
61  }
62 }
63 
68 inline void clampInPlaceAndWarn(double & value, double lower, double upper, const std::string & label)
69 { value = clampAndWarn(value, lower, upper, label); }
70 
85 template<typename VectorT>
86 inline VectorT clamp(const VectorT & v, double lower, double upper)
87 {
88  VectorT result(v.size());
89  for(unsigned i = 0; i < v.size(); i++) { result(i) = clamp(v(i), lower, upper); }
90  return result;
91 }
92 
99 template<typename VectorT>
100 inline VectorT clamp(const VectorT & v, const VectorT & lower, const VectorT & upper)
101 {
102  VectorT result(v.size());
103  for(unsigned i = 0; i < v.size(); i++) { result(i) = clamp(v(i), lower(i), upper(i)); }
104  return result;
105 }
106 
113 template<typename VectorT>
114 inline VectorT clampAndWarn(const VectorT & v, const VectorT & lower, const VectorT & upper, const std::string & label)
115 {
116  VectorT result(v.size());
117  for(unsigned i = 0; i < v.size(); i++)
118  {
119  result(i) = clampAndWarn(v(i), lower(i), upper(i), label + " (" + std::to_string(i) + ")");
120  }
121  return result;
122 }
123 
129 template<typename VectorT>
130 inline void clampInPlace(VectorT & v, const VectorT & lower, const VectorT & upper)
131 {
132  for(unsigned i = 0; i < v.size(); i++) { v(i) = clamp(v(i), lower(i), upper(i)); }
133 }
134 
140 template<typename VectorT>
141 inline void clampInPlace(VectorT & v, double lower, double upper)
142 {
143  for(unsigned i = 0; i < v.size(); i++) { v(i) = clamp(v(i), lower, upper); }
144 }
145 
152 template<typename VectorT>
153 inline void clampInPlaceAndWarn(VectorT & vector, double lower, double upper, const std::string & label)
154 {
155  for(unsigned i = 0; i < vector.size(); i++)
156  {
157  clampInPlaceAndWarn(vector(i), lower, upper, label + " (" + std::to_string(i) + ")");
158  }
159 }
160 
167 template<typename VectorT>
168 inline void clampInPlaceAndWarn(VectorT & vector,
169  const VectorT & lower,
170  const VectorT & upper,
171  const std::string & label)
172 {
173  for(unsigned i = 0; i < vector.size(); i++)
174  {
175  clampInPlaceAndWarn(vector(i), lower(i), upper(i), label + " (" + std::to_string(i) + ")");
176  }
177 }
178 
179 } // namespace utils
180 } // namespace mc_filter
std::optional< Eigen::VectorXd > value
void clampInPlace(double &value, double lower, double upper)
Definition: clamp.h:36
void clampInPlaceAndWarn(double &value, double lower, double upper, const std::string &label)
Definition: clamp.h:68
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:45
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(const S &format, Args &&... args)
Definition: logging.h:75