1 ## Copyright (C) 2011, José Luis García Pallero, <jgpallero@gmail.com>
3 ## This file is part of OctCLIP.
5 ## OctCLIP is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## This program is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn{Function File}{[@var{X},@var{Y},@var{nPol},@var{nInt},@var{nPert}] =}_oc_polybool(@var{sub},@var{clip},@var{op})
21 ## @deftypefnx{Function File}{[@var{X},@var{Y},@var{nPol},@var{nInt},@var{nPert}] =}_oc_polybool(@var{sub},@var{clip})
23 ## This function performs boolean operations between two polygons using the
24 ## Greiner-Hormann algorithm (http://davis.wpi.edu/~matt/courses/clipping/).
26 ## @var{sub} is a two column matrix containing the X and Y coordinates of the
27 ## vertices for the subject polygon.
29 ## @var{clip} is a two column matrix containing the X and Y coordinates of the
30 ## vertices for the clipper polygon.
32 ## @var{op} is a text string containing the operation to perform between
33 ## @var{sub} and @var{clip}. Possible values are:
37 ## Intersection of @var{sub} and @var{clip} (value by default).
39 ## Union of @var{subt} and @var{clip}.
41 ## Operation @var{sub} - @var{clip}.
43 ## Operation of @var{clip} - @var{sub}.
46 ## For the matrices @var{sub} and @var{clip}, the first point is not needed to
47 ## be repeated at the end (but is permitted). Pairs of (NaN,NaN) coordinates in
48 ## @var{sub} and/or @var{clip} are omitted.
50 ## @var{X} is a column vector containing the X coordinates of the vertices for.
51 ## resultant polygon(s).
53 ## @var{Y} is a column vector containing the Y coordinates of the vertices for.
54 ## resultant polygon(s).
56 ## @var{nPol} is the number of output polygons.
58 ## @var{nInt} is the number of intersections between @var{sub} and @var{clip}.
60 ## @var{nPert} is the number of perturbed points of the @var{clip} polygon in
61 ## any particular case (points in the oborder of the other polygon) occurs see
62 ## http://davis.wpi.edu/~matt/courses/clipping/ for details.
68 function [X,Y,nPol,nInt,nPert] = oc_polybool(sub,clip,op)
71 functionName = 'oc_polybool';
75 %*******************************************************************************
76 %NUMBER OF INPUT ARGUMENTS CHECKING
77 %*******************************************************************************
79 %number of input arguments checking
80 if (nargin<minArg)||(nargin>maxArg)
81 error(['Incorrect number of input arguments (%d)\n\t ',...
82 'Correct number of input arguments = %d or %d'],...
83 nargin,minArg,maxArg);
85 %check if we omit the op argument
91 %*******************************************************************************
92 %INPUT ARGUMENTS CHECKING
93 %*******************************************************************************
95 %checking input arguments
96 [op] = checkInputArguments(sub,clip,op);
99 error('\n\tIn function %s:\n\t -%s ',functionName,lasterr);
102 %*******************************************************************************
104 %*******************************************************************************
107 %calling oct function
108 [X,Y,nPol,nInt,nPert] = _oc_polybool(sub,clip,op);
111 error('\n\tIn function %s:\n\tIn function %s ',functionName,lasterr);
117 %*******************************************************************************
119 %*******************************************************************************
124 function [outOp] = checkInputArguments(sub,clip,inOp)
126 %sub must be matrix type
129 [rowSub,colSub] = size(sub);
131 error('The first input argument is not numeric');
133 %clip must be matrix type
136 [rowClip,colClip] = size(clip);
138 error('The second input argument is not numeric');
141 if (colSub~=2)||(colClip~=2)
142 error('The columns of input arguments must be 2');
144 %operation must be a text string
146 error('The third input argument is not a text string');
149 outOp = toupper(inOp);
151 if (~strcmp(outOp,'AND'))&&(~strcmp(outOp,'OR'))&& ...
152 (~strcmp(outOp,'AB'))&&(~strcmp(outOp,'BA'))
153 error('The third input argument is not correct');
160 %*****END OF FUNCIONS*****
165 %*****FUNCTION TESTS*****
170 %tests for input arguments
172 %!error(oc_polybool(1,2,3,4))
173 %!error(oc_polybool('string',2,3))
174 %!error(oc_polybool(1,'string',3))
175 %!error(oc_polybool(1,2,3))
206 %! %limits for the plots
207 %! clXLim = [1.5 11.75];
208 %! clYLim = [0.5 8.50];
209 %! %compute intersection
210 %! [clXI,clYI] = oc_polybool(clSub,clClip,'and');
212 %! [clXU,clYU] = oc_polybool(clSub,clClip,'or');
214 %! [clXA,clYA] = oc_polybool(clSub,clClip,'ab');
216 %! [clXB,clYB] = oc_polybool(clSub,clClip,'ba');
217 %! %plot window for intersection
219 %! plot(clXI,clYI,'r.-','markersize',10,'linewidth',3,clSub(:,1),clSub(:,2),...
220 %! clClip(:,1),clClip(:,2));
224 %! title('OctCLIP intersection');
225 %! legend('Intersection','Subject polygon','Clipper polygon',...
226 %! 'location','southeast');
227 %! %plot window for union
229 %! plot(clXU,clYU,'r.-','markersize',10,'linewidth',3,clSub(:,1),clSub(:,2),...
230 %! clClip(:,1),clClip(:,2));
234 %! title('OctCLIP union');
235 %! legend('Union','Subject polygon','Clipper polygon','location','southeast');
236 %! %plot window for A-B
238 %! plot(clXA,clYA,'r.-','markersize',10,'linewidth',3,clSub(:,1),clSub(:,2),...
239 %! clClip(:,1),clClip(:,2));
243 %! title('OctCLIP A-B');
244 %! legend('A-B','Subject polygon','Clipper polygon','location','southeast');
245 %! %plot window for B-A
247 %! plot(clXB,clYB,'r.-','markersize',10,'linewidth',3,clSub(:,1),clSub(:,2),...
248 %! clClip(:,1),clClip(:,2));
252 %! title('OctCLIP B-A');
253 %! legend('B-A','Subject polygon','Clipper polygon','location','southeast');
255 %! disp('Press ENTER to continue ...');
257 %! %kill and close the plot window
264 %*****END OF TESTS*****