Skip to content

Commit

Permalink
Refine hash table's type. (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStoryM committed Oct 9, 2021
1 parent 20f4a99 commit 9df19f3
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 208 deletions.
10 changes: 5 additions & 5 deletions plot-lib/plot/private/common/plot-device.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@
;; get max widths and heights per row/column
(define-values (max-label-widths max-label-heights)
(let-values ([(width height)
(for/fold ([width : (HashTable Integer Exact-Rational) #hash()]
[height : (HashTable Integer Exact-Rational) #hash()])
(for/fold ([width : (Immutable-HashTable Integer Exact-Rational) #hash()]
[height : (Immutable-HashTable Integer Exact-Rational) #hash()])
([label (in-list labels)]
[k (in-naturals)])
(define-values (i j)
Expand Down Expand Up @@ -666,7 +666,7 @@
;; different gaps
(define-values (horiz-gap min-label-height baseline _1)
(get-text-extent " "))

(define top-gap baseline)
(define bottom-gap (* 1/2 baseline))
(define in-label-gap (* 3 horiz-gap))
Expand Down Expand Up @@ -724,7 +724,7 @@
(define draw-x-mins (for/list : (Listof Real)
([x (in-list label-x-mins)]
[w (in-list max-label-widths)]) (+ x w in-label-gap)))

(values legend-rect max-label-heights
draw-x-size label-x-mins draw-x-mins
draw-y-size label-y-mins
Expand Down Expand Up @@ -801,7 +801,7 @@
(define max-label-height (list-ref max-label-heights i))
(define label-y-min (+ legend-entry-y-min
(* 1/2 (- max-label-height label-height))))

(if (pict? label)
(draw-pict label (vector label-x-min label-y-min) 'top-left 0)
(draw-text label (vector label-x-min label-y-min) 'top-left 0 0 #t))
Expand Down
20 changes: 10 additions & 10 deletions plot-lib/plot/private/plot3d/bsp-trees.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

(provide (all-defined-out))

(: build-bsp-trees (-> (HashTable Integer (Listof BSP-Shape))
(HashTable Integer BSP-Tree)))
(: build-bsp-trees (-> (Immutable-HashTable Integer (Listof BSP-Shape))
(Immutable-HashTable Integer BSP-Tree)))
(define (build-bsp-trees structural-shapes)
(for/hasheq : (HashTable Integer BSP-Tree) ([(layer ss) (in-hash structural-shapes)])
(for/hasheq : (Immutable-HashTable Integer BSP-Tree) ([(layer ss) (in-hash structural-shapes)])
(values layer (build-bsp-tree ss))))

(: walk-bsp-trees (-> (HashTable Integer BSP-Tree)
(: walk-bsp-trees (-> (Immutable-HashTable Integer BSP-Tree)
FlVector
(HashTable Integer (Listof BSP-Shape))
(HashTable Integer (Listof BSP-Shape))))
(Immutable-HashTable Integer (Listof BSP-Shape))
(Immutable-HashTable Integer (Listof BSP-Shape))))
(define (walk-bsp-trees bsp-trees view-dir detail-shapes)
(define vx (flvector-ref view-dir 0))
(define vy (flvector-ref view-dir 1))
(define vz (flvector-ref view-dir 2))

(define layers (sort (append (hash-keys bsp-trees) (hash-keys detail-shapes)) >))
(for/hasheq : (HashTable Integer (Listof BSP-Shape)) ([layer (in-list layers)])
(for/hasheq : (Immutable-HashTable Integer (Listof BSP-Shape)) ([layer (in-list layers)])
(define bsp (hash-ref bsp-trees layer (λ () (bsp-leaf empty))))
(define ss (hash-ref detail-shapes layer (λ () empty)))

(: in-order-ss (Listof BSP-Shape))
(define in-order-ss
(let loop ([bsp (bsp-tree-insert bsp ss)])
Expand All @@ -40,5 +40,5 @@
(if (cos-angle . > . -1e-16)
(append (loop neg) (loop pos))
(append (loop pos) (loop neg)))])))

(values layer in-order-ss)))
22 changes: 11 additions & 11 deletions plot-lib/plot/private/plot3d/bsp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
[(n . = . 2) (list (line data (first vs) (second vs)))]
[else (list (lines data vs))]))
vss)))

(define-values (vss1 vss2) (split-lines3d vs plane))
(values (vertices->lines vss2) (vertices->lines vss1))]))

Expand Down Expand Up @@ -254,7 +254,7 @@
(define n (length ivls))
(define-values (ivls1 ivls2) (split-at ivls (quotient n 2)))
(interval-list-union (loop ivls1) (loop ivls2))])))

(cond [(empty? ivls) #f]
[(empty? (rest ivls)) #f]
[else
Expand Down Expand Up @@ -408,7 +408,7 @@
(match s
[(points _ vs)
(if (empty? vs) empty (list s))]
[(line _ v1 v2)
[(line _ v1 v2)
(if (equal? v1 v2) empty (list s))]
[(poly data vs ls norm)
(let-values ([(vs ls) (canonical-polygon3d vs ls)])
Expand Down Expand Up @@ -513,17 +513,17 @@
[else
(define axes (vertices->axes (bsp-polys->vertices ps)))
(define center (list->flvector (map axis-mid axes)))

;; Planes defined by neighboring polygon vertices
(define polygon-planes (delay (sort-planes (append* (map bsp-poly-planes ps)) center)))

(: try-bsp-split/polygon-planes (-> Boolean (U #f BSP-Tree)))
;; Tries splitting using polygon-planes
(define (try-bsp-split/polygon-planes disjoint?)
(define planes (force polygon-planes))
(cond [(and disjoint? ((length planes) . > . 10)) #f]
[else (try-bsp-split/planes ss planes disjoint?)]))

(let* ([bsp #f]
[bsp (if bsp bsp (try-bsp-split/axial-planes ss axes))]
[bsp (if bsp bsp (try-bsp-split/bounding-planes ss ps center))]
Expand All @@ -543,17 +543,17 @@
[else
(define axes (vertices->axes (bsp-lines->vertices ls)))
(define center (list->flvector (map axis-mid axes)))

;; Planes defined by line segments and basis vectors (i.e. one basis in normal is zero)
(define line-planes (delay (sort-planes (append* (map bsp-line-planes ls)) center)))

(: try-bsp-split/line-planes (-> Boolean (U #f BSP-Tree)))
;; Tries splitting using line-planes
(define (try-bsp-split/line-planes disjoint?)
(define planes (force line-planes))
(cond [(and disjoint? ((length planes) . > . 10)) #f]
[else (try-bsp-split/planes ss planes disjoint?)]))

(let* ([bsp #f]
[bsp (if bsp bsp (try-bsp-split/axial-planes ss axes))]
[bsp (if bsp bsp (try-bsp-split/line-planes #t))]
Expand All @@ -573,15 +573,15 @@
(define axes (vertices->axes (append (append* (map lines-vertices ls))
(append* (map points-vertices ps)))))
(define center (list->flvector (map axis-mid axes)))

(: try-nondisjoint-split (-> (U #f BSP-Tree)))
(define (try-nondisjoint-split)
(match-define (axis i size _mn _mx mid) (argmax axis-size axes))
(cond [(size . < . 0.01) #f]
[else
(define plane (axial-plane i mid))
(try-bsp-split ss plane #f (λ () #f))]))

(let* ([bsp #f]
[bsp (if bsp bsp (try-bsp-split/axial-planes ss axes))]
[bsp (if bsp bsp (try-nondisjoint-split))])
Expand Down
Loading

0 comments on commit 9df19f3

Please sign in to comment.