Repository navigation
Expand file tree
/
Copy pathtests.py
More file actions
411 lines (334 loc) · 14.7 KB
/
Copy pathtests.py
File metadata and controls
411 lines (334 loc) · 14.7 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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)
def test_normals_encoding():
# Read reference mesh
with open(os.path.join(testdata_directory, "bunny.drc"), 'rb') as draco_file:
mesh = DracoPy.decode(draco_file.read())
# Create test normal vectors
test_normals = np.random.random((mesh.points.shape[0], 3))
# Encode with test normals
binary = DracoPy.encode(mesh.points, mesh.faces, normals=test_normals, preserve_order=True)
# Decode and verify normals
decoded_mesh = DracoPy.decode(binary)
assert np.allclose(decoded_mesh.normals, test_normals)
def test_mesh_custom_attribute_encoding():
# Read reference mesh
with open(os.path.join(testdata_directory, "bunny.drc"), 'rb') as draco_file:
mesh = DracoPy.decode(draco_file.read())
# Create test normal vectors
test_pressure = np.random.random(mesh.points.shape[0])
# Encode with test normals and custom attribute
custom_attributes = {
"pressure": test_pressure
}
binary = DracoPy.encode(
mesh.points,
mesh.faces,
preserve_order=True,
create_metadata=True,
custom_attributes=custom_attributes
)
# Decode and verify normals
decoded_mesh = DracoPy.decode(binary)
assert np.allclose(decoded_mesh.custom_attributes["pressure"], test_pressure)
def test_point_cloud_custom_attribute_encoding():
# Read reference mesh
with open(os.path.join(testdata_directory, "bunny.drc"), 'rb') as draco_file:
mesh = DracoPy.decode(draco_file.read())
# Create test normal vectors
test_pressure = np.random.random(mesh.points.shape[0])
# Encode with test normals and custom attribute
custom_attributes = {
"pressure": test_pressure
}
binary = DracoPy.encode(
mesh.points,
preserve_order=True,
create_metadata=True,
custom_attributes=custom_attributes
)
# Decode and verify normals
decoded_point_cloud = DracoPy.decode(binary)
assert np.allclose(decoded_point_cloud.custom_attributes["pressure"], test_pressure)
def test_string_metadata_encoding_decoding():
"""Test encoding and decoding with string metadata."""
# Read reference mesh
with open(os.path.join(testdata_directory, "bunny.drc"), 'rb') as draco_file:
mesh = DracoPy.decode(draco_file.read())
# Create test metadata
test_metadata = {
"name": "Stanford Bunny",
"author": "Stanford University",
"description": "Low-poly Stanford Bunny model with metadata",
"source": "Stanford 3D Scanning Repository",
"license": "Freely available for non-commercial use"
}
# Encode with metadata
binary = DracoPy.encode(
mesh.points,
mesh.faces,
create_metadata=True,
metadata=test_metadata
)
# Decode and verify metadata
decoded_mesh = DracoPy.decode(binary)
# Check if all metadata is present
for key, value in test_metadata.items():
assert key in decoded_mesh.metadata, f"Metadata key '{key}' not found"
assert decoded_mesh.metadata[key] == value, f"Metadata value for '{key}' doesn't match"
def test_string_array_metadata_encoding_decoding():
"""Test encoding and decoding with string array metadata."""
# Read reference mesh
with open(os.path.join(testdata_directory, "bunny.drc"), 'rb') as draco_file:
mesh = DracoPy.decode(draco_file.read())
# Create test string array metadata
test_metadata = {
"tags": ["bunny", "3d model", "test", "metadata"]
}
# Encode with metadata
binary = DracoPy.encode(
mesh.points,
mesh.faces,
create_metadata=True,
metadata=test_metadata
)
# Decode and verify metadata
decoded_mesh = DracoPy.decode(binary)
# Check if all metadata is present
for key, value in test_metadata.items():
assert key in decoded_mesh.metadata, f"Metadata key '{key}' not found"
assert decoded_mesh.metadata[key] == value, f"Metadata value for '{key}' doesn't match"
def test_numeric_array_metadata_encoding_decoding():
"""Test encoding and decoding with numeric array metadata."""
# Read reference mesh
with open(os.path.join(testdata_directory, "bunny.drc"), 'rb') as draco_file:
mesh = DracoPy.decode(draco_file.read())
# Create test numeric metadata
test_metadata = {
"bounding_box": np.array([-1.0, -1.0, -1.0, 1.0, 1.0, 1.0], dtype=np.float64),
"vertex_weights": np.random.random(mesh.points.shape[0]).astype(np.float32),
"face_groups": np.random.randint(0, 5, mesh.faces.shape[0], dtype=np.int32),
"importance_values": np.array([0.1, 0.5, 0.9, 1.0], dtype=np.float64)
}
# Encode with metadata
binary = DracoPy.encode(
mesh.points,
mesh.faces,
create_metadata=True,
metadata=test_metadata
)
# Decode and verify metadata
decoded_mesh = DracoPy.decode(binary)
# Check if all metadata is present and correct
for key, value in test_metadata.items():
assert key in decoded_mesh.metadata, f"Metadata key '{key}' not found"
np.testing.assert_allclose(
decoded_mesh.metadata[key],
value.flatten() if value.ndim > 1 else value,
rtol=1e-5,
err_msg=f"Metadata array '{key}' doesn't match original"
)