From: <jpi...@us...> - 2011-11-03 23:40:33
|
Revision: 8971 http://octave.svn.sourceforge.net/octave/?rev=8971&view=rev Author: jpicarbajal Date: 2011-11-03 23:40:25 +0000 (Thu, 03 Nov 2011) Log Message: ----------- geometry. Adding new functions, improving docstring Added Paths: ----------- trunk/octave-forge/main/geometry/inst/polygons2d/polygon2shape.m trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolygon.m trunk/octave-forge/main/geometry/inst/shape2d/ trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m trunk/octave-forge/main/geometry/inst/shape2d/shapearea.m trunk/octave-forge/main/geometry/inst/shape2d/shapecentroid.m trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m trunk/octave-forge/main/geometry/inst/shape2d/shapetransform.m Removed Paths: ------------- trunk/octave-forge/main/geometry/inst/polygon2shape.m trunk/octave-forge/main/geometry/inst/shape2polygon.m trunk/octave-forge/main/geometry/inst/shapearea.m trunk/octave-forge/main/geometry/inst/shapecentroid.m trunk/octave-forge/main/geometry/inst/shapeplot.m trunk/octave-forge/main/geometry/inst/shapetransform.m trunk/octave-forge/main/geometry/inst/simplifypolygon.m Deleted: trunk/octave-forge/main/geometry/inst/polygon2shape.m =================================================================== --- trunk/octave-forge/main/geometry/inst/polygon2shape.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/polygon2shape.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,34 +0,0 @@ -## Copyright (c) 2011 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 -## (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/>. - -function shape = polygon2shape (polygon) - - # Filter colinear points - polygon = simplifypolygon (polygon); - - np = size(polygon,1); - # polygonal shapes are memory inefficient!! - # TODO filter the regions where edge angles are canging slowly and fit - # polynomial of degree 3; - pp = nan (2*np,2); - - # Transform edges into polynomials of degree 1; - # pp = [(p1-p0) p0]; - pp(:,1) = diff(polygon([1:end 1],:)).'(:); - pp(:,2) = polygon.'(:); - - shape = mat2cell(pp, 2*ones (1,np), 2); - -endfunction Added: trunk/octave-forge/main/geometry/inst/polygons2d/polygon2shape.m =================================================================== --- trunk/octave-forge/main/geometry/inst/polygons2d/polygon2shape.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/polygons2d/polygon2shape.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,54 @@ +## Copyright (c) 2011 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 +## (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 -*- +%% @deftypefn {Function File} {@var{shape} = } polygon2shape (@var{polygon}) +%% Converts a polygon to a shape with edges defined by smooth polynomials. +%% +%% @var{polygon} is a N-by-2 matrix, each row representing a vertex. +%% @var{shape} is a N-by-1 cell, where each element is a pair of polynomials +%% compatible with polyval. +%% +%% In its current state, the shape is formed by polynomials of degree 1. Therefore +%% the shape representation costs more memory except for colinear points in the +%% polygon. +%% +%% @seealso{shape2polygon, simplifypolygon, polyval} +%% @end deftypefn + +function shape = polygon2shape (polygon) + + # Filter colinear points + polygon = simplifypolygon (polygon); + + np = size(polygon,1); + # polygonal shapes are memory inefficient!! + # TODO filter the regions where edge angles are canging slowly and fit + # polynomial of degree 3; + pp = nan (2*np,2); + + # Transform edges into polynomials of degree 1; + # pp = [(p1-p0) p0]; + pp(:,1) = diff(polygon([1:end 1],:)).'(:); + pp(:,2) = polygon.'(:); + + shape = mat2cell(pp, 2*ones (1,np), 2); + +endfunction + +%!test +%! pp = [0 0; 1 0; 1 1; 0 1]; +%! s = polygon2shape (pp); + Added: trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolygon.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/polygons2d/simplifypolygon.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,59 @@ +## Copyright (c) 2011 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 +## (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 -*- +%% @deftypefn {Function File} {@var{spoly} = } simplifypolygon (@var{poly}) +%% Filter colinear vertex from a 2D polygon. +%% +%% @var{poly} is a N-by-2 matrix, each row representing a vertex. +%% +%% @seealso{shape2polygon} +%% @end deftypefn +function polygonsimp = simplifypolygon (polygon) + + # Filter colinear points + edges = diff(polygon([1:end 1],:)); + ned = size(edges,1); + nxt = [2:ned 1]; + + # check if consecutive edges are parallel + para = edges(:,1).*edges(nxt,2) - edges(:,2).*edges(nxt,1); + ind = abs(para) > sqrt(eps); + + polygonsimp = polygon(circshift (ind,1),:); + +endfunction + +%!test +%! P = [0 0; 1 0; 0 1]; +%! P2 = [0 0; 0.1 0; 0.2 0; 0.25 0; 1 0; 0 1; 0 0.7; 0 0.6; 0 0.3; 0 0.1]; +%! assert(P,simplifypolygon (P2)) + +%!demo +%! +%! P = [0 0; 1 0; 0 1]; +%! P2 = [0 0; 0.1 0; 0.2 0; 0.25 0; 1 0; 0 1; 0 0.7; 0 0.6; 0 0.3; 0 0.1]; +%! Pr = simplifypolygon (P2); +%! +%! cla +%! drawPolygon(P,'or;Reference;'); +%! hold on +%! drawPolygon(P2,'x-b;Redundant;'); +%! drawPolygon(Pr,'*g;Simplified;'); +%! hold off +%! +%! % -------------------------------------------------------------------------- +%! % The two polygons describe the same figure, a triangle. Extra points are +%! % removed form the redundant one. Added: trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/shape2polygon.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,51 @@ +## Copyright (c) 2011 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 +## (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 -*- +%% @deftypefn {Function File} {@var{polygon} = } shape2polygon (@var{shape}) +%% @deftypefnx {Function File} {@var{polygon} = } shape2polygon (@var{shape},@var{N}) +%% 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-straigth edges. +%% +%% @seealso{polygon2shape, drawPolygon} +%% @end deftypefn + +function polygon = shape2polygon (shape, N=16) + + polygon = cell2mat ( ... + cellfun(@(x) func (x,N), shape,'UniformOutput',false) ); + + if size(polygon, 1) == 1 + polygon(2,1) = polyval(shape{1}(1,:),1); + polygon(2,2) = polyval(shape{1}(2,:),1); + end + +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 Added: trunk/octave-forge/main/geometry/inst/shape2d/shapearea.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shapearea.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/shapearea.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,71 @@ +%% Copyright (c) 2011 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 +%% (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 -*- +%% @deftypefn {Function File} { @var{a} =} shapearea (@var{pp}) +%% Calculate the area of a 2D shape defined with piecewise smooth polynomials. +%% +%% Shape is defined with piecewise smooth polynomials. @var{pp} is a +%% cell where each elements is a 2-by-(poly_degree+1) array containing a pair of +%% polynomials. +%% +%% @code{px(i,:) = pp@{i@}(1,:)} and @code{py(i,:) = pp@{i@}(2,:)}. +%% +%% @seealso{shapecentroid, shape2polygon, shapeplot} +%% @end deftypefn + +function A = shapearea (shape) + + A = sum(cellfun (@Aint, shape)); + if A < 0 + A = -A; + end + +endfunction + +function dA = Aint (x) + + px = x(1,:); + py = x(2,:); + + P = polyint (conv (px, polyderiv(py))); + + dA = diff(polyval(P,[0 1])); + +end + +%!demo % non-convex bezier shape +%! weirdhearth ={[-17.6816 -34.3989 7.8580 3.7971; ... +%! 15.4585 -28.3820 -18.7645 9.8519]; ... +%! [-27.7359 18.1039 -34.5718 3.7878; ... +%! -40.7440 49.7999 -25.5011 2.2304]}; +%! A = shapearea (weirdhearth) + +%!test +%! triangle = {[1 0; 0 0]; [-0.5 1; 1 0]; [-0.5 0.5; -1 1]}; +%! A = shapearea (triangle); +%! assert (0.5, A); + +%!test +%! circle = {[1.715729 -6.715729 0 5; ... +%! -1.715729 -1.568542 8.284271 0]; ... +%! [1.715729 1.568542 -8.284271 0; ... +%! 1.715729 -6.715729 0 5]; ... +%! [-1.715729 6.715729 0 -5; ... +%! 1.715729 1.568542 -8.284271 0]; ... +%! [-1.715729 -1.568542 8.284271 0; ... +%! -1.715729 6.715729 0 -5]}; +%! A = shapearea (circle); +%! assert (pi*5^2, A, 5e-2); Added: trunk/octave-forge/main/geometry/inst/shape2d/shapecentroid.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shapecentroid.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/shapecentroid.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,71 @@ +%% Copyright (c) 2011 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 +%% (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 -*- +%% @deftypefn {Function File} { @var{cm} =} shapecentroid (@var{pp}) +%% Centroid of a plane shape defined with piecewise smooth polynomials. +%% +%% The shape is defined with piecewise smooth polynomials. @var{pp} is a +%% cell where each elements is a 2-by-(poly_degree+1) matrix containing a pair +%% of polynomials. +%% @code{px(i,:) = pp@{i@}(1,:)} and @code{py(i,:) = pp@{i@}(2,:)}. +%% +%% @seealso{shapearea, shape2polygon} +%% @end deftypefn + +function cm = shapecentroid (shape) + + cm = sum( cell2mat ( cellfun (@CMint, shape, 'UniformOutput', false))); + A = shapearea(shape); + cm = cm / A; + +endfunction + +function dcm = CMint (x) + + px = x(1,:); + py = x(2,:); + Px = polyint (conv(conv (px , px)/2 , polyderiv (py))); + Py = polyint (conv(-conv (py , py)/2 , polyderiv (px))); + + dcm = zeros (1,2); + dcm(1) = diff(polyval(Px,[0 1])); + dcm(2) = diff(polyval(Py,[0 1])); + +endfunction + +%!demo % non-convex bezier shape +%! weirdhearth ={[-17.6816 -34.3989 7.8580 3.7971; ... +%! 15.4585 -28.3820 -18.7645 9.8519]; ... +%! [-27.7359 18.1039 -34.5718 3.7878; ... +%! -40.7440 49.7999 -25.5011 2.2304]}; +%! CoM = shapecentroid (weirdhearth) + +%!test +%! square = {[1 -0.5; 0 -0.5]; [0 0.5; 1 -0.5]; [-1 0.5; 0 0.5]; [0 -0.5; -1 0.5]}; +%! CoM = shapecentroid (square); +%! assert (CoM, [0 0], sqrt(eps)); + +%!test +%! circle = {[1.715729 -6.715729 0 5; ... +%! -1.715729 -1.568542 8.284271 0]; ... +%! [1.715729 1.568542 -8.284271 0; ... +%! 1.715729 -6.715729 0 5]; ... +%! [-1.715729 6.715729 0 -5; ... +%! 1.715729 1.568542 -8.284271 0]; ... +%! [-1.715729 -1.568542 8.284271 0; ... +%! -1.715729 6.715729 0 -5]}; +%! CoM = shapecentroid (circle); +%! assert (CoM , [0 0], 5e-3); Added: trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/shapeplot.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,35 @@ +%% Copyright (c) 2011 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{h} = } shapeplot (@var{shape}) +%% @deftypefnx {Function File} {@var{h} = } shapeplot (@var{shape}, @var{N}) +%% @deftypefnx {Function File} {@var{h} = } shapeplot (@dots{}, @var{param}, @var{value}) +%% Pots a 2D shape defined by piecewise smooth polynomials. +%% +%% @var{pp} is a cell where each elements is a 2-by-(poly_degree+1) matrix +%% containing a pair of polynomials. +%% @var{N} is the number of points to be used in non-straight edges. +%% Additional parameter value pairs are passed to @code{drawPolygon}. +%% +%% @seealso{drawPolygon, shape2polygon} +%% @end deftypefn + +function h = shapeplot(shape, N = 16, varargin) + + p = shape2polygon(shape, N); + h = drawPolygon(p,varargin{:}); + +endfunction Added: trunk/octave-forge/main/geometry/inst/shape2d/shapetransform.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2d/shapetransform.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2d/shapetransform.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -0,0 +1,142 @@ +%% Copyright (c) 2011 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{nshape} = } shapetransform (@var{shape}, @var{T}) +%% Applies transformation to a shape defined by piecewise smooth polynomials. +%% +%% @var{shape} is a cell where each elements is a 2-by-(poly_degree+1) matrix +%% containing a pair of polynomials. +%% +%% Format of @var{T} can be one of : +%% @group +%% [c] , [a b] , [a b c] or [a b c] +%% [f] [d e] [d e f] [d e f] +%% [0 0 1] +%% @end group +%% +%% @seealso{shape2polygon, shapeplot} +%% @end deftypefn + +function nshape = shapetransform (shape, Trans) + + if size(Trans,1) < 2 + error("geometry:shapetransform:InvalidArgument", ... + "Transformation can be 2x1, 2x2, 2x3 or 3x3. See help."); + end + + if ~iscell(shape) + error("geometry:shapetransform:InvalidArgument", "Shape must be a cell of 2D polynomials."); + end + + A =[]; + v = []; + + switch size(Trans,2) + case 1 + % Just translation + v = Trans; + + case 2 + % Just linear transformation + A = Trans; + + case 3 + % Affine transform + A = Trans(1:2,1:2); + v = Trans(1:2,3); + end + + nshape = cellfun (@(x)polytransform (x,A,v), shape, 'UniformOutput',false); + +endfunction + +function np = polytransform(p,A,v) + + np = p; + if ~isempty (A) + np = A*np; + end + if ~isempty (v) + np(:,end) = np(:,end) + v; + 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]}; +%! +%! A = shapearea (shape); +%! T = eye(2)/sqrt(A); +%! shape = shapetransform (shape,T); +%! T = shapecentroid (shape)(:); +%! shape = shapetransform (shape,-T + [2; 0]); +%! +%! cla +%! shapeplot (shape,10,'-r','linewidth',2) +%! hold on +%! for i = 1:9 +%! T = createRotation (i*pi/5)(1:2,1:2)/exp(0.3*i); +%! shapeplot (shapetransform(shape, T), 10, 'color',rand(1,3),'linewidth',2); +%! end +%! hold off +%! axis tight +%! axis square + + +%!shared shape +%! 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]}; + +%!test +%! v = shapecentroid (shape)(:); +%! nshape = shapetransform (shape, -v); +%! vn = shapecentroid (nshape)(:); +%! assert(vn,[0; 0],1e-2); + +%!test +%! v = shapecentroid (shape)(:); +%! T = createLineReflection([0 0 1 0]); +%! nshape = shapetransform (shape, T); +%! vn = shapecentroid (nshape)(:); +%! assert(vn,T(1:2,1:2)*v); + +%!test +%! v = shapecentroid (shape)(:); +%! T = createRotation(v.',pi/2); +%! nshape = shapetransform (shape, T); +%! vn = shapecentroid (nshape)(:); +%! assert(vn,v,1e-2); Deleted: trunk/octave-forge/main/geometry/inst/shape2polygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2polygon.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/shape2polygon.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,46 +0,0 @@ -## Copyright (c) 2011 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 -## (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 -*- -%% @deftypefn {Function File} {@var{polygon} = } shape2polygon (@var{shape}) -%% @deftypefnx {Function File} {@var{polygon} = } shape2polygon (@var{shape},@var{N}) -%% Transforms a 2D shape described by piecewise smooth polynomials into a polygon. -%% -%% @seealso{drawPolygon} -%% @end deftypefn -function polygon = shape2polygon (shape, N=16) - - polygon = cell2mat ( ... - cellfun(@(x) func (x,N), shape,'UniformOutput',false) ); - - if size(polygon, 1) == 1 - polygon(2,1) = polyval(shape{1}(1,:),1); - polygon(2,2) = polyval(shape{1}(2,:),1); - end - -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 - Deleted: trunk/octave-forge/main/geometry/inst/shapearea.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapearea.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/shapearea.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,67 +0,0 @@ -%% Copyright (c) 2011 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 -%% (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 -*- -%% @deftypefn {Function File} { @var{a} =} shapearea (@var{pp}) -%% Shape is defined with piecewise smooth polynomials. @var{pp} is a -%% cell where each elements is a 2-by-(poly_degree+1) array containing -%% @code{px(i,:) = pp@{i@}(1,:)} and @code{py(i,:) = pp@{i@}(2,:)}. -%% -%% @end deftypefn - -function A = shapearea (shape) - - A = sum(cellfun (@Aint, shape)); - if A < 0 - A = -A; - end - -endfunction - -function dA = Aint (x) - - px = x(1,:); - py = x(2,:); - - P = polyint (conv (px, polyderiv(py))); - - dA = diff(polyval(P,[0 1])); - -end - -%!demo % non-convex bezier shape -%! weirdhearth ={[-17.6816 -34.3989 7.8580 3.7971; ... -%! 15.4585 -28.3820 -18.7645 9.8519]; ... -%! [-27.7359 18.1039 -34.5718 3.7878; ... -%! -40.7440 49.7999 -25.5011 2.2304]}; -%! A = shapearea (weirdhearth) - -%!test -%! triangle = {[1 0; 0 0]; [-0.5 1; 1 0]; [-0.5 0.5; -1 1]}; -%! A = shapearea (triangle); -%! assert (0.5, A); - -%!test -%! circle = {[1.715729 -6.715729 0 5; ... -%! -1.715729 -1.568542 8.284271 0]; ... -%! [1.715729 1.568542 -8.284271 0; ... -%! 1.715729 -6.715729 0 5]; ... -%! [-1.715729 6.715729 0 -5; ... -%! 1.715729 1.568542 -8.284271 0]; ... -%! [-1.715729 -1.568542 8.284271 0; ... -%! -1.715729 6.715729 0 -5]}; -%! A = shapearea (circle); -%! assert (pi*5^2, A, 5e-2); - Deleted: trunk/octave-forge/main/geometry/inst/shapecentroid.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapecentroid.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/shapecentroid.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,71 +0,0 @@ -%% Copyright (c) 2011 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 -%% (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 -*- -%% @deftypefn {Function File} { @var{cm} =} shapecentroid (@var{pp}) -%% Centroid of a plane shape. -%% -%% The shape is defined with piecewise smooth polynomials. @var{pp} is a -%% cell where each elements is a 2-by-(poly_degree+1) matrix containing -%% @code{px(i,:) = pp@{i@}(1,:)} and @code{py(i,:) = pp@{i@}(2,:)}. -%% -%% @seealso{shapearea} -%% @end deftypefn - -function cm = shapecentroid (shape) - - cm = sum( cell2mat ( cellfun (@CMint, shape, 'UniformOutput', false))); - A = shapearea(shape); - cm = cm / A; - -endfunction - -function dcm = CMint (x) - - px = x(1,:); - py = x(2,:); - Px = polyint (conv(conv (px , px)/2 , polyderiv (py))); - Py = polyint (conv(-conv (py , py)/2 , polyderiv (px))); - - dcm = zeros (1,2); - dcm(1) = diff(polyval(Px,[0 1])); - dcm(2) = diff(polyval(Py,[0 1])); - -endfunction - -%!demo % non-convex bezier shape -%! weirdhearth ={[-17.6816 -34.3989 7.8580 3.7971; ... -%! 15.4585 -28.3820 -18.7645 9.8519]; ... -%! [-27.7359 18.1039 -34.5718 3.7878; ... -%! -40.7440 49.7999 -25.5011 2.2304]}; -%! CoM = shapecentroid (weirdhearth) - -%!test -%! square = {[1 -0.5; 0 -0.5]; [0 0.5; 1 -0.5]; [-1 0.5; 0 0.5]; [0 -0.5; -1 0.5]}; -%! CoM = shapecentroid (square); -%! assert (CoM, [0 0], sqrt(eps)); - -%!test -%! circle = {[1.715729 -6.715729 0 5; ... -%! -1.715729 -1.568542 8.284271 0]; ... -%! [1.715729 1.568542 -8.284271 0; ... -%! 1.715729 -6.715729 0 5]; ... -%! [-1.715729 6.715729 0 -5; ... -%! 1.715729 1.568542 -8.284271 0]; ... -%! [-1.715729 -1.568542 8.284271 0; ... -%! -1.715729 6.715729 0 -5]}; -%! CoM = shapecentroid (circle); -%! assert (CoM , [0 0], 5e-3); - Deleted: trunk/octave-forge/main/geometry/inst/shapeplot.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapeplot.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/shapeplot.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,28 +0,0 @@ -%% Copyright (c) 2011 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{h} = } shapeplot (@var{shape}) -%% @deftypefnx {Function File} {@var{h} = } shapeplot (@var{shape}, @var{N}) -%% Pots a 2D shape defined by piecewise smooth polynomials. -%% -%% @end deftypefn - -function h = shapeplot(shape, N = 16, varargin) - - p = shape2polygon(shape, N); - h = drawPolygon(p,varargin{:}); - -endfunction Deleted: trunk/octave-forge/main/geometry/inst/shapetransform.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapetransform.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/shapetransform.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,137 +0,0 @@ -%% Copyright (c) 2011 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{nshape} = } shapetransform (@var{shape}, @var{T}) -%% Applies transformation to a shape defined by piecewise smooth polynomials. -%% -%% Format of @var{T} can be one of : -%% [c] , [a b] , [a b c] or [a b c] -%% [f] [d e] [d e f] [d e f] -%% [0 0 1] -%% -%% @end deftypefn - -function nshape = shapetransform (shape, Trans) - - if size(Trans,1) < 2 - error("geometry:shapetransform:InvalidArgument", ... - "Transformation can be 2x1, 2x2, 2x3 or 3x3. See help."); - end - - if ~iscell(shape) - error("geometry:shapetransform:InvalidArgument", "Shape must be a cell of 2D polynomials."); - end - - A =[]; - v = []; - - switch size(Trans,2) - case 1 - % Just translation - v = Trans; - - case 2 - % Just linear transformation - A = Trans; - - case 3 - % Affine transform - A = Trans(1:2,1:2); - v = Trans(1:2,3); - end - - nshape = cellfun (@(x)polytransform (x,A,v), shape, 'UniformOutput',false); - -endfunction - -function np = polytransform(p,A,v) - - np = p; - if ~isempty (A) - np = A*np; - end - if ~isempty (v) - np(:,end) = np(:,end) + v; - 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]}; -%! -%! A = shapearea (shape); -%! T = eye(2)/sqrt(A); -%! shape = shapetransform (shape,T); -%! T = shapecentroid (shape)(:); -%! shape = shapetransform (shape,-T + [2; 0]); -%! -%! cla -%! shapeplot (shape,10,'-r','linewidth',2) -%! hold on -%! for i = 1:9 -%! T = createRotation (i*pi/5)(1:2,1:2)/exp(0.3*i); -%! shapeplot (shapetransform(shape, T), 10, 'color',rand(1,3),'linewidth',2); -%! end -%! hold off -%! axis tight -%! axis square - - -%!shared shape -%! 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]}; - -%!test -%! v = shapecentroid (shape)(:); -%! nshape = shapetransform (shape, -v); -%! vn = shapecentroid (nshape)(:); -%! assert(vn,[0; 0],1e-2); - -%!test -%! v = shapecentroid (shape)(:); -%! T = createLineReflection([0 0 1 0]); -%! nshape = shapetransform (shape, T); -%! vn = shapecentroid (nshape)(:); -%! assert(vn,T(1:2,1:2)*v); - -%!test -%! v = shapecentroid (shape)(:); -%! T = createRotation(v.',pi/2); -%! nshape = shapetransform (shape, T); -%! vn = shapecentroid (nshape)(:); -%! assert(vn,v,1e-2); - Deleted: trunk/octave-forge/main/geometry/inst/simplifypolygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/simplifypolygon.m 2011-11-03 22:45:47 UTC (rev 8970) +++ trunk/octave-forge/main/geometry/inst/simplifypolygon.m 2011-11-03 23:40:25 UTC (rev 8971) @@ -1,29 +0,0 @@ -## Copyright (c) 2011 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 -## (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/>. - -function polygonsimp = simplifypolygon (polygon) - - # Filter colinear points - edges = diff(polygon([1:end 1],:)); - ned = size(edges,1); - nxt = [2:ned 1]; - - # check if consecutive edges are parallel - para = edges(:,1).*edges(nxt,2) - edges(:,2).*edges(nxt,1); - ind = abs(para) > sqrt(eps); - - polygonsimp = polygon(circshift (ind,1),:); - -endfunction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |