Annotation of OpenXM_contrib2/asir2000/lib/glib, Revision 1.8
1.8 ! takayama 1: /* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.7 2002/07/15 02:14:48 takayama Exp $ */
1.1 takayama 2: /* $Id: drill,v 1.9 2000/11/20 01:44:49 taka Exp $ */
3:
4:
5: /* #define CLIP 1 */
6: /* #define DEBUG 1 */
7: #define LIST 4
8:
1.5 takayama 9: def glib_floor(N) {
1.1 takayama 10: return(pari(floor,N));
11: }
12:
1.5 takayama 13: /*
1.1 takayama 14: def join(A,B) {
15: if (type(B) == 0) return(A);
16: return(append(A,B));
17: }
18: def eigenvalues(M) {
19: return(pari(eigen,M));
20: }
21: def roots(F) {
22: return(pari(roots,F));
23: }
1.5 takayama 24: */
1.1 takayama 25:
26: /* ---------------------------------------------- */
1.8 ! takayama 27: def glib_ox_get_errors(P) {
1.1 takayama 28: ox_push_cmd(P,276);
29: return(ox_pop_cmo(P));
30: }
31:
32: def reset_plot() {
33: extern Glib_process;
34: print(" Sending signal to ox_plot",0);
35: ox_reset(Glib_process);
36: print(" Done.");
37: }
1.5 takayama 38: Glib_ps = 0$
39: Glib_h = []$
1.1 takayama 40: Glib_canvas_x = 400$
41: Glib_canvas_y = 400$
42: Glib_xmin=0$ Glib_xmax=Glib_canvas_x$
43: Glib_ymin=0$ Glib_ymax=Glib_canvas_y$
44: #define START_SERVER \
45: extern Glib_server_started, Glib_process$ \
46: if (type(Glib_server_started) == 0) { \
47: Glib_process = ox_launch_nox(0,"ox_plot")$ \
48: register_handler(reset_plot); \
49: Glib_server_started = 1$ \
50: }$
51:
52: Glib_canvas = -1$
53: Glib_xmag = 1$ Glib_ymag=1$ Glib_xoffset=0$ Glib_yoffset=0$
54: Glib_safe_mode = 2 $
55: Glib_counter = 100$
56:
57: def open_Canvas(P,S) {
58: open_canvas(P,S);
59: R = ox_pop_cmo(P);
60: /*
1.8 ! takayama 61: if (glib_ox_get_errors(P) == []) {
1.1 takayama 62: R = ox_pop_cmo(P);
63: }else{
64: debug;
65: }
66: */
67: return(R);
68: }
69:
1.8 ! takayama 70: /*&usage begin: glib_open()
! 71: It starts the ox_plot server and opens a canvas.
! 72: The canvas size is set to {Glib_canvas_x} X {Glib_canvas_y}
! 73: (the default value is 400).
! 74: This function is automatically called when the user calls glib
! 75: functions.
! 76: end: */
! 77:
1.1 takayama 78: def glib_open() {
79: extern Glib_canvas_x, Glib_canvas_y,
80: Glib_process, Glib_canvas;
81: START_SERVER
82: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
83: glib_check_strict();
84: return(Glib_canvas);
85: }
86:
87: def glib_check() {
88: extern Glib_process, Glib_canvas, Glib_safe_mode,
89: Glib_canvas_x, Glib_canvas_y, Glib_counter;
90: if (Glib_safe_mode == 0) {
91: return(0);
92: }
93: if (Glib_safe_mode == 2) {
94: if (Glib_counter > 0) {
95: Glib_counter--;
96: return(0);
97: }else{
98: Glib_counter=100;
99: }
100: }
101: glib_check_strict();
102: }
103: def glib_check_strict() {
104: extern Glib_process, Glib_canvas, Glib_safe_mode,
105: Glib_canvas_x, Glib_canvas_y, Glib_counter;
106: if (Glib_canvas < 0) {
107: glib_open();
108: }
1.8 ! takayama 109: E = glib_ox_get_errors(Glib_process);
1.1 takayama 110: if (E != []) {
111: ox_pops(Glib_process,200);
112: print(E);
113: print("Warning: ",0);
114: print("Drawing canvas seems to be closed.");
115: print("Opening a new canvas.");
116: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
117: error("Drawing aborted");
118: }
119: }
120:
121: def glib_clear() {
122: extern Glib_process, Glib_canvas;
123: if (Glib_canvas < 0) glib_open();
124: clear_canvas(Glib_process,Glib_canvas);
125: }
126:
1.5 takayama 127: /*&usage begin: glib_window(Xmin,Ymin,Xmax,Ymax)
128: It generates a window with the left top corner [{Xmin},{Ymin}] and
129: the right bottom corner [{Xmax},{Ymax}].
130: example: glib_window(-1,-1,10,10);
131: end: */
1.1 takayama 132: def glib_window(Xmin,Ymin,Xmax,Ymax) {
133: extern Glib_xmin, Glib_xmax, Glib_ymin, Glib_ymax,
134: Glib_canvas_x, Glib_canvas_y, Glib_process, Glib_canvas,
135: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag;
136: if (Xmax <= Xmin) error("glib window: Invalid size");
137: if (Ymax <= Ymin) error("glib window: Invalid size");
1.2 takayama 138: glib_check_arg(Xmin,Ymin); glib_check_arg(Xmax,Ymax);
1.1 takayama 139: Glib_xmin = Xmin; Glib_xmax = Xmax;
140: Glib_ymin = Ymin; Glib_ymax = Ymax;
141: Glib_xoffset = -Xmin;
142: Glib_yoffset = -Ymin;
143: Glib_xmag = Glib_canvas_x/(Xmax-Xmin);
144: Glib_ymag = Glib_canvas_y/(Ymax-Ymin);
145: if (Glib_canvas < 0) glib_open();
146: }
147:
1.2 takayama 148: def glib_check_arg(X,Y) {
149: if (type(X) <= 1 && type(Y) <= 1) return 1;
150: else {
151: print("Error in glib: arguments ",0);
152: print([X,Y],0);
153: print(" are not numbers.");
154: error("Invalid argument for glib_window, glib_putpixel, glib_line.");
155: }
156: }
157:
1.6 takayama 158: /*&usage begin: glib_putpixel(X,Y|color)
1.5 takayama 159: It puts a pixel at [{X},{Y}] with {color}
160: example: glib_putpixel(1,2 | color=0xffff00);
161: end: */
1.1 takayama 162: def glib_putpixel(X,Y) {
163: extern Glib_process, Glib_canvas,
1.5 takayama 164: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps;
1.1 takayama 165: if (Glib_canvas < 0) glib_open();
166: glib_check();
1.2 takayama 167: glib_check_arg(X,Y);
1.3 noro 168: C = getopt(color);
1.5 takayama 169: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
170: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
171: if ( type(C) != -1 ) {
172: draw_obj(Glib_process,Glib_canvas,Pos,C);
173: if (Glib_ps) glib_history(["putpixel",Pos,C]);
174: }else{
175: draw_obj(Glib_process,Glib_canvas,Pos);
176: if (Glib_ps) glib_history(["putpixel",Pos,0]);
177: }
1.1 takayama 178: }
179:
1.6 takayama 180: /*&usage begin: glib_line(X0,Y0,X1,Y1|color)
1.8 ! takayama 181: It draws the line [{X0},{Y0}]-- [{X1},{Y1}] with {color}
1.5 takayama 182: example: glib_line(0,0,5,3/2 | color=0xff00ff);
183: end: */
1.1 takayama 184: def glib_line(X0,Y0,X1,Y1) {
185: extern Glib_xmag, Glib_ymag, Glib_xoffset, Glib_yoffset;
1.3 noro 186: C = getopt(color);
1.2 takayama 187: glib_check_arg(X0,Y0);
188: glib_check_arg(X1,Y1);
1.5 takayama 189: glib_clip_line(glib_floor(Glib_xmag*(X0+Glib_xoffset)),
190: glib_floor(Glib_ymag*(Y0+Glib_yoffset)),
191: glib_floor(Glib_xmag*(X1+Glib_xoffset)),
192: glib_floor(Glib_ymag*(Y1+Glib_yoffset)),C);
1.1 takayama 193: }
194:
1.3 noro 195: def glib_clip_line(X0,Y0,X1,Y1,Color) {
1.1 takayama 196: /* X0, Y0, X1, Y1 should be integers.
197: Coordinates are already translated. */
1.5 takayama 198: extern Glib_process, Glib_canvas, Glib_canvas_x, Glib_canvas_y,
199: Glib_ps;
1.1 takayama 200: if (Glib_canvas < 0) glib_open();
201:
1.5 takayama 202: if (Glib_ps) {
1.1 takayama 203: #ifdef DEBUG
204: print(["clip_line",[X0,Y0,X1,Y1]]);
205: #endif
206: /* clip by x = 0 */
207: S = glib_clip0_x(X0,Y0,X1,Y1,0);
208: #ifdef DEBUG
209: print(["clip0_x",S]);
210: #endif
211: if (type(S) == 0) return;
212: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
213:
214: S = glib_clip1_x(X0,Y0,X1,Y1,Glib_canvas_x-1);
215: #ifdef DEBUG
216: print(["clip1_x",S]);
217: #endif
218: if (type(S) == 0) return;
219: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
220:
221: S = glib_clip0_y(X0,Y0,X1,Y1,0);
222: #ifdef DEBUG
223: print(["clip0_y",S]);
224: #endif
225: if (type(S) == 0) return;
226: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
227:
228: S = glib_clip1_y(X0,Y0,X1,Y1,Glib_canvas_y-1);
229: #ifdef DEBUG
230: print(["clip1_y",S]);
231: #endif
232: if (type(S) == 0) return;
233: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
234:
235: #ifdef DEBUG
236: print([X0,Y0,X1,Y1]);
237: #endif
1.5 takayama 238: }
239:
1.1 takayama 240: glib_check();
1.5 takayama 241: Pos = [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
242: if ( type(Color) != -1 ) {
243: draw_obj(Glib_process,Glib_canvas,Pos,Color);
244: if (Glib_ps) glib_history(["line",Pos,Color]);
245: }else{
246: draw_obj(Glib_process,Glib_canvas,Pos);
247: if (Glib_ps) glib_history(["line",Pos,0]);
248: }
1.1 takayama 249: }
250:
251: def glib_clip0_x(X0,Y0,X1,Y1,Clip) {
252: if (X0 < Clip && X1 < Clip) return(0);
253: if (X0 < Clip && X1 > Clip) {
254: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
255: }
256: if (X1 > Clip && X1 < Clip) {
257: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
258: }
259: return([X0,Y0,X1,Y1]);
260: }
261: def glib_clip0_y(X0,Y0,X1,Y1,Clip) {
262: if (Y0 < Clip && Y1 < Clip) return(0);
263: if (Y0 < Clip && Y1 > Clip) {
264: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
265: }
266: if (Y1 > Clip && Y1 < Clip) {
267: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
268: }
269: return([X0,Y0,X1,Y1]);
270: }
271: def glib_clip1_x(X0,Y0,X1,Y1,Clip) {
272: if (X0 > Clip && X1 > Clip) return(0);
273: if (X0 > Clip && X1 < Clip) {
274: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
275: }
276: if (X1 < Clip && X1 > Clip) {
277: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
278: }
279: return([X0,Y0,X1,Y1]);
280: }
281: def glib_clip1_y(X0,Y0,X1,Y1,Clip) {
282: if (Y0 > Clip && Y1 > Clip) return(0);
283: if (Y0 > Clip && Y1 < Clip) {
284: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
285: }
286: if (Y0 < Clip && Y1 > Clip) {
287: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
288: }
289: return([X0,Y0,X1,Y1]);
1.4 takayama 290: }
291:
1.6 takayama 292: /*&usage begin: glib_print(X,Y,Text|color)
1.4 takayama 293: It put a string {Text} at [{X},{Y}] on the glib canvas.
294: example: glib_print(100,100,"Hello Worlds" | color=0xff0000);
295: end: */
296: def glib_print(X,Y,Text) {
297: extern Glib_process, Glib_canvas,
1.5 takayama 298: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps;
1.4 takayama 299: if (Glib_canvas < 0) glib_open();
300: glib_check();
301: glib_check_arg(X,Y);
302: if (type(Text) != 7) error("glib_print(X,Y,Text): Text must be a string.");
303: C = getopt(color);
1.5 takayama 304: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
305: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
306: if ( type(C) != -1 ) {
307: draw_string(Glib_process,Glib_canvas,Pos,Text,C);
308: if (Glib_ps) glib_history(["print",Pos,Text,C]);
309: }else{
310: draw_string(Glib_process,Glib_canvas,Pos,Text);
311: if (Glib_ps) glib_history(["print",Pos,Text,0]);
312: }
313: }
314:
315: def glib_history(L) {
316: extern Glib_h, Glib_canvas_x, Glib_canvas_y;
317: if (L[0] == "putpixel" || L[0] == "print") {
318: if (L[1][0] <= Glib_canvas_x && L[1][0] >= 0) {
319: if (L[1][1] <= Glib_canvas_x && L[1][1] >= 0)
320: Glib_h = cons(L,Glib_h);
321: }
322: }else {
323: Glib_h = cons(L,Glib_h);
324: }
325: return 0;
326: }
327:
1.6 takayama 328: /*&usage begin: glib_tops()
1.5 takayama 329: If Glib_ps is set to 1,
330: it returns a postscript program to draw the picture on the canvas.
1.8 ! takayama 331: ref: print_output
1.5 takayama 332: end: */
333: def glib_tops() {
334: extern Glib_h;
335: return glib_ps(Glib_h);
336: }
337: def glib_ps(L) {
338: PS = "";
339: Prev_color = 0;
340: /* Prolog */
341: PS += "%%!PS-Adobe-1.0\n";
342: PS += "%%BoundingBox: 0 0 " +
343: rtostr(Glib_canvas_x) + " " + rtostr(Glib_canvas_y) + "\n";
344: PS += "%%Creator: This is generated by ifplot\n";
345: PS += "%%Title: ifplot\n";
346: PS += "%%EndComments: \n";
347: PS += "0.1 setlinewidth \n";
348: PS += "2 setlinecap \n";
349: PS += "2 setlinejoin \n";
350: PS += "/ifplot_putpixel { \n";
351: PS += " /yyy 2 1 roll def /xxx 2 1 roll def \n";
352: PS += " gsave newpath xxx yyy .5 0 360 arc \n";
353: PS += " fill grestore \n";
354: PS += "} def \n";
355:
356: L = reverse(L);
357: N = length(L);
358: for (I=0; I<N; I++) {
359: C = L[I];
360: if (C[length(C)-1] != Prev_color) {
361: Prev_color = C[length(C)-1];
362: PS += rtostr(deval(ishift(Prev_color,16)/256)) + " " +
363: rtostr(deval(iand(ishift(Prev_color,8),0xff)/256)) + " " +
364: rtostr(deval(iand(Prev_color,0xff)/256)) + " setrgbcolor \n";
365: }
366: if (C[0] == "putpixel") {
367: PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " ifplot_putpixel \n";
368: }
369: if (C[0] == "line") {
370: PS += " newpath ";
371: PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto " +
372: rtostr(C[1][2]) + " " + rtostr(C[1][3]) + " lineto stroke \n";
373: }
374: if (C[0] == "print") {
375: PS += "/Times-Roman findfont 10 scalefont setfont \n";
376: PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto ";
377: PS += "(" + C[2] + ") show \n";
378: }
379: }
380:
381: /* Epilog */
382: PS += "0 0 0 setrgbcolor \n";
383: PS += "showpage \n";
384:
385: return PS;
1.1 takayama 386: }
387: end$
388:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>