summaryrefslogtreecommitdiff
path: root/inst/clipPolygon_mrf.m
blob: 73437d1e93133cb219cfb39c8ccc5f5c7859a7fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
## Copyright (C) 2017 - Piyush Jain
## Copyright (C) 2017 - Juan Pablo Carbajal
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deffn {}  [@var{outpol}, @var{npol}] = clipPolygon_mrf (@var{inpol}, @var{clippol})
## @deffnx {} [@var{outpol}, @var{npol}] = clipPolygon_mrf (@var{inpol}, @var{clippol}, @var{op})
## Perform boolean operation on polygon(s) using the algorithm by Martinez, Rueda and Feito.
##
## @var{inpol} = Nx2 matrix of (X, Y) coordinates constituting the polygons(s)
## to be clipped (subject polygon).
## @var{clippol} = Nx2 matrix of (X, Y) coordinates representing the clip polygon(s).
##
## Polygons may have multiple non-intersecting regions. The contours are separated by [Nan NaN] rows.
## Polygons may also contain holes. Holes are defined as the region enclosed within the boundaries of polygon which are not part of the polygon region. Here again, the inner and outer contours are separated by [Nan NaN] rows. Specifically, if a contour is lying inside another contour, it is automatically treated as a hole. 
##
## The following features are allowed in input polygons:
##
## @itemize
## @item 0: Contours can be described in clockwise or counterclockwise order.
##
## @item 1: Holes.
##
## @item 2: A vertex of a polygon can touch (in a point) a vertex or edge of the same polygon.
##
## @item 3: Self-intersecting polygons.
## @end itemize
##
## The following features are not allowed in input polygons:
##
## @itemize
## The intersection of two edges of the same polygon can be a point, but cannot be a segment.
## @end itemize
##
## The argument @var{op}, the boolean operation, can be:
##
## @itemize
## @item 0: difference @var{inpol} - @var{clippol}
##
## @item 1: intersection ("AND") of @var{inpol} and @var{clippol} (= default)
##
## @item 2: exclusiveOR ("XOR") of @var{inpol} and @var{clippol}
##
## @item 3: union ("OR") of @var{inpol} and @var{clippol}
## @end itemize
##
## Output array @var{outpol} will be an Nx2 array of polygons resulting from
## the requested boolean operation
##
## Optional output argument @var{npol} indicates the number of output polygons.
##
## Know more about the algorithm by Martinez, Rueda and Feito[1].
##
## [1]: @uref{http://www4.ujaen.es/~fmartin/bool_op.html}
##
## @seealso{clipPolygon_clipper,clipPolygon}
## @end deffn

## Created: 2017-06-09

function [outpol, npol] = clipPolygon_mrf (inpoly, clippoly=[], method=1)

  ## Input validation

  if (nargin < 2 || nargin > 3)
    print_usage ();
  endif

  if(isempty(inpoly) || isempty(clippoly))
    error ('Octave:invalid-input-arg', ...
            "clipPolygon_mrf: Empty polygon");
  endif

  if (! isnumeric (inpoly) || size (inpoly, 2) < 2)
    error ('Octave:invalid-input-arg', ...
            "clipPolygon_mrf: inpoly should be a numeric Nx2 array");
  endif

  if (! isnumeric (clippoly) || size (clippoly, 2) < 2)
    error ('Octave:invalid-input-arg', ...
            "clipPolygon_mrf: clippoly should be a numeric Nx2 array");
  elseif (! isnumeric (method) || method < 0 || method > 3)
    error ('Octave:invalid-input-arg', ...
            "clipPolygon_mrf: operation must be a number in the range [0..3]");
  endif

  inpol = __polytostruct__ (inpoly);

  clpol = __polytostruct__ (clippoly);

  ## Perform boolean operation
  [X, Y, npol] = polybool_mrf(inpol, clpol, method);

  if(npol == 0)
    outpol = [ , ];
  else
    outpol = [X Y];
  endif
endfunction

%!test
%! pol1 = [-0.15 -0.15; 0.45 -0.15; 0.15 0.45];
%! pol2 = [-0.05 -0.05; 0.15 0.35; 0.35 -0.05; NaN NaN;
%!        0.05 0.05; 0.25 0.05; 0.15 0.25];
%! opol_0 = [0.15 0.25; 0.05 0.05; 0.25 0.05; NaN NaN;
%!            0.15 0.35; -0.05 -0.05; 0.35 -0.05; NaN NaN;
%!           0.15 0.45; -0.15 -0.15; 0.45 -0.15];
%! npol_0 = 3;
%! opol_1= [0.15 0.25; 0.05 0.05; 0.25 0.05; NaN NaN;
%!            0.15 0.35; -0.05 -0.05; 0.35 -0.05];
%! npol_1 = 2;
%! opol_2 = [0.15 0.25; 0.05 0.05; 0.25 0.05; NaN NaN;
%!            0.15 0.35; -0.05 -0.05; 0.35 -0.05; NaN NaN;
%!            0.15 0.45; -0.15 -0.15; 0.45 -0.15];
%! npol_2 = 3;
%! opol_3 = [0.15 0.45; -0.15 -0.15; 0.45 -0.15];
%! npol_3 = 1;
%! [opol npol] = clipPolygon_mrf (pol1, pol2, 0);
%! assert(opol, opol_0);
%! assert(npol, npol_0);
%! [opol npol] = clipPolygon_mrf (pol1, pol2, 1);
%! assert(opol, opol_1);
%! assert(npol, npol_1);
%! [opol npol] = clipPolygon_mrf (pol1, pol2, 2);
%! assert(opol, opol_2);
%! assert(npol, npol_2);
%! [opol npol] = clipPolygon_mrf (pol1, pol2, 3);
%! assert(opol, opol_3);
%! assert(npol, npol_3);
%!error <clipPolygon_mrf: Empty polygon> clipPolygon_mrf([], [], 0);
%!error
%! pol1 = [0.15 0.15; 0.55 0.45; 0.15 0.75];
%! pol2 = [0.35 0.45; 0.75 0.15; 0.75 0.75];
%! clipPolygon_mrf(pol1, pol2, 4);
 
%!test  ## Bug #56506
%! subpol = [0 0; 5 5; 2.5 4;0 0];
%! clppol2 = [10 1; 16 6; 13.5 5; 10 1];
%! [a, b] = clipPolygon_mrf (subpol, clppol2, 1);
%! assert (a, []);
%! assert (b, 0);

%!demo
%! pol1 = [0.15 0.15; 0.55 0.45; 0.15 0.75];
%! pol2 = [0.35 0.45; 0.75 0.15; 0.75 0.75];
%! pol1a = polygon2patch(pol1);
%! pol2a = polygon2patch(pol2);
%! lw = 2;
%! subplot (2, 6, [2 3])
%! patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'k', 'linewidth', lw);
%! axis image
%! grid on
%! title ("1. Subject polygon")
%!
%! subplot (2, 6, [4 5])
%! patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
%! hold on
%! patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'b', 'linewidth', lw);
%! axis image
%! grid on
%! title "2. Clip polygon"
%!
%! op   = {"Sub -clip", "AND / Intersection", "Exclusive OR", "OR / Union"};
%! for i=1:numel(op)
%!   subplot (6, 4, [12 16]+i);
%!   [opol, npol] = clipPolygon_mrf (pol1, pol2, i-1);
%!   opol = polygon2patch (opol);
%!   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
%!   hold on
%!   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
%!   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
%!         'linewidth', lw);
%!   axis image
%!   grid on
%!   title (sprintf("%d. %s", i+2, op{i}));
%!   axis off
%! endfor
%!
%! subplot (10, 4, 37);
%!   [opol, npol] = clipPolygon_mrf (pol2, pol1, 0);
%!   opol = polygon2patch (opol);
%!   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
%!   hold on
%!   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
%!   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
%!         'linewidth', lw);
%!   axis image
%!   grid on
%!   axis off
%!   title "7. Clip - sub";

%!demo
%! pol1 = [1 1; 5 1; 3 7; NaN NaN; 2 2; 3 5; 4 2];
%! pol2 = [3 1; 5 6; 1 6];
%! pol1a = polygon2patch(pol1);
%! pol2a = polygon2patch(pol2);
%! lw = 2;
%! subplot (2, 6, [2 3])
%! patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'k', 'linewidth', lw);
%! axis image
%! grid on
%! title ("1. Subject polygon")
%!
%! subplot (2, 6, [4 5])
%! patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
%! hold on
%! patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'b', 'linewidth', lw);
%! axis image
%! grid on
%! title "2. Clip polygon"
%!
%! op   = {"Sub -clip", "AND / Intersection", "Exclusive OR", "OR / Union"};
%! for i=1:numel(op)
%!   subplot (6, 4, [12 16]+i);
%!   [opol, npol] = clipPolygon_mrf (pol1, pol2, i-1);
%!   opol = polygon2patch (opol);
%!   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
%!   hold on
%!   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
%!   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
%!         'linewidth', lw);
%!   axis image
%!   grid on
%!   title (sprintf("%d. %s", i+2, op{i}));
%!   axis off
%! endfor
%!
%! subplot (10, 4, 37);
%!   [opol, npol] = clipPolygon_mrf (pol2, pol1, 0);
%!   opol = polygon2patch (opol);
%!   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
%!   hold on
%!   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
%!   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
%!         'linewidth', lw);
%!   axis image
%!   grid on
%!   axis off
%!   title "7. Clip - sub";