home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / LIB3D30A.ZIP / CTM3D.INT < prev    next >
Text File  |  1994-03-10  |  10KB  |  167 lines

  1. (******************************************************************************
  2. *                                    Ctm3d                                    *
  3. ******************************************************************************)
  4. Unit Ctm3d;
  5.  
  6. (*******************************************************************************
  7. *                          Homogeneous Coordinates                            *
  8. *                          -----------------------                            *
  9. *                                                                             *
  10. *      Homogeneous coordinates allow transformations to be represented by     *
  11. *      matrices. A 3x3 matrix is used for 2D transformations, and a 4x4 matrix*
  12. *      for 3D transformations.                                                *
  13. *                                                                             *
  14. *      THIS MODULE IMPLEMENTS ONLY 3D TRANSFORMATIONS.                        *
  15. *                                                                             *
  16. *      in homogeneous coordination the point P(x,y,z) is represented as       *
  17. *      P(w*x, w*y, w*z, w) for any scale factor w!=0.                         *
  18. *      in this module w == 1.                                                 *
  19. *                                                                             *
  20. * Transformations:                                                            *
  21. *          1. translation                                                     *
  22. *                  [x, y, z] --> [x + Dx, y + Dy, z + Dz]                     *
  23. *                                                                             *
  24. *                              ┌          ┐                                   *
  25. *                              │1  0  0  0│                                   *
  26. *              T(Dx, Dy, Dz) = │0  1  0  0│                                   *
  27. *                              │0  0  1  0│                                   *
  28. *                              │Dx Dy Dz 1│                                   *
  29. *                              └          ┘                                   *
  30. *          2. scaling                                                         *
  31. *                  [x, y, z] --> [Sx * x, Sy * y, Sz * z]                     *
  32. *                                                                             *
  33. *                          ┌          ┐                                       *
  34. *                          │Sx 0  0  0│                                       *
  35. *              S(Sx, Sy) = │0  Sy 0  0│                                       *
  36. *                          │0  0  Sz 0│                                       *
  37. *                          │0  0  0  1│                                       *
  38. *                          └          ┘                                       *
  39. *                                                                             *
  40. *          3. rotation                                                        *
  41. *                                                                             *
  42. *              a) Around the Z axis:                                          *
  43. *                                                                             *
  44. *                  [x, y, z] --> [x*cost - t*sint, x*sint + y*cost, z]        *
  45. *                      ┌                  ┐                                   *
  46. *                      │cost  sint   0   0│                                   *
  47. *              Rz(t) = │-sint cost   0   0│                                   *
  48. *                      │0     0      1   0│                                   *
  49. *                      │0     0      0   1│                                   *
  50. *                      └                  ┘                                   *
  51. *                                                                             *
  52. *              b) Around the X axis:                                          *
  53. *                                                                             *
  54. *                  [x, y, z] --> [x, y*cost - z*sint, y*sint + z*cost]        *
  55. *                      ┌                  ┐                                   *
  56. *                      │1     0     0    0│                                   *
  57. *              Rx(t) = │0     cost  sint 0│                                   *
  58. *                      │0    -sint  cost 0│                                   *
  59. *                      │0     0     0    1│                                   *
  60. *                      └                  ┘                                   *
  61. *                                                                             *
  62. *              c) Around the Y axis:                                          *
  63. *                                                                             *
  64. *                  [x, y, z] --> [xcost + z*sint, y, z*cost - x*sint]         *
  65. *                      ┌                  ┐                                   *
  66. *                      │cost  0   -sint  0│                                   *
  67. *              Ry(t) = │0     1    0     0│                                   *
  68. *                      │sint  0    cost  0│                                   *
  69. *                      │0     0    0     1│                                   *
  70. *                      └                  ┘                                   *
  71. *                                                                             *
  72. *   transformation of the vector [x,y,z,1] by transformation matrix T is given *
  73. *   by the formula:                                                           *
  74. *                                         ┌   ┐                               *
  75. *              [x', y', z', 1] = [x,y,z,1]│ T │                               *
  76. *                                         └   ┘                               *
  77. * Optimizations:                                                              *
  78. *   The most general composition of R, S and T operations will produce a matrix*
  79. *   of the form:                                                              *
  80. *              ┌                       ┐                                      *
  81. *              │r11    r12     r13    0│                                      *
  82. *              │r21    r22     r23    0│                                      *
  83. *              │r31    r32     r33    0│                                      *
  84. *              │tx     ty      tz     1│                                      *
  85. *              └                       ┘                                      *
  86. *   The task of matrix multiplication can be simplified by                    *
  87. *      x' = x*r11 + y*r21 + z*r31 + tx                                        *
  88. *      y' = x*r12 + y*r22 + z*r32 + ty                                        *
  89. *      z' = x*r13 + y*r23 + z*r33 + tz                                        *
  90. *                                                                             *
  91. *                                                                             *
  92. * See also:                                                                   *
  93. *      "Fundamentals of Interactive Computer Graphics" J.D FOLEY A.VAN DAM    *
  94. *      Adison-Weslely ISBN 0-201-14468-9 pp 245-265                           *
  95. *******************************************************************************)
  96.  
  97. interface
  98.  
  99. uses 
  100.    hdr3d
  101.    ;
  102.  
  103. type
  104.     ctmPtr = ^ ctm;         
  105.     ctm = object
  106.        r11, r12, r13   : real; { change to single if numeric processor is present }
  107.        r21, r22, r23   : real;
  108.        r31, r32, r33   : real;
  109.        tx,  ty,  tz    : real;
  110.  
  111.        constructor SetUnit; { set to the unit (I) matrix }
  112.        constructor Copy(var src : ctm); { construct from another }
  113.        procedure save(var dest : ctm);
  114.  
  115.        procedure translate(Dx, Dy, Dz : real); { used to move .. }
  116.  
  117.        procedure translateX(dx : real);
  118.        procedure translateY(dy : real);
  119.        procedure translateZ(dz : real); { translate in one axis only }
  120.        {use these routines for single axis translations, they are faster!}
  121.  
  122.        procedure rotateX(t : real);
  123.        procedure rotateY(t : real);
  124.        procedure rotateZ(t : real);
  125.  
  126.        procedure scale(Sx, Sy, Sz : real);
  127.  
  128.        procedure scaleX(sx : real);
  129.        procedure scaleY(sy : real);
  130.        procedure scaleZ(sz : real);
  131.        {use these routines for single axis scaling, they are faster!!!}
  132.  
  133.        procedure Left_translate(Dx, Dy, Dz : real);
  134.  
  135.        procedure Left_translateX(dx : real);
  136.        procedure Left_translateY(dy : real);
  137.        procedure Left_translateZ(dz : real);
  138.        {use these Left_ routines for single axis translations, they are faster!}
  139.  
  140.        procedure Left_rotateX(t : real);
  141.        procedure Left_rotateY(t : real);
  142.        procedure Left_rotateZ(t : real);
  143.  
  144.        procedure Left_scale(Sx, Sy, Sz : real);
  145.  
  146.        procedure Left_scaleX(sx : real);
  147.        procedure Left_scaleY(sy : real);
  148.        procedure Left_scaleZ(sz : real);
  149.        {use these routines for single axis scaling, they are faster!!!}
  150.  
  151.        procedure transform(var t: point3d; p : point3d);
  152.        {
  153.          Note that t (target) is var, but p is NOT.
  154.          p cannot be a var parameter, because if the same point is
  155.          transformed, data should be copied for correct results
  156.        }
  157.        procedure inv_transform(var p : point3d);
  158.        { inv_transform changes the input point }
  159.  
  160.        procedure inverse; { M ^-1 .Bring the matrix to (-1) power}
  161.        procedure multiply(var c : ctm); {multiply from right self * c}
  162.        procedure Multiply_2(var a, b : ctm);
  163.     end;
  164.  
  165. implementation
  166. end.
  167.