Skip to content

Commit 5abd92f

Browse files
committed
Merge pull request #253 from personalrobotics/feature/rogue_util
Adding Util function for RoGuE
2 parents fe4c12c + 76d8f08 commit 5abd92f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/prpy/util.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,3 +1598,39 @@ def GetManipulatorIndex(robot, manip=None):
15981598
manip_idx = manip.GetRobot().GetActiveManipulatorIndex()
15991599

16001600
return (manip, manip_idx)
1601+
1602+
def GetPointFrom(focus):
1603+
"""
1604+
Given a kinbody, array or transform, returns the xyz
1605+
location.
1606+
param focus The area to be referred to
1607+
"""
1608+
#Pointing at a kinbody
1609+
if isinstance(focus, openravepy.KinBody):
1610+
with focus.GetEnv():
1611+
focus_trans = focus.GetTransform()
1612+
coord = list(focus_trans[0:3, 3])
1613+
1614+
#Pointing at a kinbody link
1615+
elif isinstance(focus, openravepy.KinBody.Link):
1616+
with focus.GetParent().GetEnv():
1617+
focus_trans = focus.GetTransform()
1618+
coord = list(focus_trans[0:3, 3])
1619+
1620+
#Pointing at a point in space as numpy array
1621+
elif (isinstance(focus, numpy.ndarray) and (focus.ndim == 1)
1622+
and (len(focus) == 3)):
1623+
coord = list(focus)
1624+
1625+
#Pointing at point in space as 4x4 transform
1626+
elif isinstance(focus, numpy.ndarray) and (focus.shape == (4, 4)):
1627+
coord = list(focus[0:3, 3])
1628+
1629+
#Pointing at a point in space as list or tuple
1630+
elif (isinstance(focus, (tuple, list)) and len(focus) == 3):
1631+
coord = focus
1632+
1633+
else:
1634+
raise ValueError('Focus of the point is an unknown object')
1635+
1636+
return coord

tests/test_util.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,51 @@ def test_SampleTimeGenerator_(self):
595595
decimal=7, err_msg=error, \
596596
verbose=True)
597597

598+
class Test_GetPointFrom(unittest.TestCase):
599+
def setUp(self):
600+
self.env = openravepy.Environment()
601+
602+
def tearDown(self):
603+
self.env.Destroy()
604+
605+
def test_GetPointFrom_Kinbody(self):
606+
# Check that each input type returns the correct xyz coord
607+
608+
# Kinbody
609+
self.env.Load('data/mug1.dae')
610+
mug = self.env.GetKinBody('mug')
611+
mug.SetTransform(numpy.eye(4))
612+
coord_kinbody = prpy.util.GetPointFrom(mug)
613+
expected_kinbody_coords = [0, 0, 0]
614+
numpy.testing.assert_array_almost_equal(coord_kinbody, expected_kinbody_coords)
615+
616+
def test_GetPointFrom_Space(self):
617+
# Check that each input type returns the correct xyz coord
618+
expected_coord = [1, 3, 4]
619+
620+
# Point in space
621+
space_coord = numpy.array([1, 3, 4])
622+
space_result = prpy.util.GetPointFrom(space_coord)
623+
numpy.testing.assert_array_almost_equal(space_result, expected_coord)
624+
625+
def test_GetPointFrom_Transform(self):
626+
# Check that each input type returns the correct xyz coord
627+
expected_coord = [1, 3, 4]
628+
629+
# 4x4 Transform
630+
trans_coord = numpy.eye(4)
631+
trans_coord[0:3, 3] = expected_coord
632+
trans_result = prpy.util.GetPointFrom(trans_coord)
633+
numpy.testing.assert_array_almost_equal(trans_result, expected_coord)
634+
635+
def test_GetPointFrom_List(self):
636+
# Check that each input type returns the correct xyz coord
637+
expected_coord = [1, 3, 4]
638+
639+
# List
640+
list_coord = [1, 3, 4]
641+
list_result = prpy.util.GetPointFrom(list_coord)
642+
numpy.testing.assert_array_almost_equal(list_result, expected_coord)
598643

599644
if __name__ == '__main__':
600645
unittest.main()

0 commit comments

Comments
 (0)