test_geometry.py
Go to the documentation of this file.
1 import unittest
2 import numpy as np
3 import ros_numpy
4 
5 from tf import transformations
6 
7 from geometry_msgs.msg import Vector3, Quaternion, Transform, Point, Pose
8 
9 class TestGeometry(unittest.TestCase):
10  def test_point(self):
11  p = Point(1, 2, 3)
12 
13  p_arr = ros_numpy.numpify(p)
14  np.testing.assert_array_equal(p_arr, [1, 2, 3])
15 
16  p_arrh = ros_numpy.numpify(p, hom=True)
17  np.testing.assert_array_equal(p_arrh, [1, 2, 3, 1])
18 
19  self.assertEqual(p, ros_numpy.msgify(Point, p_arr))
20  self.assertEqual(p, ros_numpy.msgify(Point, p_arrh))
21  self.assertEqual(p, ros_numpy.msgify(Point, p_arrh * 2))
22 
23  def test_vector3(self):
24  v = Vector3(1, 2, 3)
25 
26  v_arr = ros_numpy.numpify(v)
27  np.testing.assert_array_equal(v_arr, [1, 2, 3])
28 
29  v_arrh = ros_numpy.numpify(v, hom=True)
30  np.testing.assert_array_equal(v_arrh, [1, 2, 3, 0])
31 
32  self.assertEqual(v, ros_numpy.msgify(Vector3, v_arr))
33  self.assertEqual(v, ros_numpy.msgify(Vector3, v_arrh))
34 
35  with self.assertRaises(AssertionError):
36  ros_numpy.msgify(Vector3, np.array([0, 0, 0, 1]))
37 
38  def test_transform(self):
39  t = Transform(
40  translation=Vector3(1, 2, 3),
41  rotation=Quaternion(*transformations.quaternion_from_euler(np.pi, 0, 0))
42  )
43 
44  t_mat = ros_numpy.numpify(t)
45 
46  np.testing.assert_allclose(t_mat.dot([0, 0, 1, 1]), [1.0, 2.0, 2.0, 1.0])
47 
48  msg = ros_numpy.msgify(Transform, t_mat)
49 
50  np.testing.assert_allclose(msg.translation.x, t.translation.x)
51  np.testing.assert_allclose(msg.translation.y, t.translation.y)
52  np.testing.assert_allclose(msg.translation.z, t.translation.z)
53  np.testing.assert_allclose(msg.rotation.x, t.rotation.x)
54  np.testing.assert_allclose(msg.rotation.y, t.rotation.y)
55  np.testing.assert_allclose(msg.rotation.z, t.rotation.z)
56  np.testing.assert_allclose(msg.rotation.w, t.rotation.w)
57 
58  def test_pose(self):
59  t = Pose(
60  position=Point(1.0, 2.0, 3.0),
61  orientation=Quaternion(*transformations.quaternion_from_euler(np.pi, 0, 0))
62  )
63 
64  t_mat = ros_numpy.numpify(t)
65 
66  np.testing.assert_allclose(t_mat.dot([0, 0, 1, 1]), [1.0, 2.0, 2.0, 1.0])
67 
68  msg = ros_numpy.msgify(Pose, t_mat)
69 
70  np.testing.assert_allclose(msg.position.x, t.position.x)
71  np.testing.assert_allclose(msg.position.y, t.position.y)
72  np.testing.assert_allclose(msg.position.z, t.position.z)
73  np.testing.assert_allclose(msg.orientation.x, t.orientation.x)
74  np.testing.assert_allclose(msg.orientation.y, t.orientation.y)
75  np.testing.assert_allclose(msg.orientation.z, t.orientation.z)
76  np.testing.assert_allclose(msg.orientation.w, t.orientation.w)
77 
78 if __name__ == '__main__':
79  unittest.main()


ros_numpy
Author(s): Eric Wieser
autogenerated on Sat Oct 3 2020 03:25:57