home *** CD-ROM | disk | FTP | other *** search
/ ftp.hitl.washington.edu / ftp.hitl.washington.edu.tar / ftp.hitl.washington.edu / pub / people / habib / kodak / trackball.cc < prev    next >
C/C++ Source or Header  |  2000-04-18  |  17KB  |  569 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  * Trackball code:
  39.  *
  40.  * Implementation of a virtual trackball.
  41.  * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
  42.  *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
  43.  *
  44.  * Vector manip code:
  45.  *
  46.  * Original code from:
  47.  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  48.  *
  49.  * Much mucking with by:
  50.  * Gavin Bell
  51.  */
  52. #include <math.h>
  53. #include <iostream.h>
  54. #include "trackball.h"
  55. struct point {float x,y,z;} ;
  56. struct mirror { 
  57.    struct point cr,    // center of the mirror.
  58.       c1, c2, c3, c4,  // Those are the 4 corners in the mirror coordinate system.
  59.       cc1,cc2,cc3,cc4; // Those are the 4 corners of the mirror in the camera coordinate system.
  60.    struct point nl;   // normal vector, should have a lenght of 1.
  61.    struct point up;   // up vector
  62.    struct point side; // side vector
  63.    double width;        // this is the width /2
  64.    double height; }  ; // this is the height /2
  65. extern struct point eye,eye1,eye2, eye_mirror,  lk, lk_rt ,up, up_rt, hor, hor_rt, meye, MEM_POINT;  
  66. extern struct mirror m1[100];
  67. /*
  68.  * This size should really be based on the distance from the center of
  69.  * rotation to the point on the object underneath the mouse.  That
  70.  * point would then track the mouse as closely as possible.  This is a
  71.  * simple example, though, so that is left as an Exercise for the
  72.  * Programmer.
  73.  */
  74. #define TRACKBALLSIZE  (0.8)
  75.  
  76. /*
  77.  * Local function prototypes (not defined in trackball.h)
  78.  */
  79. static double tb_project_to_sphere(double, double, double);
  80. static void normalize_quat(double [4]);
  81.  
  82. void
  83. vzero(double *v)
  84. {
  85.     v[0] = 0.0;
  86.     v[1] = 0.0;
  87.     v[2] = 0.0;
  88. }
  89.  
  90. void
  91. vset(double *v, double x, double y, double z)
  92. {
  93.     v[0] = x;
  94.     v[1] = y;
  95.     v[2] = z;
  96. }
  97.  
  98. void
  99. vsub(const double *src1, const double *src2, double *dst)
  100. {
  101.     dst[0] = src1[0] - src2[0];
  102.     dst[1] = src1[1] - src2[1];
  103.     dst[2] = src1[2] - src2[2];
  104. }
  105.  
  106. void
  107. vcopy(const double *v1, double *v2)
  108. {
  109.     register int i;
  110.     for (i = 0 ; i < 3 ; i++)
  111.         v2[i] = v1[i];
  112. }
  113.  
  114. void
  115. vcross(const double *v1, const double *v2, double *cross)
  116. {
  117.     double temp[3];
  118.  
  119.     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  120.     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  121.     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  122.     vcopy(temp, cross);
  123. }
  124.  
  125. double
  126. vlength(const double *v)
  127. {
  128.     return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  129. }
  130.  
  131. void
  132. vscale(double *v, double div)
  133. {
  134.     v[0] *= div;
  135.     v[1] *= div;
  136.     v[2] *= div;
  137. }
  138.  
  139. void
  140. vnormal(double *v)
  141. {
  142.     vscale(v,1.0/vlength(v));
  143. }
  144.  
  145. double
  146. vdot(const double *v1, const double *v2)
  147. {
  148.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  149. }
  150.  
  151. void
  152. vadd(const double *src1, const double *src2, double *dst)
  153. {
  154.     dst[0] = src1[0] + src2[0];
  155.     dst[1] = src1[1] + src2[1];
  156.     dst[2] = src1[2] + src2[2];
  157. }
  158.  
  159. /*
  160.  * Ok, simulate a track-ball.  Project the points onto the virtual
  161.  * trackball, then figure out the axis of rotation, which is the cross
  162.  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  163.  * Note:  This is a deformed trackball-- is a trackball in the center,
  164.  * but is deformed into a hyperbolic sheet of rotation away from the
  165.  * center.  This particular function was chosen after trying out
  166.  * several variations.
  167.  *
  168.  * It is assumed that the arguments to this routine are in the range
  169.  * (-1.0 ... 1.0)
  170.  */
  171. void
  172. trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
  173. {
  174.     double a[3]; /* Axis of rotation */
  175.     double phi;  /* how much to rotate about axis */
  176.     double p1[3], p2[3], d[3];
  177.     double t;
  178.  
  179.     if (p1x == p2x && p1y == p2y) {
  180.         /* Zero rotation */
  181.         vzero(q);
  182.         q[3] = 1.0;
  183.         return;
  184.     }
  185.  
  186.     /*
  187.      * First, figure out z-coordinates for projection of P1 and P2 to
  188.      * deformed sphere
  189.      */
  190.     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  191.     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  192.  
  193.     /*
  194.      *  Now, we want the cross product of P1 and P2
  195.      */
  196.     vcross(p2,p1,a);
  197.  
  198.     /*
  199.      *  Figure out how much to rotate around that axis.
  200.      */
  201.     vsub(p1,p2,d);
  202.     t = vlength(d) / (2.0*TRACKBALLSIZE);
  203.  
  204.     /*
  205.      * Avoid problems with out-of-control values...
  206.      */
  207.     if (t > 1.0) t = 1.0;
  208.     if (t < -1.0) t = -1.0;
  209.     phi = 2.0 * asin(t);
  210.     
  211.     axis_to_quat(a,phi,q);
  212. }
  213.  
  214. // I did Habib_trackball to allow the camera to turn only up-down or left-right.
  215. // since a[] is the axes of rotation, and since a is given in the coordinate system
  216. // of the up_rt, hor_rt, and lk_rt (not up, hor, lk) I always make sure that for
  217. // left-right a is (0,1,0) in the fix rotational system (up,hor,lk) and up down should be
  218. // (1,0,0) in the rotated coordinate system (hor_rt, up_rt, lk_rt).
  219. // And it did work but it was not homogeneous: the turning when the cursor is
  220. // next to the center of the sphere, is not like the turning when the cursor
  221. // is at the bottom of the sphere, although It was always constrained to turn
  222. // like I wanted it to turn.
  223. // But then I improved in interface.h the way I call Habib_trackball in a way
  224. // where it would be called with the same p1x, p1y, p2x, p2y wherever my x,y mouse
  225. // position is... and it works wonderfully.
  226. void
  227. Habib_trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
  228. {
  229.     double a[3]; /* Axis of rotation */
  230.     double phi;  /* how much to rotate about axis */
  231.     double p1[3], p2[3], d[3];
  232.     double t;
  233.  
  234.     if (p1x == p2x && p1y == p2y) {
  235.         /* Zero rotation */
  236.         vzero(q);
  237.         q[3] = 1.0;
  238.         return;
  239.     }
  240.  
  241.     /*
  242.      * First, figure out z-coordinates for projection of P1 and P2 to
  243.      * deformed sphere
  244.      */
  245.     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  246.     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  247.  
  248.     /*
  249.      *  Now, we want the cross product of P1 and P2
  250.      */
  251.     vcross(p2,p1,a);
  252.  
  253.     /*
  254.      *  Figure out how much to rotate around that axis.
  255.      */
  256.     vsub(p1,p2,d);
  257.     t = vlength(d) / (2.0*TRACKBALLSIZE);
  258.  
  259.     /*
  260.      * Avoid problems with out-of-control values...
  261.      */
  262.     if (t > 1.0) t = 1.0;
  263.     if (t < -1.0) t = -1.0;
  264.     phi = 2.0 * asin(t);
  265.  
  266.     // a is the current axes of rotation and 
  267.     // a[0], a[1], a[2] are the coordinates in the hor_rt, up_rt, lk_rt coordinate system.
  268.     double sgn;
  269.     if ( ( fabs(a[1])> abs(a[0]) ) && (  fabs(a[1])>fabs(a[2])  )     ) {
  270.        // Turning the head left and right.
  271.       sgn = -a[1]/fabs(a[1]);
  272.       a[0] = hor_rt.y*sgn; // Here I want the rotation to be made around up vector
  273.       a[1] =  up_rt.y*sgn; // Not the up_rt vector!!
  274.       a[2] =  lk_rt.y*sgn;
  275.        }
  276.     else     if ( ( fabs(a[0])> abs(a[1]) ) && (  fabs(a[0])>fabs(a[2])  )     ) {
  277.        // Turning the head up and down. This time I want the rotation affected
  278.        // around the horizontal axes hor_rt and not hor!!!
  279.       sgn = -a[0]/fabs(a[0]);
  280.       a[0] = sgn; // this is hor_rt relatively to the rt coord system.
  281.       a[1] = 0;   // this is hor_rt relatively to the rt coord system.
  282.       a[2] = 0;   // this is hor_rt relatively to the rt coord system.
  283.        }
  284.     else phi = 0;
  285.  
  286.     axis_to_quat(a,phi,q);
  287. }
  288.  
  289. // I did Habib_trackball to allow the camera to turn only up-down or left-right.
  290. // since a[] is the axes of rotation, and since a is given in the coordinate system
  291. // of the up_rt, hor_rt, and lk_rt (not up, hor, lk) I always make sure that for
  292. // left-right a is (0,1,0) in the fix rotational system (up,hor,lk) and up down should be
  293. // (1,0,0) in the rotated coordinate system (m1.side, m1.up, m1.nl).
  294. // And it did work but it was not homogeneous: the turning when the cursor is
  295. // next to the center of the sphere, is not like the turning when the cursor
  296. // is at the bottom of the sphere, although It was always constrained to turn
  297. // like I wanted it to turn.
  298. // But then I improved in interface.h the way I call Habib_trackball_mirror in a way
  299. // where it would be called with the same p1x, p1y, p2x, p2y wherever my x,y mouse
  300. // position is... and it works wonderfully.
  301. void
  302. Habib_trackball_mirror(double q[4], double p1x, double p1y, double p2x, double p2y,int i)
  303. {
  304.     double a[3]; /* Axis of rotation */
  305.     double phi;  /* how much to rotate about axis */
  306.     double p1[3], p2[3], d[3];
  307.     double t;
  308.  
  309.     if (p1x == p2x && p1y == p2y) {
  310.         /* Zero rotation */
  311.         vzero(q);
  312.         q[3] = 1.0;
  313.         return;
  314.     }
  315.  
  316.     /*
  317.      * First, figure out z-coordinates for projection of P1 and P2 to
  318.      * deformed sphere
  319.      */
  320.     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  321.     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  322.  
  323.     /*
  324.      *  Now, we want the cross product of P1 and P2
  325.      */
  326.     vcross(p2,p1,a);
  327.  
  328.     /*
  329.      *  Figure out how much to rotate around that axis.
  330.      */
  331.     vsub(p1,p2,d);
  332.     t = vlength(d) / (2.0*TRACKBALLSIZE);
  333.  
  334.     /*
  335.      * Avoid problems with out-of-control values...
  336.      */
  337.     if (t > 1.0) t = 1.0;
  338.     if (t < -1.0) t = -1.0;
  339.     phi = 2.0 * asin(t);
  340.  
  341.     // a is the current axes of rotation and 
  342.     // a[0], a[1], a[2] are the coordinates in the hor_rt, up_rt, lk_rt coordinate system.
  343.     double sgn;
  344.     if ( ( fabs(a[1])> abs(a[0]) ) && (  fabs(a[1])>fabs(a[2])  )     ) {
  345.        // Turning the head left and right.
  346.       sgn = -a[1]/fabs(a[1]);
  347.       a[0] = 0; //m1.side.y*sgn; // Here I want the rotation to be made around up vector
  348.       a[1] = sgn; //  m1.up.y*sgn; // Not the m1.up vector!!
  349.       a[2] = 0; //  m1.nl.y*sgn;
  350.        }
  351.     else     if ( ( fabs(a[0])> abs(a[1]) ) && (  fabs(a[0])>fabs(a[2])  )     ) {
  352.        // Turning the head up and down. This time I want the rotation affected
  353.        // around the horizontal axes hor_rt and not hor!!!
  354.       sgn = -a[0]/fabs(a[0]);
  355.       a[0] = m1[i].side.x*sgn; // this is hor_rt relatively to the rt coord system.
  356.       a[1] = m1[i].side.y*sgn;   // this is hor_rt relatively to the rt coord system.
  357.       a[2] = m1[i].side.z*sgn;   // this is hor_rt relatively to the rt coord system.
  358.        }
  359.     else phi = 0;
  360.  
  361.     axis_to_quat(a,phi,q);
  362. }
  363. /*
  364.  * Ok, simulate a track-ball.  Project the points onto the virtual
  365.  * trackball, then figure out the axis of rotation, which is the cross
  366.  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  367.  * Note:  This is a deformed trackball-- is a trackball in the center,
  368.  * but is deformed into a hyperbolic sheet of rotation away from the
  369.  * center.  This particular function was chosen after trying out
  370.  * several variations.
  371.  *
  372.  * It is assumed that the arguments to this routine are in the range
  373.  * (-1.0 ... 1.0)
  374.  */
  375. void
  376. opp_trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
  377. {
  378.     double a[3]; /* Axis of rotation */
  379.     double phi;  /* how much to rotate about axis */
  380.     double p1[3], p2[3], d[3];
  381.     double t;
  382.  
  383.     if (p1x == p2x && p1y == p2y) {
  384.         /* Zero rotation */
  385.         vzero(q);
  386.         q[3] = 1.0;
  387.         return;
  388.     }
  389.  
  390.     /*
  391.      * First, figure out z-coordinates for projection of P1 and P2 to
  392.      * deformed sphere
  393.      */
  394.     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  395.     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  396.  
  397.     /*
  398.      *  Now, we want the cross product of P1 and P2
  399.      */
  400.     vcross(p2,p1,a);
  401.  
  402.     /*
  403.      *  Figure out how much to rotate around that axis.
  404.      */
  405.     vsub(p1,p2,d);
  406.     t = vlength(d) / (2.0*TRACKBALLSIZE);
  407.  
  408.     /*
  409.      * Avoid problems with out-of-control values...
  410.      */
  411.     if (t > 1.0) t = 1.0;
  412.     if (t < -1.0) t = -1.0;
  413.     phi = -2.0 * asin(t);
  414.  
  415.     axis_to_quat(a,phi,q);
  416. }
  417.  
  418. /*
  419.  *  Given an axis and angle, compute quaternion.
  420.  */
  421. void
  422. axis_to_quat(double a[3], double phi, double q[4])
  423. {
  424.     vnormal(a);
  425.     vcopy(a,q);
  426.     vscale(q,sin(phi/2.0));
  427.     q[3] = cos(phi/2.0);
  428. }
  429.  
  430. /*
  431.  * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
  432.  * if we are away from the center of the sphere.
  433.  */
  434. static double
  435. tb_project_to_sphere(double r, double x, double y)
  436. {
  437.     double d, t, z;
  438.  
  439.     d = sqrt(x*x + y*y);
  440.     if (d < r * 0.70710678118654752440) {    /* Inside sphere */
  441.         z = sqrt(r*r - d*d);
  442.     } else {           /* On hyperbola */
  443.         t = r / 1.41421356237309504880;
  444.         z = t*t / d;
  445.     }
  446.     return z;
  447. }
  448.  
  449. /*
  450.  * Given two rotations, e1 and e2, expressed as quaternion rotations,
  451.  * figure out the equivalent single rotation and stuff it into dest.
  452.  *
  453.  * This routine also normalizes the result every RENORMCOUNT times it is
  454.  * called, to keep error from creeping in.
  455.  *
  456.  * NOTE: This routine is written so that q1 or q2 may be the same
  457.  * as dest (or each other).
  458.  */
  459.  
  460. #define RENORMCOUNT 97
  461.  
  462. void
  463. add_quats(double q1[4], double q2[4], double dest[4])
  464. {
  465.     static int count=0;
  466.     double t1[4], t2[4], t3[4];
  467.     double tf[4];
  468.  
  469.     vcopy(q1,t1);
  470.     vscale(t1,q2[3]);
  471.  
  472.     vcopy(q2,t2);
  473.     vscale(t2,q1[3]);
  474.  
  475.     vcross(q2,q1,t3);
  476.     vadd(t1,t2,tf);
  477.     vadd(t3,tf,tf);
  478.     tf[3] = q1[3] * q2[3] - vdot(q1,q2);
  479.  
  480.     dest[0] = tf[0];
  481.     dest[1] = tf[1];
  482.     dest[2] = tf[2];
  483.     dest[3] = tf[3];
  484.  
  485.     if (++count > RENORMCOUNT) {
  486.         count = 0;
  487.         normalize_quat(dest);
  488.     }
  489. }
  490.  
  491. /*
  492.  * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0
  493.  * If they don't add up to 1.0, dividing by their magnitued will
  494.  * renormalize them.
  495.  *
  496.  * Note: See the following for more information on quaternions:
  497.  *
  498.  * - Shoemake, K., Animating rotation with quaternion curves, Computer
  499.  *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
  500.  * - Pletinckx, D., Quaternion calculus as a basic tool in computer
  501.  *   graphics, The Visual Computer 5, 2-13, 1989.
  502.  */
  503. static void
  504. normalize_quat(double q[4])
  505. {
  506.     int i;
  507.     double mag;
  508.  
  509.     mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
  510.     for (i = 0; i < 4; i++) q[i] /= mag;
  511. }
  512.  
  513. /*
  514.  * Build a rotation matrix, given a quaternion rotation.
  515.  *
  516.  */
  517. void
  518. build_rotmatrix(double m[4][4], double q[4])
  519. {
  520.     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
  521.     m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
  522.     m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
  523.     m[0][3] = 0.0;
  524.  
  525.     m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
  526.     m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
  527.     m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
  528.     m[1][3] = 0.0;
  529.  
  530.     m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
  531.     m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
  532.     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
  533.     m[2][3] = 0.0;
  534.  
  535.     m[3][0] = 0.0;
  536.     m[3][1] = 0.0;
  537.     m[3][2] = 0.0;
  538.     m[3][3] = 1.0;
  539. }
  540.  
  541. /*
  542.  * Build a rotation matrix, given a quaternion rotation.
  543.  *
  544.  */
  545. void
  546. build_rotmatrix_hab(double m[4][4], double q[4])
  547. {
  548.     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
  549.     m[2][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
  550.     m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
  551.     m[0][3] = 0.0;
  552.  
  553.     m[1][2] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
  554.     m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
  555.     m[1][0] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
  556.     m[1][3] = 0.0;
  557.  
  558.     m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
  559.     m[0][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
  560.     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
  561.     m[2][3] = 0.0;
  562.  
  563.     m[3][0] = 0.0;
  564.     m[3][1] = 0.0;
  565.     m[3][2] = 0.0;
  566.     m[3][3] = 1.0;
  567. }
  568.  
  569.