1## Copyright (C) 2010-2015   Lukas F. Reichlin
2##
3## This program is free software: you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation, either version 3 of the License, or
6## (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful,
9## but WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11## GNU General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16## -*- texinfo -*-
17## @deftypefn {Function File} {@var{q} =} cast (@var{q}, @var{'type'})
18## Convert the components of quaternion @var{q} to data type @var{type}.
19## Valid types are int8, uint8, int16, uint16, int32, uint32, int64,
20## uint64, double, single and logical.
21## @end deftypefn
22
23## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
24## Created: December 2013
25## Version: 0.1
26
27function qret = cast (q, typ)
28
29  if (nargin != 2 || ! ischar (typ))  # q is always a quaternion, no need to test
30    print_usage ();
31  endif
32
33  if (! any (strcmp (typ, {"int8"; "uint8"; "int16"; "uint16";
34                           "int32"; "uint32"; "int64"; "uint64";
35                           "double"; "single"; "logical"})))
36    error ("quaternion: cast: type name '%s' is not a built-in type", typ);
37  endif
38
39  ## NOTE: Without strcmp, cast could be used to apply any function to the
40  ##       components of a quaternion.  Shall I create such a method with
41  ##       a different name?
42
43  w = feval (typ, q.w);
44  x = feval (typ, q.x);
45  y = feval (typ, q.y);
46  z = feval (typ, q.z);
47
48  qret = quaternion (w, x, y, z);
49
50endfunction
51