-
Notifications
You must be signed in to change notification settings - Fork 3
/
chazelle.lisp
2821 lines (2666 loc) · 113 KB
/
chazelle.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
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
(in-package #:binpack/2)
#++(ql:quickload 'binpack/2)
;; based on Chazelle's "Bottom-Left Bin-Packing Heuristic: An
;; Efficient Implementation" (
;; https://www.cs.princeton.edu/~chazelle/pubs/blbinpacking.pdf ),
;; with heuristics from http://clb.demon.fi/files/RectangleBinPack.pdf
;; and with options for controlling shape of packing
;;; goals for shaping heuristics:
;;
;; for a given set of rects, we want them efficiently packed into some
;; rectangle, but might not care about the actual size/shape of the
;; rectangle beyond some much larger constraints like maximum image or
;; texture sizes. Just packing into that maximum might end up with a
;; wide/flat packing, or one that mostly uses 2 edges and leaves the
;; middle empty.
;;
;; instead, we give options for biasing the packing while maintaining
;; the "bottom left" constraint, and some modifiers for common
;; constraints like "multiple of X" widths etc.
;;
;;; uses cases:
;; fixed width or height, pack into minimum for other dimension
;;
;; pack into minimal square, limited to 4096 or whatever in either
;; dimension
;;
;; same as above, but with width limited to multiples of 4 or 8
;;
;; pack into minimal rectangle with power-of-2 dimensions?
;;
;;; biasing heuristics:
;; given a starting width/height (estimated from total size of rects
;; if possible, or specified manually), and quantization (multiples of
;; 8 etc) for each axis, we add a penalty to the original packing
;; heuristic for any packing which would extend the current
;; (quantized) width or height, and further penalize those that would
;; push the aspect ratio further from that of the starting dimensions.
;;
;; or possibly bias based on amount of area added to total size when a
;; packing extends past current extents? possibly scaled by starting
;; aspect ratio if maintaining that is desired? added area without
;; aspect ratio would probably be best for power-of-2 packings
;;; optimization options
;; (ideally try to make them interruptable, for example add a "stop
;; and return best result so far" restart, so user can let it run as
;; long as they want, or interrupt it early)
;;
;; retry pack with fixed sizes a few sizes smaller than final pack?
;; (size affects packing, so can't binary search or stop when it
;; fails, have to do linear search and try a few past first failure in
;; case they get luckier)
;;
;; pack, then remove anything above or to right of a gap, then repack
;; those (possibly with different heuristics/sorting/etc)
;;
;; retry pack a few times with some randomization of sorting,
;; heuristic, and packing choices?
;;
;; variant of full retry: add some restart points partway to allow
;; backtracking, with some estimate of quality of partial pack for
;; doing a search (max dimensions, gaps/density, ?)
;;; other features to add:
;;
;; multipage packing?
;;
;; pack-from-center? (divide into quadrants and pack all 4 at once
;; from origin (possibly with options for overlapping axis?))
;;
;; mode that reuses the search to try to pack multiple rects of same
;; width at once?
(defun fff (&rest r) (declare (ignore r)))
(defclass dll ()
;; doubly linked list links
((next :accessor dll-next)
(prev :accessor dll-prev)))
(defmethod initialize-instance :after ((o dll) &key &allow-other-keys)
(unless (slot-boundp o 'next)
(setf (dll-next o) o))
(unless (slot-boundp o 'prev)
(setf (dll-prev o) o)))
(defun insert-after (prev new)
(cond
(prev
(setf (dll-prev new) prev)
(setf (dll-next new) (dll-next prev))
(setf (dll-prev (dll-next prev)) new)
(setf (dll-next prev) new))
(t
(setf (dll-prev new) new)
(setf (dll-next new) new)))
new)
(defun insert-before (next new)
(cond
(next
(setf (dll-next new) next)
(setf (dll-prev new) (dll-prev next))
(setf (dll-next (dll-prev next)) new)
(setf (dll-prev next) new))
(t
(setf (dll-prev new) new)
(setf (dll-next new) new)))
new)
(defun delete-node (node)
;; returns NEXT, or NIL if last node was removed
(cond
((not (dll-next node))
nil)
((eql (dll-next node) node)
(setf (dll-next node) nil
(dll-prev node) nil)
nil)
(t
(let ((next (dll-next node)))
(setf (dll-prev next) (dll-prev node)
(dll-next (dll-prev node)) next)
(setf (dll-next node) nil
(dll-prev node) nil)
next))))
(defmacro %do-dll ((node dll endp iterator) &body body)
(alexandria:with-gensyms (start)
(alexandria:once-only (dll)
`(loop with ,start = ,dll
for ,node = ,dll then (,iterator ,node)
for ,endp = (eql ,start (,iterator ,node))
do (progn ,@body)
until (eql ,start (,iterator ,node))))))
(defmacro do-dll/next ((node dll &optional endp) &body body)
`(%do-dll (,node ,dll ,endp dll-next) ,@body))
(defmacro do-dll/prev ((node dll &optional endp) &body body)
`(%do-dll (,node ,dll ,endp dll-prev) ,@body))
(defun dll-length (dll)
(let ((a 0))
(do-dll/next (n dll end)
(incf a))
a))
(defclass deq-entry (dll)
((v :reader deq-v :initarg :v)))
(defclass deq ()
((front :accessor %deq-front :initform nil)
(back :accessor %deq-back :initform nil)))
(defun make-deq ()
(make-instance 'deq))
(defun deq-empty-p (deq)
(null (%deq-front deq)))
(defun empty-deq (deq)
(setf (%deq-front deq) nil)
(setf (%deq-back deq) nil))
(defun top1 (deq)
(when (%deq-front deq)
(deq-v (%deq-front deq))))
(defun top2 (deq)
(when (%deq-back deq)
(deq-v (%deq-back deq))))
(defun pop1 (deq)
(when (%deq-front deq)
(setf (%deq-front deq) (delete-node (%deq-front deq)))
(unless (%deq-front deq)
(setf (%deq-back deq) nil))))
(defun pop2 (deq)
(when (%deq-front deq)
(let ((b2 (delete-node (%deq-back deq))))
(setf (%deq-back deq) (when b2 (dll-prev (%deq-front deq)))))
(unless (%deq-back deq)
(setf (%deq-front deq) nil))))
(defun push1 (n deq)
(let ((node (make-instance 'deq-entry :v n)))
(if (deq-empty-p deq)
(setf (%deq-front deq) node
(%deq-back deq) node)
(setf (%deq-front deq)
(insert-after (%deq-back deq) node)))))
(defun push2 (n deq)
(let ((node (make-instance 'deq-entry :v n)))
(if (deq-empty-p deq)
(setf (%deq-front deq) node
(%deq-back deq) node)
(progn
(insert-after (%deq-back deq) node)
(setf (%deq-back deq) (dll-prev (%deq-front deq)))))))
;;
(defclass hole-vertex (dll)
((x :accessor hv-x :initform 0 :initarg :x)
(y :accessor hv-y :initform 0 :initarg :y)
;; Q<->QN and Q<->QW links. Links from Q to N/W if Q is also set,
;; otherwise from N/W back to Q
(n :accessor hv-n :initform nil :initarg :qn)
(w :accessor hv-w :initform nil :initarg :qw)
;; true if node is a Q vertex (top of left notch), NIL otherwise
(q :accessor hv-q :initform nil :initarg :q)
;; what type of vertical edge this vertex is corner of
;; (:left-notch, :leftmost, :rightmost, :falling, etc) or :QW or
;; :QN for QW or QN vertices in middle of span
(classify :accessor hv-classify :initform nil)
;; where this vertex is on a vertical edge, :top,:middle,:bottom,
;; or :middle if it is QN vertex in middle of horizontal span
(corner :accessor hv-corner :initform nil)
;; if this vertex is the bottom of a leftmost edge of a subhole, or
;; 'end' vertex for a subhole, link to the subhole
(sh :accessor hv-sh :initform nil)
;; if vertex is falling corner, link to hole
(hole :accessor hv-hole :initform nil)))
(defmacro %do-hole-vertices ((node dll dx dy endp iterator) &body body)
(alexandria:with-gensyms (prev n)
`(let ((,prev nil))
(%do-dll (,node ,dll ,endp ,iterator)
(let (,@(when dx `((,dx (if ,prev (- (hv-x ,node) (hv-x ,prev))))))
,@(when dy `((,dy (if ,prev (- (hv-y ,node) (hv-y ,prev)))))))
(setf ,prev ,node)
(flet ((jump (,n)
(setf ,node (,(ecase iterator
(dll-prev 'dll-next)
(dll-next 'dll-prev))
,n))))
(declare (ignorable #'jump))
,@body))))))
(defmacro do-hole-vertices/next ((node dll dx dy &optional endp) &body body)
`(%do-hole-vertices (,node ,dll ,dx ,dy ,endp dll-next) ,@body))
(defmacro do-hole-vertices/prev ((node dll dx dy &optional endp) &body body)
`(%do-hole-vertices (,node ,dll ,dx ,dy ,endp dll-prev) ,@body))
(defun up-down-dir (v)
(cond
((or (< (hv-y v) (hv-y (dll-prev v)))
(> (hv-y v) (hv-y (dll-next v))))
(values 'dll-prev 'dll-next :falling))
((or (< (hv-y v) (hv-y (dll-next v)))
(> (hv-y v) (hv-y (dll-prev v))))
(values 'dll-next 'dll-prev :rising))
(t nil)))
(defun extend-edge-up (v dir)
;; find local maximum of Y coord along this edge
(unless dir
(return-from extend-edge-up v))
(loop for n = (funcall dir v)
while (and (= (hv-x v) (hv-x n))
(< (hv-y v) (hv-y n)))
do (setf v n))
v)
(defun extend-edge-right (v dir)
;; find local maximum of x coord along this edge
(unless dir
(return-from extend-edge-right v))
(loop for n = (funcall dir v)
while (and (= (hv-y v) (hv-y n))
(< (hv-x v) (hv-x n)))
do (setf v n))
v)
(defun extend-edge-down (v dir)
;; find local minimum of Y coord along this edge
(unless dir
(return-from extend-edge-down v))
(loop for n = (funcall dir v)
while (and (= (hv-x v) (hv-x n))
(> (hv-y v) (hv-y n)))
do (setf v n))
v)
(defun classify-vertical-edge (v)
(multiple-value-bind (up down dir) (up-down-dir v)
(let* ((top (extend-edge-up v up))
(bottom (extend-edge-down v down))
(rising (eql dir :rising))
(falling (eql dir :falling))
(corner (cond ((eql top v) :top)
((eql bottom v) :bottom)
(t :middle))))
(unless (and up down)
;; N vertex or bug?
(assert (eql (hv-classify v) :qn))
(assert (eql (hv-corner v) :middle))
(return-from classify-vertical-edge
(values (hv-classify v) (hv-corner v))))
(when (eql top bottom)
(break "degen")
(return-from classify-vertical-edge
(values :degenerate :middle)))
(let ((dxt (- (hv-x (funcall up top)) (hv-x top)))
(dxb (- (hv-x (funcall down bottom)) (hv-x bottom))))
(values
(cond
((and (not (eql v top))
(not (eql v bottom)))
(assert (eql (hv-corner v) :middle))
(hv-classify v))
((and rising (plusp dxt) (plusp dxb))
;; cw: -x +y +x
:leftmost)
((and rising (minusp dxt) (minusp dxb))
;; cw +x +y -x
:left-notch)
((and falling (minusp dxt) (plusp dxb))
;; cw: +x -y +x
:falling-edge)
;; shouldn't happen in bottom-left packing
((and falling (plusp dxt) (plusp dxb))
;; cw: -x -y +x
:right-notch)
;; these aren't interesting, but including for debugging for now
((and rising (minusp dxt) (plusp dxb))
;; CW: -x +y -x
:step-up)
((and rising (plusp dxt) (minusp dxb))
;; cw: +x +y +x
:rising-edge)
((and falling (plusp dxt) (minusp dxb))
;; cw: -x -y -x
:step-down)
((and falling (minusp dxt) (minusp dxb))
;; cw: +x -y -x
:rightmost)
((and (not falling) (not rising) (eql (hv-corner v) :middle))
(hv-classify v))
(t (error "~s"(list :dxt dxt :dxb dxb :rising rising :falling falling))))
corner)))))
(defun set-qnw (v)
;; build Q->N and Q->W links for a hole that doesn't have any old
;; Q->N links from before this left-notch was added
(assert (eql (hv-classify v) :left-notch))
(assert (eql (hv-corner v) :top))
(setf (hv-q v) t)
;; walk NEXT until we find an X value right of Q
(let ((x1 (hv-x v))
(y1 (hv-y v)))
(block h
(do-dll/next (v2 (dll-next v))
(let* ((v3 (dll-next v2))
(x2 (hv-x v2))
(x3 (hv-x v3)))
(cond
((and (= x1 x2)
;; not top or middle of a falling edge
(<= (hv-y v2)
(hv-y (dll-next v2))))
;; directly under a vertex, just link to it
(assert (not (hv-n v2)))
;; can't be a :top or :middle corner
(assert (eql (hv-corner v2) :bottom))
(setf (hv-n v) v2)
(setf (hv-n v2) v)
(return-from h nil))
((< x2 x1 x3)
(assert (= (hv-y v2) (hv-y v3)))
;; under an edge, split it and link to new vertex
(let ((n (make-hole-vertex x1 (hv-y v2) v2)))
(setf (hv-n v) n)
(setf (hv-n n) v)
(setf (hv-classify n) :qn)
(setf (hv-corner n) :middle)
(return-from h nil)))
((and (hv-n v2) (hv-q v2)
(< x2 x1))
;; follow Q->N links, but make sure next iteration sees the
;; N vertex so we can notice if Q is between that and next vertex
(setf v2 (dll-prev (hv-n v2))))
((or (> x2 x1) (eql v2 v))
(error "couldn't find edge above left notch?"))))))
;; walk PREV until we find a Y value above Q
(assert (eql (hv-classify (dll-prev v)) :left-notch))
(assert (eql (hv-corner (dll-prev v)) :bottom))
(let ((rightmost nil)
(count 0))
(do-dll/prev (v2 (dll-prev v))
(let* ((v3 (dll-next v2))
(y2 (hv-y v2))
(y3 (hv-y (dll-next v2))))
(when (or (eql (hv-classify v2) :rightmost)
(eql (hv-classify v2) :falling-edge))
(setf rightmost t))
(incf count)
#++(when (> count 1000)
(error "looped?"))
(cond
((and (= y2 y1)
;; not top of rightmost edge next to falling edge
(<= (hv-x v2)
(hv-x (dll-prev v2))))
(assert (/= (hv-x v) (hv-x v2)))
(assert (not (hv-w v2)))
;; directly left of another vertex (upper left of lower
;; notch or step down) use it directly
(setf (hv-w v) v2)
(setf (hv-w v2) v)
(return-from set-qnw t))
((< y3 y1 y2)
(assert (= (hv-x v2) (hv-x v3)))
;; left of an edge, split it and link to new vertex
(let ((w (make-hole-vertex (hv-x v2) y1 v2)))
(setf (hv-w v) w)
(setf (hv-w w) v)
(setf (hv-classify w) :qw)
(setf (hv-corner w) :middle)
(return-from set-qnw nil)))
;; follow QN->Q links if we haven't seen rightmost edge yet
((and (hv-n v2)
(not (hv-q v2))
(< y2 y1)
(not rightmost))
(setf v2 (dll-next (hv-n v2))))
;; and Q->QW links
((and (hv-w v2)
(hv-q v2))
(assert (< y2 y1))
(setf v2 (dll-next (hv-w v2))))
((or (> y2 y1) (eql v2 v))
(error "couldn't find edge right of Q node?")))))))
nil)
(defun classify-vertex (v)
(when (and (eql v (dll-prev v))
(eql v (dll-next v)))
(return-from classify-vertex :degenerate))
(when (or (eql v (dll-prev v))
(eql v (dll-next v)))
(error "half-degenerate vertex?"))
(let* ((prev (dll-prev v))
(next (dll-next v))
(x (hv-x v))
(y (hv-y v))
(px (hv-x prev))
(py (hv-y prev))
(nx (hv-x next))
(ny (hv-y next))
(dx1 (- px x))
(dy1 (- py y))
(dx2 (- nx x))
(dy2 (- ny y)))
;; make sure we have only flat lines
(assert (and (or (zerop dx1) (zerop dy1))
(or (zerop dx2) (zerop dy2))))
(multiple-value-bind (edge corner)
(classify-vertical-edge v)
(when (eql corner :middle)
(assert (member (hv-classify v) '(:qn :qw)))
(assert (member edge '(:qn :qw :rightmost))))
(setf (hv-classify v) (or edge (hv-classify v)))
(setf (hv-corner v) corner)
(when (and (eql edge :left-notch)
(eql corner :top))
(set-qnw v))
edge)))
(defclass point ()
((x :initform 0.0 :initarg :x :accessor x)
(y :initform 0.0 :initarg :y :accessor y)
(ref :initform nil :initarg :ref :accessor ref)))
(defun make-point (x y &key ref)
(make-instance 'point :x x :y y :ref ref))
(defun point-p (x)
(typep x 'point))
;; used to indicate a point in D that is above a gap or gaps caused by
;; skipping subholes. Depending on the height of the placement and Y
;; value of the support from C, we might be able to place something
;; against the edge below this point or might not
(defclass point-gap (point)
;; list of y,h for each gap on the vertical edge ending at this
;; point
((gaps :initform nil :accessor gaps :initarg :gaps)))
(defun make-point-gap (x y gaps)
(make-instance 'point-gap :x x :y y :gaps gaps))
(defun point-gap-p (x)
(typep x 'point-gap))
;; used to indicate a point in C that can't support a placement at the
;; exact X value, but can if strictly greater (or less depending on
;; which side)
(defclass point-open (point)
())
(defun make-point-open (x y &key ref)
(make-instance 'point-open :x x :y y :ref ref))
(defun point-open-p (x)
(typep x 'point-open))
;; used to indicate a point in C that starts a span at the bottom of a
;; vertical edge, so is supported to the left without needing support
;; from D
(defclass point-bottom-left (point)
())
(defun make-point-bottom-left (x y &key ref)
(make-instance 'point-bottom-left :x x :y y :ref ref))
(defun point-bottom-left-p (x)
(typep x 'point-bottom-left))
(defclass f-edge ()
;; f-edge contains the vertical edges of the top or bottom edge of F
;; data structure, in the form of vectors of lower and upper
;; points. Gap stores amount skipped in corresponding edge by
;; Q->QN links if any
((d :accessor f-d :initarg :d)
(h :accessor f-h :initarg :h)
(gaps :accessor f-gaps :initarg :gaps :initform nil)))
;; subhole
(defclass subhole (dll)
;; link to containing hole
((hole :accessor sh-hole :initarg :hole)
;; f-edge struct for top,bottom edges of subhole
(top :accessor sh-top :initarg :top)
(bottom :accessor sh-bottom :initarg :bottom)
;; link to lower vertex of leftmost edge for the subhole
(start :accessor sh-start :initarg :start)
;; link to end of top/bottom, = Q vertex if any, otherwise lower right vertex
(endv :accessor sh-endv :initform nil)
;; x coord of right edge of hole
(end :accessor sh-end :initarg :end)
;; to be calculated in constructor
;; link to corner if subhole has a "falling corner"
(falling-corner-p :accessor sh-falling-corner-p :initarg :falling)
;; for fast rejection of things that definitely won't fit
(max-width :accessor sh-max-width :initarg :width)
(max-height :accessor sh-max-height :initarg :height)))
(defclass hole (dll)
(;; link to dll of hole-vertex
(vertices :accessor h-vertices :initform nil :initarg :vertices)
;; calculated in constructor
;; link to dll of subholes for this hole
(subholes :accessor h-subholes :initform nil)
;; link to corner if hole has a "falling corner"
(falling-corner-p :accessor h-falling-corner-p :initarg :falling
:initform nil)
;; for fast rejection of things that definitely won't fit
(max-width :accessor ht-max-width :initform 0)
(max-height :accessor ht-max-height :initform 0)))
(defmacro with-minmax ((min-var max-var minmax-fun) &body body)
(alexandria:with-gensyms (v)
`(let ((,min-var nil)
(,max-var nil))
(declare (ignorable ,min-var ,max-var))
(flet ((,minmax-fun (,v)
(setf ,min-var (min (or ,min-var ,v) ,v))
(setf ,max-var (max (or ,max-var ,v) ,v))))
,@body))))
(defun make-hole-vertex (x y &optional prev)
(let ((a (make-instance 'hole-vertex :x x :y y)))
(insert-after prev a)
a))
(defun pxy (tx ty)
(loop for x = (pop tx)
for y = (pop ty)
while (or x y)
collect (list x y)))
(defun update-subhole (subhole)
(let* ((start (dll-next (sh-start subhole)))
(top start)
(bottom (dll-prev start))
(top-x nil)
(top-y nil)
(top-gaps nil)
(bottom-x nil)
(bottom-y nil)
(ref nil)
(end nil)
(falling nil)
(x-trim nil)
(q nil))
(assert (not (eql top bottom)))
(assert (= (hv-x top) (hv-x bottom)))
(assert (> (hv-y top) (hv-y bottom)))
(with-minmax (x1 x2 mmx)
(with-minmax (y1 y2 mmy)
;; we build bottom of subhole first, since a lower notch
;; could block the subhole completely, and we don't want
;; to include any falling corner in that case.
(let ((hit-q nil)
(prev-dy nil)
(once nil)
(has-rightmost nil))
(flet ((add (x y v &optional force)
(unless (and (not force)
(eql y (car bottom-y)))
(push v ref)
(push x bottom-x)
(push y bottom-y))))
(do-hole-vertices/prev (v bottom dx dy)
;; to build subhole bottom, we walk CCW from bottom
;; (following only prev) collecting vertical edges,
;; stopping at leftwards edge or first hv-w
;; once we see hv-q, we skip to hv-w and continue
;; (following hv-w) until we see a leftwards edge,
;; collecting only the points whose y value is > y value of
;; hv-n (can be any # >= 1)
(when (and dy (not (zerop dy)))
(setf prev-dy dy))
(when (and (not hit-q)
(hv-q v))
(setf q v)
(setf hit-q v))
(cond
;; first point
((and (not dy) (not hit-q))
(add (hv-x v) (hv-y (dll-next v)) v t)
(add (hv-x v) (hv-y v) v))
;; leftwards edge, just passed right edge or left notch,
;; depending on direction of previous vertical edge
((and dx (minusp dx))
(when (and (not hit-q) (not once))
(setf has-rightmost t)
(unless q
(setf q (dll-next v))
;; we are at top of right edge, follow it to bottom corner
(loop while (= (hv-x q) (hv-x (dll-next q)))
do (setf q (dll-next q))))
(when (and prev-dy (minusp prev-dy))
(assert (hv-w q))))
;; we go 1 extra edge in case there is a falling edge
(when once
(return nil))
(unless (= (hv-y (dll-prev v))
(hv-y v))
(setf once t)))
((member (hv-classify v)
'(:leftmost :rising-edge))
(return nil))
;; upwards vertical edges
((and dy
(plusp dy)
;; ignore extra points in the middle of vertical edge
(not (and (hv-w v)
(eql (hv-x (dll-prev v))
(hv-x v))))
;; if we have seen a Q vertex for this subhole,
;; only include edges that extend above the Q
;; vertex, since ones below that can't support a
;; placement in this subhole
(or (not hit-q)
(> (hv-y v) (hv-y q)))
;; ignore any edges left of last added edge,
;; which should be from some other subhole below
;; this one
(> (hv-x v) (car bottom-x)))
(add (hv-x v) (hv-y v) v)
;; if edge extends past the QN vertex for this subhole
;; (if any), stop here and tell the top pass to stop
;; at this X coordinate
(when (and hit-q
(>= (hv-y v) (hv-y (hv-n q))))
(assert (not x-trim))
(setf x-trim (hv-x v))
(return nil)))
;; downwards edges
((and dy
(minusp dy)
(not hit-q)
(not (hv-n v))
(not (hv-q v))
(not once))
(unless hit-q
(add (hv-x v) (hv-y v) v))))
;; follow q->qw
(when (and (hv-w v) (hv-q v))
(assert (dll-next (hv-w v)))
(jump (hv-w v))))))
(let ((hit-q nil))
(flet ((add (x y &optional gap keep)
(unless (and (not keep)
(eql y (car top-y)))
(push x top-x)
(push y top-y)
(push gap top-gaps))))
(do-hole-vertices/next (v top dx dy endp)
;; to build a subhole top, we walk clockwise from start
;; collecting vertical edges until we see either a line
;; moving left (= end of top), or a QN vertex on a
;; horizontal edge (which should be the N vertex for this
;; subhole's Q node, but can't check that directly since
;; it hasn't been linked yet)
;; While walking, we need to follow Q->QN links, grouping
;; any set of edges with same X coord into 1 edge with a
;; 'gap' for every Q->QN link on that edge,
;; once we see the QN, we continue until we see a
;; leftwards edge, only collecting edges that go below the
;; y value of the hv-q (should be 1 or 2 depending on
;; whether we have a falling edge in hole and where it is)
(when (and dx (minusp dx))
(unless end
(setf end (hv-x (dll-prev v))))
(return nil))
;; if 'bottom' pass stopped early, trim top to same X coords
(when (and x-trim
(>= (hv-x v) x-trim))
;; x-trim must be right of a Q->QN link, so if we
;; haven't seen that yet something is wrong
(assert hit-q)
(add x-trim (hv-y q) nil t)
(return nil))
;; find an N vertex
(when (and (not hit-q)
(hv-n v)
(not (hv-q v)))
(setf end (hv-x v))
(setf hit-q v))
(cond
;; first point
((and (not dy) (not hit-q))
(push (hv-x v) top-x)
(push (hv-y (dll-prev v)) top-y)
(add (hv-x v) (hv-y v)))
;; upwards vertical edges
((and dy
;; we get dx,dy set when jumping past gaps
(zerop dx)
(plusp dy))
(let ((gap nil)
;; bottom of vertical edge
(v1 (dll-prev v)))
(loop while (and (hv-n v)
(hv-q v))
for n = (hv-n v)
do (push (list (hv-y v)
(- (hv-y (hv-n v))
(hv-y v)))
gap)
(setf v (hv-n v))
while (= (hv-x (dll-next v)) (hv-x v1))
do (setf v (dll-next v)))
(unless hit-q
(add (hv-x v) (hv-y v) (nreverse gap)))
(when gap
(jump (dll-next v)))))
;; downwards edges
((and dy
(minusp dy)
;; skip QW vertices in middle of edge
(not (and (hv-w v)
(not (hv-q v))
(= (hv-x v)
(hv-x (dll-next v))))))
(let ((up (extend-edge-up v #'dll-prev)))
(cond
((or (not hit-q)
(and (<= (hv-y v)
(hv-y hit-q))
(> (hv-y up)
(hv-y (hv-n hit-q)))))
(when (and (or (not hit-q)
(eql hit-q v)
(< (hv-y v)
(hv-y hit-q)))
(>= (hv-x (dll-next v))
(hv-x v)))
(assert (not falling))
(setf falling v))
(when (or (not hit-q)
(<= (hv-y v) (hv-y hit-q)))
(let ((rm (eql (hv-classify v) :rightmost)))
(add (hv-x v)
(if hit-q
(if rm
(min (hv-y (hv-n hit-q))
(hv-y v))
(max (hv-y (hv-n hit-q))
(hv-y v)))
(hv-y v))
nil rm))))
;; if we see a falling edge after end, add 1 or 2
;; more segments to close the top of subhole
((and hit-q
(or (< (hv-y v)
(hv-y (hv-n hit-q)))
(eql (hv-classify v) :rightmost)))
(when (> (hv-y (dll-prev v))
(hv-y (hv-n hit-q)))
(add (hv-x v)
(min (hv-y v)
(hv-y (hv-n hit-q)))
nil)
(loop-finish)))))))
;; follow q->n (only see them here if we already stopped
;; collecting upwards edges)
(when (and (hv-q v) (hv-n v))
(assert (dll-next (hv-n v)))
(jump (hv-n v))))))
;; once we have top/bottom, make sure the ends match properly
;; (may need to trim or extend ends depending on how previous
;; loops terminated, not sure yet)
(when (or top-y bottom-y)
(when (or (<= (length top-x) 2)
(<= (length top-y) 2))
(error "short top? ~s~%" (pxy top-x top-y)))
(when (or (<= (length bottom-x) 2)
(<= (length bottom-y) 2))
(error "short bottom? ~s ~s~%"
bottom-x bottom-y))
(when (/= (car top-x)
(car bottom-x))
(let ((end (min (car top-x)
(car bottom-x))))
(cond
((= end (car bottom-x))
(loop for (x next) = top-x
when (= x end)
return nil
when (< next end x)
do (setf (car top-x) end)
(loop-finish)
else
do (pop top-x)
(pop top-y)
(pop top-gaps)))
((= end (car top-x))
;; trim bottom (not sure this can happen?)
))))
(assert (= (length top-x) (length top-y)))
(assert (= (length bottom-x) (length bottom-y))))
(flet ((make-dh (xx yy &optional making-c refs)
(loop for y0 = nil then y
for x0 = nil then x
for x in xx
for y in yy
for ref = (pop refs)
for falling = (and y0 (< y y0))
do (mmx x) (mmy y)
when y0
collect (if (and falling making-c)
(make-point-bottom-left
x (alexandria:clamp (min y0 y) y1 y2)
:ref ref)
(make-point
x (alexandria:clamp (min y0 y) y1 y2)
:ref ref))
into d
and collect (make-point x (alexandria:clamp
(max y0 y) y1 y2)
:ref ref)
into h
finally (return (list :d (coerce d 'vector)
:h (coerce h 'vector))))))
(if (and falling
(> (hv-y falling)
(hv-y q)))
(setf (sh-falling-corner-p subhole) falling)
(setf (sh-falling-corner-p subhole) nil))
(setf (sh-top subhole) (apply
#'make-instance
'f-edge
:gaps (coerce (nreverse top-gaps)
'vector)
(make-dh (nreverse top-x)
(nreverse top-y))))
(setf (sh-bottom subhole) (apply
#'make-instance
'f-edge
(make-dh (nreverse bottom-x)
(nreverse bottom-y)
t
(nreverse ref))))
(setf (sh-end subhole) (hv-x q))
(setf (sh-endv subhole) q)
(assert (not (hv-sh q)))
(setf (hv-sh q) subhole)
(setf (sh-max-width subhole) (- x2 x1))
(setf (sh-max-height subhole) (- y2 y1)))))))
(defun make-subhole (hole start prev &key (update t))
(let ((s (make-instance 'subhole
:Falling nil
:hole hole
:top nil
:bottom nil
:end nil
:start (dll-prev start)
:width 0
:height 0)))
(setf (hv-sh (sh-start s)) s)
(when update
(update-subhole s))
(insert-after prev s)
s))
(defun clean-subholes (hole)
;; to simplify things, sometimes we want to just rebuild subholes
;; from scratch, so remove any extra vertices in the middle of an
;; edge (old N,W verts, previous corner directly under edge of
;; placement, etc), and clean up stale subhole backlinks
(flet ((r (v)
(when (eql v (h-vertices hole))
(setf (h-vertices hole) (dll-prev v)))
(delete-node v)))
(let ((c nil))
(do-dll/next (n (h-vertices hole))
(setf (hv-sh n) nil)
(cond
((and (= (hv-x (dll-prev n))
(hv-x n))
(= (hv-y (dll-prev n))
(hv-y n)))
;; repeated point, probably from touching corners,
;; was removed by other test so keep it this time
(error "shouldn't happen"))
((and (or (not (hv-n n))
(not (hv-w n)))
(or (= (hv-x (dll-prev n))
(hv-x n)
(hv-x (dll-next n)))
(= (hv-y (dll-prev n))
(hv-y n)
(hv-y (dll-next n)))))
(push n c))))
;; remove separately so we don't confuse iteration
(map nil #'r c))))
(defun make-subholes (hole &key clean)
(when clean
(clean-subholes hole))
(with-minmax (x1 x2 mmx)
(with-minmax (y1 y2 mmy)
(let ((edges (make-hash-table)))
;; find edges and bounds, build Q links, classify vertices
(do-dll/next (n (h-vertices hole))
(mmx (hv-x n))
(mmy (hv-y n))
;; run for side effects
(classify-vertex n)
(when (and (eql (hv-classify n) :leftmost)
(eql (hv-corner n) :top))
(setf (gethash n edges) n)))
(setf (ht-max-width hole) (- x2 x1))
(setf (ht-max-height hole) (- y2 y1))
(when (and clean (h-falling-corner-p hole))
(setf (hv-hole (h-falling-corner-p hole)) nil)
(setf (h-falling-corner-p hole) nil))
;; we build subholes in 2nd pass since we need to wait until
;; classify-vertex builds the QN/QW links for all nodes
(loop for e being the hash-keys of edges
for h = (make-subhole hole e nil)
then (make-subhole hole e h)
for f = (sh-falling-corner-p h)
when f
do (if (h-falling-corner-p hole)
(assert (eql f (h-falling-corner-p hole)))
(progn
(when (hv-hole f)