-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots.scm
144 lines (121 loc) · 4.84 KB
/
plots.scm
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
;;; This file is part of Adaptive Plot, a library for intelligently
;;; plotting functions from the MIT Scheme REPL.
;;; Copyright (C) 2010-2011, 2013 Alexey Radul
;;;
;;; Adaptive Plot is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; Adaptive Plot is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with Adaptive Plot. If not, see
;;; <http://www.gnu.org/licenses/>.
(declare (usual-integrations))
;;;; Plots
;;; A plot is a thing that knows what it's trying to plot, the x-y box
;;; in which it's trying to plot it (which may adapt to the data), the
;;; resolution within that box that it's trying to achieve, and the
;;; points it has so far. A plot may also know a Scheme window into
;;; which to draw points as they appear, and updated line-drawings
;;; from time to time.
;;; I am using setters rather than updaters because the window is an
;;; imperative structure anyway, and has to be kept in sync with the
;;; known-points list, so a persistent (in the Sleator and Tarjan
;;; sense) plot object doesn't make much sense. And the mutators all
;;; return the plot, so you can chain them just the same.
(define-structure
(plot safe-accessors (constructor %make-plot))
xresolution
yresolution
xlow
xhigh
ylow
yhigh
known-points
point-source
window
dimensions-cache)
(define *plot-x-res* 1200)
(define *plot-y-res* 960)
(define (make-plot point-source)
(%make-plot
*plot-x-res* *plot-y-res* #!default #!default #!default #!default
(empty-point-set) point-source #f #f))
(define (plot-clear-dimensions-cache! plot)
(set-plot-dimensions-cache! plot #f)
plot)
(define (plot-pixels plot)
(* (plot-xresolution plot) (plot-yresolution plot)))
(define (plot-resize! plot #!optional new-xlow new-xhigh new-ylow new-yhigh)
(plot-resize-x! plot new-xlow new-xhigh)
(plot-resize-y! plot new-ylow new-yhigh))
(define (plot-resize-x! plot #!optional new-xlow new-xhigh)
(plot-clear-dimensions-cache! plot)
(set-plot-xlow! plot new-xlow)
(set-plot-xhigh! plot new-xhigh)
plot)
(define (plot-resize-y! plot #!optional new-ylow new-yhigh)
(plot-clear-dimensions-cache! plot)
(set-plot-ylow! plot new-ylow)
(set-plot-yhigh! plot new-yhigh)
plot)
(define (plot-new-resolution! plot xres yres)
(set-plot-xresolution! plot xres)
(set-plot-yresolution! plot yres)
plot)
(define (plot-compute-dimensions plot)
(let ((relevant-points (plot-relevant-points plot)))
(define (... thing compute)
(if (default-object? thing)
(compute relevant-points)
thing))
(let ((xlow (... (plot-xlow plot) xmin))
(xhigh (... (plot-xhigh plot) xmax))
(ylow (... (plot-ylow plot) ymin))
(yhigh (... (plot-yhigh plot) ymax)))
(values xlow xhigh ylow yhigh))))
(define plot-dimensions
(slot-memoize plot-compute-dimensions
plot-dimensions-cache set-plot-dimensions-cache! #f))
(define (plot-known-points-alist plot)
(point-set->alist (plot-known-points plot)))
(define (plot-relevant-points plot)
(range-query-2d (plot-known-points plot)
(plot-xlow plot) (plot-xhigh plot) (plot-ylow plot) (plot-yhigh plot)))
(define (plot-relevant-points-alist plot)
(point-set->alist (plot-relevant-points plot)))
(define (plot-learn-point! plot x y)
(set-plot-known-points!
plot (point-set-insert (plot-known-points plot) x y))
(receive (xlow xhigh ylow yhigh) (plot-dimensions plot)
(if ((in-box? xlow xhigh ylow yhigh) (cons x y))
plot
(plot-clear-dimensions-cache! plot))))
(define (plot-learn-point-set! plot points)
(set-plot-known-points!
plot (point-set-union (plot-known-points plot) points))
;; TODO Test whether all the new points are in the old dimensions?
(plot-clear-dimensions-cache! plot))
(define (plot-ensure-initialized! plot)
(define (ensure-x-point-known! x-value)
(if (> (length (range-query-2d (plot-known-points plot) x-value x-value)) 0)
'ok
(plot-learn-point! plot x-value ((plot-point-source plot) x-value))))
(if (default-object? (plot-xlow plot))
(ensure-x-point-known! -1.)
(ensure-x-point-known! (plot-xlow plot)))
(if (default-object? (plot-xhigh plot))
(ensure-x-point-known! 1.)
(ensure-x-point-known! (plot-xhigh plot)))
plot)
(define (plot-count plot)
(length (plot-known-points plot)))
(define (counting-used-points plot thunk)
(let ((known-already (plot-count plot)))
(thunk)
(- (length (plot-known-points plot)) known-already)))