-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtests.py
249 lines (206 loc) · 9.42 KB
/
tests.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import DracoPy
import pytest
import numpy as np
testdata_directory = "testdata_files"
EXPECTED_POINTS_BUNNY_MESH = 104502 // 3
EXPECTED_POINTS_BUNNY_PTC = 107841 // 3
EXPECTED_FACES_BUNNY = 208353 // 3
def test_decoding_and_encoding_mesh_file():
with open(os.path.join(testdata_directory, "bunny.drc"), "rb") as draco_file:
mesh = DracoPy.decode(draco_file.read())
assert len(mesh.points) == EXPECTED_POINTS_BUNNY_MESH
assert len(mesh.faces) == EXPECTED_FACES_BUNNY
assert len(mesh.normals) == 0
with open(os.path.join(testdata_directory, "bunny_normals.drc"), "rb") as draco_file:
mesh = DracoPy.decode(draco_file.read())
assert len(mesh.points) == EXPECTED_POINTS_BUNNY_MESH
assert len(mesh.faces) == EXPECTED_FACES_BUNNY
assert len(mesh.normals) == EXPECTED_POINTS_BUNNY_MESH
encoding_test1 = DracoPy.encode(mesh.points, mesh.faces)
encoding_test2 = DracoPy.encode(mesh.points.flatten(), mesh.faces)
assert encoding_test1 == encoding_test2
encoding_test = encoding_test1
# test preserve_order
np.random.shuffle(mesh.faces)
encoding_test3 = DracoPy.encode(mesh.points, mesh.faces, compression_level=10,
preserve_order=True)
mesh_decode = DracoPy.decode(encoding_test3)
assert np.allclose(mesh.points, mesh_decode.points)
assert np.allclose(mesh.faces, mesh_decode.faces)
# color is None
assert mesh_decode.colors is None, "colors should not present"
colors = np.random.randint(0, 255, [mesh.points.shape[0], 16]).astype(np.uint8)
tex_coord = np.random.random([mesh.points.shape[0], 2]).astype(float)
# test extreme quantization
encoding_test4 = DracoPy.encode(mesh.points, mesh.faces, compression_level=10,
quantization_bits=1, colors=colors,
preserve_order=True)
mesh_decode = DracoPy.decode(encoding_test4)
assert np.array_equal(colors, mesh_decode.colors), "colors decode result is wrong"
# Setting quantization_bits 26 here. Larger value causes MemoryError on 32bit systems.
encoding_test5 = DracoPy.encode(mesh.points, mesh.faces, compression_level=1,
quantization_bits=26, colors=colors, tex_coord=tex_coord)
mesh_decode = DracoPy.decode(encoding_test5)
#this is very slow
# from tqdm import tqdm
# ptmap = {}
# for pt in tqdm(mesh.points):
# for i in range(len(mesh.points)):
# if np.all(np.isclose(pt, mesh.points[i])):
# ptmap[tuple(pt)] = i
# break
# for i, pt in enumerate(mesh_decode.points):
# pt = tuple(pt)
# assert np.all(np.isclose(mesh.tex_coord[ptmap[pt]], mesh_decode.tex_coord[i]))
assert mesh_decode.colors is not None, "colors should present"
assert mesh_decode.tex_coord is not None, "tex_coord should present"
with open(os.path.join(testdata_directory, "bunny_test.drc"), "wb") as test_file:
test_file.write(encoding_test)
with open(os.path.join(testdata_directory, "bunny_test.drc"), "rb") as test_file:
mesh = DracoPy.decode(test_file.read())
assert (mesh.encoding_options) is None
assert len(mesh.points) == EXPECTED_POINTS_BUNNY_MESH
assert len(mesh.faces) == EXPECTED_FACES_BUNNY
with pytest.raises(AssertionError):
invalid_c = np.random.randint(0, 255, [mesh.points.shape[0], 128]).astype(np.uint8)
invalid_m = DracoPy.encode(mesh.points, mesh.faces, compression_level=1,
quantization_bits=26, colors=invalid_c)
def test_tex_coord_encoding():
points = np.array([
[0,0,0],
[1,0,0],
[1,1,0],
[0,1,0],
])
faces = np.array([[0,1,2], [0,3,2]])
# tex_coord = np.random.random([len(points), 2]).astype(float)
tex_coord = np.array([
[0.0, 0.1],
[0.2, 0.3],
[0.4, 0.5],
[0.6, 0.7],
])
encoded = DracoPy.encode(
points, faces,
compression_level=1,
quantization_bits=26,
# colors=colors,
tex_coord=tex_coord
)
mesh = DracoPy.decode(encoded)
ptmap = {}
for pt in mesh.points:
for i in range(len(points)):
if np.all(pt == points[i]):
ptmap[tuple(pt)] = i
break
for i, pt in enumerate(mesh.points):
pt = tuple(pt)
assert np.all(np.isclose(tex_coord[ptmap[pt]], mesh.tex_coord[i]))
def test_decoding_and_encoding_mesh_file_integer_positions():
with open(os.path.join(testdata_directory, "bunny.drc"), "rb") as draco_file:
file_content = draco_file.read()
mesh_object = DracoPy.decode(file_content)
assert len(mesh_object.points) == EXPECTED_POINTS_BUNNY_MESH
assert len(mesh_object.faces) == EXPECTED_FACES_BUNNY
points = np.array(mesh_object.points)
points = points.astype(np.float64)
points -= np.min(points, axis=0)
points /= np.max(points, axis=0)
points *= 2 ** 16
points = points.astype(np.uint32)
encoding_test_uint = DracoPy.encode(
points, mesh_object.faces,
quantization_bits=16,
)
encoding_test_int = DracoPy.encode(
points.astype(np.int64), mesh_object.faces,
quantization_bits=16,
)
encoding_test_float = DracoPy.encode(
points.astype(np.float32), mesh_object.faces,
quantization_bits=16,
)
assert encoding_test_uint != encoding_test_float
assert encoding_test_uint != encoding_test_int
assert encoding_test_int != encoding_test_float
encoding_test_uint64 = DracoPy.encode(
points.astype(np.uint64), mesh_object.faces,
quantization_bits=16,
)
assert encoding_test_uint == encoding_test_uint64
mesh_object = DracoPy.decode(encoding_test_uint)
assert len(mesh_object.points) == EXPECTED_POINTS_BUNNY_MESH
assert len(mesh_object.faces) == EXPECTED_FACES_BUNNY
pts_f = np.array(mesh_object.points)
pts_f = np.sort(pts_f, axis=0)
pts_i = np.sort(np.copy(points), axis=0)
assert np.all(np.isclose(pts_i, pts_f))
def test_decoding_improper_file():
with open(os.path.join(testdata_directory, "bunny.obj"), "rb") as improper_file:
file_content = improper_file.read()
with pytest.raises(DracoPy.FileTypeException):
DracoPy.decode(file_content)
def test_metadata():
with open(os.path.join(testdata_directory, "bunny.drc"), "rb") as draco_file:
file_content = draco_file.read()
mesh_object = DracoPy.decode(file_content)
encoding_options = {
"quantization_bits": 12,
"compression_level": 3,
"quantization_range": 1000,
"quantization_origin": [-100, -100, -100],
"create_metadata": True,
}
encoding_test = DracoPy.encode(
mesh_object.points, mesh_object.faces, **encoding_options
)
with open(
os.path.join(testdata_directory, "bunny_test.drc"), "wb"
) as test_file:
test_file.write(encoding_test)
with open(os.path.join(testdata_directory, "bunny_test.drc"), "rb") as test_file:
file_content = test_file.read()
mesh_object = DracoPy.decode(file_content)
eo = mesh_object.encoding_options
assert (eo) is not None
assert (eo.quantization_bits) == 12
assert (eo.quantization_range) == 1000
assert (eo.quantization_origin) == [-100, -100, -100]
def test_decoding_and_encoding_point_cloud_file():
with open(
os.path.join(testdata_directory, "point_cloud_bunny.drc"), "rb"
) as draco_file:
file_content = draco_file.read()
point_cloud_object = DracoPy.decode(file_content)
assert len(point_cloud_object.points) == EXPECTED_POINTS_BUNNY_PTC
encoding_test = DracoPy.encode(point_cloud_object.points)
with open(
os.path.join(testdata_directory, "point_cloud_bunny_test.drc"), "wb"
) as test_file:
test_file.write(encoding_test)
# test preserve_order
np.random.shuffle(point_cloud_object.points)
colors = np.random.randint(0, 255, [point_cloud_object.points.shape[0], 127]).astype(np.uint8)
encoding_test2 = DracoPy.encode(point_cloud_object.points, compression_level=10,
quantization_bits=26, preserve_order=True, colors=colors)
ptc_decode = DracoPy.decode(encoding_test2)
assert np.allclose(point_cloud_object.points, ptc_decode.points)
assert np.array_equal(colors, ptc_decode.colors)
# test extreme quantization
encoding_test3 = DracoPy.encode(point_cloud_object.points, compression_level=10,
quantization_bits=1)
ptc_decode = DracoPy.decode(encoding_test3)
assert ptc_decode.colors is None, "colors should not present"
with open(
os.path.join(testdata_directory, "point_cloud_bunny_test.drc"), "rb"
) as test_file:
file_content = test_file.read()
point_cloud_object = DracoPy.decode(file_content)
assert (point_cloud_object.encoding_options) is None
assert len(point_cloud_object.points) == EXPECTED_POINTS_BUNNY_PTC
with pytest.raises(AssertionError):
invalid_c = np.random.randint(0, 255, [point_cloud_object.points.shape[0], 128]).astype(np.uint8)
invalid_m = DracoPy.encode(point_cloud_object.points, compression_level=1,
quantization_bits=26, colors=invalid_c)