-(define* (make-regions points triangles #:optional (regions '()))
- (cond ((null? points)
- regions)
- (else
- (let* ((p (car points))
- (vertices (filter
- (lambda (tr) (member p (triangle-points tr)))
- triangles)))
- (display p)(newline)
- (make-regions (cdr points)
- triangles
- (cons (make-voronoi-region p vertices) regions))))))
+(define (partitioning voronoi)
+ (define (is-real-triangle triangle)
+ ;; real triangle points cannot be frame points
+ (let ((frame (voronoi-mesh-frame voronoi)))
+ (let check-points ((points (triangle-points triangle)))
+ (cond ((null? points)
+ #t) ; triangle is real
+ (else
+ (and (not (member (car points) frame))
+ (check-points (cdr points))))))))
+
+ (define (is-real-region region)
+ ;; real region points cannot be frame points
+ (let check-vertices ((vertices (voronoi-region-vertices region)))
+ (cond ((null? vertices)
+ #t) ; region is real
+ (else
+ (and (is-real-triangle (car vertices))
+ (check-vertices (cdr vertices)))))))
+
+ (let* ((points (voronoi-mesh-points voronoi))
+ (regions (voronoi-mesh-regions voronoi))
+ ;; we need the regions in the same order as points are
+ (ordered-regions
+ (map-in-order
+ (lambda (p)
+ (assoc-ref regions p))
+ points))
+ ;; only real regions without frame points
+ (filtered-regions
+ (filter (lambda (r) (is-real-region r))
+ ordered-regions)))
+ filtered-regions))
+
+(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
+ (lambda (p)
+ (let ((vertices
+ (filter
+ (lambda (tr) (member p (triangle-points tr)))
+ triangles)))
+ (cons p (make-voronoi-region p vertices))))
+ points))