Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# get a path we can write
temp_name = g.tempfile.NamedTemporaryFile(
suffix='.dxf', delete=False).name
# split drawings into single body parts
splits = []
for d in g.get_2D():
s = d.split()
# check area of split result vs source
assert g.np.isclose(sum(i.area for i in s),
d.area)
splits.append(s)
d.export(file_obj=temp_name)
r = g.trimesh.load(temp_name)
assert g.np.isclose(r.area, d.area)
single = g.np.hstack(splits)
for p in single:
p.vertices /= p.scale
# make sure exporting by name works
# use tempfile to avoid dumping file in
# our working directory
p.export(temp_name)
r = g.trimesh.load(temp_name)
ratio = abs(p.length - r.length) / p.length
if ratio > .01:
g.log.error('perimeter ratio on export %s wrong! %f %f %f',
p.metadata['file_name'],
def check_triangulation(v, f, true_area):
assert g.trimesh.util.is_shape(v, (-1, 2))
assert v.dtype.kind == 'f'
assert g.trimesh.util.is_shape(f, (-1, 3))
assert f.dtype.kind == 'i'
tri = g.trimesh.util.stack_3D(v)[f]
area = g.trimesh.triangles.area(tri).sum()
assert g.np.isclose(area, true_area)
assert g.np.allclose(a.center_mass, 0.0)
# should be along Z
axis = g.np.eye(3)
if T is not None:
# rotate the symmetry axis ground truth
axis = g.trimesh.transform_points(axis, T)
# should be along rotated Z
assert (g.np.allclose(a.symmetry_axis, axis[2]) or
g.np.allclose(a.symmetry_axis, -axis[2]))
radii = [g.np.dot(a.vertices, i) for i in axis[:2]]
radii = g.np.linalg.norm(radii, axis=0)
# vertices should all be at r_min or r_max
assert g.np.logical_or(g.np.isclose(radii, 1.0),
g.np.isclose(radii, 2.0)).all()
# all heights should be at +/- height/2.0
assert g.np.allclose(g.np.abs(g.np.dot(a.vertices,
axis[2])), 0.5)
# try slicing at random origin and at center mass
mesh.slice_plane(o, n)
mesh.slice_plane(mesh.center_mass, n)
# section at random origin and center mass
mesh.section(o, n)
mesh.section(mesh.center_mass, n)
# same with multiplane
mesh.section_multiplane(o, n, heights)
mesh.section_multiplane(mesh.center_mass, n, heights)
assert not mesh.vertices.flags['WRITEABLE']
assert not mesh.faces.flags['WRITEABLE']
assert g.np.all(g.np.isclose(verts, mesh.vertices))
assert g.np.all(g.np.isclose(faces, mesh.faces))
def test_layer(self):
from shapely.geometry import Point
# create two disjoint circles and apply layers
a = g.trimesh.load_path(Point([0, 0]).buffer(1))
a.apply_layer('ACIRCLE')
b = g.trimesh.load_path(Point([2, 0]).buffer(1))
b.apply_layer('BCIRCLE')
# combine two circles
c = a + b
# should be combined
assert g.np.isclose(c.area, a.area + b.area)
# export C with just layer of A
aX = g.trimesh.load(g.io_wrap(
c.export(file_type='svg',
layers=['ACIRCLE'])),
file_type='svg')
# export C with all layers
cX = g.trimesh.load(g.io_wrap(
c.export(file_type='svg',
layers=None)),
file_type='svg')
assert len(cX.entities) == len(c.entities)
assert len(aX.entities) == 1
# export C with all layers
cX = g.trimesh.load(g.io_wrap(
c.export(file_type='svg',
layers=None)),
file_type='svg')
assert len(cX.entities) == len(c.entities)
assert len(aX.entities) == 1
# make
aR = g.trimesh.load(g.io_wrap(c.export(file_type='dxf',
layers=['ACIRCLE'])),
file_type='dxf')
assert g.np.isclose(aR.area, a.area)
tf2 = g.np.eye(4)
tf2[:3, 3] = g.np.array([-5, 0, 0])
tf3 = g.np.eye(4)
tf3[:3, 3] = g.np.array([2, 0, 0])
tf4 = g.np.eye(4)
tf4[:3, 3] = g.np.array([-2, 0, 0])
# Test one-to-many distance checking
m = g.trimesh.collision.CollisionManager()
m.add_object('cube1', cube, tf1)
dist = m.min_distance_single(cube)
assert g.np.isclose(dist, 4.0)
dist, name = m.min_distance_single(cube, return_name=True)
assert g.np.isclose(dist, 4.0)
assert name == 'cube1'
m.add_object('cube2', cube, tf2)
dist, name = m.min_distance_single(cube, tf3, return_name=True)
assert g.np.isclose(dist, 2.0)
assert name == 'cube1'
dist, name = m.min_distance_single(cube, tf4, return_name=True)
assert g.np.isclose(dist, 2.0)
assert name == 'cube2'
# Test internal distance checking and object
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)
# Test manager-to-manager distance checking
m = g.trimesh.collision.CollisionManager()
m.add_object('cube0', cube)
m.add_object('cube1', cube, tf1)
n = g.trimesh.collision.CollisionManager()
n.add_object('cube0', cube, tf2)
dist, names = m.min_distance_other(n, return_names=True)
assert g.np.isclose(dist, 4.0)
assert names == ('cube0', 'cube0')
n.add_object('cube4', cube, tf4)
dist, names = m.min_distance_other(n, return_names=True)
assert g.np.isclose(dist, 1.0)
assert names == ('cube0', 'cube4')
for d in [original, roundtrip]:
# should contain a single Text entity
assert len(d.entities) == 1
# shouldn't crash anything
assert len(d.polygons_closed) == 0
assert len(d.polygons_full) == 0
assert len(d.discrete) == 0
assert len(d.paths) == 0
# make sure it preserved case and special chars
assert d.entities[0].text == "HEY WHAT's poppin"
# height should 1.0
assert g.np.isclose(d.entities[0].height, 1.0)
# get the 2D rotation of the text
angle = d.entities[0].angle(d.vertices)
# angle should be 30 degrees
assert g.np.isclose(angle, g.np.radians(30.0))