1 /*****************************************************************************
4 ** Name: transformmatrix.cpp
5 ** Function: Transform Matrix routine for graphic library
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: 1-22-85
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: transformmatrix.cpp,v 1.3 2000/12/06 01:46:43 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
29 #include "ctsupport.h"
30 #include "transformmatrix.h"
33 TransformationMatrix2D::TransformationMatrix2D (double m[3][3])
35 mtx[0][0] = m[0][0]; mtx[0][1] = m[0][1]; mtx[0][2] = m[0][2];
36 mtx[1][0] = m[1][0]; mtx[1][1] = m[1][1]; mtx[1][2] = m[1][2];
37 mtx[2][0] = m[2][0]; mtx[2][1] = m[2][1]; mtx[2][2] = m[2][2];
41 TransformationMatrix2D::setTranslate (double x, double y)
50 TransformationMatrix2D::setScale (double sx, double sy)
59 TransformationMatrix2D::setShear (double shrx, double shry)
67 TransformationMatrix2D::setRotate (double theta)
69 double s = sin (theta);
70 double c = cos (theta);
74 mtx[0][0] = c; mtx[0][1] = s;
75 mtx[1][0] = -s; mtx[1][1] = c;
80 TransformationMatrix2D::setIdentity ()
82 mtx[0][0] = 1.; mtx[0][1] = 0.; mtx[0][2] = 0.;
83 mtx[1][0] = 0.; mtx[1][1] = 1.; mtx[1][2] = 0.;
84 mtx[2][0] = 0.; mtx[2][1] = 0.; mtx[2][2] = 1.;
88 const TransformationMatrix2D
89 TransformationMatrix2D::invert () const
93 double determ = determinant ();
94 if (fabs(determ) < 1E-6) {
95 sys_error (ERR_WARNING, "Determinant = %g [TransformationMatrix2D::invert]", determ);
\r
97 std::cout << std::endl;
100 b[0][0] = (mtx[1][1] * mtx[2][2] - mtx[2][1] * mtx[1][2]) / determ;
101 b[1][0] = -(mtx[1][0] * mtx[2][2] - mtx[2][0] * mtx[1][2]) / determ;
102 b[2][0] = (mtx[1][0] * mtx[2][1] - mtx[2][0] * mtx[1][1]) / determ;
104 b[0][1] = -(mtx[0][1] * mtx[2][2] - mtx[2][1] * mtx[0][2]) / determ;
105 b[1][1] = (mtx[0][0] * mtx[2][2] - mtx[2][0] * mtx[0][2]) / determ;
106 b[2][1] = -(mtx[0][0] * mtx[2][1] - mtx[2][0] * mtx[0][1]) / determ;
108 b[0][2] = (mtx[0][1] * mtx[1][2] - mtx[1][1] * mtx[0][2]) / determ;
109 b[1][2] = -(mtx[0][0] * mtx[1][2] - mtx[1][0] * mtx[0][2]) / determ;
110 b[2][2] = (mtx[0][0] * mtx[1][1] - mtx[1][0] * mtx[0][1]) / determ;
112 return TransformationMatrix2D (b);
117 TransformationMatrix2D::determinant () const
120 (mtx[0][0] * mtx[1][1] * mtx[2][2] - mtx[0][0] * mtx[2][1] * mtx[1][2] -
121 mtx[0][1] * mtx[1][0] * mtx[2][2] + mtx[0][1] * mtx[2][0] * mtx[1][2] +
122 mtx[0][2] * mtx[1][0] * mtx[2][1] - mtx[0][2] * mtx[2][0] * mtx[1][1]);
127 TransformationMatrix2D::transformPoint (double* pX, double *pY) const
129 double x = *pX * mtx[0][0] + *pY * mtx[1][0] + mtx[2][0];
130 double y = *pX * mtx[0][1] + *pY * mtx[1][1] + mtx[2][1];
137 TransformationMatrix2D::print (std::ostream& ostr) const
139 std::cout << mtx[0][0] << " " << mtx[0][1] << " " << mtx[0][2] << std::endl;
140 std::cout << mtx[1][0] << " " << mtx[1][1] << " " << mtx[1][2] << std::endl;
141 std::cout << mtx[2][0] << " " << mtx[2][1] << " " << mtx[2][2] << std::endl;
145 // Friend of TransformMatrix2D
147 const TransformationMatrix2D operator* (const TransformationMatrix2D& a, const TransformationMatrix2D& b)
151 for (int row = 0; row < 3; row++)
152 for (int col = 0; col < 3; col++) {
154 for (int calc = 0; calc < 3; calc++)
155 c[row][col] += a.mtx[row][calc] * b.mtx[calc][col];
158 return TransformationMatrix2D (c);