From: <jpi...@us...> - 2012-04-15 16:42:08
|
Revision: 10232 http://octave.svn.sourceforge.net/octave/?rev=10232&view=rev Author: jpicarbajal Date: 2012-04-15 16:42:01 +0000 (Sun, 15 Apr 2012) Log Message: ----------- geometry: updating all functions related to polygon simplification Modified Paths: -------------- trunk/octave-forge/main/geometry/INDEX trunk/octave-forge/main/geometry/NEWS trunk/octave-forge/main/geometry/inst/geom2d/distancePointEdge.m trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolyline.m trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m Modified: trunk/octave-forge/main/geometry/INDEX =================================================================== --- trunk/octave-forge/main/geometry/INDEX 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/INDEX 2012-04-15 16:42:01 UTC (rev 10232) @@ -117,6 +117,7 @@ 2D Polygons drawPolygon simplifypolygon + simplifypolyline oc_polybool 2D Piecewise polynomial shapes polygon2shape Modified: trunk/octave-forge/main/geometry/NEWS =================================================================== --- trunk/octave-forge/main/geometry/NEWS 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/NEWS 2012-04-15 16:42:01 UTC (rev 10232) @@ -1,7 +1,7 @@ Summary of important user-visible changes for releases of the geometry package =============================================================================== -geometry-1.4.2 Release Date: 2012-XX-XX Release Manager: Juan Pablo Carbajal +geometry-1.5.0 Release Date: 2012-XX-XX Release Manager: Juan Pablo Carbajal =============================================================================== * Added functions: Modified: trunk/octave-forge/main/geometry/inst/geom2d/distancePointEdge.m =================================================================== --- trunk/octave-forge/main/geometry/inst/geom2d/distancePointEdge.m 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/inst/geom2d/distancePointEdge.m 2012-04-15 16:42:01 UTC (rev 10232) @@ -32,8 +32,8 @@ %% -*- texinfo -*- %% @deftypefn {Function File} {@var{dist} = } distancePointEdge (@var{point}, @var{edge}) -%% @deftypefnx {Function File} {@var{dist} = } distancePointEdge (@dots, @var{opt}) -%% @deftypefnx {Function File} {[@var{dist} @var{pos}]= } distancePointEdge (@dots) +%% @deftypefnx {Function File} {@var{dist} = } distancePointEdge (@dots{}, @var{opt}) +%% @deftypefnx {Function File} {[@var{dist} @var{pos}]= } distancePointEdge (@dots{}) %% Minimum distance between a point and an edge %% %% Return the euclidean distance between edge @var{edge} and point @var{point}. Modified: trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolyline.m =================================================================== --- trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolyline.m 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolyline.m 2012-04-15 16:42:01 UTC (rev 10232) @@ -27,7 +27,7 @@ %% @strong{Parameters} %% @table @samp %% @item 'Nmax' -%% Maximum number of vertices. Default value @code{100}. +%% Maximum number of vertices. Default value @code{1e3}. %% @item 'Tol' %% Tolerance for the error criteria. Default value @code{1e-4}. %% @item 'MaxIter' @@ -47,7 +47,7 @@ # --- Parse arguments --- # parser = inputParser (); parser.FunctionName = "simplifypolyline"; - parser = addParamValue (parser,'Nmax', 100, @(x)x>0); + parser = addParamValue (parser,'Nmax', 1e3, @(x)x>0); toldef = 1e-4;%max(sumsq(diff(pline_o),2))*2; parser = addParamValue (parser,'Tol', toldef, @(x)x>0); parser = addParamValue (parser,'MaxIter', 100, @(x)x>0); @@ -58,8 +58,8 @@ MaxIter = parser.Results.MaxIter; clear parser toldef - msg = ["Maximum number of points reached with maximal error %g." ... - " Increase '%s' if the result is not satisfactory."]; + msg = ["simplifypolyline: Maximum number of points reached with maximum error %g." ... + " Increase %s if the result is not satisfactory."]; # ------ # [N dim] = size(pline_o); @@ -79,13 +79,14 @@ idx = sort(idx); if length(idx) >= Nmax - warning('geometry:MayBeWrongOutput', sprintf(msg,dist,'Nmax')); + %% TODO remove extra points + warning('geometry:MayBeWrongOutput', sprintf(msg,max(dist),'Nmax')); break; end end if iter == MaxIter - warning('geometry:MayBeWrongOutput', sprintf(msg,dist,'MaxIter')); + warning('geometry:MayBeWrongOutput', sprintf(msg,max(dist),'MaxIter')); end pline = pline_o(idx,:); Modified: trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m 2012-04-15 16:42:01 UTC (rev 10232) @@ -15,7 +15,7 @@ %% -*- texinfo -*- %% @deftypefn {Function File} {@var{polyline} = } curve2polyline (@var{curve}) -%% @deftypefnx {Function File} {@var{polyline} = } curve2polyline (@dots,@var{property},@var{value},@dots) +%% @deftypefnx {Function File} {@var{polyline} = } curve2polyline (@dots{},@var{property},@var{value},@dots{}) %% Adaptive sampling of a parametric curve. %% %% The @var{curve} is described as a 2-by-N matrix. Rows correspond to the @@ -45,6 +45,7 @@ %% Thursday, April 12 2012 -- JuanPi function [polyline t bump]= curve2polyline (curve, varargin) +%% TODO make tolerance relative to the "diameter" of the curve. # --- Parse arguments --- # parser = inputParser (); @@ -58,7 +59,7 @@ tol = parser.Results.Tol; MaxIter = parser.Results.MaxIter; - clear parser + clear parser toldef # ------ # t = [0; 1]; Modified: trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m 2012-04-15 16:42:01 UTC (rev 10232) @@ -15,25 +15,23 @@ %% -*- texinfo -*- %% @deftypefn {Function File} {@var{polygon} = } shape2polygon (@var{shape}) -%% @deftypefnx {Function File} {@var{polygon} = } shape2polygon (@var{shape},@var{N}) +%% @deftypefnx {Function File} {@var{polygon} = } shape2polygon (@dots{},@var{property},@var{value},@dots{}) %% Transforms a 2D shape described by piecewise smooth polynomials into a polygon. %% %% @var{shape} is a n-by-1 cell where each element is a pair of polynomials %% compatible with polyval. %% @var{polygon} is a k-by-2 matrix, where each row represents a vertex. -%% @var{N} defines the number of points to be used in non-straight edges. +%% The property-value pairs are passed to @code{curve2polyline}. %% -%% @seealso{polygon2shape, drawPolygon} +%% @seealso{polygon2shape, curve2poyline} %% @end deftypefn -function polygon = shape2polygon (shape, N=16) +function polygon = shape2polygon (shape, varargin) polygon = cell2mat ( ... - cellfun (@(x) func (x,N), shape,'UniformOutput',false) ); + cellfun (@(x) curve2polyline(x,varargin{:}), shape,'UniformOutput',false) ); + polygon = simplifypolygon(polygon); - %% TODO simply the polygon based on curvature -% polygon = unique (polygon, 'rows'); - if size (polygon, 1) == 1 polygon(2,1) = polyval (shape{1}(1,:), 1); polygon(2,2) = polyval (shape{1}(2,:), 1); @@ -41,14 +39,27 @@ endfunction -function y = func (x,N) - - if size (x,2) > 2 - t = linspace (0,1-1/N,N).'; - y(:,1) = polyval (x(1,:), t); - y(:,2) = polyval (x(2,:), t); - else - y = x(:,2).'; - end - -endfunction +%!demo +%! shape = {[-93.172 606.368 -476.054 291.429; ... +%! -431.196 637.253 11.085 163.791]; ... +%! [-75.3626 -253.2337 457.1678 328.5714; ... +%! 438.7659 -653.6278 -7.9953 380.9336]; ... +%! [-89.5841 344.9716 -275.3876 457.1429; ... +%! -170.3613 237.8858 1.0469 158.0765];... +%! [32.900 -298.704 145.804 437.143; ... +%! -243.903 369.597 -34.265 226.648]; ... +%! [-99.081 409.127 -352.903 317.143; ... +%! 55.289 -114.223 -26.781 318.076]; ... +%! [-342.231 191.266 168.108 274.286; ... +%! 58.870 -38.083 -89.358 232.362]}; +%! +%! % Estimate a good tolerance +%! n = cell2mat(cellfun(@(x)curveval(x,[0 1]), shape, 'uniformoutput',false)); +%! dr = (max(n(:,1))-min(n(:,1)))*(max(n(:,2))-min(n(:,2)))*40; +%! p = shape2polygon (shape,'tol',dr); +%! +%! figure(1) +%! shapeplot(shape,'-b'); +%! hold on; +%! drawPolygon (p,'-or'); +%! hold off Modified: trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m 2012-04-15 15:27:04 UTC (rev 10231) +++ trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m 2012-04-15 16:42:01 UTC (rev 10232) @@ -27,9 +27,11 @@ %% @seealso{drawPolygon, shape2polygon} %% @end deftypefn -function h = shapeplot(shape, N = 16, varargin) +function h = shapeplot(shape, varargin) - p = shape2polygon(shape, N); + n = cell2mat(cellfun(@(x)curveval(x,[0 1]), shape, 'uniformoutput',false)); + dr = (max(n(:,1))-min(n(:,1)))*(max(n(:,2))-min(n(:,2)))/10; + p = shape2polygon(shape,'tol', dr); h = drawPolygon(p,varargin{:}); endfunction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |