/trunk/octave-forge/extra/quaternion-legacy/inst/quaternion.m

# · MATLAB · 114 lines · 101 code · 13 blank · 0 comment · 13 complexity · 35f52194fdb018d7bc7a2596675208c5 MD5 · raw file

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