-
Notifications
You must be signed in to change notification settings - Fork 3
/
binpack2-vis.lisp
1108 lines (1027 loc) · 41.1 KB
/
binpack2-vis.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
(defpackage #:binpack2-vis
(:use :cl #:3b-glim-example/s #:3b-glim-example/s-shaders)
(:local-nicknames (#:glim #:3b-glim/s)
(#:2d #:3b-glim/2d)
(#:a #:alexandria-2)
(#:b #:binpack)))
(in-package binpack2-vis)
(defvar *format*
(glim:compile-vertex-format
'(1
(0 :vec3) ;; position
(1 :vec3) ;; normal
(2 :vec4) ;; uv
(3 :vec4) ;; color
)))
(defvar *validate* nil)
(declaim (inline vertex vertex-v color normal uv))
(defun vertex (x &optional (y 0.0) (z 0.0))
(glim:attrib-f 0 x y z))
(defun vertex-v (v)
(glim:attrib-fv 0 v))
(defun normal (x &optional (y 0.0) (z 0.0) (w 1.0))
(glim:attrib-f 1 x y z w))
(defun uv (x &optional (y 0.0) (z 0.0) (w 1.0))
(glim:attrib-f 2 x y z w))
(defun color (r g b &optional (a 1.0))
(glim:attrib-f 3 r g b a))
(defparameter *esc* nil)
(defparameter *debug* 0)
(defparameter *flags* (make-array 10 :initial-element nil))
(defparameter *kx* 0)
(defparameter *ky* 0)
(defparameter *mouse* t)
(Defparameter *undo* nil)
(defun regression-file ()
(format nil "/tmp/binpack/regression.~a.lisp" (get-universal-time)))
(defvar *replays* nil)
(defclass binpack2-vis (scratchpad)
((scale :initform 1 :accessor scale)
(draw-scale :initform 16 :accessor draw-scale)
(spacing :initform 16 :accessor spacing)
(origin :initform :center :accessor origin)
(shapes :initform nil :accessor shapes)
(x1 :initform 0 :accessor x1)
(x2 :initform 0 :accessor x2)
(y1 :initform 0 :accessor y1)
(y2 :initform 0 :accessor y2)
(gmx :initform 0 :accessor gmx)
(gmy :initform 0 :accessor gmy)
(snap :initform t :accessor snap)
(show-colors :initform nil :accessor show-colors)
(color-picker-w :initform 22 :accessor color-picker-w)
(color-picker-bits :initform 3 :accessor color-picker-bits)
(color-picker-col :initform '(1 0 0 1) :accessor color-picker-col)
(edit-point :initform nil :accessor edit-point)
(editing-point :initform nil :accessor editing-point)
(edit-point-prev :initform nil :accessor edit-point-prev)
(placing :initform nil :accessor placing)
(places :initform nil :accessor places)
(placesxy :initform (list 0 0) :accessor placesxy)
(placed :initform nil :accessor placed)
(saved :initform nil :accessor saved)
(replay :initform nil :accessor replay)
(replay-save :initform nil :accessor replay-save)
(replay-delay :initform 0.1 :accessor replay-delay)
(replay-time :initform 0 :accessor replay-time)
(pwx :initform 1 :accessor pwx)
(pwy :initform 1 :accessor pwy)
(hole :initform nil :accessor hole)
(tool :initform 'polyline :accessor tool))
(:default-initargs :shaders '((:tex :vertex vertex/simple
:fragment frag/tex)
(:solid :vertex vertex/simple
:fragment frag/solid))))
(defvar *w* nil)
(defun reset (w &key replay)
(if replay
(if (eql replay :saved)
(setf (Replay w) (or (replay-save w)
(reverse (placed w))))
(setf (replay w)
(reverse (placed w))))
(setf (replay w) nil
(replay-save w) nil))
(setf (hole w) (b::init-hole 16 16))
(when (replay w)
(unless (eql (caar (replay w)) :init)
(push (list :init 16 16) (replay w))))
(setf (placed w) nil)
(unless (replay w)
(push (list :init 16 16) (placed w))
(setf (saved w) nil))
(if replay
(setf (placing w) nil)
(setf (placing w) t))
(setf (places w) nil))
(defun replay-file (f)
(cond
((consp f)
(setf (replay *w*) f))
(t
(setf (replay *w*) (with-open-file (s f) (read s)))))
#++(format t "replaying file ~s:~% ~s~%" f (replay *w*))
(unless (eql (caar (replay *w*)) :init)
(push (list :init 16 16) (replay *w*)))
(setf (replay-save *w*) (replay *w*)))
(defun save-regression (w)
(with-open-file (f (regression-file) :direction :output
:if-does-not-exist :create)
(write (reverse (placed w))
:stream f :readably t)))
(defmacro with-regression-restart ((w) &body body)
`(restart-case
(progn ,@body)
(:retry ()
:report (lambda (s)
(format s "save regression test if needed, then reset and replay it"))
(unless (saved ,w)
(save-regression w))
(reset ,w :replay t))
(:dump-and-restart ()
:report (lambda (s)
(format s "save regression test, then reset and continue"))
(unless (saved ,w)
(save-regression ,w))
(reset ,w))
(:cancel-replay ()
:report (lambda (s)
(format s "reset and stop replay"))
(reset ,w :replay :saved)
(setf (replay ,w) nil))
(:stop-replay ()
:report (lambda (s)
(format s "stop replay!"))
(setf (placing ,w) nil)
(setf (pwx ,w) nil
(pwy ,w) nil)
(setf (replay ,w) nil))))
(defun save-undo (w)
(push (shapes w) *undo*))
#++
(setf (color-picker-w *w*) 12
(color-picker-bits *w*) 4)
#++
(setf (color-picker-w *w*) 4
(color-picker-bits *w*) 5)
#++
(setf (color-picker-w *w*) 16
(color-picker-bits *w*) 3)
(defclass point ()
((x :initform 0 :accessor x :initarg :x)
(y :initform 0 :accessor y :initarg :y)))
(defclass shape ()
((rgba :initform '(1 0 0 1) :accessor rgba :initarg :rgba)
(vertex-rgba :initform '(1 0.5 1 1) :accessor vertex-rgba)
(drawing :initform t :accessor drawing)))
(defclass polyline (shape)
((points :initform (make-array 0 :adjustable t :fill-pointer 0)
:reader points)
(closed :initform nil :accessor closed)))
(defmethod draw-shape ((w binpack2-vis) (s polyline))
(glim:with-draw (:lines :shader :solid)
(apply #'color (rgba s))
(loop for prev = nil then p
for p across (points s)
when prev
do (vertex (x prev) (y prev))
(vertex (x p) (y p))
finally (when (and p (closed s))
(let ((e (aref (points s) 0)))
(vertex (x p) (y p))
(vertex (x e) (y e))))
(when (and p (drawing s))
(color 1 1 1 1)
(vertex (x p) (y p))
(vertex (gmx w) (gmy w)))))
(when (vertex-rgba s)
(gl:point-size 3)
(glim:with-draw (:points :shader :solid)
(apply #'color (vertex-rgba s))
(loop for p across (points s)
do (vertex (x p) (y p)))))
(dispatch-draws w))
(defmethod click-shape ((w binpack2-vis) (s null) x y)
(push (make-instance (tool w) :rgba (color-picker-col w)) (shapes w))
(assert (not (closed (car (shapes w)))))
(click-shape w (car (shapes w)) x y))
(defmethod click-shape ((w binpack2-vis) (s polyline) x y)
(cond
((drawing s)
(if (and (plusp (length (points s)))
(= x (x (aref (points s) 0)))
(= y (y (aref (points s) 0))))
(progn
(setf (closed s) t)
(setf (drawing s) nil))
(vector-push-extend (make-instance 'point :x x :y y)
(points s))))
(t (click-shape w nil x y))))
(defmethod print-shape ((s polyline))
(format t "polyline: ~s points, :closed ~s~%" (length (points s))
(closed s))
(loop for p across (points s)
do (format t " ~s ~s~%" (x p) (y p))))
(defun translate-mouse (w x y &key snap)
(let ((mx (+ (x1 w)
(/ x (/ (wx w)
(- (x2 w) (x1 w))))))
(my (+ (y1 w)
(/ (- (wy w) y)
(/ (wy w)
(- (y2 w) (y1 w)))))))
(if (and snap (snap w))
(let ((s (spacing w)))
(values (* s (round mx s))
(* s (round my s))))
(values mx my))))
(defun uniforms ()
(glim:uniform 'modelview (glim:ensure-matrix :modelview))
(glim:uniform 'proj (glim:ensure-matrix :projection))
#++
(glim:uniform 'mvp (sb-cga:matrix*
(glim:ensure-matrix :projection)
(glim:ensure-matrix :modelview)))
(glim:uniform 'normal-matrix (glim:ensure-matrix :modelview)))
(defmacro with-polyline ((&key (rgba ''(1 0 0 1))
(close t))
&body body)
`(when *w*
(progn
(finish-shape *w*)
(start-polyline :rgba ,rgba)
,@body
(finish-shape *w* :close ,close))))
(defparameter *wait* nil)
(defun do-replay (w)
(restart-case
(let ((now (/ (get-internal-real-time) internal-time-units-per-second)))
(when (and (> now (+ (replay-time w)
#++(replay-delay w)))
(not (eql *wait* 0)))
(when (numberp *wait*)
(decf *wait*))
(setf (replay-time w) now)
(let ((r (pop (replay w))))
(push r (placed w))
#++ (format t "~%replay ~s~%" r)
(let (#++(*standard-output* (make-broadcast-stream)))
(ecase (car r)
(:init
(setf (placed w) nil)
(setf (placing w) nil)
(setf (places w) nil)
(setf (pwx w) nil
(pwy w) nil)
(setf (hole w) (apply #'b::init-hole (cdr r))))
(:show
(setf (pwx w) (second r)
(pwy w) (third r))
(setf (places w) nil)
(with-polyline (:rgba `(1 0 1 1) :close t)
(add-shape-point 0 0)
(add-shape-point 0 (pwy w))
(add-shape-point (pwx w) (pwy w))
(add-shape-point (pwx w) 0))
#++(format t "pw* = ~s, ~s~%" (pwx w) (pwy w))
(setf (placing w) nil)
(when (or (not (places w))
(/= (pwx w) (first (placesxy w)))
(/= (pwy w) (second (placesxy w))))
(setf (places w)
(binpack::find-all-placements (hole w)
(pwx w) (pwy w)))
(setf (placesxy w) (list (pwx w) (pwy w))))
(when *validate*
(let ((x (loop for p in (places w)
unless (b::valid-placement-p* p)
collect p)))
(when x
(break "invalid placement(s) ~sx~s @ ~s?"
(b::w (car x)) (b::h (car x))
(mapcar 'b::pp x)))))
#++(format t "places = ~s~%" (places w)))
(:place
(setf (pwx w) nil
(pwy w) nil)
(destructuring-bind (x y wx wy) (cdr r)
(glim:with-draw (:quads :shader :solid)
(color 0 1 0 1)
(let ((x1 (* x (spacing w)))
(x2 (* (+ x wx) (spacing w)))
(y1 (* y (spacing w)))
(y2 (* (+ y wy) (spacing w))))
(vertex x1 y1)
(vertex x2 y1)
(vertex x2 y2)
(vertex x1 y2)))
(dispatch-draws w)
(let ((ok nil))
(loop for p in (places w)
#+do (format *debug-io* "~s,~s ~sx~s =? ~s,~s ~sx~s?"
x y wx wy
(b::x p) (b::y p)
(b::w p) (b::h p))
when (and (= x (b::x p))
(= y (b::y p))
(= wx (b::w p))
(= wy (b::h p)))
do (setf (hole w)
(b::remove-quad-from-hole (hole w) p))
(setf ok t)
(loop-finish))
(unless ok
#++(glut:swap-buffers)
(let ((x1 x)
(x2 (+ x wx))
(y1 y)
(y2 (+ y wy)))
(with-polyline (:rgba `(1 1 1 1) :close t)
(add-shape-point x1 y1)
(add-shape-point x2 y1)
(add-shape-point x2 y2)
(add-shape-point x1 y2)))
(break "failed to place ~sx~s @ ~s,~s?"
wx wy x y)))))))
(when (and (not (replay w))
(not (places w)))
(setf (placing w) t
(pwx w) nil
(pwy w) nil))
(when (and (hole w) *validate*)
(b::check-hole (hole w)))
(redraw-hole w)
(when (and (eql (car r) :show)
(pwx w)
(pwy w))
(with-polyline (:rgba `(1 0 1 1) :close t)
(add-shape-point 0 0)
(add-shape-point 0 (pwy w))
(add-shape-point (pwx w) (pwy w))
(add-shape-point (pwx w) 0))))))
(:replay ()
:report "replay the placements"
(reset w :Replay :saved))
(:cancel-replay ()
:report "cancel replay and reset"
(reset w :Replay :saved)
(setf (replay w) nil)
(setf (placing w) t)
(redraw-hole w))
(:stop-replay ()
:report "cancel replay"
(setf (replay w) nil)
(setf *replays* nil)
(setf (placing w) t)
(redraw-hole w))))
(defmethod display ((w binpack2-vis) now)
(setf *w* w)
(when (and *replays* (not (replay w)))
(when (> (length *replays*) 2)
(format t "~s replays left~%" (length *replays*)))
(replay-file (#-sbcl pop
#+sbcl sb-ext:atomic-pop
*replays*)))
(glim:with-state (*format*)
(when (editing-point w)
(setf (x (editing-point w)) (gmx w)
(y (editing-point w)) (gmy w)))
(glim:uniform 'proj sb-cga:+identity-matrix+)
(glim:matrix-mode :projection)
(glim:load-identity)
(glim:matrix-mode :modelview)
(glim:load-identity)
(gl:enable :depth-test :sample-alpha-to-coverage
:polygon-smooth :line-smooth
:blend :multisample :texture-2d)
(gl:blend-func :src-alpha :one-minus-src-alpha)
(gl:disable :cull-face :lighting :light0 :light1
:depth-test :sample-alpha-to-coverage)
(glim:uniform 'debug1 *debug*)
(glim:uniform 'tex0 0)
(glim:uniform 'tex1 1)
(glim:uniform 'flip 0)
(let* ((x1 0)
(y1 0)
(scale (scale w))
(step (spacing w))
(x2 (/ (wx w) scale))
(y2 (/ (wy w) scale)))
(glim:with-pushed-matrix (:modelview)
(ecase (origin w)
(:center
(setf x2 (/ (wx w) scale))
(setf y2 (/ (wy w) scale))
(setf x1 (- x2))
(setf y1 (- y2)))
(:lower-left
(glim:translate -1 -1 0))
;; todo: add others
)
(when (aref *flags* 1)
(incf x1 (/ (wx w) 2 scale))
(incf y1 (/ (wy w) 2 scale))
(incf x2 (/ (wx w) 2 scale))
(incf y2 (/ (wy w) 2 scale))
(glim:translate -0.5 -0.5 0))
(when (aref *flags* 2)
(incf x1 (/ (wx w) 4 scale))
(incf y1 (/ (wy w) 4 scale))
(incf x2 (/ (wx w) 4 scale))
(incf y2 (/ (wy w) 4 scale))
(glim:translate -0.25 -0.25 0))
(glim:scale (/ (wx w)) (/ (wy w)) 1)
(glim:translate 0.5 0.5 0)
(glim:scale scale scale 1)
(uniforms)
(setf (x1 w) x1)
(setf (x2 w) x2)
(setf (y1 w) y1)
(setf (y2 w) y2)
(setf (values (gmx w) (gmy w))
(translate-mouse w (mx w) (my w)))
;(format t "x1=~s,~s, 2=~s,~s~%" x1 y1 x2 y2)
(when (replay w)
(do-replay w))
(glim:with-draw (:lines :shader :solid)
(color 0.2 0.1 0.2 1)
(when (minusp y1)
(loop for x from 0 downto x1 by step
do (vertex x y1)
(vertex x y2)))
(loop for x from 0 upto x2 by step
do (vertex x y1)
(vertex x y2))
(when (minusp y1)
(loop for y from 0 downto y1 by step
do (vertex x1 y)
(vertex x2 y)))
(loop for y from 0 upto y2 by step
do (vertex x1 y)
(vertex x2 y))
(color 0.2 0.2 0.2 1)
(vertex -500 -500)
(vertex 500 500)
(vertex -500 500)
(vertex 500 -500)
(color 0.2 0.2 0.4 1)
(vertex x1 0)
(vertex x2 0)
(vertex 0 y1)
(vertex 0 y2))
(with-regression-restart (w)
(when (and (placing w) (hole w))
(flet ((s (x) (* (spacing w)
(if (minusp x) -1 1)
(ceiling (abs x) (spacing w)))))
(let* ((mx (s (gmx w)))
(my (s (gmy w)))
(in 2))
(glim:with-draw (:lines :shader :solid)
(color 1 0.1 1.0 1)
(vertex 0 0) (vertex 0 (s my))
(vertex 0 (s my)) (vertex (s mx) (s my))
(vertex (s mx) (s my)) (vertex (s mx) 0)
(vertex (s mx) 0) (vertex 0 0))
(when (and (not (zerop mx)) (not (zerop my)))
(glim:with-draw (:quads :shader :solid)
(color 0 1 0 0.1)
(let* ((mx (/ (abs mx) 16))
(my (/ (abs my) 16))
(px (binpack::find-all-placements (hole w) mx my)))
(setf (places w) px)
(loop with mx = (* mx 16)
with my = (* my 16)
for p in px
for x = (* 16 (binpack::x p))
for y = (* 16 (binpack::y p))
do (vertex (+ x in) (+ y in))
(vertex (+ x in) (+ y my (- in)))
(vertex (+ x mx (- in) in) (+ y my (- in)))
(vertex (+ x mx (- in) in) (+ y in))))))))))
(when (and (not (placing w)) (places w))
(let* ((mx (/ (gmx w) 16))
(my (/ (gmy w) 16))
(in 2))
(color 0 1 0 0.1)
(loop for p in (places w)
for x1 = (* 16 (b::x p))
for y1 = (* 16 (b::y p))
for x2 = (+ x1 (* 16 (b::w p)))
for y2 = (+ y1 (* 16 (b::h p)))
when (b::point-in-rect p mx my)
do (glim:with-draw (:quads :shader :solid)
(vertex (+ x1 in) (+ y1 in))
(vertex (+ x1 in) (+ y2 (- in)))
(vertex (+ x2 (- in) in) (+ y2 (- in)))
(vertex (+ x2 (- in) in) (+ y1 in))))))
(dispatch-draws w)
(ignore-errors
(when (shapes w)
(loop for s in (reverse (shapes w)) do (draw-shape w s))))
(dispatch-draws w)))
(when (show-colors w)
(glim:with-pushed-matrix (:modelview)
(glim:load-identity)
(glim:translate -1 -1 0)
(glim:scale (/ 2 (wx w)) (/ 2 (wy w)) 1)
(uniforms)
(let* ((ww (color-picker-w w))
(bits (color-picker-bits w))
(cols (expt 2 bits))
(rows (* cols (floor (wy w) (* cols ww)))))
(assert (< bits 6))
(glim:with-draw (:quads :shader :solid)
(loop for div = (expt 2.0 bits)
for c from 0 below (expt 2 (* bits 3))
for x1 = (* ww (+ (mod (floor c) cols)
(* cols (floor c (* rows cols)))))
for y1 = (* ww (floor (mod c (* rows cols))
cols))
for x2 = (+ x1 ww)
for y2 = (+ y1 ww)
do (color (/ (ldb (byte bits 0) c) div)
(/ (ldb (byte bits bits) c) div)
(/ (ldb (byte bits (* bits 2)) c) div)
1)
(vertex x1 y1)
(vertex x1 y2)
(vertex x2 y2)
(vertex x2 y1))))
(dispatch-draws w)))))
(defmethod redraw-hole ((w binpack2-vis))
(when (hole w)
(clear-shapes)
(let ((*w* w))
(with-regression-restart (w)
(b::do-dll/next (hole (hole w))
(draw-hole hole (aref *flags* 7))
(when (and (aref *flags* 8) (pwx w))
(draw-cd hole (pwx w)))
(when (and (pwx w) (pwy w))
(let ((l (list :show (pwx w) (pwy w))))
(unless (equalp l (car (placed w)))
(push l (placed w))))
(draw-placements hole (pwx w) (pwy w))))))))
(defmethod mouse ((w binpack2-vis) button state x y)
(format t "~s ~s~%" button state)
(setf *esc* nil)
(when (eql state :down)
(case button
(:left-button
(cond
((placing w)
(setf (pwx w) (ceiling (abs (gmx w)) (spacing w)))
(setf (pwy w) (ceiling (abs (gmy w)) (spacing w)))
(redraw-hole w)
(setf (placing w) nil))
(t
(when (and (hole w) (places w))
(with-regression-restart (w)
(loop for p in (places w)
when (b::point-in-rect p (/ (gmx w) 16)
(/ (gmy w) 16))
do (push (list :place
(b::x p) (b::y p)
(b::w p) (b::h p))
(placed w))
(setf (hole w)
(b::remove-quad-from-hole (hole w) p))
(loop-finish))
(when (hole w)
(b::do-dll/next (h (hole w))
(b::check-hole h)))
(setf (places w) nil
(pwx w) nil
(pwy w) nil
(placing w) t)
(redraw-hole w))))))
(:right-button
(when (shapes w)
(setf (closed (first (shapes w)))
t))))))
(defun finish-shape (w &key close)
(when (shapes w)
(when close
(setf (closed (first (shapes w))) t))
(setf (drawing (first (shapes w))) nil)))
(defun rnd-rgb (&key (alpha 1))
(let ((a (random (* 2 pi))))
(list (1+ (sin a))
(1+ (sin (+ a (* 1/3 pi))))
(1+ (sin (+ a (* 2/3 pi))))
alpha)))
(defun clear-shapes ()
(when *w*
(setf (shapes *w*) nil)))
(defun add-shape-points (points &key (close t))
(when *w*
(finish-shape *w*)
(push (make-instance 'polyline) (shapes *w*))
(let ((s (draw-scale *w*)))
(loop for (x y) on points by #'cddr
do (vector-push-extend (make-instance 'point :x (* s x) :y (* s y))
(points (first (shapes *w*))))))
(finish-shape *w* :close close)))
(defun start-polyline (&key (rgba '(1 0 0 1)))
(when *w*
(finish-shape *w*)
(push (make-instance 'polyline :rgba rgba) (shapes *w*))))
(defun add-shape-point (x y)
(when *w*
(let ((s (draw-scale *w*)))
(vector-push-extend (make-instance 'point :x (* s x) :y (* s y))
(points (first (shapes *w*)))))))
(defmethod mouse-wheel ((window binpack2-vis) button state x y)
(format t "wheel ~s ~s~%" button state)
(if (eql state :up)
(setf (scale window) (* (scale window) 1.1))
(setf (scale window) (/ (scale window) 1.1))))
(defun cancel-edit-point (w)
(setf (edit-point w) nil)
(when (editing-point w)
(setf (x (editing-point w)) (car (edit-point-prev w))
(y (editing-point w)) (cdr (edit-point-prev w))))
(setf (editing-point w) nil))
(defmethod keyboard-up ((window binpack2-vis) key x y)
(format t "key up ~s~%" key)
(case key
(:key-left-ctrl
(setf (show-colors window) nil))
(:key-left-alt
(when (edit-point window)
(format t "end edit point~%")
(cancel-edit-point window)))))
(defmethod keyboard :around ((window binpack2-vis) key x y)
(cond
((and (eql key #\q) *esc*)
(destroy-window window))
((and (eql key #\d) *esc*)
(pop (shapes window)))
((eql key #\esc)
(cond
((and (shapes window) (drawing (car (shapes window))))
(pop (shapes window)))
((edit-point window)
(cancel-edit-point window)))
(save-undo window)
(setf *esc* t))
(t
(setf *esc* nil)
(call-next-method))))
(defmethod keyboard ((window binpack2-vis) key x y)
(declare (ignore x y))
(print key)
(case key
(:key-left-ctrl (setf (show-colors window) t))
(:key-left-alt (setf (edit-point window) t))
(:key-right
(setf *mouse* nil))
(:key-left
(setf *mouse* nil))
(:key-down
(setf *mouse* nil))
(:key-up
(setf *mouse* nil))
((#\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
(let ((i (digit-char-p key)))
(when i
(setf (aref *flags* i) (not (aref *flags* i)))
(format t "flags ~s -> ~s~%" i (aref *flags* i)))
(when (>= i 7)
(redraw-hole window))))
#++(#\p
(finish-shape window)
(setf (tool window) 'polyline)
(push (make-instance 'polyline :rgba (color-picker-col window))
(shapes window)))
(#\p (setf (placing window) (not (placing window))))
(#\r
(reset window :replay :saved))
(#\c
(when (hole window)
(b::check-hole (hole window))))
(#\s
(save-regression window))
(#\w
(setf *wait* 1))
(#\i
(reset window)
(redraw-hole window))
(#\d
(loop for s in (shapes window)
do (print-shape s)))
(#\l
(glut:reshape-window 1024 768))
#++(#\d
(unless *debug* (setf *debug* 0))
(setf *debug* (mod (1+ *debug*) 4))
(format t "debug = ~s~%" *debug*))
(#\Esc
(glut:destroy-current-window))))
(defmethod entry ((w binpack2-vis) state)
(format t "mouse -> ~s~%" state)
#++(when (eql state :left) (break "enter ~s~%" state))
(setf *mouse* (eql state :entered)))
(defmethod init-gl ((w binpack2-vis))
(setf *esc* nil)
(gl:pixel-store :unpack-alignment 1)
(gl:disable :dither))
(defun binpack2-vis (&rest args)
(glut:display-window (apply #'make-instance 'binpack2-vis args)))
(defun draw-hole (hole &optional draw-subholes)
(with-polyline (:close t)
(b::do-dll/next (n (b::h-vertices hole))
(add-shape-point (b::hv-x n) (b::hv-y n))))
(when draw-subholes
(let ((ofs 0))
;(b::check-hole hole)
(b::do-dll/next (n (b::h-subholes hole))
(incf ofs)
#++
(with-polyline (:rgba (rnd-rgb) :close t)
(block nil
(b::do-hole-vertices/next (v (b::sh-start n) dx dy)
(add-shape-point (+ ofs (b::hv-x v)) (+ ofs (b::hv-y v)))
(when (and dy (zerop dy) (b::hv-q v))
(b::jump (b::hv-q v))
#++(setf v (b::dll-prev (b::hv-q v)))
#++(add-shape-point (+ ofs (b::hv-x v)) (+ ofs (b::hv-y v))))
(when (and dx (zerop dx) (b::hv-n v))
(b::jump (b::hv-n v))
#++(setf v (b::dll-prev (b::hv-n v)))
#++
(add-shape-point (+ ofs (b::hv-x v))
(+ ofs (b::hv-y v)))))))
(let* ((c1 (1+ (floor ofs)))
(col #++(rnd-rgb)
(list (ldb (byte 1 0) c1)
(ldb (byte 1 1) c1)
(ldb (byte 1 2) c1)
1))
(ofs (/ ofs 16)))
(with-polyline (:rgba col :close nil)
(loop
for d across (b::f-d (b::sh-top n))
for h across (b::f-h (b::sh-top n))
for x = (b::x d)
for y1 = (b::y d) then (if (= y2 (b::y d))
(b::y d)
(b::y h))
for y2 = (b::y h) then (if (= y1 (b::y d))
(b::y h)
(b::y d))
for gaps across (b::f-gaps (b::sh-top n))
do (add-shape-point (+ ofs (b::x d)) (+ ofs y1))
(add-shape-point (+ ofs x) (+ ofs y2))))
#++
(with-polyline (:rgba col :close nil)
(loop with end = (b::sh-end n)
for d across (b::f-d (b::sh-top n))
for h across (b::f-h (b::sh-top n))
for x = (b::x d)
for y1 = (b::y d) then (if (= y2 (b::y d))
(b::y d)
(b::y h))
for y2 = (b::y h) then (if (= y1 (b::y d))
(b::y h)
(b::y d))
for gap across (b::f-gap (b::sh-top n))
when (> x end)
do (add-shape-point (+ ofs end) (+ ofs y1))
and return nil
do (add-shape-point (+ ofs (b::x d)) (+ ofs y1))
(when gap
(add-shape-point (+ ofs x) (+ ofs (- y2 gap)))
(add-shape-point (+ ofs x 1)
(+ ofs (- y2 (* 1/2 gap))))
(add-shape-point (+ ofs x -1)
(+ ofs (- y2 (* 1/2 gap)))))
(add-shape-point (+ ofs x) (+ ofs y2))))
#++
(with-polyline (:rgba (mapcar (lambda (a) (* a 0.7)) col)
:close nil)
(loop with end = (b::sh-end n)
for x1 = nil then x
for d across (b::f-d (b::sh-top n))
for h across (b::f-h (b::sh-top n))
for x = (b::x d)
for y1 = (b::y d) then (if (= y2 (b::y d)) (b::y d) (b::y h))
for y2 = (b::y h) then (if (= y1 (b::y d)) (b::y h) (b::y d))
when (and x1 (<= x1 end x))
do (add-shape-point (+ ofs end) (+ ofs y1))
when (< end x)
do (add-shape-point (+ ofs (b::x d)) (+ ofs y1))
(add-shape-point (+ ofs (b::x h)) (+ ofs y2))))
(incf ofs (/ 0.5 16))
(with-polyline (:rgba col :close nil)
(loop with end = (b::sh-end n)
for d across (b::f-d (b::sh-bottom n))
for h across (b::f-h (b::sh-bottom n))
for x = (b::x d)
for y1 = (b::y h) then (if (= y2 (b::y d)) (b::y d) (b::y h))
for y2 = (b::y d) then (if (= y1 (b::y d)) (b::y h) (b::y d))
when (> x end)
do (add-shape-point (+ ofs end) (+ ofs y1))
and return nil
do (add-shape-point (+ ofs (b::x d)) (+ ofs y1))
(add-shape-point (+ ofs (b::x h)) (+ ofs y2))))
(with-polyline (:rgba (mapcar (lambda (a) (* a 0.6)) col)
:close nil)
(loop with end = (b::sh-end n)
for x1 = nil then x
for d across (b::f-d (b::sh-bottom n))
for h across (b::f-h (b::sh-bottom n))
for x = (b::x d)
for y1 = (b::y h) then (if (= y2 (b::y d)) (b::y d) (b::y h))
for y2 = (b::y d) then (if (= y1 (b::y d)) (b::y h) (b::y d))
when (and x1 (<= x1 end x))
do (add-shape-point (+ ofs end) (+ ofs y1))
when (< end x)
do (add-shape-point (+ ofs (b::x d)) (+ ofs y1))
(add-shape-point (+ ofs (b::x h)) (+ ofs y2))))
(incf ofs (/ -0.5 16)))))))
(defun draw-cd (hole l1)
(let ((ofs 0))
(b::do-dll/next (n (b::h-subholes hole))
(incf ofs)
(let* ((c1 (1+ (floor ofs)))
(col (list (ldb (byte 1 0) c1)
(ldb (byte 1 1) c1)
(ldb (byte 1 2) c1)
1)))
(let* ((l (* l1))
(c (b::make-c (b::sh-bottom n) l (b::sh-end n)))
(ofs (/ (+ ofs 2) 16)))
#++(format t "c = ~s~%"
(loop for i across c collect (list (b::x i) (b::y i))))
(loop for (p1 p2) on (coerce c 'list) by #'cddr
for i from 0 by (/ 0.3 16)
do (with-polyline (:rgba col ;(rnd-rgb)
:close nil)
(add-shape-point (+ ofs (b::x p1))
(+ ofs (b::y p1) i))
#++(add-shape-point (+ ofs (b::x p1) l)
(+ ofs (b::y p1) i))
(add-shape-point (+ ofs (b::x p2))
(+ ofs (b::y p2) i))
(add-shape-point (+ ofs (b::x p2))
(+ ofs (b::y p2) i 3/16)))
(typecase p1
(b::point-open
(with-polyline (:rgba col ;(rnd-rgb)
:close nil)
(add-shape-point (+ ofs (b::x p1) -3/16)
(+ ofs (b::y p1) -3/16))
(add-shape-point (+ ofs (b::x p1))
(+ ofs (b::y p1)))
(add-shape-point (+ ofs (b::x p1) +3/16)
(+ ofs (b::y p1) -3/16))))
(b::point-bottom-left
(with-polyline (:rgba col ;(rnd-rgb)
:close nil)
(add-shape-point (+ ofs (b::x p1) -3/16)
(+ ofs (b::y p1) 3/16))
(add-shape-point (+ ofs (b::x p1))
(+ ofs (b::y p1)))
(add-shape-point (+ ofs (b::x p1) +3/16)
(+ ofs (b::y p1) 3/16)))))
(when (typep p2 'b::point-open)
(with-polyline (:rgba col ;(rnd-rgb)
:close nil)
(add-shape-point (+ ofs (b::x p2) -3/16)
(+ ofs (b::y p2) -3/16))
(add-shape-point (+ ofs (b::x p2))
(+ ofs (b::y p2)))
(add-shape-point (+ ofs (b::x p2) +3/16)
(+ ofs (b::y p2) -3/16)))))
(let* ((d (b::make-d (b::sh-top n) l (b::sh-end n)
(b::sh-falling-corner-p n)))
(ofs (max 1/16 (+ ofs 1/16))))
#++(format t "d = ~s~%"
(loop for i across d collect (list (b::x i) (b::y i))))
(loop for (p1 p2) on (coerce d 'list) by #'cddr
for i from 0 by (/ 0.3 16)
do (with-polyline (:rgba col ;(rnd-rgb)
:close nil)
#++(when (and (typep p2 'b::point-gap)
(b::gaps p2))
(add-shape-point (+ ofs (b::x p1))
(+ (- ofs)
(b::y p2)
(- (b::gap p2))
i)))
(add-shape-point (+ ofs (b::x p1))
(+ (- ofs) (b::y p1) i))
#++(add-shape-point (+ ofs (b::x p1) l)
(+ ofs (b::y p1) i))
(when p2
(add-shape-point (+ ofs (b::x p2))
(+ (- ofs) (b::y p2) i))
(add-shape-point (+ ofs (b::x p2))
(+ (- ofs) (b::y p2) i -4/16))))
(when (b::point-open-p p1)
(with-polyline (:rgba col ;(rnd-rgb)
:close nil)
(add-shape-point (+ ofs (b::x p1) -2/16)
(+ (- ofs) (b::y p1) -2/16))
(add-shape-point (+ ofs (b::x p1))
(+ (- ofs) (b::y p1)))
(add-shape-point (+ ofs (b::x p1) +2)
(+ (- ofs) (b::y p1) -2/16)))))))))))
(defun draw-placements (hole w h)
(let* ((ofs 1)
(in 2/16)
(c1 (1+ (floor ofs)))
(col (list (ldb (byte 1 0) c1)
(ldb (byte 1 1) c1)
(ldb (byte 1 2) c1)
1)))
(let ((px (b::find-all-placements hole w h)))
(loop for p in px
for x = (b::x p)
for y = (b::y p)
for w = (b::w p)
for h = (b::h p)
;for in from in by 1