Skip to content

Commit

Permalink
Merge pull request #680 from danieledapo/dev
Browse files Browse the repository at this point in the history
fix location_at, position_at and tangent_at with PositionMode.LENGTH for Wire
  • Loading branch information
gumyr authored Aug 30, 2024
2 parents e9539dc + dfb1cee commit 55a6e8b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/build123d/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def tangent_at(
if position_mode == PositionMode.PARAMETER:
parameter = self.param_at(position)
else:
parameter = position
parameter = self.param_at(position / self.length)
else:
try:
pnt = Vector(position)
Expand Down Expand Up @@ -682,7 +682,7 @@ def position_at(
if position_mode == PositionMode.PARAMETER:
param = self.param_at(distance)
else:
param = distance
param = self.param_at(distance / self.length)

return Vector(curve.Value(param))

Expand Down Expand Up @@ -733,7 +733,7 @@ def location_at(
if position_mode == PositionMode.PARAMETER:
param = self.param_at(distance)
else:
param = distance
param = self.param_at(distance / self.length)

law: GeomFill_TrihedronLaw
if frame_method == FrameMethod.FRENET:
Expand Down
60 changes: 60 additions & 0 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,52 @@ def test_position_at(self):
)
self.assertTrue(all([0.0 < v < 1.0 for v in point]))

wire = Wire([Edge.make_line((0, 0, 0), (10, 0, 0))])
self.assertVectorAlmostEquals(wire.position_at(0.3), (3, 0, 0), 5)
self.assertVectorAlmostEquals(
wire.position_at(3, position_mode=PositionMode.LENGTH), (3, 0, 0), 5
)
self.assertVectorAlmostEquals(wire.edge().position_at(0.3), (3, 0, 0), 5)
self.assertVectorAlmostEquals(
wire.edge().position_at(3, position_mode=PositionMode.LENGTH), (3, 0, 0), 5
)

circle_wire = Wire(
[
Edge.make_circle(1, start_angle=0, end_angle=180),
Edge.make_circle(1, start_angle=180, end_angle=360),
]
)
p1 = circle_wire.position_at(math.pi, position_mode=PositionMode.LENGTH)
p2 = circle_wire.position_at(math.pi / circle_wire.length)
self.assertVectorAlmostEquals(p1, (-1, 0, 0), 14)
self.assertVectorAlmostEquals(p2, (-1, 0, 0), 14)
self.assertVectorAlmostEquals(p1, p2, 14)

circle_edge = Edge.make_circle(1)
p3 = circle_edge.position_at(math.pi, position_mode=PositionMode.LENGTH)
p4 = circle_edge.position_at(math.pi / circle_edge.length)
self.assertVectorAlmostEquals(p3, (-1, 0, 0), 14)
self.assertVectorAlmostEquals(p4, (-1, 0, 0), 14)
self.assertVectorAlmostEquals(p3, p4, 14)

circle = Wire(
[
Edge.make_circle(2, start_angle=0, end_angle=180),
Edge.make_circle(2, start_angle=180, end_angle=360),
]
)
self.assertVectorAlmostEquals(
circle.position_at(0.5),
(-2, 0, 0),
5,
)
self.assertVectorAlmostEquals(
circle.position_at(2 * math.pi, position_mode=PositionMode.LENGTH),
(-2, 0, 0),
5,
)

def test_positions(self):
e = Edge.make_line((0, 0, 0), (1, 1, 1))
distances = [i / 4 for i in range(3)]
Expand All @@ -2009,6 +2055,14 @@ def test_tangent_at(self):
)
self.assertTrue(all([0.0 <= v <= 1.0 for v in tangent]))

self.assertVectorAlmostEquals(
Edge.make_circle(1, start_angle=0, end_angle=180).tangent_at(
math.pi / 2, position_mode=PositionMode.LENGTH
),
(-1, 0, 0),
5,
)

def test_tangent_at_point(self):
circle = Wire(
[
Expand Down Expand Up @@ -2078,6 +2132,12 @@ def test_location_at(self):
self.assertVectorAlmostEquals(loc.position, (0, 1, 0), 5)
self.assertVectorAlmostEquals(loc.orientation, (0, -90, -90), 5)

loc = Edge.make_circle(1).location_at(
math.pi / 2, position_mode=PositionMode.LENGTH
)
self.assertVectorAlmostEquals(loc.position, (0, 1, 0), 5)
self.assertVectorAlmostEquals(loc.orientation, (0, -90, -90), 5)

def test_locations(self):
locs = Edge.make_circle(1).locations([i / 4 for i in range(4)])
self.assertVectorAlmostEquals(locs[0].position, (1, 0, 0), 5)
Expand Down

0 comments on commit 55a6e8b

Please sign in to comment.