Skip to content

Commit

Permalink
add exception closure for error message
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Mar 16, 2022
1 parent a41d03e commit d940c2f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
8 changes: 6 additions & 2 deletions trimesh/creation.py
Expand Up @@ -30,6 +30,12 @@
Polygon = exceptions.closure(E)
load_wkb = exceptions.closure(E)

try:
from triangle import triangulate
except BaseException as E:
from . import exceptions
triangulate = exceptions.closure(E)


def revolve(linestring,
angle=None,
Expand Down Expand Up @@ -455,8 +461,6 @@ def triangulate_polygon(polygon,

return vertices, faces

# do the import here for soft requirement
from triangle import triangulate
# set default triangulation arguments if not specified
if triangle_args is None:
triangle_args = 'p'
Expand Down
5 changes: 3 additions & 2 deletions trimesh/curvature.py
Expand Up @@ -10,8 +10,9 @@

try:
from scipy.sparse import coo_matrix
except ImportError:
pass
except ImportError as E:
from . import exceptions
coo_matrix = exceptions.closure(E)


def face_angles_sparse(mesh):
Expand Down
4 changes: 2 additions & 2 deletions trimesh/rendering.py
Expand Up @@ -359,9 +359,9 @@ def matrix_to_gl(matrix):
Transform in pyglet format
"""
from pyglet import gl

# convert to GLfloat, switch to column major and flatten to (16,)
return (gl.GLfloat * 16)(*np.asanyarray(
return (gl.GLfloat * 16)(*np.array(
matrix, dtype=np.float32).T.ravel())


Expand Down
10 changes: 7 additions & 3 deletions trimesh/scene/transforms.py
Expand Up @@ -8,6 +8,9 @@
from .. import caching
from .. import transformations

# we compare to identity a lot
_identity = np.eye(4)
_identity.flags['WRITEABLE'] = False

class SceneGraph(object):
"""
Expand Down Expand Up @@ -265,7 +268,7 @@ def to_gltf(self, scene, mesh_index=None):
# get the matrix from this edge
matrix = edge_data[(parent, node)]['matrix']
# only include if it's not an identify matrix
if np.abs(matrix - np.eye(4)).max() > 1e-5:
if not util.allclose(matrix, _identity):
info['matrix'] = matrix.T.reshape(-1).tolist()

# if an extra was stored on this edge
Expand Down Expand Up @@ -523,8 +526,9 @@ def add_edge(self, u, v, **kwargs):
else:
# check to see if matrix and geometry are identical
edge = self.edge_data[(u, v)]
if (np.allclose(kwargs.get('matrix', np.eye(4)),
edge.get('matrix', np.eye(4)))
if (util.allclose(kwargs.get('matrix', _identity),
edge.get('matrix', _identity),
1e-8)
and (edge.get('geometry') ==
kwargs.get('geometry'))):
return False
Expand Down
4 changes: 2 additions & 2 deletions trimesh/util.py
Expand Up @@ -2250,7 +2250,7 @@ def isclose(a, b, atol):
return close


def allclose(a, b, atol):
def allclose(a, b, atol=1e-8):
"""
A replacement for np.allclose that does few checks
and validation and as a result is faster.
Expand All @@ -2268,7 +2268,7 @@ def allclose(a, b, atol):
-----------
bool indicating if all elements are within `atol`.
"""
return np.all(np.abs(a - b).max() < atol)
return (a - b).ptp() < atol


class FunctionRegistry(Mapping):
Expand Down

0 comments on commit d940c2f

Please sign in to comment.