math.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2017, Alexander Stumpf, TU Darmstadt
3 // Based on http://wiki.ros.org/footstep_planner by Johannes Garimort and Armin Hornung
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 the Simulation, Systems Optimization and Robotics
14 // group, TU Darmstadt nor the names of its contributors may be used to
15 // endorse or promote products derived from this software without
16 // specific prior written permission.
17 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
22 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //=================================================================================================
29 
30 #ifndef VIGIR_FOOTSTEP_PLANNING_LIB_MATH_H__
31 #define VIGIR_FOOTSTEP_PLANNING_LIB_MATH_H__
32 
33 #define DEBUG_HASH 0
34 #define DEBUG_TIME 0
35 
36 #include <math.h>
37 
38 #include <tf/tf.h>
39 #include <angles/angles.h>
40 
41 #include <vigir_footstep_planning_msgs/footstep_planning_msgs.h>
42 
43 
44 
46 {
47 static const double TWO_PI = 2 * M_PI;
48 
49 static const double FLOAT_CMP_THR = 0.0001;
50 
52 static const int cvMmScale = 1000;
53 
54 enum Leg { RIGHT=0, LEFT=1, NOLEG=2 };
55 
56 
61 inline double euclidean_distance_sq(int x1, int y1, int x2, int y2)
62 {
63  // note: do *not* use pow() to square!
64  return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
65 }
66 
68 inline double euclidean_distance_sq(double x1, double y1, double x2, double y2)
69 {
70  // note: do *not* use pow() to square!
71  return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
72 }
73 
74 inline double euclidean_distance_sq(int x1, int y1, int z1, int x2, int y2, int z2)
75 {
76  // note: do *not* use pow() to square!
77  return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2);
78 }
79 
81 inline double euclidean_distance_sq(double x1, double y1, double z1, double x2, double y2, double z2)
82 {
83  // note: do *not* use pow() to square!
84  return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2);
85 }
86 
88 inline double euclidean_distance(int x1, int y1, int x2, int y2)
89 {
90  return sqrt(double(euclidean_distance_sq(x1, y1, x2, y2)));
91 }
92 
94 inline double euclidean_distance(double x1, double y1, double x2, double y2)
95 {
96  return sqrt(euclidean_distance_sq(x1, y1, x2, y2));
97 }
98 
100 inline double euclidean_distance(int x1, int y1, int z1, int x2, int y2, int z2)
101 {
102  return sqrt(double(euclidean_distance_sq(x1, y1, z1, x2, y2, z2)));
103 }
104 
106 inline double euclidean_distance(double x1, double y1, double z1, double x2, double y2, double z2)
107 {
108  return sqrt(euclidean_distance_sq(x1, y1, z1, x2, y2, z2));
109 }
110 
111 inline double parabol(double x, double y, double a_inv, double b_inv)
112 {
113  return x*x*a_inv + y*y*b_inv;
114 }
115 
117 inline double grid_cost(int x1, int y1, int x2, int y2, float cell_size)
118 {
119  int x = abs(x1 - x2);
120  int y = abs(y1 - y2);
121 
122  if (x + y > 1)
123  return M_SQRT2 * cell_size;
124  else
125  return cell_size;
126 }
127 
128 
129 inline double pround(double x, double prec)
130 {
131  return ::round(x/prec)*prec;
132 }
133 
134 inline double pceil(double x, double prec)
135 {
136  return ::ceil(x/prec)*prec;
137 }
138 
139 inline double pfloor(double x, double prec)
140 {
141  return ::floor(x/prec)*prec;
142 }
143 
144 
146 // TODO: check consistency for negative values
147 inline int disc_val(double length, double cell_size)
148 {
149  //return int(floor((length / cell_size) + 0.5));
150  return static_cast<int>(round(length / cell_size));
151 }
152 
153 
158 // TODO: check consistency for negative values
159 inline double cont_val(int length, double cell_size)
160 {
161  //return double(length * cell_size);
162  return static_cast<double>(length) * cell_size;
163 }
164 
165 
167 inline int angle_state_2_cell(double angle, double angle_bin_size)
168 {
169 // double bin_size_half = M_PI / angle_bin_num;
170 // return int(angles::normalize_angle_positive(angle + bin_size_half) /
171 // TWO_PI * angle_bin_num);
172  //return int(angles::normalize_angle_positive(angle + 0.5*angle_bin_size) / angle_bin_size);
173  return disc_val(angle, angle_bin_size);
174 }
175 
176 
178 inline double angle_cell_2_state(int angle, double angle_bin_size)
179 {
180 // double bin_size = TWO_PI / angle_bin_num;
181 // return angle * bin_size;
182  //return double(angle * angle_bin_size);
183  return cont_val(angle, angle_bin_size);
184 }
185 
186 
191 inline int state_2_cell(float value, float cell_size)
192 {
193  //return value >= 0 ? int(value / cell_size) : int(value / cell_size) - 1;
194  return disc_val(value, cell_size);
195 }
196 
197 
202 inline double cell_2_state(int value, double cell_size)
203 {
204  //return (double(value) + 0.5) * cell_size;
205  return cont_val(value, cell_size);
206 }
207 
208 
210 inline unsigned int int_hash(int key)
211 {
212  key += (key << 12);
213  key ^= (key >> 22);
214  key += (key << 4);
215  key ^= (key >> 9);
216  key += (key << 10);
217  key ^= (key >> 2);
218  key += (key << 7);
219  key ^= (key >> 12);
220  return key;
221 }
222 
223 
229 inline unsigned int calc_hash_tag(int x, int y, int theta, int leg, unsigned int hash_pred, unsigned int hash_succ, int max_hash_size)
230 {
231  return int_hash((int_hash(x) << 3) + (int_hash(y) << 2) + (int_hash(theta) << 1) +
232  (int_hash(leg))) % max_hash_size;
233 
234 // return int_hash((int_hash(x) << 4) + (int_hash(y) << 3) + (int_hash(theta) << 2) +
235 // (int_hash(leg) << 1)/* + hash_pred + hash_succ*/) % max_hash_size;
236 }
237 
245 bool pointWithinPolygon(int x, int y, const std::vector<std::pair<int, int> >& edges);
246 
247 void getDeltaStep(const msgs::Foot& current, const msgs::Foot& next, geometry_msgs::Pose& dstep);
248 void getDeltaStep(const tf::Pose& current, const tf::Pose& next, tf::Pose& dstep);
249 
250 void quaternionToNormalYaw(const geometry_msgs::Quaternion& q, geometry_msgs::Vector3& n, double& yaw);
251 void quaternionToNormal(const geometry_msgs::Quaternion& q, geometry_msgs::Vector3& n);
252 void normalToQuaternion(const geometry_msgs::Vector3& n, double yaw, geometry_msgs::Quaternion& q);
253 
254 void RPYToNormal(double r, double p, double y, geometry_msgs::Vector3& n);
255 void normalToRP(const geometry_msgs::Vector3& n, double yaw, double& r, double& p);
256 }
257 
258 #endif
int state_2_cell(float value, float cell_size)
Discretize a (continuous) state value into a cell. (Should be used to discretize a State to a Plannin...
Definition: math.h:191
bool pointWithinPolygon(int x, int y, const std::vector< std::pair< int, int > > &edges)
Crossing number method to determine whether a point lies within a polygon or not. ...
Definition: math.cpp:5
int angle_state_2_cell(double angle, double angle_bin_size)
Discretize a (continuous) angle into a bin.
Definition: math.h:167
static const int cvMmScale
Used to scale continuous values in meter to discrete values in mm.
Definition: math.h:52
double cont_val(int length, double cell_size)
Calculates the continuous value for a length discretized in cell size.
Definition: math.h:159
void normalToQuaternion(const geometry_msgs::Vector3 &n, double yaw, geometry_msgs::Quaternion &q)
Definition: math.cpp:84
double euclidean_distance(int x1, int y1, int x2, int y2)
Definition: math.h:88
static const double FLOAT_CMP_THR
Definition: math.h:49
double grid_cost(int x1, int y1, int x2, int y2, float cell_size)
Definition: math.h:117
TFSIMD_FORCE_INLINE const tfScalar & y() const
void quaternionToNormal(const geometry_msgs::Quaternion &q, geometry_msgs::Vector3 &n)
Definition: math.cpp:73
double euclidean_distance_sq(int x1, int y1, int x2, int y2)
Definition: math.h:61
void quaternionToNormalYaw(const geometry_msgs::Quaternion &q, geometry_msgs::Vector3 &n, double &yaw)
Definition: math.cpp:60
double cell_2_state(int value, double cell_size)
Calculate the respective (continuous) state value for a cell. (Should be used to get a State from a d...
Definition: math.h:202
void RPYToNormal(double r, double p, double y, geometry_msgs::Vector3 &n)
Definition: math.cpp:91
double pfloor(double x, double prec)
Definition: math.h:139
static const double TWO_PI
Definition: math.h:47
TFSIMD_FORCE_INLINE const tfScalar & x() const
void normalToRP(const geometry_msgs::Vector3 &n, double yaw, double &r, double &p)
Definition: math.cpp:104
unsigned int int_hash(int key)
Definition: math.h:210
void getDeltaStep(const msgs::Foot &current, const msgs::Foot &next, geometry_msgs::Pose &dstep)
Definition: math.cpp:27
int disc_val(double length, double cell_size)
Discretize a (continuous) value into cell size.
Definition: math.h:147
double parabol(double x, double y, double a_inv, double b_inv)
Definition: math.h:111
double pround(double x, double prec)
Definition: math.h:129
double pceil(double x, double prec)
Definition: math.h:134
unsigned int calc_hash_tag(int x, int y, int theta, int leg, unsigned int hash_pred, unsigned int hash_succ, int max_hash_size)
Definition: math.h:229
double angle_cell_2_state(int angle, double angle_bin_size)
Calculate the respective (continuous) angle for a bin.
Definition: math.h:178


vigir_footstep_planning_lib
Author(s): Alexander Stumpf
autogenerated on Mon Jun 10 2019 15:47:33