-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathedge-test.lisp
More file actions
175 lines (160 loc) · 6.33 KB
/
edge-test.lisp
File metadata and controls
175 lines (160 loc) · 6.33 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
#++(ql:quickload '(sdf/test))
#++(asdf:test-system 'sdf)
(in-package sdf/test)
(defun ulp+ (f n)
(etypecase f
(single-float
(float-features:bits-single-float
(+ (float-features:single-float-bits f) n)))
(double-float
(float-features:bits-double-float
(+ (float-features:double-float-bits f) n)))))
(defun make-samples (ref skip type box)
(assert (plusp skip))
(assert (< (b::raabb-y1 box) (b::raabb-y2 box)))
(destructuring-bind (type &optional bits)
(alexandria:ensure-list type)
(let* ((y1 (b::raabb-y1 box))
(y2 (b::raabb-y2 box))
(j12 (cond
((< ref y1)
(loop for j from 0
for y = (+ ref (* skip j))
for ny = (+ ref (* skip (1+ j)))
when (and (<= y y1) (< y1 ny))
collect j
when (and (< y y2) (<= y2 ny))
collect (1+ j)
and do (loop-finish)))
((> ref y2)
(reverse
(loop for j downfrom 0
for y = (+ ref (* skip j))
for ny = (+ ref (* skip (1+ j)))
when (and (< y y2) (<= y2 ny))
collect (1+ j)
when (and (<= y y1) (< y1 ny))
collect j
and do (loop-finish))))
(t
(list
(loop for j downfrom 0
for y = (+ ref (* skip j))
for ny = (+ ref (* skip (1+ j)))
when (and (<= y y1) (< y1 ny))
return j)
(loop for j from 0
for y = (+ ref (* skip j))
for ny = (+ ref (* skip (1+ j)))
when (and (< y y2) (<= y2 ny))
return (1+ j)))
)))
(j1 (- (first j12) 2))
(j2 (+ (second j12) 2))
(ns (abs (1+ (- j2 j1))))
(s (make-array ns :initial-element nil)))
(loop for j below ns
for y = (coerce (+ ref (* skip (+ j1 j))) type)
do (setf (aref s j) (if bits (ulp+ y bits) y)))
s)))
(defun shape-max-length (s)
(let ((l 0)
(m 0))
(b::map-contour-segments
s
(lambda (c a e)
(declare (ignore c))
(when (typep a 'b::point)
(incf l))
(when e
(setf m (max l m))
(setf l 0))))
m))
(defun rotate-shape (s n)
(let ((new (make-instance 'b::shape)))
(setf (slot-value new 'b::contours) (copy-seq (b::contours s)))
(setf (slot-value new 'b::%next) (alexandria:copy-hash-table (b::%next s)))
(setf (slot-value new 'b::%prev) (alexandria:copy-hash-table (b::%prev s)))
(setf (slot-value new 'b::bounding-box) (b::bounding-box s))
(setf (slot-value new 'b::rbounding-box) (b::rbounding-box s))
(loop with c = (b::contours new)
with next = (b::%next new)
for i below (length c)
do (loop repeat n
do (setf (aref c i) (gethash (aref c i) next))
(setf (aref c i) (gethash (aref c i) next)))
(unless (typep (aref c i) 'b::point)
(format t "rotated ~s by ~s = ~s~%" i n (aref c i)))
#++(assert (typep (aref c i) 'b::point)))
new))
(defmacro do-rotated-shapes ((var orig &optional (rot (gensym "I"))) &body body)
(alexandria:with-gensyms (l)
`(let ((,l (shape-max-length ,orig)))
(dotimes (,rot ,l)
(let ((,var (rotate-shape ,orig ,rot)))
,@body)))))
(defun test-shape1 (shape samples)
(let ((edges (b::%make-edge-list shape samples)))
(flet ((edge-ok (e)
(when e
(assert (evenp (length e)))
(let ((up (count :up e :key 'car))
(down (count :down e :key 'car)))
(assert (= up down)))
#++
(loop for (a b) on e
while b
do (assert (not (eql (first a) (first b))))
#++
(assert (>= (second b) (second a))))
)
t
#++
(or
(not e)
(and (evenp (length e))
(loop for (a b) on e
while b
always (and (not (eql (first a) (first b)))
(> (second b) (second a))))))))
(assert (every #'edge-ok edges))))
t)
(defun %test-shape (shape ref skip)
(loop for sample-type
in `(real double-float single-float
;; floats with +- 1 bit offset from exact value
(double-float 1) (double-float -1)
(single-float 1) (single-float -1))
for samples = (make-samples ref skip sample-type
(b::rbounding-box shape))
do (loop for shape-type in '(real double-float single-float)
for shape2 = (b::coerce-shape shape shape-type)
do (do-rotated-shapes (rshape shape2 rot)
(test-shape1 rshape samples)
"test-shape1 sample=~s @~s+~s, shape=~s,rot~s"
sample-type ref skip shape-type rot))))
(defun parse-shape (desc dx dy &optional transpose)
(let ((a (b::parse-shape desc)))
(b::clean-shape (b::translate-shape (if transpose
(b::transpose-shape a)
a)
dx dy))))
(defun test-shape (desc ref skip &optional (dx 1023) (dy 1023))
(%test-shape (parse-shape desc dx dy) ref skip)
(%test-shape (parse-shape desc dx dy t) ref skip))
(define-test shapes
(finish
(test-shape "{ 0, 0; 10, 0; 10, 10; 5, 15; 0, 10; # }" 0 1/8))
(finish
(test-shape "{ 0, 0; 10, 0; 10, 10; 5, 15; 0, 10; # }" 1/3 1/11))
(finish
(test-shape "{ 0,0; (0,2); 5,5; (10,10); 20,20; (20, 10); 20,0; 20,-10; (10,-10); #}"
0 1/40))
(finish
(test-shape "{ 313, 903; (291, 819); 292, 739; (292, 695); 296, 651; (301, 607); 306, 575; # }"
232 30 0 0)
)
)
#++
(let ((*break-on-signals* t))
(test 'shapes))