polygon_utils.cpp
Go to the documentation of this file.
00001 #include <lama_common/angular_point.h>
00002 #include <lama_common/polygon_utils.h>
00003 
00004 #include <ros/ros.h> // DEBUG
00005 #include <fstream> // DEBUG
00006 #include <iostream> // DEBUG
00007 
00008 namespace lama_common
00009 {
00010 
00014 void centerPolygon(geometry_msgs::Polygon& polygon)
00015 {
00016   double dx;
00017   double dy;
00018   centerPolygon(polygon, dx, dy);
00019 }
00020 
00027 void centerPolygon(geometry_msgs::Polygon& polygon, double& dx, double& dy)
00028 {
00029   dx = 0.0;
00030   dy = 0.0;
00031 
00032   if (polygon.points.empty())
00033   {
00034     return;
00035   }
00036 
00037   // Compute the center of the polygon.
00038   vector<Point32>::const_iterator cpt;
00039   for (cpt = polygon.points.begin(); cpt != polygon.points.end(); ++cpt)
00040   {
00041     dx -= cpt->x;
00042     dy -= cpt->y;
00043   }
00044   dx /= (double)polygon.points.size();
00045   dy /= (double)polygon.points.size();
00046 
00047   vector<Point32>::iterator pt;
00048   for (pt = polygon.points.begin(); pt != polygon.points.end(); ++pt)
00049   {
00050     pt->x += dx;
00051     pt->y += dy;
00052   }
00053 }
00054 
00060 geometry_msgs::Polygon centeredPolygon(const geometry_msgs::Polygon& polygon)
00061 {
00062   geometry_msgs::Polygon centered_polygon = polygon;
00063   centerPolygon(centered_polygon);
00064   return centered_polygon;
00065 }
00066 
00074 geometry_msgs::Polygon centeredPolygon(const geometry_msgs::Polygon& polygon, double& dx, double& dy)
00075 {
00076   geometry_msgs::Polygon centered_polygon = polygon;
00077   centerPolygon(centered_polygon, dx, dy);
00078   return centered_polygon;
00079 }
00080 
00086 static bool normalizablePolygon(const vector<AngularPoint>& angular_points)
00087 {
00088   unsigned int count_plus = 0;
00089   unsigned int count_minus = 0;
00090 
00091   std::ofstream ofs("/tmp/diff.txt", std::ios_base::out); // DEBUG
00092   for (size_t i = 0; i < angular_points.size(); ++i)
00093   {
00094     const double this_angle = angular_points[i].angle;
00095     const double next_angle = angular_points[(i + 1) % angular_points.size()].angle;
00096     ofs << this_angle << " " << next_angle << " " << next_angle - this_angle << std::endl; // DEBUG
00097     if (this_angle == next_angle)
00098     {
00099       return false;
00100     }
00101     if (this_angle < next_angle)
00102     {
00103       count_plus++;
00104     }
00105     else
00106     {
00107       count_minus++;
00108     }
00109     if (count_plus > 1 && count_minus > 1)
00110     {
00111       /* DEBUG */
00112       std::ofstream ofs2("/tmp/normalizable.txt", std::ios_base::out);
00113       ofs2 << "count_plus: " << count_plus << "; count_minus: " << count_minus << std::endl; // DEBUG
00114       ofs2.close();
00115       /* DEBUG */
00116       return false;
00117     }
00118   }
00119   ofs.close(); // DEBUG
00120   return true;
00121 }
00122 
00136 bool normalizePolygon(geometry_msgs::Polygon& poly)
00137 {
00138   std::vector<AngularPoint> angular_points = toAngularPoints(poly);
00139   const size_t point_count = poly.points.size();
00140 
00141   /* DEBUG */
00142   std::ofstream ofs2("/tmp/points_and_angle.txt");
00143   for (size_t i = 0; i < poly.points.size(); ++i)
00144   {
00145     ofs2 << poly.points[i].x << " " << poly.points[i].y << " " << angular_points[i].angle << std::endl; // DEBUG
00146   }
00147   ofs2.close();
00148   /* DEBUG */
00149   if (!normalizablePolygon(angular_points))
00150   {
00151     return false;
00152   }
00153 
00154   if (!is_sorted(angular_points.begin(), angular_points.end()))
00155   {
00156     // TODO: Improve sort performance.
00157     // Appart from the position of the step from +pi to -pi (CCW) which can be in
00158     // the middle of the array instead of being absent, angular_points
00159     // should be sorted. Use a circular_iterator, if this exists.
00160     std::sort(angular_points.begin(), angular_points.end());
00161 
00162     geometry_msgs::Polygon old_poly = poly;
00163     for (size_t i = 0; i < point_count; ++i)
00164     {
00165       poly.points[i] = old_poly.points[angular_points[i].index];
00166     }
00167   }
00168   return true;
00169 }
00170 
00180 geometry_msgs::Polygon normalizedPolygon(const geometry_msgs::Polygon& poly, bool& normalized)
00181 {
00182   geometry_msgs::Polygon new_poly = poly;
00183   normalized = normalizePolygon(new_poly);
00184   return new_poly;
00185 }
00186 
00191 bool isClosed(const geometry_msgs::Polygon& poly, double max_frontier_width)
00192 {
00193   const size_t size = poly.points.size();
00194   const double width2 = max_frontier_width * max_frontier_width;
00195 
00196   for(size_t i = 0; i < size; ++i)
00197   {
00198     Point32 a = poly.points[i];
00199     Point32 b = poly.points[(i + 1) % size];
00200 
00201     const double dx = b.x - a.x;
00202     const double dy = b.y - a.y;
00203 
00204     if (dx * dx + dy * dy > width2)
00205     {
00206       return false;
00207     }
00208   }
00209   return true;
00210 }
00211 
00212 } /* namespace lama_common */


lama_common
Author(s): Gaël Ecorchard , Karel Košnar , Vojtěch Vonásek
autogenerated on Thu Jun 6 2019 22:02:03