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