home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / graphics / utility / raymovi2.lzh / RAYMOVI2 / SUPPORT.C (.txt) < prev    next >
C/C++ Source or Header  |  1988-12-21  |  4KB  |  202 lines

  1. /*
  2.  *   supportive subroutines...
  3.  */
  4. #include <math.h>
  5. #include <stdio.h>
  6. #undef min
  7. #include "rtd.h"
  8. #include "extern.h"
  9. #define time(x) 0
  10.  
  11. /* returns dot product of two vectors */
  12.  
  13. double  dot (v1, v2)
  14.     struct vector  *v1,
  15.                    *v2;
  16. {
  17.     double  r;
  18.     r = v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
  19.     return (r);
  20. }
  21.  
  22.  
  23. /* multiplies a vector by a scalar */
  24.  
  25. scamult (scal, vect)
  26.     double  scal;
  27.     struct vector  *vect;
  28. {
  29.     vect->x *= scal;
  30.     vect->y *= scal;
  31.     vect->z *= scal;
  32.     vect->l *= scal;
  33.     vect->xzl *= scal; 
  34. }
  35.  
  36.  
  37. /* loads a vector with given x,y,z values */
  38.  
  39. mv (x, y, z, _vp) 
  40.     double  x,
  41.             y,
  42.             z;
  43.     struct vector  *_vp;
  44. {
  45.     _vp->x = x;
  46.     _vp->y = y;
  47.     _vp->z = z;
  48. }
  49.  
  50.  
  51. /* vector subtraction: v1 = v2 - v3 */
  52.  
  53. sv (v1, v2, v3) 
  54.     struct vector  *v1,
  55.                    *v2,
  56.                    *v3;
  57. {
  58.     v1->x = v2->x - v3->x;
  59.     v1->y = v2->y - v3->y;
  60.     v1->z = v2->z - v3->z;
  61. }
  62.  
  63.  
  64.  
  65. /* vector addition: v1 = v2 + v3 */
  66.  
  67. av (v1, v2, v3) 
  68.     struct vector  *v1,
  69.                    *v2,
  70.                    *v3;
  71. {
  72.     v1->x = v2->x + v3->x;
  73.     v1->y = v2->y + v3->y;
  74.     v1->z = v2->z + v3->z;
  75. }
  76.  
  77.  
  78. /*  loads a transform matrix so that when vec is multiplied by it,
  79.     the result is (1,0,0). it does so with out deforming space */
  80.  
  81. mt (vec, trans) 
  82.     struct vector  *vec;
  83.     struct mat *trans;
  84. {
  85.     vecl (vec);
  86.     vexzl (vec);
  87.  
  88.     if (vec->xzl == 0.0) {
  89.     trans->x.x = 0.0;
  90.     trans->x.y = 1.0;
  91.     trans->x.z = 0.0;
  92.     trans->y.x = -1.0;
  93.     trans->y.y = 0.0;
  94.     trans->y.z = 0.0;
  95.     trans->z.x = 0.0;
  96.     trans->z.y = 0.0;
  97.     trans->z.z = 1.0;
  98.     }
  99.     else {
  100.     trans->x.x = (vec->x) / (vec->l);
  101.     trans->x.y = (vec->y) / (vec->l);
  102.     trans->x.z = (vec->z) / (vec->l);
  103.     trans->y.x = -(vec->x) * (vec->y) / ((vec->l) * (vec->xzl));
  104.     trans->y.y = (vec->xzl) / (vec->l);
  105.     trans->y.z = -(vec->z) * (vec->y) / ((vec->l) * (vec->xzl));
  106.     trans->z.x = -(vec->z) / (vec->xzl);
  107.     trans->z.y = 0;
  108.     trans->z.z = (vec->x) / (vec->xzl);
  109.     }
  110. }
  111.  
  112. /* multiply vector by matrix: vc1 = m*vc2 */
  113.  
  114. mt_vec (vc1, m, vc2)
  115.     struct vector  *vc1,
  116.                    *vc2;
  117.     struct mat *m;
  118. {
  119.     double  x,
  120.             y,
  121.             z;
  122.  
  123.     x = dot (&(m->x), vc2);
  124.     y = dot (&(m->y), vc2);
  125.     z = dot (&(m->z), vc2);
  126.     mv (x, y, z, vc1);
  127. }
  128.  
  129.  
  130.  
  131. /* will update a file containing information on 
  132. how we are coming along... useful for long runs*/
  133.  
  134. update (x, xmin, xmax)
  135.     float   x,
  136.             xmin,
  137.             xmax;
  138. {
  139.     FILE * fff;
  140.     static int  t;
  141.     static double   size,
  142.                     xm;
  143.     int     tt,
  144.             ttt,
  145.             hr,
  146.             min,
  147.             sec;
  148.  
  149.  
  150.     fprintf (stderr, "%d\n", (int) x);
  151.     return;
  152.  
  153.  
  154.     if (x == xmin) {
  155. /*      fff = fopen ("time.update", "w"); */
  156.     fff = stderr;
  157.     fprintf (fff, "starting...\n");
  158. /*      fclose (fff); */
  159.     t = time (0);
  160.     xm = xmin;
  161.     size = (xmax - xmin);
  162.     }
  163.     else {
  164.     x -= xm;
  165.     tt = time (0) - t;
  166.     ttt = (double) tt * size / x;
  167.     tt = (double) tt * (size - x) / x;
  168.     hr = tt / 3600;
  169.     min = (tt % 3600) / 60;
  170.     sec = tt % 60;
  171. /*      fff = fopen ("time.update", "w"); */
  172.     fff = stderr;
  173.     fprintf (fff, "%5.1f%% done.\n", 100.0 * x / size);
  174.     fprintf (fff, "%d:%2d:%2d left.\n", hr, min, sec);
  175.     hr = ttt / 3600;
  176.     min = (ttt % 3600) / 60;
  177.     sec = ttt % 60;
  178.     fprintf (fff, "%d:%2d:%2d  total estimate.\n", hr, min, sec);
  179. /*      fclose (fff); */
  180.     }
  181. }
  182.  
  183.  
  184. /* loads a vectors length... saves time to do this only when needed */
  185.  
  186. vecl (v)
  187.     struct vector *v;
  188. {
  189.     v->l = sqrt ((v->x) * (v->x) + (v->y) * (v->y) + (v->z) * (v->z));
  190. }
  191.  
  192.  
  193. /* loads a vectors xz component. needed for mt */
  194.  
  195. vexzl (v)
  196.     struct vector *v;
  197. {
  198.     v->xzl = sqrt ((v->x) * (v->x) + (v->z) * (v->z));
  199. }
  200.  
  201.  
  202.