helpers.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012-2016, Institute of Flight Systems and Automatic Control,
3 // Technische Universität Darmstadt.
4 // All rights reserved.
5 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of hector_quadrotor nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef HECTOR_QUADROTOR_MODEL_HELPERS_H
30 #define HECTOR_QUADROTOR_MODEL_HELPERS_H
31 
32 #include <geometry_msgs/Wrench.h>
33 #include <boost/range/iterator_range.hpp>
34 
36 {
37 
38 template <typename T> int isnan(const T& value) {
39  return std::isnan(value);
40 }
41 
42 template <typename T, std::size_t N> int isnan(const boost::array<T,N>& array) {
43  for(typename boost::array<T,N>::const_iterator it = array.begin(); it != array.end(); ++it)
44  if (std::isnan(*it)) return std::isnan(*it);
45  return false;
46 }
47 
48 template <typename IteratorT> int isnan(const boost::iterator_range<IteratorT>& range, const typename boost::iterator_range<IteratorT>::value_type& min, const typename boost::iterator_range<IteratorT>::value_type& max) {
49  for(IteratorT it = range.begin(); it != range.end(); ++it)
50  if (std::isnan(*it)) return std::isnan(*it);
51  return false;
52 }
53 
54 template <typename T> int isinf(const T& value) {
55  return std::isinf(value);
56 }
57 
58 template <typename T, std::size_t N> int isinf(const boost::array<T,N>& array) {
59  for(typename boost::array<T,N>::const_iterator it = array.begin(); it != array.end(); ++it)
60  if (std::isinf(*it)) return std::isinf(*it);
61  return false;
62 }
63 
64 template <typename IteratorT> int isinf(const boost::iterator_range<IteratorT>& range, const typename boost::iterator_range<IteratorT>::value_type& min, const typename boost::iterator_range<IteratorT>::value_type& max) {
65  for(IteratorT it = range.begin(); it != range.end(); ++it)
66  if (std::isinf(*it)) return std::isinf(*it);
67  return false;
68 }
69 
70 template <typename T> void limit(T& value, const T& min, const T& max) {
71  if (!isnan(min) && value < min) value = min;
72  if (!isnan(max) && value > max) value = max;
73 }
74 
75 template <typename T, std::size_t N> void limit(boost::array<T,N>& array, const T& min, const T& max) {
76  for(typename boost::array<T,N>::iterator it = array.begin(); it != array.end(); ++it)
77  limit(*it, min, max);
78 }
79 
80 template <typename IteratorT> void limit(const boost::iterator_range<IteratorT>& range, const typename boost::iterator_range<IteratorT>::value_type& min, const typename boost::iterator_range<IteratorT>::value_type& max) {
81  for(IteratorT it = range.begin(); it != range.end(); ++it)
82  limit(*it, min, max);
83 }
84 
85 template <typename T> static inline void checknan(T& value, const std::string& text = "") {
86  if (isnan(value)) {
87 #ifndef NDEBUG
88  if (!text.empty()) std::cerr << text << " contains **!?* Nan values!" << std::endl;
89 #endif
90  value = T();
91  return;
92  }
93 
94  if (isinf(value)) {
95 #ifndef NDEBUG
96  if (!text.empty()) std::cerr << text << " is +-Inf!" << std::endl;
97 #endif
98  value = T();
99  return;
100  }
101 }
102 
103 template <typename Message, typename Vector> static inline void toVector(const Message& msg, Vector& vector)
104 {
105  vector.x = msg.x;
106  vector.y = msg.y;
107  vector.z = msg.z;
108 }
109 
110 template <typename Message, typename Vector> static inline void fromVector(const Vector& vector, Message& msg)
111 {
112  msg.x = vector.x;
113  msg.y = vector.y;
114  msg.z = vector.z;
115 }
116 
117 template <typename Message, typename Quaternion> static inline void toQuaternion(const Message& msg, Quaternion& quaternion)
118 {
119  quaternion.w = msg.w;
120  quaternion.x = msg.x;
121  quaternion.y = msg.y;
122  quaternion.z = msg.z;
123 }
124 
125 template <typename Message, typename Quaternion> static inline void fromQuaternion(const Quaternion& quaternion, Message& msg)
126 {
127  msg.w = quaternion.w;
128  msg.x = quaternion.x;
129  msg.y = quaternion.y;
130  msg.z = quaternion.z;
131 }
132 
133 static inline geometry_msgs::Vector3 operator+(const geometry_msgs::Vector3& a, const geometry_msgs::Vector3& b)
134 {
135  geometry_msgs::Vector3 result;
136  result.x = a.x + b.x;
137  result.y = a.y + b.y;
138  result.z = a.z + b.z;
139  return result;
140 }
141 
142 static inline geometry_msgs::Wrench operator+(const geometry_msgs::Wrench& a, const geometry_msgs::Wrench& b)
143 {
144  geometry_msgs::Wrench result;
145  result.force = a.force + b.force;
146  result.torque = a.torque + b.torque;
147  return result;
148 }
149 
150 template<typename T>
151 class PrintVector {
152 public:
153  typedef const T* const_iterator;
154  PrintVector(const_iterator begin, const_iterator end, const std::string &delimiter = "[ ]") : begin_(begin), end_(end), delimiter_(delimiter) {}
155  const_iterator begin() const { return begin_; }
156  const_iterator end() const { return end_; }
157  std::size_t size() const { return end_ - begin_; }
158 
159  std::ostream& operator>>(std::ostream& os) const {
160  if (!delimiter_.empty()) os << delimiter_.substr(0, delimiter_.size() - 1);
161  for(const_iterator it = begin(); it != end(); ++it) {
162  if (it != begin()) os << " ";
163  os << *it;
164  }
165  if (!delimiter_.empty()) os << delimiter_.substr(1, delimiter_.size() - 1);
166  return os;
167  }
168 
169 private:
170  const_iterator begin_, end_;
171  std::string delimiter_;
172 };
173 template <typename T> std::ostream &operator<<(std::ostream& os, const PrintVector<T>& vector) { return vector >> os; }
174 
175 }
176 
177 #endif // HECTOR_QUADROTOR_MODEL_HELPERS_H
const_iterator end() const
Definition: helpers.h:156
int isnan(const T &value)
Definition: helpers.h:38
int isinf(const boost::iterator_range< IteratorT > &range, const typename boost::iterator_range< IteratorT >::value_type &min, const typename boost::iterator_range< IteratorT >::value_type &max)
Definition: helpers.h:64
std::size_t size() const
Definition: helpers.h:157
static void fromVector(const Vector &vector, Message &msg)
Definition: helpers.h:110
void limit(T &value, const T &min, const T &max)
Definition: helpers.h:70
static void checknan(T &value, const std::string &text="")
Definition: helpers.h:85
std::ostream & operator>>(std::ostream &os) const
Definition: helpers.h:159
const_iterator begin() const
Definition: helpers.h:155
static void fromQuaternion(const Quaternion &quaternion, Message &msg)
Definition: helpers.h:125
int isnan(const boost::iterator_range< IteratorT > &range, const typename boost::iterator_range< IteratorT >::value_type &min, const typename boost::iterator_range< IteratorT >::value_type &max)
Definition: helpers.h:48
static void toQuaternion(const Message &msg, Quaternion &quaternion)
Definition: helpers.h:117
int isinf(const T &value)
Definition: helpers.h:54
static geometry_msgs::Vector3 operator+(const geometry_msgs::Vector3 &a, const geometry_msgs::Vector3 &b)
Definition: helpers.h:133
static void toVector(const Message &msg, Vector &vector)
Definition: helpers.h:103
PrintVector(const_iterator begin, const_iterator end, const std::string &delimiter="[ ]")
Definition: helpers.h:154


hector_quadrotor_model
Author(s): Johannes Meyer , Alexander Sendobry
autogenerated on Mon Jun 10 2019 13:36:56