X-Git-Url: https://git.jsancho.org/?p=dungeon-master.git;a=blobdiff_plain;f=dungeon-master%2Fgeom%2Fvoronoi.scm;h=66acf9e8c9402a46ef677d057758d037ee725826;hp=9217285ad97c2295dd539acd6cba191284526801;hb=d4dcc4ec9815f1574eb12b3ee20aa0362f77704b;hpb=09c4aef3b2dde4e8fdf0a4b673b36c1ddbc84b6e diff --git a/dungeon-master/geom/voronoi.scm b/dungeon-master/geom/voronoi.scm index 9217285..66acf9e 100644 --- a/dungeon-master/geom/voronoi.scm +++ b/dungeon-master/geom/voronoi.scm @@ -32,6 +32,14 @@ (> (angle-sign seed c1 c2) 0))) (make-raw-voronoi-region seed (list-sort compare-angles vertices))) +(define (voronoi-region-center region) + (let* ((vertices (voronoi-region-vertices region)) + (centers (map (lambda (v) (triangle-center v)) vertices))) + (scale-point + (apply sum-points centers) + (/ 1 (length vertices))))) + + ;;; Voronoi mesh (define-record-type @@ -42,7 +50,7 @@ (frame voronoi-mesh-frame) (regions voronoi-mesh-regions)) -(define* (make-voronoi-mesh vertices #:optional (relax-steps 3)) +(define (make-voronoi-mesh vertices) ;; Delaunay triangulation (receive (triangles points frame) (bowyer-watson vertices) @@ -86,9 +94,27 @@ ordered-regions))) filtered-regions)) -(define* (voronoi-mesh-relax voronoi #:optional (to-relax '())) - (let ((regions (partitioning voronoi))) - voronoi)) +(define* (voronoi-mesh-relax voronoi #:optional (points-to-relax '())) + (let ((regions + (map + (lambda (r) (cons (voronoi-region-seed r) (voronoi-region-center r))) + (partitioning voronoi))) + (points + (filter + (lambda (p) (not (member p (voronoi-mesh-frame voronoi)))) + (voronoi-mesh-points voronoi))) + (to-relax + (if (null? points-to-relax) + (voronoi-mesh-points voronoi) + points-to-relax))) + (make-voronoi-mesh + (map + (lambda (point) + (let ((center (assoc-ref regions point))) + (if (and center (member point to-relax)) + center + point))) + points)))) (define (make-regions points triangles) (map-in-order