home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_6 / matrix / mat3.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-27  |  4.1 KB  |  143 lines

  1. /* Copyright 1991, Brown Computer Graphics Group.  All Rights Reserved. */
  2.  
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define MAT3_EPSILON    1e-12            /* Close enough to zero   */
  8. #define MAT3_PI     M_PI            /* Pi              */
  9.  
  10. typedef double MAT3mat[4][4];        /* 4x4 matrix             */
  11. typedef double MAT3vec[3];        /* Vector             */
  12. typedef double MAT3hvec[4];             /* Vector with homogeneous coord */
  13.  
  14. #define MAT3_IS_ZERO(N)     ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
  15.  
  16. /*
  17.  * TITLE   :    Vector operations
  18.  * RETURNS :    void
  19.  */
  20.  
  21. #define MAT3_SET_VEC(RESULT_V,X,Y,Z)    \
  22.         ((RESULT_V)[0]=(X), (RESULT_V)[1]=(Y), (RESULT_V)[2]=(Z))
  23.  
  24. #define MAT3_COPY_VEC(TO,FROM)    ((TO)[0] = (FROM)[0], \
  25.                  (TO)[1] = (FROM)[1], \
  26.                  (TO)[2] = (FROM)[2])
  27.  
  28. /*
  29.  * TITLE   :
  30.  * DESCR   :    Basic operations on vector @V@, that modify vector
  31.  *        @RESULT_V@.
  32.  *
  33.  */
  34.  
  35. #define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \
  36.     MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE))
  37.  
  38. #define MAT3_ADD_VEC(RESULT_V,V1,V2) \
  39.     MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
  40.                    (V1)[2]+(V2)[2])
  41.  
  42. #define MAT3_SUB_VEC(RESULT_V,V1,V2) \
  43.     MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
  44.                    (V1)[2]-(V2)[2])
  45.  
  46. /*
  47.  * TITLE   :
  48.  * DESCR   :    More  complex operations on multiple vectors
  49.  *
  50.  */
  51.  
  52. /*
  53.  * DESCR   :    Compute dot product of 3-vectors @V1@ and @V2@
  54.  * RETURNS :    double
  55.  */
  56. #define MAT3_DOT_PRODUCT(V1,V2) \
  57.             ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2])
  58.  
  59. /*
  60.  * DESCR   :    Normalize the 3-vector @V@, using @TEMP@ as temporary variable.
  61.  * DETAILS :    If the norm of @V@ is too close to zero, no normalization
  62.  *         takes place, and @TEMP@ is set to zero.
  63.  */
  64. #define MAT3_NORMALIZE_VEC(V,TEMP) \
  65.     if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \
  66.        TEMP = 1.0 / TEMP; \
  67.        MAT3_SCALE_VEC(V,V,TEMP); \
  68.     } else TEMP = 0.0
  69.  
  70. /*
  71.  * DESCR   :    Sets @RESULT_V@ to the linear combination of @V1@ and @V2@,
  72.  *         scaled by @SCALE1@ and @SCALE2@, respectively 
  73.  */
  74. #define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \
  75.     MAT3_SET_VEC(RESULT_V,    (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \
  76.                 (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \
  77.                 (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2])
  78.  
  79. /*
  80.  * TITLE   :    Homogeneous vector operations
  81.  */
  82.  
  83. #define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \
  84.                   (V)[2]=(Z), (V)[3]=(W))
  85.  
  86. #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
  87.                  (TO)[1] = (FROM)[1], \
  88.                  (TO)[2] = (FROM)[2], \
  89.                  (TO)[3] = (FROM)[3])
  90.  
  91. /* ------------------------------  Entries  ------------------------------- */
  92.  
  93. void MAT3scale(
  94.          MAT3mat result_mat,        /* Scale matrix [output] */
  95.          MAT3vec scale             /* The entries for the diagonal */
  96. );
  97.  
  98. void MAT3translate(
  99.          MAT3mat result_mat,        /* Translation matrix [output] */
  100.          MAT3vec trans            /* Translation vector [input]  */
  101. );
  102.  
  103. void MAT3shear(
  104.          register MAT3mat result_mat,    /* Shear matrix [output] */
  105.          register MAT3vec normal,    /* Normal to the shear plane */
  106.          register MAT3vec shear     /* Shearing vector [perp to normal] */
  107. );
  108.  
  109. void MAT3identity(
  110.          MAT3mat m             /* Matrix to be set to identity */
  111. );
  112.  
  113. void MAT3zero(
  114.          MAT3mat m             /* Matrix to be set to zero */
  115. );
  116.  
  117. void MAT3copy(
  118.          MAT3mat t,            /* Target matrix [output] */
  119.          MAT3mat f            /* Source matrix [input] */
  120. );
  121. void MAT3mult(
  122.          MAT3mat result_mat,        /* Computed product [output] */
  123.          MAT3mat mat1,            /* The first factor of the product */
  124.          MAT3mat mat2             /* The second factor of the product */
  125. );
  126.  
  127. /* The ordering of "vec" and "mat" parameters in following three functions is
  128.  * probably counterintuitive but is retained for backward compatibility.
  129.  */
  130.  
  131. int MAT3mult_hvec(
  132.          MAT3hvec          result_vec,    /* Product of vec with mat (output) */
  133.          register MAT3hvec vec,        /* Vector to multiply (input) */
  134.          register MAT3mat  mat,        /* Matrix to multiply (input) */
  135.          int               homogenize    /* Should we homogenize the result? */
  136. );
  137.  
  138. void MAT3cross_product(
  139.          MAT3vec      result_vec,    /* Computed cross-product (output) */
  140.          register MAT3vec vec1,        /* The first factor (input) */
  141.          register MAT3vec vec2         /* The second factor (input) */
  142. );
  143.