Replace IS-BOOLE-CHECK with the new $IS-BOOLE-EVAL
[maxima.git] / share / draw / gnuplot.lisp
blobcce71b7cd0ff14005d3b2270595622a5d17bbcc7
1 ;;; COPYRIGHT NOTICE
2 ;;;
3 ;;; Copyright (C) 2007-2016 Mario Rodriguez Riotorto
4 ;;; Time-stamp: "2022-03-25 12:13:47 villate"
5 ;;;
6 ;;; This program is free software; you can redistribute
7 ;;; it and/or modify it under the terms of the
8 ;;; GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 2
10 ;;; of the License, or (at your option) any later version.
11 ;;;
12 ;;; This program is distributed in the hope that it
13 ;;; will be useful, but WITHOUT ANY WARRANTY;
14 ;;; without even the implied warranty of MERCHANTABILITY
15 ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details at
17 ;;; http://www.gnu.org/copyleft/gpl.html
19 ;;; This is a maxima-gnuplot interface.
21 ;;; Visit
22 ;;; http://tecnostats.net/Maxima/gnuplot
23 ;;; for examples
25 ;;; For questions, suggestions, bugs and the like, feel free
26 ;;; to contact me at
27 ;;; riotorto @@@ yahoo DOT com
30 ;; use $draw_version to save package version
31 ;; and to know whether the package was loaded
32 ($put '$gnuplot 1 '$version); to be removed in the future
33 (defvar $draw_version 2)
36 (defun write-font-type ()
37 (if (and (string= (get-option '$font) "") (not (eql (get-option '$font_size) 10)))
38 (mwarning "Cannot set the gnuplot font size without a font name."))
40 (if (or (eq (get-option '$font) nil) (string= (get-option '$font) ""))
42 (format nil "font '~a,~a'" (get-option '$font) (get-option '$font_size))))
45 ;; one-window multiplot: consecutive calls
46 ;; to draw allways plot on the same window
47 (defvar *multiplot-is-active* nil)
48 (defun $multiplot_mode (term)
49 (case term
50 ($screen
51 ($multiplot_mode '$none)
52 (send-gnuplot-command
53 (format nil "set terminal GNUTERM dashed ~a~%set multiplot~%" (write-font-type)))
54 (setf *multiplot-is-active* t))
55 ($wxt
56 ($multiplot_mode '$none)
57 (send-gnuplot-command
58 (format nil "set terminal wxt dashed ~a~%set multiplot~%" (write-font-type)))
59 (setf *multiplot-is-active* t))
60 ($qt
61 ($multiplot_mode '$none)
62 (send-gnuplot-command
63 (format nil "set terminal qt dashed ~a~%set multiplot~%" (write-font-type)))
64 (setf *multiplot-is-active* t))
65 ($windows
66 ($multiplot_mode '$none)
67 (send-gnuplot-command
68 (format nil "set terminal windows dashed ~a~%set multiplot~%" (write-font-type)))
69 (setf *multiplot-is-active* t))
70 ($none
71 (send-gnuplot-command
72 (format nil "unset multiplot~%unset output~%"))
73 (setf *multiplot-is-active* nil))
74 (otherwise
75 (merror "draw: ~M is not recognized as a multiplot mode" term))))
79 ;; This function is called from the graphic objects constructors
80 ;; (points, rectangle, etc.). When a new object is created, and if
81 ;; the user hasn't especified an x or y range, ranges are computed
82 ;; automaticallly by calling this function. There is a trick so
83 ;; that object constructors know if they can modify global variables
84 ;; xrange and yrange; if these lists are of length 2, it means that
85 ;; it was a user selection and they can't be altered; if they are of
86 ;; length 3 (with a dummy 0), object constructors should make the necessary changes
87 ;; to fit the objects in the window; if they are nil, default
88 ;; value, constructors are also allowed to make changes.
89 (defmacro update-range (axi vmin vmax)
90 `(case (length (get-option ,axi))
91 (0 (setf (gethash ,axi *gr-options*) (list ,vmin ,vmax 0)))
92 (3 (setf (gethash ,axi *gr-options*) (list (min ,vmin (first (get-option ,axi)))
93 (max ,vmax (second (get-option ,axi)))
94 0))) ))
96 (defun update-ranges-2d (xmin xmax ymin ymax)
97 (if (get-option '$xaxis_secondary)
98 (update-range '$xrange_secondary xmin xmax)
99 (update-range '$xrange xmin xmax))
100 (if (get-option '$yaxis_secondary)
101 (update-range '$yrange_secondary ymin ymax)
102 (update-range '$yrange ymin ymax)) )
104 (defun update-ranges-3d (xmin xmax ymin ymax zmin zmax)
105 (update-ranges-2d xmin xmax ymin ymax)
106 (update-range '$zrange zmin zmax))
108 (defmacro check-extremes-x ()
109 '(when (numberp xx)
110 (when (< xx xmin) (setf xmin xx))
111 (when (> xx xmax) (setf xmax xx))))
113 (defmacro check-extremes-y ()
114 '(when (numberp yy)
115 (when (< yy ymin) (setf ymin yy))
116 (when (> yy ymax) (setf ymax yy))))
118 (defmacro check-extremes-z ()
119 '(when (numberp zz)
120 (when (< zz zmin) (setf zmin zz))
121 (when (> zz zmax) (setf zmax zz))))
123 ;; Controls whether the actual graphics object must
124 ;; be plotted against the primary or the secondary axes,
125 ;; both horizontal and vertical. Secondary axes in 3D
126 ;; are not yet supported.
127 (defun axes-to-plot ()
128 (format nil "~a~a"
129 (if (get-option '$xaxis_secondary)
130 "x2"
131 "x1")
132 (if (get-option '$yaxis_secondary)
133 "y2"
134 "y1")))
136 (defstruct gr-object
137 name command groups points)
139 (defun make-obj-title (str)
140 (if (= (length str) 0)
141 "notitle"
142 (if (> (length str) 80)
143 (concatenate 'string "t '" (subseq str 0 75) " ...'")
144 (concatenate 'string "t '" str "'"))))
150 ;; Object: 'errors'
151 ;; Usage:
152 ;; errors([[x1,y1,...], [x2,y2,...], [x3,y3,...],...])
153 ;; Options:
154 ;; error_type
155 ;; points_joined
156 ;; line_width
157 ;; key
158 ;; line_type
159 ;; color
160 ;; fill_density
161 ;; xaxis_secondary
162 ;; yaxis_secondary
163 (defun-checked errors (arg)
164 (let ((etype (get-option '$error_type))
165 (joined (get-option '$points_joined))
166 element-size
167 pts pltcmd grouping xmin xmax ymin ymax with)
168 (if (and ($listp arg)
169 (every #'$listp (rest arg)))
170 (setf element-size (length (cdadr arg)))
171 (merror "draw (errors object): incorrect input format"))
172 (unless (every #'(lambda (z) (= element-size ($length z))) (rest arg))
173 (merror "draw (errors object): lists of different sizes"))
174 ; create plot command
175 (cond ((and (eql etype '$x)
176 (or (= element-size 3)
177 (= element-size 4)))
178 (if (null joined)
179 (setf with "xerrorbars")
180 (setf with "xerrorlines")) )
181 ((and (eql etype '$y)
182 (or (= element-size 3)
183 (= element-size 4)))
184 (if (null joined)
185 (setf with "yerrorbars")
186 (setf with "yerrorlines")) )
187 ((and (eql etype '$xy)
188 (or (= element-size 4)
189 (= element-size 6)))
190 (if (null joined)
191 (setf with "xyerrorbars")
192 (setf with "xyerrorlines")) )
193 ((and (eql etype '$boxes)
194 (or (= element-size 4)
195 (= element-size 6)))
196 (setf with "boxxyerrorbars") )
198 (merror "draw (errors object): incompatibility with option error_type")))
200 (setf grouping `((,element-size 0)))
201 (setf pltcmd
202 (format nil
203 " ~a w ~a ~a lw ~a lt ~a lc ~a axis ~a"
204 (make-obj-title (get-option '$key))
205 with
206 (if (eql etype '$boxes) ; in case of boxes, should they be filled?
207 (format nil "fs solid ~a"
208 (get-option '$fill_density))
210 (get-option '$line_width)
211 (get-option '$line_type)
212 (hex-to-rgb (get-option '$color))
213 (axes-to-plot)))
214 (setf pts (map 'list #'rest (rest ($float arg))))
215 (let ((x (map 'list #'first pts))
216 (y (map 'list #'second pts)))
217 (setf xmin ($tree_reduce 'min (cons '(mlist simp) x))
218 xmax ($tree_reduce 'max (cons '(mlist simp) x))
219 ymin ($tree_reduce 'min (cons '(mlist simp) y))
220 ymax ($tree_reduce 'max (cons '(mlist simp) y))))
221 (update-ranges-2d xmin xmax ymin ymax)
222 (make-gr-object
223 :name 'errors
224 :command pltcmd
225 :groups grouping
226 :points (list (make-array (* element-size (length pts))
227 :element-type 'flonum
228 :initial-contents (flatten pts)))) ))
235 ;; Object: 'points'
236 ;; Usage:
237 ;; points([[x1,y1], [x2,y2], [x3,y3],...])
238 ;; points([x1,x2,x3,...], [y1,y2,y3,...])
239 ;; points([y1,y2,y3,...]), abscissas are automatically chosen: 1,2,3,...
240 ;; points(matrix), one-column, one-row, two-column or two-row matrix
241 ;; points(array1d)
242 ;; points(array1d, array1d)
243 ;; points(array2d), two-column or two-row array
244 ;; Options:
245 ;; point_size
246 ;; point_type
247 ;; points_joined
248 ;; line_width
249 ;; key
250 ;; line_type
251 ;; color
252 ;; xaxis_secondary
253 ;; yaxis_secondary
254 ;; transform
255 (defun points-command ()
256 (let ((pj (get-option '$points_joined))
257 (ps (get-option '$point_size))
258 (pt (get-option '$point_type)) )
259 (cond
260 ((null pj) ; draws isolated points
261 (format nil " ~a w p ps ~a pt ~a lc ~a axis ~a"
262 (make-obj-title (get-option '$key))
265 (hex-to-rgb (get-option '$color))
266 (axes-to-plot)))
267 ((and (eq pj t) (or (= ps 0.0) (= pt 0)) ) ; draws joined points without symbols
268 (format nil " ~a w l lw ~a lt ~a lc ~a axis ~a"
269 (make-obj-title (get-option '$key))
270 (get-option '$line_width)
271 (get-option '$line_type)
272 (hex-to-rgb (get-option '$color))
273 (axes-to-plot)))
274 ((eq pj t) ; draws joined points
275 (format nil " ~a w lp ps ~a pt ~a lw ~a lt ~a lc ~a axis ~a"
276 (make-obj-title (get-option '$key))
279 (get-option '$line_width)
280 (get-option '$line_type)
281 (hex-to-rgb (get-option '$color))
282 (axes-to-plot)))
283 (t ; draws impulses
284 (format nil " ~a w i lw ~a lt ~a lc ~a axis ~a"
285 (make-obj-title (get-option '$key))
286 (get-option '$line_width)
288 (hex-to-rgb (get-option '$color))
289 (axes-to-plot))))) )
291 (defun points-array-2d (arg)
292 (let ((xmin most-positive-double-float)
293 (xmax most-negative-double-float)
294 (ymin most-positive-double-float)
295 (ymax most-negative-double-float)
296 (pos -1)
297 (dim (array-dimensions arg))
298 n xx yy pts twocolumns)
299 (cond
300 ((and (= (length dim) 2) ; two-column array
301 (= (cadr dim) 2))
302 (setf n (car dim))
303 (setf twocolumns t))
304 ((and (= (length dim) 2) ; two-row array
305 (= (car dim) 2))
306 (setf n (cadr dim))
307 (setf twocolumns nil))
308 (t (merror "draw (points2d): bad 2d array input format")))
309 (setf pts (make-array (* 2 n) :element-type 'flonum))
310 (loop for k below n do
311 (if twocolumns
312 (setf xx ($float (aref arg k 0))
313 yy ($float (aref arg k 1)))
314 (setf xx ($float (aref arg 0 k))
315 yy ($float (aref arg 1 k))))
316 (transform-point 2)
317 (check-extremes-x)
318 (check-extremes-y)
319 (setf (aref pts (incf pos)) xx)
320 (setf (aref pts (incf pos)) yy))
321 (update-ranges-2d xmin xmax ymin ymax)
322 (make-gr-object
323 :name 'points
324 :command (points-command)
325 :groups '((2 0)) ; numbers are sent to gnuplot in groups of 2
326 :points (list pts))))
328 (defun points-array-1d (arg1 &optional (arg2 nil))
329 (let ((xmin most-positive-double-float)
330 (xmax most-negative-double-float)
331 (ymin most-positive-double-float)
332 (ymax most-negative-double-float)
333 (pos -1)
334 (dim (array-dimensions arg1))
335 n x y xx yy pts)
336 (cond
337 ((and (null arg2)
338 (= (length dim) 1)) ; y format
339 (setf n (car dim))
340 (setf x (make-array n
341 :element-type 'flonum
342 :initial-contents (loop for k from 1 to n collect ($float k)) ))
343 (setf y (make-array n
344 :element-type 'flonum
345 :initial-contents (loop for k below n collect ($float (aref arg1 k))))))
346 ((and (arrayp arg2) ; xx yy format
347 (= (length dim) 1)
348 (equal dim (array-dimensions arg2)))
349 (setf n (car dim))
350 (setf x arg1
351 y arg2))
352 (t (merror "draw (points2d): bad 1d array input format")))
353 (setf pts (make-array (* 2 n) :element-type 'flonum))
354 (loop for k below n do
355 (setf xx ($float (aref x k))
356 yy ($float (aref y k)))
357 (transform-point 2)
358 (check-extremes-x)
359 (check-extremes-y)
360 (setf (aref pts (incf pos)) xx)
361 (setf (aref pts (incf pos)) yy))
362 (update-ranges-2d xmin xmax ymin ymax)
363 (make-gr-object
364 :name 'points
365 :command (points-command)
366 :groups '((2 0)) ; numbers are sent to gnuplot in groups of 2
367 :points (list pts))))
369 (defun points-list (arg1 &optional (arg2 nil))
370 (let (x y xmin xmax ymin ymax pts)
371 (cond
372 ((and ($listp arg1)
373 (null arg2)
374 (every #'$listp (rest arg1))) ; xy format
375 (let ((tmp (mapcar #'rest (rest arg1))))
376 (setf x (map 'list #'$float (map 'list #'first tmp))
377 y (map 'list #'$float (map 'list #'second tmp)))) )
378 ((and ($matrixp arg1)
379 (= (length (cadr arg1)) 3)
380 (null arg2)) ; two-column matrix
381 (let ((tmp (mapcar #'rest (rest arg1))))
382 (setf x (map 'list #'$float (map 'list #'first tmp))
383 y (map 'list #'$float (map 'list #'second tmp)) ) ) )
384 ((and ($listp arg1)
385 (null arg2)
386 (notany #'$listp (rest arg1))) ; y format
387 (setf x (loop for xx from 1 to (length (rest arg1)) collect ($float xx))
388 y (map 'list #'$float (rest arg1))))
389 ((and ($matrixp arg1)
390 (= (length (cadr arg1)) 2)
391 (null arg2)) ; one-column matrix
392 (setf x (loop for xx from 1 to (length (rest arg1)) collect ($float xx))
393 y (map 'list #'$float (map 'list #'second (rest arg1)))))
394 ((and ($matrixp arg1)
395 (= ($length arg1) 1)
396 (null arg2)) ; one-row matrix
397 (setf x (loop for xx from 1 to (length (cdadr arg1)) collect ($float xx))
398 y (map 'list #'$float (cdadr arg1))))
399 ((and ($listp arg1)
400 ($listp arg2)
401 (= (length arg1) (length arg2))) ; xx yy format
402 (setf x (map 'list #'$float (rest arg1))
403 y (map 'list #'$float (rest arg2))))
404 ((and ($matrixp arg1)
405 (= ($length arg1) 2)
406 (null arg2)) ; two-row matrix
407 (setf x (map 'list #'$float (cdadr arg1))
408 y (map 'list #'$float (cdaddr arg1))))
409 (t (merror "draw (points2d): incorrect input format")))
410 (transform-lists 2)
411 (setf xmin ($tree_reduce 'min (cons '(mlist simp) x))
412 xmax ($tree_reduce 'max (cons '(mlist simp) x))
413 ymin ($tree_reduce 'min (cons '(mlist simp) y))
414 ymax ($tree_reduce 'max (cons '(mlist simp) y)) )
415 (setf pts (make-array (* 2 (length x)) :element-type 'flonum
416 :initial-contents (mapcan #'list x y)))
417 ;; update x-y ranges if necessary
418 (update-ranges-2d xmin xmax ymin ymax)
419 (make-gr-object
420 :name 'points
421 :command (points-command)
422 :groups '((2 0)) ; numbers are sent to gnuplot in groups of 2
423 :points (list pts) ) ))
425 (defun-checked points (arg1 &optional (arg2 nil))
426 (if (arrayp arg1)
427 (if (= (length (array-dimensions arg1)) 2)
428 (points-array-2d arg1)
429 (points-array-1d arg1 arg2))
430 (points-list arg1 arg2)))
438 ;; Object: 'points3d'
439 ;; Usage:
440 ;; points([[x1,y1,z1], [x2,y2,z2], [x3,y3,z3],...])
441 ;; points([x1,x2,x3,...], [y1,y2,y3,...], [z1,z2,z3,...])
442 ;; points(matrix), three-column or three-row matrix
443 ;; points(array2d), three-column or three-row array
444 ;; points(array1d, array1d, array1d, array1d)
445 ;; Options:
446 ;; point_size
447 ;; point_type
448 ;; points_joined
449 ;; line_width
450 ;; key
451 ;; line_type
452 ;; color
453 ;; enhanced3d
454 ;; transform
455 (defun points3d-command ()
456 (let ((pj (get-option '$points_joined))
457 (ps (get-option '$point_size))
458 (pt (get-option '$point_type))
459 (pal (if (> *draw-enhanced3d-type* 0)
460 "palette"
461 (hex-to-rgb (get-option '$color)) )))
462 (cond
463 ((null pj) ; draws isolated points
464 (format nil " ~a w p ps ~a pt ~a lc ~a"
465 (make-obj-title (get-option '$key))
468 pal ))
469 ((and (eq pj t) (or (= ps 0.0) (= pt 0)) ) ; draws joined points without symbols
470 (format nil " ~a w l lw ~a lt ~a lc ~a "
471 (make-obj-title (get-option '$key))
472 (get-option '$line_width)
473 (get-option '$line_type)
474 (hex-to-rgb (get-option '$color))))
475 ((eq pj t) ; draws joined points
476 (format nil " ~a w lp ps ~a pt ~a lw ~a lt ~a lc ~a"
477 (make-obj-title (get-option '$key))
480 (get-option '$line_width)
481 (get-option '$line_type)
482 pal ))
483 (t ; draws impulses
484 (format nil " ~a w i lw ~a lt ~a lc ~a"
485 (make-obj-title (get-option '$key))
486 (get-option '$line_width)
487 (get-option '$line_type)
488 pal )))))
490 (defun-checked points3d (arg1 &optional (arg2 nil) (arg3 nil))
491 (let (pts x y z xmin xmax ymin ymax zmin zmax ncols col)
492 (check-enhanced3d-model "points" '(0 1 3))
493 (cond (($listp arg1) ; list input
494 (cond ((and (every #'$listp (rest arg1)) ; xyz format
495 (null arg2)
496 (null arg3))
497 (let ((tmp (mapcar #'rest (rest arg1))))
498 (setf x (map 'list #'$float (map 'list #'first tmp))
499 y (map 'list #'$float (map 'list #'second tmp))
500 z (map 'list #'$float (map 'list #'third tmp)) ) ) )
501 ((and ($listp arg2) ; xx yy zz format
502 ($listp arg3)
503 (= (length arg1) (length arg2) (length arg3)))
504 (setf x (map 'list #'$float (rest arg1))
505 y (map 'list #'$float (rest arg2))
506 z (map 'list #'$float (rest arg3)) ))
507 (t (merror "draw (points3d): bad list input format"))))
508 (($matrixp arg1) ; matrix input
509 (cond ((and (= (length (cadr arg1)) 4) ; three-column matrix
510 (null arg2)
511 (null arg3))
512 (let ((tmp (mapcar #'rest (rest arg1))))
513 (setf x (map 'list #'$float (map 'list #'first tmp))
514 y (map 'list #'$float (map 'list #'second tmp))
515 z (map 'list #'$float (map 'list #'third tmp)) ) ) )
516 ((and (= ($length arg1) 3) ; three-row matrix
517 (null arg2)
518 (null arg3))
519 (setf x (map 'list #'$float (cdadr arg1))
520 y (map 'list #'$float (cdaddr arg1))
521 z (map 'list #'$float (cdaddr (rest arg1)) ) ) )
522 (t (merror "draw (points3d): bad matrix input format"))))
523 ((arrayp arg1) ; array input
524 (let ((dim (array-dimensions arg1)))
525 (cond ((and (= (length dim) 2) ; three-row array
526 (= (first dim) 3)
527 (null arg2)
528 (null arg3))
529 (setf x (loop for k from 0 below (second dim)
530 collect ($float (aref arg1 0 k)))
531 y (loop for k from 0 below (second dim)
532 collect ($float (aref arg1 1 k)))
533 z (loop for k from 0 below (second dim)
534 collect ($float (aref arg1 2 k))) ))
535 ((and (= (length dim) 2) ; three-column array
536 (= (second dim) 3)
537 (null arg2)
538 (null arg3))
539 (setf x (loop for k from 0 below (first dim)
540 collect ($float (aref arg1 k 0)))
541 y (loop for k from 0 below (first dim)
542 collect ($float (aref arg1 k 1)))
543 z (loop for k from 0 below (first dim)
544 collect ($float (aref arg1 k 2))) ))
545 ((and (= (length dim) 1) ; three 1d arrays
546 (arrayp arg2)
547 (arrayp arg3)
548 (equal dim (array-dimensions arg2))
549 (equal dim (array-dimensions arg3)))
550 (setf x (map 'list #'$float arg1)
551 y (map 'list #'$float arg2)
552 z (map 'list #'$float arg3) ) )
553 (t (merror "draw (points3d): bad array input format")) ) ) )
554 (t (merror "draw (points3d): bad input format")))
555 (transform-lists 3)
556 ; set pm3d colors
557 (cond ((= *draw-enhanced3d-type* 0)
558 (setf ncols 3)
559 (setf pts (make-array (* ncols (length x))
560 :element-type 'flonum
561 :initial-contents (mapcan #'list x y z))))
562 ((= *draw-enhanced3d-type* 1)
563 (setf col (loop for k from 1 to (length x)
564 collect (funcall *draw-enhanced3d-fun* k)))
565 (setf ncols 4)
566 (setf pts (make-array (* ncols (length x))
567 :element-type 'flonum
568 :initial-contents (mapcan #'list x y z col))))
569 ((= *draw-enhanced3d-type* 3)
570 (setf col (mapcar #'(lambda (xx yy zz) (funcall *draw-enhanced3d-fun* xx yy zz)) x y z))
571 (setf ncols 4)
572 (setf pts (make-array (* ncols (length x))
573 :element-type 'flonum
574 :initial-contents (mapcan #'list x y z col)))) )
575 (setf xmin ($tree_reduce 'min (cons '(mlist simp) x))
576 xmax ($tree_reduce 'max (cons '(mlist simp) x))
577 ymin ($tree_reduce 'min (cons '(mlist simp) y))
578 ymax ($tree_reduce 'max (cons '(mlist simp) y))
579 zmin ($tree_reduce 'min (cons '(mlist simp) z))
580 zmax ($tree_reduce 'max (cons '(mlist simp) z)) )
581 ;; update x-y-y ranges if necessary
582 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
583 (make-gr-object
584 :name 'points
585 :command (points3d-command)
586 :groups `((,ncols 0)) ; numbers are sent to gnuplot in groups of 4 or 3
587 ; (depending on colored 4th dimension or not), without blank lines
588 :points (list pts) ) ))
595 ;; Object: 'polygon'
596 ;; Usage:
597 ;; polygon([[x1,y1], [x2,y2], [x3,y3],...])
598 ;; polygon([x1,x2,x3,...], [y1,y2,y3,...])
599 ;; Options:
600 ;; transparent
601 ;; fill_color
602 ;; border
603 ;; line_width
604 ;; line_type
605 ;; color
606 ;; key
607 ;; xaxis_secondary
608 ;; yaxis_secondary
609 ;; transform
610 (defun-checked polygon (arg1 &optional (arg2 nil))
611 (if (and (get-option '$transparent)
612 (not (get-option '$border)))
613 (merror "draw (polygon): transparent is true and border is false; this is not consistent"))
614 (let (pltcmd pts grps x y xmin xmax ymin ymax)
615 (cond ((and ($listp arg1)
616 (every #'$listp (rest arg1))
617 (null arg2) ) ; xy format
618 (let ((tmp (mapcar #'rest (rest arg1))))
619 (setf x (map 'list #'(lambda (z) ($float (first z))) tmp)
620 y (map 'list #'(lambda (z) ($float (second z))) tmp) ) ) )
621 ((and ($listp arg1)
622 ($listp arg2)
623 (= (length arg1) (length arg2))) ; xx yy format
624 (setf x (map 'list #'$float (rest arg1))
625 y (map 'list #'$float (rest arg2))) )
626 (t (merror "draw (polygon): bad input format")) )
627 (transform-lists 2)
628 (setf xmin ($tree_reduce 'min (cons '(mlist simp) x))
629 xmax ($tree_reduce 'max (cons '(mlist simp) x))
630 ymin ($tree_reduce 'min (cons '(mlist simp) y))
631 ymax ($tree_reduce 'max (cons '(mlist simp) y)) )
632 ;; update x-y ranges if necessary
633 (update-ranges-2d xmin xmax ymin ymax)
634 (cond
635 ((get-option '$transparent) ; if transparent, draw only the border
636 (setf pltcmd (format nil " ~a w l lw ~a lt ~a lc ~a axis ~a"
637 (make-obj-title (get-option '$key))
638 (get-option '$line_width)
639 (get-option '$line_type)
640 (hex-to-rgb (get-option '$color))
641 (axes-to-plot)))
642 (setf grps '((2 0))) ; numbers are sent to gnuplot in groups of 2
643 (setf pts (list (make-array (+ (* 2 (length x)) 2)
644 :element-type 'flonum
645 :initial-contents (append (mapcan #'list x y)
646 (list (first x) (first y))) )) ) )
647 ((not (get-option '$border)) ; no transparent, no border
648 (setf pltcmd (format nil " ~a w filledcurves lc ~a axis ~a"
649 (make-obj-title (get-option '$key))
650 (hex-to-rgb (get-option '$fill_color))
651 (axes-to-plot)))
652 (setf grps '((2 0))) ; numbers are sent to gnuplot in groups of 2
653 (setf pts (list (make-array (* 2 (length x))
654 :element-type 'flonum
655 :initial-contents (mapcan #'list x y)) ) ))
656 (t ; no transparent with border
657 (setf pltcmd (list (format nil " ~a w filledcurves lc ~a axis ~a"
658 (make-obj-title (get-option '$key))
659 (hex-to-rgb (get-option '$fill_color))
660 (axes-to-plot))
661 (format nil " t '' w l lw ~a lt ~a lc ~a axis ~a"
662 (get-option '$line_width)
663 (get-option '$line_type)
664 (hex-to-rgb (get-option '$color))
665 (axes-to-plot))))
666 (setf grps '((2 0) (2 0))) ; both sets of vertices (interior and border)
667 ; are sent to gnuplot in groups of 2
668 (setf pts (list (make-array (* 2 (length x))
669 :element-type 'flonum
670 :initial-contents (mapcan #'list x y))
671 (make-array (+ (* 2 (length x)) 2)
672 :element-type 'flonum
673 :initial-contents (append (mapcan #'list x y)
674 (list (first x) (first y))))))))
675 (make-gr-object
676 :name 'polygon
677 :command pltcmd
678 :groups grps
679 :points pts )))
687 ;; Object: 'triangle'
688 ;; Usage:
689 ;; triangle([x1,y1], [x2,y2], [x3,y3])
690 ;; Options:
691 ;; transparent
692 ;; fill_color
693 ;; border
694 ;; line_width
695 ;; line_type
696 ;; color
697 ;; key
698 ;; xaxis_secondary
699 ;; yaxis_secondary
700 ;; transform
701 (defun-checked triangle (arg1 arg2 arg3)
702 (if (or (not ($listp arg1))
703 (not (= ($length arg1) 2))
704 (not ($listp arg2))
705 (not (= ($length arg2) 2))
706 (not ($listp arg3))
707 (not (= ($length arg3) 2)))
708 (merror "draw2d (triangle): vertices are not correct"))
709 (let* ((x1 ($float (cadr arg1)))
710 (y1 ($float (caddr arg1)))
711 (x2 ($float (cadr arg2)))
712 (y2 ($float (caddr arg2)))
713 (x3 ($float (cadr arg3)))
714 (y3 ($float (caddr arg3)))
715 (grobj (polygon `((mlist simp)
716 ((mlist simp) ,x1 ,y1)
717 ((mlist simp) ,x2 ,y2)
718 ((mlist simp) ,x3 ,y3)
719 ((mlist simp) ,x1 ,y1)))))
720 (setf (gr-object-name grobj) 'triangle)
721 grobj))
728 ;; Object: 'quadrilateral'
729 ;; Usage:
730 ;; quadrilateral([x1,y1], [x2,y2], [x3,y3], [x4,y4])
731 ;; Options:
732 ;; transparent
733 ;; fill_color
734 ;; border
735 ;; line_width
736 ;; line_type
737 ;; color
738 ;; key
739 ;; xaxis_secondary
740 ;; yaxis_secondary
741 ;; transform
742 (defun-checked quadrilateral (arg1 arg2 arg3 arg4)
743 (if (or (not ($listp arg1))
744 (not (= ($length arg1) 2))
745 (not ($listp arg2))
746 (not (= ($length arg2) 2))
747 (not ($listp arg3))
748 (not (= ($length arg3) 2))
749 (not ($listp arg4))
750 (not (= ($length arg4) 2)))
751 (merror "draw2d (quadrilateral): vertices are not correct"))
752 (let* ((x1 ($float (cadr arg1)))
753 (y1 ($float (caddr arg1)))
754 (x2 ($float (cadr arg2)))
755 (y2 ($float (caddr arg2)))
756 (x3 ($float (cadr arg3)))
757 (y3 ($float (caddr arg3)))
758 (x4 ($float (cadr arg4)))
759 (y4 ($float (caddr arg4)))
760 (grobj (polygon `((mlist simp)
761 ((mlist simp) ,x1 ,y1)
762 ((mlist simp) ,x2 ,y2)
763 ((mlist simp) ,x3 ,y3)
764 ((mlist simp) ,x4 ,y4)
765 ((mlist simp) ,x1 ,y1)))))
766 (setf (gr-object-name grobj) 'quadrilateral)
767 grobj))
775 ;; Object: 'rectangle'
776 ;; Usage:
777 ;; rectangle([x1,y1], [x2,y2]), being [x1,y1] & [x2,y2] opposite vertices
778 ;; Options:
779 ;; transparent
780 ;; fill_color
781 ;; border
782 ;; line_width
783 ;; line_type
784 ;; color
785 ;; key
786 ;; xaxis_secondary
787 ;; yaxis_secondary
788 ;; transform
789 (defun-checked rectangle (arg1 arg2)
790 (if (or (not ($listp arg1))
791 (not (= ($length arg1) 2))
792 (not ($listp arg2))
793 (not (= ($length arg2) 2)))
794 (merror "draw2d (rectangle): vertices are not correct"))
795 (let* ((x1 ($float (cadr arg1)))
796 (y1 ($float (caddr arg1)))
797 (x2 ($float (cadr arg2)))
798 (y2 ($float (caddr arg2)))
799 (grobj (polygon `((mlist simp)
800 ((mlist simp) ,x1 ,y1)
801 ((mlist simp) ,x2 ,y1)
802 ((mlist simp) ,x2 ,y2)
803 ((mlist simp) ,x1 ,y2)
804 ((mlist simp) ,x1 ,y1)))))
805 (setf (gr-object-name grobj) 'rectangle)
806 grobj))
814 ;; Object: 'ellipse'
815 ;; Usage:
816 ;; ellipse(xc, yc, a, b, ang1 ang2)
817 ;; Options:
818 ;; nticks
819 ;; transparent
820 ;; fill_color
821 ;; border
822 ;; line_width
823 ;; line_type
824 ;; key
825 ;; color
826 ;; xaxis_secondary
827 ;; yaxis_secondary
828 ;; transform
829 (defun-checked ellipse (xc yc a b ang1 ang2)
830 (if (and (get-option '$transparent)
831 (not (get-option '$border)))
832 (merror "draw2d (ellipse): transparent is true and border is false; this is not consistent"))
833 (let ((fxc ($float xc))
834 (fyc ($float yc))
835 (fa ($float a))
836 (fb ($float b))
837 (fang1 ($float ang1))
838 (fang2 ($float ang2))
839 (nticks (get-option '$nticks))
840 (xmin most-positive-double-float)
841 (xmax most-negative-double-float)
842 (ymin most-positive-double-float)
843 (ymax most-negative-double-float)
844 (result nil)
845 pts grps tmin tmax eps xx yy tt pltcmd)
846 (when (or (notevery #'floatp (list fxc fyc fa fb fang1 fang2))
847 (<= fa 0.0)
848 (<= fb 0.0))
849 (merror "draw (ellipse): illegal argument(s)"))
850 ; degrees to radians
851 (setf fang1 (* 0.017453292519943295 fang1)
852 fang2 (* 0.017453292519943295 fang2))
853 (setf tmin (min fang1 (+ fang1 fang2))
854 tmax (max fang1 (+ fang1 fang2))
855 eps (/ (- tmax tmin) (- nticks 1)))
856 (setf tt tmin)
857 (loop
858 (setf xx (+ fxc (* fa (cos tt))))
859 (setf yy (+ fyc (* fb (sin tt))))
860 (transform-point 2)
861 (check-extremes-x)
862 (check-extremes-y)
863 (setf result (append (list xx yy) result))
864 (if (>= tt tmax) (return))
865 (setf tt (+ tt eps))
866 (if (>= tt tmax) (setq tt tmax)) )
867 (when (> *draw-transform-dimensions* 0)
868 (let ((xold fxc)
869 (yold fyc))
870 (setf fxc (funcall *draw-transform-f1* xold yold)
871 fyc (funcall *draw-transform-f2* xold yold))) )
872 ; update x-y ranges if necessary
873 (setf xmin (min fxc xmin)
874 xmax (max fxc xmax)
875 ymin (min fyc ymin)
876 ymax (max fyc ymax))
877 (update-ranges-2d xmin xmax ymin ymax)
878 (cond
879 ((get-option '$transparent) ; if transparent, draw only the border
880 (setf pltcmd (format nil " ~a w l lw ~a lt ~a lc ~a axis ~a"
881 (make-obj-title (get-option '$key))
882 (get-option '$line_width)
883 (get-option '$line_type)
884 (hex-to-rgb (get-option '$color))
885 (axes-to-plot)))
886 (setf grps '((2 0)))
887 (setf pts `( ,(make-array (length result) :element-type 'flonum
888 :initial-contents result))) )
889 ((not (get-option '$border)) ; no transparent, no border
890 (setf pltcmd (format nil " ~a w filledcurves xy=~a,~a lc ~a axis ~a"
891 (make-obj-title (get-option '$key))
892 fxc fyc
893 (hex-to-rgb (get-option '$fill_color))
894 (axes-to-plot)))
895 (setf grps '((2 0)))
896 (setf pts `( ,(make-array (length result) :element-type 'flonum
897 :initial-contents result))) )
898 (t ; no transparent with border
899 (setf pltcmd (list (format nil " ~a w filledcurves xy=~a,~a lc ~a axis ~a"
900 (make-obj-title (get-option '$key))
901 fxc fyc
902 (hex-to-rgb (get-option '$fill_color))
903 (axes-to-plot))
904 (format nil " t '' w l lw ~a lt ~a lc ~a axis ~a"
905 (get-option '$line_width)
906 (get-option '$line_type)
907 (hex-to-rgb (get-option '$color))
908 (axes-to-plot))))
909 (setf grps '((2 0) (2 0)))
910 (setf pts (list (make-array (length result) :element-type 'flonum
911 :initial-contents result)
912 (make-array (length result) :element-type 'flonum
913 :initial-contents result))) ))
914 (make-gr-object
915 :name 'ellipse
916 :command pltcmd
917 :groups grps
918 :points pts ) ))
927 ;; Object: 'label'
928 ;; Usage in 2d:
929 ;; label([string1,xx1,y1],[string2,xx2,y2],...)
930 ;; Usage in 3d:
931 ;; label([string1,x1,y1,z1],[string2,x2,y2,z2],...)
932 ;; Options:
933 ;; label_alignment
934 ;; label_orientation
935 ;; color
936 ;; xaxis_secondary
937 ;; yaxis_secondary
939 (defun replace-substring (string part replacement)
940 (with-output-to-string (out)
941 (loop with part-length = (length part)
942 for old-pos = 0 then (+ pos part-length)
943 for pos = (search part string
944 :start2 old-pos
945 :test #'char=)
946 do (write-string string out
947 :start old-pos
948 :end (or pos (length string)))
949 when pos do (write-string replacement out)
950 while pos)))
952 (defun-checked label (&rest lab)
953 (let ((n (length lab))
954 (result nil)
955 is2d)
956 (cond ((= n 0)
957 (merror "draw (label): no arguments in object labels"))
958 ((every #'$listp lab)
959 (cond ((every #'(lambda (z) (= 3 ($length z))) lab) ; labels in 2d
960 (setf is2d t))
961 ((every #'(lambda (z) (= 4 ($length z))) lab) ; labels in 3d
962 (setf is2d nil))
964 (merror "draw (label): arguments of not equal length")))
965 (cond (is2d
966 (let (xx yy text)
967 (dolist (k lab)
968 (setf xx ($float ($second k))
969 yy ($float ($third k))
970 ; backslashes are replaced by double backslashes to allow LaTeX code in labels.
971 text (format nil "\"~a\"" (replace-substring ($first k) "\\" "\\\\")))
972 (if (or (not (floatp xx))
973 (not (floatp yy)))
974 (merror "draw (label): non real 2d coordinates"))
975 (transform-point 2)
976 (update-ranges-2d xx xx yy yy)
977 (setf result (append (list xx yy text) result)))))
978 (t ; labels in 3d
979 (let (xx yy zz text)
980 (dolist (k lab)
981 (setf xx ($float ($second k))
982 yy ($float ($third k))
983 zz ($float ($fourth k))
984 text (format nil "\"~a\"" ($first k)) )
985 (if (or (not (floatp xx))
986 (not (floatp yy))
987 (not (floatp zz)))
988 (merror "draw (label): non real 3d coordinates"))
989 (transform-point 3)
990 (update-ranges-3d xx xx yy yy zz zz)
991 (setf result (append (list xx yy zz text) result)))))) )
992 (t (merror "draw (label): illegal arguments")))
993 (make-gr-object
994 :name 'label
995 :command (format nil " t '' w labels ~a ~a tc ~a ~a"
996 (case (get-option '$label_alignment)
997 ($center "center")
998 ($left "left")
999 ($right "right"))
1000 (case (get-option '$label_orientation)
1001 ($horizontal "norotate")
1002 ($vertical "rotate"))
1003 (hex-to-rgb (get-option '$color))
1004 (if is2d
1005 (format nil "axis ~a" (axes-to-plot))
1006 "") )
1007 :groups (if is2d '((3 0)) '((4 0)))
1008 :points (list (make-array (length result) :initial-contents result))) ))
1016 ;; Object: 'bars'
1017 ;; bars([x1,h1,w1],[x2,h2,w2],...), x, height and width
1018 ;; Options:
1019 ;; key
1020 ;; fill_color
1021 ;; fill_density
1022 ;; line_width
1023 ;; xaxis_secondary
1024 ;; yaxis_secondary
1025 (defun-checked bars (&rest boxes)
1026 (let ((n (length boxes))
1027 (count -1)
1028 (xmin most-positive-double-float)
1029 (xmax most-negative-double-float)
1030 (ymin most-positive-double-float)
1031 (ymax most-negative-double-float)
1032 result x h w w2)
1033 (when (= n 0)
1034 (merror "draw2d (bars): no arguments in object bars"))
1035 (when (not (every #'(lambda (z) (and ($listp z) (= 3 ($length z)))) boxes))
1036 (merror "draw2d (bars): arguments must be lists of length three"))
1037 (setf result (make-array (* 3 n) :element-type 'flonum))
1038 (dolist (k boxes)
1039 (setf x ($float ($first k))
1040 h ($float ($second k))
1041 w ($float ($third k)))
1042 (setf w2 (/ w 2))
1043 (setf (aref result (incf count)) x
1044 (aref result (incf count)) h
1045 (aref result (incf count)) w)
1046 (setf xmin (min xmin (- x w2))
1047 xmax (max xmax (+ x w2))
1048 ymin (min ymin h)
1049 ymax (max ymax h)) )
1050 (update-ranges-2d xmin xmax ymin ymax)
1051 (make-gr-object
1052 :name 'bars
1053 :command (format nil " ~a w boxes fs solid ~a lw ~a lc ~a axis ~a"
1054 (make-obj-title (get-option '$key))
1055 (get-option '$fill_density)
1056 (get-option '$line_width)
1057 (hex-to-rgb (get-option '$fill_color))
1058 (axes-to-plot) )
1059 :groups '((3 0)) ; numbers are sent to gnuplot in groups of 3, without blank lines
1060 :points (list (make-array (length result) :initial-contents result))) ))
1069 ;; Object: 'vector'
1070 ;; Usage:
1071 ;; vector([x,y], [dx,dy]), represents vector from [x,y] to [x+dx,y+dy]
1072 ;; Options:
1073 ;; head_both
1074 ;; head_length
1075 ;; head_angle
1076 ;; head_type
1077 ;; line_width
1078 ;; line_type
1079 ;; key
1080 ;; color
1081 ;; unit_vectors
1082 ;; xaxis_secondary
1083 ;; yaxis_secondary
1084 (defun-checked vect (arg1 arg2)
1085 (if (or (not ($listp arg1))
1086 (not (= ($length arg1) 2))
1087 (not ($listp arg2))
1088 (not (= ($length arg2) 2)))
1089 (merror "draw (vector): coordinates are not correct"))
1090 (let ((xo ($float (cadr arg1)))
1091 (yo ($float (caddr arg1)))
1092 (dx ($float (cadr arg2)))
1093 (dy ($float (caddr arg2)))
1094 xdx ydy x y)
1095 (when (and (get-option '$unit_vectors)
1096 (or (/= dx 0) (/= dy 0)))
1097 (let ((module (sqrt (+ (* dx dx) (* dy dy)))))
1098 (setf dx (/ dx module)
1099 dy (/ dy module) )))
1100 (setf xdx ($float (+ xo dx))
1101 ydy ($float (+ yo dy)))
1102 ;; apply geometric transformation before plotting
1103 (setf x (list xo xdx)
1104 y (list yo ydy))
1105 (transform-lists 2)
1106 (update-ranges-2d (apply 'min x) (apply 'max x) (apply 'min y) (apply 'max y))
1107 (make-gr-object
1108 :name 'vector
1109 :command (format nil " ~a w vect ~a size ~a, ~a ~a lw ~a lt ~a lc ~a axis ~a"
1110 (make-obj-title (get-option '$key))
1111 (if (get-option '$head_both) "heads" "head")
1112 (get-option '$head_length)
1113 (get-option '$head_angle)
1114 (case (get-option '$head_type)
1115 ($filled "filled")
1116 ($empty "empty")
1117 ($nofilled "nofilled"))
1118 (get-option '$line_width)
1119 (get-option '$line_type)
1120 (hex-to-rgb (get-option '$color))
1121 (axes-to-plot) )
1122 :groups '((4 0))
1123 :points `(,(make-array 4 :element-type 'flonum
1124 :initial-contents (list (car x)
1125 (car y)
1126 (- (cadr x) (car x))
1127 (- (cadr y) (car y))))) ) ))
1135 ;; Object: 'vector3d'
1136 ;; Usage:
1137 ;; vector([x,y,z], [dx,dy,dz]), represents vector from [x,y,z] to [x+dx,y+dy,z+dz]
1138 ;; Options:
1139 ;; head_both
1140 ;; head_length
1141 ;; head_angle
1142 ;; head_type
1143 ;; line_width
1144 ;; line_type
1145 ;; key
1146 ;; color
1147 ;; unit_vectors
1148 (defun-checked vect3d (arg1 arg2)
1149 (if (or (not ($listp arg1))
1150 (not (= ($length arg1) 3))
1151 (not ($listp arg2))
1152 (not (= ($length arg2) 3)))
1153 (merror "draw (vector): coordinates are not correct"))
1154 (let ((xo ($float (cadr arg1)))
1155 (yo ($float (caddr arg1)))
1156 (zo ($float (cadddr arg1)))
1157 (dx ($float (cadr arg2)))
1158 (dy ($float (caddr arg2)))
1159 (dz ($float (cadddr arg2)))
1160 xdx ydy zdz x y z)
1161 (when (and (get-option '$unit_vectors)
1162 (or (/= dx 0) (/= dy 0) (/= dz 0)))
1163 (let ((module (sqrt (+ (* dx dx) (* dy dy) (* dz dz)))))
1164 (setf dx (/ dx module)
1165 dy (/ dy module)
1166 dz (/ dz module) )))
1167 (setf xdx ($float (+ xo dx))
1168 ydy ($float (+ yo dy))
1169 zdz ($float (+ zo dz)) )
1170 ;; apply geometric transformation before plotting
1171 (setf x (list xo xdx)
1172 y (list yo ydy)
1173 z (list zo zdz))
1174 (transform-lists 3)
1175 (update-ranges-3d (apply 'min x) (apply 'max x) (apply 'min y) (apply 'max y) (apply 'min z) (apply 'max z))
1176 (make-gr-object
1177 :name 'vector
1178 :command (format nil " ~a w vect ~a size ~a, ~a ~a lw ~a lt ~a lc ~a"
1179 (make-obj-title (get-option '$key))
1180 (if (get-option '$head_both) "heads" "head")
1181 (get-option '$head_length)
1182 (get-option '$head_angle)
1183 (case (get-option '$head_type)
1184 ($filled "filled")
1185 ($empty "empty")
1186 ($nofilled "nofilled"))
1187 (get-option '$line_width)
1188 (get-option '$line_type)
1189 (hex-to-rgb (get-option '$color)) )
1190 :groups '((6 0))
1191 :points `(,(make-array 6 :element-type 'flonum
1192 :initial-contents (list (car x)
1193 (car y)
1194 (car z)
1195 (- (cadr x) (car x))
1196 (- (cadr y) (car y))
1197 (- (cadr z) (car z))))) ) ))
1206 ;; Object: 'explicit'
1207 ;; Usage:
1208 ;; explicit(fcn,var,minval,maxval)
1209 ;; Options:
1210 ;; nticks
1211 ;; adapt_depth
1212 ;; line_width
1213 ;; line_type
1214 ;; color
1215 ;; filled_func
1216 ;; fill_color
1217 ;; key
1218 ;; xaxis_secondary
1219 ;; yaxis_secondary
1220 (defun-checked explicit (fcn var minval maxval)
1221 (let* ((nticks (get-option '$nticks))
1222 (depth (get-option '$adapt_depth))
1223 ($numer t)
1224 (xmin ($float minval))
1225 (xmax ($float maxval))
1226 (x-step (/ (- xmax xmin) ($float nticks) 2))
1227 (ymin most-positive-double-float)
1228 (ymax most-negative-double-float)
1229 (*plot-realpart* *plot-realpart*)
1230 x-samples y-samples yy result pltcmd result-array)
1231 (when (< xmax xmin)
1232 (merror "draw2d (explicit): illegal range"))
1233 (setq *plot-realpart* (get-option '$draw_realpart))
1234 (setq fcn (coerce-float-fun fcn `((mlist) ,var)))
1235 (when (get-option '$logx)
1236 (setf xmin (log xmin))
1237 (setf xmax (log xmax))
1238 (setf x-step (/ (- xmax xmin) ($float nticks) 2)))
1239 (flet ((fun (x)
1240 (let ((y (if (get-option '$logx)
1241 (funcall fcn (exp x))
1242 (funcall fcn x))))
1243 (if (and (get-option '$logy)
1244 (numberp y))
1245 (if (> y 0)
1246 (log y)
1247 (merror "draw2d (explicit): logarithm of negative number"))
1248 y))))
1249 (dotimes (k (1+ (* 2 nticks)))
1250 (let ((x (+ xmin (* k x-step))))
1251 (push x x-samples)
1252 (push (fun x) y-samples)))
1253 (setf x-samples (nreverse x-samples))
1254 (setf y-samples (nreverse y-samples))
1255 ;; For each region, adaptively plot it.
1256 (do ((x-start x-samples (cddr x-start))
1257 (x-mid (cdr x-samples) (cddr x-mid))
1258 (x-end (cddr x-samples) (cddr x-end))
1259 (y-start y-samples (cddr y-start))
1260 (y-mid (cdr y-samples) (cddr y-mid))
1261 (y-end (cddr y-samples) (cddr y-end)))
1262 ((null x-end))
1263 ;; The region is x-start to x-end, with mid-point x-mid.
1264 (let ((sublst (adaptive-plot #'fun (car x-start) (car x-mid) (car x-end)
1265 (car y-start) (car y-mid) (car y-end)
1266 depth 1e-5)))
1267 (when (notevery #'(lambda (x) (or (numberp x) (eq x t) (eq x nil))) sublst)
1268 (let ((items sublst) (item 'nil))
1269 ;; Search for the item in sublist that is the undefined variable
1270 (while items
1271 (when (not (or (numberp (car items))
1272 (eq (car items) t)
1273 (eq (car items) nil)))
1274 (setq item (car items)) )
1275 (setq items (cdr items)) )
1276 (merror "draw2d (explicit): non defined variable in term: ~M" item) ) )
1278 (when (not (null result))
1279 (setf sublst (cddr sublst)))
1280 (do ((lst sublst (cddr lst)))
1281 ((null lst) 'done)
1282 (setf result (cons (if (and (get-option '$logy) (numberp (second lst)))
1283 (exp (second lst))
1284 (second lst))
1285 result))
1286 (setf result (cons (if (and (get-option '$logx) (numberp (first lst)))
1287 (exp (first lst))
1288 (first lst))
1289 result)) ) )))
1291 ; reset x extremes to original values
1292 (when (get-option '$logx)
1293 (setf xmin (exp xmin))
1294 (setf xmax (exp xmax)))
1296 (cond ((null (get-option '$filled_func))
1297 (cond
1298 ((> *draw-transform-dimensions* 0)
1299 ; With geometric transformation.
1300 ; When option filled_func in not nil,
1301 ; geometric transformation is ignored
1302 (setf result-array (make-array (length result)))
1303 (setf xmin most-positive-double-float
1304 xmax most-negative-double-float)
1305 (let (xold yold x y (count -1))
1306 (do ((lis result (cddr lis)))
1307 ((null lis))
1308 (setf xold (first lis)
1309 yold (second lis))
1310 (setf x (funcall *draw-transform-f1* xold yold)
1311 y (funcall *draw-transform-f2* xold yold))
1312 (if (> x xmax) (setf xmax x))
1313 (if (< x xmin) (setf xmin x))
1314 (if (> y ymax) (setf ymax y))
1315 (if (< y ymin) (setf ymin y))
1316 (setf (aref result-array (incf count)) x)
1317 (setf (aref result-array (incf count)) y) ) ) )
1319 ; No geometric transformation invoked.
1320 (do ((y (cdr result) (cddr y)))
1321 ((null y))
1322 (setf yy (car y))
1323 (check-extremes-y))
1324 (setf result-array (make-array (length result)
1325 :initial-contents result))))
1326 (update-ranges-2d xmin xmax ymin ymax)
1327 (setf pltcmd (format nil " ~a w l lw ~a lt ~a lc ~a axis ~a"
1328 (make-obj-title (get-option '$key))
1329 (get-option '$line_width)
1330 (get-option '$line_type)
1331 (hex-to-rgb (get-option '$color))
1332 (axes-to-plot)))
1333 (make-gr-object
1334 :name 'explicit
1335 :command pltcmd
1336 :groups '((2 0)) ; numbers are sent to gnuplot in groups of 2
1337 :points (list result-array )) )
1338 ((equal (get-option '$filled_func) t)
1339 (do ((y (cdr result) (cddr y)))
1340 ((null y))
1341 (setf yy (car y))
1342 (check-extremes-y))
1343 (update-ranges-2d xmin xmax ymin ymax)
1344 (setf result-array (make-array (length result)
1345 :element-type 'flonum
1346 :initial-contents result))
1347 (setf pltcmd (format nil " ~a w filledcurves x1 lc ~a axis ~a"
1348 (make-obj-title (get-option '$key))
1349 (hex-to-rgb (get-option '$fill_color))
1350 (axes-to-plot)))
1351 (make-gr-object
1352 :name 'explicit
1353 :command pltcmd
1354 :groups '((2 0)) ; numbers are sent to gnuplot in groups of 2
1355 :points (list result-array )))
1357 (let (fcn2 yy2 (count -1))
1358 (setf result-array (make-array (* (/ (length result) 2) 3)
1359 :element-type 'flonum))
1360 (setq fcn2 (coerce-float-fun (get-option '$filled_func) `((mlist), var)))
1361 (flet ((fun (x) (funcall fcn2 x)))
1362 (do ((xx result (cddr xx)))
1363 ((null xx))
1364 (setf yy (second xx)
1365 yy2 (fun (first xx)))
1366 (setf ymax (max ymax yy yy2)
1367 ymin (min ymin yy yy2))
1368 (setf (aref result-array (incf count)) (first xx)
1369 (aref result-array (incf count)) yy
1370 (aref result-array (incf count)) yy2) ) ))
1371 (update-ranges-2d xmin xmax ymin ymax)
1372 (setf pltcmd (format nil " ~a w filledcurves lc ~a axis ~a"
1373 (make-obj-title (get-option '$key))
1374 (hex-to-rgb (get-option '$fill_color))
1375 (axes-to-plot) ))
1376 (make-gr-object
1377 :name 'explicit
1378 :command pltcmd
1379 :groups '((3 0)) ; numbers are sent to gnuplot in groups of 3
1380 :points (list result-array)))) ))
1388 ;; Object: 'region'
1389 ;; Usage:
1390 ;; region(ineq,x-var,x-minval,x-maxval,y-var,y-minval,y-maxval)
1391 ;; Options:
1392 ;; fill_color
1393 ;; key
1394 ;; x_voxel
1395 ;; y_voxel
1396 (defmacro build-polygon (coord)
1397 (let ((len (1- (length coord))))
1398 `(push (make-array ,len :element-type 'flonum :initial-contents ,coord) pts)))
1400 (defun-checked region (ineq x-var x-minval x-maxval y-var y-minval y-maxval)
1401 (let* ((nx (get-option '$x_voxel))
1402 (ny (get-option '$y_voxel))
1403 (xmin ($float x-minval))
1404 (xmax ($float x-maxval))
1405 (ymin ($float y-minval))
1406 (ymax ($float y-maxval))
1407 (dx (/ (- xmax xmin) nx))
1408 (dy (/ (- ymax ymin) ny))
1409 (err (* 0.02 (min dx dy)))
1410 (xarr (make-array (list (1+ nx) (1+ ny)) :element-type 'flonum))
1411 (yarr (make-array (list (1+ nx) (1+ ny)) :element-type 'flonum))
1412 (barr (make-array (list (1+ nx) (1+ ny)) :initial-element nil :element-type 'boolean))
1413 (pts '())
1414 pltcmd grouping x y)
1416 (when (not (subsetp (rest ($listofvars ineq)) (list x-var y-var) :test #'like))
1417 (merror "draw2d (region): non defined variable"))
1419 ; build 2d arrays: x, y and boolean
1420 (labels ((fun (xx yy) ; evaluates boolean expression
1421 ($is-boole-eval
1422 (simplify
1423 ($substitute
1424 (list '(mlist)
1425 (list '(mequal) x-var xx)
1426 (list '(mequal) y-var yy))
1427 ineq))))
1428 (bipart (xx1 yy1 xx2 yy2) ; bipartition, (xx1, yy1) => T, (xx2, yy2) => NIL
1429 (let ((xm (* 0.5 (+ xx1 xx2)))
1430 (ym (* 0.5 (+ yy1 yy2))))
1431 (cond
1432 ((< (+ (* (- xx2 xx1) (- xx2 xx1))
1433 (* (- yy2 yy1) (- yy2 yy1)))
1434 (* err err))
1435 (list xm ym))
1436 ((fun xm ym)
1437 (bipart xm ym xx2 yy2))
1439 (bipart xx1 yy1 xm ym)) )) ))
1440 ; fill arrays
1441 (loop for i to nx do
1442 (loop for j to ny do
1443 (setf x (+ xmin (* i dx)))
1444 (setf y (+ ymin (* j dy)))
1445 (setf (aref xarr i j) x)
1446 (setf (aref yarr i j) y)
1447 (setf (aref barr i j) (fun x y))))
1448 ; check vertices of rectangles and cuts
1449 (loop for i below nx do
1450 (loop for j below ny do
1451 (let ((x1 (aref xarr i j)) ; SW point
1452 (y1 (aref yarr i j))
1453 (b1 (aref barr i j))
1454 (x2 (aref xarr (1+ i) j)) ; SE point
1455 (y2 (aref yarr (1+ i) j))
1456 (b2 (aref barr (1+ i) j))
1457 (x3 (aref xarr (1+ i) (1+ j))) ; NE point
1458 (y3 (aref yarr (1+ i) (1+ j)))
1459 (b3 (aref barr (1+ i) (1+ j)))
1460 (x4 (aref xarr i (1+ j))) ; NW point
1461 (y4 (aref yarr i (1+ j)))
1462 (b4 (aref barr i (1+ j)))
1463 pa pb pc) ; pa and pb are frontier points
1465 (cond ((and b1 b2 b3 b4)
1466 (build-polygon (list x1 y1 x2 y2 x3 y3 x4 y4)))
1467 ((and b1 b2 b3)
1468 (setf pa (bipart x3 y3 x4 y4)
1469 pb (bipart x1 y1 x4 y4))
1470 (build-polygon (list x1 y1 x2 y2 x3 y3 (first pa) (second pa) (first pb) (second pb))))
1471 ((and b4 b1 b2)
1472 (setf pa (bipart x2 y2 x3 y3)
1473 pb (bipart x4 y4 x3 y3))
1474 (build-polygon (list x4 y4 x1 y1 x2 y2 (first pa) (second pa) (first pb) (second pb))))
1475 ((and b3 b4 b1)
1476 (setf pa (bipart x1 y1 x2 y2)
1477 pb (bipart x3 y3 x2 y2))
1478 (build-polygon (list x3 y3 x4 y4 x1 y1 (first pa) (second pa) (first pb) (second pb))))
1479 ((and b2 b3 b4)
1480 (setf pa (bipart x4 y4 x1 y1)
1481 pb (bipart x2 y2 x1 y1))
1482 (build-polygon (list x2 y2 x3 y3 x4 y4 (first pa) (second pa) (first pb) (second pb))))
1483 ((and b2 b3)
1484 (setf pa (bipart x3 y3 x4 y4)
1485 pb (bipart x2 y2 x1 y1))
1486 (build-polygon (list x2 y2 x3 y3 (first pa) (second pa) (first pb) (second pb))))
1487 ((and b4 b1)
1488 (setf pa (bipart x1 y1 x2 y2)
1489 pb (bipart x4 y4 x3 y3))
1490 (build-polygon (list x4 y4 x1 y1 (first pa) (second pa) (first pb) (second pb))))
1491 ((and b3 b4)
1492 (setf pa (bipart x4 y4 x1 y1)
1493 pb (bipart x3 y3 x2 y2))
1494 (build-polygon (list x3 y3 x4 y4 (first pa) (second pa) (first pb) (second pb))))
1495 ((and b1 b2)
1496 (setf pa (bipart x2 y2 x3 y3)
1497 pb (bipart x1 y1 x4 y4))
1498 (build-polygon (list x1 y1 x2 y2 (first pa) (second pa) (first pb) (second pb))))
1500 (setf pa (bipart x1 y1 x2 y2)
1501 pb (bipart x1 y1 x3 y4)
1502 pc (bipart x1 y1 x4 y4))
1503 (build-polygon (list x1 y1 (first pa) (second pa) (first pb) (second pb)(first pc) (second pc))))
1505 (setf pa (bipart x2 y2 x3 y3)
1506 pb (bipart x2 y2 x4 y4)
1507 pc (bipart x2 y2 x1 y1))
1508 (build-polygon (list x2 y2 (first pa) (second pa) (first pb) (second pb) (first pc) (second pc))))
1510 (setf pa (bipart x3 y3 x4 y4)
1511 pb (bipart x3 y3 x1 y1)
1512 pc (bipart x3 y3 x2 y2))
1513 (build-polygon (list x3 y3 (first pa) (second pa) (first pb) (second pb) (first pc) (second pc))))
1515 (setf pa (bipart x4 y4 x1 y1)
1516 pb (bipart x4 y4 x2 y2)
1517 pc (bipart x4 y4 x3 y3))
1518 (build-polygon (list x4 y4 (first pa) (second pa) (first pb) (second pb) (first pc) (second pc)))) )))))
1520 ; list of commands
1521 (setf pltcmd
1522 (cons (format nil " ~a w filledcurves lc ~a axis ~a"
1523 (make-obj-title (get-option '$key))
1524 (hex-to-rgb (get-option '$fill_color))
1525 (axes-to-plot))
1526 (make-list (- (length pts) 1)
1527 :initial-element (format nil " t '' w filledcurves lc ~a axis ~a"
1528 (hex-to-rgb (get-option '$fill_color))
1529 (axes-to-plot) ))))
1530 (update-ranges-2d xmin xmax ymin ymax)
1531 (setf grouping
1532 (make-list (length pts)
1533 :initial-element '(2 0)))
1534 (make-gr-object
1535 :name 'region
1536 :command pltcmd
1537 :groups grouping
1538 :points pts) ))
1547 ;; Object: 'implicit'
1548 ;; Usage:
1549 ;; implicit(fcn,x-var,x-minval,x-maxval,y-var,y-minval,y-maxval)
1550 ;; Options:
1551 ;; ip_grid
1552 ;; ip_grid_in
1553 ;; line_width
1554 ;; line_type
1555 ;; key
1556 ;; color
1557 ;; xaxis_secondary
1558 ;; yaxis_secondary
1559 ;; Note: taken from implicit_plot.lisp
1561 ;; returns elements at odd positions
1562 (defun x-elements (list)
1563 (if (endp list) list
1564 (list* (first list) (x-elements (rest (rest list))))))
1566 ;; returns elements at even positions
1567 (defun y-elements (list)
1568 (x-elements (rest list)))
1570 (defvar pts ())
1572 (defun contains-zeros (i j sample)
1573 (not (and (> (* (aref sample i j) (aref sample (1+ i) j )) 0)
1574 (> (* (aref sample i j) (aref sample i (1+ j) )) 0)
1575 (> (* (aref sample i j) (aref sample (1+ i) (1+ j) )) 0) )))
1577 (defun sample-data (expr xmin xmax ymin ymax sample grid)
1578 (let* ((xdelta (/ (- xmax xmin) ($first grid)))
1579 (ydelta (/ (- ymax ymin) ($second grid)))
1580 (epsilon #+gcl (float 1/1000000) #-gcl 1e-6))
1581 (do ((x-val xmin (+ x-val xdelta))
1582 (i 0 (1+ i)))
1583 ((> i ($first grid)))
1584 (do ((y-val ymin (+ y-val ydelta))
1585 (j 0 (1+ j)))
1586 ((> j ($second grid)))
1587 (let ((fun-val (funcall expr x-val y-val)))
1588 (when (and (not (floatp fun-val)) (not (eq fun-val t)))
1589 (merror "draw2d (implicit): non defined variable in condition ~M=0" fun-val))
1590 (if (or (eq fun-val t) (>= fun-val epsilon))
1591 (setf (aref sample i j) 1)
1592 (setf (aref sample i j) -1)))))))
1594 (defun draw-print-segment (points xmin xdelta ymin ydelta)
1595 (let* ((point1 (car points)) (point2 (cadr points))
1596 (x1 (coerce (+ xmin (/ (* xdelta (+ (car point1) (caddr point1))) 2)) 'flonum) )
1597 (y1 (coerce (+ ymin (/ (* ydelta (+ (cadr point1) (cadddr point1))) 2)) 'flonum) )
1598 (x2 (coerce (+ xmin (/ (* xdelta (+ (car point2) (caddr point2))) 2)) 'flonum) )
1599 (y2 (coerce (+ ymin (/ (* ydelta (+ (cadr point2) (cadddr point2))) 2)) 'flonum) ))
1600 (setq pts (nconc (list x1 y1 x2 y2) pts))))
1602 (defun draw-print-square (xmin xmax ymin ymax sample grid)
1603 (let* ((xdelta (/ (- xmax xmin) ($first grid)))
1604 (ydelta (/ (- ymax ymin) ($second grid))))
1605 (do ((i 0 (1+ i)))
1606 ((= i ($first grid)))
1607 (do ((j 0 (1+ j)))
1608 ((= j ($second grid)))
1609 (if (contains-zeros i j sample)
1610 (let ((points ()))
1611 (if (< (* (aref sample i j) (aref sample (1+ i) j)) 0)
1612 (setq points (cons `(,i ,j ,(1+ i) ,j) points)))
1613 (if (< (* (aref sample (1+ i) j) (aref sample (1+ i) (1+ j))) 0)
1614 (setq points (cons `(,(1+ i) ,j ,(1+ i) ,(1+ j)) points)))
1615 (if (< (* (aref sample i (1+ j)) (aref sample (1+ i) (1+ j))) 0)
1616 (setq points (cons `(,i ,(1+ j) ,(1+ i) ,(1+ j)) points)))
1617 (if (< (* (aref sample i j) (aref sample i (1+ j))) 0)
1618 (setq points (cons `(,i ,j ,i ,(1+ j)) points)))
1619 (draw-print-segment points xmin xdelta ymin ydelta)) )))))
1621 (defun imp-pl-prepare-factor (expr)
1622 (cond
1623 ((or ($numberp expr) (atom expr))
1624 expr)
1625 ((eq (caar expr) 'mexpt)
1626 (cadr expr))
1628 expr)))
1630 (defun imp-pl-prepare-expr (expr)
1631 (let ((expr1 ($factor (m- ($rhs expr) ($lhs expr)))))
1632 (cond ((or ($numberp expr) (atom expr1)) expr1)
1633 ((eq (caar expr1) 'mtimes)
1634 `((mtimes simp factored 1)
1635 ,@(mapcar #'imp-pl-prepare-factor (cdr expr1))))
1636 ((eq (caar expr) 'mexpt)
1637 (imp-pl-prepare-factor expr1))
1639 expr1))))
1641 (defun-checked implicit (expr x xmin xmax y ymin ymax)
1642 (let* (($numer t) ($plot_options $plot_options)
1643 (pts ())
1644 (expr (m- ($rhs expr) ($lhs expr)))
1645 (ip-grid (get-option '$ip_grid))
1646 (ip-grid-in (get-option '$ip_grid_in))
1647 e pltcmd
1648 (xmin ($float xmin))
1649 (xmax ($float xmax))
1650 (ymin ($float ymin))
1651 (ymax ($float ymax))
1652 (xdelta (/ (- xmax xmin) ($first ip-grid)))
1653 (ydelta (/ (- ymax ymin) ($second ip-grid)))
1654 (sample (make-array `(,(1+ ($first ip-grid))
1655 ,(1+ ($second ip-grid)))))
1656 (ssample (make-array `(,(1+ ($first ip-grid-in))
1657 ,(1+ ($second ip-grid-in))))) )
1659 (setq e (coerce-float-fun (imp-pl-prepare-expr expr)
1660 `((mlist simp)
1661 ,x ,y)))
1662 (update-ranges-2d xmin xmax ymin ymax)
1663 (sample-data e xmin xmax ymin ymax sample ip-grid)
1664 (do ((i 0 (1+ i)))
1665 ((= i ($first ip-grid)))
1666 (do ((j 0 (1+ j)))
1667 ((= j ($second ip-grid)))
1668 (if (contains-zeros i j sample)
1669 (let* ((xxmin (+ xmin (* i xdelta)))
1670 (xxmax (+ xxmin xdelta))
1671 (yymin (+ ymin (* j ydelta)))
1672 (yymax (+ yymin ydelta)))
1673 (sample-data e xxmin xxmax yymin yymax
1674 ssample ip-grid-in)
1675 (draw-print-square xxmin xxmax yymin yymax
1676 ssample ip-grid-in) )) ))
1678 ; geometric transformation
1679 (when (> *draw-transform-dimensions* 0)
1680 (let ((x (x-elements pts))
1681 (y (y-elements pts))
1682 xmin xmax ymin ymax)
1683 (transform-lists 2)
1684 (setf xmin ($tree_reduce 'min (cons '(mlist simp) x))
1685 xmax ($tree_reduce 'max (cons '(mlist simp) x))
1686 ymin ($tree_reduce 'min (cons '(mlist simp) y))
1687 ymax ($tree_reduce 'max (cons '(mlist simp) y)) )
1688 (update-ranges-2d xmin xmax ymin ymax)
1689 (setf pts
1690 (loop
1691 collect (car x)
1692 collect (car y)
1693 do (setf x (cdr x))
1694 (setf y (cdr y))
1695 when (null x) do (loop-finish) ))) )
1697 (setf pltcmd (format nil " ~a w l lw ~a lt ~a lc ~a axis ~a"
1698 (make-obj-title (get-option '$key))
1699 (get-option '$line_width)
1700 (get-option '$line_type)
1701 (hex-to-rgb (get-option '$color))
1702 (axes-to-plot)))
1703 (make-gr-object
1704 :name 'implicit
1705 :command pltcmd
1706 :groups '((2 2))
1707 :points `(,(make-array (length pts) :element-type 'flonum
1708 :initial-contents pts)) ) ))
1715 ;; Object: 'implicit3d'
1716 ;; Usage:
1717 ;; implicit(expr,x,xmin,xmax,y,ymin,ymax,z,zmin,zmax)
1718 ;; Options:
1719 ;; key
1720 ;; x_voxel
1721 ;; y_voxel
1722 ;; z_voxel
1723 ;; line_width
1724 ;; line_type
1725 ;; color
1726 ;; enhanced3d
1727 ;; wired_surface
1728 ;; Some functions and macros are defined in grcommon.lisp
1729 (defun-checked implicit3d (expr par1 xmin xmax par2 ymin ymax par3 zmin zmax)
1730 (let ((xmin ($float xmin))
1731 (xmax ($float xmax))
1732 (ymin ($float ymin))
1733 (ymax ($float ymax))
1734 (zmin ($float zmin))
1735 (zmax ($float zmax))
1736 (pts '())
1737 (grouping '())
1738 pltcmd ncols vertices)
1739 (when (not (subsetp (rest ($listofvars expr)) (list par1 par2 par3) :test #'like))
1740 (merror "draw3d (implicit): non defined variable"))
1741 (check-enhanced3d-model "implicit" '(0 3 99))
1742 (when (= *draw-enhanced3d-type* 99)
1743 (update-enhanced3d-expression (list '(mlist) par1 par2 par3)))
1744 (setf ncols (if (= *draw-enhanced3d-type* 0) 3 4))
1746 (setf vertices (find-triangles expr par1 xmin xmax par2 ymin ymax par3 zmin zmax))
1747 (when (null vertices)
1748 (merror "draw3d (implicit): no surface within these ranges"))
1749 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
1750 (setf pltcmd
1751 (cons (format nil " ~a w ~a lw ~a lt ~a lc ~a"
1752 (make-obj-title (get-option '$key))
1753 (if (equal (get-option '$enhanced3d) '$none) "l" "pm3d")
1754 (get-option '$line_width)
1755 (get-option '$line_type)
1756 (hex-to-rgb (get-option '$color)))
1757 (make-list (- (/ (length vertices) 3) 1)
1758 :initial-element (format nil " t '' w ~a lw ~a lt ~a lc ~a"
1759 (if (equal (get-option '$enhanced3d) '$none) "l" "pm3d")
1760 (get-option '$line_width)
1761 (get-option '$line_type)
1762 (hex-to-rgb (get-option '$color)) ))))
1763 (do ((v vertices (cdddr v)))
1764 ((null v) 'done)
1765 (case ncols
1766 (3 (push (make-array 12 :element-type 'flonum
1767 :initial-contents (flatten (list (first v) (second v) (first v) (third v))))
1768 pts))
1769 (4 (let (v1 v2 v3
1770 color1 color2 color3)
1771 (setf v1 (first v)
1772 v2 (second v)
1773 v3 (third v))
1774 (setf color1 (funcall *draw-enhanced3d-fun* (car v1) (cadr v1) (caddr v1))
1775 color2 (funcall *draw-enhanced3d-fun* (car v2) (cadr v2) (caddr v2))
1776 color3 (funcall *draw-enhanced3d-fun* (car v3) (cadr v3) (caddr v3)) )
1777 (push (make-array 16 :element-type 'flonum
1778 :initial-contents (flatten (list v1 color1 v2 color2 v1 color1 v3 color3)))
1779 pts))) )
1780 (push `(,ncols 2)
1781 grouping) )
1782 (make-gr-object
1783 :name 'implicit
1784 :command pltcmd
1785 :groups grouping
1786 :points pts)))
1794 ;; Object: 'explicit3d'
1795 ;; Usage:
1796 ;; explicit(fcn,par1,minval1,maxval1,par2,minval2,maxval2)
1797 ;; Options:
1798 ;; xu_grid
1799 ;; yv_grid
1800 ;; line_type
1801 ;; line_width
1802 ;; color
1803 ;; key
1804 ;; enhanced3d
1805 ;; wired_surface
1806 ;; surface_hide
1807 ;; transform
1808 (defun-checked explicit3d (fcn par1 minval1 maxval1 par2 minval2 maxval2)
1809 (let* ((xu_grid (get-option '$xu_grid))
1810 (yv_grid (get-option '$yv_grid))
1811 (fminval1 ($float minval1))
1812 (fminval2 ($float minval2))
1813 (fmaxval1 ($float maxval1))
1814 (fmaxval2 ($float maxval2))
1815 (epsx (/ (- fmaxval1 fminval1) xu_grid))
1816 (epsy (/ (- fmaxval2 fminval2) yv_grid))
1817 (xx 0.0) (uu 0.0)
1818 (yy 0.0) (vv 0.0)
1819 (zz 0.0)
1820 (xmin most-positive-double-float)
1821 (xmax most-negative-double-float)
1822 (ymin most-positive-double-float)
1823 (ymax most-negative-double-float)
1824 (zmin most-positive-double-float)
1825 (zmax most-negative-double-float)
1826 (*plot-realpart* *plot-realpart*)
1827 (nx (+ xu_grid 1))
1828 (ny (+ yv_grid 1))
1829 ($numer t)
1830 (count -1)
1831 ncols result)
1832 (when (not (subsetp (rest ($listofvars fcn)) (list par1 par2) :test #'like))
1833 (let ((items (rest ($listofvars fcn))) (item 'nil))
1834 ;; Search for the item in sublist that is the undefined variable
1835 (while items
1839 (subsetp (list (car items)) (list par1 par2) :test #'like)
1841 (setq item (car items))
1843 (setq items (cdr items))
1845 (merror "draw3d (explicit): non defined variable in term ~M" item)
1848 (setq *plot-realpart* (get-option '$draw_realpart))
1849 (check-enhanced3d-model "explicit" '(0 2 3 99))
1850 (when (= *draw-enhanced3d-type* 99)
1851 (update-enhanced3d-expression (list '(mlist) par1 par2)))
1852 (setq fcn (coerce-float-fun fcn `((mlist) ,par1 ,par2)))
1853 (setf ncols (if (= *draw-enhanced3d-type* 0) 3 4))
1854 (setf result (make-array (* ncols nx ny)))
1855 (loop for j below ny
1856 initially (setf vv fminval2)
1857 do (setf uu fminval1)
1858 (loop for i below nx
1860 (setf xx uu
1861 yy vv)
1862 (setf zz (funcall fcn xx yy))
1863 (transform-point 3)
1864 (when (> *draw-transform-dimensions* 0)
1865 (check-extremes-x)
1866 (check-extremes-y))
1867 (check-extremes-z)
1868 (setf (aref result (incf count)) xx
1869 (aref result (incf count)) yy
1870 (aref result (incf count)) zz)
1871 ; check texture model
1872 (case *draw-enhanced3d-type*
1873 ((2 99) (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy)))
1874 (3 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy zz))) )
1875 (setq uu (+ uu epsx)))
1876 (setq vv (+ vv epsy)))
1877 (when (> *draw-transform-dimensions* 0)
1878 (setf fminval1 xmin
1879 fmaxval1 xmax
1880 fminval2 ymin
1881 fmaxval2 ymax))
1882 (update-ranges-3d fminval1 fmaxval1 fminval2 fmaxval2 zmin zmax)
1883 (make-gr-object
1884 :name 'explicit
1885 :command (format nil " ~a w ~a lw ~a lt ~a lc ~a"
1886 (make-obj-title (get-option '$key))
1887 (if (> *draw-enhanced3d-type* 0) "pm3d" "l")
1888 (get-option '$line_width)
1889 (get-option '$line_type)
1890 (hex-to-rgb (get-option '$color)))
1891 :groups `((,ncols ,nx))
1892 :points (list result))))
1901 ;; Object: 'elevation_grid'
1902 ;; Usage:
1903 ;; elevation_grid(mat,x0,y0,width,height)
1904 ;; Options:
1905 ;; line_type
1906 ;; line_width
1907 ;; color
1908 ;; key
1909 ;; enhanced3d
1910 ;; wired_surface
1911 ;; transform
1912 (defun-checked elevation_grid (mat x0 y0 width height)
1913 (let ( (fx0 ($float x0))
1914 (fy0 ($float y0))
1915 (fwidth ($float width))
1916 (fheight ($float height))
1917 (xmin most-positive-double-float)
1918 (xmax most-negative-double-float)
1919 (ymin most-positive-double-float)
1920 (ymax most-negative-double-float)
1921 (zmin most-positive-double-float)
1922 (zmax most-negative-double-float)
1923 ncols-file result nrows ncols)
1924 (check-enhanced3d-model "elevation_grid" '(0 2 3))
1925 (cond (($matrixp mat)
1926 (let ((xi 0.0)
1927 (yi (+ fy0 fheight))
1928 (xx 0.0)
1929 (yy 0.0)
1930 (zz 0.0)
1931 (count -1)
1932 dx dy)
1933 (setf ncols (length (cdadr mat))
1934 nrows (length (cdr mat)))
1935 (setf dx (/ fwidth (1- ncols))
1936 dy (/ fheight (1- nrows)))
1937 (setf ncols-file (if (= *draw-enhanced3d-type* 0) 3 4))
1938 (setf result (make-array (* ncols nrows ncols-file) :element-type 'flonum))
1939 (loop for row on (cdr mat) by #'cdr do
1940 (setf xi fx0)
1941 (loop for col on (cdar row) by #'cdr do
1942 (setf xx xi
1943 yy yi)
1944 (setf zz ($float (car col)))
1945 (transform-point 3)
1946 (when (> *draw-transform-dimensions* 0)
1947 (check-extremes-x)
1948 (check-extremes-y))
1949 (check-extremes-z)
1950 (setf (aref result (incf count)) xx
1951 (aref result (incf count)) yy
1952 (aref result (incf count)) zz)
1953 ; check texture model
1954 (case *draw-enhanced3d-type*
1955 (2 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy)))
1956 (3 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy zz))) )
1957 (setf xi (+ xi dx)))
1958 (setf yi (- yi dy)))))
1960 (merror "draw3d (elevation_grid): Argument not recognized")))
1961 (when (= *draw-transform-dimensions* 0)
1962 (setf xmin fx0
1963 xmax (+ fx0 fwidth)
1964 ymin fy0
1965 ymax (+ fy0 fheight)))
1966 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
1967 (make-gr-object
1968 :name 'elevation_grid
1969 :command (format nil " ~a w ~a lw ~a lt ~a lc ~a"
1970 (make-obj-title (get-option '$key))
1971 (if (> *draw-enhanced3d-type* 0) "pm3d" "l")
1972 (get-option '$line_width)
1973 (get-option '$line_type)
1974 (hex-to-rgb (get-option '$color)))
1975 :groups `((,ncols-file ,ncols))
1976 :points (list result)) ))
1985 ;; Object: 'mesh'
1986 ;; Usage:
1987 ;; mesh([[x_11,y_11,z_11], ...,[x_1n,y_1n,z_1n]],
1988 ;; [[x_21,y_21,z_21], ...,[x_2n,y_2n,z_2n]],
1989 ;; ...,
1990 ;; [[x_m1,y_m1,z_m1], ...,[x_mn,y_mn,z_mn]])
1991 ;; Options:
1992 ;; line_type
1993 ;; line_width
1994 ;; color
1995 ;; key
1996 ;; enhanced3d
1997 ;; wired_surface
1998 ;; transform
1999 (defun-checked mesh (&rest row)
2000 (let (result xx yy zz
2001 (xmin most-positive-double-float)
2002 (xmax most-negative-double-float)
2003 (ymin most-positive-double-float)
2004 (ymax most-negative-double-float)
2005 (zmin most-positive-double-float)
2006 (zmax most-negative-double-float)
2007 m n ncols-file col-num row-num
2008 (count -1))
2009 (cond
2010 ; let's see if the user wants to use mesh in the old way,
2011 ; what we now call elevation_grid
2012 ((and (= (length row) 5)
2013 ($matrixp (first row)))
2014 (print "Warning: Seems like you want to draw an elevation_grid object...")
2015 (print " Please, see documentation for object elevation_grid.")
2016 (apply #'elevation_grid row))
2018 (check-enhanced3d-model "mesh" '(0 2 3))
2019 (when (or (< (length row) 2)
2020 (not (every #'$listp row)))
2021 (merror "draw3d (mesh): Arguments must be two or more lists"))
2022 (setf ncols-file (if (= *draw-enhanced3d-type* 0) 3 4))
2023 (setf m (length row)
2024 n ($length (first row)))
2025 (setf result (make-array (* m n ncols-file) :element-type 'flonum))
2026 (setf row-num 0)
2027 (dolist (r row)
2028 (incf row-num)
2029 (setf col-num 0)
2030 (dolist (c (rest r))
2031 (incf col-num)
2032 (setf xx ($float ($first c))
2033 yy ($float ($second c))
2034 zz ($float ($third c)))
2035 (transform-point 3)
2036 (check-extremes-x)
2037 (check-extremes-y)
2038 (check-extremes-z)
2039 (setf (aref result (incf count)) xx
2040 (aref result (incf count)) yy
2041 (aref result (incf count)) zz)
2042 ; check texture model
2043 (case *draw-enhanced3d-type*
2044 (2 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* row-num col-num)))
2045 (3 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy zz)))) ) )
2046 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
2047 (make-gr-object
2048 :name 'mesh
2049 :command (format nil " ~a w ~a lw ~a lt ~a lc ~a"
2050 (make-obj-title (get-option '$key))
2051 (if (> *draw-enhanced3d-type* 0) "pm3d" "l")
2052 (get-option '$line_width)
2053 (get-option '$line_type)
2054 (hex-to-rgb (get-option '$color)))
2055 :groups `((,ncols-file ,n))
2056 :points (list result))))))
2064 ;; Object: 'triangle3d'
2065 ;; Usage:
2066 ;; triangle([x1,y1,z1], [x2,y2,z2], [x3,y3,z3])
2067 ;; Options:
2068 ;; line_type
2069 ;; line_width
2070 ;; color
2071 ;; key
2072 ;; enhanced3d
2073 ;; transform
2074 (defun-checked triangle3d (arg1 arg2 arg3)
2075 (if (or (not ($listp arg1))
2076 (not (= ($length arg1) 3))
2077 (not ($listp arg2))
2078 (not (= ($length arg2) 3))
2079 (not ($listp arg3))
2080 (not (= ($length arg3) 3)))
2081 (merror "draw3d (triangle): vertices are not correct"))
2082 (let* ((x1 ($float (cadr arg1)))
2083 (y1 ($float (caddr arg1)))
2084 (z1 ($float (cadddr arg1)))
2085 (x2 ($float (cadr arg2)))
2086 (y2 ($float (caddr arg2)))
2087 (z2 ($float (cadddr arg2)))
2088 (x3 ($float (cadr arg3)))
2089 (y3 ($float (caddr arg3)))
2090 (z3 ($float (cadddr arg3)))
2091 (grobj (mesh `((mlist simp)
2092 ((mlist simp) ,x1 ,y1 ,z1)
2093 ((mlist simp) ,x1 ,y1 ,z1) )
2095 `((mlist simp)
2096 ((mlist simp) ,x2 ,y2 ,z2)
2097 ((mlist simp) ,x3 ,y3 ,z3) ) )))
2098 (setf (gr-object-name grobj) 'triangle)
2099 grobj))
2107 ;; Object: 'quadrilateral3d'
2108 ;; Usage:
2109 ;; quadrilateral([x1,y1,z1], [x2,y2,z2], [x3,y3,z3], [x4,y4,z4])
2110 ;; Options:
2111 ;; line_type
2112 ;; line_width
2113 ;; color
2114 ;; key
2115 ;; enhanced3d
2116 ;; transform
2117 (defun-checked quadrilateral3d (arg1 arg2 arg3 arg4)
2118 (if (or (not ($listp arg1))
2119 (not (= ($length arg1) 3))
2120 (not ($listp arg2))
2121 (not (= ($length arg2) 3))
2122 (not ($listp arg3))
2123 (not (= ($length arg3) 3))
2124 (not ($listp arg4))
2125 (not (= ($length arg4) 3)))
2126 (merror "draw3d (quadrilateral): vertices are not correct"))
2127 (let* ((x1 ($float (cadr arg1)))
2128 (y1 ($float (caddr arg1)))
2129 (z1 ($float (cadddr arg1)))
2130 (x2 ($float (cadr arg2)))
2131 (y2 ($float (caddr arg2)))
2132 (z2 ($float (cadddr arg2)))
2133 (x3 ($float (cadr arg3)))
2134 (y3 ($float (caddr arg3)))
2135 (z3 ($float (cadddr arg3)))
2136 (x4 ($float (cadr arg4)))
2137 (y4 ($float (caddr arg4)))
2138 (z4 ($float (cadddr arg4)))
2139 (grobj (mesh `((mlist simp)
2140 ((mlist simp) ,x1 ,y1 ,z1)
2141 ((mlist simp) ,x2 ,y2 ,z2))
2142 `((mlist simp)
2143 ((mlist simp) ,x3 ,y3 ,z3)
2144 ((mlist simp) ,x4 ,y4 ,z4)))))
2145 (setf (gr-object-name grobj) 'quadrilateral)
2146 grobj))
2154 ;; Object: 'parametric'
2155 ;; Usage:
2156 ;; parametric(xfun,yfun,par,parmin,parmax)
2157 ;; Options:
2158 ;; nticks
2159 ;; line_width
2160 ;; line_type
2161 ;; key
2162 ;; color
2163 ;; xaxis_secondary
2164 ;; yaxis_secondary
2165 ;; transform
2166 ;; Note: similar to draw2d-parametric in plot.lisp
2167 (defun-checked parametric (xfun yfun par parmin parmax)
2168 (let* ((nticks (get-option '$nticks))
2169 ($numer t)
2170 (tmin ($float parmin))
2171 (tmax ($float parmax))
2172 (xmin most-positive-double-float)
2173 (xmax most-negative-double-float)
2174 (ymin most-positive-double-float)
2175 (ymax most-negative-double-float)
2176 (*plot-realpart* *plot-realpart*)
2177 (tt ($float parmin))
2178 (eps (/ (- tmax tmin) (- nticks 1)))
2179 result f1 f2 xx yy)
2180 (when (< tmax tmin)
2181 (merror "draw2d (parametric): illegal range"))
2182 (when (not (subsetp (append (rest ($listofvars xfun)) (rest ($listofvars yfun))) (list par) :test #'like))
2183 (merror "draw2d (parametric): non defined variable"))
2184 (setq *plot-realpart* (get-option '$draw_realpart))
2185 (setq f1 (coerce-float-fun xfun `((mlist), par)))
2186 (setq f2 (coerce-float-fun yfun `((mlist), par)))
2187 (setf result
2188 (loop
2189 do (setf xx ($float (funcall f1 tt)))
2190 (setf yy ($float (funcall f2 tt)))
2191 (transform-point 2)
2192 (check-extremes-x)
2193 (check-extremes-y)
2194 collect xx
2195 collect yy
2196 when (>= tt tmax) do (loop-finish)
2197 do (setq tt (+ tt eps))
2198 (if (>= tt tmax) (setq tt tmax)) ))
2199 ; update x-y ranges if necessary
2200 (update-ranges-2d xmin xmax ymin ymax)
2201 (make-gr-object
2202 :name 'parametric
2203 :command (format nil " ~a w l lw ~a lt ~a lc ~a axis ~a"
2204 (make-obj-title (get-option '$key))
2205 (get-option '$line_width)
2206 (get-option '$line_type)
2207 (hex-to-rgb (get-option '$color))
2208 (axes-to-plot))
2209 :groups '((2 0))
2210 :points `(,(make-array (length result) :initial-contents result))) ) )
2218 ;; Object: 'polar'
2219 ;; Usage:
2220 ;; polar(radius,ang,minang,maxang)
2221 ;; Options:
2222 ;; nticks
2223 ;; line_width
2224 ;; line_type
2225 ;; key
2226 ;; color
2227 ;; xaxis_secondary
2228 ;; yaxis_secondary
2229 ;; This object is constructed as a parametric function
2230 (defun-checked polar (radius ang minang maxang)
2231 (let ((grobj (parametric `((mtimes simp) ,radius ((%cos simp) ,ang))
2232 `((mtimes simp) ,radius ((%sin simp) ,ang))
2233 ang minang maxang)))
2234 (setf (gr-object-name grobj) 'polar)
2235 grobj ))
2243 ;; Object: 'spherical'
2244 ;; Usage:
2245 ;; spherical(radius,az,minazi,maxazi,zen,minzen,maxzen)
2246 ;; Options:
2247 ;; xu_grid
2248 ;; yv_grid
2249 ;; line_type
2250 ;; color
2251 ;; key
2252 ;; enhanced3d
2253 ;; wired_surface
2254 ;; This object is constructed as a parametric surface in 3d.
2255 ;; Functions are defined in format r=r(azimuth,zenith),
2256 ;; where, normally, azimuth is an angle in [0,2*%pi] and zenith in [0,%pi]
2257 (defun-checked spherical (radius azi minazi maxazi zen minzen maxzen)
2258 (let ((grobj (parametric_surface
2259 `((mtimes simp) ,radius ((%sin simp) ,zen) ((%cos simp) ,azi))
2260 `((mtimes simp) ,radius ((%sin simp) ,zen) ((%sin simp) ,azi))
2261 `((mtimes simp) ,radius ((%cos simp) ,zen))
2262 azi minazi maxazi
2263 zen minzen maxzen)))
2264 (setf (gr-object-name grobj) 'spherical)
2265 grobj ))
2274 ;; Object: 'cylindrical'
2275 ;; Usage:
2276 ;; cylindrical(r,z,minz,maxz,azi,minazi,maxazi)
2277 ;; Options:
2278 ;; xu_grid
2279 ;; yv_grid
2280 ;; line_type
2281 ;; color
2282 ;; key
2283 ;; enhanced3d
2284 ;; wired_surface
2285 ;; This object is constructed as a parametric surface in 3d.
2286 ;; Functions are defined in format z=z(radius,azimuth), where,
2287 ;; normally, azimuth is an angle in [0,2*%pi] and r any real
2288 (defun-checked cylindrical (r z minz maxz azi minazi maxazi)
2289 (let ((grobj (parametric_surface
2290 `((mtimes simp) ,r ((%cos simp) ,azi))
2291 `((mtimes simp) ,r ((%sin simp) ,azi))
2293 z minz maxz
2294 azi minazi maxazi)))
2295 (setf (gr-object-name grobj) 'cylindrical)
2296 grobj ))
2305 ;; Object: 'parametric3d'
2306 ;; Usage:
2307 ;; parametric(xfun,yfun,zfun,par1,parmin,parmax)
2308 ;; Options:
2309 ;; nticks
2310 ;; line_width
2311 ;; line_type
2312 ;; color
2313 ;; key
2314 ;; enhanced3d
2315 ;; surface_hide
2316 ;; transform
2317 (defun-checked parametric3d (xfun yfun zfun par1 parmin parmax)
2318 (let* ((nticks (get-option '$nticks))
2319 ($numer t)
2320 (tmin ($float parmin))
2321 (tmax ($float parmax))
2322 (xmin most-positive-double-float)
2323 (xmax most-negative-double-float)
2324 (ymin most-positive-double-float)
2325 (ymax most-negative-double-float)
2326 (zmin most-positive-double-float)
2327 (zmax most-negative-double-float)
2328 (*plot-realpart* *plot-realpart*)
2329 (tt tmin)
2330 (eps (/ (- tmax tmin) (- nticks 1)))
2331 (count -1)
2332 ncols result f1 f2 f3 xx yy zz)
2333 (when (not (subsetp (rest ($append ($listofvars xfun) ($listofvars yfun) ($listofvars zfun))) (list par1) :test #'like))
2334 (merror "draw3d (parametric): non defined variable"))
2335 (setq *plot-realpart* (get-option '$draw_realpart))
2336 (check-enhanced3d-model "parametric" '(0 1 3 99))
2337 (when (= *draw-enhanced3d-type* 99)
2338 (update-enhanced3d-expression (list '(mlist) par1)))
2339 (if (< tmax tmin)
2340 (merror "draw3d (parametric): illegal range"))
2341 (setq f1 (coerce-float-fun xfun `((mlist) ,par1)))
2342 (setq f2 (coerce-float-fun yfun `((mlist) ,par1)))
2343 (setq f3 (coerce-float-fun zfun `((mlist) ,par1)))
2344 (setf ncols (if (= *draw-enhanced3d-type* 0) 3 4))
2345 (setf result (make-array (* ncols nticks)))
2346 (dotimes (k nticks)
2347 (setf xx (funcall f1 tt))
2348 (setf yy (funcall f2 tt))
2349 (setf zz (funcall f3 tt))
2350 (transform-point 3)
2351 (check-extremes-x)
2352 (check-extremes-y)
2353 (check-extremes-z)
2354 (setf (aref result (incf count)) xx)
2355 (setf (aref result (incf count)) yy)
2356 (setf (aref result (incf count)) zz)
2357 ; check texture model
2358 (case *draw-enhanced3d-type*
2359 ((1 99) (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* tt)))
2360 (3 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy zz))))
2361 (setf tt (+ tt eps)) )
2362 ; update x-y ranges if necessary
2363 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
2364 (make-gr-object
2365 :name 'parametric
2366 :command (format nil " ~a w l lw ~a lt ~a lc ~a"
2367 (make-obj-title (get-option '$key))
2368 (get-option '$line_width)
2369 (get-option '$line_type)
2370 (if (> *draw-enhanced3d-type* 0)
2371 "palette"
2372 (hex-to-rgb (get-option '$color))) )
2373 :groups `((,ncols 0))
2374 :points (list result) )) )
2383 ;; Object: 'parametric_surface'
2384 ;; Usage:
2385 ;; parametric_surface(xfun,yfun,zfun,par1,par1min,par1max,par2,par2min,par2max)
2386 ;; Options:
2387 ;; xu_grid
2388 ;; yv_grid
2389 ;; line_type
2390 ;; line_width
2391 ;; color
2392 ;; key
2393 ;; enhanced3d
2394 ;; wired_surface
2395 ;; surface_hide
2396 ;; transform
2397 (defun-checked parametric_surface (xfun yfun zfun par1 par1min par1max par2 par2min par2max)
2398 (let* ((ugrid (get-option '$xu_grid))
2399 (vgrid (get-option '$yv_grid))
2400 ($numer t)
2401 (umin ($float par1min))
2402 (umax ($float par1max))
2403 (vmin ($float par2min))
2404 (vmax ($float par2max))
2405 (xmin most-positive-double-float)
2406 (xmax most-negative-double-float)
2407 (ymin most-positive-double-float)
2408 (ymax most-negative-double-float)
2409 (zmin most-positive-double-float)
2410 (zmax most-negative-double-float)
2411 (*plot-realpart* *plot-realpart*)
2412 (ueps (/ (- umax umin) (- ugrid 1)))
2413 (veps (/ (- vmax vmin) (- vgrid 1)))
2414 (nu (+ ugrid 1))
2415 (nv (+ vgrid 1))
2416 (count -1)
2417 ncols result f1 f2 f3 xx yy zz uu vv)
2418 (when (not (subsetp (rest ($append ($listofvars xfun) ($listofvars yfun) ($listofvars zfun))) (list par1 par2) :test #'like))
2419 (merror "draw3d (parametric_surface): non defined variable"))
2420 (setq *plot-realpart* (get-option '$draw_realpart))
2421 (check-enhanced3d-model "parametric_surface" '(0 2 3 99))
2422 (when (= *draw-enhanced3d-type* 99)
2423 (update-enhanced3d-expression (list '(mlist) par1 par2)))
2424 (if (or (< umax umin)
2425 (< vmax vmin))
2426 (merror "draw3d (parametric_surface): illegal range"))
2427 (setq f1 (coerce-float-fun xfun `((mlist) ,par1 ,par2)))
2428 (setq f2 (coerce-float-fun yfun `((mlist) ,par1 ,par2)))
2429 (setq f3 (coerce-float-fun zfun `((mlist) ,par1 ,par2)))
2430 (setf ncols (if (= *draw-enhanced3d-type* 0) 3 4))
2431 (setf result (make-array (* ncols nu nv)))
2432 (loop for j below nv
2433 initially (setq vv vmin)
2434 do (setq uu umin)
2435 (loop for i below nu
2437 (setf xx (funcall f1 uu vv))
2438 (setf yy (funcall f2 uu vv))
2439 (setf zz (funcall f3 uu vv))
2440 (transform-point 3)
2441 (check-extremes-x)
2442 (check-extremes-y)
2443 (check-extremes-z)
2444 (setf (aref result (incf count)) xx)
2445 (setf (aref result (incf count)) yy)
2446 (setf (aref result (incf count)) zz)
2447 ; check texture model
2448 (case *draw-enhanced3d-type*
2449 ((2 99) (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* uu vv)))
2450 (3 (setf (aref result (incf count)) (funcall *draw-enhanced3d-fun* xx yy zz))) )
2451 (setq uu (+ uu ueps))
2452 (if (> uu umax) (setf uu umax)))
2453 (setq vv (+ vv veps))
2454 (if (> vv vmax) (setf vv vmax)))
2455 ; update x-y-z ranges if necessary
2456 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
2457 (make-gr-object
2458 :name 'parametric_surface
2459 :command (format nil " ~a w ~a lw ~a lt ~a lc ~a"
2460 (make-obj-title (get-option '$key))
2461 (if (> *draw-enhanced3d-type* 0) "pm3d" "l")
2462 (get-option '$line_width)
2463 (get-option '$line_type)
2464 (hex-to-rgb (get-option '$color)))
2465 :groups `((,ncols ,nu)) ; ncols is 4 or 3, depending on colored 4th dimension or not
2466 :points (list result))))
2474 ;; Object: 'tube'
2475 ;; Usage:
2476 ;; tube(xfun,yfun,zfun,rad,par1,parmin,parmax)
2477 ;; Options:
2478 ;; xu_grid
2479 ;; yv_grid
2480 ;; line_type
2481 ;; line_width
2482 ;; color
2483 ;; key
2484 ;; enhanced3d
2485 ;; wired_surface
2486 ;; surface_hide
2487 ;; transform
2488 (defmacro check-tube-extreme (ex cx cy cz circ)
2489 `(when (equal (nth ,ex (get-option '$capping)) t)
2490 (let ((cxx ,cx)
2491 (cyy ,cy)
2492 (czz ,cz))
2493 (when (> *draw-transform-dimensions* 0)
2494 (setf cxx (funcall *draw-transform-f1* ,cx ,cy ,cz)
2495 cyy (funcall *draw-transform-f2* ,cx ,cy ,cz)
2496 czz (funcall *draw-transform-f3* ,cx ,cy ,cz)))
2497 (case *draw-enhanced3d-type*
2498 (0 (setf ,circ (list cxx cyy czz)))
2499 ((1 99) (setf ,circ (list cxx cyy czz (funcall *draw-enhanced3d-fun* tt))))
2500 (3 (setf ,circ (list cxx cyy czz (funcall *draw-enhanced3d-fun* cxx cyy czz)))))
2501 (dotimes (k vgrid)
2502 (setf result (append result ,circ))))))
2504 (defun-checked tube (xfun yfun zfun rad par1 parmin parmax)
2505 (let* ((ugrid (get-option '$xu_grid))
2506 (vgrid (get-option '$yv_grid))
2507 ($numer t)
2508 (tmin ($float parmin))
2509 (tmax ($float parmax))
2510 (vmax 6.283185307179586) ; = float(2*%pi)
2511 (xmin most-positive-double-float)
2512 (xmax most-negative-double-float)
2513 (ymin most-positive-double-float)
2514 (ymax most-negative-double-float)
2515 (zmin most-positive-double-float)
2516 (zmax most-negative-double-float)
2517 (teps (/ (- tmax tmin) (- ugrid 1)))
2518 (veps (/ vmax (- vgrid 1)))
2519 (tt tmin)
2520 ncols circ result
2521 f1 f2 f3 radius
2522 cx cy cz nx ny nz
2523 ux uy uz vx vy vz
2524 xx yy zz module r vv rcos rsin
2525 cxold cyold czold
2526 uxold uyold uzold ttnext)
2527 (when (< tmax tmin)
2528 (merror "draw3d (tube): illegal range"))
2529 (when (not (subsetp (rest ($append ($listofvars xfun) ($listofvars yfun)
2530 ($listofvars zfun) ($listofvars rad)))
2531 (list par1) :test #'like))
2532 (merror "draw3d (tube): non defined variable"))
2533 (check-enhanced3d-model "tube" '(0 1 3 99))
2534 (when (= *draw-enhanced3d-type* 99)
2535 (update-enhanced3d-expression (list '(mlist) par1)))
2536 (setq f1 (coerce-float-fun xfun `((mlist) ,par1)))
2537 (setq f2 (coerce-float-fun yfun `((mlist) ,par1)))
2538 (setq f3 (coerce-float-fun zfun `((mlist) ,par1)))
2539 (setf ncols (if (= *draw-enhanced3d-type* 0) 3 4))
2540 (setf radius
2541 (coerce-float-fun rad `((mlist) ,par1)))
2542 (loop do
2543 ; calculate center and radius of circle
2544 (cond
2545 ((= tt tmin) ; 1st iteration
2546 (setf cx (funcall f1 tt)
2547 cy (funcall f2 tt)
2548 cz (funcall f3 tt)
2549 ttnext (+ tt teps))
2550 (check-tube-extreme 1 cx cy cz circ)
2551 (setf nx (- (funcall f1 ttnext) cx)
2552 ny (- (funcall f2 ttnext) cy)
2553 nz (- (funcall f3 ttnext) cz)))
2554 (t ; all next iterations along the parametric curve
2555 (setf cxold cx
2556 cyold cy
2557 czold cz)
2558 (setf cx (funcall f1 tt)
2559 cy (funcall f2 tt)
2560 cz (funcall f3 tt))
2561 (setf nx (- cx cxold)
2562 ny (- cy cyold)
2563 nz (- cz czold))))
2564 (setf r (funcall radius tt))
2565 ; calculate the unitary normal vector
2566 (setf module (sqrt (+ (* nx nx) (* ny ny) (* nz nz))))
2567 (setf nx (/ nx module)
2568 ny (/ ny module)
2569 nz (/ nz module))
2570 ; calculate unitary vector perpendicular to n=(nx,ny,nz)
2571 ; ux.nx+uy.ny+uz.nz=0 => ux=-t(ny+nz)/nx, uy=uz=t
2572 ; let's take t=1
2573 (cond
2574 ((= nx 0.0)
2575 (setf ux 1.0 uy 0.0 uz 0.0))
2576 ((= ny 0.0)
2577 (setf ux 0.0 uy 1.0 uz 0.0))
2578 ((= nz 0.0)
2579 (setf ux 0.0 uy 0.0 uz 1.0))
2580 (t ; all other cases
2581 (setf ux (- (/ (+ ny nz) nx))
2582 uy 1.0
2583 uz 1.0)))
2584 (setf module (sqrt (+ (* ux ux) (* uy uy) (* uz uz))))
2585 (setf ux (/ ux module)
2586 uy (/ uy module)
2587 uz (/ uz module))
2588 (when (and (> tt tmin)
2589 (< (+ (* uxold ux)
2590 (* uyold uy)
2591 (* uzold uz))
2593 (setf ux (- ux)
2594 uy (- uy)
2595 uz (- uz)))
2596 (setf uxold ux
2597 uyold uy
2598 uzold uz)
2599 ; vector v = n times u
2600 (setf vx (- (* ny uz) (* nz uy))
2601 vy (- (* nz ux) (* nx uz))
2602 vz (- (* nx uy) (* ny ux)))
2603 ; parametric equation of the circumference of radius
2604 ; r and centered at c=(cx,cy,cz):
2605 ; x(t) = c + r(cos(t)u + sin(t)v),
2606 ; for t in (0, 2*%pi)
2607 (setf vv 0.0)
2608 (setf circ '())
2609 (loop for i below vgrid do
2610 (setf rcos (* r (cos vv))
2611 rsin (* r (sin vv)))
2612 (setf xx (+ cx (* rcos ux) (* rsin vx))
2613 yy (+ cy (* rcos uy) (* rsin vy))
2614 zz (+ cz (* rcos uz) (* rsin vz)))
2615 (transform-point 3)
2616 (check-extremes-x)
2617 (check-extremes-y)
2618 (check-extremes-z)
2619 ; check texture model
2620 (case *draw-enhanced3d-type*
2621 (0 (setf circ (cons (list xx yy zz) circ)))
2622 ((1 99) (setf circ (cons (list xx yy zz (funcall *draw-enhanced3d-fun* tt)) circ)))
2623 (3 (setf circ (cons (list xx yy zz (funcall *draw-enhanced3d-fun* xx yy zz)) circ))))
2624 (setf vv (+ vv veps))
2625 (when (> vv vmax) (setf vv vmax)) ) ; loop for
2626 (setf result (append result (apply #'append circ)))
2627 when (>= tt tmax) do (loop-finish)
2628 do (setf tt (+ tt teps))
2629 (when (> tt tmax) (setf tt tmax)) ) ; loop do
2630 (check-tube-extreme 2 cx cy cz circ)
2631 ; update x-y-z ranges
2632 (update-ranges-3d xmin xmax ymin ymax zmin zmax)
2633 (make-gr-object
2634 :name 'tube
2635 :command (format nil " ~a w ~a lw ~a lt ~a lc ~a"
2636 (make-obj-title (get-option '$key))
2637 (if (> *draw-enhanced3d-type* 0) "pm3d" "l")
2638 (get-option '$line_width)
2639 (get-option '$line_type)
2640 (hex-to-rgb (get-option '$color)))
2641 :groups `((,ncols ,vgrid))
2642 :points `(,(make-array (length result) :element-type 'flonum
2643 :initial-contents result)))))
2651 ;; Object: 'image'
2652 ;; Usages:
2653 ;; image(matrix_of_numbers,x0,y0,width,height)
2654 ;; image(matrix_of_[r,g,b],x0,y0,width,height)
2655 ;; image(picture_object,x0,y0,width,height)
2656 ;; Options:
2657 ;; colorbox
2658 ;; palette
2659 (defun-checked image (mat x0 y0 width height)
2660 (let ( (fx0 ($float x0))
2661 (fy0 ($float y0))
2662 (fwidth ($float width))
2663 (fheight ($float height))
2664 result nrows ncols dx dy n)
2665 (cond (($matrixp mat)
2666 (setf nrows (length (cdr mat))
2667 ncols (length (cdadr mat)))
2668 (setf dx (/ fwidth ncols)
2669 dy (/ fheight nrows))
2670 (if (not ($listp (cadadr mat))) ; it's a matrix of reals
2671 (setf n 3) ; 3 numbers to be sent to gnuplot: x,y,value
2672 (setf n 5)) ; 5 numbers to be sent: x,y,r,g,b
2673 (case n
2674 (3 (setf result (make-array (* 3 nrows ncols) :element-type 'flonum))
2675 (let ((yi (+ fy0 height (* dy -0.5)))
2676 (counter -1)
2678 (loop for row on (cdr mat) by #'cdr do
2679 (setf xi (+ fx0 (* dx 0.5)))
2680 (loop for col on (cdar row) by #'cdr do
2681 (setf (aref result (incf counter)) xi
2682 (aref result (incf counter)) yi
2683 (aref result (incf counter)) ($float (car col)))
2684 (setf xi (+ xi dx)))
2685 (setf yi (- yi dy)) )))
2686 (5 (setf result (make-array (* 5 nrows ncols) :element-type 'flonum))
2687 (let ((yi (+ fy0 height (* dy -0.5)))
2688 (counter -1)
2689 xi colors)
2690 (loop for row on (cdr mat) by #'cdr do
2691 (setf xi (+ fx0 (* dx 0.5)))
2692 (loop for col on (cdar row) by #'cdr do
2693 (setf colors (cdar col))
2694 (setf (aref result (incf counter)) xi
2695 (aref result (incf counter)) yi
2696 (aref result (incf counter)) ($float (car colors))
2697 (aref result (incf counter)) ($float (cadr colors))
2698 (aref result (incf counter)) ($float (caddr colors)))
2699 (setf xi (+ xi dx)))
2700 (setf yi (- yi dy)) )))))
2701 (($picturep mat)
2702 (setf nrows (nth 3 mat) ; picture height
2703 ncols (nth 2 mat)) ; picture width
2704 (setf dx (/ fwidth ncols)
2705 dy (/ fheight nrows))
2706 (if (equal (nth 1 mat) '$level) ; gray level picture
2707 (setf n 3) ; 3 numbers to be sent to gnuplot: x,y,value
2708 (setf n 5)) ; 5 numbers to be sent: x,y,r,g,b
2709 (setf result (make-array (* n nrows ncols) :element-type 'flonum))
2710 (let ((yi (+ fy0 height (* dy -0.5)))
2711 (count1 -1)
2712 (count2 -1)
2714 (loop for r from 0 below nrows do
2715 (setf xi (+ fx0 (* dx 0.5)))
2716 (loop for c from 0 below ncols do
2717 (setf (aref result (incf count1)) xi)
2718 (setf (aref result (incf count1)) yi)
2719 (loop for q from 3 to n do
2720 (setf (aref result (incf count1))
2721 ($float (aref (nth 4 mat) (incf count2)))))
2722 (setf xi (+ xi dx)))
2723 (setf yi (- yi dy)))))
2725 (merror "draw2d (image): Argument not recognized")))
2726 ; update x-y ranges if necessary
2727 (update-ranges-2d fx0 (+ fx0 fwidth) fy0 (+ fy0 fheight))
2728 (make-gr-object
2729 :name 'image
2730 :command (case n
2731 (3 (format nil " t '' w image"))
2732 (5 (format nil " t '' w rgbimage")))
2733 :groups (case n
2734 (3 '((3 0))) ; numbers are sent to gnuplot in gropus of 3, no blank lines
2735 (5 '((5 0)) )) ; numbers in groups of 5, no blank lines
2736 :points (list result)) ) )
2743 (defmacro write-palette-code ()
2744 '(let ((pal (get-option '$palette)))
2745 (cond
2746 ((equal pal '$gray)
2747 (format nil "set palette gray~%"))
2748 ((equal pal '$color)
2749 (format nil "set palette rgbformulae 7,5,15~%"))
2750 ((and (listp pal)
2751 (= (length pal) 3)
2752 (every #'(lambda (x) (and (integerp x) (<= (abs x) 36))) pal) )
2753 (format nil "set palette rgbformulae ~a,~a,~a~%"
2754 (car pal) (cadr pal) (caddr pal)))
2755 ((and (listp pal)
2756 (every #'(lambda (x) (and (listp x) (= (length x) 3))) pal) )
2757 (let (triplete
2758 (n (length pal)))
2759 (with-output-to-string (stream)
2760 (format stream "set palette defined ( \\~%")
2761 (dotimes (k n)
2762 (setf triplete (nth k pal))
2763 (format stream " ~a ~a ~a ~a "
2764 k (car triplete) (cadr triplete) (caddr triplete))
2765 (if (= (1+ k) n)
2766 (format stream ")~%")
2767 (format stream ", \\~%") )))))
2769 (merror "draw: illegal palette description")))))
2773 (defvar *2d-graphic-objects* (make-hash-table))
2775 ; table of basic 2d graphic objects
2776 (setf (gethash '$points *2d-graphic-objects*) 'points
2777 (gethash '$errors *2d-graphic-objects*) 'errors
2778 (gethash '$polygon *2d-graphic-objects*) 'polygon
2779 (gethash '$ellipse *2d-graphic-objects*) 'ellipse
2780 (gethash '$triangle *2d-graphic-objects*) 'triangle
2781 (gethash '$rectangle *2d-graphic-objects*) 'rectangle
2782 (gethash '$quadrilateral *2d-graphic-objects*) 'quadrilateral
2783 (gethash '$region *2d-graphic-objects*) 'region
2784 (gethash '$explicit *2d-graphic-objects*) 'explicit
2785 (gethash '$implicit *2d-graphic-objects*) 'implicit
2786 (gethash '$parametric *2d-graphic-objects*) 'parametric
2787 (gethash '$vector *2d-graphic-objects*) 'vect
2788 (gethash '$label *2d-graphic-objects*) 'label
2789 (gethash '$bars *2d-graphic-objects*) 'bars
2790 (gethash '$polar *2d-graphic-objects*) 'polar
2791 (gethash '$image *2d-graphic-objects*) 'image
2792 (gethash '%points *2d-graphic-objects*) 'points
2793 (gethash '%errors *2d-graphic-objects*) 'errors
2794 (gethash '%polygon *2d-graphic-objects*) 'polygon
2795 (gethash '%ellipse *2d-graphic-objects*) 'ellipse
2796 (gethash '%triangle *2d-graphic-objects*) 'triangle
2797 (gethash '%rectangle *2d-graphic-objects*) 'rectangle
2798 (gethash '%quadrilateral *2d-graphic-objects*) 'quadrilateral
2799 (gethash '%region *2d-graphic-objects*) 'region
2800 (gethash '%explicit *2d-graphic-objects*) 'explicit
2801 (gethash '%implicit *2d-graphic-objects*) 'implicit
2802 (gethash '%parametric *2d-graphic-objects*) 'parametric
2803 (gethash '%vector *2d-graphic-objects*) 'vect
2804 (gethash '%label *2d-graphic-objects*) 'label
2805 (gethash '%bars *2d-graphic-objects*) 'bars
2806 (gethash '%polar *2d-graphic-objects*) 'polar
2807 (gethash '%image *2d-graphic-objects*) 'image )
2809 (defun make-scene-2d (args)
2810 (let ((objects nil)
2811 plotcmd largs aux)
2812 (ini-gr-options)
2813 (ini-local-option-variables)
2814 (user-defaults)
2815 (setf largs (listify-arguments args))
2816 ; update option values and detect objects to be plotted
2817 (dolist (x largs)
2818 (cond ((equal ($op x) "=")
2819 (update-gr-option ($lhs x) ($rhs x)))
2820 ((not (null (gethash (setf aux (caar x)) *2d-graphic-objects*)))
2821 (setf objects
2822 (append
2823 objects
2824 (list (apply (gethash aux *2d-graphic-objects*) (rest x))))))
2825 (t (merror "draw: 2D graphic object not recognized, ~M" aux))))
2826 ; save in plotcmd the gnuplot preamble
2827 (setf plotcmd
2828 (concatenate 'string
2829 (unless (or *multiplot-is-active*
2830 (member (get-option '$terminal) '($eps $epslatex $epslatex_standalone)))
2831 (format nil "set obj 1 fc rgb '~a' fs solid 1.0 noborder ~%"
2832 (get-option '$background_color)) )
2833 (if (equal (get-option '$proportional_axes) '$none)
2834 (format nil "set size noratio~%")
2835 (format nil "set size ratio -1~%") )
2836 ; this let statement is to prevent error messages from gnuplot when
2837 ; the amplitude of the ranges equals zero
2838 (let ((xi (first (get-option '$xrange)))
2839 (xf (second (get-option '$xrange)))
2840 (yi (first (get-option '$yrange)))
2841 (yf (second (get-option '$yrange)))
2842 (x2i (first (get-option '$xrange_secondary)))
2843 (x2f (second (get-option '$xrange_secondary)))
2844 (y2i (first (get-option '$yrange_secondary)))
2845 (y2f (second (get-option '$yrange_secondary))) )
2846 (when (and (get-option '$xrange) (near-equal xi xf))
2847 (setf xi (- xi 0.01)
2848 xf (+ xf 0.01)))
2849 (when (and (get-option '$xrange_secondary) (near-equal x2i x2f))
2850 (setf x2i (- x2i 0.01)
2851 x2f (+ x2f 0.01)))
2852 (when (and (get-option '$yrange) (near-equal yi yf))
2853 (setf yi (- yi 0.01)
2854 yf (+ yf 0.01)))
2855 (when (and (get-option '$yrange_secondary) (near-equal y2i y2f))
2856 (setf y2i (- y2i 0.01)
2857 y2f (+ y2f 0.01)))
2858 (format nil "~a~a~a~a"
2859 (if (get-option '$xrange)
2860 (format nil "set xrange [~a:~a]~%" xi xf)
2862 (if (get-option '$xrange_secondary)
2863 (format nil "set x2range [~a:~a]~%" x2i x2f)
2865 (if (get-option '$yrange)
2866 (format nil "set yrange [~a:~a]~%" yi yf)
2868 (if (get-option '$yrange_secondary)
2869 (format nil "set y2range [~a:~a]~%" y2i y2f)
2870 "") ) )
2871 (if (get-option '$cbrange)
2872 (format nil "set cbrange [~a:~a]~%"
2873 (first (get-option '$cbrange))
2874 (second (get-option '$cbrange)))
2875 (format nil "set cbrange [*:*]~%") )
2876 (if (get-option '$logx)
2877 (format nil "set logscale x~%")
2878 (format nil "unset logscale x~%"))
2879 (if (get-option '$logx_secondary)
2880 (format nil "set logscale x2~%")
2881 (format nil "unset logscale x2~%"))
2882 (if (get-option '$logy)
2883 (format nil "set logscale y~%")
2884 (format nil "unset logscale y~%"))
2885 (if (get-option '$logy_secondary)
2886 (format nil "set logscale y2~%")
2887 (format nil "unset logscale y2~%"))
2888 (if (get-option '$logcb)
2889 (format nil "set logscale cb~%")
2890 (format nil "unset logscale cb~%") )
2892 ( if (equal (car (get-option '$grid)) 0)
2893 (format nil "unset grid~%")
2894 (format nil "set grid xtics ytics mxtics mytics~%set mxtics ~d~%set mytics ~d~%"
2895 (car (get-option '$grid))
2896 (cadr (get-option '$grid))
2900 (format nil "set title '~a'~%" (get-option '$title))
2901 (format nil "set xlabel '~a'~%" (get-option '$xlabel))
2902 (format nil "set x2label '~a'~%" (get-option '$xlabel_secondary))
2903 (format nil "set ylabel '~a'~%" (get-option '$ylabel))
2904 (format nil "set y2label '~a'~%" (get-option '$ylabel_secondary))
2905 (let ((suma 0))
2906 (if (get-option '$axis_bottom) (setf suma (+ suma 1)))
2907 (if (get-option '$axis_left) (setf suma (+ suma 2)))
2908 (if (get-option '$axis_top) (setf suma (+ suma 4)))
2909 (if (get-option '$axis_right) (setf suma (+ suma 8)))
2910 (format nil "set border ~a~%" suma) )
2911 (if (get-option '$key_pos)
2912 (format nil "set key ~a~%" (get-option '$key_pos))
2913 (format nil "set key top right~%") )
2914 (if (get-option '$xaxis)
2915 (format nil "set xzeroaxis lw ~a lt ~a lc ~a~%"
2916 (get-option '$xaxis_width)
2917 (get-option '$xaxis_type)
2918 (hex-to-rgb (get-option '$xaxis_color)) )
2919 (format nil "unset xzeroaxis~%"))
2920 (if (get-option '$yaxis)
2921 (format nil "set yzeroaxis lw ~a lt ~a lc ~a~%"
2922 (get-option '$yaxis_width)
2923 (get-option '$yaxis_type)
2924 (hex-to-rgb (get-option '$yaxis_color)) )
2925 (format nil "unset yzeroaxis~%"))
2926 (if (null (get-option '$xtics_secondary))
2927 (format nil "unset x2tics~%set xtics nomirror~%")
2928 (format nil "set x2tics ~a ~a ~a~%"
2929 (if (get-option '$xtics_secondary_rotate) "rotate" "norotate")
2930 (if (get-option '$xtics_secondary_axis) "axis" "border")
2931 (get-option '$xtics_secondary)))
2932 (if (null (get-option '$xtics))
2933 (format nil "unset xtics~%")
2934 (format nil "set xtics ~a ~a ~a~%"
2935 (if (get-option '$xtics_rotate) "rotate" "norotate")
2936 (if (get-option '$xtics_axis) "axis" "border")
2937 (get-option '$xtics)))
2938 (if (null (get-option '$ytics_secondary))
2939 (format nil "unset y2tics~%set ytics nomirror~%")
2940 (format nil "set ytics nomirror~%set y2tics ~a ~a ~a~%"
2941 (if (get-option '$ytics_secondary_rotate) "rotate" "norotate")
2942 (if (get-option '$ytics_secondary_axis) "axis" "border")
2943 (get-option '$ytics_secondary)))
2944 (if (null (get-option '$ytics))
2945 (format nil "unset ytics~%")
2946 (format nil "set ytics ~a ~a ~a~%"
2947 (if (get-option '$ytics_rotate) "rotate" "norotate")
2948 (if (get-option '$ytics_axis) "axis" "border")
2949 (get-option '$ytics)))
2950 (if (null (get-option '$cbtics))
2951 (format nil "unset cbtics~%")
2952 (format nil "set cbtics ~a~%" (get-option '$cbtics) ))
2953 (if (get-option '$colorbox)
2954 (format nil "set colorbox~%")
2955 (format nil "unset colorbox~%"))
2956 (format nil "set cblabel '~a'~%"
2957 (if (stringp (get-option '$colorbox))
2958 (get-option '$colorbox)
2959 ""))
2960 (write-palette-code)
2961 (if (not (string= (get-option '$user_preamble) ""))
2962 (format nil "~a~%" (get-option '$user_preamble))) ) )
2963 ; scene allocation
2964 (setf *allocations* (cons (get-option '$allocation) *allocations*))
2965 ; scene description: (dimensions, gnuplot preamble in string format, list of objects)
2966 (list
2967 2 ; it's a 2d scene
2968 plotcmd ; gnuplot preamble
2969 objects ; list of objects to be plotted
2977 (defvar *3d-graphic-objects* (make-hash-table))
2979 ; table of basic 3d graphic objects
2980 (setf (gethash '$points *3d-graphic-objects*) 'points3d
2981 (gethash '$elevation_grid *3d-graphic-objects*) 'elevation_grid
2982 (gethash '$mesh *3d-graphic-objects*) 'mesh
2983 (gethash '$triangle *3d-graphic-objects*) 'triangle3d
2984 (gethash '$quadrilateral *3d-graphic-objects*) 'quadrilateral3d
2985 (gethash '$explicit *3d-graphic-objects*) 'explicit3d
2986 (gethash '$implicit *3d-graphic-objects*) 'implicit3d
2987 (gethash '$parametric *3d-graphic-objects*) 'parametric3d
2988 (gethash '$vector *3d-graphic-objects*) 'vect3d
2989 (gethash '$label *3d-graphic-objects*) 'label
2990 (gethash '$parametric_surface *3d-graphic-objects*) 'parametric_surface
2991 (gethash '$tube *3d-graphic-objects*) 'tube
2992 (gethash '$spherical *3d-graphic-objects*) 'spherical
2993 (gethash '$cylindrical *3d-graphic-objects*) 'cylindrical
2994 (gethash '%points *3d-graphic-objects*) 'points3d
2995 (gethash '%elevation_grid *3d-graphic-objects*) 'elevation_grid
2996 (gethash '%mesh *3d-graphic-objects*) 'mesh
2997 (gethash '%triangle *3d-graphic-objects*) 'triangle3d
2998 (gethash '%quadrilateral *3d-graphic-objects*) 'quadrilateral3d
2999 (gethash '%explicit *3d-graphic-objects*) 'explicit3d
3000 (gethash '%implicit *3d-graphic-objects*) 'implicit3d
3001 (gethash '%parametric *3d-graphic-objects*) 'parametric3d
3002 (gethash '%vector *3d-graphic-objects*) 'vect3d
3003 (gethash '%label *3d-graphic-objects*) 'label
3004 (gethash '%parametric_surface *3d-graphic-objects*) 'parametric_surface
3005 (gethash '%tube *3d-graphic-objects*) 'tube
3006 (gethash '%spherical *3d-graphic-objects*) 'spherical
3007 (gethash '%cylindrical *3d-graphic-objects*) 'cylindrical )
3009 ;; This function builds a 3d scene by calling the
3010 ;; graphic objects constructors.
3011 (defun make-scene-3d (args)
3012 (let ((objects nil)
3013 plotcmd largs aux)
3014 (ini-gr-options)
3015 (ini-local-option-variables)
3016 (user-defaults)
3017 (setf largs (listify-arguments args))
3018 ; update option values and detect objects to be plotted
3019 (dolist (x largs)
3020 (cond ((equal ($op x) "=")
3021 (update-gr-option ($lhs x) ($rhs x)))
3022 ((not (null (gethash (setf aux (caar x)) *3d-graphic-objects*)))
3023 (setf objects
3024 (append
3025 objects
3026 (list (apply (gethash aux *3d-graphic-objects*) (rest x))))))
3027 (t (merror "draw: 3D graphic object not recognized, ~M" aux) )))
3028 ; save in plotcmd the gnuplot preamble
3029 (setf plotcmd
3030 (concatenate 'string
3031 (format nil "set style rectangle fillcolor rgb '~a' fs solid 1.0 noborder~%"
3032 (get-option '$background_color)) ; background rectangle
3033 ; this let statement is to prevent error messages in gnuplot when
3034 ; the amplitude of the ranges equals zero
3035 (let ((xi (first (get-option '$xrange)))
3036 (xf (second (get-option '$xrange)))
3037 (yi (first (get-option '$yrange)))
3038 (yf (second (get-option '$yrange)))
3039 (zi (first (get-option '$zrange)))
3040 (zf (second (get-option '$zrange))))
3041 (when (near-equal xi xf)
3042 (setf xi (- xi 0.01)
3043 xf (+ xf 0.01)))
3044 (when (near-equal yi yf)
3045 (setf yi (- yi 0.01)
3046 yf (+ yf 0.01)))
3047 (when (near-equal zi zf)
3048 (setf zi (- zi 0.01)
3049 zf (+ zf 0.01)))
3050 (format nil "set xrange [~a:~a]~%set yrange [~a:~a]~%set zrange [~a:~a]~%"
3051 xi xf yi yf zi zf))
3052 (if (get-option '$cbrange)
3053 (format nil "set cbrange [~a:~a]~%"
3054 (first (get-option '$cbrange))
3055 (second (get-option '$cbrange)) )
3056 (format nil "set cbrange [*:*]~%") )
3057 (case (get-option '$contour)
3058 ($surface (format nil "set contour surface~%set cntrparam levels ~a~%"
3059 (get-option '$contour_levels) ))
3060 ($base (format nil "set contour base~%set cntrparam levels ~a~%"
3061 (get-option '$contour_levels) ))
3062 ($both (format nil "set contour both~%set cntrparam levels ~a~%"
3063 (get-option '$contour_levels) ))
3064 ($map (format nil "set contour base~%unset surface~%set cntrparam levels ~a~%"
3065 (get-option '$contour_levels))) )
3066 (format nil "set title '~a'~%" (get-option '$title))
3067 (format nil "set xlabel '~a'~%" (get-option '$xlabel))
3068 (format nil "set ylabel '~a'~%" (get-option '$ylabel))
3069 (if (< (length (get-option '$zlabel)) 5)
3070 (format nil "set zlabel '~a'~%" (get-option '$zlabel))
3071 (format nil "set zlabel '~a' rotate~%" (get-option '$zlabel)) )
3072 (format nil "set datafile missing 'NIL'~%")
3073 (if (get-option '$logx)
3074 (format nil "set logscale x~%")
3075 (format nil "unset logscale x~%"))
3076 (if (get-option '$logy)
3077 (format nil "set logscale y~%")
3078 (format nil "unset logscale y~%"))
3079 (if (get-option '$logz)
3080 (format nil "set logscale z~%")
3081 (format nil "unset logscale z~%"))
3082 (if (get-option '$key_pos)
3083 (format nil "set key ~a~%" (get-option '$key_pos))
3084 (format nil "set key top center~%") )
3085 (if (get-option '$logcb)
3086 (format nil "set logscale cb~%")
3087 (format nil "unset logscale cb~%") )
3088 (if (equal (car (get-option '$grid)) 0)
3089 (format nil "unset grid~%")
3090 (format nil "set grid xtics ytics ztics mxtics mytics mztics~%set mxtics ~d~%set mytics ~d~%"
3091 (car (get-option '$grid))
3092 (cadr (get-option '$grid))
3095 (if (get-option '$xaxis)
3096 (format nil "set xzeroaxis lw ~a lt ~a lc ~a~%"
3097 (get-option '$xaxis_width)
3098 (get-option '$xaxis_type)
3099 (hex-to-rgb (get-option '$xaxis_color)) )
3100 (format nil "unset xzeroaxis~%"))
3101 (if (get-option '$yaxis)
3102 (format nil "set yzeroaxis lw ~a lt ~a lc ~a~%"
3103 (get-option '$yaxis_width)
3104 (get-option '$yaxis_type)
3105 (hex-to-rgb (get-option '$yaxis_color)) )
3106 (format nil "unset yzeroaxis~%"))
3107 (if (get-option '$zaxis)
3108 (format nil "set zzeroaxis lw ~a lt ~a lc ~a~%"
3109 (get-option '$zaxis_width)
3110 (get-option '$zaxis_type)
3111 (hex-to-rgb (get-option '$zaxis_color)))
3112 (format nil "unset zzeroaxis~%"))
3113 (if (null (get-option '$xtics))
3114 (format nil "unset xtics~%")
3115 (format nil "set xtics ~a ~a ~a~%"
3116 (if (get-option '$xtics_rotate) "rotate" "norotate")
3117 (if (get-option '$xtics_axis) "axis" "border")
3118 (get-option '$xtics)))
3119 (if (null (get-option '$ytics))
3120 (format nil "unset ytics~%")
3121 (format nil "set ytics ~a ~a ~a~%"
3122 (if (get-option '$ytics_rotate) "rotate" "norotate")
3123 (if (get-option '$ytics_axis) "axis" "border")
3124 (get-option '$ytics)))
3125 (if (null (get-option '$ztics))
3126 (format nil "unset ztics~%")
3127 (format nil "set ztics ~a ~a ~a~%"
3128 (if (get-option '$ztics_rotate) "rotate" "norotate")
3129 (if (get-option '$ztics_axis) "axis" "border")
3130 (get-option '$ztics)))
3131 (if (null (get-option '$cbtics))
3132 (format nil "unset cbtics~%")
3133 (format nil "set cbtics ~a~%"
3134 (get-option '$cbtics)) )
3135 (if (or (eql (get-option '$contour) '$map)
3136 (eql (get-option '$view) '$map) )
3137 (format nil "set view map~%~a~%"
3138 (if (equal (get-option '$proportional_axes) '$none)
3139 "set size noratio"
3140 "set size ratio -1") )
3141 (format nil "set view ~a, ~a, 1, 1~%~a~%"
3142 (first (get-option '$view))
3143 (second (get-option '$view))
3144 (case (get-option '$proportional_axes)
3145 ($xy "set view equal xy" )
3146 ($xyz "set view equal xyz")
3147 (otherwise ""))))
3148 (if (not (get-option '$axis_3d))
3149 (format nil "set border 0~%unset xtics~%unset ytics~%unset ztics~%"))
3150 (when (not (null (get-option '$enhanced3d)))
3151 (if (null (get-option '$wired_surface))
3152 (format nil "set pm3d at s ~a explicit~%" (get-option '$interpolate_color))
3153 (format nil "set style line 1 lt 1 lw 1 lc rgb '#000000'~%set pm3d at s depthorder explicit hidden3d 1~%") ))
3154 (if (get-option '$surface_hide)
3155 (format nil "set hidden3d nooffset~%"))
3156 (if (get-option '$xyplane)
3157 (format nil "set xyplane at ~a~%" (get-option '$xyplane)))
3158 (if (get-option '$colorbox)
3159 (format nil "set colorbox~%")
3160 (format nil "unset colorbox~%"))
3161 (format nil "set cblabel '~a'~%"
3162 (if (stringp (get-option '$colorbox))
3163 (get-option '$colorbox)
3164 ""))
3165 (write-palette-code)
3166 (if (not (string= (get-option '$user_preamble) ""))
3167 (format nil "~a~%" (get-option '$user_preamble))) ))
3168 ; scene allocation
3169 (setf *allocations* (cons (get-option '$allocation) *allocations*))
3170 ; scene description: (dimensions, gnuplot preamble in string format, list of objects)
3171 (list
3172 3 ; it's a 3d scene
3173 plotcmd ; gnuplot preamble
3174 objects ; list of objects to be plotted
3175 ) ))
3183 (defmacro write-subarray (arr str)
3184 `(format ,str
3185 "~a~%"
3186 (apply
3187 #'concatenate 'string
3188 (map
3189 'list
3190 #'(lambda (z) (format nil "~a " z))
3191 ,arr))))
3195 (defun draw_gnuplot (&rest args)
3196 (ini-global-options)
3197 (user-defaults)
3198 (setf *allocations* nil)
3199 (let ((counter 0)
3200 (scenes-list '((mlist simp))) ; these two variables will be used
3201 gfn ; gnuplot_file_name
3202 dfn ; data_file_name
3203 scene-short-description ; to build the text output
3204 scenes
3205 cmdstorage ; file maxout.gnuplot
3206 datastorage ; file data.gnuplot
3207 datapath ; path to data.gnuplot
3208 (ncols 1)
3209 nrows width height ; multiplot parameters
3210 isanimatedgif ismultipage is1stobj biglist grouplist largs)
3212 (setf largs (listify-arguments args))
3213 (dolist (x largs)
3214 (cond ((equal ($op x) "=")
3215 (case ($lhs x)
3216 ($terminal (update-gr-option '$terminal ($rhs x)))
3217 ($columns (update-gr-option '$columns ($rhs x)))
3218 ($dimensions (update-gr-option '$dimensions ($rhs x)))
3219 ($file_name (update-gr-option '$file_name ($rhs x)))
3220 ($gnuplot_file_name (update-gr-option '$gnuplot_file_name ($rhs x)))
3221 ($data_file_name (update-gr-option '$data_file_name ($rhs x)))
3222 ($delay (update-gr-option '$delay ($rhs x)))
3224 ; deprecated global options
3225 ($file_bgcolor (update-gr-option '$file_bgcolor ($rhs x)))
3226 ($pic_width (update-gr-option '$pic_width ($rhs x)))
3227 ($pic_height (update-gr-option '$pic_height ($rhs x)))
3228 ($eps_width (update-gr-option '$eps_width ($rhs x)))
3229 ($eps_height (update-gr-option '$eps_height ($rhs x)))
3230 ($pdf_width (update-gr-option '$pdf_width ($rhs x)))
3231 ($pdf_height (update-gr-option '$pdf_height ($rhs x)))
3233 (otherwise (merror "draw: unknown global option ~M " ($lhs x)))))
3234 ((equal (caar x) '$gr3d)
3235 (setf scenes (append scenes (list (funcall #'make-scene-3d (rest x))))))
3236 ((equal (caar x) '$gr2d)
3237 (setf scenes (append scenes (list (funcall #'make-scene-2d (rest x))))))
3239 (merror "draw: item ~M is not recognized" x))) )
3240 (setf isanimatedgif
3241 (equal (get-option '$terminal) '$animated_gif))
3242 (setf ismultipage
3243 (member (get-option '$terminal)
3244 '($multipage_pdf $multipage_pdfcairo $multipage_eps $multipage_eps_color)))
3246 (setf
3247 gfn (plot-temp-file (get-option '$gnuplot_file_name) t)
3248 dfn (plot-temp-file (get-option '$data_file_name) t))
3250 ;; we now create two files: maxout.gnuplot and data.gnuplot
3251 (setf cmdstorage
3252 (open
3253 #+sbcl (sb-ext:native-namestring gfn)
3254 #-sbcl gfn
3255 :direction :output :if-exists :supersede))
3256 (if (eql cmdstorage nil)
3257 (merror "draw: Cannot create file '~a'. Probably maxima_tempdir doesn't point to a writable directory." gfn))
3258 (setf datastorage
3259 (open
3260 #+sbcl (sb-ext:native-namestring dfn)
3261 #-sbcl dfn
3262 :direction :output :if-exists :supersede))
3263 (if (eql datastorage nil)
3264 (merror "draw: Cannot create file '~a'. Probably maxima_tempdir doesn't point to a writable directory." dfn))
3266 (setf datapath (format nil "'~a'" dfn))
3267 ; when one multiplot window is active, change of terminal is not allowed
3268 (if (not *multiplot-is-active*)
3269 (case (get-option '$terminal)
3270 ($dumb (format cmdstorage "set terminal dumb size ~a, ~a"
3271 (round (/ (first (get-option '$dimensions)) 10))
3272 (round (/ (second (get-option '$dimensions)) 10))))
3273 ($dumb_file (format cmdstorage "set terminal dumb size ~a, ~a~%set out '~a.dumb'"
3274 (round (/ (first (get-option '$dimensions)) 10))
3275 (round (/ (second (get-option '$dimensions)) 10))
3276 (get-option '$file_name)))
3277 ($canvas (format cmdstorage "set terminal canvas enhanced ~a size ~a, ~a~%set out '~a.html'"
3278 (write-font-type)
3279 (round (first (get-option '$dimensions)))
3280 (round (second (get-option '$dimensions)))
3281 (get-option '$file_name)))
3282 ($png (format cmdstorage "set terminal png enhanced truecolor ~a size ~a, ~a~%set out '~a.png'"
3283 (write-font-type)
3284 (round (first (get-option '$dimensions)))
3285 (round (second (get-option '$dimensions)))
3286 (get-option '$file_name) ) )
3287 ($pngcairo (format cmdstorage "set terminal pngcairo dashed enhanced truecolor ~a size ~a, ~a~%set out '~a.png'"
3288 (write-font-type)
3289 (round (first (get-option '$dimensions)))
3290 (round (second (get-option '$dimensions)))
3291 (get-option '$file_name) ) )
3292 (($eps $multipage_eps) (format cmdstorage "set terminal postscript dashed eps enhanced ~a size ~acm, ~acm~%set out '~a.eps'"
3293 (write-font-type)
3294 (/ (first (get-option '$dimensions)) 100.0)
3295 (/ (second (get-option '$dimensions)) 100.0)
3296 (get-option '$file_name)))
3297 (($eps_color $multipage_eps_color) (format cmdstorage "set terminal postscript dashed eps enhanced ~a color size ~acm, ~acm~%set out '~a.eps'"
3298 (write-font-type)
3299 (/ (first (get-option '$dimensions)) 100.0)
3300 (/ (second (get-option '$dimensions)) 100.0)
3301 (get-option '$file_name)))
3302 ($epslatex (format cmdstorage "set terminal epslatex ~a color size ~acm, ~acm~%set out '~a.tex'"
3303 (write-font-type)
3304 (/ (first (get-option '$dimensions)) 100.0)
3305 (/ (second (get-option '$dimensions)) 100.0)
3306 (get-option '$file_name)))
3307 ($epslatex_standalone (format cmdstorage "set terminal epslatex dashed standalone ~a color size ~acm, ~acm~%set out '~a.tex'"
3308 (write-font-type)
3309 (/ (first (get-option '$dimensions)) 100.0)
3310 (/ (second (get-option '$dimensions)) 100.0)
3311 (get-option '$file_name)))
3312 (($pdf $multipage_pdf) (format cmdstorage "set terminal pdf dashed enhanced ~a color size ~acm, ~acm~%set out '~a.pdf'"
3313 (write-font-type)
3314 (/ (first (get-option '$dimensions)) 100.0)
3315 (/ (second (get-option '$dimensions)) 100.0)
3316 (get-option '$file_name)))
3317 (($pdfcairo $multipage_pdfcairo) (format cmdstorage "set terminal pdfcairo dashed enhanced ~a color size ~acm, ~acm~%set out '~a.pdf'"
3318 (write-font-type)
3319 (/ (first (get-option '$dimensions)) 100.0)
3320 (/ (second (get-option '$dimensions)) 100.0)
3321 (get-option '$file_name)))
3322 ($jpg (format cmdstorage "set terminal jpeg enhanced ~a size ~a, ~a~%set out '~a.jpg'"
3323 (write-font-type)
3324 (round (first (get-option '$dimensions)))
3325 (round (second (get-option '$dimensions)))
3326 (get-option '$file_name)))
3327 ($gif (format cmdstorage "set terminal gif enhanced ~a size ~a, ~a~%set out '~a.gif'"
3328 (write-font-type)
3329 (round (first (get-option '$dimensions)))
3330 (round (second (get-option '$dimensions)))
3331 (get-option '$file_name)))
3332 ($svg (format cmdstorage "set terminal svg dashed enhanced ~a size ~a, ~a~%set out '~a.svg'"
3333 (write-font-type)
3334 (round (first (get-option '$dimensions)))
3335 (round (second (get-option '$dimensions)))
3336 (get-option '$file_name)))
3337 ($animated_gif (format cmdstorage "set terminal gif enhanced animate ~a size ~a, ~a delay ~a~%set out '~a.gif'"
3338 (write-font-type)
3339 (round (first (get-option '$dimensions)))
3340 (round (second (get-option '$dimensions)))
3341 (get-option '$delay)
3342 (get-option '$file_name)))
3343 ($aquaterm (format cmdstorage "set terminal aqua enhanced ~a ~a size ~a ~a~%"
3344 *draw-terminal-number*
3345 (write-font-type)
3346 (round (first (get-option '$dimensions)))
3347 (round (second (get-option '$dimensions)))))
3348 ($wxt (format cmdstorage "set terminal wxt dashed enhanced ~a ~a size ~a, ~a~%"
3349 *draw-terminal-number*
3350 (write-font-type)
3351 (round (first (get-option '$dimensions)))
3352 (round (second (get-option '$dimensions)))))
3353 ($qt (format cmdstorage "set terminal qt dashed enhanced ~a ~a size ~a, ~a~%"
3354 *draw-terminal-number*
3355 (write-font-type)
3356 (round (first (get-option '$dimensions)))
3357 (round (second (get-option '$dimensions)))))
3358 ($x11 (format cmdstorage "if(GPVAL_VERSION >= 5.0){set terminal x11 dashed enhanced ~a ~a size ~a, ~a replotonresize} else {set terminal x11 dashed enhanced ~a ~a size ~a, ~a}~%"
3359 *draw-terminal-number*
3360 (write-font-type)
3361 (round (first (get-option '$dimensions)))
3362 (round (second (get-option '$dimensions)))
3363 *draw-terminal-number*
3364 (write-font-type)
3365 (round (first (get-option '$dimensions)))
3366 (round (second (get-option '$dimensions)))))
3367 ($windows (format cmdstorage "set terminal windows enhanced ~a ~a size ~a, ~a~%"
3368 *draw-terminal-number*
3369 (write-font-type)
3370 (round (first (get-option '$dimensions)))
3371 (round (second (get-option '$dimensions)))))
3373 (otherwise
3374 ;; If we arrive here, (get-option '$terminal) returned something
3375 ;; unrecognized; at present (commit ccd8074, 2020-12-07) the following
3376 ;; are accepted by UPDATE-TERMINAL, but not handled here: $obj $ply
3377 ;; $pnm $screen $stl $tiff $vrml
3378 (unless (eq (get-option '$terminal) '$screen)
3379 (mtell "draw: warning: I don't know about terminal '~m'; I'll try to restore the default.~%" (get-option '$terminal))
3380 (mtell "draw: try this: set_draw_defaults(terminal = <something>);~%"))
3382 (format cmdstorage "set terminal GNUTERM ~a ~a size ~a, ~a~%"
3383 *draw-terminal-number*
3384 (write-font-type)
3385 (round (first (get-option '$dimensions)))
3386 (round (second (get-option '$dimensions)))))))
3388 ; compute some parameters for multiplot
3389 (when (and (not isanimatedgif) (not ismultipage))
3390 (setf ncols (get-option '$columns))
3391 (setf nrows (ceiling (/ (length scenes) ncols)))
3392 (if (> (length scenes) 1)
3393 (format cmdstorage "~%set size 1.0, 1.0~%set origin 0.0, 0.0~%set multiplot~%")) )
3395 ; Make gnuplot versions newer than 5.0 understand that linetype means
3396 ; we try to set the dash type
3397 (format cmdstorage "~%if(GPVAL_VERSION >= 5.0){set for [i=1:8] linetype i dashtype i; set format '%h'}")
3399 ;; By default gnuplot assumes everything below 1e-8 to be a rounding error
3400 ;; and rounds it down to 0. This is handy for standalone gnuplot as it allows
3401 ;; to suppress pixels with imaginary part while allowing for small calculation
3402 ;; errors. As plot and draw handle the imaginary part without gnuplot's help
3403 ;; this isn't needed here and is turned off as it often surprises users.
3404 (format cmdstorage "~%set zero 0.0")
3406 ; We use whitespace as a separator, so override any datafile separator
3407 ; the user has set. The user could have set the separator to a comma
3408 ; for reading CSV files, for example.
3409 (format cmdstorage "~%set datafile separator whitespace")
3411 ; write descriptions of 2d and 3d scenes
3412 (let ((i -1)
3413 (alloc (reverse *allocations*))
3414 (nilcounter 0)
3415 thisalloc origin1 origin2 size1 size2)
3417 ; recalculate nrows for automatic scene allocations
3418 (setf nrows (ceiling (/ (count nil alloc) ncols)))
3420 (when (> nrows 0)
3421 (setf width (/ 1.0 ncols)
3422 height (/ 1.0 nrows)))
3423 (dolist (scn scenes)
3424 ; write size and origin if necessary
3425 (cond ((or isanimatedgif ismultipage)
3426 (format cmdstorage "~%set size 1.0, 1.0~%")
3427 (format cmdstorage "set obj 1 rectangle behind from screen 0.0,0.0 to screen 1.0,1.0~%") )
3428 (t ; it's not an animated gif
3429 (setf thisalloc (car alloc))
3430 (setf alloc (cdr alloc))
3431 (cond
3432 (thisalloc ; user defined scene allocation
3433 (setf origin1 (first thisalloc)
3434 origin2 (second thisalloc)
3435 size1 (third thisalloc)
3436 size2 (fourth thisalloc)))
3437 (t ; automatic scene allocation
3438 (setf origin1 (* width (mod nilcounter ncols))
3439 origin2 (* height (- nrows 1.0 (floor (/ nilcounter ncols))))
3440 size1 width
3441 size2 height)
3442 (incf nilcounter)))
3443 (format cmdstorage "~%set size ~a, ~a~%" size1 size2)
3444 (format cmdstorage "set origin ~a, ~a~%" origin1 origin2)
3445 (unless (or *multiplot-is-active*
3446 (member (get-option '$terminal) '($epslatex $epslatex_standalone)))
3447 (format cmdstorage "set obj 1 rectangle behind from screen ~a,~a to screen ~a,~a~%"
3448 origin1 origin2 (+ origin1 size1 ) (+ origin2 size2))) ))
3449 (setf is1stobj t
3450 biglist '()
3451 grouplist '())
3452 (format cmdstorage "~a" (second scn))
3453 (cond ((= (first scn) 2) ; it's a 2d scene
3454 (setf scene-short-description '(($gr2d simp)))
3455 (format cmdstorage "plot "))
3456 ((= (first scn) 3) ; it's a 3d scene
3457 (setf scene-short-description '(($gr3d simp)))
3458 (format cmdstorage "splot ")))
3459 (dolist (obj (third scn))
3460 (setf scene-short-description
3461 (cons (gr-object-name obj) scene-short-description))
3462 (if is1stobj
3463 (setf is1stobj nil)
3464 (format cmdstorage ", \\~%") )
3465 (let ((pcom (gr-object-command obj)))
3466 (cond
3467 ((listp pcom)
3468 (while (consp pcom)
3469 (format cmdstorage "~a~a~a~a"
3470 datapath
3471 (format nil " index ~a" (incf i))
3472 (pop pcom)
3473 (if (null pcom)
3475 "," )) ) )
3476 (t (format cmdstorage "~a~a~a"
3477 datapath
3478 (format nil " index ~a" (incf i))
3479 pcom) )))
3480 (setf grouplist (append grouplist (gr-object-groups obj)))
3481 (setf biglist (append biglist (gr-object-points obj))) )
3483 ; let's write data in data.gnuplot
3484 (do ( (blis biglist (cdr blis))
3485 (glis grouplist (cdr glis) ))
3486 ((null blis) 'done)
3487 (let* ((vect (car blis))
3488 (k (length vect))
3489 (ncol (caar glis))
3490 (l 0)
3491 (m (cadar glis))
3492 (non-numeric-region nil)
3493 coordinates)
3494 (cond
3495 ((= m 0) ; no blank lines
3496 (do ((cont 0 (+ cont ncol)))
3497 ((= cont k) 'done)
3498 (setf coordinates (subseq vect cont (+ cont ncol)))
3499 ; control of non numeric y values,
3500 ; code related to draw_realpart
3501 (cond
3502 (non-numeric-region
3503 (when (numberp (aref coordinates 1))
3504 (setf non-numeric-region nil)
3505 (write-subarray coordinates datastorage) ))
3507 (cond
3508 ((numberp (aref coordinates 1))
3509 (write-subarray coordinates datastorage))
3511 (setf non-numeric-region t)
3512 (format datastorage "~%")))))) )
3514 (t ; blank lines every m lines
3515 (do ((cont 0 (+ cont ncol)))
3516 ((= cont k) 'done)
3517 (when (eql l m)
3518 (format datastorage "~%")
3519 (setf l 0) )
3520 (write-subarray (subseq vect cont (+ cont ncol)) datastorage)
3521 (incf l)))))
3522 (format datastorage "~%~%") )
3523 (incf counter)
3524 (setf scenes-list (cons (reverse scene-short-description) scenes-list)) )) ; end let-dolist scenes
3525 (close datastorage)
3527 (cond ((or isanimatedgif ismultipage) ; this is an animated gif or multipage plot file
3528 (if isanimatedgif
3529 (format cmdstorage "~%unset output~%quit~%~%")
3530 (format cmdstorage "~%set term dumb~%~%") )
3531 (close cmdstorage)
3532 #+(or (and sbcl win32) (and sbcl win64) (and ccl windows))
3533 ($system $gnuplot_command gfn)
3534 #-(or (and sbcl win32) (and sbcl win64) (and ccl windows))
3535 ($system (format nil "~a \"~a\""
3536 $gnuplot_command
3537 gfn) ))
3538 (t ; non animated gif
3539 ; command file maxout.gnuplot is now ready
3540 (format cmdstorage "~%")
3541 (cond ((> (length scenes) 1)
3542 (format cmdstorage "unset multiplot~%unset output ~%"))
3543 ; if we want to save the coordinates in a file,
3544 ; print them when hitting the x key after clicking the mouse button
3545 ((not (string= (get-option '$xy_file) ""))
3546 (format cmdstorage
3547 "set print \"~a\" append~%bind x \"print MOUSE_X,MOUSE_Y\"~%"
3548 (get-option '$xy_file))) )
3550 ; in svg and pdfcairo terminals it is necessary to unset output to force
3551 ; Gnuplot to write the last few bytes ("</svg>" in the svg case)
3552 ; at the end of the file. On windows in all other terminals that write
3553 ; to files if the output isn't unset the file isn't closed and therefore
3554 ; cannot be moved or removed after a plot is finished.
3556 ; If the plot isn't written to a file the variable "output" is empty and
3557 ; unused and unsetting it a second time doesn't change anything
3558 ; => It is always save to unset the output at the end of a scene.
3559 (when (not *multiplot-is-active*) ; not in a one window multiplot
3560 (format cmdstorage "unset output~%"))
3561 (close cmdstorage)
3562 ; get the plot
3563 (cond
3564 ; connect to gnuplot via pipes
3565 ((and (member (get-option '$terminal) '($screen $aquaterm $wxt $x11 $qt $windows))
3566 (equal $draw_renderer '$gnuplot_pipes))
3567 (check-gnuplot-process)
3568 (when (not *multiplot-is-active*) ; not in a one window multiplot
3569 (send-gnuplot-command "unset output"))
3570 (send-gnuplot-command "reset")
3571 (send-gnuplot-command
3572 (format nil "load '~a'" gfn)))
3573 ; call gnuplot via system command
3576 #+(or (and sbcl win32) (and sbcl win64) (and ccl windows))
3577 (if (member (get-option '$terminal) '($screen $aquaterm $wxt $x11 $qt $windows))
3578 ($system $gnuplot_command "-persist" gfn)
3579 ($system $gnuplot_command gfn))
3580 #-(or (and sbcl win32) (and sbcl win64) (and ccl windows))
3581 ($system (if (member (get-option '$terminal) '($screen $aquaterm $wxt $x11 $qt $windows))
3582 (format nil "~a ~a"
3583 $gnuplot_command
3584 (format nil $gnuplot_view_args gfn))
3585 (format nil "~a \"~a\""
3586 $gnuplot_command
3587 gfn))) ))))
3589 ; the output is a simplified description of the scene(s)
3590 (reverse scenes-list)) )
3593 ;; This function transforms an integer number into
3594 ;; a string, adding zeros at the left side until the
3595 ;; length of the string equals 10. This function is
3596 ;; useful to name a sequence of frames.
3597 (defun $add_zeroes (num)
3598 (format nil "~10,'0d" ($sconcat num)) )
3601 ;; copies current plot in window into a file
3602 (defun $draw_file (&rest opts)
3603 (let (str)
3604 (dolist (x opts)
3605 (if (equal ($op x) "=")
3606 (update-gr-option ($lhs x) ($rhs x))
3607 (merror "draw: item ~M is not recognized as an option assignment" x)))
3608 (case (get-option '$terminal)
3609 ($canvas (setf str (format nil "set terminal canvas enhanced ~a size ~a, ~a~%set out '~a.html'"
3610 (write-font-type)
3611 (round (first (get-option '$dimensions)))
3612 (round (second (get-option '$dimensions)))
3613 (get-option '$file_name))))
3614 ($png (setf str (format nil "set terminal png enhanced truecolor ~a size ~a, ~a~%set out '~a.png'"
3615 (write-font-type)
3616 (round (first (get-option '$dimensions)))
3617 (round (second (get-option '$dimensions)))
3618 (get-option '$file_name) ) ))
3619 ($pngcairo (setf str (format nil "set terminal pngcairo dashed enhanced truecolor ~a size ~a, ~a~%set out '~a.png'"
3620 (write-font-type)
3621 (round (first (get-option '$dimensions)))
3622 (round (second (get-option '$dimensions)))
3623 (get-option '$file_name) ) ))
3624 ($eps (setf str (format nil "set terminal postscript dashed eps enhanced ~a size ~acm, ~acm~%set out '~a.eps'"
3625 (write-font-type) ; other alternatives are Arial, Courier
3626 (/ (first (get-option '$dimensions)) 100.0)
3627 (/ (second (get-option '$dimensions)) 100.0)
3628 (get-option '$file_name))))
3629 ($epslatex (format str "set terminal epslatex ~a color dashed size ~acm, ~acm~%set out '~a.tex'"
3630 (write-font-type)
3631 (/ (first (get-option '$dimensions)) 100.0)
3632 (/ (second (get-option '$dimensions)) 100.0)
3633 (get-option '$file_name)))
3634 ($epslatex_standalone (format str "set terminal epslatex dashed standalone ~a color size ~acm, ~acm~%set out '~a.tex'"
3635 (write-font-type)
3636 (/ (first (get-option '$dimensions)) 100.0)
3637 (/ (second (get-option '$dimensions)) 100.0)
3638 (get-option '$file_name)))
3639 ($eps_color (setf str (format nil "set terminal postscript dashed eps enhanced ~a color size ~acm, ~acm~%set out '~a.eps'"
3640 (write-font-type)
3641 (/ (first (get-option '$dimensions)) 100.0)
3642 (/ (second (get-option '$dimensions)) 100.0)
3643 (get-option '$file_name))))
3644 ($pdf (setf str (format nil "set terminal pdf dashed enhanced ~a color size ~acm, ~acm~%set out '~a.pdf'"
3645 (write-font-type)
3646 (/ (first (get-option '$dimensions)) 100.0)
3647 (/ (second (get-option '$dimensions)) 100.0)
3648 (get-option '$file_name))))
3649 ($pdfcairo (setf str (format nil "set terminal pdfcairo dashed enhanced ~a color size ~acm, ~acm~%set out '~a.pdf'"
3650 (write-font-type)
3651 (/ (first (get-option '$dimensions)) 100.0)
3652 (/ (second (get-option '$dimensions)) 100.0)
3653 (get-option '$file_name))))
3654 ($jpg (setf str (format nil "set terminal jpeg ~a size ~a, ~a~%set out '~a.jpg'"
3655 (write-font-type)
3656 (round (first (get-option '$dimensions)))
3657 (round (second (get-option '$dimensions)))
3658 (get-option '$file_name))))
3659 ($gif (setf str (format nil "set terminal gif ~a size ~a, ~a~%set out '~a.gif'"
3660 (write-font-type)
3661 (round (first (get-option '$dimensions)))
3662 (round (second (get-option '$dimensions)))
3663 (get-option '$file_name))))
3664 ($svg (setf str (format nil "set terminal svg dashed enhanced ~a size ~a, ~a~%set out '~a.svg'"
3665 (write-font-type)
3666 (round (first (get-option '$dimensions)))
3667 (round (second (get-option '$dimensions)))
3668 (get-option '$file_name))))
3669 (otherwise (merror "draw: not a file format")))
3670 (send-gnuplot-command (format nil "~a~%replot~%unset output~%" str)) ))
3673 ;; When working with multiple windows, for example
3674 ;; terminal = [wxt, 5], only the newest window is active.
3675 ;; This function activates any other previous window at will.
3676 (defun $activate_window (term num)
3677 (when (or (not (member term '($screen $wxt $aquaterm)))
3678 (not (integerp num))
3679 (< num 0) )
3680 (merror "draw: Incorrect terminal or window number"))
3681 (let (str)
3682 (case term
3683 ($wxt (setf str "wxt"))
3684 ($aquaterm (setf str "aquaterm"))
3685 ($qt (setf str "qt"))
3686 (otherwise (setf str "GNUTERM")))
3687 (send-gnuplot-command (format nil "set terminal ~a ~a~%" str num)) ))