Replaced to_location method with location property Issue #76

This commit is contained in:
gumyr 2023-08-21 12:13:43 -04:00
parent 0b93225593
commit e756f8a003
7 changed files with 35 additions and 48 deletions

View file

@ -84,7 +84,7 @@ Neutral element: :math:`\; l_0 \in L`: ``Location()``
:math:`*: P \times L \rightarrow P` with :math:`(p,l) \mapsto p * l`
:math:`\; p * l :=` ``Plane(p.to_location() * l)`` (move plane :math:`p \in P` to location :math:`l \in L`)
:math:`\; p * l :=` ``Plane(p.location * l)`` (move plane :math:`p \in P` to location :math:`l \in L`)
Inverse element: :math:`\; l^{-1} \in L`: ``l.inverse()``
@ -93,7 +93,7 @@ Inverse element: :math:`\; l^{-1} \in L`: ``l.inverse()``
:math:`*: P \times C^n \rightarrow C^n \;` with :math:`(p,c) \mapsto p * c`, :math:`\;` for :math:`n=1,2,3`
Locate an object :math:`c \in C^n` onto plane :math:`p \in P`, i.e. ``c.moved(p.to_location())``
Locate an object :math:`c \in C^n` onto plane :math:`p \in P`, i.e. ``c.moved(p.location)``
**Placing objects at locations**

View file

@ -54,9 +54,9 @@ base_top_edges: ShapeList[Edge] = (
# Rigid Joint
#
fixed_arm = JointBox(1, 1, 5, 0.2)
j1 = RigidJoint("side", base, Plane(base.faces().sort_by(Axis.X)[-1]).to_location())
j1 = RigidJoint("side", base, Plane(base.faces().sort_by(Axis.X)[-1]).location)
j2 = RigidJoint(
"top", fixed_arm, (-Plane(fixed_arm.faces().sort_by(Axis.Z)[-1])).to_location()
"top", fixed_arm, (-Plane(fixed_arm.faces().sort_by(Axis.Z)[-1])).location
)
base.joints["side"].connect_to(fixed_arm.joints["top"])
# or
@ -76,7 +76,7 @@ swing_arm_hinge_axis = swing_arm_hinge_edge.to_axis()
base_corner_edge = base.edges().sort_by(Axis((0, 0, 0), (1, 1, 0)))[-1]
base_hinge_axis = base_corner_edge.to_axis()
j3 = RevoluteJoint("hinge", base, axis=base_hinge_axis, angular_range=(0, 180))
j4 = RigidJoint("corner", hinge_arm, swing_arm_hinge_axis.to_location())
j4 = RigidJoint("corner", hinge_arm, swing_arm_hinge_axis.location)
base.joints["hinge"].connect_to(hinge_arm.joints["corner"], angle=90)
#
@ -121,7 +121,7 @@ j7.connect_to(j8, position=6, angle=60)
#
# BallJoint
#
j9 = BallJoint("socket", base, Plane(base.faces().sort_by(Axis.X)[0]).to_location())
j9 = BallJoint("socket", base, Plane(base.faces().sort_by(Axis.X)[0]).location)
ball = JointBox(2, 2, 2, 0.99)
j10 = RigidJoint("ball", ball, Location(Vector(0, 0, 1)))
j9.connect_to(j10, angles=(10, 20, 30))

View file

@ -46,12 +46,8 @@ base_top_edges = base.edges().filter_by(loc.x_axis).group_by(loc.z_axis)[-1]
# Rigid Joint
#
fixed_arm = JointBox(1, 1, 5, 0.2)
j1 = RigidJoint(
"side", base, Plane(base.faces().sort_by(loc.x_axis).last).to_location()
)
j2 = RigidJoint(
"top", fixed_arm, (-Plane(fixed_arm.faces().sort_by().last)).to_location()
)
j1 = RigidJoint("side", base, Plane(base.faces().sort_by(loc.x_axis).last).location)
j2 = RigidJoint("top", fixed_arm, (-Plane(fixed_arm.faces().sort_by().last)).location)
base.joints["side"].connect_to(fixed_arm.joints["top"])
# or
# j1.connect_to(j2)
@ -70,7 +66,7 @@ swing_arm_hinge_axis = swing_arm_hinge_edge.to_axis()
base_corner_edge = base.edges().sort_by(Axis((0, 0, 0), (1, 1, 0)))[-1]
base_hinge_axis = base_corner_edge.to_axis()
j3 = RevoluteJoint("hinge", base, axis=base_hinge_axis, angular_range=(0, 180))
j4 = RigidJoint("corner", hinge_arm, swing_arm_hinge_axis.to_location())
j4 = RigidJoint("corner", hinge_arm, swing_arm_hinge_axis.location)
base.joints["hinge"].connect_to(hinge_arm.joints["corner"], angle=90)
@ -116,7 +112,7 @@ j7.connect_to(j8, position=6, angle=60)
#
# BallJoint
#
j9 = BallJoint("socket", base, Plane(base.faces().sort_by(Axis.X)[0]).to_location())
j9 = BallJoint("socket", base, Plane(base.faces().sort_by(Axis.X)[0]).location)
ball = JointBox(2, 2, 2, 0.99)
j10 = RigidJoint("ball", ball, Location(Vector(0, 0, 1)))
j9.connect_to(j10, angles=(10, 20, 30))

View file

@ -624,7 +624,7 @@ class LocationList:
context = WorkplaneList._get_context()
workplanes = context.workplanes if context else [Plane.XY]
global_locations = [
plane.to_location() * local_location
plane.location * local_location
for plane in workplanes
for local_location in self.local_locations
]
@ -831,7 +831,7 @@ class Locations(LocationList):
elif isinstance(point, Plane):
local_locations.append(Location(point))
elif isinstance(point, Axis):
local_locations.append(point.to_location())
local_locations.append(point.location)
elif isinstance(point, Face):
local_locations.append(Location(Plane(point)))
else:

View file

@ -445,6 +445,11 @@ class Axis:
"""Z Axis"""
return Axis((0, 0, 0), (0, 0, 1))
@property
def location(self) -> Location:
"""Return self as Location"""
return Location(Plane(origin=self.position, z_dir=self.direction))
def __init__(self, origin: VectorLike, direction: VectorLike):
self.wrapped = gp_Ax1(
Vector(origin).to_pnt(), gp_Dir(*Vector(direction).normalized().to_tuple())
@ -496,10 +501,6 @@ class Axis:
new_gp_ax1 = self.wrapped.Transformed(new_location.wrapped.Transformation())
return Axis.from_occt(new_gp_ax1)
def to_location(self) -> Location:
"""Return self as Location"""
return Location(Plane(origin=self.position, z_dir=self.direction))
def to_plane(self) -> Plane:
"""Return self as Plane"""
return Plane(origin=self.position, z_dir=self.direction)
@ -1642,7 +1643,7 @@ class Plane:
self, other: Union[Location, "Shape"]
) -> Union[Plane, List[Plane], "Shape"]:
if isinstance(other, Location):
return Plane(self.to_location() * other)
return Plane(self.location * other)
elif ( # LocationList
hasattr(other, "local_locations") and hasattr(other, "location_index")
) or ( # tuple of locations
@ -1651,7 +1652,7 @@ class Plane:
):
return [self * loc for loc in other]
elif hasattr(other, "wrapped") and not isinstance(other, Vector): # Shape
return self.to_location() * other
return self.location * other
else:
raise TypeError(
@ -1795,10 +1796,6 @@ class Plane:
"""Return Location representing the origin and z direction"""
return Location(self)
def to_location(self) -> Location:
"""Return Location representing the origin and z direction"""
return Location(self)
def to_gp_ax2(self) -> gp_Ax2:
"""Return gp_Ax2 version of the plane"""
axis = gp_Ax2()

View file

@ -3035,7 +3035,7 @@ class ShapeList(list[T]):
"""
if isinstance(group_by, Axis):
axis_as_location = group_by.to_location().inverse()
axis_as_location = group_by.location.inverse()
key_f = lambda obj: round(
# group_by.to_plane().to_local_coords(obj).center().Z, tol_digits
(axis_as_location * Location(obj.center())).position.Z,
@ -3082,7 +3082,7 @@ class ShapeList(list[T]):
ShapeList: sorted list of objects
"""
if isinstance(sort_by, Axis):
axis_as_location = sort_by.to_location().inverse()
axis_as_location = sort_by.location.inverse()
objects = sorted(
self,
# key=lambda o: sort_by.to_plane().to_local_coords(o).center().Z,
@ -6808,7 +6808,7 @@ class RevoluteJoint(Joint):
Edge.make_line((0, 0, 0), (0, 0, radius * 10)),
Edge.make_circle(radius),
]
).move(self.parent.location * self.relative_axis.to_location())
).move(self.parent.location * self.relative_axis.location)
def __init__(
self,
@ -6863,9 +6863,7 @@ class RevoluteJoint(Joint):
)
)
return (
self.relative_axis.to_location()
* rotation
* other.relative_location.inverse()
self.relative_axis.location * rotation * other.relative_location.inverse()
)
@ -6893,7 +6891,7 @@ class LinearJoint(Joint):
),
Edge.make_circle(radius),
]
).move(self.parent.location * self.relative_axis.to_location())
).move(self.parent.location * self.relative_axis.location)
def __init__(
self,
@ -7029,12 +7027,12 @@ class CylindricalJoint(Joint):
),
Edge.make_circle(radius),
]
).move(self.parent.location * self.relative_axis.to_location())
).move(self.parent.location * self.relative_axis.location)
# @property
# def axis_location(self) -> Location:
# """Current global location of joint axis"""
# return self.parent.location * self.relative_axis.to_location()
# return self.parent.location * self.relative_axis.location
def __init__(
self,
@ -7205,7 +7203,7 @@ class BallJoint(Joint):
Rotation(*[self.angular_range[i][0] for i in [0, 1, 2]])
if angles is None
else Rotation(*angles)
) * self.angle_reference.to_location()
) * self.angle_reference.location
for i, rotations in zip(
[0, 1, 2],

View file

@ -222,7 +222,7 @@ class TestAxis(DirectApiTestCase):
def test_axis_to_location(self):
# TODO: Verify this is correct
x_location = Axis.X.to_location()
x_location = Axis.X.location
self.assertTrue(isinstance(x_location, Location))
self.assertVectorAlmostEquals(x_location.position, (0, 0, 0), 5)
self.assertVectorAlmostEquals(x_location.orientation, (0, 90, 180), 5)
@ -2053,10 +2053,8 @@ class TestPlane(DirectApiTestCase):
p.y_dir, (-math.sqrt(2) / 2, math.sqrt(2) / 2, 0), 6
)
self.assertVectorAlmostEquals(p.z_dir, (0, 0, 1), 6)
self.assertVectorAlmostEquals(loc.position, p.to_location().position, 6)
self.assertVectorAlmostEquals(
loc.orientation, p.to_location().orientation, 6
)
self.assertVectorAlmostEquals(loc.position, p.location.position, 6)
self.assertVectorAlmostEquals(loc.orientation, p.location.orientation, 6)
# rotated location around x and origin <> (0,0,0)
loc = Location((0, 2, -1), (45, 0, 0))
@ -2069,8 +2067,8 @@ class TestPlane(DirectApiTestCase):
self.assertVectorAlmostEquals(
p.z_dir, (0, -math.sqrt(2) / 2, math.sqrt(2) / 2), 6
)
self.assertVectorAlmostEquals(loc.position, p.to_location().position, 6)
self.assertVectorAlmostEquals(loc.orientation, p.to_location().orientation, 6)
self.assertVectorAlmostEquals(loc.position, p.location.position, 6)
self.assertVectorAlmostEquals(loc.orientation, p.location.orientation, 6)
# from a face
f = Face.make_rect(1, 2).located(Location((1, 2, 3), (45, 0, 45)))
@ -2085,11 +2083,9 @@ class TestPlane(DirectApiTestCase):
self.assertVectorAlmostEquals(
p.z_dir, (0, -math.sqrt(2) / 2, math.sqrt(2) / 2), 6
)
self.assertVectorAlmostEquals(f.location.position, p.location.position, 6)
self.assertVectorAlmostEquals(
f.location.position, p.to_location().position, 6
)
self.assertVectorAlmostEquals(
f.location.orientation, p.to_location().orientation, 6
f.location.orientation, p.location.orientation, 6
)
# from a face with x_dir
@ -2315,7 +2311,7 @@ class TestPlane(DirectApiTestCase):
)
def test_to_location(self):
loc = Plane(origin=(1, 2, 3), x_dir=(0, 1, 0), z_dir=(0, 0, 1)).to_location()
loc = Plane(origin=(1, 2, 3), x_dir=(0, 1, 0), z_dir=(0, 0, 1)).location
self.assertVectorAlmostEquals(loc.position, (1, 2, 3), 5)
self.assertVectorAlmostEquals(loc.orientation, (0, 0, 90), 5)