]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/polygons2d/simplifypolygon.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / polygons2d / simplifypolygon.m
1 ## Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% -*- texinfo -*-
17 %% @deftypefn {Function File} {@var{spoly} = } simplifypolygon (@var{poly})
18 %% Simplify a polygon using the Ramer-Douglas-Peucker algorithm.
19 %%
20 %% @var{poly} is a N-by-2 matrix, each row representing a vertex.
21 %%
22 %% @seealso{simplifypolyline, shape2polygon}
23 %% @end deftypefn
24
25 function polygonsimp = simplifypolygon (polygon, varargin)
26
27   polygonsimp = simplifypolyline (polygon,varargin{:});
28
29   # Remove parrallel consecutive edges
30   PL = polygonsimp(1:end-1,:);
31   PC = polygonsimp(2:end,:);
32   PR = polygonsimp([3:end 1],:);
33   a = PL - PC;
34   b = PR - PC;
35   tf = find(isParallel(a,b))+1;
36   polygonsimp (tf,:) = [];
37
38 endfunction
39
40 %!test
41 %!  P = [0 0; 1 0; 0 1];
42 %!  P2 = [0 0; 0.1 0; 0.2 0; 0.25 0; 1 0; 0 1; 0 0.7; 0 0.6; 0 0.3; 0 0.1];
43 %! assert(simplifypolygon (P2),P,min(P2(:))*eps)
44
45 %!demo
46 %!
47 %!  P = [0 0; 1 0; 0 1];
48 %!  P2 = [0 0; 0.1 0; 0.2 0; 0.25 0; 1 0; 0 1; 0 0.7; 0 0.6; 0 0.3; 0 0.1];
49 %!  Pr = simplifypolygon (P2);
50 %!
51 %!  cla
52 %!  drawPolygon(P,'or;Reference;');
53 %!  hold on
54 %!  drawPolygon(P2,'x-b;Redundant;');
55 %!  drawPolygon(Pr,'*g;Simplified;');
56 %!  hold off
57 %!
58 %! % --------------------------------------------------------------------------
59 %! % The two polygons describe the same figure, a triangle. Extra points are
60 %! % removed form the redundant one.