Index of Functions: A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X 
Index Page
cspice_m2q

Table of contents
Abstract
I/O
Parameters
Examples
Particulars
Exceptions
Files
Restrictions
Required_Reading
Literature_References
Author_and_Institution
Version
Index_Entries


Abstract


   CSPICE_M2Q calculates a unit quaternion corresponding to a
   specified 3x3 double precision, rotation matrix.

I/O


   Given:

      r        a rotation matrix.

               help, r
                  DOUBLE = Array[3,3]

   the call:

      cspice_m2q, r, q

   returns:

      q        a unit-length SPICE-style quaternion representing `r'.

               help, q
                  DOUBLE = Array[4]

               See the discussion of quaternion styles in -Particulars below.

               `q' is a 4-dimensional vector. If `r' rotates vectors in the
               counterclockwise sense by an angle of theta radians about a unit
               vector `a', where

                  0 < theta < pi
                    -       -

               then letting h = theta/2,

                  q = ( cos(h), sin(h)a ,  sin(h)a ,  sin(h)a ).
                                       1          2          3

               The restriction that theta must be in the range [0, pi]
               determines the output quaternion `q' uniquely except when
               theta = pi; in this special case, both of the quaternions

                  q = ( 0,  a ,  a ,  a  )
                             1    2    3
               and

                  q = ( 0, -a , -a , -a  )
                             1    2    3

               are possible outputs.

Parameters


   None.

Examples


   Any numerical results shown for this example may differ between
   platforms as the results depend on the SPICE kernels used as input
   and the machine specific arithmetic implementation.

   1) Create a 3-dimensional rotation matrix of 90 degrees about the
      Z axis and convert it to a unit quaternion. Verify that the
      norm of the quaternion is equal to 1.

      Example code begins here.


      PRO m2q_ex1

         ;;
         ;; Create a rotation matrix of 90 degrees about the Z axis.
         ;;
         cspice_rotate, cspice_halfpi(), 3, r
         print, 'Rotation matrix: '
         print, r

         ;;
         ;; Convert the matrix to a quaternion.
         ;;
         cspice_m2q, r, q
         print, 'Unit quaternion: '
         print, q
         print, ''

         ;;
         ;; Confirm || q || = 1.
         ;;
         print, '|| q || = ', q ## transpose(q)

      END


      When this program was executed on a Mac/Intel/IDL8.x/64-bit
      platform, the output was:


      Rotation matrix:
         6.1232340e-17       1.0000000       0.0000000
            -1.0000000   6.1232340e-17       0.0000000
             0.0000000       0.0000000       1.0000000
      Unit quaternion:
            0.70710678       0.0000000       0.0000000     -0.70710678

      || q || =        1.0000000


      Note, that the call sequence:

         cspice_m2q, r, q
         cspice_q2m, q,r

      preserves 'r' except for round-off error. Yet, the call
      sequence:

         cspice_q2m, q,r
         cspice_m2q, r, q

      may preserve 'q' or return '-q'.

Particulars


   A unit quaternion is a 4-dimensional vector for which the sum of
   the squares of the components is 1. Unit quaternions can be used
   to represent rotations in the following way: given a rotation
   angle theta, where

      0 < theta < pi
        -       -

   and a unit vector `a', we can represent the transformation that
   rotates vectors in the counterclockwise sense by theta radians
   about `a' using the quaternion `q', where

      q = ( cos(theta/2), sin(theta/2)a , sin(theta/2)a , sin(theta/2)a )
                                       1               2               3

   As mentioned in Detailed Output, our restriction on the range of
   theta determines `q' uniquely, except when theta = pi.

   The Icy routine cspice_q2m is an one-sided inverse of this routine:
   given any rotation matrix `r', the calls

      cspice_m2q, r, q
      cspice_q2m, q, r

   leave `r' unchanged, except for round-off error. However, the
   calls

      cspice_q2m, q, r
      cspice_m2q, r, q

   might preserve `q' or convert `q' to -q.



   Quaternion Styles
   -----------------

   There are different "styles" of quaternions used in
   science and engineering applications. Quaternion styles
   are characterized by

   -  The order of quaternion elements

   -  The quaternion multiplication formula

   -  The convention for associating quaternions
      with rotation matrices

   Two of the commonly used styles are

      - "SPICE"

         > Invented by Sir William Rowan Hamilton
         > Frequently used in mathematics and physics textbooks

      - "Engineering"

         > Widely used in aerospace engineering applications


   Icy routine interfaces ALWAYS use SPICE quaternions. Quaternions of any
   other style must be converted to SPICE quaternions before they are passed to
   Icy routines.


   Relationship between SPICE and Engineering Quaternions
   ------------------------------------------------------

   Let `m' be a rotation matrix such that for any vector `v',

      m*v

   is the result of rotating `v' by theta radians in the
   counterclockwise direction about unit rotation axis vector `a'.
   Then the SPICE quaternions representing `m' are

      (+/-) (  cos(theta/2),
               sin(theta/2) a[0],
               sin(theta/2) a[1],
               sin(theta/2) a[2]  )

   while the engineering quaternions representing `m' are

      (+/-) ( -sin(theta/2) a[0],
              -sin(theta/2) a[1],
              -sin(theta/2) a[2],
               cos(theta/2)       )

   For both styles of quaternions, if a quaternion `q' represents
   a rotation matrix `m', then -q represents `m' as well.

   Given an engineering quaternion

      qeng   = ( q0,  q1,  q2,  q3 )

   the equivalent SPICE quaternion is

      qspice = ( q3, -q0, -q1, -q2 )


   Associating SPICE Quaternions with Rotation Matrices
   ----------------------------------------------------

   Let `from' and `to' be two right-handed reference frames, for
   example, an inertial frame and a spacecraft-fixed frame. Let the
   symbols

      v    ,   v
       from     to

   denote, respectively, an arbitrary vector expressed relative to
   the `from' and `to' frames. Let `m' denote the transformation matrix
   that transforms vectors from frame `from' to frame `to'; then

      v   =  m * v
       to         from

   where the expression on the right hand side represents left
   multiplication of the vector by the matrix.

   Then if the unit-length SPICE quaternion q represents `m', where

      q = (q0, q1, q2, q3)

   the elements of `m' are derived from the elements of q as follows:

        .-                                                         -.
        |           2    2                                          |
        | 1 - 2*( q0 + q1 )   2*(q1*q2 - q0*q3)   2*(q1*q3 + q0*q2) |
        |                                                           |
        |                                                           |
        |                               2    2                      |
    m = | 2*(q1*q2 + q0*q3)   1 - 2*( q1 + q3 )   2*(q2*q3 - q0*q1) |
        |                                                           |
        |                                                           |
        |                                                   2    2  |
        | 2*(q1*q3 - q0*q2)   2*(q2*q3 + q0*q1)   1 - 2*( q1 + q2 ) |
        |                                                           |
        `-                                                         -'

   Note that substituting the elements of -q for those of q in the
   right hand side leaves each element of `m' unchanged; this shows
   that if a quaternion q represents a matrix `m', then so does the
   quaternion -q.

   To map the rotation matrix `m' to a unit quaternion, we start by
   decomposing the rotation matrix as a sum of symmetric
   and skew-symmetric parts:

                                      2
      m = [ i  +  (1-cos(theta)) omega  ] + [ sin(theta) omega ]

                   symmetric                   skew-symmetric


   `omega' is a skew-symmetric matrix of the form

                 .-             -.
                 |  0   -n2   n1 |
                 |               |
       omega  =  |  n2   0   -n0 |
                 |               |
                 | -n1   n0   0  |
                 `-             -'

   The vector N of matrix entries (n0, n1, n2) is the rotation axis
   of `m' and theta is m's rotation angle. Note that N and theta
   are not unique.

   Let

      c = cos(theta/2)
      s = sin(theta/2)

   Then the unit quaternions `q' corresponding to `m' are

      q = +/- ( c, s*n0, s*n1, s*n2 )

   The mappings between quaternions and the corresponding rotations
   are carried out by the Icy routines

      cspice_q2m {quaternion to matrix}
      cspice_m2q {matrix to quaternion}

   cspice_m2q always returns a quaternion with scalar part greater than
   or equal to zero.


   SPICE Quaternion Multiplication Formula
   ---------------------------------------

   Given a SPICE quaternion

      q = ( q0, q1, q2, q3 )

   corresponding to rotation axis `a' and angle theta as above, we can
   represent `q' using "scalar + vector" notation as follows:

      s =   q0           = cos(theta/2)

      v = ( q1, q2, q3 ) = sin(theta/2) * a

      q = s + v

   Let `q1' and `q2' be SPICE quaternions with respective scalar
   and vector parts `s1', `s2' and `v1', `v2':

      q1 = s1 + v1
      q2 = s2 + v2

   We represent the dot product of `v1' and `v2' by

      <v1, v2>

   and the cross product of `v1' and `v2' by

      v1 x v2

   Then the SPICE quaternion product is

      q1*q2 = s1*s2 - <v1,v2>  + s1*v2 + s2*v1 + (v1 x v2)

   If `q1' and `q2' represent the rotation matrices `m1' and `m2'
   respectively, then the quaternion product

      q1*q2

   represents the matrix product

      m1*m2

Exceptions


   1)  If `r' is not a rotation matrix, the error SPICE(NOTAROTATION)
       is signaled by a routine in the call tree of this routine.

   2)  If the input argument `r' is undefined, an error is signaled
       by the IDL error handling system.

   3)  If the input argument `r' is not of the expected type, or it
       does not have the expected dimensions and size, an error is
       signaled by the Icy interface.

   4)  If the output argument `q' is not a named variable, an error
       is signaled by the Icy interface.

Files


   None.

Restrictions


   None.

Required_Reading


   ICY.REQ
   ROTATION.REQ

Literature_References


   None.

Author_and_Institution


   J. Diaz del Rio     (ODC Space)
   E.D. Wright         (JPL)

Version


   -Icy Version 1.0.2, 10-AUG-2021 (JDR)

       Edited the header to comply with NAIF standard. Added example's
       problem statement and reformatted example's output.

       Added -Parameters, -Exceptions, -Files, -Restrictions,
       -Literature_References and -Author_and_Institution sections.

       Removed reference to the routine's corresponding CSPICE header from
       -Abstract section.

       Added arguments' type and size information in the -I/O section.

   -Icy Version 1.0.1, 06-NOV-2005 (EDW)

       Updated -Particulars section to include the
       "About SPICE Quaternions" description. Recast
       the -I/O section to meet Icy format standards.

   -Icy Version 1.0.0, 16-JUN-2003 (EDW)

Index_Entries


   matrix to quaternion



Fri Dec 31 18:43:06 2021