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