From: <jpi...@us...> - 2012-04-12 21:22:54
|
Revision: 10204 http://octave.svn.sourceforge.net/octave/?rev=10204&view=rev Author: jpicarbajal Date: 2012-04-12 21:22:47 +0000 (Thu, 12 Apr 2012) Log Message: ----------- geometry: Adding curveval and cuve2polyline. functions to convert form polygonal curves to polylines. Modified Paths: -------------- trunk/octave-forge/main/geometry/DESCRIPTION trunk/octave-forge/main/geometry/INDEX trunk/octave-forge/main/geometry/NEWS trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m Added Paths: ----------- trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m trunk/octave-forge/main/geometry/inst/shape2d/curveval.m Modified: trunk/octave-forge/main/geometry/DESCRIPTION =================================================================== --- trunk/octave-forge/main/geometry/DESCRIPTION 2012-04-12 13:47:32 UTC (rev 10203) +++ trunk/octave-forge/main/geometry/DESCRIPTION 2012-04-12 21:22:47 UTC (rev 10204) @@ -1,11 +1,11 @@ Name: Geometry -Version: 1.4.1 -Date: 2012-03-24 +Version: 1.4.2 +Date: 2012-XX-XX Author: David Legland <dav...@gr...>, José Luis García Pallero <jgp...@gm...>, Juan Pablo Carbajal <car...@if...> Maintainer: Juan Pablo Carbajal <car...@if...> Title: Computational Geometry Description: Library for geometric computing extending MatGeom functions. Useful to create, transform, manipulate and display geometric primitives. -Depends: octave (>= 3.4.0) +Depends: octave (>= 3.4.0), general (>= 1.3.0) Autoload: yes License: GPLv3+, FreeBSD Url: http://octave.sf.net, http://matgeom.sf.net, http://davis.wpi.edu/~matt/courses/clipping/, https://bitbucket.org/jgpallero/octclip Modified: trunk/octave-forge/main/geometry/INDEX =================================================================== --- trunk/octave-forge/main/geometry/INDEX 2012-04-12 13:47:32 UTC (rev 10203) +++ trunk/octave-forge/main/geometry/INDEX 2012-04-12 21:22:47 UTC (rev 10204) @@ -124,6 +124,8 @@ shapecentroid shapeplot shapetransform + curvelval + curve2polyline 2D Others bisector crackPattern2 Modified: trunk/octave-forge/main/geometry/NEWS =================================================================== --- trunk/octave-forge/main/geometry/NEWS 2012-04-12 13:47:32 UTC (rev 10203) +++ trunk/octave-forge/main/geometry/NEWS 2012-04-12 21:22:47 UTC (rev 10204) @@ -1,6 +1,19 @@ 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 +=============================================================================== + +* Added function + - curveval.m: Evaluates a polynomial curve defined as a 2-by-N matrix. + - curve2polyline.m: Converts a polynomial curve into a polyline by the adaptive + sampling method. + +* Known issues + - simplifypolygon.m returns empty polygons when points are repeated, i.e when + the polygon is not correctly formed. + +=============================================================================== geometry-1.4.1 Release Date: 2012-03-24 Release Manager: Juan Pablo Carbajal =============================================================================== Added: trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/curve2polyline.m 2012-04-12 21:22:47 UTC (rev 10204) @@ -0,0 +1,139 @@ +%% Copyright (c) 2012 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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 -*- +%% @deftypefn {Function File} {@var{polyline} = } curve2polyline (@var{curve}) +%% @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 +%% polynomial (compatible with @code{polyval}) describing the respective component +%% of the curve. The curve must be parametrized in the interval [0,1]. +%% The vertices of the polyline are accumulated in regions of the curve where +%% the curvature is higher. +%% +%% @strong{Parameters} +%% @table @samp +%% @item 'Nmax' +%% Maximum number of vertices. Not used. +%% @item 'Tol' +%% Tolerance for the error criteria. Default value @code{1e-4}. +%% @item 'MaxIter' +%% Maximum number of iterations. Default value @code{10}. +%% @item 'Method' +%% Not implemented. +%% @end table +%% +%% @seealso{shape2polygon, curveval} +%% @end deftypefn + +%% This function is based on the algorithm described in +%% L. H. de Figueiredo (1993). "Adaptive Sampling of Parametric Curves". Graphic Gems III. +%% I had to remove the recursion so this version could be improved. +%% Thursday, April 12 2012 -- JuanPi + +function [polyline t bump]= curve2polyline (curve, varargin) + + # --- Parse arguments --- # + parser = inputParser (); + parser.FunctionName = "curve2polyline"; + parser = addParamValue (parser,'Nmax', 32, @(x)x>0); + parser = addParamValue (parser,'Tol', 1e-4, @(x)x>0); + parser = addParamValue (parser,'MaxIter', 10, @(x)x>0); + parser = parse(parser,varargin{:}); + + Nmax = parser.Results.Nmax; + tol = parser.Results.Tol; + MaxIter = parser.Results.MaxIter; + + clear parser + # ------ # + + t = [0; 1]; + tf = 1; + points = 1; + for iter = 1:MaxIter + % Add parameter values where error is still bigger than tol. + t = interleave(t, tf); + nt = length(t); + + % Update error + polyline = curveval (curve,t); + bump = bumpyness(polyline); + + % Check which intervals must be subdivided + idx = find(bump > tol); + % The position of the bumps mpas into intervals + % 1 -> 1 2 + % 2 -> 3 4 + % 3 -> 5 6 + % and so on + idx = [2*(idx-1)+1; 2*idx](:); + tf = false (nt-1,1); + tf(idx) = true; + + if all (!tf) + break; + end + + end + +endfunction + +function f = bumpyness (p) +%% Check for co-linearity +%% TODO implement various method for this +%% -- Area of the triangle close to zero (used currently). +%% -- Angle close to pi. +%% -- abs(p0-pt) + abs(pt-p1) - abs(p0-p1) almost zero. +%% -- Curve's tange at 0,t,1 are almost parallel. +%% -- pt is in chord p0 -> p1. + + PL = p(1:2:end-2,:); + PC = p(2:2:end-1,:); + PR = p(3:2:end,:); + + a = PL - PC; + b = PR - PC; + + f = (a(:,1).*b(:,2) - a(:,2).*b(:,1)).^2; + +endfunction + +function tt = interleave (t,varargin) + + nt = length(t); + ntt = 2 * nt -1; + tt = zeros(ntt,1); + tt(1:2:ntt) = t; + beta = 0.4 + 0.2*rand(nt-1, 1); + tt(2:2:ntt) = t(1:end-1) + beta.*(t(2:end)-t(1:end-1)); + + if nargin > 1 + tf = true (ntt,1); + tf(2:2:ntt) = varargin{1}; + tt(!tf) = []; + end + +endfunction + +%!demo +%! curve = [0 0 1 0;1 -0.3-1 0.3 0]; +%! polyline = curve2polyline(curve,'tol',1e-8); +%! +%! t = linspace(0,1,100)'; +%! pc = curveval(curve,t); +%! +%! plot(p(:,1),p(:,2),'-o',pc(:,1),pc(:,2),'-r') Added: trunk/octave-forge/main/geometry/inst/shape2d/curveval.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/curveval.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/curveval.m 2012-04-12 21:22:47 UTC (rev 10204) @@ -0,0 +1,30 @@ +%% Copyright (c) 2012 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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 -*- +%% @deftypefn {Function File} {@var{p} = } curveval (@var{curve}, @var{t}) +%% Evaluates parametric @var{curve} at @var{t}. +%% +%% @end deftypefn +function p = curveval (curve, t) + + dim = size (curve,1); + p = zeros (length(t), dim); + + for i = 1:dim + p(:,i) = polyval (curve(i,:), t); + end + +endfunction Modified: trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m 2012-04-12 13:47:32 UTC (rev 10203) +++ trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m 2012-04-12 21:22:47 UTC (rev 10204) @@ -21,7 +21,7 @@ %% @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-straigth edges. +%% @var{N} defines the number of points to be used in non-straight edges. %% %% @seealso{polygon2shape, drawPolygon} %% @end deftypefn This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |