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 
00035 #ifndef _POLYGON_SIMPLIFICATION_CPP_
00036 #define _POLYGON_SIMPLIFICATION_CPP_
00037 
00038 #include <bo_polygon2d/polygon_simplification.h>
00039 
00040 
00041 class_polygon_simplification::class_polygon_simplification(void){};
00042 class_polygon_simplification::~class_polygon_simplification(void){};
00043 
00044 int class_polygon_simplification::simplify_polygon(PS_CGALPolygon_2* p)
00045 {
00046 
00047         if(!p->is_simple())
00048         {
00049 
00050                 int keep_trying=1;
00051                 int num_attempts=0;
00052 
00053                 while(keep_trying)
00054                 {
00055 
00056                         int tt=0;
00057                         double prev_x=-999999, prev_y=-999999;
00058                         ROS_WARN("polygon_simplification: polygon is not simple. Printing all vertexes");
00059 
00060                         int tttt=0;
00061                         for (PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin(); j != p->vertices_end(); ++ j )
00062                         {
00063                                 
00064                                 tttt++;
00065                         }
00066 
00067 
00068                         PS_CGALPolygon_2::Vertex_const_iterator begin = p->vertices_begin();
00069                         PS_CGALPolygon_2::Vertex_const_iterator end = --p->vertices_end();
00070                         if ( begin->x() == end->x() && begin->y() == end->y())
00071                         {
00072                                 ROS_WARN("Final and start point are equal. Erasing final point");
00073                                 p->erase(end);  
00074                         }       
00075 
00076 
00077                         for (PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin(); j != p->vertices_end(); ++ j )
00078                         {
00079                                 if(tt!=0)
00080                                 {
00081                                         if (prev_x==j->x() && prev_y==j->y())
00082                                         {
00083                                                 ROS_INFO("Erasing vertex %d",tt);
00084                                                 p->erase(j);    
00085                                                 break;
00086                                         }
00087                                         else
00088                                         {
00089                                                 prev_x = CGAL::to_double(j->x());       
00090                                                 prev_y = CGAL::to_double(j->y());       
00091                                         }
00092 
00093                                 }
00094                                 else
00095                                 {
00096                                         prev_x = CGAL::to_double(j->x());       
00097                                         prev_y = CGAL::to_double(j->y());       
00098                                 }
00099 
00100 
00101                                 
00102                                 tt++;
00103                         }
00104 
00105 
00106                         ROS_WARN("offset polygon %d: result of fix: %d",(int)p->size(), p->is_simple());
00107 
00108                         int ttt=0;
00109                         for (PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin(); j != p->vertices_end(); ++ j )
00110                         {
00111                                 
00112                                 ttt++;
00113                         }
00114 
00115                         if (p->is_simple()) keep_trying=0;
00116 
00117                         num_attempts++;
00118                         if (num_attempts>50)
00119                         {
00120                                 
00121                                 return 0;
00122                         }
00123                 }
00124         }
00125 
00126         if(!p->is_counterclockwise_oriented()) 
00127         {
00128                 ROS_INFO("Offset Polygon: ch  (%d points) is not counterclockwise oriented. Reversing orientation",(int)p->size());
00129                 p->reverse_orientation();       
00130                 ROS_INFO("Result Polygon: ch  (%d points)",(int)p->size());
00131         }
00132 
00133 
00134         ROS_INFO("Removing collinear points... %d", (int)p->size());
00135 
00136         int keep_going=true;
00137 
00138         while (keep_going)
00139         {
00140                 keep_going=false;
00141                 PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin();
00142                 double x2 = CGAL::to_double((*j).x());
00143                 double y2 = CGAL::to_double((*j).y());
00144 
00145                 j++;
00146                 double x1 = CGAL::to_double((*j).x());
00147                 double y1 = CGAL::to_double((*j).y());
00148                 j++;
00149 
00150                 for (; j != p->vertices_end(); ++ j )
00151                 {
00152                         double m1 = (y2-y1)/(x2-x1);
00153                         double m2 = ( CGAL::to_double((*j).y()) - y1)/( CGAL::to_double((*j).x())-x1);
00154 
00155                         if (fabs(m1-m2)<0.01)
00156                         {
00157                                 j--;
00158                                 p->erase(j);
00159 
00160                                 keep_going=true;        
00161                                 break;
00162                         }
00163 
00164                         x2=x1;
00165                         y2=y1;
00166                         x1= CGAL::to_double((*j).x());
00167                         y1= CGAL::to_double((*j).y());
00168                 }
00169         }
00170 
00171         ROS_INFO("End removing collinear points... %d", (int)p->size());
00172         return 1;
00173 }
00174 
00175 #endif
00176