-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.lisp
324 lines (312 loc) · 16.2 KB
/
geometry.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
(in-package #:org.shirakumo.fraf.convex-covering)
;;; Bounding box
(declaim (ftype (function (dvec3 dvec3) (values vec3 vec3 &optional NIL))
center-and-size-from-min-and-max)
(inline center-and-size-from-min-and-max))
(defun center-and-size-from-min-and-max (min max)
(let* ((size/2 (v* (v- max min) .5))
(center (v+ min size/2)))
;; TODO avoid conversion
(values (vec (vx center) (vy center) (vz center))
(vec (vx size/2) (vy size/2) (vz size/2)))))
(declaim (ftype (function (dvec3 dvec3 dvec3) (values vec3 vec3 &optional NIL))
triangle-bounding-box)
(inline triangle-bounding-box))
(defun triangle-bounding-box (v1 v2 v3)
(let ((min (vmin v1 v2 v3))
(max (vmax v1 v2 v3)))
(center-and-size-from-min-and-max min max)))
(declaim (ftype (function (manifolds:vertex-array manifolds:face-array manifolds:u32)
(values vec3 vec3 &optional NIL))
face-bounding-box)
(inline face-bounding-box))
(defun face-bounding-box (vertices faces face-index)
;; TODO add manifolds:f
(let ((v1 (manifolds:v vertices (aref faces (+ (* 3 face-index) 0))))
(v2 (manifolds:v vertices (aref faces (+ (* 3 face-index) 1))))
(v3 (manifolds:v vertices (aref faces (+ (* 3 face-index) 2)))))
(triangle-bounding-box v1 v2 v3)))
;;; Helper functions
(defun p~ (point1 point2 &key (eps 1d-6))
(<= (vsqrdistance point1 point2) eps))
;;; Intersections via Separating Axis Theorem
(defmacro <=~ (tolerance &rest numbers)
`(and ,@(loop :for (number1 number2) :on numbers
:while number2
:collect `(<= (- ,number1 ,number2) ,tolerance))))
(defun intervals-intersect-p (i1-min i1-max i2-min i2-max &key (tolerance 1d-6) (threshold 1d-6))
(declare (type double-float i1-min i1-max i2-min i2-max)
(optimize (speed 3)))
;; case 1 | s1 e1 s2 e2
;; case 2 | s1 s2 e1 e2 *
;; case 3 | s1 s2 e2 e1 *
;; case 4 | s2 s1 e1 e2 *
;; case 5 | s2 s1 e2 e1 *
;; case 6 | s2 e2 s1 e1
(let ((tolerance (coerce tolerance 'double-float)) ; TODO avoid coercion
(threshold (coerce threshold 'double-float)))
(cond ((<= (abs (- (min i1-min i2-min) (max i1-max i2-max))) threshold)
(values T :identical))
((<=~ tolerance i1-min i2-min i1-max i2-max) ; case 2
(d "~4@TOverlap ~A~%"
(abs (- i2-min i1-max)))
(values T (if (<= (- i1-max i2-min) tolerance) :touching :penetrating)))
((<=~ tolerance i1-min i2-min i2-max i1-max) #+no (<= i1-min i2-min i2-max i1-max) ; case 3
(d "~4@TI₂ ⊂ I₁ (case 3) Overlaps ~A ~A | threshold ~A~%"
(abs (- i1-min i2-min)) ; cases 3 4
(abs (- i1-max i2-max)) ; cases 3 4
threshold)
(values T (if (and (< (- i2-max i2-min) tolerance)
(or (< (- i2-min i1-min) tolerance)
(< (- i1-max i2-min) tolerance)))
:touching
:penetrating)))
((<=~ tolerance i2-min i1-min i1-max i2-max) ; case 4
(d "~4@TI₁ ⊂ I₂ (case 4) Overlaps ~A ~A~%"
(abs (- i1-min i2-min)) ; cases 3 4
(abs (- i1-max i2-max)) ; cases 3 4
)
(values T (if (and (< (- i1-max i1-min) tolerance)
(or (< (- i1-min i2-min) tolerance)
(< (- i2-max i1-min) tolerance)))
:touching
:penetrating)))
((<=~ tolerance i2-min i1-min i2-max i1-max) ; case 5
(d "~4@TOverlap ~A~%"
(abs (- i1-min i2-max)))
(values T (if (< (- i2-max i1-min) tolerance) :touching :penetrating)))
#+no ((not (or (> (- i2-min i1-max) threshold)
(> (- i1-min i2-max) threshold)))
(d "~4@TOverlaps ~A ~A ~A ~A~%"
(abs (- i2-min i1-max)) ; case 2
(abs (- i1-min i2-min)) ; cases 3 4
(abs (- i1-max i2-max)) ; cases 3 4
(abs (- i1-min i2-max))) ; case 5
T)
(T ; no intersection
NIL))))
(defun triangles-intersect-p (u1 u2 u3 v1 v2 v3 &key (threshold 1d-6))
(declare (type dvec3 u1 u2 u3 v1 v2 v3))
(d "U~2@T~A~%~2@T~A~%~2@T~A~%V~2@T~A~%~2@T~A~%~2@T~A~%" u1 u2 u3 v1 v2 v3)
(flet ((separating-axis-p (axis &optional note)
(declare (type dvec3 axis))
(let* ((du1 (v. axis u1))
(du2 (v. axis u2))
(du3 (v. axis u3))
(dv1 (v. axis v1))
(dv2 (v. axis v2))
(dv3 (v. axis v3))
(iu-min (min du1 du2 du3)) ; interval u min
(iu-max (max du1 du2 du3)) ; interval u max
(iv-min (min dv1 dv2 dv3)) ; interval v min
(iv-max (max dv1 dv2 dv3)))
(d "~2@TAxis~@[ ~A~] ~A~&~
~4@T=> {~5,2F ~5,2F ~5,2F} {~5,2F ~5,2F ~5,2F}~&~
~4@T=> [~9,6F, ~9,6F] [~9,6F, ~9,6F hi]~%"
note axis
du1 du2 du3 dv1 dv2 dv3
iu-min iu-max iv-min iv-max)
(multiple-value-bind (intersectp class)
(intervals-intersect-p iu-min iu-max iv-min iv-max
:threshold threshold)
(d "~4@T=> ~A~@[ | ~A~]~%" (if intersectp "not separating" "separating") class)
(values (not intersectp) class)))))
(d "--------------------~%")
(multiple-value-bind (result constellation contact)
(let ((normal-u (vunit (vc (v- u2 u1) (v- u3 u1)))))
(d "Normal~%")
(multiple-value-bind (normal-separating-p normal-class)
(separating-axis-p normal-u 0)
(d "~2@T=> ~:[not separating~;separating~]~@[ | ~A~]~%"
normal-separating-p normal-class)
(cond (normal-separating-p
(d "normal separating~%")
NIL)
((eq normal-class :identical)
(d "Edge normals (coplanar case)~%")
(block NIL
(let ((contact :penetrating))
(flet ((test-axis (e1 e2)
(declare (type dvec3 e1 e2))
(let ((edge-normal (vunit (vc normal-u (v- e2 e1)))))
(multiple-value-bind (separatingp class)
(separating-axis-p edge-normal 0)
(cond (separatingp
(return (values NIL :coplanar)))
(T
(when (and (eq contact :penetrating)
(eq class :touching))
(setf contact class))))))))
(test-axis u1 u2)
(test-axis u2 u3)
(test-axis u3 u1)
(test-axis v1 v2)
(test-axis v2 v3)
(test-axis v3 v1)
(values T :coplanar contact)))))
(T
(d "General edge pairs~%")
(let ((contact normal-class))
(if (flet ((test-axis (u1 u2 v1 v2)
(let ((c (vc (v- u2 u1) (v- v2 v1))))
(unless (< (vsqrlength c) threshold)
(let ((c* (vunit c)))
(multiple-value-bind (separatingp class)
(separating-axis-p c* 0)
(when (and (not separatingp)
(eq contact :penetrating)
(eq class :touching))
(setf contact class))
separatingp))))))
(prog1
(loop named outer
for (a1 a2) on (list u1 u2 u3 u1)
while a2
do (loop for (b1 b2) on (list v1 v2 v3 v1)
while b2
when (test-axis a1 a2 b1 b2)
do (return-from outer T)))))
NIL
(values T NIL contact)))))))
(d "=> ~A~:*~:[~; ~A ~A~]~%"
result
(or constellation :general)
contact)
(d "--------------------~%")
(values result constellation contact))))
(defun line-intersects-triangle-p (u1 u2 u3 l1 l2 &key (tolerance 1d-6) (threshold 1d-6))
(declare (type dvec3 u1 u2 u3 l1 l2))
(d "U~2@T~A~%~2@T~A~%~2@T~A~%L~2@T~A~%~2@T~A~%" u1 u2 u3 l1 l2)
(flet ((separating-axis-p (axis threshold)
(declare (type dvec3 axis))
(let* ((du1 (v. axis u1))
(du2 (v. axis u2))
(du3 (v. axis u3))
(dl1 (v. axis l1))
(dl2 (v. axis l2))
(iu-min (min du1 du2 du3)) ; interval u min
(iu-max (max du1 du2 du3)) ; interval u max
(iv-min (min dl1 dl2)) ; interval v min
(iv-max (max dl1 dl2)))
(d "~2@TAxis~@[ ~A~] ~A~&~
~4@T=> {~5,2F ~5,2F ~5,2F} {~5,2F ~5,2F}~&~
~4@T=> [~9,6F, ~9,6F] [~9,6F, ~9,6F]~%"
NIL axis
du1 du2 du3 dl1 dl2
iu-min iu-max iv-min iv-max)
(multiple-value-bind (intersectp class)
(intervals-intersect-p iu-min iu-max iv-min iv-max
:tolerance tolerance :threshold threshold)
(d "~4@T=> ~A~@[ | ~A~]~%" (if intersectp "not separating" "separating") class)
(values (not intersectp) class)))))
(d "--------------------~%")
(multiple-value-bind (result constellation contact)
(let ((normal-u (vunit (vc (v- u2 u1) (v- u3 u1)))))
(d "Normal~%")
(multiple-value-bind (normal-separating-p normal-class)
(separating-axis-p normal-u 1d-3) ; TODO
(d "~2@T=> ~:[not separating~;separating~]~@[ | ~A~]~%"
normal-separating-p normal-class)
(cond (normal-separating-p
(d "normal separating~%")
NIL)
((eq normal-class :identical)
(d "Edge normals (coplanar case)~%")
(block NIL
(let ((contact :penetrating))
(flet ((test-axis (e1 e2)
(declare (type dvec3 e1 e2))
(let ((edge-normal (vunit (vc normal-u (v- e2 e1)))))
(multiple-value-bind (separatingp class)
(separating-axis-p edge-normal threshold)
(cond (separatingp
(return (values NIL :coplanar)))
((and (eq class :touching)
(eq contact :penetrating))
(setf contact class)))))))
(test-axis u1 u2)
(test-axis u2 u3)
(test-axis u3 u1)
(test-axis l1 l2)
(if (and (eq contact :penetrating)
(member l1 (list u1 u2 u3) :test (lambda (v1 v2)
(< (vsqrdistance v1 v2) (* threshold threshold))))
(member l2 (list u1 u2 u3) :test (lambda (v1 v2)
(< (vsqrdistance v1 v2) (* threshold threshold)))))
(values T :coplanar :edge)
(values T :coplanar contact))))))
(T
(d "General edge pairs~%")
(let ((contact normal-class))
(if (flet ((test-axis (u1 u2 v1 v2)
(let ((c (vc (v- u2 u1) (v- v2 v1))))
(unless (< (vsqrlength c) (* threshold threshold))
(let ((c* (vunit c)))
(multiple-value-bind (separatingp class)
(separating-axis-p c* threshold)
(when (and (not separatingp)
(eq contact :penetrating)
(eq class :touching))
(setf contact class))
separatingp))))))
;; TODO(jmoringe): The last call is a hack
;; in case the triangle and the line
;; segment are actually coplanar but we
;; didn't detect it due to insufficient
;; precision.
(or (macrolet
((define ()
`(or
,@(loop for (a1 a2) on '(u1 u2 u3 u1)
while a2
collect `(test-axis ,a1 ,a2 l1 l2)))))
(define))
(let ((extra-axis (vc normal-u (v- l1 l2))))
(declare (dynamic-extent extra-axis))
(and (> (vsqrlength extra-axis) (* threshold threshold))
(separating-axis-p (nvunit extra-axis) threshold)))))
NIL
(values T NIL contact)))))))
(d "=> ~A~:*~:[~; ~A ~A~]~%"
result
(or constellation :general)
contact)
(d "--------------------~%")
(values result constellation contact))))
(defun point-on-triangle-p (u1 u2 u3 v &key (threshold .00001))
(let* ((u2-u1 (v- u2 u1))
(normal-u (vunit (vc u2-u1 (v- u3 u1))))
(contact :separating))
(flet ((separating-axis-p (axis bias &optional note)
(let* ((du1 (+ (v. axis u1) bias))
(du2 (+ (v. axis u2) bias))
(du3 (+ (v. axis u3) bias))
(dv (v. axis v))
(iu-min (min du1 du2 du3)) ; interval u min
(iu-max (max du1 du2 du3)) ; interval u max
)
(d "Axis~@[ ~A~] ~A~&~
~2@T=> {~5,2F ~5,2F ~5,2F} {~5,2F ~5,2F ~5,2F}~&~
~2@T=> [~5,2F, ~5,2F] [~5,2F, ~5,2F]~%"
note axis bias
du1 du2 du3 dv dv dv
iu-min iu-max dv dv)
(multiple-value-bind (result contact*)
(intervals-intersect-p iu-min iu-max dv dv
:threshold threshold)
(when (eq contact* :touching)
(setf contact contact*))
(d "~2@T~A~%" (if result "not separating" "separating"))
(not result)))))
(d "--------------------~%")
(multiple-value-prog1
(cond ((not (< (abs (v. normal-u (v- v u1))) threshold))
nil)
((and (let ((n12 (vunit (vc normal-u u2-u1))))
(not (separating-axis-p n12 0)))
(let ((n23 (vunit (vc normal-u (v- u3 u2)))))
(not (separating-axis-p n23 0)))
(let ((n31 (vunit (vc normal-u (v- u1 u3)))))
(not (separating-axis-p n31 0))))
(values T contact)))
(d "--------------------~%")))))