def fragment_poly(conts): conts = constrained_delaunay_triangles( [tuple(x) for x in np.array(conts).squeeze()]) final_contours = [] for cn in conts: final_contours.append(cn) return final_contours
def test_points(polygon_with_extra_points: Tuple[Polygon, Sequence[Point]] ) -> None: polygon, extra_points = polygon_with_extra_points border, holes = polygon result = constrained_delaunay_triangles(border, holes, extra_points=extra_points) assert set(flatten(result)) == set(sum(holes, border)) | set(extra_points)
def test_sizes(polygon_with_extra_points: Tuple[Polygon, Sequence[Point]] ) -> None: polygon, extra_points = polygon_with_extra_points border, holes = polygon result = constrained_delaunay_triangles(border, holes, extra_points=extra_points) assert all(len(element) == 3 for element in result)
def test_basic(polygon_with_extra_points: Tuple[Polygon, Sequence[Point]] ) -> None: polygon, extra_points = polygon_with_extra_points border, holes = polygon result = constrained_delaunay_triangles(border, holes, extra_points=extra_points) assert isinstance(result, list) assert all(isinstance(element, tuple) for element in result)
def test_boundary(polygon_with_extra_points: Tuple[Polygon, Sequence[Point]] ) -> None: polygon, extra_points = polygon_with_extra_points border, holes = polygon result = constrained_delaunay_triangles(border, holes, extra_points=extra_points) border, holes, _ = complete_vertices(border, holes, extra_points) assert (to_boundary_endpoints(result) == set(map(frozenset, sum(map(contour_to_segments, holes), contour_to_segments(border)))))
def test_edges(polygon_with_extra_points: Tuple[Polygon, Sequence[Point]] ) -> None: polygon, extra_points = polygon_with_extra_points border, holes = polygon result = constrained_delaunay_triangles(border, holes, extra_points=extra_points) border, holes, _ = complete_vertices(border, holes, extra_points) assert (set(flatten(map(contour_to_segments, result))) >= set(sum(map(contour_to_segments, holes), contour_to_segments(border))))
def joined_constrained_delaunay_triangles( border: ContourType, holes: Sequence[ContourType] = (), *, extra_points: Sequence[PointType] = (), extra_constraints: Sequence[SegmentType] = () ) -> ConvexPartsType: """Joins polygons to form convex parts of greater size""" triangles = constrained_delaunay_triangles( border, holes, extra_points=extra_points, extra_constraints=extra_constraints) polygons = [ Polygon.from_raw((list(triangle), [])) for triangle in triangles ] initial_polygon = polygons.pop() result = [] while True: resulting_polygon = initial_polygon for index, polygon in enumerate(iter(polygons)): polygon_sides = set(edges(polygon.border)) common_side = next((edge for edge in edges(resulting_polygon.border) if edge in polygon_sides), None) if common_side is None: continue has_point_on_edge = any( Point.from_raw(raw_point) in common_side for raw_point in extra_points) if has_point_on_edge: continue union_ = unite(resulting_polygon, polygon) if isinstance(union_, Polygon) and union_.is_convex: polygons.pop(index) resulting_polygon = union_ if resulting_polygon is not initial_polygon: initial_polygon = resulting_polygon continue result.append(resulting_polygon.border.raw()) if not polygons: return result initial_polygon = polygons.pop()
def test_connection_with_delaunay_triangles(contour: Contour) -> None: result = constrained_delaunay_triangles(contour) assert ((result == delaunay_triangles(contour)) is is_convex_contour(contour))