-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgplot.lisp
More file actions
2897 lines (2558 loc) · 107 KB
/
pgplot.lisp
File metadata and controls
2897 lines (2558 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; pgplot-cl version 1.1
;; information in README file
;; distribution site is http://www.geocities.com/pgplot_cl
(in-package pgplot)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; define class for a pgplot plot, with attributes included --
;; thus the attributes get carried around by a plot, and changing
;; one plot's attributes doesn't mess up other plots
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass pgplot ()
((name :reader get-name :accessor pgplot-name :initform "untitled" :initarg :name)
;; numerical device id of plot
(id :reader get-id :accessor pgplot-id :initarg :id)
;; is the device open?
(is-open :reader is-open :accessor pgplot-is-open :initform nil :initarg :is-open)
;; symbolic representation of device
(device :reader get-device :accessor pgplot-device :initform nil :initarg :device)
;; pgplot has a limited output file length, so for devices creating
;; output files, we need to have a temporary file and a final destination file.
;; At device closing, we copy from the temporary file to the final file.
(tmp-output-file :accessor pgplot-tmp-output-file :initform nil :initarg :tmp-output-file)
(output-file :accessor pgplot-output-file :initform nil :initarg :output-file)
;;
(character-font :reader get-character-font :accessor pgplot-character-font)
(character-height :reader get-character-height :accessor pgplot-character-height)
(color-index :reader get-color-index :accessor pgplot-color-index)
(fill-area-style :reader get-fill-area-style :accessor pgplot-fill-area-style)
(line-style :reader get-line-style :accessor pgplot-line-style)
(line-width :reader get-line-width :accessor pgplot-line-width)
(pen-position :reader get-pen-position :accessor pgplot-pen-position)
(arrow-head-style :reader get-arrow-head-style :accessor pgplot-arrow-head-style)
(hatching-style :reader get-hatching-style :accessor pgplot-hatching-style)
(clipping-state :reader get-clipping-state :accessor pgplot-clipping-state)
(text-background-color-index
:reader get-text-background-color-index
:accessor pgplot-text-background-color-index)
;;
(viewport :reader get-viewport :accessor pgplot-viewport) ;; '(XLEFT XRIGHT YBOT YTOP)
(window :reader get-window :accessor pgplot-window) ;; world coords '(x1 x2 y1 y2)
(aspect-ratio :reader get-aspect-ratio :accessor pgplot-aspect-ratio)
(width :reader get-width :accessor pgplot-width)
;;
(color-index-range :reader get-color-index-range :accessor pgplot-color-index-range)
;;
;; we use colormaps, rather than individual color indices
(colormap :reader get-colormap :accessor pgplot-colormap)
; (default-colormap :reader get-default-colormap :accessor pgplot-default-colormap)
;; last cursor position, or NULL if none
(last-curs-x :reader get-last-curs-x :accessor pgplot-last-curs-x :initform nil)
(last-curs-y :reader get-last-curs-y :accessor pgplot-last-curs-y :initform nil)
))
(defun pgplot-p (x)
(eq (type-of x) 'pgplot))
;; a colormap is three vectors of red,blue,green
;; determining color indices nos. 0..npts-1
;; we keep the first 16 at the default values
(defstruct colormap
(name nil) ;; a general name for this colormap
npoints ;; how many color points?
(color-alist nil) ;; an alist for the keyword-named colors in this map
;; single-float vectors for the actual colors
(red-vec nil :type (or null (simple-array single-float (*))))
(green-vec nil :type (or null (simple-array single-float (*))))
(blue-vec nil :type (or null (simple-array single-float (*))))
;; a list of the form eg '((:default 34 76) (:gray 34 76) (:rainbow 18 77) ...)
;; that delimits inclusive color ranges in the map, for making
;; images - each colormap should have a default range
(default-range :default) ;; which range to use by default
(named-ranges (list (list :default 0 16))))
;; name to color index mapping for default device - background/default
;; are black/white or white/black depending on device
(defvar *default-colormap-color-alist*
'((:BACKGROUND . 0) (:DEFAULT . 1) (:RED . 2) (:GREEN . 3) (:BLUE . 4)
(:CYAN . 5) (:MAGENTA . 6) (:YELLOW . 7) (:ORANGE . 8)
(:GREEN+YELLOW . 9) (:GREEN+CYAN . 10) (:BLUE+CYAN . 11)
(:BLUE+MAGENTA . 12) (:RED+MAGENTA . 13)
(:DARK-GREY . 14) (:LIGHT-GREY . 15) (:DARK-GRAY . 14) (:LIGHT-GRAY . 15)))
;; put the background color (at index 0) into index i, for the purpose
;; of having it as the first color of a colormap
(defun %put-background-color-at-index (cmap i)
(setf (aref (colormap-red-vec cmap) i)
(aref (colormap-red-vec cmap) 0))
(setf (aref (colormap-green-vec cmap) i)
(aref (colormap-green-vec cmap) 0))
(setf (aref (colormap-blue-vec cmap) i)
(aref (colormap-blue-vec cmap) 0)))
;; generate the default colormap for current device - 16<=nelem<=255 --
;; nelem>16 only if rest of colormap is to be filled in with other colors
;; fixme - currently uses 100 colors, which is OK for X11, but could
;; adjust automatically to number of available colors. also, our
;; color colormap is not so good - eg, no yellow
;; we would like to have more than 100 colors, but X11 device seems to support only
;; 100 (as determined by calling PGQCOL)
(defunL make-default-colormap ()
"make a default colormap with the normal colors, and a :grey
component"
(let* ((nelem 100) ;; ok for X
(rv (make-array nelem :element-type 'single-float :initial-element 0.0))
(gv (make-array nelem :element-type 'single-float :initial-element 0.0))
(bv (make-array nelem :element-type 'single-float :initial-element 0.0))
(cm (make-colormap
:name :default
:npoints nelem :color-alist *default-colormap-color-alist*
:red-vec rv :green-vec gv :blue-vec bv
)))
(loop for i from 0 below 16
do
(multiple-value-bind (r g b) (pgqcr i)
(setf (aref rv i) r)
(setf (aref gv i) g)
(setf (aref bv i) b)))
;; make the next highest ones be gray scale for a start - this will be changed
(insert-colormap-component cm 16 99 :grey
(lambda (x) (values x x x)))
cm ;; return value
))
;; insert a colormap component into indices - func is a function of
;; (i-imin)/(imax-imin) that returns the red,green,blue values for
;; this colormap, over a domain [0,1]. name is the keyword name of
;; this colormap - delete-overlapping-ranges tells us to
;; remove any other ranges that were overwritten by this insertiion
(defunL insert-colormap-component (cm imin imax name func &key
(delete-overlapping-ranges t)
(first-color-is-background nil))
(declare (type colormap cm)
(type (unsigned-byte 16) imin imax)
(type (function (float) (values float float float)) func))
(when (or (>= imin imax) (>= imax (colormap-npoints cm)))
(error "out of range colormap indices imin=~a imax=~a - must be in [~A,~A]" imin imax
0 (1- (colormap-npoints cm))))
;; if we want the first color to be backd (for purpose of blanking images)
(when first-color-is-background
(%put-background-color-at-index cm imin))
(loop
with imin* = (if first-color-is-background (1+ imin) imin)
with dx = (float (- imax imin))
with rv = (colormap-red-vec cm)
with gv = (colormap-green-vec cm)
with bv = (colormap-blue-vec cm)
for i of-type (unsigned-byte 16) from imin* to imax
for j of-type (unsigned-byte 16) from 0 to (- imax imin)
for x = (/ (float (- i imin)) dx)
do (multiple-value-bind (r g b) (funcall func x)
(setf (aref rv i) (coerce r 'single-float)
(aref gv i) (coerce g 'single-float)
(aref bv i) (coerce b 'single-float))))
;;
(when delete-overlapping-ranges
(setf (colormap-named-ranges cm)
(remove-if (lambda (range)
(not (or (>= (second range) imax)
(<= (third range) imin))))
(colormap-named-ranges cm))))
;;
(cond ((listp name)
(dolist (one-name name)
(push (list one-name imin imax) (colormap-named-ranges cm))))
(t
(push (list name imin imax) (colormap-named-ranges cm)))))
;; this function probably won't be used much except for user-custom colormaps,
;; since INSERT-COLORMAP handles the standard ones
(defgeneric modify-colormap (p imin imax colormap-name
&key color-function
first-color-is-background)
(:documentation "Insert a new colormap such that (COLOR-FUNCTION x) returns
(VALUES RED BLUE GREEN) where red,blue,green are in [0,1] and X
is the distance in the index range, also [0,1]. This operation
will clobber existing colormaps. Default colormap ranges are
Default Colors [0,15] - do not alter this to allow normal plotting
Others [16,99]
If FIRST-COLOR-IS-BACKGROUND is set, then the first color of the range
is set to be be the background color. This is useful for blanking images."))
(defmethodL modify-colormap ((p pgplot) imin imax colormap-name
&key
(color-function nil)
(first-color-is-background nil))
(declare (type (unsigned-byte 16) imin imax)
(type keyword colormap-name)
(type (or null (function (float) (values float float float)))
color-function))
(insert-colormap-component (pgplot-colormap p) imin imax
colormap-name color-function
:first-color-is-background first-color-is-background)
(activate-device p)
(set-colormap (pgplot-colormap p)))
;; set color map in active device to colormap cm
(defunL set-colormap (cm)
(dotimes (i (colormap-npoints cm))
(declare (type (unsigned-byte 16) i))
(pgscr i
(aref (colormap-red-vec cm) i)
(aref (colormap-green-vec cm) i)
(aref (colormap-blue-vec cm) i))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro with-viewport ((p xleft xright ybot ytop) &body body)
"Evaluate BODY with pgplot P's viewport temporarliy set to XLEFT XRIGHT
XBOT YTOP"
(let ((psym (gensym "p"))
(xleft-sym (gensym "xleft"))
(xright-sym (gensym "xright"))
(ybot-sym (gensym "ybot"))
(ytop-sym (gensym "ytop"))
(vp-sym (gensym "vp")))
`(let* ((,psym ,p)
(,xleft-sym ,xleft)
(,xright-sym ,xright)
(,ybot-sym ,ybot)
(,ytop-sym ,ytop)
;;
(,vp-sym (pgplot:get-viewport ,psym)))
;;
(pgplot:set-viewport ,psym ,xleft-sym ,xright-sym ,ybot-sym ,ytop-sym)
;;
,@body
(apply 'pgplot:set-viewport ,psym ,vp-sym))))
(defmacro with-window ((p x0 x1 y0 y1) &body body)
"Evaluate BODY with pgplot P's viewport temporarily set to X0 X1
YO Y1"
(let ((psym (gensym "p"))
(x0-sym (gensym "x0"))
(x1-sym (gensym "x1"))
(y0-sym (gensym "y0"))
(y1-sym (gensym "y1"))
(win-sym (gensym "win")))
`(let* ((,psym ,p)
(,x0-sym ,x0)
(,x1-sym ,x1)
(,y0-sym ,y0)
(,y1-sym ,y1)
;;
(,win-sym (pgplot:get-window ,psym)))
;;
(pgplot:set-window ,psym ,x0-sym ,x1-sym ,y0-sym ,y1-sym)
;;
,@body
(apply 'pgplot:set-window ,psym ,win-sym))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; now the higher level (user) versions of the pgplot routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *current-pgplot* nil
"The current active pgplot device; ie, the last device used")
(defvar *pgplot-list* nil "List of all alive PGPLOT devices.")
;; how does one get this directly from pgplot?
(defconstant +max-pgplot-open-devices+ 8)
(defparameter *pgplot-devices-alist*
;; name str need-filename? nil, t, or :maybe
'((:XWINDOW "/xwindow" nil)
(:X "/xwindow" nil)
(:X11 "/xwindow" nil)
;; xs resizable persistent window but it doesn't seem to resizable
(:X11S "/xserve" nil)
(:NULL "/null" nil)
(:GIF "/gif" t)
(:VGIF "/vgif" t)
(:PSLAND "/cps" t) ;; we use only color postscript
(:PSPORT "/vcps" t)
(:PS "/cps" t) ;; square postscript
#+pgplot-does-pdf
(:PDF "/cps" t))) ;; square postscript
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; functions to handle temporary output files
(defun %random-string (n)
(loop with abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
with outstr = (make-string n)
with m = (length abc)
for i below n
do (setf (aref outstr i) (aref abc (random m)))
finally (return outstr)))
;; return a filename that has a unique temporary file
(defun %make-temp-output-file (&key (dir "/tmp"))
(flet ((make-random-outfile-name ()
(format nil "~A/PGPLOT_tmp_~A" dir (%random-string 10))))
(loop
for file = (make-random-outfile-name)
for sout = (ignore-errors
(open file :direction :output :if-exists :error :if-does-not-exist :create))
until sout
finally
(write-line "Placeholder for temporary pgplot output file." sout)
(close sout)
(return file))))
;; check that pgplot output file is writable
(defun %verify-output-file (file)
(let ((sout (ignore-errors
(open file :direction :output :if-exists :supersede :if-does-not-exist :create))))
(when (not sout)
(error "Could not make pgplot output file ~A" file))
(write-line "Placeholder for temporary pgplot output file." sout)
(close sout)
t))
(defun %copy-file (infile outfile &key (block-size 32768))
(with-open-file (sin infile :direction :input :element-type '(unsigned-byte 8))
(with-open-file (sout outfile :direction :output
:if-does-not-exist :create ;; could have :ERROR
:if-exists :supersede ;; it will exist
:element-type '(unsigned-byte 8))
(loop
with buff = (make-array block-size :element-type '(unsigned-byte 8))
for n = (read-sequence buff sin)
until (zerop n)
do (write-sequence buff sout :end n)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defunL open-device (device &key
(plot-name "untitled") (filename nil)
(font :roman)
(colormap :viridis) (first-color-is-background nil)
(square nil)
(aspect-ratio)
(height)
(width)
(invert nil))
"open a pgplot device, where DEVICE is one of
:X11 :NULL :GIF :VGIF :PSLAND :PSPORT :PS.
PLOT-NAME is an optional name
FILENAME is the file to which to write if device type chosen targets file
FONT is the font to use
COLORMAP is one of the colormaps supported by INSERT-COLORMAP and
FIRST-COLOR-IS-BACKGROUND determines if the first color is set to background
SQUARE is a flag that tries to make the final plot square
ASPECT-RATIO tries to set the aspect ratio (over-rides SQUARE)
1 means square; <1 means horizontal rectangle; >1 means vertical rectangle
HEIGHT-WIDTH tries to set the height and width (mm), and overrides ASPECT-RATIO
INVERT inverts normal color scheme"
(if (>= (length *pgplot-list*) +max-pgplot-open-devices+)
(error "pgplot supports only ~A simultaneously open devices.
You must close one or more devices before making more plots
[eg, use (pgplot:close-all-devices) ] "
+max-pgplot-open-devices+))
(let* ((dev-list (assoc device *pgplot-devices-alist*))
(dev-str (second dev-list))
(needs-file (third dev-list))
(tmp-output-file (when (and needs-file filename
(%verify-output-file filename))
(%make-temp-output-file)))
(dev-id 0)
(p nil))
;;
(if (not dev-str)
(error "No device ~A -- Available pgplot devices are ~A"
device (map 'list #'car *pgplot-devices-alist*)))
(if (and needs-file (not filename))
(error "Need to specify :FILENAME for device ~A" device))
(if (and (not needs-file) filename)
(error "Device ~A does not take a :FILENAME" device))
;;
;;
#+nil
(if (member device '(:gif :ps)) ;; these end up messed up if not :SQUARE
(setf square t))
(setf dev-id (pgopen-raw (concatenate 'string (or tmp-output-file "") dev-str)))
;;
(setf p (make-instance 'pgplot :id dev-id
:device device
:tmp-output-file tmp-output-file
:output-file filename
:name plot-name))
;; mark device as open
(setf (pgplot-is-open p) t)
;;
;; and set defaults in p, but don't call pgplot routines yet
(setf (pgplot-character-font p) 1) ;; default - changed below
(setf (pgplot-character-height p) 1.7) ;; biggish
(setf (pgplot-color-index p) 1)
(setf (pgplot-fill-area-style p) 2) ;; OUTLINE
(setf (pgplot-line-style p) 1) ;; full line
(setf (pgplot-line-width p) 2) ;; double minimal thickness
(setf (pgplot-pen-position p) '(0.0 0.0)) ;; x,y
(setf (pgplot-arrow-head-style p) '(2 45.0 0.4)) ;; FS ANGLE BARB
(setf (pgplot-hatching-style p) '(45.0 2.0 0.0)) ;; ANGLE SEPN PHASE
(setf (pgplot-clipping-state p) 1) ;; CLIPPING ENABLED
(setf (pgplot-text-background-color-index p) -1)
(setf (pgplot-viewport p) '(0.20 0.95 0.15 0.90)) ;; what looks good
(setf (pgplot-window p) '(0.0 1.0 0.0 1.0))
(setf (pgplot-colormap p) (make-default-colormap))
;;(set-colormap (pgplot-default-colormap p)) ;; sets a default gray colormap
;; insert-colormap is in next file, so we funcall it to avoid compiler complaints
(setf (pgplot-color-index-range p)
(list 1 (colormap-npoints (pgplot-colormap p))))
(funcall 'insert-colormap p colormap :first-color-is-background first-color-is-background)
;;
(push p *pgplot-list*) ;; add to global list
(pgask 0) ;; always have this - having to hit RET is annoying
(let ((ar (cond ((and square aspect-ratio
(not (= aspect-ratio 1)))
(error "ASPECT-RATIO=~A contradicts SQUARE" aspect-ratio))
(square 1.0)
(t aspect-ratio)))
(w 0.0))
(cond ((and height width aspect-ratio)
(error "Can't set WIDTH, HEIGHT, and ASPECT-RATIO"))
((and height width)
(setf ar (* 1.0 (/ height width))))
((and aspect-ratio height)
(setf w (/ height aspect-ratio)))
((and aspect-ratio width)
(setf w (* 1.0 width)))
(aspect-ratio ;; no width given
(setf w 0.0))
((or height width)
(error "WIDTH or HEIGHT given but not ASPECT-RATIO")))
(when ar ;; maybe no dimensions given
(pgpap (/ w 25.4) ar)) ;; convert w in mm to inches
;; new page to reset size
(when (not (zerop w)) (pgpage)))
;; set aspect ratio
(multiple-value-bind (x1 x2 y1 y2) (pgqvsz 1)
(declare (ignorable x1 y1))
(setf (pgplot-width p) (* 25.4 x2)) ;; inches to mm
(setf (pgplot-aspect-ratio p) (/ y2 x2)))
(activate-device p) ;; make current active device match this open dev
(if invert
(progn
(set-colormap-color p 0 1 1 1)
(set-colormap-color p 1 0 0 0)))
(set-character-font p font)
;; eliminate the worst colormap flashing for x devices by erasing
;; first
(if (member device '(:XWINDOW :X :X11))
(erase p))
;;
p))
;; close device, ensuring that current device remains set
(defgeneric close-device (p)
(:documentation "close a pgplot device object"))
(defmethodL close-device ((p pgplot))
(if (pgplot-is-open p) ;; ignore closed devices
(progn
(setf (pgplot-is-open p) nil) ;; mark device as closed
(let ((old-dev (if *current-pgplot* (pgplot-id *current-pgplot*) nil))
(this-dev (pgplot-id p))
(ps-success nil)) ;; postscript failed - special case
(pgslct this-dev)
(pgask 0) ;; disable the silly return key requirement
(pgupdt)
(pgclos)
(when (pgplot-output-file p)
;; put the bounding box in FRONT of the file
(when (member (pgplot-device p) '(:pdf :ps))
(setf ps-success
(fix-ps-bounding-box (pgplot-tmp-output-file p) :error-on-fail nil)))
;; maybe convert ps to pdf, if device is :PDF
#+pgplot-does-pdf
(if (and (eq (pgplot-device p) :pdf) ;; tmpfile must be a .ps
ps-success)
(ps2pdf:ps2pdf (pgplot-tmp-output-file p)
:outfile (pgplot-output-file p)
:orientation :seascape) ;; fixes orientation we hope
(%copy-file (pgplot-tmp-output-file p) (pgplot-output-file p)))
#-pgplot-does-pdf ;; the output file must be the correct type
(%copy-file (pgplot-tmp-output-file p) (pgplot-output-file p))
(delete-file (pgplot-tmp-output-file p)))
;; restore the old device, if it was not the new device
(if (and old-dev (not (= this-dev old-dev)))
(pgslct old-dev))
(setf *pgplot-list* (remove p *pgplot-list*))
t
))
nil))
(defunL close-all-devices (&key (force nil))
"Close all open devices contained in pgplot::*pgplot-list*.
FORCE option causes the close to be on low device level, iterating
through all device numbers, in case normal closing causes problems."
(if (not force)
;; nice close
(dolist (p *pgplot-list*) (close-device p))
;; low level close that doesn't clean up
(loop for idev from 0 to 7 ;; 8 devices total
do (pgslct idev)
(pgask 0)
(pgclos)))
(setf *pgplot-list* nil)) ;; just in case a closed dev was on list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-device-geometry (p width height)
(:documentation "Set the window height to WIDTH x HEIGHT in mm"))
(defmethodL set-device-geometry ((p pgplot) (width real) (height real))
(activate-device p)
(pgpap (float (/ width 25.4) 1.0)
(float (/ height width) 2.0))
(pgpage))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; activate a device p -- ie, if p is not the active device in
;; *current-pgplot*, make p be the active dev. and set up the
;; active dev's parameters to be p's
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric activate-device (p &key force-reset)
(:documentation "Make pgplot device P the active one, and enable its settings."))
;;
(defmethodL activate-device ((p pgplot) &key (force-reset nil))
(cond ((not (pgplot-is-open p))
(error "Tried to activate a closed pgplot device"))
((and (eq p *current-pgplot*) (not force-reset))
t) ;; already open
(t ;; else restore settings
(setf *current-pgplot* p)
(pgslct (pgplot-id p))
(pgscf (pgplot-character-font p))
(pgsch (pgplot-character-height p))
(pgstbg (pgplot-text-background-color-index p))
(pgsci (pgplot-color-index p))
(pgsfs (pgplot-fill-area-style p))
(pgsls (pgplot-line-style p))
(pgslw (pgplot-line-width p))
(apply #'pgmove (pgplot-pen-position p))
(apply #'pgsah (pgplot-arrow-head-style p))
(apply #'pgshs (pgplot-hatching-style p))
(apply #'pgsvp (pgplot-viewport p))
(apply #'pgswin (pgplot-window p))
(pgsclp (pgplot-clipping-state p))
(set-colormap (pgplot-colormap p))
(apply #'pgscir (pgplot-color-index-range p))
t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; at the end of routines that move the pen, we call
;; fix-pen-position to update our remembered pen position
;; for this device (fix-pen-position is internal to this package)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defunL fix-pen-position (p)
(multiple-value-bind (x y) (pgqpos)
(setf (pgplot-pen-position p) (list x y))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *character-font-alist* '((:DEFAULT . 1) (:ROMAN . 2) (:ITALIC . 3)
(:SCRIPT . 4)))
(defgeneric set-character-font (p font)
(:documentation
"set the current character font of device P, where font
is either an integer 1-4, or one of :DEFAULT :ROMAN :ITALIC :SCRIPT"))
(defmethodL set-character-font ((p pgplot) (font integer))
(if (or (< font 1) (> font 4))
(error "Bad font number ~A" font))
(setf (pgplot-character-font p) font)
(if (eq p *current-pgplot*)
(pgscf font))
t)
;;
(defmethodL set-character-font ((p pgplot) (font symbol))
(let ((nf (cdr (assoc font *character-font-alist* :test 'string-equal))))
(if (not nf)
(error
"Invalid font ~A -- Available fonts are ~A"
font (map 'list #'car *character-font-alist*)))
(set-character-font p nf)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-character-height (p height)
(:documentation
"set the character height of pgplot device P to a number.
Default character height is 2.0"))
(defmethodL set-character-height ((p pgplot) (height number))
(if (or (< height 0.0) (> height 50))
(error "Bad font number ~A -- want 0<height<50" height))
(setf (pgplot-character-height p) height)
(if (eq p *current-pgplot*)
(pgsch height))
t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-text-background-color (p c)
(:documentation
"set the text background color of pgplot device P to
C, where C is either an integer from the colormap, or a
symbol for named colors"))
(defmethodL set-text-background-color ((p pgplot) (ci integer))
(setf (pgplot-text-background-color-index p) ci)
(if (eq p *current-pgplot*)
(pgstbg ci))
t)
;;
(defmethodL set-text-background-color ((p pgplot) (cs symbol))
(let* ((c-alist (cons '(:TRANSPARENT . -1) ;; add special transparent color
(colormap-color-alist (pgplot-colormap p))))
(ci (cdr (assoc cs c-alist :test 'string-equal))))
(if (not ci)
(error "Color ~A not available. Available colors are: ~A"
cs (map 'list #'car c-alist)))
(set-text-background-color p ci)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-color (p c)
(:documentation
"set the plotting color of pgplot device P to
C, where C is either an integer from the colormap, or a
symbol for named colors"))
(defmethodL set-color ((p pgplot) (ci integer))
(setf (pgplot-color-index p) ci)
(if (eq p *current-pgplot*)
(pgsci ci))
t)
(defmethodL set-color ((p pgplot) (cs symbol))
(let ((ci (cdr (assoc cs (colormap-color-alist (pgplot-colormap p))
:test 'string-equal))))
(if (not ci)
(error "Color ~A not available. Available colors are: ~A"
cs (map 'list #'car
(colormap-color-alist (pgplot-colormap p)))))
(set-color p ci)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *fill-area-style-alist* '((:SOLID . 1) (:OUTLINE . 2)
(:HATCHED . 3) (:CROSS-HATCHED . 4)))
(defgeneric set-fill-area-style (p s)
(:documentation
"set the fill area style of pgplot device P to
S, where S is either an integer or one of
:SOLID :OUTLINE :HATCHED :CROSS-HATCHED"))
(defmethodL set-fill-area-style ((p pgplot) (style integer))
(if (or (< style 0) (> style 4))
(error "out of range fill area style ~A" style))
(setf (pgplot-fill-area-style p) style)
(if (eq p *current-pgplot*)
(pgsfs style))
t)
(defmethodL set-fill-area-style ((p pgplot) (style symbol))
(let ((ns (cdr (assoc style *fill-area-style-alist* :test 'string-equal))))
(if (not ns)
(error "Invalid fill-area style ~A -- Available styles are ~A"
style (map 'list #'car *fill-area-style-alist*)))
(set-fill-area-style p ns)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *linestyle-alist* '((:SOLID . 1) (:DASHED . 2) (:DASH-DOT . 3)
(:DOTTED . 4) (:DASH-DOT-DOT-DOT . 5)))
(defgeneric set-line-style (p s)
(:documentation
"set the line style of pgplot device P to
S, where S is either an integer or one of
:SOLID :DASHED :DASHED-DOT :DOTTED :DASH-DOT-DOT-DOT"))
;;
(defmethodL set-line-style ((p pgplot) (ls integer))
(if (or (< ls 1) (> ls 5))
(error "out of range line style ~A" ls))
(setf (pgplot-line-style p) ls)
(if (eq p *current-pgplot*)
(pgsls ls))
t)
;;
(defmethodL set-line-style ((p pgplot) (lss symbol))
(let ((ls (cdr (assoc lss *linestyle-alist* :test 'string-equal))))
(if (not ls)
(error "Invalid line style ~A -- Available styles are ~A"
lss (map 'list #'car *linestyle-alist*)))
(set-line-style p ls)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-line-width (p lw)
(:documentation
"set the line width pgplot device P to
integer value LW"))
(defmethodL set-line-width ((p pgplot) (lw integer))
(if (or (< lw 1) (> lw 1000))
(error "out of range line width ~A -- expect 1<=width<=1000" lw))
(setf (pgplot-line-width p) lw)
(if (eq p *current-pgplot*)
(pgslw lw))
t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-arrow-head-style (p fs angle barb)
(:documentation
"set the arrow head style of pgplot. FS can be 1, 2, :FILLED,
:OUTLINE. ANGLE is the angle of the tip; BARB is the fraction of the
triangular arrow-head that is cut away from the back."))
(defmethodL set-arrow-head-style
((p pgplot) fs (angle real) (barb real))
(cond ((eq fs :FILLED) (setq fs 1))
((eq fs :OUTLINE) (setq fs 2))
((and (integerp fs) (or (= fs 1) (= fs 2))) t)
(t
(error
"Invalid arrow head style ~A. Available styles are ~A"
fs '(:FILLED :OUTLINE 1 2))))
(setf angle (float angle 1.0))
(setf barb (float barb 1.0))
(setf (pgplot-arrow-head-style p) (list fs angle barb))
(if (eq p *current-pgplot*)
(pgsah fs angle barb))
t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric set-hatching-style
(p angle sepn phase)
(:documentation
"set hatching style of pgplot device P.
ANGLE is angle wrt horizontal. SEPN is separation in
units of the view surface. PHASE in [0,1] is the offset
within the separation"))
(defmethodL set-hatching-style ((p pgplot) (angle real)
(sepn real) (phase real))
(setf (pgplot-hatching-style p) (list angle sepn phase))
(if (eq p *current-pgplot*)
(pgshs angle sepn phase))
t)
(defgeneric set-clipping-state (p state)
(:documentation
"set clipping state of pgplot device P. STATE=1 means clip at
edge of viewport, 0 means don't clip"))
(defmethodL set-clipping-state ((p pgplot) (state integer))
(cond ((or (= state 0) (= state 1)) t) ;; OK
(t (error "Bad state in set-clipping-state")))
(if (or (eq state t) (= state 1)) (setq state 1) (setq state 0))
(setf (pgplot-clipping-state p) state)
(if (eq p *current-pgplot*)
(pgsclp state))
t)
(defmacro with-pgplot-settings
((pgplot &key
(font nil font%)
(character-height nil character-height%)
(text-background-color nil text-background-color%)
(color nil color%)
(fill-area-style nil fill-area-style%)
(line-style nil line-style%)
(line-width nil line-width%)
(arrow-head-style nil arrow-head-style%);; list (fs angle barb)
(hatching-style nil hatching-style%);; list (angle sepn phase)
(clipping-state nil clipping-state%))
&body body)
"Macro to run BODY with pgplot's properties temporarily
set to given values.
eg draw a blue box
(with-pgplot-settings (pgplot :color :blue)
(pgplot:box pgplot))
available settings are
:FONT :CHARACTER-HEIGHT :TEXT-BACKGROUND-COLOR :COLOR
:FILL-AREA-STYLE :LINE-STYLE :CLIPPING-STATE
:ARROW-HEAD-STYLE (a list of fs,angle,barb)
:HATCHING-STYLE (a list of angle,sepn,phase)
whenever NIL is passed as as value (eg :font nil) in the
macroexpanded code at runtime, then this item is neither saved
nor restored, it being assumed that no value was specified.
"
(let ((p (gensym "pgplot"))
(sym-old-character-font
(when font% (gensym "old-character-font")))
(sym-old-character-height
(when character-height% (gensym "old-character-height")))
(sym-old-text-background-color
(when text-background-color (gensym "old-text-background-color")))
(sym-old-color
(when color% (gensym "old-color")))
(sym-old-fill-area-style
(when fill-area-style% (gensym "old-fill-area-style")))
(sym-old-line-style
(when line-style% (gensym "old-line-style")))
(sym-old-line-width
(when line-width% (gensym "old-line-width")))
(sym-old-arrow-head-style
(when arrow-head-style% (gensym "old-arrow-head-style")))
(sym-old-hatching-style
(when hatching-style% (gensym "old-hatching-style")))
(sym-old-clipping-state
(when clipping-state% (gensym "old-clipping-state")))
;;
(sym-font
(when font% (gensym "character-font")))
(sym-character-height
(when character-height% (gensym "character-height")))
(sym-text-background-color
(when text-background-color% (gensym "text-background-color")))
(sym-color
(when color% (gensym "color")))
(sym-fill-area-style
(when fill-area-style% (gensym "fill-area-style")))
(sym-line-style
(when line-style% (gensym "line-style")))
(sym-line-width
(when line-width% (gensym "line-width")))
(sym-arrow-head-style
(when arrow-head-style% (gensym "arrow-head-style")))
(sym-hatching-style
(when hatching-style% (gensym "hatching-style")))
(sym-clipping-state
(when clipping-state% (gensym "clipping-state"))))
`(let*
,(append
`((,p ,pgplot)) ;; bind pgplot to new var in case it is a complex form
;;
;; sym-font is variable evaluated version of FONT, to
;; prevent multiple-evaluation of FONT form.
;; sym-old-character-font is a variable for saving old version
(when font%
`((,sym-font ,font)
(,sym-old-character-font (when ,sym-font (pgplot-character-font ,p)))))
(when character-height%
`((,sym-character-height ,character-height)
(,sym-old-character-height
(when ,sym-character-height (pgplot-character-height ,p)))))
(when text-background-color%
`((,sym-text-background-color ,text-background-color)
(,sym-old-text-background-color
(when ,sym-text-background-color
(pgplot-text-background-color-index ,p)))))
(when color%
`((,sym-color ,color)
(,sym-old-color (when ,sym-color (pgplot-color-index ,p)))))
(when fill-area-style%
`((,sym-fill-area-style ,fill-area-style)
(,sym-old-fill-area-style
(when ,sym-fill-area-style (pgplot-fill-area-style ,p)))))
(when line-style%
`((,sym-line-style ,line-style)
(,sym-old-line-style
(when ,sym-line-style (pgplot-line-style ,p)))))
(when line-width%
`((,sym-line-width ,line-width)
(,sym-old-line-width
(when ,sym-line-width (pgplot-line-width ,p)))))
(when arrow-head-style%
`((,sym-arrow-head-style ,arrow-head-style)
(,sym-old-arrow-head-style
(when ,sym-arrow-head-style (pgplot-arrow-head-style ,p)))))
(when hatching-style%
`((,sym-hatching-style ,hatching-style)
(when ,sym-hatching-style (hatching-style (pgplot-hatching-style ,p)))))
(when clipping-state%
`((,sym-clipping-state ,clipping-state)
(clipping-state (when ,sym-clipping-state (pgplot-clipping-state ,p)))))
)
(unwind-protect
(progn
,@(append
(when font%
`((when ,sym-font (set-character-font ,p ,sym-font))))
(when character-height%
`((when ,sym-character-height
(set-character-height ,p ,sym-character-height))))
(when text-background-color%
`((when ,sym-text-background-color
(set-text-background-color ,p ,sym-text-background-color))))
(when color%
`((when ,sym-color (set-color ,p ,sym-color))))
(when fill-area-style%
`((when ,sym-fill-area-style
(set-fill-area-style ,p ,sym-fill-area-style))))
(when line-style%
`((when ,sym-line-style (set-line-style ,p ,sym-line-style))))
(when line-width%
`((when ,sym-line-width (set-line-width ,p ,sym-line-width))))
(when arrow-head-style% ;; this one takes several args in list
`((when ,sym-arrow-head-style
(apply 'set-arrow-head-style ,p ,sym-arrow-head-style))))
(when hatching-style% ;; this one takes several args in list
`((when ,sym-hatching-style
(apply set-hatching-style ,p ,sym-hatching-style))))
(when clipping-state%
`((when ,sym-clipping-state
(set-clipping-state ,p ,sym-clipping-state)))))
;; now run the body
,@body)
;; and restore the old values
(progn
,@(append
(when font
`((when ,sym-font (set-character-font ,p ,sym-old-character-font))))
(when character-height%
`((when ,sym-character-height
(set-character-height ,p ,sym-old-character-height))))
(when text-background-color%
`((when ,sym-text-background-color
(set-text-background-color
,p ,sym-old-text-background-color))))
(when color%
`((when ,sym-color
(set-color ,p ,sym-old-color))))
(when fill-area-style%
`((when ,sym-fill-area-style
(set-fill-area-style ,p ,sym-old-fill-area-style))))
(when line-style%
`((when ,sym-line-style
(set-line-style ,p ,sym-old-line-style))))
(when line-width%
`((when ,sym-line-width
(set-line-width ,p ,sym-old-line-width))))
(when arrow-head-style% ;; this one takes several args in list
`((when ,sym-arrow-head-style
(apply 'set-arrow-head-style ,p ,sym-old-arrow-head-style))))
(when hatching-style% ;; this one takes several args in list
`((when ,sym-hatching-style
(apply set-hatching-style ,p ,sym-old-hatching-style))))
(when clipping-state%
`((when ,sym-clipping-state
(set-clipping-state ,p ,sym-old-clipping-state)))))
)))))
(defgeneric set-color-index-range (p ilo ihi)
(:documentation