From: <jpi...@us...> - 2011-11-02 15:47:13
|
Revision: 8931 http://octave.svn.sourceforge.net/octave/?rev=8931&view=rev Author: jpicarbajal Date: 2011-11-02 15:47:06 +0000 (Wed, 02 Nov 2011) Log Message: ----------- geometry. Adding some functions to manipulate piecewise smooth polygons Modified Paths: -------------- trunk/octave-forge/main/geometry/DESCRIPTION trunk/octave-forge/main/geometry/PKG_ADD trunk/octave-forge/main/geometry/PKG_DEL trunk/octave-forge/main/geometry/inst/shapearea.m Added Paths: ----------- trunk/octave-forge/main/geometry/inst/polygon2shape.m trunk/octave-forge/main/geometry/inst/polygons2d/ trunk/octave-forge/main/geometry/inst/polygons2d/drawPolygon.m trunk/octave-forge/main/geometry/inst/polygons2d/polygons2d.m trunk/octave-forge/main/geometry/inst/shape2polygon.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 Modified: trunk/octave-forge/main/geometry/DESCRIPTION =================================================================== --- trunk/octave-forge/main/geometry/DESCRIPTION 2011-11-02 15:01:46 UTC (rev 8930) +++ trunk/octave-forge/main/geometry/DESCRIPTION 2011-11-02 15:47:06 UTC (rev 8931) @@ -1,6 +1,6 @@ Name: Geometry -Version: 1.2.1 -Date: 2011-11-2 +Version: 1.2.2 +Date: 2011-11-9 Author: David Legland <dav...@gr...>, Juan Pablo Carbajal <car...@if...> Maintainer: Juan Pablo Carbajal <car...@if...> Title: Computational Geometry Modified: trunk/octave-forge/main/geometry/PKG_ADD =================================================================== --- trunk/octave-forge/main/geometry/PKG_ADD 2011-11-02 15:01:46 UTC (rev 8930) +++ trunk/octave-forge/main/geometry/PKG_ADD 2011-11-02 15:47:06 UTC (rev 8931) @@ -1,5 +1,5 @@ %1 -dirlist = {"geom2d","io"}; +dirlist = {"geom2d","io","polygons2d"}; dirname = fileparts (canonicalize_file_name (mfilename ("fullpath"))); if (! exist (fullfile (dirname, "inst"), "dir")) Modified: trunk/octave-forge/main/geometry/PKG_DEL =================================================================== --- trunk/octave-forge/main/geometry/PKG_DEL 2011-11-02 15:01:46 UTC (rev 8930) +++ trunk/octave-forge/main/geometry/PKG_DEL 2011-11-02 15:47:06 UTC (rev 8931) @@ -1,5 +1,5 @@ %1 -dirlist = {"geom2d","io"}; +dirlist = {"geom2d","io","polygons2d"}; dirname = fileparts (canonicalize_file_name (mfilename ("fullpath"))); if (! exist (fullfile (dirname, "inst"), "dir")) Copied: trunk/octave-forge/main/geometry/inst/polygon2shape.m (from rev 8918, trunk/octave-forge/main/mechanics/inst/private/polygon2shape.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/polygon2shape.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/polygon2shape.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,34 @@ +## 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/drawPolygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/polygons2d/drawPolygon.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/polygons2d/drawPolygon.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,144 @@ +%% Copyright (c) 2011, INRA +%% 2005-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} = } drawPolygon (@var{coord}) +%% @deftypefnx {Function File} {@var{h} = } drawPolygon (@var{px}, @var{py}) +%% @deftypefnx {Function File} {@var{h} = } drawPolygon (@var{polys}) +%% Draw a polygon specified by a list of points. +%% +%% drawPolygon(COORD); +%% Packs coordinates in a single [N*2] array. +%% +%% drawPolygon(PX, PY); +%% Specifies coordinates in separate arrays. +%% +%% drawPolygon(POLYS) +%% Packs coordinate of several polygons in a cell array. Each element of +%% the array is a Ni*2 double array. +%% +%% H = drawPolygon(...); +%% Also return a handle to the list of line objects. +%% +%% +%% @seealso{polygons2d, drawCurve} +%% @end deftypefn + +function varargout = drawPolygon(varargin) + + % check input + if isempty(varargin) + error('need to specify a polygon'); + end + + var = varargin{1}; + + %% Manage cell arrays of polygons + + % case of a set of polygons stored in a cell array + if iscell(var) + N = length(var); + h = zeros(N, 1); + for i = 1:N + state = ishold(gca); + hold on; + % check for empty polygons + if ~isempty(var{i}) + h(i) = drawPolygon(var{i}, varargin{2:end}); + end + if ~state + hold off + end + end + + if nargout > 0 + varargout = {h}; + end + + return; + end + + + %% Parse coordinates and options + + % Extract coordinates of polygon vertices + if size(var, 2) > 1 + % first argument is a polygon array + px = var(:, 1); + py = var(:, 2); + varargin(1) = []; + else + % arguments 1 and 2 correspond to x and y coordinate respectively + if length(varargin) < 2 + error('Should specify either a N-by-2 array, or 2 N-by-1 vectors'); + end + + px = varargin{1}; + py = varargin{2}; + varargin(1:2) = []; + end + + % set default line format + if isempty(varargin) + varargin = {'b-'}; + end + + % check case of polygons with holes + if sum(isnan(px(:))) > 0 + polygons = splitPolygons([px py]); + h = drawPolygon(polygons); + + if nargout > 0 + varargout = {h}; + end + + return; + end + + + %% Draw the polygon + + % ensure last point is the same as the first one + px(size(px, 1)+1, :) = px(1,:); + py(size(py, 1)+1, :) = py(1,:); + + % draw the polygon outline + h = plot(px, py, varargin{:}); + + % format output arg + if nargout > 0 + varargout = {h}; + end + +endfunction + Added: trunk/octave-forge/main/geometry/inst/polygons2d/polygons2d.m =================================================================== --- trunk/octave-forge/main/geometry/inst/polygons2d/polygons2d.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/polygons2d/polygons2d.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,180 @@ +%% Copyright (c) 2011, INRA +%% 2005-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} {} polygons2d () +%% MATGEOM-POLYGONS +%% +%% The 'polygons' module contains functions operating on shapes composed +%% of a vertex list, like polygons or polylines. +%% +%% We call 'polyline' the curve defined by a series of vertices. +%% A polyline can be either closed or open, depending on whether the last +%% vertex is connected to the first one or not. This can be given as an +%% option is some functions in the module. +%% A 'polygon' is the planar domain delimited by a closed polyline. We +%% sometimes want to consider 'complex polygons', whose boundary is +%% composed of several disjoint domains. The domain defined by a single +%% closed polyline is called 'simple polygon'. +%% We call 'curve' a polyline with many vertices, such that the polyline +%% can be considered as a discrete approximation of a "real" curve. +%% +%% A simple polygon or polyline is represented by a N-by-2 array, each row +%% of the array representing the coordinates of a vertex. +%% Simple polygons are assumed to be closed, so there is no need to repeat +%% the first vertex at the end. +%% As both polygons and polylines can be represented by a list of vertex +%% coordinates, some functions also consider the vertex list itself. Such +%% functions are prefixed by 'pointSet'. Also, many functions prefixed by +%% 'polygon' or 'polyline' works also on the other type of shape. +%% +%% For multiple-connected polygons, the different connected boundaries are +%% separated by a row [NaN NaN]. +%% +%% For some functions, the orientation of the polygon can be relevant: CCW +%% stands for 'Conter-Clockwise' (positive orientation), CW stands for +%% 'Clockwise'. +%% +%% Polylines are parametrized in the following way: +%% * the i-th vertex is located at position i-1 +%% * points of the i-th edge have positions ranging linearly from i-1 to i +%% The parametrization domain for an open polyline is from 0 to Nv-1, and +%% from 0 to Nv for a closed polyline (positions 0 and Nv correspond to +%% the same point). +%% +%% Example: +%% % Simple polygon: +%% P1 = [1 1;2 1;2 2;1 2]; +%% drawPolygon(P1); +%% axis([0 5 0 5]); +%% % Multiple polygon: +%% P2 = [10 10;40 10;40 40;10 40;NaN NaN;20 20;20 30;30 30;30 20]; +%% figure;drawPolygon(P2); axis([0 50 0 50]); +%% +%% +%% Point Sets +%% pointSetBounds - Bounding box of a set of points +%% pointSetsAverage - Compute the average of several point sets +%% minimumCaliperDiameter - Minimum caliper diameter of a set of points +%% findPoint - Find index of a point in an set from its coordinates +%% +%% Polylines +%% polylinePoint - Extract a point from a polyline +%% polylineLength - Return length of a polyline given as a list of points +%% polylineCentroid - Compute centroid of a curve defined by a series of points +%% polylineSubcurve - Extract a portion of a polyline +%% reversePolyline - Reverse a polyline, by iterating vertices from the end +%% isPointOnPolyline - Test if a point belongs to a polyline +%% projPointOnPolyline - Compute position of a point projected on a polyline +%% distancePointPolyline - Compute shortest distance between a point and a polyline +%% distancePolylines - Compute the shortest distance between 2 polylines +%% intersectPolylines - Find the common points between 2 polylines +%% polylineSelfIntersections - Find self-intersections points of a polyline +%% +%% Curves (polylines with lot of vertices) +%% parametrize - Parametrization of a curve, based on edges length +%% curvature - Estimate curvature of a polyline defined by points +%% cart2geod - Convert cartesian coordinates to geodesic coord. +%% geod2cart - Convert geodesic coordinates to cartesian coord. +%% curveMoment - Compute inertia moment of a 2D curve +%% curveCMoment - Compute centered inertia moment of a 2D curve +%% curveCSMoment - Compute centered scaled moment of a 2D curve +%% +%% Polygons +%% polygonPoint - Extract a point from a polygon +%% polygonSubcurve - Extract a portion of a polygon +%% reversePolygon - Reverse a polygon, by iterating vertices from the end +%% projPointOnPolygon - Compute position of a point projected on a polygon +%% splitPolygons - Convert a NaN separated polygon list to a cell array of polygons +%% clipPolygon - Clip a polygon with a rectangular box +%% clipPolygonHP - Clip a polygon with a Half-plane defined by a directed line +%% intersectLinePolygon - Intersection points between a line and a polygon +%% intersectRayPolygon - Intersection points between a ray and a polygon +%% polygonSelfIntersections - Find-self intersection points of a polygon +%% convexHull - Convex hull of a set of points +%% polygonLoops - Divide a possibly self-intersecting polygon into a set of simple loops +%% expandPolygon - Expand a polygon by a given (signed) distance +%% medialAxisConvex - Compute medial axis of a convex polygon +%% +%% Measures on Polygons +%% isPointInPolygon - Test if a point is located inside a polygon +%% polygonContains - Test if a point is contained in a multiply connected polygon +%% polygonCentroid - Compute the centroid (center of mass) of a polygon +%% polygonArea - Compute the signed area of a polygon +%% polygonLength - Perimeter of a polygon +%% polygonNormalAngle - Compute the normal angle at a vertex of the polygon +%% polygonBounds - Compute the bounding box of a polygon +%% distancePointPolygon - Compute shortest distance between a point and a polygon +%% distancePolygons - Compute the shortest distance between 2 polygons +%% +%% Triangles +%% isPointInTriangle - Test if a point is located inside a triangle +%% triangleArea - Area of a triangle +%% +%% Functions from stochastic geometry +%% steinerPoint - Compute steiner point (weighted centroid) of a polygon +%% steinerPolygon - Create a Steiner polygon from a set of vectors +%% supportFunction - Compute support function of a polygon +%% convexification - Compute the convexification of a polygon +%% +%% Input, Output and conversions +%% readPolygon - Read a polygon stored in a file +%% polygonToRow - Convert polygon coordinates to a row vector +%% rowToPolygon - Create a polygon from a row vector +%% rectAsPolygon - Convert a (centered) rectangle into a series of points +%% +%% Drawing functions +%% drawPolyline - Draw a polyline specified by a list of points +%% drawPolygon - Draw a polygon specified by a list of points +%% fillPolygon - Fill a polygon specified by a list of points +%% +%% +%% Credits: +%% * function intersectPolylines uses the 'interX' contribution from "NS" +%% (file exchange 22441, called 'curve-intersections') +%% +%% ----- +%% Author: David Legland +%% e-mail: dav...@gr... +%% created the 07/11/2005. +%% Homepage: http://matgeom.sourceforge.net/ +%% http://www.pfl-cepia.inra.fr/index.php?page=geom2d +%% Copyright INRA - Cepia Software Platform. +%% +%% @end deftypefn% + +function polygons2d () + +help('polygons2d'); + +endfunction Copied: trunk/octave-forge/main/geometry/inst/shape2polygon.m (from rev 8918, trunk/octave-forge/main/mechanics/inst/private/shape2polygon.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/shape2polygon.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shape2polygon.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,46 @@ +## 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 + Modified: trunk/octave-forge/main/geometry/inst/shapearea.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapearea.m 2011-11-02 15:01:46 UTC (rev 8930) +++ trunk/octave-forge/main/geometry/inst/shapearea.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -24,7 +24,10 @@ function A = shapearea (shape) A = sum(cellfun (@Aint, shape)); - + if A < 0 + A = -A; + end + endfunction function dA = Aint (x) Added: trunk/octave-forge/main/geometry/inst/shapecentroid.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapecentroid.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shapecentroid.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -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. +%% +%% 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); + Added: trunk/octave-forge/main/geometry/inst/shapeplot.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapeplot.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shapeplot.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,28 @@ +%% 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 Added: trunk/octave-forge/main/geometry/inst/shapetransform.m =================================================================== --- trunk/octave-forge/main/geometry/inst/shapetransform.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/shapetransform.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,137 @@ +%% 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); + Copied: trunk/octave-forge/main/geometry/inst/simplifypolygon.m (from rev 8918, trunk/octave-forge/main/mechanics/inst/private/simplifypolygon.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/simplifypolygon.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/simplifypolygon.m 2011-11-02 15:47:06 UTC (rev 8931) @@ -0,0 +1,29 @@ +## 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. |