geometry_tools.h
Go to the documentation of this file.
00001 /* Original work Copyright Paul Bovbel
00002  * Modified work Copyright 2015 Institute of Digital Communication Systems - Ruhr-University Bochum
00003  * Modified by: Adrian Bauer
00004  *
00005  * This program is free software; you can redistribute it and/or modify it under the terms of
00006  * the GNU General Public License as published by the Free Software Foundation;
00007  * either version 3 of the License, or (at your option) any later version.
00008  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
00009  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010  * See the GNU General Public License for more details.
00011  * You should have received a copy of the GNU General Public License along with this program;
00012  * if not, see <http://www.gnu.org/licenses/>.
00013  *
00014  * This file is an extended version of the original file taken from the frontier_exploration ROS package by Paul Bovbel
00015  **/
00016 
00017 
00018 #ifndef GEOMETRY_TOOLS_H_
00019 #define GEOMETRY_TOOLS_H_
00020 
00021 #include <geometry_msgs/Polygon.h>
00022 #include <geometry_msgs/Point.h>
00023 #include <costmap_2d/costmap_2d.h>
00024 
00025 namespace heatmap
00026 {
00033   template<typename T, typename S>
00034   double pointsDistance(const T &one, const S &two){
00035       return sqrt(pow(one.x-two.x,2.0) + pow(one.y-two.y,2.0) + pow(one.z-two.z,2.0));
00036   }
00037 
00043   double polygonPerimeter(const geometry_msgs::Polygon &polygon){
00044 
00045       double perimeter = 0;
00046       if(polygon.points.size()   > 1)
00047       {
00048         for (int i = 0, j = polygon.points.size() - 1; i < polygon.points.size(); j = i++)
00049         {
00050           perimeter += pointsDistance(polygon.points[i], polygon.points[j]);
00051         }
00052       }
00053       return perimeter;
00054   }
00055 
00063   template<typename T, typename S>
00064   bool pointsNearby(const T &one, const S &two, const double &proximity){
00065       return pointsDistance(one, two) <= proximity;
00066   }
00067 
00074   template<typename T>
00075   bool pointInPolygon(const T &point, const geometry_msgs::Polygon &polygon){
00076       int cross = 0;
00077       for (int i = 0, j = polygon.points.size()-1; i < polygon.points.size(); j = i++) {
00078           if ( ((polygon.points[i].y > point.y) != (polygon.points[j].y>point.y)) &&
00079               (point.x < (polygon.points[j].x-polygon.points[i].x) * (point.y-polygon.points[i].y) / (polygon.points[j].y-polygon.points[i].y) + polygon.points[i].x) ){
00080               cross++;
00081           }
00082       }
00083       return bool(cross % 2);
00084   }
00085 
00092   template<typename T, typename S>
00093   double yawOfVector(const T &origin, const S &end){
00094 
00095       double delta_x, delta_y;
00096       delta_x = end.x - origin.x;
00097       delta_y = end.y - origin.y;
00098 
00099       double yaw = atan(delta_x/delta_y);
00100 
00101       if(delta_x < 0){
00102           yaw = M_PI-yaw;
00103       }
00104 
00105       return yaw;
00106   }
00107 
00114   template<typename T>
00115   T bottomLeftPointPolygon(const geometry_msgs::Polygon &polygon)
00116   {
00117     T bottom_left;
00118 
00119     if(polygon.points.size() > 0)
00120     {
00121       bottom_left = polygon.points.front();
00122 
00123       for(int i = 0; i < polygon.points.size(); i++)
00124       {
00125         if(polygon.points.at(i).x < bottom_left.x)
00126           bottom_left.x = polygon.points.at(i).x;
00127         if(polygon.points.at(i).y < bottom_left.y)
00128           bottom_left.y = polygon.points.at(i).y;
00129       }
00130     }
00131 
00132     return bottom_left;
00133   }
00134 
00142   template<typename T>
00143   T topRightPointPolygon(const geometry_msgs::Polygon &polygon)
00144   {
00145     T top_right;
00146 
00147     if(polygon.points.size() > 0)
00148     {
00149       top_right = polygon.points.front();
00150 
00151       for(int i = 0; i < polygon.points.size(); i++)
00152       {
00153         if(polygon.points.at(i).x > top_right.x)
00154           top_right.x = polygon.points.at(i).x;
00155         if(polygon.points.at(i).y > top_right.y)
00156           top_right.y = polygon.points.at(i).y;
00157       }
00158     }
00159 
00160     return top_right;
00161   }
00162 
00170   template<typename T>
00171   std::vector<T> fillPolygon(const geometry_msgs::Polygon &polygon, const double spacing)
00172   {
00173     std::vector<T> points_list;
00174     T bottom_left = bottomLeftPointPolygon<T>(polygon);
00175     T top_right = topRightPointPolygon<T>(polygon);
00176 
00177     T test_point = bottom_left;
00178 
00179       while (test_point.y <= top_right.y)
00180       {
00181         while (test_point.x <= top_right.x)
00182         {
00183           if (heatmap::pointInPolygon(test_point, polygon))
00184             points_list.push_back(test_point);
00185 
00186           test_point.x += spacing;
00187         }
00188         test_point.y += spacing;
00189         test_point.x = bottom_left.x;
00190       }
00191     return points_list;
00192   }
00193 
00203   template<typename T, typename S>
00204   std::vector<double> shepardInterpolation(const std::vector<T> interpolation_points, const std::vector<T> data_points, const std::vector<S> data, const double shepard_power)
00205   {
00206     std::vector<double> interpolated_points;
00207     long double lambda_sum = 0, other_sum = 0;
00208     double interpolation = 0;
00209 
00210     for(int i = 0; i < interpolation_points.size(); i++)
00211     {
00212       T p = interpolation_points.at(i);
00213       for(int j = 0; j < data_points.size(); j++)
00214       {
00215         T q = data_points.at(j);
00216         double dist = heatmap::pointsDistance(p, q);
00217         long double d = 1.0 / (pow(dist, shepard_power));
00218         lambda_sum += d;
00219         other_sum += d * data.at(j);
00220       }
00221       interpolation = (double)(other_sum / lambda_sum);
00222       interpolated_points.push_back(interpolation);
00223 
00224       lambda_sum = other_sum = 0;
00225     }
00226     return interpolated_points;
00227   }
00228 }
00229 #endif


heatmap
Author(s): Adrian Bauer
autogenerated on Thu Feb 11 2016 23:05:26