Skip to content

Commit

Permalink
Merge pull request #2150 from moberweger/fix-ply-binary
Browse files Browse the repository at this point in the history
fix binary ply export and extend unittest
  • Loading branch information
mikedh authored Feb 14, 2024
2 parents 4f14bd7 + ff8f6e7 commit 8c3cf07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
42 changes: 24 additions & 18 deletions tests/test_ply.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,30 @@ def test_face_attributes(self):
# Test writing face attributes to a ply, by reading
# them back and asserting the written attributes array matches

m = g.get_mesh("box.STL")
test_1d_attribute = g.np.copy(m.face_angles[:, 0])
test_nd_attribute = g.np.copy(m.face_angles)
m.face_attributes["test_1d_attribute"] = test_1d_attribute
m.face_attributes["test_nd_attribute"] = test_nd_attribute

export = m.export(file_type="ply")
reconstructed = g.roundtrip(export, file_type="ply")

face_attributes = reconstructed.metadata["_ply_raw"]["face"]["data"]
result_1d = face_attributes["test_1d_attribute"]
result_nd = face_attributes["test_nd_attribute"]["f1"]

g.np.testing.assert_almost_equal(result_1d, test_1d_attribute)
g.np.testing.assert_almost_equal(result_nd, test_nd_attribute)

no_attr = m.export(file_type="ply", include_attributes=False)
assert len(no_attr) < len(export)
for encoding in ["binary", "ascii"]:
for dt in [g.np.float32, g.np.float64]:
m = g.get_mesh("box.STL")
test_1d_attribute = g.np.copy(m.face_angles[:, 0])
test_nd_attribute = g.np.copy(m.face_angles)
m.face_attributes["test_1d_attribute"] = test_1d_attribute.astype(dt)
m.face_attributes["test_nd_attribute"] = test_nd_attribute.astype(dt)

export = m.export(file_type="ply", include_attributes=True, encoding=encoding)
reconstructed = g.roundtrip(export, file_type="ply", process=False)

face_attributes = reconstructed.metadata["_ply_raw"]["face"]["data"]
result_1d = face_attributes["test_1d_attribute"]
if encoding == "binary":
# only binary format allows this
result_nd = face_attributes["test_nd_attribute"]["f1"]
else:
result_nd = face_attributes["test_nd_attribute"]

g.np.testing.assert_almost_equal(result_1d, test_1d_attribute)
g.np.testing.assert_almost_equal(result_nd, test_nd_attribute)

no_attr = m.export(file_type="ply", include_attributes=False)
assert len(no_attr) < len(export)

def test_cases(self):
a = g.get_mesh("featuretype.STL")
Expand Down
4 changes: 2 additions & 2 deletions trimesh/exchange/ply.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def _add_attributes_to_dtype(dtype, attributes):
dtype.append((name, data.dtype))
else:
attribute_dtype = data.dtype if len(data.dtype) == 0 else data.dtype[0]
dtype.append((f"{name}_count", "u1"))
dtype.append((name, _numpy_type_to_ply_type(attribute_dtype), data.shape[1]))
dtype.append((f"{name}_count", "<u1"))
dtype.append((name, attribute_dtype, data.shape[1]))
return dtype


Expand Down

0 comments on commit 8c3cf07

Please sign in to comment.