home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / spline-patch.tar.gz / spline-patch.tar / patch / vector.c < prev    next >
C/C++ Source or Header  |  1991-11-18  |  4KB  |  188 lines

  1. /*
  2.  *   File     : vector.c
  3.  *   Author   : Sean Graves
  4.  *
  5.  *   Computer : Any 
  6.  *   Date     : 6/21/90 
  7.  *
  8.  *   Basic linear algebra stuff used by the patch.c routines
  9.  *
  10.  *
  11.  * Copyright (c) 1990, by Sean Graves and Texas A&M University
  12.  *
  13.  * Permission is hereby granted for non-commercial reproduction and use of
  14.  * this program, provided that this notice is included in any material copied
  15.  * from it. The author assumes no responsibility for damages resulting from
  16.  * the use of this software, however caused.
  17.  *
  18.  */
  19.  
  20. #include "math.h"
  21. #include "patch.h"
  22. #define TOL 0.0001
  23.  
  24. /************************************************************/
  25.  
  26. void mmult4x4_4x4(a, b, c)
  27. MATRIX a, b, c;
  28. {
  29.    int i, j, k;
  30.    MATRIX temp;
  31.  
  32.    for (i = 0; i < 4; i++) {
  33.       for (j = 0; j < 4; j++) {
  34.          temp[i][j] = 0;
  35.          for (k = 0; k < 4; k++) {
  36.             temp[i][j] += (b[i][k] * c[k][j]);
  37.      }
  38.       }
  39.    }
  40.    for (i = 0; i < 4; i++) 
  41.       for (j = 0; j < 4; j++)
  42.          a[i][j] = temp[i][j];
  43. }
  44.  
  45. /************************************************************/
  46.  
  47. void mmult1x4_4x1(a, b, c)
  48. float a[1][1], b[1][4], c[4][1];
  49. {
  50.    int k;
  51.    float temp;
  52.    
  53.    temp = 0;
  54.    for (k = 0; k < 4; k++) {
  55.       temp += (b[0][k] * c[k][0]);
  56.    }
  57.  
  58.    a[0][0] = temp;
  59. }
  60.  
  61. /************************************************************/
  62.  
  63. void mmult4x4_4x1(a, b, c)
  64. float a[4][1], b[4][4], c[4][1];
  65. {
  66.    int i, k;
  67.    float temp[4][1];
  68.  
  69.    for (i = 0; i < 4; i++) {
  70.       temp[i][0] = 0;
  71.       for (k = 0; k < 4; k++) {
  72.          temp[i][0] += (b[i][k] * c[k][0]);
  73.       }
  74.    }
  75.    for (i = 0; i < 4; i++) 
  76.       a[i][0] = temp[i][0];
  77. }
  78.  
  79. /************************************************************/
  80.  
  81. void mmult1x4_4x4(a, b, c)
  82. float a[1][4], b[1][4], c[4][4];
  83. {
  84.    int j, k;
  85.    float temp[1][4];
  86.  
  87.    for (j = 0; j < 4; j++) {
  88.       temp[0][j] = 0;
  89.       for (k = 0; k < 4; k++) {
  90.          temp[0][j] += (b[0][k] * c[k][j]);
  91.       }
  92.    }
  93.    for (j = 0; j < 4; j++)
  94.       a[0][j] = temp[0][j];
  95. }
  96.  
  97. /************************************************************/
  98.  
  99. void transpose(a,b)
  100. MATRIX a, b;
  101. {
  102.    int i, j;
  103.  
  104.    for (i = 0; i < 4 ; i++) 
  105.       for (j = 0; j < 4; j++)
  106.          b[j][i] = a[i][j];
  107. }
  108.  
  109. /************************************************************/
  110.  
  111. int fcmp(a,b)
  112. float a,b;
  113. {
  114.    if (fabs(a - b) < TOL) return 1; 
  115.    return 0;
  116. }
  117.  
  118. /************************************************************/
  119.  
  120. float length(v1, v2)
  121. POINT v1, v2;
  122. {
  123.    float a, b, c;
  124.  
  125.    a = v1.x - v2.x;
  126.    b = v1.y - v2.y;
  127.    c = v1.z - v2.z;
  128.  
  129.    return sqrt(a*a + b*b + c*c);
  130. }
  131.  
  132.  
  133. /**********************************************************/
  134.  
  135. void normalize(vec)
  136. VECTOR *vec;
  137. {
  138.    float mag;
  139.  
  140.    mag = sqrt(vec->x*vec->x + vec->y*vec->y + vec->z*vec->z);
  141.    vec->x /= mag;
  142.    vec->y /= mag;
  143.    vec->z /= mag;
  144. }
  145.  
  146. /************************************************************/
  147.  
  148. void cross_product(c, a, b)    /* c = a X b */
  149. VECTOR a, b, *c;
  150. {
  151.    c->x = a.y*b.z - a.z*b.y;
  152.    c->y = a.z*b.x - a.x*b.z;
  153.    c->z = a.x*b.y - a.y*b.x;
  154. }
  155.  
  156. /************************************************************/
  157.  
  158. float dot_product(a, b)
  159. VECTOR a, b;
  160. {
  161.    return a.x*b.x + a.y*b.y + a.z*b.z;
  162. }
  163.  
  164. /*********************DIAGNOSTIC**********************/
  165.  
  166. float aref(arr, cols, r, c)
  167. float *arr;
  168. int cols, r, c;
  169. {
  170.     return (float) *(arr + (cols * r) + c);
  171. }
  172.  
  173. /*********************DIAGNOSTIC*********************/
  174.  
  175. dump(a, rs, cs)
  176. float *a;
  177. int rs, cs;
  178. {
  179.    int i,j;
  180.  
  181.    for (i = 0; i < rs; i++) {
  182.     for (j = 0; j < cs; j++)
  183.         printf("%6.2f  ",aref(a, cs, i, j));
  184.         printf("\n");
  185.    }
  186. }
  187.  
  188.