costmap_math.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <costmap_2d/costmap_math.h>
00031 
00032 double distanceToLine(double pX, double pY, double x0, double y0, double x1, double y1)
00033 {
00034   double A = pX - x0;
00035   double B = pY - y0;
00036   double C = x1 - x0;
00037   double D = y1 - y0;
00038 
00039   double dot = A * C + B * D;
00040   double len_sq = C * C + D * D;
00041   double param = dot / len_sq;
00042 
00043   double xx, yy;
00044 
00045   if (param < 0)
00046   {
00047     xx = x0;
00048     yy = y0;
00049   }
00050   else if (param > 1)
00051   {
00052     xx = x1;
00053     yy = y1;
00054   }
00055   else
00056   {
00057     xx = x0 + param * C;
00058     yy = y0 + param * D;
00059   }
00060 
00061   return distance(pX, pY, xx, yy);
00062 }
00063 
00064 bool intersects(std::vector<geometry_msgs::Point>& polygon, float testx, float testy)
00065 {
00066   bool c = false;
00067   int i, j, nvert = polygon.size();
00068   for (i = 0, j = nvert - 1; i < nvert; j = i++)
00069   {
00070     float yi = polygon[i].y, yj = polygon[j].y, xi = polygon[i].x, xj = polygon[j].x;
00071 
00072     if (((yi > testy) != (yj > testy)) && (testx < (xj - xi) * (testy - yi) / (yj - yi) + xi))
00073       c = !c;
00074   }
00075   return c;
00076 }
00077 
00078 bool intersects_helper(std::vector<geometry_msgs::Point>& polygon1, std::vector<geometry_msgs::Point>& polygon2)
00079 {
00080   for (unsigned int i = 0; i < polygon1.size(); i++)
00081     if (intersects(polygon2, polygon1[i].x, polygon1[i].y))
00082       return true;
00083   return false;
00084 }
00085 
00086 bool intersects(std::vector<geometry_msgs::Point>& polygon1, std::vector<geometry_msgs::Point>& polygon2)
00087 {
00088   return intersects_helper(polygon1, polygon2) || intersects_helper(polygon2, polygon1);
00089 }


costmap_2d
Author(s): Eitan Marder-Eppstein, David V. Lu!!, Dave Hershberger, contradict@gmail.com
autogenerated on Thu Aug 27 2015 14:06:55