def test_path_2d(self): base_tri = [Point2(0, 0), Point2(10, 0), Point2(10, 10)] actual = euc_to_arr(path_2d(base_tri, width=2, closed=False)) expected = [ [0.0, 1.0], [9.0, 1.0], [9.0, 10.0], [11.0, 10.0], [11.0, -1.0], [0.0, -1.0] ] self.assertEqual(expected, actual)
def test_fillet_2d_remove(self): pts = list((project_to_2D(p) for p in tri)) poly = polygon(euc_to_arr(pts)) newp = fillet_2d([pts], orig_poly=poly, fillet_rad=2, remove_material=True) expected = 'difference(){polygon(paths=[[0,1,2]],points=[[0,0],[10,0],[0,10]]);translate(v=[5.1715728753,2.0000000000]){difference(){intersection(){rotate(a=-90.1000000000){translate(v=[-998,0,0]){square(center=false,size=[1000,1000]);}}rotate(a=45.1000000000){translate(v=[-998,-1000,0]){square(center=false,size=[1000,1000]);}}}circle(r=2);}}}' actual = scad_render(newp) self.assertEqualNoWhitespace(expected, actual)
def test_path_2d_polygon(self): base_tri = [Point2(0, 0), Point2(10, 0), Point2(10, 10), Point2(0, 10)] poly = path_2d_polygon(base_tri, width=2, closed=True) expected = [(1.0, 1.0), (9.0, 1.0), (9.0, 9.0), (1.0, 9.0), (-1.0, 11.0), (11.0, 11.0), (11.0, -1.0), (-1.0, -1.0)] actual = euc_to_arr(poly.params['points']) self.assertEqual(expected, actual) # Make sure the inner and outer paths in the polygon are disjoint expected = [[0, 1, 2, 3], [4, 5, 6, 7]] actual = poly.params['paths'] self.assertEqual(expected, actual)
def test_fillet_2d_remove(self): pts = tri poly = polygon(euc_to_arr(tri)) newp = fillet_2d(tri, orig_poly=poly, fillet_rad=2, remove_material=True) expected = '\n\ndifference() {\n\tpolygon(paths = [[0, 1, 2]], points = [[0, 0, 0], [10, 0, 0], [0, 10, 0]]);\n\ttranslate(v = [5.1715728753, 2.0000000000, 0.0000000000]) {\n\t\tdifference() {\n\t\t\tintersection() {\n\t\t\t\trotate(a = 268.0000000000) {\n\t\t\t\t\ttranslate(v = [-998, 0, 0]) {\n\t\t\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\trotate(a = 407.0000000000) {\n\t\t\t\t\ttranslate(v = [-998, -1000, 0]) {\n\t\t\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcircle(r = 2);\n\t\t}\n\t}\n}' actual = scad_render(newp) if expected != actual: print(''.join(difflib.unified_diff(expected, actual))) self.assertEqual(expected, actual)
def test_offset_points_open(self): actual = euc_to_arr(offset_points(tri, offset=1, closed=False)) expected = [[0.0, 1.0], [7.585786437626904, 1.0], [-0.7071067811865479, 9.292893218813452]] self.assertEqual(expected, actual)
def test_offset_points_closed(self): actual = euc_to_arr(offset_points(tri, offset=1, closed=True)) expected = [[1.0, 1.0], [7.585786437626904, 1.0], [1.0, 7.585786437626905]] self.assertEqual(expected, actual)
def extrude_along_path( shape_pts:Points, path_pts:Points, scales:Sequence[Union[Vector2, float, Tuple2]] = None, rotations: Sequence[float] = None, transforms: Sequence[Point3Transform] = None, connect_ends = False, cap_ends = True) -> OpenSCADObject: ''' Extrude the curve defined by shape_pts along path_pts. -- For predictable results, shape_pts must be planar, convex, and lie in the XY plane centered around the origin. *Some* nonconvexity (e.g, star shapes) and nonplanarity will generally work fine -- len(scales) should equal len(path_pts). No-op if not supplied Each entry may be a single number for uniform scaling, or a pair of numbers (or Point2) for differential X/Y scaling If not supplied, no scaling will occur. -- len(rotations) should equal 1 or len(path_pts). No-op if not supplied. Each point in shape_pts will be rotated by rotations[i] degrees at each point in path_pts. Or, if only one rotation is supplied, the shape will be rotated smoothly over rotations[0] degrees in the course of the extrusion -- len(transforms) should be 1 or be equal to len(path_pts). No-op if not supplied. Each entry should be have the signature: def transform_func(p:Point3, path_norm:float, loop_norm:float): Point3 where path_norm is in [0,1] and expresses progress through the extrusion and loop_norm is in [0,1] and express progress through a single loop of the extrusion -- if connect_ends is True, the first and last loops of the extrusion will be joined, which is useful for toroidal geometries. Overrides cap_ends -- if cap_ends is True, each point in the first and last loops of the extrusion will be connected to the centroid of that loop. For planar, convex shapes, this works nicely. If shape is less planar or convex, some self-intersection may happen. Not applied if connect_ends is True ''' polyhedron_pts:Points= [] facet_indices:List[Tuple[int, int, int]] = [] # Make sure we've got Euclid Point3's for all elements shape_pts = euclidify(shape_pts, Point3) path_pts = euclidify(path_pts, Point3) src_up = Vector3(0, 0, 1) shape_pt_count = len(shape_pts) tangent_path_points: List[Point3] = [] if connect_ends: tangent_path_points = [path_pts[-1]] + path_pts + [path_pts[0]] else: first = Point3(*(path_pts[0] - (path_pts[1] - path_pts[0]))) last = Point3(*(path_pts[-1] - (path_pts[-2] - path_pts[-1]))) tangent_path_points = [first] + path_pts + [last] tangents = [tangent_path_points[i+2] - tangent_path_points[i] for i in range(len(path_pts))] for which_loop in range(len(path_pts)): # path_normal is 0 at the first path_pts and 1 at the last path_normal = which_loop/ (len(path_pts) - 1) path_pt = path_pts[which_loop] tangent = tangents[which_loop] scale = scales[which_loop] if scales else 1 rotate_degrees = None if rotations: rotate_degrees = rotations[which_loop] if len(rotations) > 1 else rotations[0] * path_normal transform_func = None if transforms: transform_func = transforms[which_loop] if len(transforms) > 1 else transforms[0] this_loop = shape_pts[:] this_loop = _scale_loop(this_loop, scale) this_loop = _rotate_loop(this_loop, rotate_degrees) this_loop = _transform_loop(this_loop, transform_func, path_normal) this_loop = transform_to_point(this_loop, dest_point=path_pt, dest_normal=tangent, src_up=src_up) loop_start_index = which_loop * shape_pt_count if (which_loop < len(path_pts) - 1): loop_facets = _loop_facet_indices(loop_start_index, shape_pt_count) facet_indices += loop_facets # Add the transformed points & facets to our final list polyhedron_pts += this_loop if connect_ends: next_loop_start_index = len(polyhedron_pts) - shape_pt_count loop_facets = _loop_facet_indices(0, shape_pt_count, next_loop_start_index) facet_indices += loop_facets elif cap_ends: # endcaps at start & end of extrusion # NOTE: this block adds points & indices to the polyhedron, so it's # very sensitive to the order this is happening in start_cap_index = len(polyhedron_pts) end_cap_index = start_cap_index + 1 last_loop_start_index = len(polyhedron_pts) - shape_pt_count start_loop_pts = polyhedron_pts[:shape_pt_count] end_loop_pts = polyhedron_pts[last_loop_start_index:] start_loop_indices = list(range(0, shape_pt_count)) end_loop_indices = list(range(last_loop_start_index, last_loop_start_index + shape_pt_count)) start_centroid, start_facet_indices = _end_cap(start_cap_index, start_loop_pts, start_loop_indices) end_centroid, end_facet_indices = _end_cap(end_cap_index, end_loop_pts, end_loop_indices) polyhedron_pts += [start_centroid, end_centroid] facet_indices += start_facet_indices facet_indices += end_facet_indices return polyhedron(points=euc_to_arr(polyhedron_pts), faces=facet_indices) # type: ignore