3452
|
1 ## Copyright (C) 1998 Auburn University. All rights reserved. |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{a}, @var{b}, @var{c}, @var{d}] =} quaternion (w) |
|
22 ## @deftypefnx {Function File} {[@var{vv}, @var{theta}] =} quaternion (w) |
|
23 ## @deftypefnx {Function File} {@var{w} =} quaternion (@var{a}, @var{b}, @var{c}, @var{d}) |
|
24 ## @deftypefnx {Function File} {@var{w} =} quaternion (@var{vv}, @var{theta}) |
|
25 ## Construct or extract a quaternion |
|
26 ## |
|
27 ## @example |
|
28 ## w = a*i + b*j + c*k + d |
|
29 ## @end example |
|
30 ## |
|
31 ## @noindent |
|
32 ## from given data. |
|
33 ## @end deftypefn |
|
34 |
|
35 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
36 ## Adapted-By: jwe |
|
37 |
|
38 function [a, b, c, d] = quaternion (w, x, y, z) |
|
39 |
|
40 switch (nargin) |
|
41 case(1) |
|
42 if (! (is_vector (w) && length (w) == 4)) |
|
43 error ("input vector must be of length 4)"); |
|
44 endif |
|
45 ## extract data |
|
46 switch (nargout) |
|
47 case(4) |
|
48 a = w(1); |
|
49 b = w(2); |
|
50 c = w(3); |
|
51 d = w(4); |
3224
|
52 |
3452
|
53 case(2) |
|
54 if (abs (norm (w) - 1) > 1e-12) |
|
55 warning ("quaternion: ||w||=%e, setting=1 for vv, theta", norm(w)); |
|
56 w = w/norm(w); |
|
57 endif |
|
58 [a, b, c, d] = quaternion (w); |
|
59 theta = acos (d) * 2; |
|
60 if (abs (theta) > pi) |
|
61 theta = theta - sign (theta) * pi; |
|
62 endif |
|
63 sin_th_2 = norm ([a, b, c]); |
|
64 |
|
65 if (sin_th_2 != 0) |
|
66 vv = [a, b, c] / sin_th_2; |
|
67 else |
|
68 vv = [a, b, c]; |
|
69 endif |
|
70 a = vv; |
|
71 b = theta; |
|
72 otherwise |
|
73 usage ("[a, b, c, d] = quaternion (w) or [vv, theta] = quaternion (w)"); |
|
74 endswitch |
|
75 |
|
76 case(2) |
|
77 if (nargout != 1) |
|
78 usage ("w = quaterion (vv, theta)"); |
3224
|
79 endif |
3452
|
80 vv = w; |
|
81 theta = x; |
|
82 |
|
83 if (! is_vector (vv) || length (vv) != 3) |
|
84 error ("vv must be a length three vector"); |
|
85 elseif (! is_scalar (theta)) |
|
86 error ("theta must be a scalar"); |
|
87 elseif (norm (vv) == 0) |
3458
|
88 error ("quaternion: vv is zero"); |
3452
|
89 elseif (abs (norm (vv) - 1) > 1e-12) |
|
90 warning ("quaternion: ||vv|| != 1, normalizing") |
|
91 vv = vv / norm (vv); |
3224
|
92 endif |
|
93 |
3452
|
94 if (abs (theta) > 2*pi) |
|
95 warning ("quaternion: |theta| > 2 pi, normalizing") |
|
96 theta = rem (theta, 2*pi); |
3224
|
97 endif |
3452
|
98 vv = vv * sin (theta / 2); |
|
99 d = cos (theta / 2); |
|
100 a = quaternion (vv(1), vv(2), vv(3), d); |
|
101 |
|
102 case(4) |
|
103 if (nargout != 1) |
|
104 usage ("w = quaterion (a, b, c, d)"); |
|
105 endif |
|
106 if (! (is_scalar (w) && is_scalar (x) && is_scalar (y) && is_scalar (z))) |
3458
|
107 error ("input values must be scalars"); |
3452
|
108 endif |
|
109 a = [w, x, y, z]; |
|
110 |
|
111 otherwise |
3457
|
112 usage ("[a, b, c, d] = quaternion (w) or a = quaternion (w, x, y, z)"); |
3452
|
113 |
3224
|
114 endswitch |
|
115 |
|
116 endfunction |