forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontour.rkt
More file actions
196 lines (171 loc) · 9.47 KB
/
contour.rkt
File metadata and controls
196 lines (171 loc) · 9.47 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
#lang racket/base
;; Renderers for contour lines and contour intervals
(require racket/contract racket/class racket/match racket/list racket/flonum racket/vector racket/math
unstable/latent-contract/defthing
plot/utils
"../common/utils.rkt")
(provide (all-defined-out))
;; ===================================================================================================
;; One contour line
(define ((isoline-render-proc g z samples color width style alpha label) area)
(match-define (vector x-ivl y-ivl) (send area get-bounds-rect))
(match-define (ivl x-min x-max) x-ivl)
(match-define (ivl y-min y-max) y-ivl)
(define num (animated-samples samples))
(define sample (g (vector x-ivl y-ivl) (vector num num)))
(match-define (2d-sample xs ys zss z-min z-max) sample)
(when (<= z-min z z-max)
(send area put-alpha alpha)
(send area put-pen color width style)
(let* ([flonum-ok? (flonum-ok-for-3d? x-min x-max y-min y-max z-min z-max)]
[sample (if flonum-ok? (2d-sample-exact->inexact sample) sample)]
[z (if flonum-ok? (exact->inexact z) z)])
(for-2d-sample
(xa xb ya yb z1 z2 z3 z4) sample
(for/list ([line (in-list (heights->lines xa xb ya yb z z1 z2 z3 z4))])
(send/apply area put-line (map (λ (v) (vector-take v 2)) line))))))
(cond [label (line-legend-entry label color width style)]
[else empty]))
(defproc (isoline
[f (real? real? . -> . real?)] [z real?]
[x-min (or/c rational? #f) #f] [x-max (or/c rational? #f) #f]
[y-min (or/c rational? #f) #f] [y-max (or/c rational? #f) #f]
[#:samples samples (and/c exact-integer? (>=/c 2)) (contour-samples)]
[#:color color plot-color/c (line-color)]
[#:width width (>=/c 0) (line-width)]
[#:style style plot-pen-style/c (line-style)]
[#:alpha alpha (real-in 0 1) (line-alpha)]
[#:label label (or/c string? #f) #f]
) renderer2d?
(define x-ivl (ivl x-min x-max))
(define y-ivl (ivl y-min y-max))
(define g (2d-function->sampler f (vector x-ivl y-ivl)))
(renderer2d (vector x-ivl y-ivl) #f default-ticks-fun
(isoline-render-proc g z samples color width style alpha label)))
;; ===================================================================================================
;; Contour lines
(define ((contours-render-proc g levels samples colors widths styles alphas label) area)
(let/ec return
(match-define (vector x-ivl y-ivl) (send area get-bounds-rect))
(match-define (ivl x-min x-max) x-ivl)
(match-define (ivl y-min y-max) y-ivl)
(define num (animated-samples samples))
(define sample (g (vector x-ivl y-ivl) (vector num num)))
(match-define (2d-sample xs ys zss z-min z-max) sample)
(unless (and z-min z-max) (return empty))
(match-define (list (tick zs _ labels) ...) (contour-ticks (plot-z-ticks) z-min z-max levels #f))
;; need to check this or in-cycle below does an infinite loop (I think it's an in-cycle bug)
(unless (empty? zs)
(let* ([colors (maybe-apply colors zs)]
[widths (maybe-apply widths zs)]
[styles (maybe-apply styles zs)]
[alphas (maybe-apply alphas zs)]
[flonum-ok? (flonum-ok-for-3d? x-min x-max y-min y-max z-min z-max)]
[sample (if flonum-ok? (2d-sample-exact->inexact sample) sample)]
[zs (if flonum-ok? (map exact->inexact zs) zs)])
(for ([z (in-list zs)]
[color (in-cycle colors)]
[width (in-cycle widths)]
[style (in-cycle styles)]
[alpha (in-cycle alphas)])
(send area put-alpha alpha)
(send area put-pen color width style)
(for-2d-sample
(xa xb ya yb z1 z2 z3 z4) sample
(for/list ([line (in-list (heights->lines xa xb ya yb z z1 z2 z3 z4))])
(match-define (list v1 v2) (map (λ (v) (vector-take v 2)) line))
(send area put-line v1 v2))))))
(cond [(and label (not (empty? zs))) (line-legend-entries label zs labels colors widths styles)]
[else empty])))
(defproc (contours
[f (real? real? . -> . real?)]
[x-min (or/c rational? #f) #f] [x-max (or/c rational? #f) #f]
[y-min (or/c rational? #f) #f] [y-max (or/c rational? #f) #f]
[#:samples samples (and/c exact-integer? (>=/c 2)) (contour-samples)]
[#:levels levels (or/c 'auto exact-positive-integer? (listof real?)) (contour-levels)]
[#:colors colors (plot-colors/c (listof real?)) (contour-colors)]
[#:widths widths (pen-widths/c (listof real?)) (contour-widths)]
[#:styles styles (plot-pen-styles/c (listof real?)) (contour-styles)]
[#:alphas alphas (alphas/c (listof real?)) (contour-alphas)]
[#:label label (or/c string? #f) #f]
) renderer2d?
(define x-ivl (ivl x-min x-max))
(define y-ivl (ivl y-min y-max))
(define g (2d-function->sampler f (vector x-ivl y-ivl)))
(renderer2d (vector x-ivl y-ivl) #f default-ticks-fun
(contours-render-proc g levels samples colors widths styles alphas label)))
;; ===================================================================================================
;; Contour intervals
(define ((contour-intervals-render-proc
g levels samples colors styles contour-colors contour-widths contour-styles alphas label)
area)
(let/ec return
(match-define (vector x-ivl y-ivl) (send area get-bounds-rect))
(match-define (ivl x-min x-max) x-ivl)
(match-define (ivl y-min y-max) y-ivl)
(define num (animated-samples samples))
(define sample (g (vector x-ivl y-ivl) (vector num num)))
(match-define (2d-sample xs ys zss z-min z-max) sample)
(unless (and z-min z-max) (return empty))
(match-define (list (tick zs _ labels) ...) (contour-ticks (plot-z-ticks) z-min z-max levels #t))
(define-values (z-ivls ivl-labels)
(for/lists (z-ivls ivl-labels) ([za (in-list zs)]
[zb (in-list (rest zs))]
[la (in-list labels)]
[lb (in-list (rest labels))])
(values (ivl za zb) (format "[~a,~a]" la lb))))
(send area put-pen 0 1 'transparent)
(let* ([colors (map ->brush-color (maybe-apply colors z-ivls))]
[styles (map ->brush-style (maybe-apply styles z-ivls))]
[alphas (maybe-apply alphas z-ivls)]
[flonum-ok? (flonum-ok-for-3d? x-min x-max y-min y-max z-min z-max)]
[sample (if flonum-ok? (2d-sample-exact->inexact sample) sample)]
[zs (if flonum-ok? (map exact->inexact zs) zs)])
(for ([za (in-list zs)]
[zb (in-list (rest zs))]
[color (in-cycle colors)]
[style (in-cycle styles)]
[alpha (in-cycle alphas)])
(send area put-brush color style)
(send area put-alpha alpha)
(for-2d-sample
(xa xb ya yb z1 z2 z3 z4) sample
(for/list ([poly (in-list (heights->polys xa xb ya yb za zb z1 z2 z3 z4))])
(send area put-polygon (map (λ (v) (vector-take v 2)) poly)))))
((contours-render-proc g levels samples contour-colors contour-widths contour-styles alphas #f)
area)
(define n (- (length zs) 2))
(define contour-colors*
(append (list 0) (sequence-take (in-cycle (maybe-apply contour-colors zs)) 0 n) (list 0)))
(define contour-widths*
(append (list 0) (sequence-take (in-cycle (maybe-apply contour-widths zs)) 0 n) (list 0)))
(define contour-styles*
(append '(transparent) (sequence-take (in-cycle (maybe-apply contour-styles zs)) 0 n)
'(transparent)))
(cond [label (interval-legend-entries
label z-ivls ivl-labels
colors styles colors '(1) '(transparent)
contour-colors* contour-widths* contour-styles*
(rest contour-colors*) (rest contour-widths*) (rest contour-styles*))]
[else empty]))))
(defproc (contour-intervals
[f (real? real? . -> . real?)]
[x-min (or/c rational? #f) #f] [x-max (or/c rational? #f) #f]
[y-min (or/c rational? #f) #f] [y-max (or/c rational? #f) #f]
[#:samples samples (and/c exact-integer? (>=/c 2)) (contour-samples)]
[#:levels levels (or/c 'auto exact-positive-integer? (listof real?)) (contour-levels)]
[#:colors colors (plot-colors/c (listof ivl?)) (contour-interval-colors)]
[#:styles styles (plot-brush-styles/c (listof ivl?)) (contour-interval-styles)]
[#:contour-colors contour-colors (plot-colors/c (listof real?)) (contour-colors)]
[#:contour-widths contour-widths (pen-widths/c (listof real?)) (contour-widths)]
[#:contour-styles contour-styles (plot-pen-styles/c (listof real?)) (contour-styles)]
[#:alphas alphas (alphas/c (listof ivl?)) (contour-interval-alphas)]
[#:label label (or/c string? #f) #f]
) renderer2d?
(define x-ivl (ivl x-min x-max))
(define y-ivl (ivl y-min y-max))
(define g (2d-function->sampler f (vector x-ivl y-ivl)))
(renderer2d (vector x-ivl y-ivl) #f default-ticks-fun
(contour-intervals-render-proc g levels samples colors styles
contour-colors contour-widths contour-styles
alphas label)))