polygon_simplification.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
35 #ifndef _POLYGON_SIMPLIFICATION_CPP_
36 #define _POLYGON_SIMPLIFICATION_CPP_
37 
39 
40 
43 
45 {
46 
47  if(!p->is_simple())
48  {
49 
50  int keep_trying=1;
51  int num_attempts=0;
52 
53  while(keep_trying)
54  {
55 
56  int tt=0;
57  double prev_x=-999999, prev_y=-999999;
58  ROS_WARN("polygon_simplification: polygon is not simple. Printing all vertexes");
59 
60  int tttt=0;
61  for (PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin(); j != p->vertices_end(); ++ j )
62  {
63  //ROS_ERROR("pt%d x=%f y=%f",tttt,j->x(), j->y());
64  tttt++;
65  }
66 
67 
68  PS_CGALPolygon_2::Vertex_const_iterator begin = p->vertices_begin();
69  PS_CGALPolygon_2::Vertex_const_iterator end = --p->vertices_end();
70  if ( begin->x() == end->x() && begin->y() == end->y())
71  {
72  ROS_WARN("Final and start point are equal. Erasing final point");
73  p->erase(end);
74  }
75 
76 
77  for (PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin(); j != p->vertices_end(); ++ j )
78  {
79  if(tt!=0)
80  {
81  if (prev_x==j->x() && prev_y==j->y())
82  {
83  ROS_INFO("Erasing vertex %d",tt);
84  p->erase(j);
85  break;
86  }
87  else
88  {
89  prev_x = CGAL::to_double(j->x());
90  prev_y = CGAL::to_double(j->y());
91  }
92 
93  }
94  else
95  {
96  prev_x = CGAL::to_double(j->x());
97  prev_y = CGAL::to_double(j->y());
98  }
99 
100 
101  //ROS_ERROR("pt%d x=%f y=%f",tt,j->x(), j->y());
102  tt++;
103  }
104 
105 
106  ROS_WARN("offset polygon %d: result of fix: %d",(int)p->size(), p->is_simple());
107 
108  int ttt=0;
109  for (PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin(); j != p->vertices_end(); ++ j )
110  {
111  //ROS_ERROR("pt%d x=%f y=%f",ttt,j->x(), j->y());
112  ttt++;
113  }
114 
115  if (p->is_simple()) keep_trying=0;
116 
117  num_attempts++;
118  if (num_attempts>50)
119  {
120  //ROS_ERROR("Could not add polygon. Returning error");
121  return 0;
122  }
123  }
124  }
125 
126  if(!p->is_counterclockwise_oriented()) //check if its counterclockwise. If not reverse
127  {
128  ROS_INFO("Offset Polygon: ch (%d points) is not counterclockwise oriented. Reversing orientation",(int)p->size());
129  p->reverse_orientation();
130  ROS_INFO("Result Polygon: ch (%d points)",(int)p->size());
131  }
132 
133 
134  ROS_INFO("Removing collinear points... %d", (int)p->size());
135 
136  int keep_going=true;
137 
138  while (keep_going)
139  {
140  keep_going=false;
141  PS_CGALPolygon_2::Vertex_const_iterator j = p->vertices_begin();
142  double x2 = CGAL::to_double((*j).x());
143  double y2 = CGAL::to_double((*j).y());
144 
145  j++;
146  double x1 = CGAL::to_double((*j).x());
147  double y1 = CGAL::to_double((*j).y());
148  j++;
149 
150  for (; j != p->vertices_end(); ++ j )
151  {
152  double m1 = (y2-y1)/(x2-x1);
153  double m2 = ( CGAL::to_double((*j).y()) - y1)/( CGAL::to_double((*j).x())-x1);
154 
155  if (fabs(m1-m2)<0.01)
156  {
157  j--;
158  p->erase(j);
159 
160  keep_going=true;
161  break;
162  }
163 
164  x2=x1;
165  y2=y1;
166  x1= CGAL::to_double((*j).x());
167  y1= CGAL::to_double((*j).y());
168  }
169  }
170 
171  ROS_INFO("End removing collinear points... %d", (int)p->size());
172  return 1;
173 }
174 
175 #endif
176 
Defines the class polygon simplification.
int simplify_polygon(PS_CGALPolygon_2 *p)
CGAL::Polygon_2< PS_CGALKernel > PS_CGALPolygon_2


bo_polygon2d
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:31:29