From: <jpi...@us...> - 2011-10-06 20:49:06
|
Revision: 8692 http://octave.svn.sourceforge.net/octave/?rev=8692&view=rev Author: jpicarbajal Date: 2011-10-06 20:48:58 +0000 (Thu, 06 Oct 2011) Log Message: ----------- geomatry. Adding boxes and clips Modified Paths: -------------- trunk/octave-forge/main/geometry/geom2d/inst/cbezier2poly.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipRay.m Added Paths: ----------- trunk/octave-forge/main/geometry/geom2d/inst/boxes2d.m trunk/octave-forge/main/geometry/geom2d/inst/clipEdge.m trunk/octave-forge/main/geometry/geom2d/inst/clipLine.m trunk/octave-forge/main/geometry/geom2d/inst/clipPoints.m trunk/octave-forge/main/geometry/geom2d/inst/drawBezierCurve.m trunk/octave-forge/main/geometry/geom2d/inst/drawBox.m Removed Paths: ------------- trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testCliPoints.m trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipEdge.m trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipLine.m trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipPoints.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/boxes2d.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipEdge.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipLine.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipPoints.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/drawBezierCurve.m trunk/octave-forge/main/geometry/matGeom_raw/geom2d/drawBox.m Copied: trunk/octave-forge/main/geometry/geom2d/inst/boxes2d.m (from rev 8687, trunk/octave-forge/main/geometry/matGeom_raw/geom2d/boxes2d.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/boxes2d.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/boxes2d.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -0,0 +1,53 @@ +%% Copyright (c) 2011, INRA +%% 2008-2011, David Legland <dav...@gr...> +%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> +%% +%% All rights reserved. +%% (simplified BSD License) +%% +%% Redistribution and use in source and binary forms, with or without +%% modification, are permitted provided that the following conditions are met: +%% +%% 1. Redistributions of source code must retain the above copyright notice, this +%% list of conditions and the following disclaimer. +%% +%% 2. Redistributions in binary form must reproduce the above copyright notice, +%% this list of conditions and the following disclaimer in the documentation +%% and/or other materials provided with the distribution. +%% +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +%% POSSIBILITY OF SUCH DAMAGE. +%% +%% The views and conclusions contained in the software and documentation are +%% those of the authors and should not be interpreted as representing official +%% policies, either expressed or implied, of copyright holder. + +%% -*- texinfo -*- +%% @deftypefn {Function File} boxes2d () +%% Description of functions operating on bounding boxes. +%% +%% A box is represented as a set of limits in each direction: +%% @example +%% BOX = [XMIN XMAX YMIN YMAX]. +%% @end example +%% @noindent +%% Boxes are used as result of computation for bounding boxes, and to clip +%% shapes. +%% +%% @seealso{clipPoints, clipLine, clipEdge, clipRay, mergeBoxes, +%% intersectBoxes, randomPointInBox, drawBox} +%% @end deftypefn + +function boxes2d(varargin) + help('boxes2d'); +endfunction + Modified: trunk/octave-forge/main/geometry/geom2d/inst/cbezier2poly.m =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/cbezier2poly.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/geom2d/inst/cbezier2poly.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -34,7 +34,7 @@ %% -*- texinfo -*- %% @deftypefn {Function File} {@var{pp} =} cbezier2poly (@var{points}) %% @deftypefnx {Command} {Function File} {[@var{x} @var{y}] =} cbezier2poly (@var{points},@var{t}) -%% Returns the polynomial representation of the cubic Bezier define dby the control points @var{points}. +%% Returns the polynomial representation of the cubic Bezier defined by the control points @var{points}. %% %% With only one input argument, calculates the polynomial @var{pp} of the cubic %% Bezier curve defined by the 4 control points stored in @var{points}. Copied: trunk/octave-forge/main/geometry/geom2d/inst/clipEdge.m (from rev 8687, trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipEdge.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/clipEdge.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/clipEdge.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -0,0 +1,203 @@ +%% Copyright (c) 2011, INRA +%% 2010-2011, David Legland <dav...@gr...> +%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> +%% +%% All rights reserved. +%% (simplified BSD License) +%% +%% Redistribution and use in source and binary forms, with or without +%% modification, are permitted provided that the following conditions are met: +%% +%% 1. Redistributions of source code must retain the above copyright notice, this +%% list of conditions and the following disclaimer. +%% +%% 2. Redistributions in binary form must reproduce the above copyright notice, +%% this list of conditions and the following disclaimer in the documentation +%% and/or other materials provided with the distribution. +%% +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +%% POSSIBILITY OF SUCH DAMAGE. +%% +%% The views and conclusions contained in the software and documentation are +%% those of the authors and should not be interpreted as representing official +%% policies, either expressed or implied, of copyright holder. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{edge2} =} clipEdge (@var{edge}, @var{box}) +%% Clip an edge with a rectangular box. +%% +%% @var{edge}: [x1 y1 x2 y2], +%% @var{box} : [xmin xmax ; ymin ymax] or [xmin xmax ymin ymax]; +%% return : +%% @var{edge2} = [xc1 yc1 xc2 yc2]; +%% +%% If clipping is null, return [0 0 0 0]; +%% +%% if @var{edge} is a [nx4] array, return an [nx4] array, corresponding to each +%% clipped edge. +%% +%% @seealso{edges2d, boxes2d, clipLine} +%% @end deftypefn + +function edge2 = clipEdge(edge, bb) + + % process data input + if size(bb, 1)==2 + bb = bb'; + end + + % get limits of window + xmin = bb(1); + xmax = bb(2); + ymin = bb(3); + ymax = bb(4); + + + % convert window limits into lines + lineX0 = [xmin ymin xmax-xmin 0]; + lineX1 = [xmin ymax xmax-xmin 0]; + lineY0 = [xmin ymin 0 ymax-ymin]; + lineY1 = [xmax ymin 0 ymax-ymin]; + + + % compute outcodes of each vertex + p11 = edge(:,1)<xmin; p21 = edge(:,3)<xmin; + p12 = edge(:,1)>xmax; p22 = edge(:,3)>xmax; + p13 = edge(:,2)<ymin; p23 = edge(:,4)<ymin; + p14 = edge(:,2)>ymax; p24 = edge(:,4)>ymax; + out1 = [p11 p12 p13 p14]; + out2 = [p21 p22 p23 p24]; + + % detect edges totally inside window -> no clip. + inside = sum(out1 | out2, 2)==0; + + % detect edges totally outside window + outside = sum(out1 & out2, 2)>0; + + % select edges not totally outside, and process separately edges totally + % inside window + ind = find(~(inside | outside)); + + + edge2 = zeros(size(edge)); + edge2(inside, :) = edge(inside, :); + + + for i=1:length(ind) + % current edge + iedge = edge(ind(i), :); + + % compute intersection points with each line of bounding window + px0 = intersectLineEdge(lineX0, iedge); + px1 = intersectLineEdge(lineX1, iedge); + py0 = intersectLineEdge(lineY0, iedge); + py1 = intersectLineEdge(lineY1, iedge); + + % create array of points + points = [px0; px1; py0; py1; iedge(1:2); iedge(3:4)]; + + % remove infinite points (edges parallel to box edges) + points = points(all(isfinite(points), 2), :); + + % sort points by x then y + points = sortrows(points); + + % get center positions between consecutive points + centers = (points(2:end,:) + points(1:end-1,:))/2; + + % find the centers (if any) inside window + inside = find( centers(:,1)>=xmin & centers(:,2)>=ymin & ... + centers(:,1)<=xmax & centers(:,2)<=ymax); + + % if multiple segments are inside box, which can happen due to finite + % resolution, only take the longest segment + if length(inside)>1 + % compute delta vectors of the segments + dv = points(inside+1,:) - points(inside,:); + % compute lengths of segments + len = hypot(dv(:,1), dv(:,2)); + % find index of longest segment + [a, I] = max(len); %#ok<ASGLU> + inside = inside(I); + end + + % if one of the center points is inside box, then the according edge + % segment is indide box + if length(inside)==1 + % restore same direction of edge + if iedge(1)>iedge(3) || (iedge(1)==iedge(3) && iedge(2)>iedge(4)) + edge2(i, :) = [points(inside+1,:) points(inside,:)]; + else + edge2(i, :) = [points(inside,:) points(inside+1,:)]; + end + end + + end % end of loop of edges +endfunction + +%!demo +%! bb = [0 100 0 100]; +%! edge = [-10 10 90 110]; +%! ec = clipEdge (edge, bb); +%! +%! drawBox(bb,'color','k'); +%! line(edge([1 3]),edge([2 4]),'color','b'); +%! line(ec([1 3]),ec([2 4]),'color','r','linewidth',2); +%! axis tight +%! v = axis (); +%! axis(v+[0 10 -10 0]) + +%!shared bb +%! bb = [0 100 0 100]; +%!assert (clipEdge([20 30 80 60], bb), [20 30 80 60],1e-6); +%!assert (clipEdge([0 30 80 60], bb), [0 30 80 60],1e-6); +%!assert (clipEdge([0 30 100 60], bb), [0 30 100 60],1e-6); +%!assert (clipEdge([30 0 80 100], bb), [30 0 80 100],1e-6); +%!assert (clipEdge([0 0 100 100], bb), [0 0 100 100],1e-6); +%!assert (clipEdge([0 100 100 0], bb), [0 100 100 0],1e-6); +%!assert (clipEdge([20 60 120 60], bb), [20 60 100 60],1e-6); +%!assert (clipEdge([-20 60 80 60], bb), [0 60 80 60],1e-6); +%!assert (clipEdge([20 60 20 160], bb), [20 60 20 100],1e-6); +%!assert (clipEdge([20 -30 20 60], bb), [20 0 20 60],1e-6); +%!assert (clipEdge([120 30 180 60], bb), [0 0 0 0],1e-6); +%!assert (clipEdge([-20 30 -80 60], bb), [0 0 0 0],1e-6); +%!assert (clipEdge([30 120 60 180], bb), [0 0 0 0],1e-6); +%!assert (clipEdge([30 -20 60 -80], bb), [0 0 0 0],1e-6); +%!assert (clipEdge([-120 110 190 150], bb), [0 0 0 0],1e-6); +%!assert ([50 50 100 50], clipEdge([50 50 150 50], bb),1e-6); +%!assert ([50 50 0 50], clipEdge([50 50 -50 50], bb),1e-6); +%!assert ([50 50 50 100], clipEdge([50 50 50 150], bb),1e-6); +%!assert ([50 50 50 0], clipEdge([50 50 50 -50], bb),1e-6); +%!assert ([80 50 100 70], clipEdge([80 50 130 100], bb),1e-6); +%!assert ([80 50 100 30], clipEdge([80 50 130 0], bb),1e-6); +%!assert ([20 50 0 70], clipEdge([20 50 -30 100], bb),1e-6); +%!assert ([20 50 0 30], clipEdge([20 50 -30 0], bb),1e-6); +%!assert ([50 80 70 100], clipEdge([50 80 100 130], bb),1e-6); +%!assert ([50 80 30 100], clipEdge([50 80 0 130], bb),1e-6); +%!assert ([50 20 70 0], clipEdge([50 20 100 -30], bb),1e-6); +%!assert ([50 20 30 0], clipEdge([50 20 0 -30], bb),1e-6); +%!assert ([100 50 50 50], clipEdge([150 50 50 50], bb),1e-6); +%!assert ([0 50 50 50], clipEdge([-50 50 50 50], bb),1e-6); +%!assert ([50 100 50 50], clipEdge([50 150 50 50], bb),1e-6); +%!assert ([50 0 50 50], clipEdge([50 -50 50 50], bb),1e-6); +%!assert ([100 70 80 50], clipEdge([130 100 80 50], bb),1e-6); +%!assert ([100 30 80 50], clipEdge([130 0 80 50], bb),1e-6); +%!assert ([0 70 20 50], clipEdge([-30 100 20 50], bb),1e-6); +%!assert ([0 30 20 50], clipEdge([-30 0 20 50], bb),1e-6); +%!assert ([70 100 50 80], clipEdge([100 130 50 80], bb),1e-6); +%!assert ([30 100 50 80], clipEdge([0 130 50 80], bb),1e-6); +%!assert ([70 0 50 20], clipEdge([100 -30 50 20], bb),1e-6); +%!assert ([30 0 50 20], clipEdge([0 -30 50 20], bb),1e-6); +%!assert ([0 20 80 100], clipEdge([-10 10 90 110], bb),1e-6); + + + Copied: trunk/octave-forge/main/geometry/geom2d/inst/clipLine.m (from rev 8687, trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipLine.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/clipLine.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/clipLine.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -0,0 +1,210 @@ +%% Copyright (c) 2011, INRA +%% 2007-2011, David Legland <dav...@gr...> +%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> +%% +%% All rights reserved. +%% (simplified BSD License) +%% +%% Redistribution and use in source and binary forms, with or without +%% modification, are permitted provided that the following conditions are met: +%% +%% 1. Redistributions of source code must retain the above copyright notice, this +%% list of conditions and the following disclaimer. +%% +%% 2. Redistributions in binary form must reproduce the above copyright notice, +%% this list of conditions and the following disclaimer in the documentation +%% and/or other materials provided with the distribution. +%% +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +%% POSSIBILITY OF SUCH DAMAGE. +%% +%% The views and conclusions contained in the software and documentation are +%% those of the authors and should not be interpreted as representing official +%% policies, either expressed or implied, of copyright holder. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{edge} =} clipLine (@var{line}, @var{box}) +%% Clip a line with a box. +%% +%% @var{line} is a straight line given as a 4 element row vector: [x0 y0 dx dy], +%% with (x0 y0) being a point of the line and (dx dy) a direction vector, +%% @var{box} is the clipping box, given by its extreme coordinates: +%% [xmin xmax ymin ymax]. +%% The result is given as an edge, defined by the coordinates of its 2 +%% extreme points: [x1 y1 x2 y2]. +%% If line does not intersect the box, [NaN NaN NaN NaN] is returned. +%% +%% Function works also if @var{line} is a Nx4 array, if @var{box} is a Nx4 array, or +%% if both @var{line} and @var{box} are Nx4 arrays. In these cases, @var{edge} is a Nx4 +%% array. +%% +%% Example: +%% +%% @example +%% line = [30 40 10 0]; +%% box = [0 100 0 100]; +%% res = clipLine(line, box) +%% res = +%% 0 40 100 40 +%% @end example +%% +%% @seealso{lines2d, boxes2d, edges2d, clipEdge, clipRay} +%% @end deftypefn + +function edge = clipLine(lin, bb, varargin) + + % adjust size of two input arguments + if size(lin, 1)==1 + lin = repmat(lin, size(bb, 1), 1); + elseif size(bb, 1)==1 + bb = repmat(bb, size(lin, 1), 1); + elseif size(lin, 1) ~= size(bb, 1) + error('bad sizes for input'); + end + + % allocate memory + nbLines = size(lin, 1); + edge = zeros(nbLines, 4); + + % main loop on lines + for i=1:nbLines + % extract limits of the box + xmin = bb(i, 1); + xmax = bb(i, 2); + ymin = bb(i, 3); + ymax = bb(i, 4); + + % use direction vector for box edges similar to direction vector of the + % line in order to reduce computation errors + delta = hypot(lin(i,3), lin(i,4)); + + + % compute intersection with each edge of the box + + % lower edge + px1 = intersectLines(lin(i,:), [xmin ymin delta 0]); + % right edge + px2 = intersectLines(lin(i,:), [xmax ymin 0 delta]); + % upper edge + py1 = intersectLines(lin(i,:), [xmax ymax -delta 0]); + % left edge + py2 = intersectLines(lin(i,:), [xmin ymax 0 -delta]); + + % remove undefined intersections (case of lines parallel to box edges) + points = [px1 ; px2 ; py1 ; py2]; + points = points(isfinite(points(:,1)), :); + + % sort points according to their position on the line + pos = linePosition(points, lin(i,:)); + [pos inds] = sort(pos); %#ok<ASGLU> + points = points(inds, :); + + % create clipped edge by using the two points in the middle + ind = size(points, 1)/2; + inter1 = points(ind,:); + inter2 = points(ind+1,:); + edge(i, 1:4) = [inter1 inter2]; + + % check that middle point of the edge is contained in the box + midX = mean(edge(i, [1 3])); + xOk = xmin <= midX && midX <= xmax; + midY = mean(edge(i, [2 4])); + yOk = ymin <= midY && midY <= ymax; + + % if one of the bounding condition is not met, set edge to NaN + if ~(xOk && yOk) + edge (i,:) = NaN; + end + end +endfunction + +%!demo +%! lin = [30 40 10 0]; +%! bb = [0 100 0 100]; +%! res = clipLine(line, bb) +%! +%! drawBox(bb,'color','k'); +%! line(lin([1 3]),lin([2 4]),'color','b'); +%! line(res([1 3]),res([2 4]),'color','r','linewidth',2); +%! axis tight +%! v = axis (); +%! axis(v+[0 10 -10 0]) + +%!test % inside, to the right % inside, to the left% outside +%! bb = [0 100 0 100]; +%! lin = [30 40 10 0]; +%! edge = [0 40 100 40]; +%! assert (edge, clipLine(lin, bb), 1e-6); +%! lin = [30 40 -10 0]; +%! edge = [100 40 0 40]; +%! assert (edge, clipLine(lin, bb), 1e-6); +%! lin = [30 140 10 0]; +%! assert (sum(isnan(clipLine(lin, bb)))==4); + +%!test % inside, upward % inside, downward % outside +%! bb = [0 100 0 100]; +%! lin = [30 40 0 10]; +%! edge = [30 0 30 100]; +%! assert (edge, clipLine(lin, bb), 1e-6); +%! lin = [30 40 0 -10]; +%! edge = [30 100 30 0]; +%! assert (edge, clipLine(lin, bb), 1e-6); +%! lin = [140 30 0 10]; +%! assert (sum(isnan(clipLine(lin, bb)))==4); + +%!test % inside, top right corner% inside, down right corner % outside +%! bb = [0 100 0 100]; +%! lin = [80 30 10 10]; +%! edge = [50 0 100 50]; +%! assert (edge, clipLine(lin, bb), 1e-6); +%! lin = [20 70 10 10]; +%! edge = [0 50 50 100]; +%! assert (edge, clipLine(lin, bb), 1e-6); +%! lin = [140 -30 10 10]; +%! assert (sum(isnan(clipLine(lin, bb)))==4); +%! lin = [-40 130 10 10]; +%! assert (sum(isnan(clipLine(lin, bb)))==4); + +%!test %multilines % inside, top right corner +%! bb = [0 100 0 100]; +%! lin = [... +%! 80 30 10 10; ... +%! 20 70 10 10; ... +%! 140 -30 10 10; ... +%! -40 130 10 10]; +%! edge = [... +%! 50 0 100 50; ... +%! 0 50 50 100; ... +%! NaN NaN NaN NaN; ... +%! NaN NaN NaN NaN; ... +%! ]; +%! clipped = clipLine(lin, bb); +%! assert (4, size(clipped, 1)); +%! assert (edge(1:2, :), clipped(1:2, :), 1e-6); +%! assert (sum(isnan(clipped(3,:)))==4); +%! assert (sum(isnan(clipped(4,:)))==4); + +%!test % test clipping of horizontal lines % inside, to the right +%! bb = [-1 1 -1 1]*1e10; +%! lin = [3 0 1 2]; +%! D = 1e10; +%! edge = [3-D/2 -D 3+D/2 D]; +%! clipped = clipLine(lin, bb); +%! assert (edge, clipped); + +%!test % inside, to the right +%! bb = [-1 1 -1 1]*100; +%! lin = [3 0 1*1e10 2*1e10]; +%! D = 100; +%! edge = [3-D/2 -D 3+D/2 D]; +%! clipped = clipLine(lin, bb); +%! assert (edge, clipped, 1e-6); Copied: trunk/octave-forge/main/geometry/geom2d/inst/clipPoints.m (from rev 8687, trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipPoints.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/clipPoints.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/clipPoints.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -0,0 +1,101 @@ +%% Copyright (c) 2011, INRA +%% 2008-2011, David Legland <dav...@gr...> +%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> +%% +%% All rights reserved. +%% (simplified BSD License) +%% +%% Redistribution and use in source and binary forms, with or without +%% modification, are permitted provided that the following conditions are met: +%% +%% 1. Redistributions of source code must retain the above copyright notice, this +%% list of conditions and the following disclaimer. +%% +%% 2. Redistributions in binary form must reproduce the above copyright notice, +%% this list of conditions and the following disclaimer in the documentation +%% and/or other materials provided with the distribution. +%% +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +%% POSSIBILITY OF SUCH DAMAGE. +%% +%% The views and conclusions contained in the software and documentation are +%% those of the authors and should not be interpreted as representing official +%% policies, either expressed or implied, of copyright holder. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{points2} =} clipPoints (@var{points}, @var{box}) +%% Clip a set of points by a box. +%% +%% Returns the set @var{points2} which are located inside of the box @var{box}. +%% +%% @seealso{points2d, boxes2d, clipLine, drawPoint} +%% @end deftypefn + +function points = clipPoints(points, bb) + + % get bounding box limits + xmin = bb(1); + xmax = bb(2); + ymin = bb(3); + ymax = bb(4); + + % compute indices of points inside visible area + xOk = points(:,1)>=xmin & points(:,1)<=xmax; + yOk = points(:,2)>=ymin & points(:,2)<=ymax; + + % keep only points inside box + points = points(xOk & yOk, :); + +endfunction + +%!demo +%! points = 2*rand(100,2)-1; +%! bb = [-0.5 0.5 -0.25 0.25]; +%! cpo = clipPoints (points, bb); +%! +%! plot(points(:,1),points(:,2),'xr') +%! hold on +%! drawBox(bb,'color','k') +%! plot(cpo(:,1),cpo(:,2),'*g') +%! hold off + +%!shared bb +%! bb = [0 10 0 20]; + +%!test +%! corners = [0 0;10 0;0 20;10 20]; +%! cornersClipped = clipPoints(corners, bb); +%! assert (4, size(cornersClipped, 1)); +%! assert (corners, cornersClipped, 1e-6); + +%!test +%! borders = [0 5;10 5;5 0;5 20]; +%! bordersClipped = clipPoints(borders, bb); +%! assert (4, size(bordersClipped, 1)); +%! assert (borders, bordersClipped, 1e-6); + +%!test +%! inside = [5 5;5 10;5 15]; +%! insideClipped = clipPoints(inside, bb); +%! assert (size(inside, 1), size(insideClipped, 1)); +%! assert (inside, insideClipped); + +%!test +%! points = [-1 0;11 0;-1 20;11 20;0 -1;0 21;10 -1;10 21]; +%! pointsClipped = clipPoints(points, bb); +%! assert (0, size(pointsClipped, 1)); + +%!test +%! points = [-5 10;0 10;5 10;10 10; 15 10]; +%! pointsClipped = clipPoints(points, bb); +%! assert (3, size(pointsClipped, 1)); +%! assert (points(2:4,:), pointsClipped, 1e-6); Copied: trunk/octave-forge/main/geometry/geom2d/inst/drawBezierCurve.m (from rev 8687, trunk/octave-forge/main/geometry/matGeom_raw/geom2d/drawBezierCurve.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/drawBezierCurve.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/drawBezierCurve.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -0,0 +1,107 @@ +%% Copyright (c) 2011, INRA +%% 2007-2011, David Legland <dav...@gr...> +%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> +%% +%% All rights reserved. +%% (simplified BSD License) +%% +%% Redistribution and use in source and binary forms, with or without +%% modification, are permitted provided that the following conditions are met: +%% +%% 1. Redistributions of source code must retain the above copyright notice, this +%% list of conditions and the following disclaimer. +%% +%% 2. Redistributions in binary form must reproduce the above copyright notice, +%% this list of conditions and the following disclaimer in the documentation +%% and/or other materials provided with the distribution. +%% +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +%% POSSIBILITY OF SUCH DAMAGE. +%% +%% The views and conclusions contained in the software and documentation are +%% those of the authors and should not be interpreted as representing official +%% policies, either expressed or implied, of copyright holder. + +%% -*- texinfo -*- +%% @deftypefn {Function File} drawBezierCurve (@var{points}) +%% @deftypefnx {Command} {Function File} drawBezierCurve (@var{pp}) +%% @deftypefnx {Command} {Function File} drawBezierCurve (@ldots, @var{param}, @var{value}, ...) +%% @deftypefnx {Command} {Function File} {@var{h} =}drawBezierCurve (...) +%% Draw a cubic bezier curve defined by the control points @var{points}. +%% +%% With only one input argument, draws the Bezier curve defined by the 4 control +%% points stored in @var{points}. @var{points} is either a 4-by-2 array +%% (vertical concatenation of point coordinates), or a 1-by-8 array (horizotnal +%% concatenation of point coordinates). The curve could be described by its +%% polynomial (output of @code{cbezier2poly}) @var{pp}, which should be a 2-by-4 +%% array. +%% +%% The optional @var{param}, @var{value} pairs specify additional drawing +%% parameters, see the @code{plot} function for details. The specific parameter +%% 'discretization' with an integer associated value defines the amount of +%% points used to plot the curve. If the output is requiered, the function +%% returns the handle to the created graphic object. +%% +%% @seealso{cbezier2poly, plot} +%% @end deftypefn + +function varargout = drawBezierCurve(points, varargin) + + % default number of discretization steps + N = 64; + + % check if discretization step is specified + if ~isempty(varargin) + [tf idx] = ismember ({'discretization'},{varargin{1:2:end}}); + if ~isempty(idx) + N = varargin{idx+1}; + varargin(idx:idx+1) = []; + end + end + + % parametrization variable for bezier (use N+1 points to have N edges) + t = linspace(0, 1, N+1); + + if any(size(points) ~= [2 4]) + [x y] = cbezier2poly(points,t); + else + % Got a polynomial description + x = polyval(points(1,:),t); + y = polyval(points(2,:),t); + end + + % draw the curve + h = plot(x, y, varargin{:}); + + % eventually return a handle to the created object + if nargout > 0 + varargout = {h}; + end +endfunction + +%!demo +%! points = [0 0; 3 1; -2 1; 1 0]; +%! drawBezierCurve(points); +%! hold on +%! plot(points([1 4],1),points([1 4],2),'go'); +%! plot(points([2 3],1),points([2 3],2),'rs'); +%! line(points([1 2],1),points([1 2],2),'color','k'); +%! line(points([3 4],1),points([3 4],2),'color','k'); +%! h = drawBezierCurve(points, 'discretization', 6, 'color','r'); +%! hold off + +%!shared p +%! p = [0 0; 3 1; -2 1; 1 0]; +%!error(drawBezier()) +%!error(drawBezier ('discretization')) +%!error(drawBezier (p, 'discretization', 'a')) +%!error(drawBezier (p(:))) Copied: trunk/octave-forge/main/geometry/geom2d/inst/drawBox.m (from rev 8687, trunk/octave-forge/main/geometry/matGeom_raw/geom2d/drawBox.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/drawBox.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/drawBox.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -0,0 +1,81 @@ +%% Copyright (c) 2011, INRA +%% 2003-2011, David Legland <dav...@gr...> +%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> +%% +%% All rights reserved. +%% (simplified BSD License) +%% +%% Redistribution and use in source and binary forms, with or without +%% modification, are permitted provided that the following conditions are met: +%% +%% 1. Redistributions of source code must retain the above copyright notice, this +%% list of conditions and the following disclaimer. +%% +%% 2. Redistributions in binary form must reproduce the above copyright notice, +%% this list of conditions and the following disclaimer in the documentation +%% and/or other materials provided with the distribution. +%% +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +%% POSSIBILITY OF SUCH DAMAGE. +%% +%% The views and conclusions contained in the software and documentation are +%% those of the authors and should not be interpreted as representing official +%% policies, either expressed or implied, of copyright holder. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{h} =} drawBox (@var{box}) +%% @deftypefnx {Function File} {@var{h} =} drawBox (@var{box}, @var{param}, @var{value}, ...) +%% Draw a box defined by coordinate extents +%% +%% Draws a box defined by its extent: @var{box} = [@var{xmin} @var{xmax} +%% @var{ymin} @var{ymax}]. Addtional +%% arguments are passed to function @code{plot}. If requested, it returns the +%% handle to the graphics object created. +%% +%% @seealso{drawOrientedBox, drawRect, plot} +%% @end deftypefn + +function varargout = drawBox(box, varargin) + + % default values + xmin = box(:,1); + xmax = box(:,2); + ymin = box(:,3); + ymax = box(:,4); + + nBoxes = size(box, 1); + r = zeros(nBoxes, 1); + + % iterate on boxes + for i = 1:nBoxes + % exract min and max values + tx(1) = xmin(i); + ty(1) = ymin(i); + tx(2) = xmax(i); + ty(2) = ymin(i); + tx(3) = xmax(i); + ty(3) = ymax(i); + tx(4) = xmin(i); + ty(4) = ymax(i); + tx(5) = xmin(i); + ty(5) = ymin(i); + + % display polygon + r(i) = plot(tx, ty, varargin{:}); + end + + % format output + if nargout > 0 + varargout = {r}; + end + +endfunction Deleted: trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testCliPoints.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testCliPoints.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testCliPoints.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,58 +0,0 @@ -function test_suite = testClipPoints(varargin) -%TESTCLIPLINE One-line description here, please. -% output = testClipPoints(input) -% -% Example -% testClipPoints -% -% See also -% -% -% ------ -% Author: David Legland -% e-mail: dav...@gr... -% Created: 2009-04-22, using Matlab 7.7.0.471 (R2008b) -% Copyright 2009 INRA - Cepia Software Platform. - -initTestSuite; - -function testAllInside -% all points inside window, possibly touching edges - -box = [0 10 0 20]; -corners = [0 0;10 0;0 20;10 20]; - -cornersClipped = clipPoints(corners, box); -assertEqual(4, size(cornersClipped, 1)); -assertElementsAlmostEqual(corners, cornersClipped); - -borders = [0 5;10 5;5 0;5 20]; -bordersClipped = clipPoints(borders, box); -assertEqual(4, size(bordersClipped, 1)); -assertElementsAlmostEqual(borders, bordersClipped); - -inside = [5 5;5 10;5 15]; -insideClipped = clipPoints(inside, box); -assertEqual(size(inside, 1), size(insideClipped, 1)); -assertElementsAlmostEqual(inside, insideClipped); - - -function testAllOutside -% all points outside window - -box = [0 10 0 20]; -points = [-1 0;11 0;-1 20;11 20;0 -1;0 21;10 -1;10 21]; - -pointsClipped = clipPoints(points, box); -assertEqual(0, size(pointsClipped, 1)); - - -function testMixed -% all points inside window, possibly touching edges - -box = [0 10 0 20]; -points = [-5 10;0 10;5 10;10 10; 15 10]; - -pointsClipped = clipPoints(points, box); -assertEqual(3, size(pointsClipped, 1)); -assertElementsAlmostEqual(points(2:4,:), pointsClipped); Deleted: trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipEdge.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipEdge.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipEdge.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,100 +0,0 @@ -function test_suite = testClipEdge(varargin) %#ok<STOUT> -%TESTCLIPEDGE One-line description here, please. -% output = testClipEdge(input) -% -% Example -% testClipEdge -% -% See also -% -% -% ------ -% Author: David Legland -% e-mail: dav...@gr... -% Created: 2009-04-22, using Matlab 7.7.0.471 (R2008b) -% Copyright 2009 INRA - Cepia Software Platform. -% Licensed under the terms of the LGPL, see the file "license.txt" - -initTestSuite; - -function testInside %#ok<*DEFNU> -% test edges totally inside window, possibly touching edges - -box = [0 100 0 100]; -assertElementsAlmostEqual(clipEdge([20 30 80 60], box), [20 30 80 60]); -assertElementsAlmostEqual(clipEdge([0 30 80 60], box), [0 30 80 60]); -assertElementsAlmostEqual(clipEdge([0 30 100 60], box), [0 30 100 60]); -assertElementsAlmostEqual(clipEdge([30 0 80 100], box), [30 0 80 100]); -assertElementsAlmostEqual(clipEdge([0 0 100 100], box), [0 0 100 100]); -assertElementsAlmostEqual(clipEdge([0 100 100 0], box), [0 100 100 0]); - -function testClip -% test edges totally inside window, possibly touching edges - -box = [0 100 0 100]; -assertElementsAlmostEqual(clipEdge([20 60 120 60], box), [20 60 100 60]); -assertElementsAlmostEqual(clipEdge([-20 60 80 60], box), [0 60 80 60]); -assertElementsAlmostEqual(clipEdge([20 60 20 160], box), [20 60 20 100]); -assertElementsAlmostEqual(clipEdge([20 -30 20 60], box), [20 0 20 60]); - - -function testOutside -% test edges totally outside window - -box = [0 100 0 100]; -assertElementsAlmostEqual(clipEdge([120 30 180 60], box), [0 0 0 0]); -assertElementsAlmostEqual(clipEdge([-20 30 -80 60], box), [0 0 0 0]); -assertElementsAlmostEqual(clipEdge([30 120 60 180], box), [0 0 0 0]); -assertElementsAlmostEqual(clipEdge([30 -20 60 -80], box), [0 0 0 0]); -assertElementsAlmostEqual(clipEdge([-120 110 190 150], box), [0 0 0 0]); - -function testClipLast -% test edges clipped at last extremity, with orthogonal edges - -box = [0 100 0 100]; -assertAlmostEqual([50 50 100 50], clipEdge([50 50 150 50], box)); -assertAlmostEqual([50 50 0 50], clipEdge([50 50 -50 50], box)); -assertAlmostEqual([50 50 50 100], clipEdge([50 50 50 150], box)); -assertAlmostEqual([50 50 50 0], clipEdge([50 50 50 -50], box)); - -function testClipLastDiag -% test edges clipped at last extremity, with diagonal edges - -box = [0 100 0 100]; -assertAlmostEqual([80 50 100 70], clipEdge([80 50 130 100], box)); -assertAlmostEqual([80 50 100 30], clipEdge([80 50 130 0], box)); -assertAlmostEqual([20 50 0 70], clipEdge([20 50 -30 100], box)); -assertAlmostEqual([20 50 0 30], clipEdge([20 50 -30 0], box)); -assertAlmostEqual([50 80 70 100], clipEdge([50 80 100 130], box)); -assertAlmostEqual([50 80 30 100], clipEdge([50 80 0 130], box)); -assertAlmostEqual([50 20 70 0], clipEdge([50 20 100 -30], box)); -assertAlmostEqual([50 20 30 0], clipEdge([50 20 0 -30], box)); - -function testClipFirst -% test edges clipped at first extremity, with orthogonal edges - -box = [0 100 0 100]; -assertAlmostEqual([100 50 50 50], clipEdge([150 50 50 50], box)); -assertAlmostEqual([0 50 50 50], clipEdge([-50 50 50 50], box)); -assertAlmostEqual([50 100 50 50], clipEdge([50 150 50 50], box)); -assertAlmostEqual([50 0 50 50], clipEdge([50 -50 50 50], box)); - -function testClipFirstDiag -% test edges clipped at last extremity, with diagonal edges - -box = [0 100 0 100]; -assertAlmostEqual([100 70 80 50], clipEdge([130 100 80 50], box)); -assertAlmostEqual([100 30 80 50], clipEdge([130 0 80 50], box)); -assertAlmostEqual([0 70 20 50], clipEdge([-30 100 20 50], box)); -assertAlmostEqual([0 30 20 50], clipEdge([-30 0 20 50], box)); -assertAlmostEqual([70 100 50 80], clipEdge([100 130 50 80], box)); -assertAlmostEqual([30 100 50 80], clipEdge([0 130 50 80], box)); -assertAlmostEqual([70 0 50 20], clipEdge([100 -30 50 20], box)); -assertAlmostEqual([30 0 50 20], clipEdge([0 -30 50 20], box)); - -function testClipBoth -% test edges clipped at both extremities - -box = [0 100 0 100]; -assertAlmostEqual([0 20 80 100], clipEdge([-10 10 90 110], box)); - Deleted: trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipLine.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipLine.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipLine.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,126 +0,0 @@ -function test_suite = testClipLine(varargin) %#ok<STOUT> -% Tests the function 'clipLine' -% output = testClipLine(input) -% -% Example -% testClipLine -% -% See also -% -% -% ------ -% Author: David Legland -% e-mail: dav...@gr... -% Created: 2009-04-22, using Matlab 7.7.0.471 (R2008b) -% Copyright 2009 INRA - Cepia Software Platform. -% Licensed under the terms of the LGPL, see the file "license.txt" - -initTestSuite; - -function testHoriz %#ok<*DEFNU> -% test clipping of horizontal lines - -box = [0 100 0 100]; - -% inside, to the right -line = [30 40 10 0]; -edge = [0 40 100 40]; -assertElementsAlmostEqual(edge, clipLine(line, box)); - -% inside, to the left -line = [30 40 -10 0]; -edge = [100 40 0 40]; -assertElementsAlmostEqual(edge, clipLine(line, box)); - -% outside -line = [30 140 10 0]; -assertTrue(sum(isnan(clipLine(line, box)))==4); - -function testVert -% test clipping of vertical lines - -box = [0 100 0 100]; - -% inside, upward -line = [30 40 0 10]; -edge = [30 0 30 100]; -assertElementsAlmostEqual(edge, clipLine(line, box)); - -% inside, downward -line = [30 40 0 -10]; -edge = [30 100 30 0]; -assertElementsAlmostEqual(edge, clipLine(line, box)); - -% outside -line = [140 30 0 10]; -assertTrue(sum(isnan(clipLine(line, box)))==4); - -function testDiagUp -% test clipping of upward diagonal lines - -box = [0 100 0 100]; - -% inside, top right corner -line = [80 30 10 10]; -edge = [50 0 100 50]; -assertElementsAlmostEqual(edge, clipLine(line, box)); - -% inside, down right corner -line = [20 70 10 10]; -edge = [0 50 50 100]; -assertElementsAlmostEqual(edge, clipLine(line, box)); - -% outside -line = [140 -30 10 10]; -assertTrue(sum(isnan(clipLine(line, box)))==4); - -line = [-40 130 10 10]; -assertTrue(sum(isnan(clipLine(line, box)))==4); - - -function testMultiLines - -box = [0 100 0 100]; - -% inside, top right corner -line = [... - 80 30 10 10; ... - 20 70 10 10; ... - 140 -30 10 10; ... - -40 130 10 10]; -edge = [... - 50 0 100 50; ... - 0 50 50 100; ... - NaN NaN NaN NaN; ... - NaN NaN NaN NaN; ... - ]; - -clipped = clipLine(line, box); -assertEqual(4, size(clipped, 1)); -assertElementsAlmostEqual(edge(1:2, :), clipped(1:2, :)); -assertTrue(sum(isnan(clipped(3,:)))==4); -assertTrue(sum(isnan(clipped(4,:)))==4); - -function testBigBox -% test clipping of horizontal lines - -box = [-1 1 -1 1]*1e10; - -% inside, to the right -line = [3 0 1 2]; -D = 1e10; -edge = [3-D/2 -D 3+D/2 D]; -clipped = clipLine(line, box); -assertElementsAlmostEqual(edge, clipped); - -function testBigLine -% test clipping of horizontal lines - -box = [-1 1 -1 1]*100; - -% inside, to the right -line = [3 0 1*1e10 2*1e10]; -D = 100; -edge = [3-D/2 -D 3+D/2 D]; -clipped = clipLine(line, box); -assertElementsAlmostEqual(edge, clipped); Deleted: trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipPoints.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipPoints.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/Tests/geom2d/testClipPoints.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,58 +0,0 @@ -function test_suite = testClipPoints(varargin) -%TESTCLIPLINE One-line description here, please. -% output = testClipPoints(input) -% -% Example -% testClipPoints -% -% See also -% -% -% ------ -% Author: David Legland -% e-mail: dav...@gr... -% Created: 2009-04-22, using Matlab 7.7.0.471 (R2008b) -% Copyright 2009 INRA - Cepia Software Platform. - -initTestSuite; - -function testAllInside -% all points inside window, possibly touching edges - -box = [0 10 0 20]; -corners = [0 0;10 0;0 20;10 20]; - -cornersClipped = clipPoints(corners, box); -assertEqual(4, size(cornersClipped, 1)); -assertElementsAlmostEqual(corners, cornersClipped); - -borders = [0 5;10 5;5 0;5 20]; -bordersClipped = clipPoints(borders, box); -assertEqual(4, size(bordersClipped, 1)); -assertElementsAlmostEqual(borders, bordersClipped); - -inside = [5 5;5 10;5 15]; -insideClipped = clipPoints(inside, box); -assertEqual(size(inside, 1), size(insideClipped, 1)); -assertElementsAlmostEqual(inside, insideClipped); - - -function testAllOutside -% all points outside window - -box = [0 10 0 20]; -points = [-1 0;11 0;-1 20;11 20;0 -1;0 21;10 -1;10 21]; - -pointsClipped = clipPoints(points, box); -assertEqual(0, size(pointsClipped, 1)); - - -function testMixed -% all points inside window, possibly touching edges - -box = [0 10 0 20]; -points = [-5 10;0 10;5 10;10 10; 15 10]; - -pointsClipped = clipPoints(points, box); -assertEqual(3, size(pointsClipped, 1)); -assertElementsAlmostEqual(points(2:4,:), pointsClipped); Deleted: trunk/octave-forge/main/geometry/matGeom_raw/geom2d/boxes2d.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/geom2d/boxes2d.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/geom2d/boxes2d.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,55 +0,0 @@ -%% Copyright (c) 2011, INRA -%% 2007-2011, David Legland <dav...@gr...> -%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> -%% -%% All rights reserved. -%% (simplified BSD License) -%% -%% Redistribution and use in source and binary forms, with or without -%% modification, are permitted provided that the following conditions are met: -%% -%% 1. Redistributions of source code must retain the above copyright notice, this -%% list of conditions and the following disclaimer. -%% -%% 2. Redistributions in binary form must reproduce the above copyright notice, -%% this list of conditions and the following disclaimer in the documentation -%% and/or other materials provided with the distribution. -%% -%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -%% POSSIBILITY OF SUCH DAMAGE. -%% -%% The views and conclusions contained in the software and documentation are -%% those of the authors and should not be interpreted as representing official -%% policies, either expressed or implied, of copyright holder. - - -function boxes2d(varargin) -%BOXES2D Description of functions operating on bounding boxes -% -% A box is represented as a set of limits in each direction: -% BOX = [XMIN XMAX YMIN YMAX]. -% -% Boxes are used as result of computation for bounding boxes, and to clip -% shapes. -% -% See also -% clipPoints, clipLine, clipEdge, clipRay -% mergeBoxes, intersectBoxes, randomPointInBox -% -% -% ------ -% Author: David Legland -% e-mail: dav...@gr... -% Created: 2008-10-13, using Matlab 7.4.0.287 (R2007a) -% Copyright 2010 INRA - Cepia Software Platform. - -help('boxes2d'); Deleted: trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipEdge.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipEdge.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipEdge.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,157 +0,0 @@ -%% Copyright (c) 2011, INRA -%% 2007-2011, David Legland <dav...@gr...> -%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> -%% -%% All rights reserved. -%% (simplified BSD License) -%% -%% Redistribution and use in source and binary forms, with or without -%% modification, are permitted provided that the following conditions are met: -%% -%% 1. Redistributions of source code must retain the above copyright notice, this -%% list of conditions and the following disclaimer. -%% -%% 2. Redistributions in binary form must reproduce the above copyright notice, -%% this list of conditions and the following disclaimer in the documentation -%% and/or other materials provided with the distribution. -%% -%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -%% POSSIBILITY OF SUCH DAMAGE. -%% -%% The views and conclusions contained in the software and documentation are -%% those of the authors and should not be interpreted as representing official -%% policies, either expressed or implied, of copyright holder. - - -function edge2 = clipEdge(edge, box) -%CLIPEDGE Clip an edge with a rectangular box -% -% EDGE2 = clipEdge(EDGE, BOX); -% EDGE: [x1 y1 x2 y2], -% BOX : [xmin xmax ; ymin ymax] or [xmin xmax ymin ymax]; -% return : -% EDGE2 = [xc1 yc1 xc2 yc2]; -% -% If clipping is null, return [0 0 0 0]; -% -% if EDGE is a [nx4] array, return an [nx4] array, corresponding to each -% clipped edge. -% -% See also -% edges2d, boxes2d, clipLine -% -% --------- -% author : David Legland -% created the 14/05/2005. -% Copyright 2010 INRA - Cepia Software Platform. - -% HISTORY -% 2007-01-08 sort points according to position on edge, not to x coord -% -> this allows to return edges with same orientation a source, and -% to keep first or end points at the same position if their are not -% clipped. -% 01/10/2010 fix bug due to precision, thanks to Reto Zingg. - -% process data input -if size(box, 1)==2 - box = box'; -end - -% get limits of window -xmin = box(1); -xmax = box(2); -ymin = box(3); -ymax = box(4); - - -% convert window limits into lines -lineX0 = [xmin ymin xmax-xmin 0]; -lineX1 = [xmin ymax xmax-xmin 0]; -lineY0 = [xmin ymin 0 ymax-ymin]; -lineY1 = [xmax ymin 0 ymax-ymin]; - - -% compute outcodes of each vertex -p11 = edge(:,1)<xmin; p21 = edge(:,3)<xmin; -p12 = edge(:,1)>xmax; p22 = edge(:,3)>xmax; -p13 = edge(:,2)<ymin; p23 = edge(:,4)<ymin; -p14 = edge(:,2)>ymax; p24 = edge(:,4)>ymax; -out1 = [p11 p12 p13 p14]; -out2 = [p21 p22 p23 p24]; - -% detect edges totally inside window -> no clip. -inside = sum(out1 | out2, 2)==0; - -% detect edges totally outside window -outside = sum(out1 & out2, 2)>0; - -% select edges not totally outside, and process separately edges totally -% inside window -ind = find(~(inside | outside)); - - -edge2 = zeros(size(edge)); -edge2(inside, :) = edge(inside, :); - - -for i=1:length(ind) - % current edge - iedge = edge(ind(i), :); - - % compute intersection points with each line of bounding window - px0 = intersectLineEdge(lineX0, iedge); - px1 = intersectLineEdge(lineX1, iedge); - py0 = intersectLineEdge(lineY0, iedge); - py1 = intersectLineEdge(lineY1, iedge); - - % create array of points - points = [px0; px1; py0; py1; iedge(1:2); iedge(3:4)]; - - % remove infinite points (edges parallel to box edges) - points = points(all(isfinite(points), 2), :); - - % sort points by x then y - points = sortrows(points); - - % get center positions between consecutive points - centers = (points(2:end,:) + points(1:end-1,:))/2; - - % find the centers (if any) inside window - inside = find( centers(:,1)>=xmin & centers(:,2)>=ymin & ... - centers(:,1)<=xmax & centers(:,2)<=ymax); - - % if multiple segments are inside box, which can happen due to finite - % resolution, only take the longest segment - if length(inside)>1 - % compute delta vectors of the segments - dv = points(inside+1,:) - points(inside,:); - % compute lengths of segments - len = hypot(dv(:,1), dv(:,2)); - % find index of longest segment - [a, I] = max(len); %#ok<ASGLU> - inside = inside(I); - end - - % if one of the center points is inside box, then the according edge - % segment is indide box - if length(inside)==1 - % restore same direction of edge - if iedge(1)>iedge(3) || (iedge(1)==iedge(3) && iedge(2)>iedge(4)) - edge2(i, :) = [points(inside+1,:) points(inside,:)]; - else - edge2(i, :) = [points(inside,:) points(inside+1,:)]; - end - end - -end % end of loop of edges - - Deleted: trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipLine.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipLine.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipLine.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,138 +0,0 @@ -%% Copyright (c) 2011, INRA -%% 2007-2011, David Legland <dav...@gr...> -%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> -%% -%% All rights reserved. -%% (simplified BSD License) -%% -%% Redistribution and use in source and binary forms, with or without -%% modification, are permitted provided that the following conditions are met: -%% -%% 1. Redistributions of source code must retain the above copyright notice, this -%% list of conditions and the following disclaimer. -%% -%% 2. Redistributions in binary form must reproduce the above copyright notice, -%% this list of conditions and the following disclaimer in the documentation -%% and/or other materials provided with the distribution. -%% -%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -%% POSSIBILITY OF SUCH DAMAGE. -%% -%% The views and conclusions contained in the software and documentation are -%% those of the authors and should not be interpreted as representing official -%% policies, either expressed or implied, of copyright holder. - - -function edge = clipLine(line, box, varargin) -%CLIPLINE Clip a line with a box -% -% EDGE = clipLine(LINE, BOX); -% LINE is a straight line given as a 4 element row vector: [x0 y0 dx dy], -% with (x0 y0) being a point of the line and (dx dy) a direction vector, -% BOX is the clipping box, given by its extreme coordinates: -% [xmin xmax ymin ymax]. -% The result is given as an edge, defined by the coordinates of its 2 -% extreme points: [x1 y1 x2 y2]. -% If line does not intersect the box, [NaN NaN NaN NaN] is returned. -% -% Function works also if LINE is a Nx4 array, if BOX is a Nx4 array, or -% if both LINE and BOX are Nx4 arrays. In these cases, EDGE is a Nx4 -% array. -% -% -% Example -% line = [30 40 10 0]; -% box = [0 100 0 100]; -% res = clipLine(line, box) -% res = -% 0 40 100 40 -% -% See also: -% lines2d, boxes2d, edges2d -% clipEdge, clipRay -% -% ------ -% Author: David Legland -% e-mail: dav...@gr... -% Created: 2007-08-27, using Matlab 7.4.0.287 (R2007a) -% Copyright 2010 INRA - Cepia Software Platform. - -% HISTORY -% 2010-05-16 rewrite using intersectLines, add precision management -% 2010-08-03 fix bugs (thanks to Reto Zingg) -% 2010-08-06 remove management of EPS by checking edge midpoint (thanks -% again to Reto Zingg) - -% adjust size of two input arguments -if size(line, 1)==1 - line = repmat(line, size(box, 1), 1); -elseif size(box, 1)==1 - box = repmat(box, size(line, 1), 1); -elseif size(line, 1) ~= size(box, 1) - error('bad sizes for input'); -end - -% allocate memory -nbLines = size(line, 1); -edge = zeros(nbLines, 4); - -% main loop on lines -for i=1:nbLines - % extract limits of the box - xmin = box(i, 1); - xmax = box(i, 2); - ymin = box(i, 3); - ymax = box(i, 4); - - % use direction vector for box edges similar to direction vector of the - % line in order to reduce computation errors - delta = hypot(line(i,3), line(i,4)); - - - % compute intersection with each edge of the box - - % lower edge - px1 = intersectLines(line(i,:), [xmin ymin delta 0]); - % right edge - px2 = intersectLines(line(i,:), [xmax ymin 0 delta]); - % upper edge - py1 = intersectLines(line(i,:), [xmax ymax -delta 0]); - % left edge - py2 = intersectLines(line(i,:), [xmin ymax 0 -delta]); - - % remove undefined intersections (case of lines parallel to box edges) - points = [px1 ; px2 ; py1 ; py2]; - points = points(isfinite(points(:,1)), :); - - % sort points according to their position on the line - pos = linePosition(points, line(i,:)); - [pos inds] = sort(pos); %#ok<ASGLU> - points = points(inds, :); - - % create clipped edge by using the two points in the middle - ind = size(points, 1)/2; - inter1 = points(ind,:); - inter2 = points(ind+1,:); - edge(i, 1:4) = [inter1 inter2]; - - % check that middle point of the edge is contained in the box - midX = mean(edge(i, [1 3])); - xOk = xmin <= midX && midX <= xmax; - midY = mean(edge(i, [2 4])); - yOk = ymin <= midY && midY <= ymax; - - % if one of the bounding condition is not met, set edge to NaN - if ~(xOk && yOk) - edge (i,:) = NaN; - end -end - Deleted: trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipPoints.m =================================================================== --- trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipPoints.m 2011-10-06 18:57:03 UTC (rev 8691) +++ trunk/octave-forge/main/geometry/matGeom_raw/geom2d/clipPoints.m 2011-10-06 20:48:58 UTC (rev 8692) @@ -1,63 +0,0 @@ -%% Copyright (c) 2011, INRA -%% 2007-2011, David Legland <dav...@gr...> -%% 2011 Adapted to Octave by Juan Pablo Carbajal <car...@if...> -%% -%% All rights reserved. -%% (simplified BSD License) -%% -%% Redistribution and use in source and binary forms, with or without -%% modification, are permitted provided that the following conditions are met: -%% -%% 1. Redistributions of source code must retain the above copyright notice, this -%% list of conditions and the following disclaimer. -%% -%% 2. Redistributions in binary form must reproduce the above copyright notice, -%% this list of conditions and the following disclaimer in the documentation -%% and/or other materials provided with the distribution. -%% -%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -%% ARE DISC... [truncated message content] |