Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_multiple(self):
"""
Make sure boolean operations work on multiple meshes.
"""
engines = [
('blender', g.trimesh.interfaces.blender.exists),
('scad', g.trimesh.interfaces.scad.exists)]
for engine, exists in engines:
if not exists:
continue
a = g.trimesh.primitives.Sphere(center=[0, 0, 0])
b = g.trimesh.primitives.Sphere(center=[0, 0, .75])
c = g.trimesh.primitives.Sphere(center=[0, 0, 1.5])
r = g.trimesh.boolean.union([a, b, c])
assert r.is_volume
assert r.body_count == 1
assert g.np.isclose(r.volume,
8.617306056726884)
def test_units(self):
"""
Trimesh will store units as a GLTF extra if they
are defined so check that.
"""
original = g.get_mesh('pins.glb')
# export it as a a GLB file
export = original.export(file_type='glb')
kwargs = g.trimesh.exchange.gltf.load_glb(
g.trimesh.util.wrap_as_stream(export))
# roundtrip it
reloaded = g.trimesh.exchange.load.load_kwargs(kwargs)
# make basic assertions
g.scene_equal(original, reloaded)
# make assertions on original and reloaded
for scene in [original, reloaded]:
# units should be stored as an extra
assert scene.units == 'mm'
# make sure we have two unique geometries
assert len(scene.geometry) == 2
# that should have seven instances
assert len(scene.graph.nodes_geometry) == 7
# all meshes should be well constructed
assert all(m.is_volume for m in
scene.geometry.values())
assert g.np.isclose(pattern.area, 6.0)
# should be a valid Path2D
g.check_path2D(pattern)
# make 10 untouching rectangles
pattern = creation.rectangle(
g.np.arange(40).reshape((-1, 2, 2)))
assert len(pattern.entities) == 10
assert len(pattern.polygons_closed) == 10
assert len(pattern.polygons_full) == 10
# should be a valid Path2D
g.check_path2D(pattern)
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()
def test_block_wrap(self):
"""
Test blocks with wrapping
"""
# save the mouthful
blocks = g.trimesh.grouping.blocks
# case: both ends are in a block
data = g.np.array([1, 1, 0, 0, 1, 1, 1, 1])
kwargs = {'data': data,
'min_len': 2,
'wrap': True,
'only_nonzero': True}
r = blocks(**kwargs)
# should be one group
assert len(r) == 1
# should have every element
assert g.np.allclose(r[0], [4, 5, 6, 7, 0, 1])
assert len(r[0]) == data.sum()
assert g.np.allclose(data[r[0]], 1)
check_roll_wrap(**kwargs)
assert id(copied.triangles_tree) == id(
mesh.triangles_tree)
# cache should be same data in different object
assert id(copied._cache.cache) != id(mesh._cache.cache)
assert id(copied._cache) != id(mesh._cache)
# identifier shouldn't change
assert g.np.allclose(copied.identifier,
mesh.identifier)
# ...still shouldn't have changed anything
assert start == {mesh.md5(), mesh.crc()}
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()
raise ValueError('inf values in %s/%s',
file_name, name)
# ...still shouldn't have changed anything
assert start == {mesh.md5(), mesh.crc()}
# log the names of properties we need to make read-only
if len(writeable) > 0:
# TODO : all cached values should be read-only
g.log.error(
'cached properties writeable: {}'.format(
', '.join(writeable)))
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()
def test_primitives(self):
for prim in [g.trimesh.primitives.Sphere(),
g.trimesh.primitives.Cylinder(),
g.trimesh.primitives.Box()]:
assert prim.is_convex
# convex things should have hulls of the same volume
# convert to mesh to get tessellated volume rather than
# analytic primitive volume
tess = prim.to_mesh()
assert g.np.isclose(tess.convex_hull.volume,
tess.volume)
distance = a_check.nearest.on_surface(b.vertices)[1]
assert distance.max() < (noise * 2)
# try our registration with points
points = g.trimesh.transform_points(
scan.sample(100),
matrix=g.trimesh.transformations.random_rotation_matrix())
truth_to_points, cost = truth.register(points)
truth.apply_transform(truth_to_points)
distance = truth.nearest.on_surface(points)[1]
assert distance.mean() < noise
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()
def test_on_edge(self):
for use_embree in [True, False]:
m = g.get_mesh('7_8ths_cube.stl')
points = [[4.5, 0, -23], [4.5, 0, -2], [0, 0, -1e-6], [0, 0, -1]]
truth = [False, True, True, True]
result = g.trimesh.ray.ray_util.contains_points(m.ray, points)
assert (result == truth).all()
inner = g.trimesh.creation.annulus(r_min=.5, r_max=.6)
outer = g.trimesh.creation.annulus(r_min=.9, r_max=1.0)
m = inner + outer
s = m.section(plane_normal=[0, 0, 1],
plane_origin=[0, 0, 0])
p = s.to_planar()[0]
assert len(p.polygons_closed) == 4
assert len(p.polygons_full) == 2
assert len(p.root) == 2
g.check_path2D(p)
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()