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
use cgmath::prelude::*; use voronoi; use super::Point; #[derive(Default)] struct CentroidVisitor { points: Vec<Point>, } impl voronoi::Visitor for CentroidVisitor { fn vertex(&mut self, point: Point, cells: [usize; 3]) { self.points[cells[0]] += point; self.points[cells[1]] += point; self.points[cells[2]] += point; } fn edge(&mut self, _vertices: [usize; 2]) { } fn cell(&mut self) { self.points.push(Point::zero()) } } pub fn build_relaxed<V: voronoi::Visitor>(points: &[Point], visitor: &mut V, relaxations: usize) { if relaxations > 0 { let mut centroids = CentroidVisitor::default(); voronoi::build(points, &mut centroids); build_relaxed(¢roids.points, visitor, relaxations - 1); } else { voronoi::build(points, visitor); } }