def to_polygon(self): """ turn into a polygon by joining all points. only meaningful if paths inside are sorted in the right order """ return Polygon(list(self.get_points()))
def test_one_level(): """ very basic test with all polygons at same height """ outside_square = Polygon.square(0, 0, 10) inner_square = Polygon.square(2, 2, 2) inner_square2 = Polygon.square(6, 6, 2) inner_inner = Polygon.square(2.1, 2.1, 0.3) polygons = {0: [outside_square, inner_square, inner_square2, inner_inner]} tycat(polygons[0]) print("outside square", id(outside_square)) print("inner square (top left)", id(inner_square)) print("inner square (bottom right)", id(inner_square2)) print("inner inner square (inside top left)", id(inner_inner)) tree = build_inclusion_tree(polygons) tree.tycat()
#!/usr/bin/env python3 from jimn.point import Point from jimn.polygon import Polygon from jimn.displayable import tycat from jimn.algorithms.offsetter import offset_holed_polygon from jimn.utils.debug import add_module_to_debug print("polygon with hole reaching border") outer = Polygon.square(0, 0, 10).orient(False) inner = Polygon.square(1, 1, 3).orient(True) tycat(outer, inner) pockets = offset_holed_polygon(2, outer, inner) tycat(outer, inner, *pockets) print("polygon with thin part") outer = Polygon([ Point([0, 0]), Point([0, 1]), Point([3, 1]), Point([3, 10]), Point([10, 10]), Point([10, 0]), ]) tycat(outer) pockets = offset_holed_polygon(2, outer) tycat(outer, *pockets)
p = Polygon([ Point([-1.5, 0.2071000039577484]), Point([-1.29497096657753, 0.7020999744534493]), Point([-1.2928999662399292, 0.707099974155426]), Point([-1.1728129839897157, 0.9970709997415542]), Point([-1.1715999841690063, 1.0]), Point([-1.1728129839897157, 1.0029289996623993]), Point([-1.2928999662399292, 1.2928999662399292]), Point([-1.0029289996623993, 1.1728129839897157]), Point([-1.0, 1.1715999841690063]), Point([-0.7100289744138718, 1.2916869664192199]), Point([-0.707099974155426, 1.2928999662399292]), Point([-0.2121000036597252, 1.4979289996623992]), Point([-0.2071000039577484, 1.5]), Point([-0.002071000039577484, 1.005]), Point([0.0, 1.0]), Point([0.20502900391817092, 1.495]), Point([0.2071000039577484, 1.5]), Point([0.7020999744534493, 1.29497096657753]), Point([0.707099974155426, 1.2928999662399292]), Point([0.9970709997415542, 1.1728129839897157]), Point([1.0, 1.1715999841690063]), Point([1.2899709665775299, 1.2916869664192199]), Point([1.2928999662399292, 1.2928999662399292]), Point([1.2916869664192199, 1.2899709665775299]), Point([1.1715999841690063, 1.0]), Point([1.2916869664192199, 0.7100289744138718]), Point([1.2928999662399292, 0.707099974155426]), Point([1.4979289996623992, 0.2121000036597252]), Point([1.5, 0.2071000039577484]), Point([1.495, 0.20502900391817092]), Point([1.0, 0.0]), Point([1.495, -0.20502900391817092]), Point([1.5, -0.2071000039577484]), Point([1.4979289996623992, -0.2121000036597252]), Point([1.2928999662399292, -0.707099974155426]), Point([1.2916869664192199, -0.7100289744138718]), Point([1.1715999841690063, -1.0]), Point([1.2916869664192199, -1.2899709665775299]), Point([1.2928999662399292, -1.2928999662399292]), Point([1.2899709665775299, -1.2916869664192199]), Point([1.0, -1.1715999841690063]), Point([0.9970709997415542, -1.1728129839897157]), Point([0.707099974155426, -1.2928999662399292]), Point([0.7020999744534493, -1.29497096657753]), Point([0.2071000039577484, -1.5]), Point([0.20502900391817092, -1.495]), Point([0.0, -1.0]), Point([-0.002071000039577484, -1.005]), Point([-0.2071000039577484, -1.5]), Point([-0.2121000036597252, -1.4979289996623992]), Point([-0.707099974155426, -1.2928999662399292]), Point([-0.7100289744138718, -1.2916869664192199]), Point([-1.0, -1.1715999841690063]), Point([-1.0029289996623993, -1.1728129839897157]), Point([-1.2928999662399292, -1.2928999662399292]), Point([-1.1728129839897157, -1.0029289996623993]), Point([-1.1715999841690063, -1.0]), Point([-1.1728129839897157, -0.9970709997415542]), Point([-1.2928999662399292, -0.707099974155426]), Point([-1.29497096657753, -0.7020999744534493]), Point([-1.5, -0.2071000039577484]), Point([-1.005, -0.002071000039577484]), Point([-1.0, 0.0]), Point([-1.005, 0.002071000039577484]) ])
#!/usr/bin/env python3 from jimn.polygon import Polygon from jimn.algorithms.offsetter import offset_holed_polygon from jimn.pocket.graph_builder import build_graph from jimn.graph.eulerian_cycle import find_eulerian_cycle, cycle_to_path from jimn.tree.path_tree import PathTree polygons = [ Polygon.square(0, 0, 12), Polygon.square(2.5, 2.5, 3), Polygon.square(6.5, 6.5, 3), Polygon.square(0.1, 0.1, 4.1), Polygon.square(0.1, 0.1, 4.1), Polygon.square(0.1, 0.1, 4.1), ] polygons = [p.orient(False) for p in polygons] pockets = [offset_holed_polygon(1, p)[0] for p in polygons] graphs = [build_graph(p, 1) for p in pockets] paths = [cycle_to_path(find_eulerian_cycle(g)) for g in graphs] trees = [PathTree(paths[i], pockets[i].outer_edge) for i in range(len(paths))] print("fully overlapping") t = PathTree() t.children = [trees[3]] trees[3].children = [trees[4]] trees[4].children = [trees[5]] t.tycat() result = t.global_path(1)
#!/usr/bin/env python3 from jimn.polygon import Polygon from jimn.displayable import tycat from jimn.tree.polygon_tree import PolygonTree from jimn.tree.pocket_tree import PocketTree from jimn.tree.path_tree import PathTree milling_radius = 0.9 polygons = {} polygons[1] = [Polygon.square(0, 0, 10)] polygons[0] = [ Polygon.square(0, 0, 10), Polygon.square(2, 2, 6), Polygon.square(3.55, 4.55, 3), ] print("layouts are") tycat(polygons[1]) tycat(polygons[0]) tree = PolygonTree.build(milling_radius, polygons) print("corresponding tree") tree.tycat() pockets = PocketTree.build(tree, milling_radius) print("offsetted tree") pockets.tycat() paths = PathTree.build(pockets, milling_radius)
from jimn.holed_pocket import HoledPocket from jimn.pocket.graph_builder import build_graph from jimn.graph.eulerian_cycle import find_eulerian_cycle, cycle_to_path tycat_set_svg_dimensions(640, 480) print("small graph with hole") a = Point([0.5, 0.1]) b = Point([0.0, 1.1]) c = Point([1.0, 1.1]) d = Point([0.5, 0.4]) e = Point([0.2, 1.0]) f = Point([0.8, 1.0]) outer_edge = Pocket(list(Polygon([a, b, c]).segments())) hole = Pocket(list(Polygon([d, e, f]).segments())) abc = HoledPocket(outer_edge, [hole]) g = build_graph(abc, 0.3) tycat(g) p = cycle_to_path(find_eulerian_cycle(g)) p.animate(0.3) print("top vertex is lying on a cut line") a = Point([0.6, 0.0]) b = Point([0.0, 1.2]) c = Point([1.2, 1.2]) segments = list(Polygon([a, b, c]).segments()) # outer edge
def test(): """ small examples with holes and fillings. """ outer_shell = [ Point([0.0, 0.0]), Point([9.0, 0.0]), Point([9.0, 8.0]), Point([0.0, 8.0]), ] big_hole = [ Point([1.0, 5.0]), Point([4.0, 3.0]), Point([8.0, 5.0]), Point([4.0, 7.0]) ] small_fill1 = [ Point([2.0, 4.5]), Point([3.0, 4.5]), Point([3.0, 5.5]), Point([2.0, 5.5]) ] small_fill2 = [ Point([5.0, 4.5]), Point([6.0, 4.5]), Point([6.0, 5.5]), Point([5.0, 5.5]) ] small_hole = [ Point([6.0, 1.0]), Point([7.0, 1.0]), Point([7.0, 2.0]), Point([6.0, 2.0]) ] polygons = {} polygons[4] = [Polygon(list(outer_shell))] polygons[3] = [Polygon(list(outer_shell))] polygons[2] = [ Polygon(list(outer_shell)), Polygon(list(big_hole)), Polygon(list(small_fill1)), Polygon(list(small_fill2)), Polygon(list(small_hole)) ] polygons[1] = [ Polygon(list(outer_shell)), Polygon(list(big_hole)), Polygon(list(small_fill1)), Polygon(list(small_hole)) ] for height in sorted(polygons.keys(), reverse=True): print("height:", height) tycat(polygons[height]) add_module_to_debug( "jimn.algorithms.sweeping_line_algorithms.inclusion_tree_builder") add_module_to_debug("jimn.tree.polygon_tree") tree = PolygonTree.build(0.01, polygons) print("simulating depth first carving") tree.display_depth_first()
#!/usr/bin/env python3 from jimn.point import Point from jimn.segment import Segment from jimn.polygon import Polygon from jimn.pocket import Pocket from jimn.envelope import Envelope from jimn.arc import Arc from jimn.displayable import tycat from jimn.displayable import tycat_start, tycat_end print("testing intersection") s = Polygon.square(0, 0, 8) s = s.orient(False) path_in = Pocket(list(s.segments())) path_out = Segment([Point([-3, 3]), Point([12, 5])]) e1 = Envelope(path_in, 1) e2 = Envelope(path_out, 1) tycat(e1, e2) points = e2.junction_points(e1) tycat(e1, e2, points)
#!/usr/bin/env python from jimn.point import Point from jimn.displayable import tycat from jimn.utils.iterators import all_two_elements from jimn.segment import Segment from jimn.pocket import Pocket from jimn.polygon import Polygon # testing inclusion square = Polygon.square(-0.5, 5, 1) tested_pocket = Pocket(list(square.segments())) square2 = Polygon.square(0.5, 5, 1) tested_pocket2 = Pocket(list(square2.segments())) including = Polygon.square(-1, 4, 3) including_pocket = Pocket(list(including.segments())) not_including = [ Point([-2.0, 3.0]), Point([-2.0, 2.0]), Point([9.0, 2.0]), Point([9.0, 10.0]), Point([-2.0, 10.0]), Point([-2.0, 9.0]), Point([8.0, 9.0]), Point([8.0, 3.0]), ] not_including_pocket = Pocket(
#!/usr/bin/env python3 from jimn.polygon import Polygon from jimn.displayable import tycat from jimn.tree.polygon_tree import PolygonTree from jimn.tree.pocket_tree import PocketTree polygons = {} polygons[2] = [Polygon.square(0, 0, 10)] polygons[1] = [ Polygon.square(0, 0, 10), Polygon.square(1, 1, 3), Polygon.square(5, 1, 3), Polygon.square(5.5, 1.5, 2) ] polygons[0] = [ Polygon.square(0, 0, 10), Polygon.square(5, 1, 3), Polygon.square(5.5, 1.5, 0.5), Polygon.square(7, 3, 0.5) ] print("layouts are") tycat(polygons[2]) tycat(polygons[1]) tycat(polygons[0]) print("corresponding tree") tree = PolygonTree.build(0.1, polygons) tree.tycat()