-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.lisp
313 lines (290 loc) · 13.7 KB
/
tests.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
#++(ql:quickload '(sdf/test))
(in-package sdf/test)
(defun v2= (b a)
(and (typep b 'b:v2)
(eql (aref a 0) (b:vx b))
(eql (aref a 1) (b:vy b))))
(defun v2~ (b a eps-mul)
(or (eql a b)
(and (< (abs (- (aref a 0) (b:vx b))) (* eps-mul double-float-epsilon))
(< (abs (- (aref a 1) (b:vy b))) (* eps-mul double-float-epsilon)))))
(define-test v2
;;(fail (b:v2))
;;(fail (b:v2 1))
(of-type (simple-array double-float (2)) (b:v2 1 2))
(of-type b:v2 (b:v2 1 2))
(is eql 1d0 (b:vx (b:v2 1 2)))
(is eql 2d0 (b:vy (b:v2 1 2)))
;;(fail (b:v2 1 2 3))
(is v2= #(2d0 3d0) (b:v2- (b:v2 6 5) (b:v2 4 2)))
(is v2= #(4d0 6d0) (b:v2+ (b:v2 1 2) (b:v2 3 4)))
(is v2= #(8d0 15d0) (b:v2h* (b:v2 2 3) (b:v2 4 5)))
(is eql -2d0 (b:v2x (b:v2 2 3) (b:v2 4 5)))
(is eql 23d0 (b:v2. (b:v2 2 3) (b:v2 4 5)))
(is eql 2d0 (b:v2dist (b:v2 2 3) (b:v2 4 3)))
(is eql 3d0 (b:v2dist (b:v2 0 0) (b:v2 0 3)))
(is eql 5d0 (b:v2dist (b:v2 2 3) (b:v2 5 7)))
(is eql (sqrt 2d0) (b:v2mag (b:v2 1 1)))
(is v2= #(6d0 9d0) (b:v2scale (b:v2 2 3) 3d0))
(is v2= #(0.6d0 0.8d0) (b:v2n (b:v2 15 20)))
(is v2= #(-4d0 3d0) (b:v2rx (b:v2 3 4)))
)
#++
(test 'v2)
(defun a= (aa r)
(and (typep aa 'b::aabb)
(v2= (b:aabb-p1 aa) (first r))
(v2= (b:aabb-p2 aa) (second r))))
(defun p= (p r)
(v2= (b::p-dv p) r))
(defun s= (s r)
(and (typep s 'b::segment)
(p= (b:s-p1 s) (first r))
(p= (b:s-p2 s) (second r))))
(define-test geom
(let ((a (finish (b:make-aabb 0 0 0 0))))
(is a= '(#(0d0 0d0) #(0d0 0d0)) a)
(finish (b:update-aabb a -1 0))
(is a= '(#(-1d0 0d0) #(0d0 0d0)) a)
(finish (b:update-aabb a 1 -2))
(is a= '(#(-1d0 -2d0) #(1d0 0d0)) a)
(finish (b:update-aabb a -2 2))
(is a= '(#(-2d0 -2d0) #(1d0 2d0)) a)
(finish (b:update-aabb a 0 0))
(is a= '(#(-2d0 -2d0) #(1d0 2d0)) a)
(finish (b:update-aabb a -3 -4))
(is a= '(#(-3d0 -4d0) #(1d0 2d0)) a)
(is eql -3d0 (b:aabb-x1 a))
(is eql -4d0 (b:aabb-y1 a))
(is eql 1d0 (b:aabb-x2 a))
(is eql 2d0 (b:aabb-y2 a)))
(is p= #(0d0 0d0) (b:make-point))
(let ((p (finish (b:make-point 1 2))))
(is p= #(1d0 2d0) p)
(is eql 1d0 (b::p-dx p))
(is eql 2d0 (b::p-dy p))
(b::with-dpoint (p x y)
(is eql 1d0 x)
(is eql 2d0 y)
(true (b:point= p (b:make-point 1 2)))
(false (b:point= p (b:make-point 2 1)))
(false (b:point= p (b:make-point 1 1)))
(false (b:point= p (b:make-point 2 2)))
#++ ;; not mutable for now
(progn
(finish (setf x 3d0 y 4d0))
(is p= #(3d0 4d0) p))))
(let ((s (finish (b:make-segment 1 2 3 4))))
(is p= #(1d0 2d0) (b:s-p1 s))
(is p= #(3d0 4d0) (b:s-p2 s))
(is eql 1d0 (b::s-dx1 s))
(is eql 2d0 (b::s-dy1 s))
(is eql 3d0 (b::s-dx2 s))
(is eql 4d0 (b::s-dy2 s))
(is s= '(#(3d0 4d0) #(5d0 6d0)) (b:make-segment/p (b:make-point 3 4)
(b:make-point 5 6))))
(flet ((test-tan (x1 y1 xc yc ax2 ay2 bx2 by2)
(flet ((test-tan ()
(let* ((p1 (b::make-point x1 y1))
(c1 (b::make-point xc yc))
(a2 (b::make-point ax2 ay2))
(b2 (b::make-point bx2 by2))
(ba (b::%make-bezier2 p1 c1 a2))
(bb (b::%make-bezier2 p1 c1 b2))
(i (b::%intersect/tan p1 c1 a2 b2))
(eps (max (abs x1) (abs y1)
(abs xc) (abs yc)
(abs ax2) (abs ay2)
(abs bx2) (abs by2)))
(pa (b::eval-at/b2/fast ba (aref i 2)))
(pb (b::eval-at/b2/fast bb (aref i 3))))
(true (v2~ (subseq i 0 2) pa eps)
"%intersect/tan eval @ a: ~s ~s ~s ~s ~s ~s ~s ~s:~%~s ~s"
x1 y1 xc yc ax2 ay2 bx2 by2
pa i)
(true (v2~ (subseq i 0 2) pb eps)
"%intersect/tan eval @ b: ~s ~s ~s ~s ~s ~s ~s ~s:~%~s ~s"
x1 y1 xc yc ax2 ay2 bx2 by2 pb i))))
;; test body in an flet to avoid dumping entire body into
;; test results
(finish (test-tan))
;; try again with curves swapped
(rotatef ax2 bx2) (rotatef ay2 by2)
(finish (test-tan) "test-tan swapped")))
(test-tan2 (x1 y1 axc ayc bxc byc ax2 ay2 bx2 by2)
(flet ((test-tan2 ()
(let* ((p1 (b::make-point x1 y1))
(ac (b::make-point axc ayc))
(bc (b::make-point bxc byc))
(a2 (b::make-point ax2 ay2))
(b2 (b::make-point bx2 by2))
(ba (b::%make-bezier2 p1 ac a2))
(bb (b::%make-bezier2 p1 bc b2))
(i (b::%intersect/tan2 p1 ac bc a2 b2))
(eps (* 2 (max (abs x1) (abs y1)
(abs axc) (abs ayc)
(abs bxc) (abs byc)
(abs ax2) (abs ay2)
(abs bx2) (abs by2))))
(pa (b::eval-at/b2/fast ba (aref i 2)))
(pb (b::eval-at/b2/fast bb (aref i 3))))
(true (v2~ (subseq i 0 2) pa eps)
"%intersect/tan2 eval @ a: ~s ~s ~s ~s ~s ~s ~s ~s ~s ~s:~%~s~%~s~%~s"
x1 y1 axc ayc bxc byc ax2 ay2 bx2 by2
pa i eps)
(true (v2~ (subseq i 0 2) pb eps)
"%intersect/tan2 eval @ b: ~s ~s ~s ~s ~s ~s ~s ~s ~s ~s:~%~s ~s"
x1 y1 axc ayc bxc byc ax2 ay2 bx2 by2 pb i))))
;; test body in an flet to avoid dumping entire body into
;; test results
(finish (test-tan2))
;; try again with curves swapped
(rotatef axc bxc) (rotatef ayc byc)
(rotatef ax2 bx2) (rotatef ay2 by2)
(finish (test-tan2) "test-tan2 swapped"))))
(macrolet ((test-tan/ni (x1 y1 xc yc ax2 ay2 bx2 by2)
`(progn
(is eql nil
(b::%intersect/tan (b::make-point ,x1 ,y1)
(b::make-point ,xc ,yc)
(b::make-point ,ax2 ,ay2)
(b::make-point ,bx2 ,by2)))
(is eql nil
(b::%intersect/tan (b::make-point ,x1 ,y1)
(b::make-point ,xc ,yc)
(b::make-point ,bx2 ,by2)
(b::make-point ,ax2 ,ay2)))))
(test-tan2/ni (x1 y1 axc ayc bxc byc ax2 ay2 bx2 by2)
`(progn
(is eql nil
(b::%intersect/tan2 (b::make-point ,x1 ,y1)
(b::make-point ,axc ,ayc)
(b::make-point ,bxc ,byc)
(b::make-point ,ax2 ,ay2)
(b::make-point ,bx2 ,by2)))
(is eql nil
(b::%intersect/tan2 (b::make-point ,x1 ,y1)
(b::make-point ,bxc ,byc)
(b::make-point ,axc ,ayc)
(b::make-point ,bx2 ,by2)
(b::make-point ,ax2 ,ay2))))))
(test-tan 0 0 1 0 2 2 1 1)
(test-tan 1 1 2 1 2 2 3 3)
(test-tan 1 1 3 2 4 4 2.6 2.2)
(test-tan/ni 347 359 348 359 348 360 349 360)
(test-tan2/ni 108 119 109 121 110 123 107.5d0 118.5d0 111.5d0 128)
(test-tan2 108 119 109 121 110 123 107.5d0 118.5d0 103 124)
(test-tan2 108 119 109 121 110 123 107.5d0 118.5d0 107.75d0 118.72d0)
(test-tan2 0 0 1 0 2 0 1 1 0.5d0 0.5d0)
;; 2 shared endpoints = no intersections for this function
(test-tan2/ni 0 0 1 0 2 0 1 1 1 1)
(test-tan2 0 0 1 0 1.3 0 1 1 0 1)
(test-tan2/ni 359 218 376 218 350 218 385 194 345 209)
)
)
)
#++
(test 'geom)
(define-test shapes
(is = 1 (length (b::contours (b::parse-shape "{ 0,0; 10,0; 10,10; 0,10; # }"))))
(is = 2 (length (b::contours (b::parse-shape "{ 0,0; 10,0; 10,10; 0,10; # } { 2,2; 8,2; 5,8; # }"))))
(is string= "{ 0,0; 10,0; 10,10; 0,10; # }"
(b::serialize-shape (b::parse-shape "{0,0;10,0;10,10;0,10; # }")))
(is string= "{ 0,0; 10,0; 10,10; 0,10; # }
{ 2,2; 8,2; 5,8; # }"
(b::serialize-shape
(b::parse-shape "{0, 0;10,0;10,10;0,10;#}{2,2;8,2;5,8;#}")))
(is = 8 (hash-table-count
(b::%prev (b::parse-shape "{ 0,0; 10,0; 10,10; 0,10; # }"))))
(is = 14 (hash-table-count (b::%prev (b::parse-shape "{ 0,0; 10,0; 10,10; 0,10; # } { 2,2; 8,2; 5,8; # }"))))
(is string= "{ 0,0; 0,10; 10,10; 10,0; # }"
(b::serialize-shape
(b::%edit-shape-to-shape
(list (b::%reverse-contour
(first (b::contours (b::shape-to-edit-shape (b::parse-shape "{ 0,0; 10,0; 10,10; 0,10; # }")))))))))
)
#++
(test 'shapes)
(define-test quadratic-intersect-internals-tests
(labels ((same-parabola-p (a1 a2 a3 b1 b2 b3 e)
(qii::same-parabola-p (b::make-bezier2/v2 a1 a2 a3)
(b::make-bezier2/v2 b1 b2 b3)
e))
(dx (v) (b::v2 (+ (b::vx v) (/ (expt 2 20)))
(b::vy v)))
(dy (v) (b::v2 (b::vx v)
(+ (b::vy v) (/ (expt 2 20)))))
(same-parabola (a b)
(let* ((eps (* double-float-epsilon
(max (reduce 'max a :key 'abs)
(reduce 'max b :key 'abs))))
(a1 (b::v2 (first a) (second a)))
(a2 (b::v2 (third a) (fourth a)))
(a3 (b::v2 (fifth a) (sixth a)))
(b1 (b::v2 (first b) (second b)))
(b2 (b::v2 (third b) (fourth b)))
(b3 (b::v2 (fifth b) (sixth b))))
(true (same-parabola-p a1 a2 a3 b1 b2 b3 eps)
"~s ~s" a b)
;; should still be same parabola with 1 or both
;; reversed
(true (same-parabola-p a1 a2 a3 b3 b2 b1 eps)
"r1 ~s ~s" a b)
(true (same-parabola-p a3 a2 a1 b1 b2 b3 eps)
"r2 ~s ~s" a b)
(true (same-parabola-p a3 a2 a1 b3 b2 b1 eps)
"r12 ~s ~s" a b)
;; slight permutations should be different parabolas
(false (same-parabola-p (dx a1) a2 a3 b1 b2 b3 eps)
"dx1 ~s ~s" a b)
(false (same-parabola-p a1 (dx a2) a3 b1 b2 b3 eps)
"dx2 ~s ~s" a b)
(false (same-parabola-p a1 a2 (dx a3) b1 b2 b3 eps)
"dx3 ~s ~s" a b)
(false (same-parabola-p a1 a2 a3 (dx b1) b2 b3 eps)
"dx4 ~s ~s" a b)
(false (same-parabola-p a1 a2 a3 b1 (dx b2) b3 eps)
"dx5 ~s ~s" a b)
(false (same-parabola-p a1 a2 a3 b1 b2 (dx b3) eps)
"dx6 ~s ~s" a b)
(false (same-parabola-p (dy a1) a2 a3 b1 b2 b3 eps)
"dy1 ~s ~s" a b)
(false (same-parabola-p a1 (dy a2) a3 b1 b2 b3 eps)
"dy2 ~s ~s" a b)
(false (same-parabola-p a1 a2 (dy a3) b1 b2 b3 eps)
"dy3 ~s ~s" a b)
(false (same-parabola-p a1 a2 a3 (dy b1) b2 b3 eps)
"dy4 ~s ~s" a b)
(false (same-parabola-p a1 a2 a3 b1 (dy b2) b3 eps)
"dy5 ~s ~s" a b)
(false (same-parabola-p a1 a2 a3 b1 b2 (dy b3) eps)
"dy6 ~s ~s" a b)))
(same-parabola-splits (a &rest splits)
(let* ((eps (* double-float-epsilon (reduce 'max a :key 'abs)))
(a1 (b::v2 (first a) (second a)))
(a2 (b::v2 (third a) (fourth a)))
(a3 (b::v2 (fifth a) (sixth a)))
(ab (b::make-bezier2/v2 a1 a2 a3)))
(loop for s in splits
do (let ((b1 a1)
(b2 (b::%%trim-b2-p1 ab 0 s))
(bx (b::eval-at/b2/fast ab s))
(b3 (b::%%trim-b2-p1 ab s 1))
(b4 a3))
(true (same-parabola-p a1 a2 a3 b1 b2 bx eps)
"~s ~s ~s | ~s | ~s ~s ~s"
a1 a2 a3 s b1 b2 bx)
(true (same-parabola-p a1 a2 a3 bx b3 b4 eps)
"~s split ~s-1" a s)
(true (same-parabola-p b1 b2 bx bx b3 b4 eps)
"~s split 0-~s vs ~s-1" a s s))))))
(finish (same-parabola '(877 30 873 34 869 40)
'(869 40 871 37 873 34.5d0)))
(finish (same-parabola '(877 30 873 34 869 40)
'(873 34.5d0 875 32 877 30)))
(finish (same-parabola '(869 40 871 37 873 34.5d0)
'(873 34.5d0 875 32 877 30)))
(finish (same-parabola-splits '(877 30 873 34 869 40)
1/2 1/3 1/4 1/5 2/5 1/7 2/7 3/7))))
#++
(test 'quadratic-intersect-internals-tests)