00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef VIGIR_FOOTSTEP_PLANNING_LIB_MATH_H__
00031 #define VIGIR_FOOTSTEP_PLANNING_LIB_MATH_H__
00032
00033 #define DEBUG_HASH 0
00034 #define DEBUG_TIME 0
00035
00036 #include <math.h>
00037
00038 #include <tf/tf.h>
00039 #include <angles/angles.h>
00040
00041 #include <vigir_footstep_planning_msgs/footstep_planning_msgs.h>
00042
00043
00044
00045 namespace vigir_footstep_planning
00046 {
00047 static const double TWO_PI = 2 * M_PI;
00048
00049 static const double FLOAT_CMP_THR = 0.0001;
00050
00052 static const int cvMmScale = 1000;
00053
00054 enum Leg { RIGHT=0, LEFT=1, NOLEG=2 };
00055
00056
00061 inline double euclidean_distance_sq(int x1, int y1, int x2, int y2)
00062 {
00063
00064 return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
00065 }
00066
00068 inline double euclidean_distance_sq(double x1, double y1, double x2, double y2)
00069 {
00070
00071 return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
00072 }
00073
00074 inline double euclidean_distance_sq(int x1, int y1, int z1, int x2, int y2, int z2)
00075 {
00076
00077 return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2);
00078 }
00079
00081 inline double euclidean_distance_sq(double x1, double y1, double z1, double x2, double y2, double z2)
00082 {
00083
00084 return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2);
00085 }
00086
00088 inline double euclidean_distance(int x1, int y1, int x2, int y2)
00089 {
00090 return sqrt(double(euclidean_distance_sq(x1, y1, x2, y2)));
00091 }
00092
00094 inline double euclidean_distance(double x1, double y1, double x2, double y2)
00095 {
00096 return sqrt(euclidean_distance_sq(x1, y1, x2, y2));
00097 }
00098
00100 inline double euclidean_distance(int x1, int y1, int z1, int x2, int y2, int z2)
00101 {
00102 return sqrt(double(euclidean_distance_sq(x1, y1, z1, x2, y2, z2)));
00103 }
00104
00106 inline double euclidean_distance(double x1, double y1, double z1, double x2, double y2, double z2)
00107 {
00108 return sqrt(euclidean_distance_sq(x1, y1, z1, x2, y2, z2));
00109 }
00110
00111 inline double parabol(double x, double y, double a_inv, double b_inv)
00112 {
00113 return x*x*a_inv + y*y*b_inv;
00114 }
00115
00117 inline double grid_cost(int x1, int y1, int x2, int y2, float cell_size)
00118 {
00119 int x = abs(x1 - x2);
00120 int y = abs(y1 - y2);
00121
00122 if (x + y > 1)
00123 return M_SQRT2 * cell_size;
00124 else
00125 return cell_size;
00126 }
00127
00128
00129 inline double pround(double x, double prec)
00130 {
00131 return ::round(x/prec)*prec;
00132 }
00133
00134 inline double pceil(double x, double prec)
00135 {
00136 return ::ceil(x/prec)*prec;
00137 }
00138
00139 inline double pfloor(double x, double prec)
00140 {
00141 return ::floor(x/prec)*prec;
00142 }
00143
00144
00146
00147 inline int disc_val(double length, double cell_size)
00148 {
00149
00150 return static_cast<int>(round(length / cell_size));
00151 }
00152
00153
00158
00159 inline double cont_val(int length, double cell_size)
00160 {
00161
00162 return static_cast<double>(length) * cell_size;
00163 }
00164
00165
00167 inline int angle_state_2_cell(double angle, double angle_bin_size)
00168 {
00169
00170
00171
00172
00173 return disc_val(angle, angle_bin_size);
00174 }
00175
00176
00178 inline double angle_cell_2_state(int angle, double angle_bin_size)
00179 {
00180
00181
00182
00183 return cont_val(angle, angle_bin_size);
00184 }
00185
00186
00191 inline int state_2_cell(float value, float cell_size)
00192 {
00193
00194 return disc_val(value, cell_size);
00195 }
00196
00197
00202 inline double cell_2_state(int value, double cell_size)
00203 {
00204
00205 return cont_val(value, cell_size);
00206 }
00207
00208
00210 inline unsigned int int_hash(int key)
00211 {
00212 key += (key << 12);
00213 key ^= (key >> 22);
00214 key += (key << 4);
00215 key ^= (key >> 9);
00216 key += (key << 10);
00217 key ^= (key >> 2);
00218 key += (key << 7);
00219 key ^= (key >> 12);
00220 return key;
00221 }
00222
00223
00229 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)
00230 {
00231 return int_hash((int_hash(x) << 3) + (int_hash(y) << 2) + (int_hash(theta) << 1) +
00232 (int_hash(leg))) % max_hash_size;
00233
00234
00235
00236 }
00237
00245 bool pointWithinPolygon(int x, int y, const std::vector<std::pair<int, int> >& edges);
00246
00247 void getDeltaStep(const msgs::Foot& current, const msgs::Foot& next, geometry_msgs::Pose& dstep);
00248 void getDeltaStep(const tf::Pose& current, const tf::Pose& next, tf::Pose& dstep);
00249
00250 void quaternionToNormalYaw(const geometry_msgs::Quaternion& q, geometry_msgs::Vector3& n, double& yaw);
00251 void quaternionToNormal(const geometry_msgs::Quaternion& q, geometry_msgs::Vector3& n);
00252 void normalToQuaternion(const geometry_msgs::Vector3& n, double yaw, geometry_msgs::Quaternion& q);
00253
00254 void RPYToNormal(double r, double p, double y, geometry_msgs::Vector3& n);
00255 void normalToRP(const geometry_msgs::Vector3& n, double yaw, double& r, double& p);
00256 }
00257
00258 #endif