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
|
#lang typed/racket/base
(require (for-syntax racket/base)
racket/match
racket/list)
(provide point-in-bounds?
point-inside-plane?
clip-line/bounds
clip-line/plane
clip-lines/bounds
clip-lines/plane
clip-polygon/bounds
clip-polygon/plane)
;; ===================================================================================================
;; Basic plane intersection and distance tests
;; Need to use this instead of `*' to make axial plane tests as fast as hand-written because the
;; optimizer doesn't always figure out (* 0 e) = 0, etc.
(define-syntax (times stx)
(syntax-case stx ()
[(_ 0 e) (syntax/loc stx 0)]
[(_ 1 e) (syntax/loc stx e)]
[(_ 0.0 e) (syntax/loc stx 0.0)]
[(_ 1.0 e) (syntax/loc stx (real->double-flonum e))]
[(_ . es) (syntax/loc stx (* . es))]))
(define-syntax-rule (plane-line-intersect a b c d x1-stx y1-stx z1-stx x2-stx y2-stx z2-stx)
(let ([x1 x1-stx] [y1 y1-stx] [z1 z1-stx] [x2 x2-stx] [y2 y2-stx] [z2 z2-stx])
(let ([dot1 (+ (times a x1) (times b y1) (times c z1))]
[dot2 (+ (times a x2) (times b y2) (times c z2))])
(let ([denom (- dot1 dot2)])
(if (zero? denom)
(values x2 y2 z2)
(let ([t (/ (+ dot1 d) denom)])
(values (+ x1 (* t (- x2 x1)))
(+ y1 (* t (- y2 y1)))
(+ z1 (* t (- z2 z1))))))))))
(define-syntax-rule (plane-point-dist a b c d x y z)
(+ (times a x) (times b y) (times c z) d))
;; ===================================================================================================
;; Points
(: point-in-bounds? (-> (Vectorof Real) Real Real Real Real Real Real Boolean))
(define (point-in-bounds? v x-min x-max y-min y-max z-min z-max)
(match-define (vector x y z) v)
(and (<= x-min x x-max) (<= y-min y y-max) (<= z-min z z-max)))
(: point-inside-plane? (-> (Vectorof Real) (Vector Real Real Real Real) Boolean))
(define (point-inside-plane? v plane)
(match-define (vector x y z) v)
(match-define (vector a b c d) plane)
(>= (plane-point-dist a b c d x y z) 0))
;; ===================================================================================================
;; Line clipping
(: clip-line/plane (-> (Vectorof Real) (Vectorof Real) (Vector Real Real Real Real)
(Values (U #f (Vectorof Real)) (U #f (Vectorof Real)))))
(define (clip-line/plane v1 v2 plane)
(match-define (vector x1 y1 z1) v1)
(match-define (vector x2 y2 z2) v2)
(match-define (vector a b c d) plane)
(define v1? (>= (plane-point-dist a b c d x1 y1 z1) 0))
(define v2? (>= (plane-point-dist a b c d x2 y2 z2) 0))
(cond [(and v1? v2?) (values v1 v2)]
[(not (or v1? v2?)) (values #f #f)]
[else
(define-values (x y z) (plane-line-intersect a b c d x1 y1 z1 x2 y2 z2))
(if v1? (values v1 (vector x y z)) (values (vector x y z) v2))]))
;; ---------------------------------------------------------------------------------------------------
;; Clipping inside axial bounding box
(define-syntax-rule (make-clip-line/axis a b c gte?)
(λ ([val : Real] [x1 : Real] [y1 : Real] [z1 : Real] [x2 : Real] [y2 : Real] [z2 : Real])
(define v1? (gte? (plane-point-dist a b c (- val) x1 y1 z1) 0))
(define v2? (gte? (plane-point-dist a b c (- val) x2 y2 z2) 0))
(cond [(or (and v1? v2?) (not (or v1? v2?))) (values x1 y1 z1 x2 y2 z2)]
[else
(define-values (x y z) (plane-line-intersect a b c (- val) x1 y1 z1 x2 y2 z2))
(if v1? (values x1 y1 z1 x y z) (values x y z x2 y2 z2))])))
(define clip-line-x-min (make-clip-line/axis 1 0 0 >=))
(define clip-line-x-max (make-clip-line/axis 1 0 0 <=))
(define clip-line-y-min (make-clip-line/axis 0 1 0 >=))
(define clip-line-y-max (make-clip-line/axis 0 1 0 <=))
(define clip-line-z-min (make-clip-line/axis 0 0 1 >=))
(define clip-line-z-max (make-clip-line/axis 0 0 1 <=))
(: clip-line/bounds (-> (Vectorof Real) (Vectorof Real) Real Real Real Real Real Real
(Values (U #f (Vectorof Real)) (U #f (Vectorof Real)))))
(define (clip-line/bounds v1 v2 x-min x-max y-min y-max z-min z-max)
(let/ec return : (Values (U #f (Vectorof Real)) (U #f (Vectorof Real)))
(match-define (vector x1 y1 z1) v1)
(match-define (vector x2 y2 z2) v2)
;; early accept: both endpoints in bounds
(when (and (<= x-min x1 x-max) (<= y-min y1 y-max) (<= z-min z1 z-max)
(<= x-min x2 x-max) (<= y-min y2 y-max) (<= z-min z2 z-max))
(return v1 v2))
;; early reject: both endpoints on the outside of the same plane
(when (or (and (x1 . < . x-min) (x2 . < . x-min)) (and (x1 . > . x-max) (x2 . > . x-max))
(and (y1 . < . y-min) (y2 . < . y-min)) (and (y1 . > . y-max) (y2 . > . y-max))
(and (z1 . < . z-min) (z2 . < . z-min)) (and (z1 . > . z-max) (z2 . > . z-max)))
(return #f #f))
(let*-values ([(x1 y1 z1 x2 y2 z2) (clip-line-x-min x-min x1 y1 z1 x2 y2 z2)]
[(x1 y1 z1 x2 y2 z2) (clip-line-x-max x-max x1 y1 z1 x2 y2 z2)]
[(x1 y1 z1 x2 y2 z2) (clip-line-y-min y-min x1 y1 z1 x2 y2 z2)]
[(x1 y1 z1 x2 y2 z2) (clip-line-y-max y-max x1 y1 z1 x2 y2 z2)]
[(x1 y1 z1 x2 y2 z2) (clip-line-z-min z-min x1 y1 z1 x2 y2 z2)]
[(x1 y1 z1 x2 y2 z2) (clip-line-z-max z-max x1 y1 z1 x2 y2 z2)])
(values (vector x1 y1 z1) (vector x2 y2 z2)))))
;; ===================================================================================================
;; Connected lines clipping
(define-syntax-rule (make-clip-lines/plane a b c d gte?)
(λ (vs)
(cond
[(empty? vs) empty]
[else
(define v1 (first vs))
(match-define (vector x1 y1 z1) v1)
(define v1? (gte? (plane-point-dist a b c d x1 y1 z1) 0))
(define init-vss (if v1? (list (list v1)) (list empty)))
(define-values (vss _x1 _y1 _z1 _v1?)
(for/fold ([vss : (Listof (Listof (Vectorof Real))) init-vss]
[x1 : Real x1]
[y1 : Real y1]
[z1 : Real z1]
[v1? : Boolean v1?])
([v2 (in-list (rest vs))])
(define x2 (vector-ref v2 0))
(define y2 (vector-ref v2 1))
(define z2 (vector-ref v2 2))
(define v2? (gte? (plane-point-dist a b c d x2 y2 z2) 0))
(cond [(and v1? v2?) (values (cons (cons v2 (first vss)) (rest vss)) x2 y2 z2 v2?)]
[(not (or v1? v2?)) (values vss x2 y2 z2 v2?)]
[else
(define-values (x y z) (plane-line-intersect a b c d x1 y1 z1 x2 y2 z2))
(if v1?
(values (cons (cons (vector x y z) (first vss)) (rest vss)) x2 y2 z2 v2?)
(values (cons (list v2 (vector x y z)) vss) x2 y2 z2 v2?))])))
(filter (compose not empty?) vss)])))
(: clip-lines/plane (-> (Listof (Vectorof Real)) (Vector Real Real Real Real)
(Listof (Listof (Vectorof Real)))))
(define (clip-lines/plane vs plane)
(match-define (vector a b c d) plane)
((make-clip-lines/plane a b c d >=) vs))
;; ---------------------------------------------------------------------------------------------------
;; Clipping inside axial bounding box
(: early-accept? (-> (Listof (Vectorof Real)) Real Real Real Real Real Real Boolean))
;; Early accept: all endpoints in bounds (or empty vs)
(define (early-accept? vs x-min x-max y-min y-max z-min z-max)
(andmap (λ ([v : (Vectorof Real)])
(and (<= x-min (vector-ref v 0) x-max)
(<= y-min (vector-ref v 1) y-max)
(<= z-min (vector-ref v 2) z-max)))
vs))
(: early-reject? (-> (Listof (Vectorof Real)) Real Real Real Real Real Real Boolean))
;; Early reject: all endpoints on the outside of the same plane
(define (early-reject? vs x-min x-max y-min y-max z-min z-max)
(or (andmap (λ ([v : (Vectorof Real)]) ((vector-ref v 0) . < . x-min)) vs)
(andmap (λ ([v : (Vectorof Real)]) ((vector-ref v 0) . > . x-max)) vs)
(andmap (λ ([v : (Vectorof Real)]) ((vector-ref v 1) . < . y-min)) vs)
(andmap (λ ([v : (Vectorof Real)]) ((vector-ref v 1) . > . y-max)) vs)
(andmap (λ ([v : (Vectorof Real)]) ((vector-ref v 2) . < . z-min)) vs)
(andmap (λ ([v : (Vectorof Real)]) ((vector-ref v 2) . > . z-max)) vs)))
(define-syntax-rule (make-clip-lines/axis a b c gte?)
(λ ([val : Real] [vs : (Listof (Vectorof Real))])
((make-clip-lines/plane a b c (- val) gte?) vs)))
(define clip-lines-x-min (make-clip-lines/axis 1 0 0 >=))
(define clip-lines-x-max (make-clip-lines/axis 1 0 0 <=))
(define clip-lines-y-min (make-clip-lines/axis 0 1 0 >=))
(define clip-lines-y-max (make-clip-lines/axis 0 1 0 <=))
(define clip-lines-z-min (make-clip-lines/axis 0 0 1 >=))
(define clip-lines-z-max (make-clip-lines/axis 0 0 1 <=))
(: clip-lines/bounds (-> (Listof (Vectorof Real)) Real Real Real Real Real Real
(Listof (Listof (Vectorof Real)))))
(define (clip-lines/bounds vs x-min x-max y-min y-max z-min z-max)
(let/ec return : (Listof (Listof (Vectorof Real)))
(when (early-accept? vs x-min x-max y-min y-max z-min z-max) (return (list vs)))
(when (early-reject? vs x-min x-max y-min y-max z-min z-max) (return empty))
(let* ([vss (clip-lines-x-min x-min vs)]
[_ (when (empty? vss) (return empty))]
[vss (append* (map (λ ([vs : (Listof (Vectorof Real))])
(clip-lines-x-max x-max vs))
vss))]
[_ (when (empty? vss) (return empty))]
[vss (append* (map (λ ([vs : (Listof (Vectorof Real))])
(clip-lines-y-min y-min vs))
vss))]
[_ (when (empty? vss) (return empty))]
[vss (append* (map (λ ([vs : (Listof (Vectorof Real))])
(clip-lines-y-max y-max vs))
vss))]
[_ (when (empty? vss) (return empty))]
[vss (append* (map (λ ([vs : (Listof (Vectorof Real))])
(clip-lines-z-min z-min vs))
vss))]
[_ (when (empty? vss) (return empty))]
[vss (append* (map (λ ([vs : (Listof (Vectorof Real))])
(clip-lines-z-max z-max vs))
vss))])
vss)))
;; ===================================================================================================
;; Polygon clipping
(define-syntax-rule (make-clip-polygon/plane a b c d gte?)
(plambda: (L) ([vs : (Listof (Vectorof Real))] [ls : (Listof L)])
(define v1 (last vs))
(define x1 (vector-ref v1 0))
(define y1 (vector-ref v1 1))
(define z1 (vector-ref v1 2))
(define v1? (gte? (plane-point-dist a b c d x1 y1 z1) 0))
(define-values (new-vs new-ls _x1 _y1 _z1 _v1?)
(for/fold ([vs : (Listof (Vectorof Real)) empty]
[ls : (Listof (U #t L)) empty]
[x1 : Real x1]
[y1 : Real y1]
[z1 : Real z1]
[v1? : Boolean v1?])
([v2 (in-list vs)]
[l (in-list ls)])
(define x2 (vector-ref v2 0))
(define y2 (vector-ref v2 1))
(define z2 (vector-ref v2 2))
(define v2? (gte? (plane-point-dist a b c d x2 y2 z2) 0))
(cond [(and v1? v2?) (values (cons v2 vs) (cons l ls) x2 y2 z2 v2?)]
[(not (or v1? v2?)) (values vs ls x2 y2 z2 v2?)]
[else
(define-values (x y z) (plane-line-intersect a b c d x1 y1 z1 x2 y2 z2))
(if v1?
(values (cons (vector x y z) vs) (cons l ls) x2 y2 z2 v2?)
(values (list* v2 (vector x y z) vs) (list* l #t ls) x2 y2 z2 v2?))])))
(values (reverse new-vs) (reverse new-ls))))
(: clip-polygon/plane (All (L) (-> (Listof (Vectorof Real)) (Listof L) (Vector Real Real Real Real)
(Values (Listof (Vectorof Real)) (Listof (U #t L))))))
(define (clip-polygon/plane vs ls plane)
(match-define (vector a b c d) plane)
((make-clip-polygon/plane a b c d >=) vs ls))
;; ---------------------------------------------------------------------------------------------------
;; Clipping inside axial bounding box
(define-syntax-rule (make-clip-polygon/axis a b c gte?)
(plambda: (L) ([vs : (Listof (Vectorof Real))] [ls : (Listof L)] [val : Real])
((make-clip-polygon/plane a b c (- val) gte?) vs ls)))
(define clip-polygon-x-min (make-clip-polygon/axis 1 0 0 >=))
(define clip-polygon-x-max (make-clip-polygon/axis 1 0 0 <=))
(define clip-polygon-y-min (make-clip-polygon/axis 0 1 0 >=))
(define clip-polygon-y-max (make-clip-polygon/axis 0 1 0 <=))
(define clip-polygon-z-min (make-clip-polygon/axis 0 0 1 >=))
(define clip-polygon-z-max (make-clip-polygon/axis 0 0 1 <=))
(: clip-polygon/bounds (All (L) (-> (Listof (Vectorof Real)) (Listof L) Real Real Real Real Real Real
(Values (Listof (Vectorof Real)) (Listof (U #t L))))))
(define (clip-polygon/bounds vs ls x-min x-max y-min y-max z-min z-max)
(let/ec return : (Values (Listof (Vectorof Real)) (Listof (U #t L)))
(when (early-accept? vs x-min x-max y-min y-max z-min z-max) (return vs ls))
(when (early-reject? vs x-min x-max y-min y-max z-min z-max) (return empty empty))
(let*-values ([(vs ls) (clip-polygon-x-min vs ls x-min)]
[(_) (when (empty? vs) (return empty empty))]
[(vs ls) (clip-polygon-x-max vs ls x-max)]
[(_) (when (empty? vs) (return empty empty))]
[(vs ls) (clip-polygon-y-min vs ls y-min)]
[(_) (when (empty? vs) (return empty empty))]
[(vs ls) (clip-polygon-y-max vs ls y-max)]
[(_) (when (empty? vs) (return empty empty))]
[(vs ls) (clip-polygon-z-min vs ls z-min)]
[(_) (when (empty? vs) (return empty empty))]
[(vs ls) (clip-polygon-z-max vs ls z-max)])
(values vs ls))))
|