-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtests.py
More file actions
235 lines (191 loc) · 7.13 KB
/
tests.py
File metadata and controls
235 lines (191 loc) · 7.13 KB
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
225
226
227
228
229
230
231
232
233
234
235
import unittest
import math
import numpy as np
import math3d as m3d
from IPython import embed
import hcn
try:
import vtk_visualizer as vv
except:
pass
class TestsHTuple(unittest.TestCase):
def test_tuple_double(self):
val = 0.199
d = hcn.HTuple(val)
self.assertEqual(d.length(), 1)
a = np.array([val])
self.assertEqual(d.to_array(), a)
self.assertEqual(d[0], val)
self.assertEqual(d.type(), hcn.TupleType.Double)
def test_tuple_int(self):
val = 7
d = hcn.HTuple(val)
self.assertEqual(d.length(), 1)
a = np.array([val])
self.assertEqual(d.to_array(), a)
self.assertEqual(d[0], val)
self.assertEqual(d.type(), hcn.TupleType.Int)
def test_tuple_bytes(self):
val = b"toto is None"
d = hcn.HTuple(val)
self.assertEqual(d.length(), 1)
a = [val]
self.assertEqual(d.to_list(), a)
self.assertEqual(d[0], val)
self.assertEqual(d.type(), hcn.TupleType.String)
def test_to_from_bytes(self):
l = ["1", "2", "3", "b"]
def test_append(self):
val = 0.199
d = hcn.HTuple(val)
d.append(3.4)
self.assertEqual(d.length(), 2)
def test_array_double(self):
a = np.array([1.1, 2.1, 3.1, 4.1, 5.1], dtype=np.double)
t = hcn.HTuple.from_array(a)
self.assertEqual(t.length(), 5)
self.assertEqual(t[0], a[0])
self.assertEqual(t[4], t[4])
def test_array_int(self):
a = np.array([1, 2, 3, 4, 5], dtype=np.int)
t = hcn.HTuple.from_array(a)
self.assertEqual(t.length(), 5)
self.assertEqual(t[0], t[0])
self.assertEqual(t[4], t[4])
def test_mixed(self):
l = [1, 2.5, b"jkl", "oøl"]
tup = hcn.HTuple.from_list(l)
self.assertEqual(tup.length(), 4)
self.assertEqual(tup[0], 1)
self.assertEqual(tup[2], b"jkl")
with self.assertRaises(ValueError):
print(tup[5])
class TestsHPose(unittest.TestCase):
def test_empty(self):
p = hcn.HPose()
def test_transform(self):
p = hcn.HPose()
t = m3d.Transform()
t.pos = m3d.Vector(2, 3, 1)
t.orient.rotate_zb(math.pi / 2)
p = hcn.HPose(*t.pose_vector)
t2 = m3d.Transform(p.to_list()[:-1])
self.assertEqual(t, t2)
class TestsModel3D(unittest.TestCase):
def test_read_model(self):
m = hcn.Model3D.from_file("simple.obj", "m")
ar = m.to_array()
np.testing.assert_array_equal(ar[0], np.array([1, 1, 1]))
np.testing.assert_array_equal(ar[3], np.array([1, 1, 2]))
def test_to_from_array_model(self):
ar = np.array([[0, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0]], dtype=np.double)
m = hcn.Model3D.from_array(ar)
new = m.to_array()
np.testing.assert_array_equal(ar, new)
def test_bounding_box(self):
print("A")
m = self._get_simple_model()
print("B")
m.get_bounding_box()
#print("BOUND", m.get_bounding_box())
#FIXME: check result
def test_convex_hull(self):
m = self._get_simple_model()
ch = m.get_convex_hull()
def test_select(self):
m = self._get_simple_model()
m = m.select_x(0, 1)
ar = m.to_array()
self.assertTrue(max(ar[:, 0]) <= 1)
def test_create_surface(self):
m = self._get_simple_model()
#s = m.create_surface_model(0.1)
def test_sample(self):
m = self._get_simple_model()
new = m.sampled("fast", 2)
self.assertEqual(len(new.to_array()), 2)
def test_smooth(self):
m = self._get_simple_model()
new = m.smoothed(knn=200)
#self.assertEqual(len(new.to_array()), 2)
def test_exception(self):
m = self._get_simple_model()
with self.assertRaises(RuntimeError):
m.to_file("obj", "/t.obj")
def _get_simple_model(self):
ar = np.array([[0, 0, 0], [1, 0, 0], [2, 0, 0.1], [3, 0, -0.1]], dtype=np.double)
return hcn.Model3D.from_array(ar)
def test_box(self):
p = hcn.HPose(1, 2, 3)
b = hcn.Box(p, 1, 2, 0.5)
n = b.sampled("fast", 0.01)
#embed()
def test_plane(self):
pose = hcn.HPose(0, 0, 2)
p = hcn.Plane(pose, [1, 0, 0], [0, 1, 0])
n = p.sampled("fast", 0.1)
def test_sphere(self):
s = hcn.Sphere(1, 2, 3, 2)
n = s.sampled("fast", 0.1)
def test_normals(self):
m = hcn.Model3D.from_file("simple.obj", "m")
#print("F", m.normals_to_array())
m.compute_normals(60, 2)
#print("E", m.normals_to_array())
# FIXME
def test_localize(self):
box = hcn.Box(hcn.HPose(0.1, 0.2, -0.05), 0.1, 0.2, 0.3)
# make a scene and sample it
trans = m3d.Transform((0.2, 0, 0, 0, 0, math.pi / 2))
scene = box.transformed(hcn.HPose(trans))
scene = scene.sampled("fast_compute_normals", 0.01)
#scene = scene.select_z(0, 1) # FIXME: does not work
# sample our box to something different
box = box.sampled("fast_compute_normals", 0.01)
surf = box.create_surface_model(0.02)
poses, score = surf.find_surface_model(scene, 0.001, 0.2, min_score=0, params={"num_matches":1})
if poses:
tr = box.transformed(poses[0])
self.assertGreater(len(poses), 1)
def test_segment(self):
box = hcn.Box(hcn.HPose(0.1, 0.2, -0.05), 0.1, 0.2, 0.3)
box = box.sampled("fast_compute_normals", 0.001)
#box = hcn.Model3D.from_file("KA/punktskyer/ka1.ply", "mm", params={"xyz_map_width":2024})
#box.prepare("segmentation")
seg = box.segment2({"fitting":"true", "primitive_type":"plane", "fitting_algorithm":"least_squares"})
#seg = box.segment({"primitive_type":"plane"})
#seg = box.segment2({"fitting":"false", "output_xyz_mapping":"false"})
#seg = box.segment()
s = seg[0]
#embed()
def test_fit(self):
mod = hcn.Sphere(0.1, 0.2, 0.3, 0.1)
mod = mod.sampled("fast", 0.01)
results = mod.fit_primitive({"primitive_type":"all"})
res = results[0]
ptype = res.get_attribute("primitive_type").to_list()
#embed()
self.assertEqual(ptype[0], b"sphere")
def test_distance(self):
box = hcn.Box(hcn.HPose(0.1, 0.2, -0.05), 0.1, 0.2, 0.3)
box = box.sampled("fast", 0.1)
sphere = hcn.Sphere(0.1, 0.2, -0.05, 0.2)
sphere = sphere.sampled("fast", 0.1)
box.distance(sphere)
dists = box.get_attribute("&distance")
self.assertEqual(box.to_array().shape[0], dists.length())
class TestsImage(unittest.TestCase):
def test_open(self):
im = hcn.Image('tests/image.jpg')
self.assertIsInstance(im.width, int)
self.assertIsInstance(im.height, int)
#im.height())
def test_crop(self):
im = hcn.Image('tests/image.jpg')
crp = im.croped(50, 1030, 1130, 1330)
crp.write("toto.jpg")
crp.write("toto.tiff")
self.assertEqual(crp.width, 1330 - 1030 + 1)
self.assertEqual(crp.height, 1130 - 50 + 1)
if __name__ == "__main__":
unittest.main(verbosity=3)