Skip to content

Commit c412600

Browse files
authored
Update plot functions to return information about the plot (#90, #83)
The `Plot-Metrics<%>` interface implements functions to query information about the plot. Currently, `get-plot-bounds`, `plot->dc` and `dc->plot` methods are implemented. Objects returned by plot/dc, `plot-bitmap` and `plot-snip` implement this interface in addition to their respective types (e.g. a `bitmap%` for `plot-bitmap` and `snip%` for `plot-snip`). `plot-pict` returns a `pict` which is extended with these new interface functions as well.
1 parent c457141 commit c412600

File tree

20 files changed

+417
-79
lines changed

20 files changed

+417
-79
lines changed

plot-gui-lib/plot/private/gui/lazy-snip-typed.rkt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
(provide make-2d-plot-snip
88
make-3d-plot-snip
9-
make-snip-frame)
9+
make-snip-frame
10+
Plot-Snip%)
1011

1112
(type-environment
1213
[make-2d-plot-snip (parse-type #'Make-2D-Plot-Snip)]

plot-gui-lib/plot/private/gui/lazy-snip-types.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
(provide (all-defined-out))
77

8+
(define-type Plot-Snip% (Class #:implements Snip% #:implements Plot-Metrics<%>))
9+
810
(define-type Make-2D-Plot-Snip
911
(-> (Instance Bitmap%)
1012
Plot-Parameters
@@ -14,7 +16,7 @@
1416
(U #f (Instance 2D-Plot-Area%))
1517
Positive-Integer
1618
Positive-Integer
17-
(Instance Snip%)))
19+
(Instance Plot-Snip%)))
1820

1921
(define-type Make-3D-Plot-Snip
2022
(-> (Instance Bitmap%)
@@ -26,7 +28,7 @@
2628
(U #f (Instance 3D-Plot-Area%))
2729
Positive-Integer
2830
Positive-Integer
29-
(Instance Snip%)))
31+
(Instance Plot-Snip%)))
3032

3133
(define-type Make-Snip-Frame
3234
(-> (-> Positive-Integer Positive-Integer (Instance Snip%))

plot-gui-lib/plot/private/gui/plot2d.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
#:y-label (U String pict #f)
6969
#:aspect-ratio (U Nonnegative-Real #f)
7070
#:legend-anchor Legend-Anchor]
71-
(Instance Snip%)))
71+
(Instance Plot-Snip%)))
7272
(define (plot-snip renderer-tree
7373
#:x-min [x-min #f] #:x-max [x-max #f]
7474
#:y-min [y-min #f] #:y-max [y-max #f]

plot-gui-lib/plot/private/gui/plot3d.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#:z-label (U String pict #f)
4747
#:aspect-ratio (U Nonnegative-Real #f)
4848
#:legend-anchor Legend-Anchor]
49-
(Instance Snip%)))
49+
(Instance Plot-Snip%)))
5050
(define (plot3d-snip renderer-tree
5151
#:x-min [x-min #f] #:x-max [x-max #f]
5252
#:y-min [y-min #f] #:y-max [y-max #f]

plot-gui-lib/plot/private/gui/snip2d.rkt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
plot/private/plot2d/renderer
1414
plot/private/no-gui/plot2d-utils
1515
plot/private/common/contract
16+
(submod plot/private/common/plotmetrics untyped)
1617
"worker-thread.rkt"
1718
"snip.rkt")
1819

@@ -35,7 +36,7 @@
3536
(struct draw-command (animating? plot-bounds-rect width height) #:transparent)
3637

3738
(define 2d-plot-snip%
38-
(class plot-snip%
39+
(class* plot-snip% (plot-metrics<%>)
3940
(init init-bm saved-plot-parameters)
4041
(init-field make-bm plot-bounds-rect area width height)
4142

@@ -140,6 +141,7 @@
140141
(worker-thread-try-get rth (λ () (values #f #f))))
141142
(cond [(is-a? new-bm bitmap%)
142143
(set! area new-area)
144+
(set! plot-metrics-ok? #f)
143145
(set-bitmap new-bm)
144146
(set-message-center)
145147
#t]
@@ -372,6 +374,23 @@
372374
(start-update-thread #f)
373375
(set-update #t))
374376
(super resize w h))
377+
378+
(define plot-metrics-ok? #f)
379+
(match-define (list bounds ->dc ->plot plane)
380+
(send area get-plot-metrics-functions))
381+
(define (update-metrics)
382+
(match-define (list new-bounds new-->dc new-->plot new-plane)
383+
(send area get-plot-metrics-functions))
384+
(set! bounds new-bounds)
385+
(set! ->dc new-->dc)
386+
(set! ->plot new-->plot)
387+
(set! plane new-plane)
388+
(set! plot-metrics-ok? #t))
389+
(define/public (get-plot-bounds) (unless plot-metrics-ok? (update-metrics)) (bounds))
390+
(define/public (plot->dc coords) (unless plot-metrics-ok? (update-metrics)) (->dc coords))
391+
(define/public (dc->plot coords) (unless plot-metrics-ok? (update-metrics)) (->plot coords))
392+
(define/public (plane-vector) (unless plot-metrics-ok? (update-metrics)) (plane))
393+
(define/public (get-plot-metrics-functions) (unless plot-metrics-ok? (update-metrics)) (list bounds ->dc ->plot plane))
375394
))
376395

377396
(define (make-2d-plot-snip

plot-gui-lib/plot/private/gui/snip3d.rkt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(require racket/gui/base racket/class racket/match
44
plot/private/common/math
55
plot/private/common/parameters
6+
(submod plot/private/common/plotmetrics untyped)
67
"worker-thread.rkt"
78
"snip.rkt")
89

@@ -15,7 +16,7 @@
1516
(define (clamp x mn mx) (min* (max* x mn) mx))
1617

1718
(define 3d-plot-snip%
18-
(class plot-snip%
19+
(class* plot-snip% (plot-metrics<%>)
1920
(init init-bm saved-plot-parameters)
2021
(init-field make-bm angle altitude area width height)
2122

@@ -68,6 +69,7 @@
6869
(worker-thread-try-get rth (λ () (values #f #f))))
6970
(cond [(is-a? new-bm bitmap%)
7071
(set! area new-area)
72+
(set! plot-metrics-ok? #f)
7173
(set-bitmap new-bm)
7274
(set-message-center)
7375
(when (not (and (= last-angle angle)
@@ -140,6 +142,23 @@
140142
(start-update-thread #t))
141143
(set-update #t))
142144
(super resize w h))
145+
146+
(define plot-metrics-ok? #f)
147+
(match-define (list bounds ->dc ->plot plane)
148+
(send area get-plot-metrics-functions))
149+
(define (update-metrics)
150+
(match-define (list new-bounds new-->dc new-->plot new-plane)
151+
(send area get-plot-metrics-functions))
152+
(set! bounds new-bounds)
153+
(set! ->dc new-->dc)
154+
(set! ->plot new-->plot)
155+
(set! plane new-plane)
156+
(set! plot-metrics-ok? #t))
157+
(define/public (get-plot-bounds) (unless plot-metrics-ok? (update-metrics)) (bounds))
158+
(define/public (plot->dc coords) (unless plot-metrics-ok? (update-metrics)) (->dc coords))
159+
(define/public (dc->plot coords) (unless plot-metrics-ok? (update-metrics)) (->plot coords))
160+
(define/public (plane-vector) (unless plot-metrics-ok? (update-metrics)) (plane))
161+
(define/public (get-plot-metrics-functions) (unless plot-metrics-ok? (update-metrics)) (list bounds ->dc ->plot plane))
143162
))
144163

145164
(define (make-3d-plot-snip

plot-lib/plot/private/common/contract.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@
121121
(define (treeof elem-contract)
122122
(or/c elem-contract
123123
(listof (recursive-contract (treeof elem-contract) #:flat))))
124+
125+
(require (submod "plotmetrics.rkt" untyped))
126+
(provide plot-metrics-object/c)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#lang typed/racket/base
2+
3+
;; Untyped interface / contract
4+
(module untyped racket/base
5+
(require racket/class
6+
racket/contract
7+
racket/match
8+
racket/draw)
9+
10+
(provide plot-metrics<%>
11+
plot-metrics-object/c)
12+
13+
(define plot-metrics<%> (interface ()
14+
get-plot-bounds
15+
dc->plot
16+
plot->dc
17+
plane-vector
18+
get-plot-metrics-functions))
19+
20+
(define plot-metrics-object/c
21+
(object/c [get-plot-bounds (->m (vectorof (vector/c real? real?)))]
22+
[plot->dc (->m (vectorof real?) (vectorof real?))]
23+
[dc->plot (->m (vectorof real?) (vectorof real?))]
24+
[plane-vector (->m (vectorof real?))]
25+
[get-plot-metrics-functions (->m (values (-> (vectorof (vector/c real? real?)))
26+
(-> (vectorof real?) (vectorof real?))
27+
(-> (vectorof real?) (vectorof real?))
28+
(-> (vectorof real?))))]))
29+
)
30+
31+
;; Typed Types / mixin / structures
32+
(require typed/pict
33+
typed/racket/class
34+
racket/match)
35+
36+
(provide plot-metrics-mixin Plot-Metrics-Functions Plot-Metrics<%> plot-metrics%
37+
(struct-out plot-pict) Plot-Pict pict->pp)
38+
39+
(define-type Metrics-Object (Object [get-plot-metrics-functions (-> Plot-Metrics-Functions)]))
40+
(define-type Plot-Metrics-Functions (List (-> (Vectorof (Vectorof Real)))
41+
(-> (Vectorof Real) (Vectorof Real))
42+
(-> (Vectorof Real) (Vectorof Real))
43+
(-> (Vectorof Real))))
44+
(define-type Plot-Metrics<%>
45+
(Class
46+
[get-plot-bounds (-> (Vectorof (Vectorof Real)))]
47+
[plot->dc (-> (Vectorof Real) (Vectorof Real))]
48+
[dc->plot (-> (Vectorof Real) (Vectorof Real))]
49+
[plane-vector (-> (Vectorof Real))]
50+
[get-plot-metrics-functions (-> Plot-Metrics-Functions)]))
51+
52+
(: plot-metrics-mixin (All (A #:row) (-> (Class #:row-var A)
53+
(Class [init [->metrics-object (-> Metrics-Object)]]
54+
#:row-var A
55+
#:implements Plot-Metrics<%>))))
56+
(define (plot-metrics-mixin %)
57+
(class %
58+
(init ->metrics-object)
59+
(define (load) : Void
60+
(match-define (list new-bounds new-->dc new-->plot new-plane)
61+
(send (->metrics-object) get-plot-metrics-functions))
62+
(set! bounds new-bounds)
63+
(set! ->dc new-->dc)
64+
(set! ->plot new-->plot)
65+
(set! plane new-plane)
66+
(set! getall (λ () (list bounds ->dc ->plot plane)))
67+
(set! load void))
68+
69+
(define (bounds) : (Vectorof (Vectorof Real)) (load)(bounds))
70+
(define (plane) : (Vectorof Real) (load)(plane))
71+
(define (getall) : Plot-Metrics-Functions (load)(list bounds ->dc ->plot plane))
72+
(define (->dc [v : (Vectorof Real)]) : (Vectorof Real) (load)(->dc v))
73+
(define (->plot [v : (Vectorof Real)]) : (Vectorof Real) (load)(->plot v))
74+
75+
(super-make-object)
76+
(define/public (get-plot-bounds) (bounds))
77+
(define/public (dc->plot coords) (->plot coords))
78+
(define/public (plot->dc coords) (->dc coords))
79+
(define/public (plane-vector) (plane))
80+
(define/public (get-plot-metrics-functions) (getall))))
81+
82+
(define plot-metrics% (plot-metrics-mixin object%))
83+
84+
85+
(struct plot-pict pict ([bounds : (Vectorof (Vectorof Real))]
86+
[plot->dc : (-> (Vectorof Real) (Vectorof Real))]
87+
[dc->plot : (-> (Vectorof Real) (Vectorof Real))]
88+
[plane-vector : (Vectorof Real)]))
89+
(define-type Plot-Pict plot-pict)
90+
(define (pict->pp [P : pict]
91+
[metrics-object : Metrics-Object]) : plot-pict
92+
(match-define (list bounds ->dc ->plot plane)
93+
(send metrics-object get-plot-metrics-functions))
94+
95+
(plot-pict (pict-draw P) (pict-width P) (pict-height P) (pict-ascent P)
96+
(pict-descent P) (pict-children P) (pict-panbox P) (pict-last P)
97+
(bounds) ->dc ->plot (plane)))

plot-lib/plot/private/common/types.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,6 @@
162162
[draw-pict (->* [pict (Vectorof Real)] (Anchor Real) Void)]
163163
[calculate-legend-rect (-> (Listof legend-entry) Rect Anchor Rect)]
164164
[draw-legend (-> (Listof legend-entry) Rect Void)]))
165+
166+
(require "plotmetrics.rkt")
167+
(provide Plot-Metrics<%> Plot-Pict Plot-Metrics-Functions)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#lang typed/racket
22

3+
(require typed/pict)
4+
35
(provide Pict pict?)
46

5-
(require/typed
6-
pict
7-
[#:opaque Pict pict?])
7+
(define-type Pict pict)

0 commit comments

Comments
 (0)