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