test_pointclouds.py
Go to the documentation of this file.
1 import unittest
2 import numpy as np
3 
4 import ros_numpy
5 from sensor_msgs.msg import PointCloud2, PointField, Image
6 from ros_numpy import numpy_msg
7 
8 class TestPointClouds(unittest.TestCase):
9  def makeArray(self, npoints):
10  points_arr = np.zeros((npoints,), dtype=[
11  ('x', np.float32),
12  ('y', np.float32),
13  ('z', np.float32),
14  ('r', np.uint8),
15  ('g', np.uint8),
16  ('b', np.uint8)])
17  points_arr['x'] = np.random.random((npoints,))
18  points_arr['y'] = np.random.random((npoints,))
19  points_arr['z'] = np.random.random((npoints,))
20  points_arr['r'] = np.floor(np.random.random((npoints,))*255)
21  points_arr['g'] = 0
22  points_arr['b'] = 255
23 
24  return points_arr
25 
26  def test_convert_dtype(self):
27  fields = [
28  PointField(name='x', offset=0, count=1, datatype=PointField.FLOAT32),
29  PointField(name='y', offset=4, count=1, datatype=PointField.FLOAT32)
30  ]
31  dtype = np.dtype([
32  ('x', np.float32),
33  ('y', np.float32)
34  ])
35  conv_fields = ros_numpy.msgify(PointField, dtype, plural=True)
36  self.assertSequenceEqual(fields, conv_fields, 'dtype->Pointfield Failed with simple values')
37 
38  conv_dtype = ros_numpy.numpify(fields, point_step=8)
39  self.assertSequenceEqual(dtype, conv_dtype, 'dtype->Pointfield Failed with simple values')
40 
42  fields = [
43  PointField(name='x', offset=0, count=1, datatype=PointField.FLOAT32),
44  PointField(name='y', offset=4, count=1, datatype=PointField.FLOAT32),
45  PointField(name='vectors', offset=8, count=3, datatype=PointField.FLOAT32)
46  ]
47 
48  dtype = np.dtype([
49  ('x', np.float32),
50  ('y', np.float32),
51  ('vectors', np.float32, (3,))
52  ])
53 
54  conv_fields = ros_numpy.msgify(PointField, dtype, plural=True)
55  self.assertSequenceEqual(fields, conv_fields, 'dtype->Pointfield with inner dimensions')
56 
57  conv_dtype = ros_numpy.numpify(fields, point_step=8)
58  self.assertEqual(dtype, conv_dtype, 'Pointfield->dtype with inner dimensions')
59 
60 
61  def test_roundtrip(self):
62 
63  points_arr = self.makeArray(100)
64  cloud_msg = ros_numpy.msgify(PointCloud2, points_arr)
65  new_points_arr = ros_numpy.numpify(cloud_msg)
66 
67  np.testing.assert_equal(points_arr, new_points_arr)
68 
70 
71  points_arr = self.makeArray(100)
72  cloud_msg = ros_numpy.msgify(numpy_msg(PointCloud2), points_arr)
73  new_points_arr = ros_numpy.numpify(cloud_msg)
74 
75  np.testing.assert_equal(points_arr, new_points_arr)
76 
77 if __name__ == '__main__':
78  unittest.main()
def numpy_msg(cls)
Definition: numpy_msg.py:11


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