-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscanner.lisp
448 lines (380 loc) · 14.4 KB
/
scanner.lisp
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
(in-package :plotter)
;;-------------------------------------------------------------------
;; Abstract superclass <scanner> represent objects that respond to the NEXT-ITEM method
;;
(defclass <scanner> ()
())
(defclass <limited-scanner> (<scanner>)
((limit :accessor scanner-limit :initarg :limit)
(pos :accessor scanner-position :initform 0)))
(defclass <counting-scanner> (<limited-scanner>)
())
(defclass <vector-scanner> (<limited-scanner>)
((vec :accessor scanner-vector :initarg :vector)))
(defclass <list-scanner> (<limited-scanner>)
((lst :accessor scanner-list :initarg :list)
(lst-backup :accessor scanner-list-backup)))
(defclass <array-scanner> (<limited-scanner>)
((arr :accessor scanner-array :initarg :array)))
(defclass <carray-scanner> (<limited-scanner>)
((arr :accessor scanner-array :initarg :array)))
(defclass <user-seq-scanner> (<limited-scanner>)
((arr :accessor scanner-array :initarg :array)))
;; ===============
(defmethod make-scanner ((limit integer) &key (max-items limit))
(make-instance '<counting-scanner>
:limit (min limit max-items)))
(defmethod make-scanner ((vec vector) &key (max-items (length vec)))
(make-instance '<vector-scanner>
:limit (min (length vec) max-items)
:vector vec))
(defmethod make-scanner ((lst list) &key (max-items (length lst)))
(make-instance '<list-scanner>
:list lst
:limit (min (length lst) max-items)))
(defmethod initialize-instance :after ((self <list-scanner>)
&rest args &key &allow-other-keys)
(setf (scanner-list-backup self) (scanner-list self)))
(defmethod make-scanner ((arr array) &key (max-items (array-total-size arr)))
(make-instance '<array-scanner>
:array arr
:limit (min (array-total-size arr) max-items)))
(defmethod make-scanner ((arr ca:<carray>) &key (max-items (ca:carray-total-size arr)))
(make-instance '<carray-scanner>
:array arr
:limit (min (ca:carray-total-size arr) max-items)))
(defmethod make-scanner ((arr um:user-defined-sequence) &key (max-items (um:user-defined-sequence-length arr)))
(make-instance '<user-seq-scanner>
:array arr
:limit (min max-items (um:user-defined-sequence-length arr))))
;; ===============
(defmethod reset-scanner ((scanner <limited-scanner>))
(setf (scanner-position scanner) 0))
(defmethod reset-scanner :after ((scanner <list-scanner>))
(setf (scanner-list scanner) (scanner-list-backup scanner)))
;; ===============
;; With an eye toward parallel processing...
(defmethod copy-scanner ((scanner <scanner>))
(clos:copy-standard-object scanner))
(defmethod make-scanner ((scanner <limited-scanner>) &key max-items)
(let* ((new-scanner (copy-scanner scanner))
(old-limit (scanner-limit scanner))
(new-limit (min (or max-items old-limit)
old-limit)))
(setf (scanner-limit new-scanner) new-limit)
(reset-scanner new-scanner)
new-scanner))
(defmethod set-position ((scanner <limited-scanner>) pos)
(let* ((new-pos (min pos (scanner-limit scanner))))
(setf (scanner-position scanner) new-pos)
))
(defmethod set-position :after ((scanner <list-scanner>) pos)
(declare (ignore pos))
(setf (scanner-list scanner) (nthcdr (scanner-position scanner)
(scanner-list-backup scanner))))
;; ===============
;; All scanners pass through NIL as the terminal value
(defmethod next-item ((cscanner <counting-scanner>))
(with-accessors ((position scanner-position)
(limit scanner-limit )) cscanner
(let ((ans position))
(when (< ans limit)
(incf position)
ans)
)))
(defmethod next-item ((lscanner <list-scanner>))
(with-accessors ((limit scanner-limit )
(position scanner-position)
(its-list scanner-list )) lscanner
(when (< position limit)
(incf position)
(pop its-list))
))
(defmethod next-item ((vscanner <vector-scanner>))
(with-accessors ((position scanner-position)
(limit scanner-limit )
(its-vector scanner-vector )) vscanner
(when (< position limit)
(prog1
(aref its-vector position)
(incf position)))
))
(defmethod next-item ((ascanner <array-scanner>))
(with-accessors ((position scanner-position)
(limit scanner-limit )
(its-array scanner-array )) ascanner
(when (< position limit)
(prog1
(row-major-aref its-array position)
(incf position)))
))
(defmethod next-item ((cascanner <carray-scanner>))
(with-accessors ((position scanner-position)
(limit scanner-limit )
(its-array scanner-array )) cascanner
(when (< position limit)
(prog1
(ca:row-major-caref its-array position)
(incf position)))
))
(defmethod next-item ((scanner <user-seq-scanner>))
(with-accessors ((position scanner-position)
(limit scanner-limit )
(its-array scanner-array )) scanner
(when (< position limit)
(prog1
(um:user-defined-sequence-fetch its-array position)
(incf position)))
))
;; ===============
(defclass <transformer> (<scanner>)
((src :accessor transformer-source :initarg :source)
(xform :accessor transformer-xform :initarg :xform)))
(defmethod make-transformer ((src <scanner>) (xform function))
(make-instance '<transformer>
:source src
:xform xform))
(defmethod next-item ((xf <transformer>))
;; pass along NIL as a terminal value
(with-accessors ((source transformer-source)
(xform transformer-xform )) xf
(let ((item (next-item source)))
(when item
(funcall xform item)))
))
(defmethod reset-scanner ((xf <transformer>))
(reset-scanner (transformer-source xf)))
;; ===============
(defclass <pair-scanner> (<scanner>)
((xsrc :accessor pair-scanner-xsrc :initarg :xsrc)
(ysrc :accessor pair-scanner-ysrc :initarg :ysrc)
(pair :accessor pair-scanner-values :initform (make-array 2))
))
(defmethod make-pair-scanner ((xs <scanner>) (ys <scanner>))
(make-instance '<pair-scanner>
:xsrc xs
:ysrc ys
))
(defmethod next-item ((pairs <pair-scanner>))
(with-accessors ((xs pair-scanner-xsrc )
(ys pair-scanner-ysrc )
(pair pair-scanner-values)) pairs
(let* ((x (next-item xs))
(y (next-item ys)))
(when (and x y)
(setf (aref pair 0) x
(aref pair 1) y)
pair))
))
(defmethod reset-scanner ((pairs <pair-scanner>))
(reset-scanner (pair-scanner-xsrc pairs))
(reset-scanner (pair-scanner-ysrc pairs)))
;; -----------------------------------------------------
;; In order to allow for line thickness scaling, we need to plot
;; things in pixel space. Hence we need to pre-transform data coords
;; to pixel coords using a GP:TRANSFORM-POINT.
(defclass <gpxform-pair-scanner> (<pair-scanner>)
((gpxform :accessor pair-scanner-gpxform :initarg :gpxform)))
(defmethod make-gpxform-pair-scanner (xform (xs <scanner>) (ys <scanner>))
(make-instance '<gpxform-pair-scanner>
:xsrc xs
:ysrc ys
:gpxform xform
))
(defmethod make-gpxform-pairs ((pairs <pair-scanner>) xform)
(make-instance '<gpxform-pair-scanner>
:xsrc (pair-scanner-xsrc pairs)
:ysrc (pair-scanner-ysrc pairs)
:gpxform xform))
(defmethod next-item ((pairs <gpxform-pair-scanner>))
(with-accessors ((xs pair-scanner-xsrc )
(ys pair-scanner-ysrc )
(pair pair-scanner-values)
(gpxform pair-scanner-gpxform)) pairs
(let* ((x (next-item xs))
(y (next-item ys)))
(when (and x y)
(multiple-value-bind (xt yt)
(gp:transform-point gpxform x y)
(setf (aref pair 0) xt
(aref pair 1) yt)
pair)
))
))
;; -----------------------------------------------------
(defmethod do-with-pairs ((pairs <pair-scanner>) fn)
(reset-scanner pairs)
(loop for pair = (next-item pairs)
while pair
do
(funcall fn (aref pair 0) (aref pair 1))))
(defmacro with-pairs ((pairs x y) &body body)
`(do-with-pairs ,pairs
(lambda (,x ,y)
,@body)))
(defmethod do-with-scanner ((scanner <scanner>) fn)
(reset-scanner scanner)
(loop for x = (next-item scanner)
while x
do
(funcall fn x)))
(defmacro with-scanner ((scanner x) &body body)
`(do-with-scanner ,scanner
(lambda (,x)
,@body)))
;; ----------------------------------------------------------
;; For transformed points, it is more efficient to transform them in
;; bulk.
(defmethod collect-pairs ((pairs <pair-scanner>))
(reset-scanner pairs)
(let (xprev yprev)
(loop for pair = (next-item pairs)
while pair
when (or (null xprev)
;; weed out adjacent duplicate points
(/= (aref pair 0) xprev)
(/= (aref pair 1) yprev))
nconc (list (setf xprev (aref pair 0))
(setf yprev (aref pair 1))))
))
(defmethod collect-pairs ((pairs <gpxform-pair-scanner>))
(let* ((sub-scanner (make-pair-scanner
(pair-scanner-xsrc pairs)
(pair-scanner-ysrc pairs)))
(xys (collect-pairs sub-scanner)))
(gp:transform-points (pair-scanner-gpxform pairs) xys xys)
))
;; --------------------------------------------------------
(defun generic-histo-pairs (pairs start-fn mid-fn end-fn)
;; Generic driver shared by several variant functions.
;;
;; In prep for draw-polygon point pairs. START-FN, MID-FN, END-FN
;; should furnish LIST objects that can be spliced into the
;; accumlating answer.
;;
;; NOTE: After COLLECT-PAIRS, we are working in unscaled pixel
;; space. Rightward and Downward are positive X and Y directions.
;;
(let ((xys (collect-pairs pairs)))
(when xys
(let+ (( (x1 y1 . rest) xys)
(ans (funcall start-fn x1 y1)))
(um:nlet iter ((xys rest)
(x1 x1)
(y1 y1)
(tl (last ans)))
(cond ((endp xys)
(rplacd tl (funcall end-fn x1 y1))
ans)
(t
(let+ (( (x2 y2 . rest) xys))
(go-iter rest x2 y2
(last
(rplacd tl
(funcall mid-fn x1 y1 x2 y2)
)))
))
))
))))
(defmethod stairstep-pairs ((pairs <pair-scanner>))
;; x2,y2
;; +
;; |
;; +--------+
;; x1,y1 x2,y1
(generic-histo-pairs pairs
#'list
(lambda (x1 y1 x2 y2)
(declare (ignore x1))
(list x2 y1 x2 y2))
#'lw:false))
(defmethod histo-pairs ((pairs <pair-scanner>))
;; xm,y2 x2,y2
;; +------
;; |
;; +--------+
;; x1,y1 xm,y1
(generic-histo-pairs pairs
#'list
(lambda (x1 y1 x2 y2)
(let ((xmid (/2 (+ x1 x2))))
(list xmid y1 xmid y2 x2 y2)))
#'lw:false))
(defmethod histo-vbars-pairs ((pairs <pair-scanner>) ybase)
;; (x,y) pairs converted to vert bars. X-axis values should be
;; monotonic.
;;
;; xm,y2 x2,y2
;; +-----+
;; | |
;; +--------+ |
;; x1,y1 xm,y1 |
;; | |
;; +--------------+
;; x1,0 x2,0
;;
;; In prep for draw-polygon with :filled t, we need to record the
;; baseline point at start and finish.
(generic-histo-pairs pairs
(lambda (x1 y1)
(list x1 ybase x1 y1))
(lambda (x1 y1 x2 y2)
(let ((xmid (/2 (+ x1 x2))))
(list xmid y1 xmid y2 x2 y2)))
(lambda (x1 y1)
(declare (ignore y1))
(list x1 ybase))))
(defmethod histo-hbars-pairs ((pairs <pair-scanner>) xbase)
;; Here pairs of (x,y) need to be given a similar treatment along
;; the y-axis, as we did for histo-vbars-pairs along the x-axis.
;;
;; Y-axis vals should be monotonic.
;;
;; 0,y1 +---------------+ x1,y1
;; | |
;; | x2,ym |
;; | +-------+ x1,ym
;; | |
;; | |
;; 0,y2 +-------+ x2,y2
;;
;; In prep for draw-polygon with :filled t, we need to record the
;; baseline point at start and finish.
(generic-histo-pairs pairs
(lambda (x1 y1)
(list xbase y1 x1 y1))
(lambda (x1 y1 x2 y2)
(let ((ymid (/2 (+ y1 y2))))
(list x1 ymid x2 ymid x2 y2)))
(lambda (x1 y1)
(declare (ignore x1))
(list xbase y1))))
#|
(let* ((xs '(1 2 3))
(ys '(10 20 30))
(pairs (make-pair-scanner (make-scanner xs) (make-scanner ys))))
(histo-pairs pairs))
(loop for ix from 1 to 10 nconc (list ix))
|#
(defun hbar-rects (pairs wd off x0)
(let* ((ans (list nil))
(tl ans)
(yoff (- off (/2 wd))))
(with-pairs (pairs x y)
(setf tl (last
(rplacd tl
(list x0 (+ yoff y)
(- x x0) wd)
))))
(cdr ans)))
(defun vbar-rects (pairs wd off y0)
(let* ((ans (list nil))
(tl ans)
(xoff (- off (/2 wd))))
(with-pairs (pairs x y)
(setf tl (last
(rplacd tl
(list (+ xoff x) y0
wd (- y y0))
))))
(cdr ans)))