Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
x.texcoord_1 = np.zeros(10)
with pytest.raises(TypeError):
x.material = np.zeros(10)
assert x.targets is None
assert np.allclose(x.bounds, np.array([
[-0.5, -0.5, -0.5],
[0.5, 0.5, 0.5]
]))
assert np.allclose(x.centroid, np.zeros(3))
assert np.allclose(x.extents, np.ones(3))
assert np.allclose(x.scale, np.sqrt(3))
x.material.baseColorFactor = np.array([0.0, 0.0, 0.0, 0.0])
assert x.is_transparent
# From two trimeshes
x = Mesh.from_trimesh([trimesh.creation.box(),
trimesh.creation.cylinder(radius=0.1, height=2.0)],
smooth=False)
assert isinstance(x, Mesh)
assert len(x.primitives) == 2
assert x.is_visible
assert np.allclose(x.bounds, np.array([
[-0.5, -0.5, -1.0],
[0.5, 0.5, 1.0]
]))
assert np.allclose(x.centroid, np.zeros(3))
assert np.allclose(x.extents, [1.0, 1.0, 2.0])
assert np.allclose(x.scale, np.sqrt(6))
assert not x.is_transparent
# From bad data
with pytest.raises(TypeError):
def create_scene():
"""
Create a scene with a Fuze bottle, some cubes, and an axis.
Returns
----------
scene : trimesh.Scene
Object with geometry
"""
scene = trimesh.Scene()
# plane
geom = trimesh.creation.box((0.5, 0.5, 0.01))
geom.apply_translation((0, 0, -0.005))
geom.visual.face_colors = (.6, .6, .6)
scene.add_geometry(geom)
# axis
geom = trimesh.creation.axis(0.02)
scene.add_geometry(geom)
box_size = 0.1
# box1
geom = trimesh.creation.box((box_size,) * 3)
geom.visual.face_colors = np.random.uniform(
0, 1, (len(geom.faces), 3))
transform = tf.translation_matrix([0.1, 0.1, box_size / 2])
scene.add_geometry(geom, transform=transform)
def __init__(self, size, material=None):
""" Parameters
----------
size : tuple of float
The side lengths the box like (length, width, height)
"""
self._size = np.array(size)
mesh = trimesh.creation.box(size)
super(Box, self).__init__(mesh, material=material)
def Box():
sphere = trimesh.creation.box(extents=np.array([0.5, 0.5, 0.5]))
mesh = Mesh.from_trimesh(sphere, smooth=False)
node = Node(mesh=mesh, translation=np.array([0, 0.25, 0]))
return node
scene = trimesh.Scene()
# plane
geom = trimesh.creation.box((0.5, 0.5, 0.01))
geom.apply_translation((0, 0, -0.005))
geom.visual.face_colors = (.6, .6, .6)
scene.add_geometry(geom)
# axis
geom = trimesh.creation.axis(0.02)
scene.add_geometry(geom)
box_size = 0.1
# box1
geom = trimesh.creation.box((box_size,) * 3)
geom.visual.face_colors = np.random.uniform(
0, 1, (len(geom.faces), 3))
transform = tf.translation_matrix([0.1, 0.1, box_size / 2])
scene.add_geometry(geom, transform=transform)
# box2
geom = trimesh.creation.box((box_size,) * 3)
geom.visual.face_colors = np.random.uniform(
0, 1, (len(geom.faces), 3))
transform = tf.translation_matrix([-0.1, 0.1, box_size / 2])
scene.add_geometry(geom, transform=transform)
# fuze
geom = trimesh.load(str(here / '../models/fuze.obj'))
transform = tf.translation_matrix([-0.1, -0.1, 0])
scene.add_geometry(geom, transform=transform)
if __name__ == '__main__':
# print log messages to terminal
trimesh.util.attach_to_log()
# the size of our boxes
extents = [6, 12, 2]
# create a simulated brick with noise and random transform
scan = simulated_brick(face_count=5000,
extents=extents,
noise=.05)
# create a "true" mesh
truth = trimesh.creation.box(extents=extents)
# (4, 4) float homogenous transform from truth to scan
# this will do an ICP refinement with initial transforms
# seeded by the principal components of inertia
truth_to_scan, cost = truth.register(scan)
print("centroid distance pre-registration:",
np.linalg.norm(truth.centroid - scan.centroid))
# apply the registration transform
truth.apply_transform(truth_to_scan)
print("centroid distance post-registration:",
np.linalg.norm(truth.centroid - scan.centroid))
# find the distance from the truth mesh to each scan vertex
def convert_oriented_box_to_trimesh_fmt(box):
ctr = box[:3]
lengths = box[3:6]
trns = np.eye(4)
trns[0:3, 3] = ctr
trns[3,3] = 1.0
trns[0:3,0:3] = heading2rotmat(box[6])
box_trimesh_fmt = trimesh.creation.box(lengths, trns)
return box_trimesh_fmt
# Water bottle trimesh
bottle_gltf = trimesh.load('./models/WaterBottle.glb')
bottle_trimesh = bottle_gltf.geometry[list(bottle_gltf.geometry.keys())[0]]
bottle_mesh = Mesh.from_trimesh(bottle_trimesh)
bottle_pose = np.array([
[1.0, 0.0, 0.0, 0.1],
[0.0, 0.0, -1.0, -0.16],
[0.0, 1.0, 0.0, 0.13],
[0.0, 0.0, 0.0, 1.0],
])
#------------------------------------------------------------------------------
# Creating meshes with per-vertex colors
#------------------------------------------------------------------------------
boxv_trimesh = trimesh.creation.box(extents=0.1*np.ones(3))
boxv_vertex_colors = np.random.uniform(size=(boxv_trimesh.vertices.shape))
boxv_trimesh.visual.vertex_colors = boxv_vertex_colors
boxv_mesh = Mesh.from_trimesh(boxv_trimesh, smooth=False)
#------------------------------------------------------------------------------
# Creating meshes with per-face colors
#------------------------------------------------------------------------------
boxf_trimesh = trimesh.creation.box(extents=0.1*np.ones(3))
boxf_face_colors = np.random.uniform(size=boxf_trimesh.faces.shape)
boxf_trimesh.visual.face_colors = boxf_face_colors
boxf_mesh = Mesh.from_trimesh(boxf_trimesh, smooth=False)
#------------------------------------------------------------------------------
# Creating meshes from point clouds
#------------------------------------------------------------------------------
points = trimesh.creation.icosphere(radius=0.05).vertices
[0.0, 1.0, 0.0, 0.13],
[0.0, 0.0, 0.0, 1.0],
])
#------------------------------------------------------------------------------
# Creating meshes with per-vertex colors
#------------------------------------------------------------------------------
boxv_trimesh = trimesh.creation.box(extents=0.1*np.ones(3))
boxv_vertex_colors = np.random.uniform(size=(boxv_trimesh.vertices.shape))
boxv_trimesh.visual.vertex_colors = boxv_vertex_colors
boxv_mesh = Mesh.from_trimesh(boxv_trimesh, smooth=False)
#------------------------------------------------------------------------------
# Creating meshes with per-face colors
#------------------------------------------------------------------------------
boxf_trimesh = trimesh.creation.box(extents=0.1*np.ones(3))
boxf_face_colors = np.random.uniform(size=boxf_trimesh.faces.shape)
boxf_trimesh.visual.face_colors = boxf_face_colors
boxf_mesh = Mesh.from_trimesh(boxf_trimesh, smooth=False)
#------------------------------------------------------------------------------
# Creating meshes from point clouds
#------------------------------------------------------------------------------
points = trimesh.creation.icosphere(radius=0.05).vertices
point_colors = np.random.uniform(size=points.shape)
points_mesh = Mesh.from_points(points, colors=point_colors)
#==============================================================================
# Light creation
#==============================================================================
direc_l = DirectionalLight(color=np.ones(3), intensity=1.0)
"""
Produce a mesh that is a rectangular solid with noise
with a random transform.
Parameters
-------------
face_count : int
Approximate number of faces desired
extents : (n,3) float
Dimensions of brick
noise : float
Magnitude of vertex noise to apply
"""
# create the mesh as a simple box
mesh = trimesh.creation.box(extents=extents)
# add some systematic error pre- tesselation
mesh.vertices[0] += mesh.vertex_normals[0] + (noise * 2)
# subdivide until we have more faces than we want
for i in range(max_iter):
if len(mesh.vertices) > face_count:
break
mesh = mesh.subdivide()
# apply tesselation and random noise
mesh = mesh.permutate.noise(noise)
# randomly rotation with translation
transform = trimesh.transformations.random_rotation_matrix()
transform[:3, 3] = (np.random.random(3) - .5) * 1000