From: <jpi...@us...> - 2011-10-03 23:13:29
|
Revision: 8668 http://octave.svn.sourceforge.net/octave/?rev=8668&view=rev Author: jpicarbajal Date: 2011-10-03 23:13:22 +0000 (Mon, 03 Oct 2011) Log Message: ----------- geometry. Adapting matGeom Added Paths: ----------- trunk/octave-forge/main/geometry/geom2d/inst/ trunk/octave-forge/main/geometry/geom2d/inst/angle2Points.m trunk/octave-forge/main/geometry/geom2d/inst/angle3Points.m trunk/octave-forge/main/geometry/geom2d/inst/angleAbsDiff.m trunk/octave-forge/main/geometry/geom2d/inst/normalizeAngle.m trunk/octave-forge/main/geometry/geom2d/inst/vectorAngle.m Removed Paths: ------------- trunk/octave-forge/main/geometry/geom2d/angle2Points.m trunk/octave-forge/main/geometry/geom2d/angle3Points.m trunk/octave-forge/main/geometry/geom2d/angleAbsDiff.m trunk/octave-forge/main/geometry/geom2d/normalizeAngle.m trunk/octave-forge/main/geometry/geom2d/vectorAngle.m Deleted: trunk/octave-forge/main/geometry/geom2d/angle2Points.m =================================================================== --- trunk/octave-forge/main/geometry/geom2d/angle2Points.m 2011-10-03 23:11:11 UTC (rev 8667) +++ trunk/octave-forge/main/geometry/geom2d/angle2Points.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -1,109 +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. - -%% -*- texinfo -*- -%% @deftypefn {Function File} {@var{alpha} =} angle2Points (@var{p1}, @var{p2}) -%% Compute horizontal angle between 2 points -%% -%% @var{p1} and @var{p2} are either [1x2] arrays, or [Nx2] arrays, in this case -%% @var{alpha} is a [Nx1] array. The angle computed is the horizontal angle of -%% the line (@var{p1},@var{p2}). -%% -%% Result is always given in radians, between 0 and 2*pi. -%% -%% @seealso{points2d, angles2d, angle3points, normalizeAngle, vectorAngle} -%% @end deftypefn - -function theta = angle2Points(varargin) - - % process input arguments - if length(varargin)==2 - p1 = varargin{1}; - p2 = varargin{2}; - elseif length(varargin)==1 - var = varargin{1}; - p1 = var(1,:); - p2 = var(2,:); - end - - % ensure data have correct size - n1 = size(p1, 1); - n2 = size(p2, 1); - if n1~=n2 && min(n1, n2)>1 - error('angle2Points: wrong size for inputs'); - end - - % angle of line (P2 P1), between 0 and 2*pi. - dp = bsxfun(@minus, p2, p1); - theta = mod(atan2(dp(:,2), dp(:,1)) + 2*pi, 2*pi); - -endfunction - -%!test -%! % all points inside window, possibly touching edges -%! p1 = [0 0]; -%! p2 = [10 0]; -%! angle_ = angle2Points (p1, p2); -%! assert (angle_,0,1e-6); -%! angle_ = angle2Points (p2, p1); -%! assert (angle_,pi,1e-6); - - -%!test -%! % all points inside window, possibly touching edges -%! p1 = [0 0]; -%! p2 = [0 10]; -%! angle_ = angle2Points (p1, p2); -%! assert (pi/2, angle_,1e-6); -%! angle_ = angle2Points (p2, p1); -%! assert (3*pi/2, angle_,1e-6); - -%!test -%! % all points inside window, possibly touching edges -%! p1 = [0 0;0 0;0 0;0 0]; -%! p2 = [10 0;10 10;0 10;-10 10]; -%! angle_ = angle2Points (p1, p2); -%! assert (size (p1, 1), size (angle_, 1)); -%! res = [0;pi/4;pi/2;3*pi/4]; -%! assert (res, angle_, 1e-6); - -%!test -%! % all points inside window, possibly touching edges -%! p1 = [0 0]; -%! p2 = [10 0;10 10;0 10;-10 10]; -%! angle_ = angle2Points (p1, p2); -%! assert(size (p2, 1), size (angle_, 1)); -%! res = [0;pi/4;pi/2;3*pi/4]; -%! assert(res, angle_,1e-6); - - Deleted: trunk/octave-forge/main/geometry/geom2d/angle3Points.m =================================================================== --- trunk/octave-forge/main/geometry/geom2d/angle3Points.m 2011-10-03 23:11:11 UTC (rev 8667) +++ trunk/octave-forge/main/geometry/geom2d/angle3Points.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -1,82 +0,0 @@ -%% Copyright (c) 2011, INRA -%% 2004-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{alpha} =} angle3Points (@var{p1}, @var{p2}, @var{p3}) -%% Computes the angle between the points @var{p1}, @var{p2} and @var{p3}. -%% -%% @var{p1}, @var{p2} and @var{p3} are either [1x2] arrays, or [Nx2] arrays, in this case -%% @var{alpha} is a [Nx1] array. The angle computed is the directed angle between line -%% (@var{p2}@var{p1}) and line (@var{p2}@var{p3}). -%% -%% Result is always given in radians, between 0 and 2*pi. -%% -%% @seealso{points2d, angles2d, angle2points} -%% @end deftypefn - -function theta = angle3Points(varargin) - - if length(varargin)==3 - p1 = varargin{1}; - p2 = varargin{2}; - p3 = varargin{3}; - elseif length(varargin)==1 - var = varargin{1}; - p1 = var(1,:); - p2 = var(2,:); - p3 = var(3,:); - end - - % angle line (P2 P1) - theta = lineAngle(createLine(p2, p1), createLine(p2, p3)); - -endfunction - -%!test -%! % all points inside window, possibly touching edges -%! p1 = [10 0]; -%! p2 = [0 0]; -%! p3 = [0 10]; -%! angle_ = angle3Points(p1, p2, p3); -%! assert(pi/2, angle_,1e-6); -%! angle_ = angle3Points([p1; p2; p3]); -%! assert(pi/2, angle_, 1e-6); - -%!test -%! p1 = [10 0; 20 0]; -%! p2 = [0 0;0 0]; -%! p3 = [0 10; 0 20]; -%! angle_ = angle3Points(p1, p2, p3); -%! assert(2, size(angle_, 1)); -%! assert([pi/2;pi/2], angle_, 1e-6); - Deleted: trunk/octave-forge/main/geometry/geom2d/angleAbsDiff.m =================================================================== --- trunk/octave-forge/main/geometry/geom2d/angleAbsDiff.m 2011-10-03 23:11:11 UTC (rev 8667) +++ trunk/octave-forge/main/geometry/geom2d/angleAbsDiff.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -1,66 +0,0 @@ -%% Copyright (c) 2011, INRA -%% 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{dif} =} angleAbsDiff (@var{angle1}, @var{angle2}) -%% Computes the absolute angular difference between two angles in radians. -%% The result is comprised between 0 and pi. -%% -%% @example -%% A = angleAbsDiff(pi/2, pi/3) -%% A = -%% 0.5236 % equal to pi/6 -%% @end example -%% -%% @seealso{angles2d, angleDiff} -%% @end deftypefn - -function dif = angleAbsDiff(angle1, angle2) - - % first, normalization - angle1 = normalizeAngle(angle1); - angle2 = normalizeAngle(angle2); - - % compute difference and normalize - dif = normalizeAngle(angle1 - angle2); - dif = min(dif, 2*pi - dif); - -endfunction - -%!shared xp -%! xp = pi/2; -%!assert (xp, angleAbsDiff (pi/2, 0), 1e-6); -%!assert (xp, angleAbsDiff (0, pi/2), 1e-6); -%!assert (xp, angleAbsDiff (0, 3*pi/2), 1e-6); -%!assert (xp, angleAbsDiff (3*pi/2, 0), 1e-6); - Copied: trunk/octave-forge/main/geometry/geom2d/inst/angle2Points.m (from rev 8667, trunk/octave-forge/main/geometry/geom2d/angle2Points.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/angle2Points.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/angle2Points.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -0,0 +1,109 @@ +%% 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{alpha} =} angle2Points (@var{p1}, @var{p2}) +%% Compute horizontal angle between 2 points +%% +%% @var{p1} and @var{p2} are either [1x2] arrays, or [Nx2] arrays, in this case +%% @var{alpha} is a [Nx1] array. The angle computed is the horizontal angle of +%% the line (@var{p1},@var{p2}). +%% +%% Result is always given in radians, between 0 and 2*pi. +%% +%% @seealso{points2d, angles2d, angle3points, normalizeAngle, vectorAngle} +%% @end deftypefn + +function theta = angle2Points(varargin) + + % process input arguments + if length(varargin)==2 + p1 = varargin{1}; + p2 = varargin{2}; + elseif length(varargin)==1 + var = varargin{1}; + p1 = var(1,:); + p2 = var(2,:); + end + + % ensure data have correct size + n1 = size(p1, 1); + n2 = size(p2, 1); + if n1~=n2 && min(n1, n2)>1 + error('angle2Points: wrong size for inputs'); + end + + % angle of line (P2 P1), between 0 and 2*pi. + dp = bsxfun(@minus, p2, p1); + theta = mod(atan2(dp(:,2), dp(:,1)) + 2*pi, 2*pi); + +endfunction + +%!test +%! % all points inside window, possibly touching edges +%! p1 = [0 0]; +%! p2 = [10 0]; +%! angle_ = angle2Points (p1, p2); +%! assert (angle_,0,1e-6); +%! angle_ = angle2Points (p2, p1); +%! assert (angle_,pi,1e-6); + + +%!test +%! % all points inside window, possibly touching edges +%! p1 = [0 0]; +%! p2 = [0 10]; +%! angle_ = angle2Points (p1, p2); +%! assert (pi/2, angle_,1e-6); +%! angle_ = angle2Points (p2, p1); +%! assert (3*pi/2, angle_,1e-6); + +%!test +%! % all points inside window, possibly touching edges +%! p1 = [0 0;0 0;0 0;0 0]; +%! p2 = [10 0;10 10;0 10;-10 10]; +%! angle_ = angle2Points (p1, p2); +%! assert (size (p1, 1), size (angle_, 1)); +%! res = [0;pi/4;pi/2;3*pi/4]; +%! assert (res, angle_, 1e-6); + +%!test +%! % all points inside window, possibly touching edges +%! p1 = [0 0]; +%! p2 = [10 0;10 10;0 10;-10 10]; +%! angle_ = angle2Points (p1, p2); +%! assert(size (p2, 1), size (angle_, 1)); +%! res = [0;pi/4;pi/2;3*pi/4]; +%! assert(res, angle_,1e-6); + + Copied: trunk/octave-forge/main/geometry/geom2d/inst/angle3Points.m (from rev 8666, trunk/octave-forge/main/geometry/geom2d/angle3Points.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/angle3Points.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/angle3Points.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -0,0 +1,82 @@ +%% Copyright (c) 2011, INRA +%% 2004-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{alpha} =} angle3Points (@var{p1}, @var{p2}, @var{p3}) +%% Computes the angle between the points @var{p1}, @var{p2} and @var{p3}. +%% +%% @var{p1}, @var{p2} and @var{p3} are either [1x2] arrays, or [Nx2] arrays, in this case +%% @var{alpha} is a [Nx1] array. The angle computed is the directed angle between line +%% (@var{p2}@var{p1}) and line (@var{p2}@var{p3}). +%% +%% Result is always given in radians, between 0 and 2*pi. +%% +%% @seealso{points2d, angles2d, angle2points} +%% @end deftypefn + +function theta = angle3Points(varargin) + + if length(varargin)==3 + p1 = varargin{1}; + p2 = varargin{2}; + p3 = varargin{3}; + elseif length(varargin)==1 + var = varargin{1}; + p1 = var(1,:); + p2 = var(2,:); + p3 = var(3,:); + end + + % angle line (P2 P1) + theta = lineAngle(createLine(p2, p1), createLine(p2, p3)); + +endfunction + +%!test +%! % all points inside window, possibly touching edges +%! p1 = [10 0]; +%! p2 = [0 0]; +%! p3 = [0 10]; +%! angle_ = angle3Points(p1, p2, p3); +%! assert(pi/2, angle_,1e-6); +%! angle_ = angle3Points([p1; p2; p3]); +%! assert(pi/2, angle_, 1e-6); + +%!test +%! p1 = [10 0; 20 0]; +%! p2 = [0 0;0 0]; +%! p3 = [0 10; 0 20]; +%! angle_ = angle3Points(p1, p2, p3); +%! assert(2, size(angle_, 1)); +%! assert([pi/2;pi/2], angle_, 1e-6); + Copied: trunk/octave-forge/main/geometry/geom2d/inst/angleAbsDiff.m (from rev 8666, trunk/octave-forge/main/geometry/geom2d/angleAbsDiff.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/angleAbsDiff.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/angleAbsDiff.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -0,0 +1,66 @@ +%% Copyright (c) 2011, INRA +%% 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{dif} =} angleAbsDiff (@var{angle1}, @var{angle2}) +%% Computes the absolute angular difference between two angles in radians. +%% The result is comprised between 0 and pi. +%% +%% @example +%% A = angleAbsDiff(pi/2, pi/3) +%% A = +%% 0.5236 % equal to pi/6 +%% @end example +%% +%% @seealso{angles2d, angleDiff} +%% @end deftypefn + +function dif = angleAbsDiff(angle1, angle2) + + % first, normalization + angle1 = normalizeAngle(angle1); + angle2 = normalizeAngle(angle2); + + % compute difference and normalize + dif = normalizeAngle(angle1 - angle2); + dif = min(dif, 2*pi - dif); + +endfunction + +%!shared xp +%! xp = pi/2; +%!assert (xp, angleAbsDiff (pi/2, 0), 1e-6); +%!assert (xp, angleAbsDiff (0, pi/2), 1e-6); +%!assert (xp, angleAbsDiff (0, 3*pi/2), 1e-6); +%!assert (xp, angleAbsDiff (3*pi/2, 0), 1e-6); + Copied: trunk/octave-forge/main/geometry/geom2d/inst/normalizeAngle.m (from rev 8666, trunk/octave-forge/main/geometry/geom2d/normalizeAngle.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/normalizeAngle.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/normalizeAngle.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -0,0 +1,96 @@ +%% 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{alpha2} =} normalizeAngle (@var{alpha}) +%% @deftypefnx {Function File} {@var{alpha2} =} normalizeAngle (@var{alpha}, @var{center}) +%% NORMALIZEANGLE Normalize an angle value within a 2*PI interval +%% +%% ALPHA2 = normalizeAngle(ALPHA); +%% ALPHA2 is the same as ALPHA modulo 2*PI and is positive. +%% +%% ALPHA2 = normalizeAngle(ALPHA, CENTER); +%% Specifies the center of the angle interval. +%% If CENTER==0, the interval is [-pi ; +pi] +%% If CENTER==PI, the interval is [0 ; 2*pi] (default). +%% +%% Example: +%% % normalization between 0 and 2*pi (default) +%% normalizeAngle(5*pi) +%% ans = +%% 3.1416 +%% +%% % normalization between -pi and +pi +%% normalizeAngle(7*pi/2, 0) +%% ans = +%% -1.5708 +%% +%% References +%% Follows the same convention as apache commons library, see: +%% http://commons.apache.org/math/api-2.2/org/apache/commons/math/util/MathUtils.html%% +%% +%% @seealso{vectorAngle, lineAngle} +%% @end deftypefn + +function alpha = normalizeAngle(alpha, varargin) + + center = pi; + if ~isempty(varargin) + center = varargin{1}; + end + + alpha = mod(alpha-center+pi, 2*pi) + center-pi; + +endfunction + +%!assert (pi/2, normalizeAngle (pi/2), 1e-6); +%!assert (pi, normalizeAngle (pi), 1e-6); +%!assert (3*pi/2, normalizeAngle (3*pi/2), 1e-6); +%!assert (pi/2, normalizeAngle (pi/2, pi), 1e-6); +%!assert (pi, normalizeAngle (pi, pi), 1e-6); +%!assert (3*pi/2, normalizeAngle (3*pi/2, pi), 1e-6); + +%!test +%! theta = linspace(0, 2*pi-.1, 100); +%! assert(theta, normalizeAngle (theta), 1e-6); + +%!assert (0, normalizeAngle (0, 0), 1e-6); +%!assert (pi/2, normalizeAngle (pi/2, 0), 1e-6); +%!assert (-pi, normalizeAngle (-pi, 0), 1e-6); +%!assert (-pi/2, normalizeAngle (7*pi/2, 0), 1e-6); + +%!test +%! theta = linspace(-pi+.1, pi-.1, 100); +%! assert(theta, normalizeAngle (theta, 0), 1e-6); + + Copied: trunk/octave-forge/main/geometry/geom2d/inst/vectorAngle.m (from rev 8667, trunk/octave-forge/main/geometry/geom2d/vectorAngle.m) =================================================================== --- trunk/octave-forge/main/geometry/geom2d/inst/vectorAngle.m (rev 0) +++ trunk/octave-forge/main/geometry/geom2d/inst/vectorAngle.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -0,0 +1,221 @@ +%% 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{alpha} =} vectorAngle (@var{v1}) +%%VECTORANGLE Angle of a vector, or between 2 vectors +%% +%% A = vectorAngle(V); +%% Returns angle between Ox axis and vector direction, in Counter +%% clockwise orientation. +%% The result is normalised between 0 and 2*PI. +%% +%% A = vectorAngle(V1, V2); +%% Returns the angle from vector V1 to vector V2, in counter-clockwise +%% order, and in radians. +%% +%% A = vectorAngle(..., 'cutAngle', CUTANGLE); +%% A = vectorAngle(..., CUTANGLE); % (deprecated syntax) +%% Specifies convention for angle interval. CUTANGLE is the center of the +%% 2*PI interval containing the result. See <a href="matlab:doc +%% ('normalizeAngle')">normalizeAngle</a> for details. +%% +%% Example: +%% rad2deg(vectorAngle([2 2])) +%% ans = +%% 45 +%% rad2deg(vectorAngle([1 sqrt(3)])) +%% ans = +%% 60 +%% rad2deg(vectorAngle([0 -1])) +%% ans = +%% 270 +%% +%% @seealso{vectors2d, angles2d, normalizeAngle} +%% @end deftypefn + +function alpha = vectorAngle(v1, varargin) + + %% Initializations + + % default values + v2 = []; + cutAngle = pi; + + % process input arguments + while ~isempty(varargin) + var = varargin{1}; + if isnumeric(var) && isscalar(var) + % argument is normalization constant + cutAngle = varargin{1}; + varargin(1) = []; + + elseif isnumeric(var) && size(var, 2) == 2 + % argument is second vector + v2 = varargin{1}; + varargin(1) = []; + + elseif ischar(var) && length(varargin) >= 2 + % argument is option given as string + value + if strcmpi(var, 'cutAngle') + cutAngle = varargin{2}; + varargin(1:2) = []; + + else + error(['Unknown option: ' var]); + end + + else + error('Unable to parse inputs'); + end + end + + + %% Case of one vector + + % If only one vector is provided, computes its angle + if isempty(v2) + % compute angle and format result in a 2*pi interval + alpha = atan2(v1(:,2), v1(:,1)); + + % normalize within a 2*pi interval + alpha = normalizeAngle(alpha + 2*pi, cutAngle); + + return; + end + + + %% Case of two vectors + + % compute angle of each vector + alpha1 = atan2(v1(:,2), v1(:,1)); + alpha2 = atan2(v2(:,2), v2(:,1)); + + % difference + alpha = bsxfun(@minus, alpha2, alpha1); + + % normalize within a 2*pi interval + alpha = normalizeAngle(alpha + 2*pi, cutAngle); + +endfunction + +%!test +%! ang = vectorAngle([1 0]); +%! assert(0, ang, 1e-6); + +%!test +%! ang = vectorAngle([0 1]); +%! assert(pi/2, ang, 1e-6); + +%!test +%! ang = vectorAngle([-1 0]); +%! assert(pi, ang, 1e-6); + +%!test +%! ang = vectorAngle([0 -1]); +%! assert(3*pi/2, ang, 1e-6); + +%!test +%! ang = vectorAngle([-1 1]); +%! assert(3*pi/4, ang, 1e-6); + +%!test +%! ang = vectorAngle([1 0], pi); +%! assert(0, ang, 1e-6); + +%!test +%! ang = vectorAngle([0 1], pi); +%! assert(pi/2, ang, 1e-6); + +%!test +%! ang = vectorAngle([-1 0], pi); +%! assert(pi, ang, 1e-6); + +%!test +%! ang = vectorAngle([0 -1], pi); +%! assert(3*pi/2, ang, 1e-6); + +%!test +%! ang = vectorAngle([-1 1], pi); +%! assert(3*pi/4, ang, 1e-6); + +%!test +%! vecs = [1 0;0 1;-1 0;0 -1;1 1]; +%! angs = [0;pi/2;pi;3*pi/2;pi/4]; +%! assert(angs, vectorAngle(vecs)); +%! assert(angs, vectorAngle(vecs, pi)); + +%!test +%! ang = vectorAngle([1 0], 0); +%! assert(0, ang, 1e-6); + +%!test +%! ang = vectorAngle([0 1], 0); +%! assert(pi/2, ang, 1e-6); + +%!test +%! ang = vectorAngle([0 -1], 0); +%! assert(-pi/2, ang, 1e-6); + +%!test +%! ang = vectorAngle([-1 1], 0); +%! assert(3*pi/4, ang, 1e-6); + +%!test +%! vecs = [1 0;0 1;0 -1;1 1;1 -1]; +%! angs = [0;pi/2;-pi/2;pi/4;-pi/4]; +%! assert(angs, vectorAngle(vecs, 0)); + +%!test +%! v1 = [1 0]; +%! v2 = [0 1]; +%! ang = pi /2 ; +%! assert(ang, vectorAngle(v1, v2)); + +%!test +%! v1 = [1 0]; +%! v2 = [0 1; 0 1; 1 1; -1 1]; +%! ang = [pi / 2 ;pi / 2 ;pi / 4 ; 3 * pi / 4]; +%! assert(ang, vectorAngle(v1, v2)); + +%!test +%! v1 = [0 1; 0 1; 1 1; -1 1]; +%! v2 = [-1 0]; +%! ang = [pi / 2 ;pi / 2 ; 3 * pi / 4 ; pi / 4]; +%! assert(ang, vectorAngle(v1, v2)); + +%!test +%! v1 = [1 0; 0 1; 1 1; -1 1]; +%! v2 = [0 1; 1 0; -1 1; -1 0]; +%! ang = [pi / 2 ;3 * pi / 2 ;pi / 2 ; pi / 4]; +%! assert(ang, vectorAngle(v1, v2)); Deleted: trunk/octave-forge/main/geometry/geom2d/normalizeAngle.m =================================================================== --- trunk/octave-forge/main/geometry/geom2d/normalizeAngle.m 2011-10-03 23:11:11 UTC (rev 8667) +++ trunk/octave-forge/main/geometry/geom2d/normalizeAngle.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -1,96 +0,0 @@ -%% 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{alpha2} =} normalizeAngle (@var{alpha}) -%% @deftypefnx {Function File} {@var{alpha2} =} normalizeAngle (@var{alpha}, @var{center}) -%% NORMALIZEANGLE Normalize an angle value within a 2*PI interval -%% -%% ALPHA2 = normalizeAngle(ALPHA); -%% ALPHA2 is the same as ALPHA modulo 2*PI and is positive. -%% -%% ALPHA2 = normalizeAngle(ALPHA, CENTER); -%% Specifies the center of the angle interval. -%% If CENTER==0, the interval is [-pi ; +pi] -%% If CENTER==PI, the interval is [0 ; 2*pi] (default). -%% -%% Example: -%% % normalization between 0 and 2*pi (default) -%% normalizeAngle(5*pi) -%% ans = -%% 3.1416 -%% -%% % normalization between -pi and +pi -%% normalizeAngle(7*pi/2, 0) -%% ans = -%% -1.5708 -%% -%% References -%% Follows the same convention as apache commons library, see: -%% http://commons.apache.org/math/api-2.2/org/apache/commons/math/util/MathUtils.html%% -%% -%% @seealso{vectorAngle, lineAngle} -%% @end deftypefn - -function alpha = normalizeAngle(alpha, varargin) - - center = pi; - if ~isempty(varargin) - center = varargin{1}; - end - - alpha = mod(alpha-center+pi, 2*pi) + center-pi; - -endfunction - -%!assert (pi/2, normalizeAngle (pi/2), 1e-6); -%!assert (pi, normalizeAngle (pi), 1e-6); -%!assert (3*pi/2, normalizeAngle (3*pi/2), 1e-6); -%!assert (pi/2, normalizeAngle (pi/2, pi), 1e-6); -%!assert (pi, normalizeAngle (pi, pi), 1e-6); -%!assert (3*pi/2, normalizeAngle (3*pi/2, pi), 1e-6); - -%!test -%! theta = linspace(0, 2*pi-.1, 100); -%! assert(theta, normalizeAngle (theta), 1e-6); - -%!assert (0, normalizeAngle (0, 0), 1e-6); -%!assert (pi/2, normalizeAngle (pi/2, 0), 1e-6); -%!assert (-pi, normalizeAngle (-pi, 0), 1e-6); -%!assert (-pi/2, normalizeAngle (7*pi/2, 0), 1e-6); - -%!test -%! theta = linspace(-pi+.1, pi-.1, 100); -%! assert(theta, normalizeAngle (theta, 0), 1e-6); - - Deleted: trunk/octave-forge/main/geometry/geom2d/vectorAngle.m =================================================================== --- trunk/octave-forge/main/geometry/geom2d/vectorAngle.m 2011-10-03 23:11:11 UTC (rev 8667) +++ trunk/octave-forge/main/geometry/geom2d/vectorAngle.m 2011-10-03 23:13:22 UTC (rev 8668) @@ -1,221 +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. - -%% -*- texinfo -*- -%% @deftypefn {Function File} {@var{alpha} =} vectorAngle (@var{v1}) -%%VECTORANGLE Angle of a vector, or between 2 vectors -%% -%% A = vectorAngle(V); -%% Returns angle between Ox axis and vector direction, in Counter -%% clockwise orientation. -%% The result is normalised between 0 and 2*PI. -%% -%% A = vectorAngle(V1, V2); -%% Returns the angle from vector V1 to vector V2, in counter-clockwise -%% order, and in radians. -%% -%% A = vectorAngle(..., 'cutAngle', CUTANGLE); -%% A = vectorAngle(..., CUTANGLE); % (deprecated syntax) -%% Specifies convention for angle interval. CUTANGLE is the center of the -%% 2*PI interval containing the result. See <a href="matlab:doc -%% ('normalizeAngle')">normalizeAngle</a> for details. -%% -%% Example: -%% rad2deg(vectorAngle([2 2])) -%% ans = -%% 45 -%% rad2deg(vectorAngle([1 sqrt(3)])) -%% ans = -%% 60 -%% rad2deg(vectorAngle([0 -1])) -%% ans = -%% 270 -%% -%% @seealso{vectors2d, angles2d, normalizeAngle} -%% @end deftypefn - -function alpha = vectorAngle(v1, varargin) - - %% Initializations - - % default values - v2 = []; - cutAngle = pi; - - % process input arguments - while ~isempty(varargin) - var = varargin{1}; - if isnumeric(var) && isscalar(var) - % argument is normalization constant - cutAngle = varargin{1}; - varargin(1) = []; - - elseif isnumeric(var) && size(var, 2) == 2 - % argument is second vector - v2 = varargin{1}; - varargin(1) = []; - - elseif ischar(var) && length(varargin) >= 2 - % argument is option given as string + value - if strcmpi(var, 'cutAngle') - cutAngle = varargin{2}; - varargin(1:2) = []; - - else - error(['Unknown option: ' var]); - end - - else - error('Unable to parse inputs'); - end - end - - - %% Case of one vector - - % If only one vector is provided, computes its angle - if isempty(v2) - % compute angle and format result in a 2*pi interval - alpha = atan2(v1(:,2), v1(:,1)); - - % normalize within a 2*pi interval - alpha = normalizeAngle(alpha + 2*pi, cutAngle); - - return; - end - - - %% Case of two vectors - - % compute angle of each vector - alpha1 = atan2(v1(:,2), v1(:,1)); - alpha2 = atan2(v2(:,2), v2(:,1)); - - % difference - alpha = bsxfun(@minus, alpha2, alpha1); - - % normalize within a 2*pi interval - alpha = normalizeAngle(alpha + 2*pi, cutAngle); - -endfunction - -%!test -%! ang = vectorAngle([1 0]); -%! assert(0, ang, 1e-6); - -%!test -%! ang = vectorAngle([0 1]); -%! assert(pi/2, ang, 1e-6); - -%!test -%! ang = vectorAngle([-1 0]); -%! assert(pi, ang, 1e-6); - -%!test -%! ang = vectorAngle([0 -1]); -%! assert(3*pi/2, ang, 1e-6); - -%!test -%! ang = vectorAngle([-1 1]); -%! assert(3*pi/4, ang, 1e-6); - -%!test -%! ang = vectorAngle([1 0], pi); -%! assert(0, ang, 1e-6); - -%!test -%! ang = vectorAngle([0 1], pi); -%! assert(pi/2, ang, 1e-6); - -%!test -%! ang = vectorAngle([-1 0], pi); -%! assert(pi, ang, 1e-6); - -%!test -%! ang = vectorAngle([0 -1], pi); -%! assert(3*pi/2, ang, 1e-6); - -%!test -%! ang = vectorAngle([-1 1], pi); -%! assert(3*pi/4, ang, 1e-6); - -%!test -%! vecs = [1 0;0 1;-1 0;0 -1;1 1]; -%! angs = [0;pi/2;pi;3*pi/2;pi/4]; -%! assert(angs, vectorAngle(vecs)); -%! assert(angs, vectorAngle(vecs, pi)); - -%!test -%! ang = vectorAngle([1 0], 0); -%! assert(0, ang, 1e-6); - -%!test -%! ang = vectorAngle([0 1], 0); -%! assert(pi/2, ang, 1e-6); - -%!test -%! ang = vectorAngle([0 -1], 0); -%! assert(-pi/2, ang, 1e-6); - -%!test -%! ang = vectorAngle([-1 1], 0); -%! assert(3*pi/4, ang, 1e-6); - -%!test -%! vecs = [1 0;0 1;0 -1;1 1;1 -1]; -%! angs = [0;pi/2;-pi/2;pi/4;-pi/4]; -%! assert(angs, vectorAngle(vecs, 0)); - -%!test -%! v1 = [1 0]; -%! v2 = [0 1]; -%! ang = pi /2 ; -%! assert(ang, vectorAngle(v1, v2)); - -%!test -%! v1 = [1 0]; -%! v2 = [0 1; 0 1; 1 1; -1 1]; -%! ang = [pi / 2 ;pi / 2 ;pi / 4 ; 3 * pi / 4]; -%! assert(ang, vectorAngle(v1, v2)); - -%!test -%! v1 = [0 1; 0 1; 1 1; -1 1]; -%! v2 = [-1 0]; -%! ang = [pi / 2 ;pi / 2 ; 3 * pi / 4 ; pi / 4]; -%! assert(ang, vectorAngle(v1, v2)); - -%!test -%! v1 = [1 0; 0 1; 1 1; -1 1]; -%! v2 = [0 1; 1 0; -1 1; -1 0]; -%! ang = [pi / 2 ;3 * pi / 2 ;pi / 2 ; pi / 4]; -%! assert(ang, vectorAngle(v1, v2)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |