home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_7 / mactbox / mat2.h next >
Encoding:
C/C++ Source or Header  |  1994-09-28  |  3.7 KB  |  144 lines

  1. /* ------------------------------------------------------------------------- *\
  2.    MAT2.H :
  3.  
  4.    Definition and manipulation of a 2D matrix (either integers or reals)
  5.  
  6.    by Christophe Schlick (1 June 1992)
  7. \* ------------------------------------------------------------------------- */
  8.  
  9. #ifndef _MAT2_
  10. #define _MAT2_
  11.  
  12. #include "tool.h"
  13. #include "vec2.h"
  14.  
  15. /*
  16. ** Creation, duplication, swapping
  17. */
  18.  
  19. #define MAKE_MAT2(M,A,B)\
  20.         (COPY_VEC2 ((M)[0],(A)),\
  21.          COPY_VEC2 ((M)[1],(B)))
  22.  
  23. #define COPY_MAT2(M,A)\
  24.         (COPY_VEC2 ((M)[0],(A)[0]),\
  25.          COPY_VEC2 ((M)[1],(A)[1]))
  26.  
  27. #define SWAP_MAT2(A,B,t)\
  28.         (SWAP_VEC2 ((A)[0], (B)[0], t),\
  29.          SWAP_VEC2 ((A)[1], (B)[1], t))
  30.  
  31. /*
  32. ** Addition, subtraction, multiplication, division (by a matrix element)
  33. */
  34.  
  35. #define INC_MAT2(M,A)\
  36.         (INC_VEC2 ((M)[0],(A)[0]),\
  37.          INC_VEC2 ((M)[1],(A)[1]))
  38.  
  39. #define DEC_MAT2(M,A)\
  40.         (DEC_VEC2 ((M)[0],(A)[0]),\
  41.          DEC_VEC2 ((M)[1],(A)[1]))
  42.  
  43. #define ADD_MAT2(M,A,B)\
  44.         (ADD_VEC2 ((M)[0],(A)[0],(B)[0]),\
  45.          ADD_VEC2 ((M)[1],(A)[1],(B)[1]))
  46.  
  47. #define SUB_MAT2(M,A,B)\
  48.         (SUB_VEC2 ((M)[0],(A)[0],(B)[0]),\
  49.          SUB_VEC2 ((M)[1],(A)[1],(B)[1]))
  50.  
  51. #define MUL_MAT2(M,A,B)\
  52.         (MUL_VEC2 ((M)[0],(A)[0],(B)[0]),\
  53.          MUL_VEC2 ((M)[1],(A)[1],(B)[1]))
  54.  
  55. #define DIV_MAT2(M,A,B)\
  56.         (DIV_VEC2 ((M)[0],(A)[0],(B)[0]),\
  57.          DIV_VEC2 ((M)[1],(A)[1],(B)[1]))
  58.  
  59. /*
  60. ** Addition, subtraction, multiplication, division (by a scalar element)
  61. */
  62.  
  63. #define ADDS_MAT2(M,A,s)\
  64.         (ADDS_VEC2 ((M)[0],(A)[0],s),\
  65.          ADDS_VEC2 ((M)[1],(A)[1],s))  
  66.  
  67. #define SUBS_MAT2(M,A,B)\
  68.         (SUBS_VEC2 ((M)[0],(A)[0],s),\
  69.          SUBS_VEC2 ((M)[1],(A)[1],s))
  70.  
  71. #define MULS_MAT2(M,A,B)\
  72.         (MULS_VEC2 ((M)[0],(A)[0],s),\
  73.          MULS_VEC2 ((M)[1],(A)[1],s))
  74.  
  75. #define DIVS_MAT2(M,A,B)\
  76.         (DIVS_VEC2 ((M)[0],(A)[0],s),\
  77.          DIVS_VEC2 ((M)[1],(A)[1],s))
  78.  
  79. /*
  80. ** Determinant, transposition, adjunction, inversion
  81. */
  82.  
  83. #define DELTA_MAT2(M)\
  84.         (DELTA_VEC2 ((M)[0],(M)[1]))
  85.  
  86. #define TRANS_MAT2(M,A)\
  87.         ((M)[0].x = (A)[0].x, (M)[0].y = (A)[1].x,\
  88.          (M)[1].x = (A)[0].y, (M)[1].y = (A)[1].y)  
  89.  
  90. #define ADJ_MAT2(M,A)\
  91.         ((M)[0].x = (A)[1].y, (M)[0].y = -(A)[0].y,\
  92.          (M)[1].x = -(A)[1].x, (M)[1].y = (A)[0].x)
  93.  
  94. #define INV_MAT2(M,A,s)\
  95.         (ADJ_MAT2 (M,A), (s) = DOT_VEC2 ((M)[0],(A)[0]),\
  96.          ZERO (s) ? (DIVS_MAT2 (M,M,s), TRUE) : FALSE)
  97.  
  98. /*
  99. ** Matrix product, left vector product, right vector product
  100. */
  101.  
  102. #define ROW_VEC2(V,M,n)\
  103.         ((V).x*(M)[0].n + (V).y*(M)[1].n)
  104.  
  105. #define PROD_MAT2(M,A,B)\
  106.         ((M)[0].x = ROW_VEC2 ((A)[0],(B),x),\
  107.          (M)[0].y = ROW_VEC2 ((A)[0],(B),y),\
  108.          (M)[1].x = ROW_VEC2 ((A)[1],(B),x),\
  109.          (M)[1].y = ROW_VEC2 ((A)[1],(B),y))
  110.  
  111. #define LMAT_VEC2(V,A,M)\
  112.         ((V).x = ROW_VEC2 ((A),(M),x),\
  113.          (V).y = ROW_VEC2 ((A),(M),y))
  114.  
  115. #define RMAT_VEC2(V,M,A)\
  116.         ((V).x = DOT_VEC2 ((A),(M)[0]),\
  117.          (V).y = DOT_VEC2 ((A),(M)[1]))
  118.  
  119. /*
  120. ** MAKE_FRAME2(F,O,A,B,s) = Create a local frame F where O is the origin,
  121. **                          (A,B) are the axis and 's' a dummy real variable
  122. ** LOCAL_FRAME2(V,F,A) = Transform A from world frame into local frame F
  123. ** WORLD_FRAME2(V,F,A) = Transform A from local frame F into world frame
  124. */
  125.  
  126. #define MAKE_FRAME2(F,O,A,B,s)\
  127.         (COPY_VEC2 ((F)[0],(A)),\
  128.          COPY_VEC2 ((F)[1],(B)),\
  129.          COPY_VEC2 ((F)[2],(O)),\
  130.      INV_MAT2 ((F)+6,(F),s))
  131.  
  132. #define LOCAL_FRAME2(V,F,A)\
  133.         (DEC_VEC2 ((A),(F)[2]),\
  134.          LMAT_VEC2 ((V),(A),(F)+6),\
  135.          INC_VEC2 ((A),(F)[2]))
  136.  
  137. #define WORLD_FRAME2(V,F,A)\
  138.         (LMAT_VEC2 ((V),(A),(F)),\
  139.          INC_VEC2 ((V),(F)[2]))
  140.  
  141. #endif
  142.  
  143. /* ------------------------------------------------------------------------- */
  144.