Exemplo n.º 1
0
def add_point_cloud_mesh_to_scene(point_cloud, scene, pose, pts_colors):
    '''
    pts_colors: np array, Nx3
    point_cloud: NX3, np array
    '''
    points = point_cloud

    mn = Mesh.from_points(points, colors=pts_colors)
    pc_node = scene.add(mn, pose=pose)

    return pc_node
Exemplo n.º 2
0
    def _create_node_from_points(points,
                                 name=None,
                                 pose=None,
                                 color=None,
                                 material=None,
                                 tube_radius=None,
                                 n_divs=20):
        """Helper method that creates a pyrender.Node from an array of points"""
        points = np.asanyarray(points)
        if points.ndim == 1:
            points = np.array([points])

        if pose is None:
            pose = np.eye(4)
        else:
            pose = pose.matrix

        # Create vertex colors if needed
        if color is not None:
            color = np.asanyarray(color, dtype=np.float)
            if color.ndim == 1 or len(color) != len(points):
                color = np.repeat(color[np.newaxis, :], len(points), axis=0)

        if tube_radius is not None:
            poses = None
            mesh = trimesh.creation.uv_sphere(tube_radius, [n_divs, n_divs])
            if color is not None:
                mesh.visual.vertex_colors = color[0]
            poses = np.tile(np.eye(4),
                            (len(points), 1)).reshape(len(points), 4, 4)
            poses[:, :3, 3::4] = points[:, :, None]
            m = Mesh.from_trimesh(mesh, material=material, poses=poses)
        else:
            m = Mesh.from_points(points, colors=color)

        return Node(mesh=m, name=name, matrix=pose)
    def _create_node_from_points(points,
                                 name=None,
                                 pose=None,
                                 color=None,
                                 material=None,
                                 radius=None,
                                 n_divs=20):
        """Helper method that creates a pyrender.Node from an array of points"""
        points = np.asanyarray(points)
        if points.ndim == 1:
            points = np.array([points])

        if pose is None:
            pose = np.eye(4)
        else:
            pose = pose.matrix

        color = np.asanyarray(color,
                              dtype=np.float) if color is not None else None

        # If color specified per point, use sprites
        if color is not None and color.ndim > 1:
            Visualizer3D._kwargs.update({"point_size": 1000 * radius})
            m = Mesh.from_points(points, colors=color)
        # otherwise, we can make pretty spheres
        else:
            mesh = trimesh.creation.uv_sphere(radius, [n_divs, n_divs])
            if color is not None:
                mesh.visual.vertex_colors = color
            poses = None
            poses = np.tile(np.eye(4),
                            (len(points), 1)).reshape(len(points), 4, 4)
            poses[:, :3, 3::4] = points[:, :, None]
            m = Mesh.from_trimesh(mesh, material=material, poses=poses)

        return Node(mesh=m, name=name, matrix=pose)
Exemplo n.º 4
0
    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)
    spot_l = SpotLight(color=np.ones(3),
                       intensity=10.0,
                       innerConeAngle=np.pi / 16,
                       outerConeAngle=np.pi / 6)
    point_l = PointLight(color=np.ones(3), intensity=10.0)

    # ==============================================================================
    # Camera creation
    # ==============================================================================
def test_meshes():

    with pytest.raises(TypeError):
        x = Mesh()
    with pytest.raises(TypeError):
        x = Primitive()
    with pytest.raises(ValueError):
        x = Primitive([], mode=10)

    # Basics
    x = Mesh([])
    assert x.name is None
    assert x.is_visible
    assert x.weights is None

    x.name = 'str'

    # From Trimesh
    x = Mesh.from_trimesh(trimesh.creation.box())
    assert isinstance(x, Mesh)
    assert len(x.primitives) == 1
    assert x.is_visible
    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))
    assert not x.is_transparent

    # Test some primitive functions
    x = x.primitives[0]
    with pytest.raises(ValueError):
        x.normals = np.zeros(10)
    with pytest.raises(ValueError):
        x.tangents = np.zeros(10)
    with pytest.raises(ValueError):
        x.texcoord_0 = np.zeros(10)
    with pytest.raises(ValueError):
        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):
        x = Mesh.from_trimesh(None)

    # With instancing
    poses = np.tile(np.eye(4), (5,1,1))
    poses[:,0,3] = np.array([0,1,2,3,4])
    x = Mesh.from_trimesh(trimesh.creation.box(), poses=poses)
    assert np.allclose(x.bounds, np.array([
        [-0.5, -0.5, -0.5],
        [4.5, 0.5, 0.5]
    ]))
    poses = np.eye(4)
    x = Mesh.from_trimesh(trimesh.creation.box(), poses=poses)
    poses = np.eye(3)
    with pytest.raises(ValueError):
        x = Mesh.from_trimesh(trimesh.creation.box(), poses=poses)

    # From textured meshes
    fm = trimesh.load('tests/data/fuze.obj')
    x = Mesh.from_trimesh(fm)
    assert isinstance(x, Mesh)
    assert len(x.primitives) == 1
    assert x.is_visible
    assert not x.is_transparent
    assert x.primitives[0].material.baseColorTexture is not None

    x = Mesh.from_trimesh(fm, smooth=False)
    fm.visual = fm.visual.to_color()
    fm.visual.face_colors = np.array([1.0, 0.0, 0.0, 1.0])
    x = Mesh.from_trimesh(fm, smooth=False)
    with pytest.raises(ValueError):
        x = Mesh.from_trimesh(fm, smooth=True)

    fm.visual.vertex_colors = np.array([1.0, 0.0, 0.0, 0.5])
    x = Mesh.from_trimesh(fm, smooth=False)
    x = Mesh.from_trimesh(fm, smooth=True)
    assert x.primitives[0].color_0 is not None
    assert x.is_transparent

    bm = trimesh.load('tests/data/WaterBottle.glb').dump()[0]
    x = Mesh.from_trimesh(bm)
    assert x.primitives[0].material.baseColorTexture is not None
    assert x.primitives[0].material.emissiveTexture is not None
    assert x.primitives[0].material.metallicRoughnessTexture is not None

    # From point cloud
    x = Mesh.from_points(fm.vertices)
Exemplo n.º 6
0
    #==============================================================================

    scene = Scene(ambient_light=np.array([0.02, 0.02, 0.02, 1.0]))
    cam_node = scene.add(cam, pose=cam_pose)

    scene.main_camera_node = cam_node

    #==============================================================================
    drill_node = scene.add(drill_mesh)
    r = OffscreenRenderer(viewport_width=640, viewport_height=480)

    rf = pyrender.RenderFlags.NONE
    rf |= pyrender.RenderFlags.USE_RAW_DEPTH
    color, raw_depth = r.render(scene, flags=rf)
    r.delete()

    # unproject to get point cloud
    pcd = unproj(inv_mvp, raw_depth)

    #==============================================================================
    #------------------------------------------------------------------------------
    # Creating meshes from point clouds
    #------------------------------------------------------------------------------
    points_mesh = Mesh.from_points(pcd, colors=np.array((0.0, 1.0, 0.0, 1.0)))
    pcd_node = scene.add(points_mesh)

    #==============================================================================
    # Using the viewer with a pre-specified camera
    #==============================================================================
    v = Viewer(scene, render_flags={'point_size': 5})
def test_offscreen_renderer(tmpdir):

    # Fuze trimesh
    fuze_trimesh = trimesh.load('examples/models/fuze.obj')
    fuze_mesh = Mesh.from_trimesh(fuze_trimesh)

    # Drill trimesh
    drill_trimesh = trimesh.load('examples/models/drill.obj')
    drill_mesh = Mesh.from_trimesh(drill_trimesh)
    drill_pose = np.eye(4)
    drill_pose[0, 3] = 0.1
    drill_pose[2, 3] = -np.min(drill_trimesh.vertices[:, 2])

    # Wood trimesh
    wood_trimesh = trimesh.load('examples/models/wood.obj')
    wood_mesh = Mesh.from_trimesh(wood_trimesh)

    # Water bottle trimesh
    bottle_gltf = trimesh.load('examples/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],
    ])

    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)
    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
    # Instanced
    poses = np.tile(np.eye(4), (2, 1, 1))
    poses[0, :3, 3] = np.array([-0.1, -0.10, 0.05])
    poses[1, :3, 3] = np.array([-0.15, -0.10, 0.05])
    boxf_mesh = Mesh.from_trimesh(boxf_trimesh, poses=poses, smooth=False)

    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)

    direc_l = DirectionalLight(color=np.ones(3), intensity=1.0)
    spot_l = SpotLight(color=np.ones(3),
                       intensity=10.0,
                       innerConeAngle=np.pi / 16,
                       outerConeAngle=np.pi / 6)

    cam = PerspectiveCamera(yfov=(np.pi / 3.0))
    cam_pose = np.array([[0.0, -np.sqrt(2) / 2,
                          np.sqrt(2) / 2, 0.5], [1.0, 0.0, 0.0, 0.0],
                         [0.0, np.sqrt(2) / 2,
                          np.sqrt(2) / 2, 0.4], [0.0, 0.0, 0.0, 1.0]])

    scene = Scene(ambient_light=np.array([0.02, 0.02, 0.02]))

    fuze_node = Node(mesh=fuze_mesh,
                     translation=np.array(
                         [0.1, 0.15, -np.min(fuze_trimesh.vertices[:, 2])]))
    scene.add_node(fuze_node)
    boxv_node = Node(mesh=boxv_mesh, translation=np.array([-0.1, 0.10, 0.05]))
    scene.add_node(boxv_node)
    boxf_node = Node(mesh=boxf_mesh)
    scene.add_node(boxf_node)

    _ = scene.add(drill_mesh, pose=drill_pose)
    _ = scene.add(bottle_mesh, pose=bottle_pose)
    _ = scene.add(wood_mesh)
    _ = scene.add(direc_l, pose=cam_pose)
    _ = scene.add(spot_l, pose=cam_pose)
    _ = scene.add(points_mesh)

    _ = scene.add(cam, pose=cam_pose)

    r = OffscreenRenderer(viewport_width=640, viewport_height=480)
    color, depth = r.render(scene)

    assert color.shape == (480, 640, 3)
    assert depth.shape == (480, 640)
    assert np.max(depth.data) > 0.05
    assert np.count_nonzero(depth.data) > (0.2 * depth.size)
    r.delete()
Exemplo n.º 8
0
          np.cos(theta / 180 * np.pi), 0.0], [0.0, 0.0, 0.0, 1.0]])


def location(x, y, z):
    return np.array([[1.0, 0.0, 0.0, x], [0.0, 1.0, 0.0, y],
                     [0.0, 0.0, 1.0, z], [0.0, 0.0, 0.0, 1.0]])


if __name__ == "__main__":

    scene = Scene(ambient_light=np.array([0.1, 0.1, 0.1, 1.0]))

    cam = PerspectiveCamera(yfov=(np.pi / 3.0))
    cam_pose = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0],
                         [0.0, 0.0, 1.0, 2.0], [0.0, 0.0, 0.0, 1.0]])
    points_mesh = Mesh.from_points(np.array([[0.0, 0.0, 0.0]]))
    human_node = scene.add(points_mesh)
    plane_size = 0.7
    plane_points = np.array([[plane_size, -0.65, plane_size],
                             [plane_size, -0.65, -plane_size],
                             [-plane_size, -0.65, plane_size],
                             [-plane_size, -0.65, -plane_size]])
    plane_faces = np.array([[0, 1, 2], [2, 1, 3]])
    plane_colors = np.ones((4, 3), dtype=np.float32) / 3.0
    plane_mesh = trimesh.Trimesh(vertices=plane_points,
                                 faces=plane_faces,
                                 vertex_colors=plane_colors)
    plane_mesh = Mesh.from_trimesh(plane_mesh, smooth=False)
    plane_node = scene.add(plane_mesh)
    direc_l = DirectionalLight(color=np.ones(3), intensity=3.0)
    light_node1 = scene.add(direc_l,