-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_pointcloud.py
224 lines (190 loc) · 8.92 KB
/
test_pointcloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pcl
import numpy as np
import unittest
class TestPointCloud(unittest.TestCase):
def test_normal_init(self):
cloud_array = np.array([[1,2,3],[2,3,4]], dtype='f4')
with self.assertRaises(ValueError):
cloud = pcl.PointCloud(cloud_array)
# add padding zeros after points
cloud_array_pad = np.insert(cloud_array, 3, 0, axis=1)
cloud = pcl.PointCloud(cloud_array_pad)
assert len(cloud) == 2
assert np.allclose(cloud.xyz, cloud_array)
assert len(cloud.fields) == 3
assert cloud.names == ['x', 'y', 'z']
def test_empty_init(self):
cloud = pcl.PointCloud()
assert len(cloud) == 0
assert cloud.ptype == 'XYZ'
assert len(cloud.xyz) == 0
assert cloud.names == ['x', 'y', 'z']
cloud = pcl.PointCloud(point_type='xyzi')
assert len(cloud) == 0
assert cloud.ptype == 'XYZI'
assert len(cloud.xyz) == 0
cloud.names == ['x', 'y', 'z', 'i']
def test_list_init(self):
cloud = pcl.PointCloud([(1,2,3),(2,3,4)], 'xyz')
assert len(cloud) == 2
assert np.allclose(cloud.xyz, [[1,2,3],[2,3,4]])
assert cloud.names == ['x', 'y', 'z']
def test_struct_init(self):
cloud_array = np.array([(1,2,3),(2,3,4)],
dtype={'names':['x','y','z'], 'formats':['f4','f4','f4'], 'itemsize':16})
cloud = pcl.PointCloud(cloud_array)
assert len(cloud) == 2
assert np.allclose(cloud.xyz, [[1,2,3],[2,3,4]])
assert cloud.names == ['x', 'y', 'z']
cloud = pcl.PointCloud(np.array([(1,2,3,5),(2,3,4,5)],
dtype={'names':['x','y','z','i'], 'formats':['f4','f4','f4','i1'], 'offsets':[0,4,8,16]}))
assert len(cloud) == 2
assert np.allclose(cloud.xyz, [[1,2,3],[2,3,4]])
assert cloud.names == ['x', 'y', 'z', 'i']
def test_copy_init(self):
cloud_array = np.array([[1,2,3],[2,3,4]], dtype='f4')
cloud = pcl.PointCloud([(1,2,3),(2,3,4)], 'xyz')
copy = pcl.PointCloud(cloud)
assert np.all(copy.xyz == cloud_array)
assert copy == cloud
cloud.xyz[0,0] = 0
assert np.any(cloud.xyz != cloud_array)
assert np.all(copy.xyz == cloud_array)
def test_rgb_type(self):
with self.assertRaises(ValueError):
cloud = pcl.PointCloud([(1,2,3,255),(2,3,4,255)], 'xyzrgb')
def test_origin(self):
cloud = pcl.PointCloud([(1,2,3),(2,3,4)])
assert np.all(cloud.sensor_origin == np.zeros(3))
cloud.sensor_origin = [1, 2, 3]
assert np.all(cloud.sensor_origin == np.array([1,2,3]))
def test_orientation(self):
cloud = pcl.PointCloud([(1,2,3),(2,3,4)])
assert np.all(cloud.sensor_orientation == np.array([0,0,0,1]))
cloud.sensor_orientation = [1, 2, 3, 1]
assert np.all(cloud.sensor_orientation == np.array([1,2,3,1]))
def test_field_operations(self):
cloud = pcl.PointCloud([(1,2,3),(2,3,4)])
new_fields = np.array([(4,5),(6,7)], dtype=[('f1','f4'),('f2','f8')])
cloud = cloud.append_fields(new_fields)
assert cloud.names == ['x', 'y', 'z', 'f1', 'f2']
assert len(cloud.to_ndarray()) == 2
assert np.all(cloud.xyz == np.array([(1,2,3),(2,3,4)]))
cloud = pcl.PointCloud([(1,2,3),(2,3,4)])
cloud = cloud.append_fields(f1=np.array([4,5], dtype='f4'),
f2=np.array([6,7], dtype='f4'))
assert 'f1' in cloud.names
assert 'f2' in cloud.names
assert len(cloud.to_ndarray()) == 2
assert np.all(cloud.xyz == np.array([(1,2,3),(2,3,4)]))
cloud = pcl.PointCloud([(1,2,3),(2,3,4)])
new_fields = np.array([(4,5),(6,7)], dtype=[('f1','f4'),('f2','f8')])
cloud = cloud.insert_fields(new_fields, [1,1])
assert cloud.names == ['x', 'f2', 'f1', 'y', 'z']
assert len(cloud.to_ndarray()) == 2
with self.assertRaises(ValueError):
_ = cloud.xyz
def test_creators(self):
# RGB points actually contains the rgba field
cloud = pcl.create_rgb([[10, 20, 30, 30], [40, 50, 60, 60]])
assert np.all(cloud.argb == np.array([[30, 10, 20, 30], [60, 40, 50, 60]]))
assert cloud.ptype == "RGB"
cloud = pcl.create_xyz([[1,2,3], [4,5,6]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert cloud.ptype == "XYZ"
cloud = pcl.create_xyzi([[1,2,3,1], [4,5,6,4]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert cloud.ptype == "XYZI"
cloud = pcl.create_xyzl([[1,2,3,1], [4,5,6,4]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert cloud.ptype == "XYZL"
cloud = pcl.create_xyzrgb([[1,2,3,1,2,3], [4,5,6,4,5,6]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert cloud.ptype == "XYZRGB"
cloud = pcl.create_xyzrgba([[1,2,3,1,2,3,1], [4,5,6,4,5,6,4]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert cloud.ptype == "XYZRGBA"
cloud = pcl.create_xyzrgbl([[1,2,3,1,2,3,1], [4,5,6,4,5,6,4]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert cloud.ptype == "XYZRGBL"
cloud = pcl.create_normal([[1,2,3,3], [4,5,6,6]])
assert np.all(cloud.normal == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud['curvature'] == [3, 6])
assert cloud.ptype == "NORMAL"
cloud = pcl.create_normal([[1,2,3], [4,5,6]])
assert np.all(cloud.normal == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud['curvature'] == 0)
assert cloud.ptype == "NORMAL"
cloud = pcl.create_xyzn([[1,2,3,1,2,3,1], [4,5,6,4,5,6,4]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud.normal == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud['curvature'] == [1, 4])
assert cloud.ptype == "XYZN"
cloud = pcl.create_xyzn([[1,2,3,1,2,3], [4,5,6,4,5,6]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud.normal == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud['curvature'] == 0)
assert cloud.ptype == "XYZN"
cloud = pcl.create_xyzrgbn([[1,2,3,1,2,3,1,2,3,1], [4,5,6,4,5,6,4,5,6,4]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud.normal == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud['curvature'] == [1, 4])
assert cloud.ptype == "XYZRGBN"
cloud = pcl.create_xyzrgbn([[1,2,3,1,2,3,1,2,3], [4,5,6,4,5,6,4,5,6]])
assert np.all(cloud.xyz == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud.normal == np.array([[1,2,3], [4,5,6]]))
assert np.all(cloud['curvature'] == 0)
assert cloud.ptype == "XYZRGBN"
def test_infer_ptype(self):
c1 = pcl.create_xyz(np.random.rand(10, 3))
c2 = pcl.create_normal(np.random.rand(10, 3))
c3 = c1.append_fields(c2)
c3.infer_ptype()
assert c3.ptype == 'XYZN'
def test_predefined_accessors(self):
cloud = pcl.create_xyzrgbn([[1,2,3,1,2,3,1,2,3,1], [4,5,6,4,5,6,4,5,6,4]])
assert np.all(cloud.xyz == [[1,2,3], [4,5,6]])
assert np.all(cloud.normal == [[1,2,3], [4,5,6]])
assert np.all(cloud.rgb == [[1,2,3], [4,5,6]])
cloud.xyz = [[2,3,4], [2,3,4]]
assert(np.all(cloud.xyz == [[2,3,4], [2,3,4]]))
cloud.normal = [[3,4,5], [3,4,5]]
assert(np.all(cloud.normal == [[3,4,5], [3,4,5]]))
cloud.rgb = [[0,1,2], [0,1,2]]
assert(np.all(cloud.rgb == [[0,1,2], [0,1,2]]))
cloud = pcl.create_rgb([[1,2,3,4], [1,2,3,4]])
assert np.all(cloud.argb == [[4,1,2,3], [4,1,2,3]])
cloud.argb = [[1,2,3,4], [1,2,3,4]]
assert np.all(cloud.argb == [[1,2,3,4], [1,2,3,4]])
def test_indexing(self):
cloud = pcl.create_xyz(np.random.rand(10, 3))
# test getitem
subcloud = cloud[0]
assert isinstance(subcloud, pcl.PointCloud)
assert len(subcloud) == 1
subcloud = cloud[:2]
assert isinstance(subcloud, pcl.PointCloud)
assert len(subcloud) == 2
subcloud = cloud[[1,2]]
assert isinstance(subcloud, pcl.PointCloud)
assert len(subcloud) == 2
subcloud = cloud[np.array([1,2])]
assert isinstance(subcloud, pcl.PointCloud)
assert len(subcloud) == 2
subcloud = cloud['x']
assert isinstance(subcloud, np.ndarray)
assert len(subcloud) == 10
subcloud = cloud[['x', 'y']]
assert isinstance(subcloud, np.ndarray)
assert len(subcloud) == 10
# test set
cloud[0] = (1,2,3)
assert cloud[0]['x'] == 1.
cloud[:2] = [(2,3,4),(3,4,5)]
assert cloud[0]['x'] == 2.
assert cloud[1]['x'] == 3.
cloud[[1,2]] = [(4,5,6),(5,6,7)]
assert cloud[1]['x'] == 4.
assert cloud[2]['x'] == 5.
cloud['x'] = np.arange(10)
assert cloud[0]['x'] == 0