4f88c772debeb81cff0a07d28bdf75105e69bef4
[ctsim.git] / libctgraphics / ctm.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: ctm.cpp,v 1.1 2000/06/19 18:05:03 kevin Exp $
6 **  $Log: ctm.cpp,v $
7 **  Revision 1.1  2000/06/19 18:05:03  kevin
8 **  initial cvs import
9 **
10 **  Revision 1.1  2000/06/13 16:20:31  kevin
11 **  finished c++ conversions
12 **
13 **  Revision 1.2  2000/05/11 14:07:23  kevin
14 **  Fixed compilation warnings
15 **
16 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
17 **  Initial CVS import for first public release
18 **
19 **
20 **
21 **  This program is free software; you can redistribute it and/or modify
22 **  it under the terms of the GNU General Public License (version 2) as
23 **  published by the Free Software Foundation.
24 **
25 **  This program is distributed in the hope that it will be useful,
26 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
27 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 **  GNU General Public License for more details.
29 **
30 **  You should have received a copy of the GNU General Public License
31 **  along with this program; if not, write to the Free Software
32 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 ******************************************************************************/
34
35 /*-----------------------------------------------------------------------------
36  * FILE IDENTIFICATION
37  *
38  *      Name:       ctm.c
39  *      Function:   Current Transform Matrix routine for graphic library
40  *      Programmer: Kevin Rosenberg
41  *      Date Started:   1-22-85
42  *
43  * FUNCTION SUMMARY
44  *      ctm_xlat_pre_2 ()
45  *      ctm_xlat_post_2 ()
46  *      ctm_scale_pre_2 ()
47  *      ctm_scale_post_2 ()
48  *      ctm_rotate_pre_2 ()
49  *      ctm_rotate_post_2 ()
50  *
51  * NOTES
52  *      The indices on the 2d matrix are opposite of the cartesian order used
53  *      in the sdf_2d files.  Here, the 1st index is the row & and 2nd is the
54  *      column
55  *---------------------------------------------------------------------------*/
56
57 #include "kstddef.h"
58 #include <math.h>
59 #include "kmath.h"
60 #include "sgp.h"
61
62
63 /*---------------------------------------------------------------------------*/
64 /*                              Geometric Operations                         */
65 /*---------------------------------------------------------------------------*/
66
67 void
68 ctm_xlat_pre_2 (double x, double y)
69 {
70     GRFMTX_2D m;
71
72     xlat_gmtx_2 (m, x, y);
73     ctm_pre_mult_2 (m);
74 }
75
76
77 void 
78 ctm_xlat_post_2 (double x, double y)
79 {
80     GRFMTX_2D m;
81
82     xlat_gmtx_2 (m, x, y);
83     ctm_post_mult_2 (m);
84 }
85
86
87 void 
88 ctm_scale_pre_2 (double sx, double sy)
89 {
90     GRFMTX_2D m;
91
92     scale_gmtx_2 (m, sx, sy);
93     ctm_pre_mult_2 (m);
94 }
95
96
97 void 
98 ctm_scale_post_2 (double sx, double sy)
99 {
100     GRFMTX_2D m;
101
102     scale_gmtx_2 (m, sx, sy);
103     ctm_post_mult_2 (m);
104 }
105
106
107 void 
108 ctm_rotate_pre_2 (double theta)
109 {
110     GRFMTX_2D m;
111
112     rotate_gmtx_2 (m, theta);
113     ctm_pre_mult_2 (m);
114 }
115
116
117 void 
118 ctm_rotate_post_2 (double theta)
119 {
120     GRFMTX_2D m;
121
122     rotate_gmtx_2 (m, theta);
123     ctm_post_mult_2 (m);
124 }
125
126
127 void 
128 ctm_shear_pre_2 (double shrx, double shry)
129 {
130     GRFMTX_2D m;
131
132     shear_gmtx_2 (m, shrx, shry);
133     ctm_pre_mult_2 (m);
134 }
135
136
137 void 
138 ctm_shear_post_2 (double shrx, double shry)
139 {
140     GRFMTX_2D m;
141
142     shear_gmtx_2 (m, shrx, shry);
143     ctm_post_mult_2 (m);
144 }
145
146 /*---------------------------------------------------------------------------*/
147 /*                      Low-Level Internal Functions                         */
148 /*---------------------------------------------------------------------------*/
149
150
151 void 
152 xlat_gmtx_2 (GRFMTX_2D m, double x, double y)
153 {
154     ident_gmtx_2 (m);
155     m[2][0] = x;
156     m[2][1] = y;
157 }
158
159
160 void 
161 scale_gmtx_2 (GRFMTX_2D m, double sx, double sy)
162 {
163     ident_gmtx_2 (m);
164     m[0][0] = sx;
165     m[1][1] = sy;
166 }
167
168
169 void 
170 shear_gmtx_2 (GRFMTX_2D m, double shrx, double shry)
171 {
172     ident_gmtx_2 (m);
173     m[1][0] = shrx;
174     m[0][1] = shry;
175 }
176
177 void 
178 rotate_gmtx_2 (GRFMTX_2D m, double theta)
179 {
180     double s, c;
181
182     s = sin (theta);
183     c = cos (theta);
184
185     ident_gmtx_2 (m);
186
187     m[0][0] =  c;  m[0][1] = s;
188     m[1][0] = -s;  m[1][1] = c;
189 }
190
191
192 void 
193 ident_gmtx_2 (GRFMTX_2D m)
194 {
195     m[0][0] = 1.;  m[0][1] = 0.;  m[0][2] = 0.;
196     m[1][0] = 0.;  m[1][1] = 1.;  m[1][2] = 0.;
197     m[2][0] = 0.;  m[2][1] = 0.;  m[2][2] = 1.;
198 }
199
200
201 void 
202 mult_gmtx_2 (GRFMTX_2D a, GRFMTX_2D b, GRFMTX_2D c)
203 {
204     int row, col, calc;
205
206     for (row = 0; row < 3; row++)
207         for (col = 0; col < 3; col++) {
208             c[row][col] = 0.;
209             for (calc = 0; calc < 3; calc++)
210                 c[row][col] += a[row][calc] * b[calc][col];
211         }
212 }
213
214 void 
215 invert_gmtx_2 (GRFMTX_2D a, GRFMTX_2D b)
216 {
217     double determ;
218
219     determ = determ_gmtx_2 (a);
220     if (fabs(determ) < 1E-6)
221         sys_error (ERR_WARNING, "Determinant = %lg [invert_gmtx_2]", determ);
222
223     b[0][0] =  (a[1][1] * a[2][2] - a[2][1] * a[1][2]) / determ;
224     b[1][0] = -(a[1][0] * a[2][2] - a[2][0] * a[1][2]) / determ;
225     b[2][0] =  (a[1][0] * a[2][1] - a[2][0] * a[1][1]) / determ;
226
227     b[0][1] = -(a[0][1] * a[2][2] - a[2][1] * a[0][2]) / determ;
228     b[1][1] =  (a[0][0] * a[2][2] - a[2][0] * a[0][2]) / determ;
229     b[2][1] = -(a[0][0] * a[2][1] - a[2][0] * a[0][1]) / determ;
230
231     b[0][2] =  (a[0][1] * a[1][2] - a[1][1] * a[0][2]) / determ;
232     b[1][2] = -(a[0][0] * a[1][2] - a[1][0] * a[0][2]) / determ;
233     b[2][2] =  (a[0][0] * a[1][1] - a[1][0] * a[0][1]) / determ;
234 }
235
236
237 double 
238 determ_gmtx_2 (GRFMTX_2D a)
239 {
240     return
241         (a[0][0] * a[1][1] * a[2][2] - a[0][0] * a[2][1] * a[1][2] -
242          a[0][1] * a[1][0] * a[2][2] + a[0][1] * a[2][0] * a[1][2] +
243          a[0][2] * a[1][0] * a[2][1] - a[0][2] * a[2][0] * a[1][1]);
244 }
245