home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / ciftomann.tar / CD / xforms.c < prev    next >
C/C++ Source or Header  |  1988-01-28  |  8KB  |  306 lines

  1. /*
  2.  * xforms.c
  3.  *
  4.  * Copyright -C- 1981 Kenneth H. Keller, Giles C. Billingsley
  5.  * sccsid "%W%  %G%"
  6.  *
  7.  *     KIC is a graphics editor that was developed by the integrated
  8.  * circuits group of the Electronics Research Laboratory and the
  9.  * Department of Electrical Engineering and Computer Sciences at
  10.  * the University of California, Berkeley, California.  The program
  11.  * KIC is available free of charge to any interested party.
  12.  * The sale, resale, or use of this program for profit without the
  13.  * express written consent of the Department of Electrical Engineering
  14.  * and Computer Sciences, University of California, Berkeley, California,
  15.  * is forbidden.
  16.  */
  17.  
  18.  
  19. /*
  20.  * Transforms package.
  21.  * 
  22.  */
  23.  
  24. #include "cd.h"
  25.  
  26. static struct tt TTransforms;
  27.  
  28. TInit(){
  29.     TTransforms.ttSP = 0;
  30.     TIdentity();
  31.     }
  32.  
  33. TEmpty(){
  34.     if(TTransforms.ttSP == 0)
  35.     return(True);
  36.     else
  37.     return(False);
  38.     }
  39.  
  40. TFull(){
  41.     if(TTransforms.ttSP == XFORMSTACKSIZE)
  42.     return(True);
  43.     else
  44.     return(False);
  45.     }
  46.  
  47. TPush(){
  48.     int Int1,Int2;
  49.  
  50.     for(Int1 = 0;Int1 < 3;++Int1)
  51.     for(Int2 = 0;Int2 < 2;++Int2)
  52.         TTransforms.ttStack[TTransforms.ttSP][Int1][Int2] = 
  53.         TTransforms.ttCurrent[Int1][Int2];
  54.     ++TTransforms.ttSP;
  55.     }
  56.  
  57. TPop(){
  58.     int Int1,Int2;
  59.  
  60.     --TTransforms.ttSP;
  61.     for(Int1 = 0;Int1 < 3;++Int1){
  62.     for(Int2 = 0;Int2 < 2;++Int2){
  63.         TTransforms.ttCurrent[Int1][Int2]
  64.         = TTransforms.ttStack[TTransforms.ttSP][Int1][Int2];
  65.         }
  66.     }
  67.     }
  68.  
  69. TCurrent(TFP)
  70.     int *TFP;
  71.     {
  72.     int i,j;
  73.     for(i=0; i<3; ++i){
  74.     for(j=0; j<3; ++j){
  75.             TFP[(3 * i) + j] = TTransforms.ttCurrent[i][j];
  76.         }
  77.     }
  78.     }
  79.  
  80. TTranslate(X,Y)
  81.     int X,Y;
  82.     {
  83.     TTransforms.ttCurrent[2][0] = TTransforms.ttCurrent[2][0]+X;
  84.     TTransforms.ttCurrent[2][1] = TTransforms.ttCurrent[2][1]+Y;
  85.     }
  86.  
  87. TMY(){
  88.     TTransforms.ttCurrent[0][1] = -TTransforms.ttCurrent[0][1];
  89.     TTransforms.ttCurrent[1][1] = -TTransforms.ttCurrent[1][1];
  90.     TTransforms.ttCurrent[2][1] = -TTransforms.ttCurrent[2][1];
  91.     }
  92.  
  93. TMX(){
  94.     TTransforms.ttCurrent[0][0] = -TTransforms.ttCurrent[0][0];
  95.     TTransforms.ttCurrent[1][0] = -TTransforms.ttCurrent[1][0];
  96.     TTransforms.ttCurrent[2][0] = -TTransforms.ttCurrent[2][0];
  97.     }
  98.  
  99. TRotate(XDirection,YDirection)
  100.     int XDirection,YDirection;
  101.     /*
  102.     Rotation angle is expressed as a CIF-style direction vector.
  103.     */
  104.     {
  105.     int Int1;
  106.  
  107.     if(XDirection == 0){
  108.     if(abs(YDirection) > 1)
  109.         if(YDirection < 0)
  110.         YDirection = -1;
  111.         else
  112.         YDirection = 1;
  113.     }
  114.     elif(YDirection == 0){
  115.     if(abs(XDirection) > 1)
  116.         if(XDirection < 0)
  117.         XDirection = -1;
  118.         else
  119.         XDirection = 1;
  120.     }
  121.     if(XDirection == 1 And YDirection == 0)
  122.     /*
  123.     Don't rotate at all.
  124.     */
  125.     return;
  126.     elif(XDirection == 0 And YDirection == -1){
  127.     /*
  128.     Rotate ccw by 270 degrees.
  129.     */
  130.     Int1 = TTransforms.ttCurrent[0][0];
  131.     TTransforms.ttCurrent[0][0] = TTransforms.ttCurrent[0][1];
  132.     TTransforms.ttCurrent[0][1] = -Int1;
  133.     Int1 = TTransforms.ttCurrent[1][0];
  134.     TTransforms.ttCurrent[1][0] = TTransforms.ttCurrent[1][1];
  135.     TTransforms.ttCurrent[1][1] = -Int1;
  136.     Int1 = TTransforms.ttCurrent[2][0];
  137.     TTransforms.ttCurrent[2][0] = TTransforms.ttCurrent[2][1];
  138.     TTransforms.ttCurrent[2][1] = -Int1;
  139.     }
  140.     elif(XDirection == 0 And YDirection == 1){
  141.     /*
  142.     Rotate ccw by 90 degrees.
  143.     */
  144.     Int1 = TTransforms.ttCurrent[0][0];
  145.     TTransforms.ttCurrent[0][0] = -TTransforms.ttCurrent[0][1];
  146.     TTransforms.ttCurrent[0][1] = Int1;
  147.     Int1 = TTransforms.ttCurrent[1][0];
  148.     TTransforms.ttCurrent[1][0] = -TTransforms.ttCurrent[1][1];
  149.     TTransforms.ttCurrent[1][1] = Int1;
  150.     Int1 = TTransforms.ttCurrent[2][0];
  151.     TTransforms.ttCurrent[2][0] = -TTransforms.ttCurrent[2][1];
  152.     TTransforms.ttCurrent[2][1] = Int1;
  153.     }
  154.     elif(XDirection == -1 And YDirection == 0){
  155.     /*
  156.     Rotate ccw by 180 degrees.
  157.     */
  158.     int Int1,Int2;
  159.  
  160.     for(Int1 = 0;Int1 < 3;++Int1){
  161.         for(Int2 = 0;Int2 < 2;++Int2){
  162.         TTransforms.ttCurrent[Int1][Int2]
  163.             = -TTransforms.ttCurrent[Int1][Int2];
  164.         }
  165.         }
  166.     }
  167.     }
  168.  
  169. TIdentity(){
  170.     TTransforms.ttCurrent[0][0] = TTransforms.ttCurrent[1][1] = 
  171.     TTransforms.ttCurrent[2][2] = 1;
  172.     TTransforms.ttCurrent[0][1] = TTransforms.ttCurrent[1][0] =
  173.     TTransforms.ttCurrent[0][2] = TTransforms.ttCurrent[1][2] =
  174.     TTransforms.ttCurrent[2][0] = TTransforms.ttCurrent[2][1] = 0;
  175.     }
  176.  
  177. TPoint(X,Y)
  178.     int *X,*Y;
  179.     /*
  180.     Transform the point.
  181.     */
  182.     {
  183.     int Int1;
  184.  
  185.     Int1 = *X*TTransforms.ttCurrent[0][0]+*Y*TTransforms.ttCurrent[1][0]+
  186.     TTransforms.ttCurrent[2][0];
  187.     *Y = *X*TTransforms.ttCurrent[0][1]+*Y*TTransforms.ttCurrent[1][1]+
  188.     TTransforms.ttCurrent[2][1];
  189.     *X = Int1;
  190.     }
  191.  
  192. TPremultiply(){
  193.     /*
  194.     Form the instance transform.
  195.     This is done by computing TTransforms.ttCurrent*TTransforms.ttStack[
  196.     TTransforms.ttSP-1] and
  197.     placing the product in TTransforms.ttCurrent.    
  198.     So, the scenario for transforming the coordinates of a master follows.
  199.     TPush();
  200.     TIdentity();
  201.     Invoke TMX, TTranslate, etc. to build instance transform.
  202.     Form the instance transform.
  203.     TPremultiply();
  204.     Invoke TPoint to transform master points to instance points.
  205.     TPop();
  206.     */
  207.     int Int1,Int2,Int3,Int4,Int5,Int6;
  208.     int SP;
  209.  
  210.     SP = TTransforms.ttSP-1;
  211.     Int1 = TTransforms.ttCurrent[0][0]*TTransforms.ttStack[SP][0][0]+
  212.     TTransforms.ttCurrent[0][1]*TTransforms.ttStack[SP][1][0];
  213.     Int2 = TTransforms.ttCurrent[0][0]*TTransforms.ttStack[SP][0][1]+
  214.     TTransforms.ttCurrent[0][1]*TTransforms.ttStack[SP][1][1];
  215.     Int3 = TTransforms.ttCurrent[1][0]*TTransforms.ttStack[SP][0][0]+
  216.     TTransforms.ttCurrent[1][1]*TTransforms.ttStack[SP][1][0];
  217.     Int4 = TTransforms.ttCurrent[1][0]*TTransforms.ttStack[SP][0][1]+
  218.     TTransforms.ttCurrent[1][1]*TTransforms.ttStack[SP][1][1];
  219.     Int5 = TTransforms.ttCurrent[2][0]*TTransforms.ttStack[SP][0][0]+
  220.     TTransforms.ttCurrent[2][1]*TTransforms.ttStack[SP][1][0]+
  221.     TTransforms.ttStack[SP][2][0];
  222.     Int6 = TTransforms.ttCurrent[2][0]*TTransforms.ttStack[SP][0][1]+
  223.     TTransforms.ttCurrent[2][1]*TTransforms.ttStack[SP][1][1]+
  224.     TTransforms.ttStack[SP][2][1];
  225.     TTransforms.ttCurrent[0][0] = Int1;
  226.     TTransforms.ttCurrent[0][1] = Int2;
  227.     TTransforms.ttCurrent[1][0] = Int3;
  228.     TTransforms.ttCurrent[1][1] = Int4;
  229.     TTransforms.ttCurrent[2][0] = Int5;
  230.     TTransforms.ttCurrent[2][1] = Int6;
  231.     }
  232.  
  233. TInverse()
  234.     /*
  235.     Compute the inverse transform of the current transform.
  236.     Because all transformations are Manhattan, the
  237.     det of the current transform matrix is always -1 or +1.
  238.     */
  239.     {
  240.     int detCurrent;
  241.  
  242.     detCurrent = 
  243.     TTransforms.ttCurrent[0][0]*
  244.     TTransforms.ttCurrent[1][1]-
  245.     TTransforms.ttCurrent[1][0]*
  246.     TTransforms.ttCurrent[0][1];
  247.  
  248.     if(detCurrent == 1)
  249.     {
  250.     TTransforms.ttInverseCurrent[0][0] = TTransforms.ttCurrent[1][1];
  251.     TTransforms.ttInverseCurrent[0][1] = -TTransforms.ttCurrent[0][1];
  252.     TTransforms.ttInverseCurrent[1][0] = -TTransforms.ttCurrent[1][0];
  253.     TTransforms.ttInverseCurrent[1][1] = TTransforms.ttCurrent[0][0];
  254.     TTransforms.ttInverseCurrent[2][0] =
  255.         TTransforms.ttCurrent[1][0]*
  256.         TTransforms.ttCurrent[2][1]-
  257.         TTransforms.ttCurrent[2][0]*
  258.         TTransforms.ttCurrent[1][1];
  259.     TTransforms.ttInverseCurrent[2][1] =
  260.         -TTransforms.ttCurrent[0][0]*
  261.         TTransforms.ttCurrent[2][1]+
  262.         TTransforms.ttCurrent[0][1]*
  263.         TTransforms.ttCurrent[2][0];
  264.     }
  265.     else
  266.     {
  267.     TTransforms.ttInverseCurrent[0][0] = -TTransforms.ttCurrent[1][1];
  268.     TTransforms.ttInverseCurrent[0][1] = TTransforms.ttCurrent[0][1];
  269.     TTransforms.ttInverseCurrent[1][0] = TTransforms.ttCurrent[1][0];
  270.     TTransforms.ttInverseCurrent[1][1] = -TTransforms.ttCurrent[0][0];
  271.     TTransforms.ttInverseCurrent[2][0] =
  272.         -TTransforms.ttCurrent[1][0]*
  273.         TTransforms.ttCurrent[2][1]+
  274.         TTransforms.ttCurrent[2][0]*
  275.         TTransforms.ttCurrent[1][1];
  276.     TTransforms.ttInverseCurrent[2][1] =
  277.         TTransforms.ttCurrent[0][0]*
  278.         TTransforms.ttCurrent[2][1]-
  279.         TTransforms.ttCurrent[0][1]*
  280.         TTransforms.ttCurrent[2][0];
  281.     }
  282.     TTransforms.ttInverseCurrent[0][2] =
  283.     0;
  284.     TTransforms.ttInverseCurrent[1][2] =
  285.     0;
  286.     TTransforms.ttInverseCurrent[2][2] =
  287.     1;
  288.     }
  289.  
  290. TInversePoint(X,Y)
  291.     int *X,*Y;
  292.     /*
  293.     Transform the point.
  294.     */
  295.     {
  296.     int Int1;
  297.  
  298.     Int1 = *X * TTransforms.ttInverseCurrent[0][0]
  299.      + *Y * TTransforms.ttInverseCurrent[1][0]
  300.      + TTransforms.ttInverseCurrent[2][0];
  301.     *Y   = *X * TTransforms.ttInverseCurrent[0][1]
  302.      + *Y * TTransforms.ttInverseCurrent[1][1]
  303.      + TTransforms.ttInverseCurrent[2][1];
  304.     *X = Int1;
  305.     }
  306.