]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/polygons2d/expandPolygon.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / polygons2d / expandPolygon.m
1 ## Copyright (C) 2003-2011 David Legland <david.legland@grignon.inra.fr>
2 ## Copyright (C) 2012 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 ## All rights reserved.
4 ##
5 ## Redistribution and use in source and binary forms, with or without
6 ## modification, are permitted provided that the following conditions are met:
7 ##
8 ##     1 Redistributions of source code must retain the above copyright notice,
9 ##       this list of conditions and the following disclaimer.
10 ##     2 Redistributions in binary form must reproduce the above copyright
11 ##       notice, this list of conditions and the following disclaimer in the
12 ##       documentation and/or other materials provided with the distribution.
13 ##
14 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
15 ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 ## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 ## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 ## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 ##
25 ## The views and conclusions contained in the software and documentation are
26 ## those of the authors and should not be interpreted as representing official
27 ## policies, either expressed or implied, of the copyright holders.
28
29 ## -*- texinfo -*-
30 ## @deftypefn {Function File} {@var{loops} = } expandPolygon (@var{poly}, @var{dist})
31 ##  Expand a polygon by a given (signed) distance
32 ##
33 ##   Associates to each edge of the polygon @var{poly} the parallel line located
34 ##   at distance @var{dist} from the current edge, and compute intersections with
35 ##   neighbor parallel lines. The resulting polygon is simplified to remove
36 ##   inner "loops", and can be disconnected.
37 ##   The result is a cell array, each cell containing a simple linear ring.
38 ##
39 ##   This is a kind of dilation, but behaviour on corners is different.
40 ##   This function keeps angles of polygons, but there is no direct relation
41 ##   between length of 2 polygons.
42 ##
43 ##   It is also possible to specify negative distance, and get all points
44 ##   inside the polygon. If the polygon is convex, the result equals
45 ##   morphological erosion of polygon by a ball with radius equal to the
46 ##   given distance.
47 ##
48 ## @seealso{polygons2d}
49 ## @end deftypefn
50
51 function loops = expandPolygon(poly, dist)
52
53   # eventually copy first point at the end to ensure closed polygon
54   if sum(poly(end, :)==poly(1,:))~=2
55       poly = [poly; poly(1,:)];
56   end
57
58   # number of vertices of the polygon
59   N = size(poly, 1)-1;
60
61   # find lines parallel to polygon edges located at distance DIST
62   lines = zeros(N, 4);
63   for i=1:N
64       side = createLine(poly(i,:), poly(i+1,:));
65       lines(i, 1:4) = parallelLine(side, dist);
66   end
67
68   # compute intersection points of consecutive lines
69   lines = [lines;lines(1,:)];
70   poly2 = zeros(N, 2);
71   for i=1:N
72       poly2(i,1:2) = intersectLines(lines(i,:), lines(i+1,:));
73   end
74
75   # split result polygon into set of loops (simple polygons)
76   loops = polygonLoops(poly2);
77
78   # keep only loops whose distance to original polygon is correct
79   distLoop = zeros(length(loops), 1);
80   for i=1:length(loops)
81       distLoop(i) = distancePolygons(loops{i}, poly);
82   end
83   loops = loops(abs(distLoop-abs(dist))<abs(dist)/1000);
84
85 endfunction