// with the manual startpoint private static Point2F prepare_segments_w_manual_starpoint(Topographer topo, List <Line2F> segments, Segpool pool, double min_dist_to_wall, double general_tolerance, Point2F startpoint) { // same as automatic, but seek the segment with the closest end to startpoint if (!topo.Is_line_inside_region(new Line2F(startpoint, startpoint), general_tolerance)) { Logger.warn("startpoint is outside the pocket"); return(Point2F.Undefined); } if (topo.Get_dist_to_wall(startpoint) < min_dist_to_wall) { Logger.warn("startpoint radius < tool radius"); return(Point2F.Undefined); } double min_dist = double.MaxValue; Point2F tree_start = Point2F.Undefined; foreach (Line2F seg in segments) { double r1 = topo.Get_dist_to_wall(seg.p1); double r2 = topo.Get_dist_to_wall(seg.p2); if (r1 >= min_dist_to_wall) { pool.Add(seg, false); double dist = startpoint.DistanceTo(seg.p1); if (dist < min_dist && topo.Is_line_inside_region(new Line2F(startpoint, seg.p1), general_tolerance)) { min_dist = dist; tree_start = seg.p1; } } if (r2 >= min_dist_to_wall) { pool.Add(seg, true); double dist = startpoint.DistanceTo(seg.p2); if (dist < min_dist && topo.Is_line_inside_region(new Line2F(startpoint, seg.p2), general_tolerance)) { min_dist = dist; tree_start = seg.p2; } } } return(tree_start); }
private static List <Line2F> get_medial_axis_segments(Topographer topo, List <Point2F> samples, double general_tolerance) { double[] xs = new double[samples.Count + 1]; double[] ys = new double[samples.Count + 1]; double min_x = double.MaxValue; double max_x = double.MinValue; double min_y = double.MaxValue; double max_y = double.MinValue; // HACK // There is a bug in Voronoi generator implementation. Sometimes it produces a completely crazy partitioning. // Looks like its overly sensitive to the first processed points, their count and location. If first stages // go ok, then everything comes nice. Beeing a Steven Fortune's algorithm, it process points by a moving sweep line. // Looks like the line is moving from the bottom to the top, thus sensitive points are the most bottom ones. // We try to cheat and add one more point so it would be the single most bottom point. // Then generator initially will see just one point, do a right magic and continue with a sane result :-) // We place this initial point under the lefmost bottom point at the sufficient distance, // then these two points will form a separate Voronoi cite not influencing the remaining partition. // Sufficient distance is defined as region width / 2 for now. int lb_idx = 0; for (int i = 0; i < samples.Count; i++) { xs[i] = samples[i].X; ys[i] = samples[i].Y; if (xs[i] < min_x) { min_x = xs[i]; } if (xs[i] > max_x) { max_x = xs[i]; } if (ys[i] > max_y) { max_y = ys[i]; } if (ys[i] <= min_y) { if (ys[i] < min_y) { min_y = ys[i]; lb_idx = i; // stricly less, it's a new leftmost bottom for sure } else { if (xs[i] < xs[lb_idx]) // it's a new leftmost bottom if more lefty { lb_idx = i; } } } } double width = max_x - min_x; xs[samples.Count] = xs[lb_idx]; ys[samples.Count] = ys[lb_idx] - width / 2; min_x -= VORONOI_MARGIN; max_x += VORONOI_MARGIN; min_y -= VORONOI_MARGIN + width / 2; max_y += VORONOI_MARGIN; List <GraphEdge> edges = new Voronoi(general_tolerance).generateVoronoi(xs, ys, min_x, max_x, min_y, max_y); Logger.log("voronoi partitioning completed. got {0} edges", edges.Count); List <Line2F> inner_segments = new List <Line2F>(); foreach (GraphEdge e in edges) { Line2F seg = new Line2F(e.x1, e.y1, e.x2, e.y2); if (seg.Length() < double.Epsilon) { continue; // extra small segment, discard } if (!topo.Is_line_inside_region(seg, ANALIZE_INNER_INTERSECTIONS, general_tolerance)) { continue; } inner_segments.Add(seg); } return(inner_segments); }