|
|
1.1 root 1: /*******************************************************************
2: * *
3: * File: CIFPLOT/transforms.c *
4: * Written by Dan Fitzpatrick *
5: * copyright 1980 -- Regents of the University of California *
6: * *
7: ********************************************************************/
8:
9: #include <stdio.h>
10: #include <math.h>
11: #include "defs.h"
12: #include "globals.h"
13: #include "parser_defs.h"
14: #include "structs.h"
15: #include "alloc.h"
16:
17: IMPORT alloc();
18: IMPORT point *MakePoint();
19: IMPORT char *Concat();
20:
21: transform *CurTransform;
22: struct TCell *TransStack;
23:
24: transform *
25: MatrixMult(t1,t2)
26: register transform *t1,*t2;
27: /* Return a pointer to a transform that is the product of 't1' & 't2' */
28: {
29: register transform *out;
30: /*
31: int i,j,k;
32: */
33: out = GetTransform();
34: out->refs = 0;
35: if(output == NOPLOT)
36: out->TransString = Concat(t1->TransString,t2->TransString,0);
37: /*
38: for(i=0; i<3; i++)
39: for(j=0; j<3; j++)
40: out->t[i][j] = 0;
41: for(i=0; i<3; i++)
42: for(j=0; j<3; j++)
43: for(k=0; k<3; k++)
44: out->t[i][j] = out->t[i][j] + t1->t[i][k]*t2->t[k][j];
45: */
46: out->t[0][0] = t1->t[0][0]*t2->t[0][0] + t1->t[0][1]*t2->t[1][0];
47: out->t[0][1] = t1->t[0][0]*t2->t[0][1] + t1->t[0][1]*t2->t[1][1];
48: out->t[1][0] = t1->t[1][0]*t2->t[0][0] + t1->t[1][1]*t2->t[1][0];
49: out->t[1][1] = t1->t[1][0]*t2->t[0][1] + t1->t[1][1]*t2->t[1][1];
50: out->t[2][0] = t1->t[2][0]*t2->t[0][0] + t1->t[2][1]*t2->t[1][0] + t2->t[2][0];
51: out->t[2][1] = t1->t[2][0]*t2->t[0][1] + t1->t[2][1]*t2->t[1][1] + t2->t[2][1];
52: return(out);
53: }
54:
55: transform *
56: MakeTransform()
57: /* Return a pointer to an identity transform */
58: {
59: transform *trans;
60: int i,j;
61: trans = GetTransform();
62: if(output == NOPLOT)
63: trans->TransString = Concat("",0);
64: trans->refs = 0;
65: for(i=0; i<3; i++)
66: for(j=0; j<3; j++)
67: if (i==j) trans->t[i][i]=1;
68: else trans->t[i][j]=0;
69: return(trans);
70: }
71:
72: transform *
73: CopyTransform(t)
74: transform *t;
75: {
76: transform *temp;
77: int i,j;
78: temp = MakeTransform();
79: for(i=0; i<3; i++)
80: for(j=0; j<3; j++)
81: temp->t[i][j] = t->t[i][j];
82: return(temp);
83: }
84:
85: transform *
86: Mirror(ch,trans)
87: char ch;
88: transform *trans;
89: /* Return a pointer to a transform that does a mirroring about the
90: * axis specified in 'ch' combined with the effect of 'trans' */
91: {
92: transform *temp,*temp2;
93: temp = MakeTransform();
94: if(output == NOPLOT)
95: Free(temp->TransString);
96: if(ch == 'y' || ch == 'Y') {
97: temp->t[1][1] = -1;
98: if(output == NOPLOT)
99: temp->TransString = Concat(" MY",0);
100: }
101: else if(ch == 'x' || ch == 'X') {
102: temp->t[0][0] = -1;
103: if(output == NOPLOT)
104: temp->TransString = Concat(" MX",0);
105: }
106: else {
107: Error("Mirror call about unknown axis",INTERNAL);
108: if(output == NOPLOT)
109: temp->TransString = Concat(" XX",0);
110: }
111: temp2 = MatrixMult(temp,trans);
112: if(output == NOPLOT)
113: Free(temp->TransString);
114: FreeTransform(temp);
115: return(temp2);
116: }
117:
118: transform *
119: Rotate(pt,trans)
120: point *pt;
121: transform *trans;
122: /* Return a pointer to a transform that does a rotation to a vector
123: * specified by 'pt' combined with the effect of trans. */
124: {
125: transform *temp,*temp2;
126: real c;
127: char s[128];
128:
129: temp = MakeTransform();
130: if(output == NOPLOT) {
131: Free(temp->TransString);
132: sprintf(s," R %d %d",TRUNC(pt->x), TRUNC(pt->y));
133: temp->TransString = Concat(s,0);
134: }
135: c = (real) sqrt((double) ((pt->x)*(pt->x) + (pt->y)*(pt->y)));
136: /* Watch out for divide by 0 */
137: if( -0.00001 <= c && c <= 0.00001 ) Error("Attempted to divide by zero",INTERNAL);
138: temp->t[0][0] = (pt->x)/c; temp->t[0][1] = (pt->y)/c;
139: temp->t[1][0] = -(pt->y)/c; temp->t[1][1] = (pt->x)/c;
140: temp2 = MatrixMult(temp,trans);
141: if(output == NOPLOT)
142: Free(temp->TransString);
143: FreeTransform(temp);
144: return(temp2);
145: }
146:
147: transform *
148: Translate(pt,trans)
149: point *pt;
150: transform *trans;
151: /* Return a pointer to a transform that translates points by the vector
152: * 'pt' combined with the effects of 'trans'. */
153: {
154: transform *temp,*temp2;
155: char s[128];
156:
157: temp = MakeTransform();
158: if(output == NOPLOT) {
159: Free(temp->TransString);
160: sprintf(s," T %d %d",TRUNC(pt->x), TRUNC(pt->y));
161: temp->TransString = Concat(s,0);
162: }
163: temp->t[2][0] = pt->x;
164: temp->t[2][1] = pt->y;
165: temp2 = MatrixMult(temp,trans);
166: if(output == NOPLOT)
167: Free(temp->TransString);
168: FreeTransform(temp);
169: return(temp2);
170: }
171:
172: transform *
173: Scale(a,b,trans)
174: int a,b;
175: transform *trans;
176: /* Return a pointer to a transform that scales points by a/b
177: * combined with the effects of 'trans'. */
178: {
179: transform *temp,*temp2;
180: char s[128];
181:
182: temp = MakeTransform();
183: if(output == NOPLOT) {
184: Free(temp->TransString);
185: sprintf(s," S %d %d",a,b);
186: temp->TransString = Concat(s,0);
187: }
188: temp->t[0][0] = ((real) a)/((real) b);
189: temp->t[1][1] = ((real) a)/((real) b);
190: temp2 = MatrixMult(temp,trans);
191: if(output == NOPLOT)
192: Free(temp->TransString);
193: FreeTransform(temp);
194: return(temp2);
195: }
196:
197: /*
198: real
199: MakeFloat(a,b)
200: int a,b;
201: {
202: real f,d;
203: int rem;
204: f = 0.0;
205: d = 1.0;
206:
207: while(b>0) {
208: rem = b%10;
209: d = d*0.1;
210: f = f + ((real) rem)*d;
211: b = TRUNC(b/10);
212: }
213: return(a>=0 ? ((real) a) + f: ((real) a) - f);
214: }
215: */
216:
217: /*
218: transform *
219: ReadTransform(a,b,c,d,e,f,g,h,i,j,k,l,t)
220: int a,b,c,d,e,f;
221: int g,h,i,j,k,l;
222: transform *t;
223: {
224: transform *temp,*temp2;
225: temp = MakeTransform();
226: temp->t[0][0] = MakeFloat(a,b);
227: temp->t[0][1] = MakeFloat(c,d);
228: temp->t[1][0] = MakeFloat(e,f);
229: temp->t[1][1] = MakeFloat(g,h);
230: temp->t[2][0] = MakeFloat(i,j);
231: temp->t[2][1] = MakeFloat(k,l);
232: temp2 = MatrixMult(temp,t);
233: FreeTransform(temp);
234: return(temp2);
235: }
236: */
237:
238: /*
239: float MakeFlt(i,d)
240: int i;
241: float d;
242: {
243: float f;
244:
245: f = (i < 0) ? i - d : i + d;
246: return(f);
247: }
248:
249: float Float(ch,f)
250: char ch;
251: float f;
252: {
253: float d;
254:
255: d = 0.1*((float) ((ch -'0') + f));
256: return(d);
257: }
258: */
259:
260: point *
261: TransPt(pt,trans)
262: point *pt;
263: transform *trans;
264: /* Transform point */
265: {
266: real x,y;
267:
268: x = pt->x*trans->t[0][0] + pt->y*trans->t[1][0] + trans->t[2][0];
269: y = pt->x*trans->t[0][1] + pt->y*trans->t[1][1] + trans->t[2][1];
270: return(MakePoint(x,y));
271: }
272:
273: Trans(x,y,trans)
274: register real *x,*y;
275: register transform *trans;
276: {
277: register real z;
278:
279: z = *x;
280: *x = z*trans->t[0][0] + (*y)*trans->t[1][0] + trans->t[2][0];
281: *y = z*trans->t[0][1] + (*y)*trans->t[1][1] + trans->t[2][1];
282: }
283:
284: /*
285: InitTransform()
286: {
287: TransStack = NIL;
288: CurTransform = MakeTransform();
289: }
290:
291: PushTransform(trans)
292: transform *trans;
293: /* Push the Current Transform on the transform stack. Multiply
294: * 'trans' by old Current Transform and make the product the
295: * Current Traansform. */
296: /*
297: {
298: struct TCell *p;
299:
300: p = (struct TCell *) alloc(sizeof(struct TCell));
301: p->TPtr = CurTransform;
302: p->TLink = TransStack;
303: TransStack = p;
304: CurTransform = MatrixMult(CurTransform,trans);
305: return;
306: }
307:
308: transform *
309: PopTransform()
310: /* Replace the Current Transform by the top of the transform stack
311: * and pop the transform stack */
312: /*
313: {
314: struct TCell *t;
315:
316: if(TransStack == NIL)
317: Error("Attempted to Pop empty Transform Stack",INTERNAL);
318: CurTransform = TransStack->TPtr;
319: t = TransStack;
320: TransStack = TransStack->TLink;
321: FreeTransform(t);
322: return(CurTransform);
323: }
324: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.