]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/normalizeAngle.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / normalizeAngle.m
1 %% Copyright (c) 2011, INRA
2 %% 2008-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 %%
5 %% All rights reserved.
6 %% (simplified BSD License)
7 %%
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %%    list of conditions and the following disclaimer.
13 %%     
14 %% 2. Redistributions in binary form must reproduce the above copyright notice, 
15 %%    this list of conditions and the following disclaimer in the documentation
16 %%    and/or other materials provided with the distribution.
17 %%
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
29 %%
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
33
34 %% -*- texinfo -*-
35 %% @deftypefn {Function File} {@var{alpha2} =} normalizeAngle (@var{alpha})
36 %% @deftypefnx {Function File} {@var{alpha2} =} normalizeAngle (@var{alpha}, @var{center})
37 %% Normalize an angle value within a 2*PI interval
38 %%
39 %%   ALPHA2 = normalizeAngle(ALPHA);
40 %%   ALPHA2 is the same as ALPHA modulo 2*PI and is positive.
41 %%
42 %%   ALPHA2 = normalizeAngle(ALPHA, CENTER);
43 %%   Specifies the center of the angle interval.
44 %%   If CENTER==0, the interval is [-pi ; +pi]
45 %%   If CENTER==PI, the interval is [0 ; 2*pi] (default).
46 %%
47 %%   Example:
48 %%   % normalization between 0 and 2*pi (default)
49 %%   normalizeAngle(5*pi)
50 %%   ans =
51 %%       3.1416
52 %%
53 %%   % normalization between -pi and +pi
54 %%   normalizeAngle(7*pi/2, 0)
55 %%   ans =
56 %%       -1.5708
57 %%
58 %%   References
59 %%   Follows the same convention as apache commons library, see:
60 %%   http://commons.apache.org/math/api-2.2/org/apache/commons/math/util/MathUtils.html%% 
61 %%
62 %% @seealso{vectorAngle, lineAngle}
63 %% @end deftypefn
64
65 function alpha = normalizeAngle(alpha, varargin)
66
67   center = pi;
68   if ~isempty(varargin)
69       center = varargin{1};
70   end
71
72   alpha = mod(alpha-center+pi, 2*pi) + center-pi;
73
74 endfunction
75
76 %!assert (pi/2, normalizeAngle (pi/2), 1e-6);
77 %!assert (pi, normalizeAngle (pi), 1e-6);
78 %!assert (3*pi/2, normalizeAngle (3*pi/2), 1e-6);
79 %!assert (pi/2, normalizeAngle (pi/2, pi), 1e-6);
80 %!assert (pi, normalizeAngle (pi, pi), 1e-6);
81 %!assert (3*pi/2, normalizeAngle (3*pi/2, pi), 1e-6);
82
83 %!test
84 %! theta = linspace(0, 2*pi-.1, 100);
85 %! assert(theta, normalizeAngle (theta), 1e-6);
86
87 %!assert (0, normalizeAngle (0, 0), 1e-6);
88 %!assert (pi/2, normalizeAngle (pi/2, 0), 1e-6);
89 %!assert (-pi, normalizeAngle (-pi, 0), 1e-6);
90 %!assert (-pi/2, normalizeAngle (7*pi/2, 0), 1e-6);
91
92 %!test
93 %! theta = linspace(-pi+.1, pi-.1, 100);
94 %! assert(theta, normalizeAngle (theta, 0), 1e-6);
95
96