Skip to content

Commit

Permalink
preserve order if possible in wavefront
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Oct 31, 2018
1 parent 4e12d6a commit e64fc9d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
21 changes: 21 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ def test_obj(self):
assert m.metadata['vertex_texture'].shape == reconstructed.metadata[
'vertex_texture'].shape

def test_obj_order(self):
"""
Make sure simple round trips through Wavefront don't
reorder vertices.
"""
# get a writeable temp file location
temp = g.tempfile.NamedTemporaryFile(
suffix='.obj',
delete=False)
temp.close()

# simple solid
x = g.trimesh.creation.icosahedron()
x.export(temp.name)
y = g.trimesh.load_mesh(temp.name, process=False)

# vertices should be same shape and order
assert g.np.allclose(x.vertices, y.vertices)
# faces should be same
assert g.np.allclose(x.faces, y.faces)

def test_ply(self):
m = g.get_mesh('machinist.XAML')

Expand Down
15 changes: 14 additions & 1 deletion trimesh/io/wavefront.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,21 @@ def append_mesh():
# much as possible by sorting by remap key
keys, values = (np.array(list(remap.keys())),
np.array(list(remap.values())))

try:
# if we sort keys as strings they will be an
# ordering like (1/1/1, 10/10/10) vs (1/1/1, 2/2/2)
# so try to convert to int before sorting
split = np.array([i.split('/')[0] for i in keys],
dtype=np.int)
order = split.argsort()
except BaseException:
# we can still use arbitrary order as a fallback
order = keys.argsort()

# new order of vertices
vert_order = values[keys.argsort()]
vert_order = values[order]

# we need to mask to preserve index relationship
# between faces and vertices
face_order = np.zeros(len(vertices),
Expand Down
2 changes: 1 addition & 1 deletion trimesh/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.35.14'
__version__ = '2.35.15'

0 comments on commit e64fc9d

Please sign in to comment.