r104: initial cvs import
[ctsim.git] / libctgraphics / ctm.cpp
diff --git a/libctgraphics/ctm.cpp b/libctgraphics/ctm.cpp
new file mode 100644 (file)
index 0000000..4f88c77
--- /dev/null
@@ -0,0 +1,245 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: ctm.cpp,v 1.1 2000/06/19 18:05:03 kevin Exp $
+**  $Log: ctm.cpp,v $
+**  Revision 1.1  2000/06/19 18:05:03  kevin
+**  initial cvs import
+**
+**  Revision 1.1  2000/06/13 16:20:31  kevin
+**  finished c++ conversions
+**
+**  Revision 1.2  2000/05/11 14:07:23  kevin
+**  Fixed compilation warnings
+**
+**  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
+**  Initial CVS import for first public release
+**
+**
+**
+**  This program is free software; you can redistribute it and/or modify
+**  it under the terms of the GNU General Public License (version 2) as
+**  published by the Free Software Foundation.
+**
+**  This program is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+**  GNU General Public License for more details.
+**
+**  You should have received a copy of the GNU General Public License
+**  along with this program; if not, write to the Free Software
+**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+******************************************************************************/
+
+/*-----------------------------------------------------------------------------
+ * FILE IDENTIFICATION
+ *
+ *     Name:       ctm.c
+ *     Function:   Current Transform Matrix routine for graphic library
+ *     Programmer: Kevin Rosenberg
+ *     Date Started:   1-22-85
+ *
+ * FUNCTION SUMMARY
+ *     ctm_xlat_pre_2 ()
+ *     ctm_xlat_post_2 ()
+ *     ctm_scale_pre_2 ()
+ *     ctm_scale_post_2 ()
+ *     ctm_rotate_pre_2 ()
+ *     ctm_rotate_post_2 ()
+ *
+ * NOTES
+ *     The indices on the 2d matrix are opposite of the cartesian order used
+ *     in the sdf_2d files.  Here, the 1st index is the row & and 2nd is the
+ *     column
+ *---------------------------------------------------------------------------*/
+
+#include "kstddef.h"
+#include <math.h>
+#include "kmath.h"
+#include "sgp.h"
+
+
+/*---------------------------------------------------------------------------*/
+/*                             Geometric Operations                         */
+/*---------------------------------------------------------------------------*/
+
+void
+ctm_xlat_pre_2 (double x, double y)
+{
+    GRFMTX_2D m;
+
+    xlat_gmtx_2 (m, x, y);
+    ctm_pre_mult_2 (m);
+}
+
+
+void 
+ctm_xlat_post_2 (double x, double y)
+{
+    GRFMTX_2D m;
+
+    xlat_gmtx_2 (m, x, y);
+    ctm_post_mult_2 (m);
+}
+
+
+void 
+ctm_scale_pre_2 (double sx, double sy)
+{
+    GRFMTX_2D m;
+
+    scale_gmtx_2 (m, sx, sy);
+    ctm_pre_mult_2 (m);
+}
+
+
+void 
+ctm_scale_post_2 (double sx, double sy)
+{
+    GRFMTX_2D m;
+
+    scale_gmtx_2 (m, sx, sy);
+    ctm_post_mult_2 (m);
+}
+
+
+void 
+ctm_rotate_pre_2 (double theta)
+{
+    GRFMTX_2D m;
+
+    rotate_gmtx_2 (m, theta);
+    ctm_pre_mult_2 (m);
+}
+
+
+void 
+ctm_rotate_post_2 (double theta)
+{
+    GRFMTX_2D m;
+
+    rotate_gmtx_2 (m, theta);
+    ctm_post_mult_2 (m);
+}
+
+
+void 
+ctm_shear_pre_2 (double shrx, double shry)
+{
+    GRFMTX_2D m;
+
+    shear_gmtx_2 (m, shrx, shry);
+    ctm_pre_mult_2 (m);
+}
+
+
+void 
+ctm_shear_post_2 (double shrx, double shry)
+{
+    GRFMTX_2D m;
+
+    shear_gmtx_2 (m, shrx, shry);
+    ctm_post_mult_2 (m);
+}
+
+/*---------------------------------------------------------------------------*/
+/*                     Low-Level Internal Functions                         */
+/*---------------------------------------------------------------------------*/
+
+
+void 
+xlat_gmtx_2 (GRFMTX_2D m, double x, double y)
+{
+    ident_gmtx_2 (m);
+    m[2][0] = x;
+    m[2][1] = y;
+}
+
+
+void 
+scale_gmtx_2 (GRFMTX_2D m, double sx, double sy)
+{
+    ident_gmtx_2 (m);
+    m[0][0] = sx;
+    m[1][1] = sy;
+}
+
+
+void 
+shear_gmtx_2 (GRFMTX_2D m, double shrx, double shry)
+{
+    ident_gmtx_2 (m);
+    m[1][0] = shrx;
+    m[0][1] = shry;
+}
+
+void 
+rotate_gmtx_2 (GRFMTX_2D m, double theta)
+{
+    double s, c;
+
+    s = sin (theta);
+    c = cos (theta);
+
+    ident_gmtx_2 (m);
+
+    m[0][0] =  c;  m[0][1] = s;
+    m[1][0] = -s;  m[1][1] = c;
+}
+
+
+void 
+ident_gmtx_2 (GRFMTX_2D m)
+{
+    m[0][0] = 1.;  m[0][1] = 0.;  m[0][2] = 0.;
+    m[1][0] = 0.;  m[1][1] = 1.;  m[1][2] = 0.;
+    m[2][0] = 0.;  m[2][1] = 0.;  m[2][2] = 1.;
+}
+
+
+void 
+mult_gmtx_2 (GRFMTX_2D a, GRFMTX_2D b, GRFMTX_2D c)
+{
+    int row, col, calc;
+
+    for (row = 0; row < 3; row++)
+       for (col = 0; col < 3; col++) {
+           c[row][col] = 0.;
+           for (calc = 0; calc < 3; calc++)
+               c[row][col] += a[row][calc] * b[calc][col];
+       }
+}
+
+void 
+invert_gmtx_2 (GRFMTX_2D a, GRFMTX_2D b)
+{
+    double determ;
+
+    determ = determ_gmtx_2 (a);
+    if (fabs(determ) < 1E-6)
+       sys_error (ERR_WARNING, "Determinant = %lg [invert_gmtx_2]", determ);
+
+    b[0][0] =  (a[1][1] * a[2][2] - a[2][1] * a[1][2]) / determ;
+    b[1][0] = -(a[1][0] * a[2][2] - a[2][0] * a[1][2]) / determ;
+    b[2][0] =  (a[1][0] * a[2][1] - a[2][0] * a[1][1]) / determ;
+
+    b[0][1] = -(a[0][1] * a[2][2] - a[2][1] * a[0][2]) / determ;
+    b[1][1] =  (a[0][0] * a[2][2] - a[2][0] * a[0][2]) / determ;
+    b[2][1] = -(a[0][0] * a[2][1] - a[2][0] * a[0][1]) / determ;
+
+    b[0][2] =  (a[0][1] * a[1][2] - a[1][1] * a[0][2]) / determ;
+    b[1][2] = -(a[0][0] * a[1][2] - a[1][0] * a[0][2]) / determ;
+    b[2][2] =  (a[0][0] * a[1][1] - a[1][0] * a[0][1]) / determ;
+}
+
+
+double 
+determ_gmtx_2 (GRFMTX_2D a)
+{
+    return
+       (a[0][0] * a[1][1] * a[2][2] - a[0][0] * a[2][1] * a[1][2] -
+        a[0][1] * a[1][0] * a[2][2] + a[0][1] * a[2][0] * a[1][2] +
+        a[0][2] * a[1][0] * a[2][1] - a[0][2] * a[2][0] * a[1][1]);
+}
+