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