]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/vectorAngle.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / vectorAngle.m
1 %% Copyright (c) 2011, INRA
2 %% 2007-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{alpha} =} vectorAngle (@var{v1})
36 %% Angle of a vector, or between 2 vectors
37 %%
38 %%   A = vectorAngle(V);
39 %%   Returns angle between Ox axis and vector direction, in Counter
40 %%   clockwise orientation.
41 %%   The result is normalised between 0 and 2*PI.
42 %%
43 %%   A = vectorAngle(V1, V2);
44 %%   Returns the angle from vector V1 to vector V2, in counter-clockwise
45 %%   order, and in radians.
46 %%
47 %%   A = vectorAngle(..., 'cutAngle', CUTANGLE);
48 %%   A = vectorAngle(..., CUTANGLE); % (deprecated syntax)
49 %%   Specifies convention for angle interval. CUTANGLE is the center of the
50 %%   2*PI interval containing the result. See <a href="matlab:doc
51 %%   ('normalizeAngle')">normalizeAngle</a> for details.
52 %%
53 %%   Example:
54 %%   rad2deg(vectorAngle([2 2]))
55 %%   ans =
56 %%       45
57 %%   rad2deg(vectorAngle([1 sqrt(3)]))
58 %%   ans =
59 %%       60
60 %%   rad2deg(vectorAngle([0 -1]))
61 %%   ans =
62 %%       270
63 %% 
64 %% @seealso{vectors2d, angles2d, normalizeAngle}
65 %% @end deftypefn
66
67 function alpha = vectorAngle(v1, varargin)
68
69   %% Initializations
70
71   % default values
72   v2 = [];
73   cutAngle = pi;
74
75   % process input arguments
76   while ~isempty(varargin)
77       var = varargin{1};
78       if isnumeric(var) && isscalar(var)
79           % argument is normalization constant
80           cutAngle = varargin{1};
81           varargin(1) = [];
82           
83       elseif isnumeric(var) && size(var, 2) == 2
84           % argument is second vector
85           v2 = varargin{1};
86           varargin(1) = [];
87           
88       elseif ischar(var) && length(varargin) >= 2
89           % argument is option given as string + value
90           if strcmpi(var, 'cutAngle')
91               cutAngle = varargin{2};
92               varargin(1:2) = [];
93               
94           else
95               error(['Unknown option: ' var]);
96           end
97           
98       else
99           error('Unable to parse inputs');
100       end
101   end
102
103
104   %% Case of one vector
105
106   % If only one vector is provided, computes its angle
107   if isempty(v2)
108       % compute angle and format result in a 2*pi interval
109       alpha = atan2(v1(:,2), v1(:,1));
110       
111       % normalize within a 2*pi interval
112       alpha = normalizeAngle(alpha + 2*pi, cutAngle);
113       
114       return;
115   end
116
117
118   %% Case of two vectors
119
120   % compute angle of each vector
121   alpha1 = atan2(v1(:,2), v1(:,1));
122   alpha2 = atan2(v2(:,2), v2(:,1));
123
124   % difference
125   alpha = bsxfun(@minus, alpha2, alpha1);
126
127   % normalize within a 2*pi interval
128   alpha = normalizeAngle(alpha + 2*pi, cutAngle);
129
130 endfunction
131
132 %!test
133 %! ang = vectorAngle([1 0]);
134 %! assert(0, ang, 1e-6);
135
136 %!test
137 %! ang = vectorAngle([0 1]);
138 %! assert(pi/2, ang, 1e-6);
139
140 %!test
141 %! ang = vectorAngle([-1 0]);
142 %! assert(pi, ang, 1e-6);
143
144 %!test
145 %! ang = vectorAngle([0 -1]);
146 %! assert(3*pi/2, ang, 1e-6);
147
148 %!test
149 %! ang = vectorAngle([-1 1]);
150 %! assert(3*pi/4, ang, 1e-6);
151
152 %!test
153 %! ang = vectorAngle([1 0], pi);
154 %! assert(0, ang, 1e-6);
155
156 %!test
157 %! ang = vectorAngle([0 1], pi);
158 %! assert(pi/2, ang, 1e-6);
159
160 %!test
161 %! ang = vectorAngle([-1 0], pi);
162 %! assert(pi, ang, 1e-6);
163
164 %!test
165 %! ang = vectorAngle([0 -1], pi);
166 %! assert(3*pi/2, ang, 1e-6);
167
168 %!test
169 %! ang = vectorAngle([-1 1], pi);
170 %! assert(3*pi/4, ang, 1e-6);
171
172 %!test
173 %! vecs = [1 0;0 1;-1 0;0 -1;1 1];
174 %! angs = [0;pi/2;pi;3*pi/2;pi/4];
175 %! assert(angs, vectorAngle(vecs));
176 %! assert(angs, vectorAngle(vecs, pi));
177
178 %!test
179 %! ang = vectorAngle([1 0], 0);
180 %! assert(0, ang, 1e-6);
181
182 %!test
183 %! ang = vectorAngle([0 1], 0);
184 %! assert(pi/2, ang, 1e-6);
185
186 %!test
187 %! ang = vectorAngle([0 -1], 0);
188 %! assert(-pi/2, ang, 1e-6);
189
190 %!test
191 %! ang = vectorAngle([-1 1], 0);
192 %! assert(3*pi/4, ang, 1e-6);
193
194 %!test
195 %! vecs = [1 0;0 1;0 -1;1 1;1 -1];
196 %! angs = [0;pi/2;-pi/2;pi/4;-pi/4];
197 %! assert(angs, vectorAngle(vecs, 0), 1e-6);
198
199 %!test
200 %! v1 = [1 0];
201 %! v2 = [0 1];
202 %! ang = pi /2 ;
203 %! assert(ang, vectorAngle(v1, v2), 1e-6);
204
205 %!test
206 %! v1 = [1 0];
207 %! v2 = [0 1; 0 1; 1 1; -1 1];
208 %! ang = [pi / 2 ;pi / 2 ;pi / 4 ; 3 * pi / 4];
209 %! assert(ang, vectorAngle(v1, v2), 1e-6);
210
211 %!test
212 %! v1 = [0 1; 0 1; 1 1; -1 1];
213 %! v2 = [-1 0];
214 %! ang = [pi / 2 ;pi / 2 ; 3 * pi / 4 ; pi / 4];
215 %! assert(ang, vectorAngle(v1, v2), 1e-6);
216
217 %!test
218 %! v1 = [1 0; 0 1; 1 1; -1 1];
219 %! v2 = [0 1; 1 0; -1 1; -1 0];
220 %! ang = [pi / 2 ;3 * pi / 2 ;pi / 2 ; pi / 4];
221 %! assert(ang, vectorAngle(v1, v2), 1e-6);