Xmaxima: ~/.xmaximrc should probably be ~/.xmaximarc.
[maxima/cygwin.git] / src / gnuplot_def.lisp
blobf97f5dac3ae1ada236b92a24d77089d60beaf705
1 ;; gnuplot.lisp: routines for Maxima's interface to gnuplot
2 ;; Copyright (C) 2007-2021 J. Villate
3 ;;
4 ;; This program is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU General Public License
6 ;; as published by the Free Software Foundation; either version 2
7 ;; of the License, or (at your option) any later version.
8 ;;
9 ;; This program is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;; GNU General Public License for more details.
13 ;;
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program; if not, write to the Free Software
16 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 ;; MA 02110-1301, USA
19 (in-package :maxima)
21 ;; Checks that color is a six-digit hexadecimal number with the prefix #,
22 ;; or a symbol for one of the 12 pre-defined colors, in which case the
23 ;; hexadecimal code for that color will be returned. Unknown colors are
24 ;; converted into black.
25 (defun rgb-color (color)
26 (if (plotcolorp color)
27 (case color
28 ($red "#ff0000")
29 ($green "#00ff00")
30 ($blue "#0000ff")
31 ($magenta "#ff00ff")
32 ($cyan "#00ffff")
33 ($yellow "#ffff00")
34 ($orange "#ffa500")
35 ($violet "#ee82ee")
36 ($brown "#a52a2a")
37 ($gray "#bebebe")
38 ($black "#000000")
39 ($white "#ffffff")
40 (t color))
41 "#000000"))
43 ;; Given a list of valid colors (see rgb-color function) and an object c
44 ;; that can be a real number or a string, produces a gnuplot color
45 ;; specification for c; when c is real, its nearest integer is assigned
46 ;; to one of the numbers in the list, using modulo length of the list.
47 (defun gnuplot-color (colors c)
48 (unless (listp colors) (setq colors (list colors)))
49 (when (realp c)
50 (unless (integerp c) (setq c (round c)))
51 (setq c (nth (mod (1- c) (length colors)) colors)))
52 (format nil "rgb ~s" (rgb-color c)))
54 (defun gnuplot-pointtype (type)
55 (case type
56 ($bullet 7) ($circle 6) ($plus 1) ($times 2) ($asterisk 3) ($box 5)
57 ($square 4) ($triangle 9) ($delta 8) ($wedge 11) ($nabla 10)
58 ($diamond 13) ($lozenge 12) (t 7)))
60 (defun gnuplot-pointtypes (types n)
61 (unless (integerp n) (setq n (round n)))
62 (unless (listp types) (setq types (list types)))
63 (gnuplot-pointtype (nth (mod (- n 1) (length types)) types)))
65 ;; style is a list starting with one of the symbols: points, lines,
66 ;; linespoints or dots,
67 ;; The meaning of the numbers that follow the symbol are:
69 ;; lines, linewidth, color
70 ;; points, radius, color, pointtype
71 ;; linespoints, linewidth, radius, color, pointtype
72 ;; dots, color
74 ;; linewidth and radius are measured in the same units and can be
75 ;; floating-point numbers.
77 ;; type must be an integer
78 ;; color can be an integer, used as index to get one of the colors defined
79 ;; by the color option, or a 6-digit hexadecimal number #rrggbb
81 (defun gnuplot-curve-style (style colors types i)
82 (unless (listp style) (setq style (list style)))
83 (unless (listp colors) (setq colors (list colors)))
84 (with-output-to-string
85 (st)
86 (case (first style)
87 ($dots
88 (format st "with dots")
89 (if (second style)
90 (format st " lt ~d" (gnuplot-color colors (second style)))
91 (format st " lt ~d" (gnuplot-color colors i))))
92 ($impulses
93 (format st "with impulses")
94 (if (integerp (second style))
95 (format st " lt ~d" (gnuplot-color colors (second style)))
96 (format st " lt ~d" (gnuplot-color colors i))))
97 ($lines
98 (format st "with lines")
99 (if (realp (second style))
100 (format st " lw ~,2f" (second style)))
101 (if (third style)
102 (format st " lt ~d" (gnuplot-color colors (third style)))
103 (format st " lt ~d" (gnuplot-color colors i))))
104 ($points
105 (format st "with points")
106 (if (realp (second style))
107 (format st " ps ~,2f" (/ (second style) 2))
108 (format st " ps 1.5"))
109 (if (third style)
110 (format st " lt ~d" (gnuplot-color colors (third style)))
111 (format st " lt ~d" (gnuplot-color colors i)))
112 (if (integerp (fourth style))
113 (format st " pt ~d" (gnuplot-pointtypes types (fourth style)))
114 (format st " pt ~d" (gnuplot-pointtypes types i))))
115 ($linespoints
116 (format st "with linespoints")
117 (if (realp (second style))
118 (format st " lw ~,2f" (second style)))
119 (if (realp (third style))
120 (format st " ps ~,2f" (/ (third style) 2))
121 (format st " ps 1.5"))
122 (if (fourth style)
123 (format st " lt ~d" (gnuplot-color colors (fourth style)))
124 (format st " lt ~d" (gnuplot-color colors i)))
125 (if (integerp (fifth style))
126 (format st " pt ~d" (gnuplot-pointtypes types (fifth style)))
127 (format st " pt ~d" (gnuplot-pointtypes types i))))
128 (t (format st "with lines lt ~d" (gnuplot-color colors i))))))
130 (defun gnuplot-palette (palette)
131 ;; palette should be a list starting with one of the symbols: hue,
132 ;; saturation, value, gray or gradient.
134 ;; If the symbol is gray, it should be followed by two floating point
135 ;; numbers that indicate the initial gray level and the interval of
136 ;; gray values.
138 ;; If the symbol is one of hue, saturation or value, it must be followed
139 ;; by three numbers that specify the hue, saturation and value for the
140 ;; initial color, and a fourth number that gives the range of values for
141 ;; the increment of hue, saturation or value.
142 ;; The values for the initial hue, saturation, value and grayness should
143 ;; be within 0 and 1, while the range can be higher or even negative.
145 ;; If the symbol is gradient, it must be followed by either a list of valid
146 ;; colors or by a list of lists with two elements, a number and a valid color.
148 (unless (listp palette) (setq palette (list palette)))
149 (let (hue sat val gray range fun)
150 (case (first palette)
151 ($gray
152 (case (length (rest palette))
153 (2 (setq gray (second palette)) (setq range (third palette)))
154 (t (merror
155 (intl:gettext
156 "palette: gray must be followed by two numbers."))))
157 (when (or (< gray 0) (> gray 1))
158 (setq gray (- gray (floor gray)))))
159 (($hue $saturation $value)
160 (case (length (rest palette))
161 (4 (setq hue (second palette))
162 (setq sat (third palette))
163 (setq val (fourth palette))
164 (setq range (fifth palette)))
165 (t (merror
166 (intl:gettext
167 "palette: ~M must be followed by four numbers.")
168 (first palette))))
169 (when (or (< hue 0) (> hue 1)) (setq hue (- hue (floor hue))))
170 (when (or (< sat 0) (> sat 1)) (setq sat (- sat (floor sat))))
171 (when (or (< val 0) (> val 1)) (setq val (- val (floor val))))))
172 (with-output-to-string (st)
173 (case (first palette)
174 ($hue
175 (if (or (< (+ hue range) 0) (> (+ hue range) 1))
176 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
177 hue range hue range))
178 (setq fun (format nil "~,3f+~,3f*gray" hue range)))
179 (format st "model HSV functions ~a, ~,3f, ~,3f" fun sat val))
180 ($saturation
181 (if (or (< (+ sat range) 0) (> (+ sat range) 1))
182 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
183 sat range sat range))
184 (setq fun (format nil "~,3f+~,3f*gray" sat range)))
185 (format st "model HSV functions ~,3f, ~a, ~,3f" hue fun val))
186 ($value
187 (if (or (< (+ val range) 0) (> (+ val range) 1))
188 (setq fun (format nil "~,3f+~,3f*gray" val range))
189 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
190 val range val range)))
191 (format st "model HSV functions ~,3f, ~,3f, ~a" hue sat fun))
192 ($gray
193 (if (or (< (+ gray range) 0) (> (+ gray range) 1))
194 (setq fun (format nil "~,3f+~,3f*gray" gray range))
195 (setq fun (format nil "~,3f+~,3f*gray-floor(~,3f+~,3f*gray)"
196 gray range gray range)))
197 (format st "model RGB functions ~a, ~a, ~a" fun fun fun))
199 ($gradient
200 (let* ((colors (rest palette)) (n (length colors)) (map nil))
201 ;; map is constructed as (n1 c1 n2 c2 ... nj cj) where ni is a
202 ;; decreasing sequence of numbers (n1=1, nj=0) and ci are colors
203 (cond
204 ;; Maxima list of numbers and colors (((mlist) ni ci) ...)
205 ((listp (first colors))
206 (setq colors (sort colors #'< :key #'cadr))
207 (dotimes (i n)
208 (setq map (cons (rgb-color (third (nth i colors))) ;; color
209 (cons
210 (/ (- (second (nth i colors)) ;; ni minus
211 (second (first colors))) ;; smallest ni
212 (- (second (nth (- n 1) colors));; biggest
213 (second (first colors)))) ;; - smallest
214 map)))))
215 ;; list of only colors
216 (t (dotimes (i n)
217 (setq map (cons (rgb-color (nth i colors)) ;; color i
218 (cons (/ i (1- n)) map)))))) ;; number i
220 ;; prints map with the format: nj, "cj", ...,n1, "c1"
221 (setq fun (format nil "~{~,8f ~s~^, ~}" (reverse map)))
222 ;; outputs the string: defined (nj, "cj", ...,n1, "c1")
223 (format st "defined (~a)" fun)))
225 (merror
226 (intl:gettext
227 "palette: wrong keyword ~M. Must be hue, saturation, value, gray or gradient.")
228 (first palette)))))))
230 (defun gnuplot-plot3d-command (file palette gstyles colors gstrings titles n)
231 (let (title (style "with pm3d"))
232 (with-output-to-string (out)
233 (format out "splot ")
234 (do ((i 1 (+ i 1))) ((> i n) (format out "~%"))
235 (unless palette
236 (if gstyles
237 (setq style (ensure-string (nth (mod i (length gstyles)) gstyles)))
238 (setq style
239 (format nil "with lines lt ~a" (gnuplot-color colors i)))))
240 (when (> i 1) (format out ", "))
241 (if titles
242 (setq title (nth (mod i (length titles)) titles))
243 (setq title ""))
244 (format out "~s title ~s ~a ~a" file title gstrings style)))))
246 (defun gnuplot-terminal-and-file (plot-options)
247 (let (terminal-command out-file (preserve-file t))
248 (cond
249 ((getf plot-options :svg_file)
250 (if (getf plot-options :gnuplot_svg_term_command)
251 (setq terminal-command
252 (getf plot-options :gnuplot_svg_term_command))
253 (setq terminal-command "set term svg font \",14\""))
254 (setq out-file (getf plot-options :svg_file)))
255 ((getf plot-options :png_file)
256 (if (getf plot-options :gnuplot_png_term_command)
257 (setq terminal-command
258 (getf plot-options :gnuplot_png_term_command))
259 (setq terminal-command "set term pngcairo font \",12\""))
260 (setq out-file (getf plot-options :png_file)))
261 ((getf plot-options :pdf_file)
262 (if (getf plot-options :gnuplot_pdf_term_command)
263 (setq terminal-command
264 (getf plot-options :gnuplot_pdf_term_command))
265 (setq terminal-command "set term pdfcairo color solid lw 3 size 17.2 cm, 12.9 cm font \",18\""))
266 (setq out-file (getf plot-options :pdf_file)))
267 ((getf plot-options :ps_file)
268 (if (getf plot-options :gnuplot_ps_term_command)
269 (setq terminal-command
270 (getf plot-options :gnuplot_ps_term_command))
271 (setq terminal-command "set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\""))
272 (setq out-file (getf plot-options :ps_file)))
273 ((eq (getf plot-options :gnuplot_term) '$ps)
274 (if (getf plot-options :gnuplot_ps_term_command)
275 (setq terminal-command
276 (getf plot-options :gnuplot_ps_term_command))
277 (setq terminal-command "set term postscript eps color solid lw 2 size 16.4 cm, 12.3 cm font \",24\""))
278 (if (getf plot-options :gnuplot_out_file)
279 (setq out-file (getf plot-options :gnuplot_out_file))
280 (setq out-file "maxplot.ps")))
281 ((eq (getf plot-options :gnuplot_term) '$dumb)
282 (if (getf plot-options :gnuplot_dumb_term_command)
283 (setq terminal-command
284 (getf plot-options :gnuplot_ps_term_command))
285 (setq terminal-command "set term dumb 79 22"))
286 (if (getf plot-options :gnuplot_out_file)
287 (setq out-file (getf plot-options :gnuplot_out_file))
288 (setq out-file "maxplot.txt")))
289 ((eq (getf plot-options :gnuplot_term) '$default)
290 (if (getf plot-options :gnuplot_default_term_command)
291 (setq terminal-command
292 (getf plot-options :gnuplot_default_term_command))
293 (setq terminal-command
294 "set term pop")))
295 ((getf plot-options :gnuplot_term)
296 (setq
297 terminal-command
298 (format nil "set term ~(~a~)"
299 (ensure-string (getf plot-options :gnuplot_term))))
300 (if (getf plot-options :gnuplot_out_file)
301 (setq out-file (getf plot-options :gnuplot_out_file))
302 (setq preserve-file nil
303 out-file
304 (format nil "maxplot.~(~a~)"
305 (get-gnuplot-term (getf plot-options :gnuplot_term)))))))
307 (unless (null out-file) (setq out-file (plot-file-path out-file preserve-file)))
308 (list terminal-command out-file)))
310 (defmethod plot-preamble ((plot gnuplot-plot) plot-options)
311 (let ((palette (getf plot-options :palette))
312 (meshcolor (if (member :mesh_lines_color plot-options)
313 (getf plot-options :mesh_lines_color)
314 '$black))
315 (gstrings (if (getf plot-options :gnuplot_strings)
316 "" "noenhanced")) terminal-file)
317 (when (find 'mlist palette :key #'car) (setq palette (list palette)))
318 ;; sets-up terminal command and output file name
319 (setq terminal-file (gnuplot-terminal-and-file plot-options))
320 (setf
321 (slot-value plot 'data)
322 (concatenate
323 'string
324 (slot-value plot 'data)
325 (with-output-to-string (dest)
326 ;; user's preamble
327 (when (and (getf plot-options :gnuplot_preamble)
328 (> (length (getf plot-options :gnuplot_preamble)) 0))
329 (format dest "~a~%" (getf plot-options :gnuplot_preamble)))
330 ;; Don't round numbers with absolute value less than 1e-8 to zero
331 (format dest "set zero 0.0~%")
332 ;; prints terminal and output commands
333 (when (first terminal-file)
334 (format dest "~a~%" (first terminal-file)))
335 (when (second terminal-file)
336 (format dest "set output ~s~%" (second terminal-file)))
337 ;; options specific to plot3d
338 (when (string= (getf plot-options :type) "plot3d")
339 (format dest "set xyplane relative 0~%")
340 (if palette
341 (progn
342 (if meshcolor
343 (progn
344 (format dest "set style line 100 lt rgb ~s lw 1~%"
345 (rgb-color meshcolor))
346 (format dest "set pm3d hidden3d 100~%")
347 (unless (getf plot-options :gnuplot_4_0)
348 (format dest "set pm3d depthorder~%")))
349 (format dest "set pm3d~%"))
350 (format dest "unset hidden3d~%")
351 (format dest "set palette ~a~%"
352 (gnuplot-palette (rest (first palette)))))
353 (format dest "set hidden3d~%"))
354 (let ((elev (getf plot-options :elevation))
355 (azim (getf plot-options :azimuth)))
356 (when (or elev azim)
357 (if elev
358 (format dest "set view ~d" elev)
359 (format dest "set view "))
360 (when azim (format dest ", ~d" azim))
361 (format dest "~%"))))
362 ;; color_bar can be used by plot3d or plot2d
363 (unless (getf plot-options :color_bar)
364 (format dest "unset colorbox~%"))
365 ;; ----- BEGIN GNUPLOT 4.0 WORK-AROUND -----
366 ;; When the expression plotted is constant, Gnuplot 4.0 fails
367 ;; with a division by 0. Explicitly assigning cbrange prevents
368 ;; the error. Also set zrange to match cbrange.
369 (when (floatp (getf plot-options :const_expr))
370 (format
371 dest "set cbrange [~a : ~a]~%"
372 (1- (getf plot-options :const_expr))
373 (1+ (getf plot-options :const_expr)))
374 (format
375 dest "set zrange [~a : ~a]~%"
376 (1- (getf plot-options :const_expr))
377 (1+ (getf plot-options :const_expr))))
378 ;; ----- END GNUPLOT 4.0 WORK-AROUND -----
379 ;; logarithmic plots
380 (when (getf plot-options :logx) (format dest "set log x~%"))
381 (when (getf plot-options :logy) (format dest "set log y~%"))
382 ;; axes labels and legend
383 (when (getf plot-options :xlabel)
384 (format dest
385 "set xlabel ~s ~a~%" (getf plot-options :xlabel) gstrings))
386 (when (getf plot-options :ylabel)
387 (format dest
388 "set ylabel ~s ~a~%" (getf plot-options :ylabel) gstrings))
389 (when (getf plot-options :zlabel)
390 (format dest
391 "set zlabel ~s ~a~%" (getf plot-options :zlabel) gstrings))
392 (when (and (member :legend plot-options)
393 (null (getf plot-options :legend)))
394 (format dest "unset key~%"))
395 ;; plotting box
396 (when (and (member :box plot-options) (not (getf plot-options :box)))
397 (format dest "unset border~%")
398 (if (and (getf plot-options :axes)
399 (string= (getf plot-options :type) "plot2d"))
400 (format dest "set xtics axis~%set ytics axis~%set ztics axis~%")
401 (format dest "unset xtics~%unset ytics~%unset ztics~%")))
402 ;; 2d grid (specific to plot2d)
403 (when (string= (getf plot-options :type) "plot2d")
404 (format dest "set grid front~%")
405 (if (getf plot-options :grid2d)
406 (format dest "set grid~%")
407 (format dest "unset grid~%"))
408 ;; plot size and aspect ratio for plot2d
409 (if (getf plot-options :same_xy)
410 (format dest "set size ratio -1~%")
411 (if (getf plot-options :yx_ratio)
412 (format dest "set size ratio ~,8f~%"
413 (getf plot-options :yx_ratio))
414 (if (not (getf plot-options :xy_scale))
415 ;; emit the default only if there is no xy_scale specified.
416 (format dest "set size ratio 0.75~%"))))
417 (if (and (getf plot-options :xy_scale)
418 (listp (getf plot-options :xy_scale)))
419 (format dest "set size ~{~,8f~^, ~}~%"
420 (getf plot-options :xy_scale))))
421 ;; plot size and aspect ratio for plot3d
422 (when (string= (getf plot-options :type) "plot3d")
423 (when (getf plot-options :same_xy)
424 (format dest "set view equal xy~%"))
425 (when (getf plot-options :same_xyz)
426 (format dest "set view equal xyz~%"))
427 (when (getf plot-options :zmin)
428 (format dest "set xyplane at ~,8f~%" (getf plot-options :zmin))))
429 ;; axes tics
430 (when (member :xtics plot-options)
431 (let ((xtics (getf plot-options :xtics)))
432 (if (consp xtics)
433 (format dest "set xtics ~{~,8f~^, ~}~%" xtics)
434 (if xtics
435 (format dest "set xtics ~,8f~%" xtics)
436 (format dest "unset xtics~%")))))
437 (when (member :ytics plot-options)
438 (let ((ytics (getf plot-options :ytics)))
439 (if (consp ytics)
440 (format dest "set ytics ~{~,8f~^, ~}~%" ytics)
441 (if ytics
442 (format dest "set ytics ~,8f~%" ytics)
443 (format dest "unset ytics~%")))))
444 (when (member :ztics plot-options)
445 (let ((ztics (getf plot-options :ztics)))
446 (if (consp ztics)
447 (format dest "set ztics ~{~,8f~^, ~}~%" ztics)
448 (if ztics
449 (format dest "set ztics ~,8f~%" ztics)
450 (format dest "unset ztics~%")))))
451 (when (member :color_bar_tics plot-options)
452 (let ((cbtics (getf plot-options :color_bar_tics)))
453 (if (consp cbtics)
454 (format dest "set cbtics ~{~,8f~^, ~}~%" cbtics)
455 (if cbtics
456 (format dest "set cbtics ~,8f~%" cbtics)
457 (format dest "unset cbtics~%")))))
458 ;; axes ranges and style
459 (when (and (getf plot-options :x) (listp (getf plot-options :x)))
460 (format dest "set xrange [~{~g~^ : ~}]~%" (getf plot-options :x)))
461 (when (and (getf plot-options :y) (listp (getf plot-options :y)))
462 (format dest "set yrange [~{~g~^ : ~}]~%" (getf plot-options :y)))
463 (when (and (getf plot-options :z) (listp (getf plot-options :z)))
464 (format dest "set zrange [~{~g~^ : ~}]~%" (getf plot-options :z)))
465 (when (and (string= (getf plot-options :type) "plot2d")
466 (member :axes plot-options))
467 (if (getf plot-options :axes)
468 (case (getf plot-options :axes)
469 ($x (format dest "set xzeroaxis~%"))
470 ($y (format dest "set yzeroaxis~%"))
471 ($solid (format dest "set zeroaxis lt -1~%"))
472 (t (format dest "set zeroaxis~%")))))
473 ;; title and labels
474 (when (getf plot-options :title)
475 (format dest "set title ~s ~a~%" (getf plot-options :title) gstrings))
476 (when (getf plot-options :label)
477 (dolist (label (getf plot-options :label))
478 (when (and (listp label) (= (length label) 4))
479 (format dest "set label ~s ~a at ~{~,8f~^, ~}~%"
480 (cadr label) gstrings (cddr label)))))
481 ;; identifier for missing data
482 (format dest "set datafile missing ~s~%" *missing-data-indicator*)
483 ;; user's commands; may overule any of the previous settings
484 (when (and (getf plot-options :gnuplot_postamble)
485 (> (length (getf plot-options :gnuplot_postamble)) 0))
486 (format dest "~a~%" (getf plot-options :gnuplot_postamble))))))
487 ;;returns a list with the name of the file created, or nil
488 (if (null (second terminal-file))
489 nil (list (second terminal-file)))))
491 (defmethod plot2d-command ((plot gnuplot-plot) fun options range)
492 ;; Compute points to plot for each element of FUN.
493 ;; If no plottable points are found, end with an error.
494 (let (points-lists)
495 (setq points-lists
496 (mapcar #'(lambda (f) (cdr (draw2d f range options))) (cdr fun)))
497 (when (= (count-if #'(lambda (x) x) points-lists) 0)
498 (merror (intl:gettext "plot2d: nothing to plot.~%")))
499 (setf
500 (slot-value plot 'data)
501 (concatenate
502 'string
503 (slot-value plot 'data)
504 (with-output-to-string (st)
505 (format st "plot")
506 (when (getf options :x)
507 (format st " [~{~g~^ : ~}]" (getf options :x)))
508 (when (getf options :y)
509 (unless (getf options :x)
510 (format st " []"))
511 (format st " [~{~g~^ : ~}]" (getf options :y)))
512 (let ((legend (getf options :legend))
513 (colors (getf options :color))
514 (types (getf options :point_type))
515 (styles (getf options :style))
516 (gstrings (if (getf options :gnuplot_strings) "" "noenhanced "))
517 (i 0) style plot-name)
518 (unless (listp legend) (setq legend (list legend)))
519 (unless (listp colors) (setq colors (list colors)))
520 (unless (listp styles) (setq styles (list styles)))
521 (loop for v in (cdr fun) for points-list in points-lists do
522 (when points-list
523 (if styles
524 (setq style (nth (mod i (length styles)) styles))
525 (setq style nil))
526 (when ($listp style) (setq style (cdr style)))
527 (incf i)
528 ;; label the expression according to the legend,
529 ;; unless it is "false" or there is only one expression
530 (if (member :legend options)
531 (setq plot-name
532 (if (first legend)
533 (ensure-string
534 (nth (mod (- i 1) (length legend)) legend)) nil))
535 (if (= 2 (length fun))
536 (setq plot-name nil)
537 (progn
538 (setq
539 plot-name
540 (with-output-to-string (pn)
541 (cond ((atom v) (format pn "~a" ($sconcat v)))
542 ((eq (second v) '$parametric)
543 (format pn "~a, ~a"
544 ($sconcat (third v))
545 ($sconcat (fourth v))))
546 ((eq (second v) '$discrete)
547 (format pn "discrete~a" i))
548 (t (format pn "~a" ($sconcat v))))))
549 (when (> (length plot-name) 50)
550 (setq plot-name (format nil "fun~a" i))))))
551 (when (> i 1) (format st ","))
552 (format st " '-'")
553 (if plot-name
554 (format st " title ~s ~a" plot-name gstrings)
555 (format st " notitle "))
556 (format st (gnuplot-curve-style style colors types i)))))
557 ;; Parses points data
558 (format st "~%")
559 (loop for points-list in points-lists do
560 (when points-list
561 (let (in-discontinuity points)
562 (loop for (v w) on points-list by #'cddr
564 (cond ((eq v 'moveto)
565 ;; A blank line means a discontinuity
566 (if (null in-discontinuity)
567 (progn
568 (format st "~%")
569 (setq in-discontinuity t))))
571 (format st "~g ~g ~%" v w)
572 (setq points t)
573 (setq in-discontinuity nil))))
574 (if (and (null points)
575 (first (getf options :x)) (first (getf options :y)))
576 (format st "~g ~g ~%" (first (getf options :x))
577 (first (getf options :y)))))
578 (format st "e~%"))))))))
580 (defmethod plot3d-command ((plot gnuplot-plot) functions options titles)
581 (let ((i 0) fun xrange yrange lvars trans (n (length functions))
582 (gstrings (if (getf options :gnuplot_strings) "" "noenhanced")))
583 (setf
584 (slot-value plot 'data)
585 (concatenate
586 'string
587 (slot-value plot 'data)
588 (with-output-to-string ($pstream)
589 (format $pstream "~a"
590 (gnuplot-plot3d-command "-" (getf options :palette)
591 (getf options :gnuplot_curve_styles)
592 (getf options :color)
593 gstrings titles n))
594 ;; generate the mesh points for each surface in the functions stack
595 (dolist (f functions)
596 (setq i (+ 1 i))
597 (setq fun (first f))
598 (setq xrange (second f))
599 (setq yrange (third f))
600 (if ($listp fun)
601 (progn
602 (setq trans
603 ($make_transform `((mlist) ,(second xrange)
604 ,(second yrange) $z)
605 (second fun) (third fun) (fourth fun)))
606 (setq fun '$zero_fun))
607 (let*
608 ((x0 (third xrange))
609 (x1 (fourth xrange))
610 (y0 (third yrange))
611 (y1 (fourth yrange))
612 (xmid (+ x0 (/ (- x1 x0) 2)))
613 (ymid (+ y0 (/ (- y1 y0) 2))))
614 (setq lvars `((mlist) ,(second xrange) ,(second yrange)))
615 (setq fun (coerce-float-fun fun lvars))
616 ;; Evaluate FUN at the middle point of the range.
617 ;; Looking at a single point is somewhat unreliable.
618 ;; Call FUN with numerical arguments (symbolic arguments may
619 ;; fail due to trouble computing real/imaginary parts for
620 ;; complicated expressions, or it may be a numerical function)
621 (when (cdr ($listofvars (mfuncall fun xmid ymid)))
622 (mtell
623 (intl:gettext
624 "plot3d: expected <expr. of v1 and v2>, [v1,min,max], [v2,min,max]~%"))
625 (mtell
626 (intl:gettext
627 "plot3d: keep going and hope for the best.~%")))))
628 (let* ((pl
629 (draw3d
630 fun (third xrange) (fourth xrange) (third yrange)
631 (fourth yrange) (first (getf options :grid))
632 (second (getf options :grid))))
633 (ar (polygon-pts pl)))
634 (declare (type (cl:array t) ar))
635 (when trans (mfuncall trans ar))
636 (when (getf options :transform_xy)
637 (mfuncall (getf options :transform_xy) ar))
638 (output-points pl (first (getf options :grid)))
639 (format $pstream "e~%"))))))))
641 (defmethod plot-shipout ((plot gnuplot-plot) options &optional output-file)
642 (case (getf options :plot_format)
643 ($gnuplot
644 (let (file)
645 (setq file (plot-file-path (format nil "maxout~d.gnuplot" (getpid))))
646 (with-open-file (fl
647 #+sbcl (sb-ext:native-namestring file)
648 #-sbcl file
649 :direction :output :if-exists :supersede)
650 (format fl "~a" (slot-value plot 'data)))
651 (gnuplot-process options file output-file)
652 (cons '(mlist) (cons file output-file))))
653 ($gnuplot_pipes
654 (check-gnuplot-process)
655 ($gnuplot_reset)
656 (send-gnuplot-command (slot-value plot 'data))
657 (if output-file (cons '(mlist) output-file) nil))))