Annotation of OpenXM_contrib2/asir2000/lib/glib, Revision 1.14
1.14 ! takayama 1: /* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.13 2004/03/11 05:16:11 takayama Exp $ */
1.1 takayama 2: /* $Id: drill,v 1.9 2000/11/20 01:44:49 taka Exp $ */
3:
1.9 takayama 4: /* BUG: this library has not yet been adapted to the new automatic launcher
5: of ox_plot. 2002, Aug, 4. Do not load glib more than twice.
6: */
1.1 takayama 7:
1.12 takayama 8: #define DO_NOT_START_SERVER_EXPLICITLY
9:
1.1 takayama 10: /* #define CLIP 1 */
11: /* #define DEBUG 1 */
12: #define LIST 4
13:
1.5 takayama 14: def glib_floor(N) {
1.1 takayama 15: return(pari(floor,N));
16: }
17:
1.5 takayama 18: /*
1.1 takayama 19: def join(A,B) {
20: if (type(B) == 0) return(A);
21: return(append(A,B));
22: }
23: def eigenvalues(M) {
24: return(pari(eigen,M));
25: }
26: def roots(F) {
27: return(pari(roots,F));
28: }
1.5 takayama 29: */
1.1 takayama 30:
31: /* ---------------------------------------------- */
1.8 takayama 32: def glib_ox_get_errors(P) {
1.1 takayama 33: ox_push_cmd(P,276);
34: return(ox_pop_cmo(P));
35: }
36:
37: def reset_plot() {
38: extern Glib_process;
39: print(" Sending signal to ox_plot",0);
40: ox_reset(Glib_process);
41: print(" Done.");
42: }
1.5 takayama 43: Glib_ps = 0$
44: Glib_h = []$
1.1 takayama 45: Glib_canvas_x = 400$
46: Glib_canvas_y = 400$
47: Glib_xmin=0$ Glib_xmax=Glib_canvas_x$
48: Glib_ymin=0$ Glib_ymax=Glib_canvas_y$
1.11 takayama 49: Glib_math_coordinate=0$
1.12 takayama 50:
1.1 takayama 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) {
1.12 takayama 58: extern Glib_process;
59: if (P < 0) P=open_canvas(S); /* BUG, get process No. */
60: else open_canvas(P,S);
61: Glib_process = P;
1.1 takayama 62: R = ox_pop_cmo(P);
63: /*
1.8 takayama 64: if (glib_ox_get_errors(P) == []) {
1.1 takayama 65: R = ox_pop_cmo(P);
66: }else{
67: debug;
68: }
69: */
70: return(R);
71: }
72:
1.8 takayama 73: /*&usage begin: glib_open()
74: It starts the ox_plot server and opens a canvas.
75: The canvas size is set to {Glib_canvas_x} X {Glib_canvas_y}
76: (the default value is 400).
77: This function is automatically called when the user calls glib
78: functions.
79: end: */
80:
1.1 takayama 81: def glib_open() {
82: extern Glib_canvas_x, Glib_canvas_y,
1.12 takayama 83: Glib_process, Glib_canvas,
84: Glib_server_started, Glib_process$
85: #ifndef DO_NOT_START_SERVER_EXPLICITLY
86: if (type(Glib_server_started) == 0) {
87: Glib_process = ox_launch_nox(0,"ox_plot")$
88: register_handler(reset_plot);
89: Glib_server_started = 1$
90: }$
1.1 takayama 91: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
1.12 takayama 92: #else
93: Glib_canvas = open_Canvas(-1,[Glib_canvas_x,Glib_canvas_y])$
94: register_handler(reset_plot);
95: Glib_server_started = 1$
96: #endif
1.1 takayama 97: glib_check_strict();
98: return(Glib_canvas);
99: }
100:
101: def glib_check() {
102: extern Glib_process, Glib_canvas, Glib_safe_mode,
103: Glib_canvas_x, Glib_canvas_y, Glib_counter;
104: if (Glib_safe_mode == 0) {
105: return(0);
106: }
107: if (Glib_safe_mode == 2) {
108: if (Glib_counter > 0) {
109: Glib_counter--;
110: return(0);
111: }else{
112: Glib_counter=100;
113: }
114: }
115: glib_check_strict();
116: }
117: def glib_check_strict() {
118: extern Glib_process, Glib_canvas, Glib_safe_mode,
119: Glib_canvas_x, Glib_canvas_y, Glib_counter;
120: if (Glib_canvas < 0) {
121: glib_open();
122: }
1.8 takayama 123: E = glib_ox_get_errors(Glib_process);
1.1 takayama 124: if (E != []) {
125: ox_pops(Glib_process,200);
126: print(E);
127: print("Warning: ",0);
128: print("Drawing canvas seems to be closed.");
129: print("Opening a new canvas.");
130: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
131: error("Drawing aborted");
132: }
133: }
134:
135: def glib_clear() {
136: extern Glib_process, Glib_canvas;
137: if (Glib_canvas < 0) glib_open();
138: clear_canvas(Glib_process,Glib_canvas);
139: }
140:
1.5 takayama 141: /*&usage begin: glib_window(Xmin,Ymin,Xmax,Ymax)
142: It generates a window with the left top corner [{Xmin},{Ymin}] and
143: the right bottom corner [{Xmax},{Ymax}].
1.11 takayama 144: If the global variable {Glib_math_coordinate} is set to 1, mathematical
145: coordinate system will be employed, i.e., the left top
146: corner will have the coordinate [{Xmin},{Ymax}].
1.5 takayama 147: example: glib_window(-1,-1,10,10);
148: end: */
1.1 takayama 149: def glib_window(Xmin,Ymin,Xmax,Ymax) {
150: extern Glib_xmin, Glib_xmax, Glib_ymin, Glib_ymax,
151: Glib_canvas_x, Glib_canvas_y, Glib_process, Glib_canvas,
152: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag;
153: if (Xmax <= Xmin) error("glib window: Invalid size");
154: if (Ymax <= Ymin) error("glib window: Invalid size");
1.2 takayama 155: glib_check_arg(Xmin,Ymin); glib_check_arg(Xmax,Ymax);
1.1 takayama 156: Glib_xmin = Xmin; Glib_xmax = Xmax;
157: Glib_ymin = Ymin; Glib_ymax = Ymax;
158: Glib_xoffset = -Xmin;
159: Glib_yoffset = -Ymin;
160: Glib_xmag = Glib_canvas_x/(Xmax-Xmin);
161: Glib_ymag = Glib_canvas_y/(Ymax-Ymin);
162: if (Glib_canvas < 0) glib_open();
163: }
164:
1.2 takayama 165: def glib_check_arg(X,Y) {
166: if (type(X) <= 1 && type(Y) <= 1) return 1;
167: else {
168: print("Error in glib: arguments ",0);
169: print([X,Y],0);
170: print(" are not numbers.");
171: error("Invalid argument for glib_window, glib_putpixel, glib_line.");
172: }
173: }
174:
1.6 takayama 175: /*&usage begin: glib_putpixel(X,Y|color)
1.5 takayama 176: It puts a pixel at [{X},{Y}] with {color}
177: example: glib_putpixel(1,2 | color=0xffff00);
178: end: */
1.1 takayama 179: def glib_putpixel(X,Y) {
180: extern Glib_process, Glib_canvas,
1.11 takayama 181: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps,
182: Glib_math_coordinate, Glib_canvas_y;
1.1 takayama 183: if (Glib_canvas < 0) glib_open();
184: glib_check();
1.2 takayama 185: glib_check_arg(X,Y);
1.3 noro 186: C = getopt(color);
1.11 takayama 187: if (Glib_math_coordinate) {
188: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
189: Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
190: Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
191: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
192: }else{
193: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
194: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
195: Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
196: Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
197: }
1.5 takayama 198: if ( type(C) != -1 ) {
199: draw_obj(Glib_process,Glib_canvas,Pos,C);
1.11 takayama 200: if (Glib_ps) glib_history(["putpixel",Pos2,C]);
1.5 takayama 201: }else{
202: draw_obj(Glib_process,Glib_canvas,Pos);
1.11 takayama 203: if (Glib_ps) glib_history(["putpixel",Pos2,0]);
1.5 takayama 204: }
1.1 takayama 205: }
206:
1.6 takayama 207: /*&usage begin: glib_line(X0,Y0,X1,Y1|color)
1.8 takayama 208: It draws the line [{X0},{Y0}]-- [{X1},{Y1}] with {color}
1.5 takayama 209: example: glib_line(0,0,5,3/2 | color=0xff00ff);
210: end: */
1.1 takayama 211: def glib_line(X0,Y0,X1,Y1) {
212: extern Glib_xmag, Glib_ymag, Glib_xoffset, Glib_yoffset;
1.3 noro 213: C = getopt(color);
1.2 takayama 214: glib_check_arg(X0,Y0);
215: glib_check_arg(X1,Y1);
1.5 takayama 216: glib_clip_line(glib_floor(Glib_xmag*(X0+Glib_xoffset)),
217: glib_floor(Glib_ymag*(Y0+Glib_yoffset)),
218: glib_floor(Glib_xmag*(X1+Glib_xoffset)),
219: glib_floor(Glib_ymag*(Y1+Glib_yoffset)),C);
1.1 takayama 220: }
221:
1.3 noro 222: def glib_clip_line(X0,Y0,X1,Y1,Color) {
1.1 takayama 223: /* X0, Y0, X1, Y1 should be integers.
224: Coordinates are already translated. */
1.5 takayama 225: extern Glib_process, Glib_canvas, Glib_canvas_x, Glib_canvas_y,
1.11 takayama 226: Glib_ps, Glib_math_coordinate;
1.1 takayama 227: if (Glib_canvas < 0) glib_open();
228:
1.5 takayama 229: if (Glib_ps) {
1.1 takayama 230: #ifdef DEBUG
231: print(["clip_line",[X0,Y0,X1,Y1]]);
232: #endif
233: /* clip by x = 0 */
234: S = glib_clip0_x(X0,Y0,X1,Y1,0);
235: #ifdef DEBUG
236: print(["clip0_x",S]);
237: #endif
238: if (type(S) == 0) return;
239: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
240:
241: S = glib_clip1_x(X0,Y0,X1,Y1,Glib_canvas_x-1);
242: #ifdef DEBUG
243: print(["clip1_x",S]);
244: #endif
245: if (type(S) == 0) return;
246: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
247:
248: S = glib_clip0_y(X0,Y0,X1,Y1,0);
249: #ifdef DEBUG
250: print(["clip0_y",S]);
251: #endif
252: if (type(S) == 0) return;
253: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
254:
255: S = glib_clip1_y(X0,Y0,X1,Y1,Glib_canvas_y-1);
256: #ifdef DEBUG
257: print(["clip1_y",S]);
258: #endif
259: if (type(S) == 0) return;
260: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
261:
262: #ifdef DEBUG
263: print([X0,Y0,X1,Y1]);
264: #endif
1.5 takayama 265: }
266:
1.1 takayama 267: glib_check();
1.11 takayama 268: if (Glib_math_coordinate) {
269: Pos = [glib_floor(X0),Glib_canvas_y-glib_floor(Y0),
270: glib_floor(X1),Glib_canvas_y-glib_floor(Y1)];
271: Pos2= [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
272: }else{
273: Pos = [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
274: Pos2 = [glib_floor(X0),Glib_canvas_y-glib_floor(Y0),
275: glib_floor(X1),Glib_canvas_y-glib_floor(Y1)];
276: }
1.5 takayama 277: if ( type(Color) != -1 ) {
278: draw_obj(Glib_process,Glib_canvas,Pos,Color);
1.11 takayama 279: if (Glib_ps) glib_history(["line",Pos2,Color]);
1.5 takayama 280: }else{
281: draw_obj(Glib_process,Glib_canvas,Pos);
1.11 takayama 282: if (Glib_ps) glib_history(["line",Pos2,0]);
1.5 takayama 283: }
1.1 takayama 284: }
285:
286: def glib_clip0_x(X0,Y0,X1,Y1,Clip) {
287: if (X0 < Clip && X1 < Clip) return(0);
288: if (X0 < Clip && X1 > Clip) {
289: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
290: }
291: if (X1 > Clip && X1 < Clip) {
292: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
293: }
294: return([X0,Y0,X1,Y1]);
295: }
296: def glib_clip0_y(X0,Y0,X1,Y1,Clip) {
297: if (Y0 < Clip && Y1 < Clip) return(0);
298: if (Y0 < Clip && Y1 > Clip) {
299: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
300: }
301: if (Y1 > Clip && Y1 < Clip) {
302: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
303: }
304: return([X0,Y0,X1,Y1]);
305: }
306: def glib_clip1_x(X0,Y0,X1,Y1,Clip) {
307: if (X0 > Clip && X1 > Clip) return(0);
308: if (X0 > Clip && X1 < Clip) {
309: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
310: }
311: if (X1 < Clip && X1 > Clip) {
312: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
313: }
314: return([X0,Y0,X1,Y1]);
315: }
316: def glib_clip1_y(X0,Y0,X1,Y1,Clip) {
317: if (Y0 > Clip && Y1 > Clip) return(0);
318: if (Y0 > Clip && Y1 < Clip) {
319: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
320: }
321: if (Y0 < Clip && Y1 > Clip) {
322: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
323: }
324: return([X0,Y0,X1,Y1]);
1.4 takayama 325: }
326:
1.6 takayama 327: /*&usage begin: glib_print(X,Y,Text|color)
1.4 takayama 328: It put a string {Text} at [{X},{Y}] on the glib canvas.
329: example: glib_print(100,100,"Hello Worlds" | color=0xff0000);
330: end: */
331: def glib_print(X,Y,Text) {
332: extern Glib_process, Glib_canvas,
1.11 takayama 333: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps,
334: Glib_math_coordinate, Glib_canvas_y;
1.4 takayama 335: if (Glib_canvas < 0) glib_open();
336: glib_check();
337: glib_check_arg(X,Y);
338: if (type(Text) != 7) error("glib_print(X,Y,Text): Text must be a string.");
339: C = getopt(color);
1.11 takayama 340: if (Glib_math_coordinate) {
341: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
342: Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
343: Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
344: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
345: }else{
346: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
347: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
348: Pos2= [glib_floor(Glib_xmag*(X+Glib_xoffset)),
349: Glib_canvas_y-glib_floor(Glib_ymag*(Y+Glib_yoffset))];
350: }
1.5 takayama 351: if ( type(C) != -1 ) {
352: draw_string(Glib_process,Glib_canvas,Pos,Text,C);
1.11 takayama 353: if (Glib_ps) glib_history(["print",Pos2,Text,C]);
1.5 takayama 354: }else{
355: draw_string(Glib_process,Glib_canvas,Pos,Text);
1.11 takayama 356: if (Glib_ps) glib_history(["print",Pos2,Text,0]);
1.5 takayama 357: }
358: }
359:
360: def glib_history(L) {
1.11 takayama 361: extern Glib_h, Glib_canvas_x, Glib_canvas_y,
362: Glib_math_coodinate;
1.5 takayama 363: if (L[0] == "putpixel" || L[0] == "print") {
364: if (L[1][0] <= Glib_canvas_x && L[1][0] >= 0) {
365: if (L[1][1] <= Glib_canvas_x && L[1][1] >= 0)
366: Glib_h = cons(L,Glib_h);
367: }
368: }else {
369: Glib_h = cons(L,Glib_h);
370: }
371: return 0;
372: }
373:
1.6 takayama 374: /*&usage begin: glib_tops()
1.5 takayama 375: If Glib_ps is set to 1,
376: it returns a postscript program to draw the picture on the canvas.
1.8 takayama 377: ref: print_output
1.5 takayama 378: end: */
379: def glib_tops() {
380: extern Glib_h;
381: return glib_ps(Glib_h);
382: }
383: def glib_ps(L) {
1.13 takayama 384: PS = string_to_tb("");
1.5 takayama 385: Prev_color = 0;
386: /* Prolog */
1.13 takayama 387: write_to_tb("%%!PS-Adobe-1.0\n",PS);
388: write_to_tb("%%BoundingBox: 0 0 " +
389: rtostr(Glib_canvas_x) + " " + rtostr(Glib_canvas_y) + "\n",PS);
390: write_to_tb("%%Creator: This is generated by ifplot\n",PS);
391: write_to_tb("%%Title: ifplot\n",PS);
392: write_to_tb("%%EndComments: \n",PS);
393: write_to_tb("0.1 setlinewidth \n",PS);
394: write_to_tb("2 setlinecap \n",PS);
395: write_to_tb("2 setlinejoin \n",PS);
396: write_to_tb("/ifplot_putpixel { \n",PS);
397: write_to_tb(" /yyy 2 1 roll def /xxx 2 1 roll def \n",PS);
398: write_to_tb(" gsave newpath xxx yyy .5 0 360 arc \n",PS);
399: write_to_tb(" fill grestore \n",PS);
400: write_to_tb("} def \n",PS);
1.5 takayama 401:
402: L = reverse(L);
403: N = length(L);
404: for (I=0; I<N; I++) {
405: C = L[I];
406: if (C[length(C)-1] != Prev_color) {
407: Prev_color = C[length(C)-1];
1.13 takayama 408: write_to_tb(rtostr(deval(ishift(Prev_color,16)/256)) + " " +
1.5 takayama 409: rtostr(deval(iand(ishift(Prev_color,8),0xff)/256)) + " " +
1.13 takayama 410: rtostr(deval(iand(Prev_color,0xff)/256)) + " setrgbcolor \n",PS);
1.5 takayama 411: }
412: if (C[0] == "putpixel") {
1.13 takayama 413: write_to_tb(rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " ifplot_putpixel \n",PS);
1.5 takayama 414: }
415: if (C[0] == "line") {
1.13 takayama 416: write_to_tb(" newpath ",PS);
417: write_to_tb(rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto " +
418: rtostr(C[1][2]) + " " + rtostr(C[1][3]) + " lineto stroke \n",PS);
1.5 takayama 419: }
420: if (C[0] == "print") {
1.13 takayama 421: write_to_tb("/Times-Roman findfont 10 scalefont setfont \n",PS);
422: write_to_tb(rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto ",PS);
423: write_to_tb("(" + C[2] + ") show \n",PS);
1.5 takayama 424: }
425: }
426:
427: /* Epilog */
1.13 takayama 428: write_to_tb("0 0 0 setrgbcolor \n",PS);
429: write_to_tb("showpage \n",PS);
1.5 takayama 430:
1.13 takayama 431: return tb_to_string(PS);
1.14 ! takayama 432: }
! 433:
! 434: /*&usage begin: glib_ps_form(S)
! 435: It returns the PS code generated by executing {S}
! 436: (experimental).
! 437: BUG. Only one function call is allowed as an argument.
! 438: example: glib_ps_form(quote( glib_line(0,0,100,100) ));
! 439: ref: glib_tops
! 440: end: */
! 441: def glib_ps_form(F) {
! 442: extern Glib_h;
! 443: extern Glib_ps;
! 444: H = Glib_h; /* push Glib_h */
! 445: P = Glib_ps; /* push Glib_ps */
! 446:
! 447: Glib_ps=1; Glib_h = [];
! 448: eval_quote(F);
! 449: /* bug. eval_str causes seg fault for ccurve.rr main(8) */
! 450: R = glib_tops();
! 451: Glib_h = H; /* pop Glib_h */
! 452: Glib_ps = P; /* pop Glib_ps */
! 453: return R;
1.1 takayama 454: }
1.10 takayama 455:
456: /*&usage begin: glib_plot(F)
457: It plots an object {F} on the glib canvas.
458: example: glib_plot([[0,1],[0.1,0.9],[0.2,0.7],[0.3,0.5],[0.4,0.8]]);
459: example: glib_plot(tan(x));
460: end: */
461: /* bug, xmin, xmax, color should be optional variables. */
462: def glib_plot(F) {
463: Opt = getopt();
464: taka_glib_plot(F,Opt);
465: }
466:
1.1 takayama 467: end$
468:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>