Skip to content

Commit 0624cc3

Browse files
Fixes to offset_points() & tests. The concept of 'open' or 'closed' didn't make much sense when returning a list of points; removed the 'closed' arg
1 parent a729e15 commit 0624cc3

2 files changed

Lines changed: 34 additions & 26 deletions

File tree

‎solid/test/test_utils.py‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,42 @@
1515
from solid.utils import label
1616

1717
tri = [Point3(0, 0, 0), Point3(10, 0, 0), Point3(0, 10, 0)]
18+
1819
scad_test_cases = [
19-
(up, [2], '\n\ntranslate(v = [0, 0, 2]);'),
20-
(down, [2], '\n\ntranslate(v = [0, 0, -2]);'),
21-
(left, [2], '\n\ntranslate(v = [-2, 0, 0]);'),
22-
(right, [2], '\n\ntranslate(v = [2, 0, 0]);'),
23-
(forward, [2], '\n\ntranslate(v = [0, 2, 0]);'),
24-
(back, [2], '\n\ntranslate(v = [0, -2, 0]);'),
25-
(arc, [10, 0, 90, 24], '\n\ndifference() {\n\tcircle($fn = 24, r = 10);\n\trotate(a = 0) {\n\t\ttranslate(v = [0, -10, 0]) {\n\t\t\tsquare(center = true, size = [30, 20]);\n\t\t}\n\t}\n\trotate(a = -90) {\n\t\ttranslate(v = [0, -10, 0]) {\n\t\t\tsquare(center = true, size = [30, 20]);\n\t\t}\n\t}\n}'),
26-
(arc_inverted, [10, 0, 90, 24], '\n\ndifference() {\n\tintersection() {\n\t\trotate(a = 0) {\n\t\t\ttranslate(v = [-990, 0, 0]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t\trotate(a = 90) {\n\t\t\ttranslate(v = [-990, -1000, 0]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t}\n\tcircle($fn = 24, r = 10);\n}'),
20+
# Test name, function, args, expected value
21+
('up', up, [2], '\n\ntranslate(v = [0, 0, 2]);'),
22+
('down', down, [2], '\n\ntranslate(v = [0, 0, -2]);'),
23+
('left', left, [2], '\n\ntranslate(v = [-2, 0, 0]);'),
24+
('right', right, [2], '\n\ntranslate(v = [2, 0, 0]);'),
25+
('forward', forward, [2], '\n\ntranslate(v = [0, 2, 0]);'),
26+
('back', back, [2], '\n\ntranslate(v = [0, -2, 0]);'),
27+
('arc', arc, [10, 0, 90, 24], '\n\ndifference() {\n\tcircle($fn = 24, r = 10);\n\trotate(a = 0) {\n\t\ttranslate(v = [0, -10, 0]) {\n\t\t\tsquare(center = true, size = [30, 20]);\n\t\t}\n\t}\n\trotate(a = -90) {\n\t\ttranslate(v = [0, -10, 0]) {\n\t\t\tsquare(center = true, size = [30, 20]);\n\t\t}\n\t}\n}'),
28+
('arc_inverted', arc_inverted, [10, 0, 90, 24], '\n\ndifference() {\n\tintersection() {\n\t\trotate(a = 0) {\n\t\t\ttranslate(v = [-990, 0, 0]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t\trotate(a = 90) {\n\t\t\ttranslate(v = [-990, -1000, 0]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t}\n\tcircle($fn = 24, r = 10);\n}'),
2729
('transform_to_point_scad', transform_to_point, [cube(2), [2, 2, 2], [3, 3, 1]], '\n\nmultmatrix(m = [[0.7071067812, -0.1622214211, -0.6882472016, 2], [-0.7071067812, -0.1622214211, -0.6882472016, 2], [0.0000000000, 0.9733285268, -0.2294157339, 2], [0, 0, 0, 1.0000000000]]) {\n\tcube(size = 2);\n}'),
2830
('extrude_along_path', extrude_along_path, [tri, [[0, 0, 0], [0, 20, 0]]], '\n\npolyhedron(faces = [[0, 3, 1], [1, 3, 4], [1, 4, 2], [2, 4, 5], [0, 2, 5], [0, 5, 3], [0, 1, 2], [3, 5, 4]], points = [[0.0000000000, 0.0000000000, 0.0000000000], [10.0000000000, 0.0000000000, 0.0000000000], [0.0000000000, 0.0000000000, 10.0000000000], [0.0000000000, 20.0000000000, 0.0000000000], [10.0000000000, 20.0000000000, 0.0000000000], [0.0000000000, 20.0000000000, 10.0000000000]]);'),
2931
('extrude_along_path_vertical', extrude_along_path, [tri, [[0, 0, 0], [0, 0, 20]]], '\n\npolyhedron(faces = [[0, 3, 1], [1, 3, 4], [1, 4, 2], [2, 4, 5], [0, 2, 5], [0, 5, 3], [0, 1, 2], [3, 5, 4]], points = [[0.0000000000, 0.0000000000, 0.0000000000], [-10.0000000000, 0.0000000000, 0.0000000000], [0.0000000000, 10.0000000000, 0.0000000000], [0.0000000000, 0.0000000000, 20.0000000000], [-10.0000000000, 0.0000000000, 20.0000000000], [0.0000000000, 10.0000000000, 20.0000000000]]);'),
3032

3133
]
3234

3335
other_test_cases = [
34-
(euclidify, [[0, 0, 0]], 'Vector3(0.00, 0.00, 0.00)'),
36+
# Test name, function, args, expected value
37+
('euclidify', euclidify, [[0, 0, 0]], 'Vector3(0.00, 0.00, 0.00)'),
3538
('euclidify_recursive', euclidify, [[[0, 0, 0], [1, 0, 0]]], '[Vector3(0.00, 0.00, 0.00), Vector3(1.00, 0.00, 0.00)]'),
3639
('euclidify_Vector', euclidify, [Vector3(0, 0, 0)], 'Vector3(0.00, 0.00, 0.00)'),
3740
('euclidify_recursive_Vector', euclidify, [[Vector3(0, 0, 0), Vector3(0, 0, 1)]], '[Vector3(0.00, 0.00, 0.00), Vector3(0.00, 0.00, 1.00)]'),
38-
(euc_to_arr, [Vector3(0, 0, 0)], '[0, 0, 0]'),
41+
('euc_to_arr', euc_to_arr, [Vector3(0, 0, 0)], '[0, 0, 0]'),
3942
('euc_to_arr_recursive', euc_to_arr, [[Vector3(0, 0, 0), Vector3(0, 0, 1)]], '[[0, 0, 0], [0, 0, 1]]'),
4043
('euc_to_arr_arr', euc_to_arr, [[0, 0, 0]], '[0, 0, 0]'),
4144
('euc_to_arr_arr_recursive', euc_to_arr, [[[0, 0, 0], [1, 0, 0]]], '[[0, 0, 0], [1, 0, 0]]'),
42-
(is_scad, [cube(2)], 'True'),
45+
('is_scad', is_scad, [cube(2)], 'True'),
4346
('is_scad_false', is_scad, [2], 'False'),
4447
('transform_to_point_single_arr', transform_to_point, [[1, 0, 0], [2, 2, 2], [3, 3, 1]], 'Point3(2.71, 1.29, 2.00)'),
4548
('transform_to_point_single_pt3', transform_to_point, [Point3(1, 0, 0), [2, 2, 2], [3, 3, 1]], 'Point3(2.71, 1.29, 2.00)'),
4649
('transform_to_point_arr_arr', transform_to_point, [[[1, 0, 0], [0, 1, 0], [0, 0, 1]], [2, 2, 2], [3, 3, 1]], '[Point3(2.71, 1.29, 2.00), Point3(1.84, 1.84, 2.97), Point3(1.31, 1.31, 1.77)]'),
4750
('transform_to_point_pt3_arr', transform_to_point, [[Point3(1, 0, 0), Point3(0, 1, 0), Point3(0, 0, 1)], [2, 2, 2], [3, 3, 1]], '[Point3(2.71, 1.29, 2.00), Point3(1.84, 1.84, 2.97), Point3(1.31, 1.31, 1.77)]'),
4851
('transform_to_point_redundant', transform_to_point, [[Point3(0, 0, 0), Point3(10, 0, 0), Point3(0, 10, 0)], [2, 2, 2], Vector3(0, 0, 1), Point3(0, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)], '[Point3(2.00, 2.00, 2.00), Point3(-8.00, 2.00, 2.00), Point3(2.00, 12.00, 2.00)]'),
49-
('offset_points_inside', offset_points, [tri, 2, True], '[Point3(2.00, 2.00, 0.00), Point3(5.17, 2.00, 0.00), Point3(2.00, 5.17, 0.00)]'),
50-
('offset_points_outside', offset_points, [tri, 2, False], '[Point3(-2.00, -2.00, 0.00), Point3(14.83, -2.00, 0.00), Point3(-2.00, 14.83, 0.00)]'),
51-
('offset_points_open_poly', offset_points, [tri, 2, False, False], '[Point3(0.00, -2.00, 0.00), Point3(14.83, -2.00, 0.00), Point3(1.41, 11.41, 0.00)]'),
52+
('offset_points_inside', offset_points, [tri, 2, True], '[Point2(2.00, 2.00), Point2(5.17, 2.00), Point2(2.00, 5.17)]'),
53+
('offset_points_outside', offset_points, [tri, 2, False], '[Point2(-2.00, -2.00), Point2(14.83, -2.00), Point2(-2.00, 14.83)]'),
5254
]
5355

5456

@@ -98,6 +100,11 @@ def test_fillet_2d_remove(self):
98100

99101
self.assertEqualNoWhitespace(expected, actual)
100102

103+
# def test_offset_points_inside(self):
104+
# expected = ''
105+
# actual = scad_render(offset_points(tri2d, offset=2, internal=True))
106+
# self.assertEqualNoWhitespace(expected, actual)
107+
101108
def test_label(self):
102109
expected = 'translate(v=[0,5.0000000000,0]){resize(newsize=[15,0,0.5000000000]){union(){translate(v=[0,0.0000000000,0]){linear_extrude(height=1){text($fn=40,font="MgOpenModata:style=Bold",halign="left",spacing=1,text="Hello,",valign="baseline");}}translate(v=[0,-11.5000000000,0]){linear_extrude(height=1){text($fn=40,font="MgOpenModata:style=Bold",halign="left",spacing=1,text="World",valign="baseline");}}}}}'
103110
actual = scad_render(label("Hello,\nWorld"))

‎solid/utils.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -900,14 +900,21 @@ def offset_point(a:Point2, b:Point2, c:Point2, offset:float, direction:Direction
900900
result = ab_par.intersect(bc_par)
901901
return result
902902

903-
def offset_points(points:Sequence[Point2],
903+
def offset_points(points:Sequence[Point23],
904904
offset:float,
905-
internal:bool=True,
906-
closed:bool=False) -> List[Point2]:
905+
internal:bool=True) -> List[Point2]:
907906
"""
908907
Given a set of points, return a set of points offset by `offset`, in the
909908
direction specified by `internal`.
910909
910+
NOTE: OpenSCAD has the native `offset()` function that generates offset
911+
polygons nicely as well as doing fillets & rounds. If you just need a shape,
912+
prefer using the native `offset()`. If you need the actual points for some
913+
purpose, use this function.
914+
See: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#offset
915+
916+
# NOTE: We accept Point2s or Point3s, but ignore all Z values and return Point2s
917+
911918
What is internal or external is defined by by the direction of curvature
912919
between the first and second points; for non-convex shapes, we will return
913920
an incorrect (internal points are all external, or vice versa) if the first
@@ -916,10 +923,7 @@ def offset_points(points:Sequence[Point2],
916923
"""
917924
# Note that we could just call offset_point() repeatedly, but we'd do
918925
# a lot of repeated calculations that way
919-
src_points = list(points)
920-
if closed:
921-
src_points.append(points[0])
922-
# src_points = cast(points, List) + [points[0]] if closed else points
926+
src_points = list((Point2(p.x, p.y) for p in (*points, points[0])))
923927

924928
vecs = vectors_between_points(src_points)
925929
direction = direction_of_bend(*src_points[:3])
@@ -933,12 +937,9 @@ def offset_points(points:Sequence[Point2],
933937
lines.append(Line2(a+perp, b+perp))
934938

935939
intersections = list((a.intersect(b) for a,b in zip(lines[:-1], lines[1:])))
936-
if closed:
940+
# First point is determined by intersection of first and last lines
941+
intersections.insert(0, lines[0].intersect(lines[-1]))
937942

938-
intersections.append(lines[0].intersect(lines[-1]))
939-
else:
940-
# Include offset points at start and end of shape
941-
intersections = [src_points[0] + perp_vecs[0], *intersections, src_points[-1] + perp_vecs[-1]]
942943
return intersections
943944

944945
# ==================

0 commit comments

Comments
 (0)