コード例 #1
0
def convert_visuals(urdf, texture_path, vis):
  link_name_to_index = {}
  link_name_to_index[urdf.base_links[0].link_name] = -1
  for link_index in range(len(urdf.links)):
    l = urdf.links[link_index]
    link_name_to_index[l.link_name] = link_index
  b2vis = {}
  uid = -1
  if texture_path:
    material = g.MeshLambertMaterial(
        map=g.ImageTexture(
            wrap=[0, 0],
            repeat=[1, 1],
            image=g.PngImage.from_file(texture_path)))
  else:
    material = g.MeshLambertMaterial(color=0xffffff, reflectivity=0.8)

  #first the base link
  link_index = -1

  b2v, uid = convert_link_visuals(urdf.base_links[0], link_index, material, vis,
                                  uid, b2vis)

  #then convert each child link
  for joint in urdf.joints:
    link_index = link_name_to_index[joint.child_name]
    link = urdf.links[link_index]
    b2v, uid = convert_link_visuals(link, link_index, material, vis, uid, b2vis)

  return b2vis
コード例 #2
0
ファイル: test_drawing.py プロジェクト: gizatt/meshcat-python
    def runTest(self):
        self.vis.delete()
        v = self.vis["shapes"]
        v.set_transform(tf.translation_matrix([1., 0, 0]))
        v["cube"].set_object(g.Box([0.1, 0.2, 0.3]))
        v["cube"].set_transform(tf.translation_matrix([0.05, 0.1, 0.15]))
        v["cylinder"].set_object(g.Cylinder(0.2, 0.1), g.MeshLambertMaterial(color=0x22dd22))
        v["cylinder"].set_transform(tf.translation_matrix([0, 0.5, 0.1]).dot(tf.rotation_matrix(-np.pi / 2, [1, 0, 0])))
        v["sphere"].set_object(g.Mesh(g.Sphere(0.15), g.MeshLambertMaterial(color=0xff11dd)))
        v["sphere"].set_transform(tf.translation_matrix([0, 1, 0.15]))
        v["ellipsoid"].set_object(g.Ellipsoid([0.3, 0.1, 0.1]))
        v["ellipsoid"].set_transform(tf.translation_matrix([0, 1.5, 0.1]))

        v = self.vis["meshes/valkyrie/head"]
        v.set_object(g.Mesh(
            g.ObjMeshGeometry.from_file(os.path.join(meshcat.viewer_assets_path(), "data/head_multisense.obj")),
            g.MeshLambertMaterial(
                map=g.ImageTexture(
                    image=g.PngImage.from_file(os.path.join(meshcat.viewer_assets_path(), "data/HeadTextureMultisense.png"))
                )
            )
        ))
        v.set_transform(tf.translation_matrix([0, 0.5, 0.5]))

        v = self.vis["points"]
        v.set_transform(tf.translation_matrix([-1, 0, 0]))
        verts = np.random.rand(3, 100000)
        colors = verts
        v["random"].set_object(g.PointCloud(verts, colors))
        v["random"].set_transform(tf.translation_matrix([-0.5, -0.5, 0]))
コード例 #3
0
ファイル: utils.py プロジェクト: zp2388/google-research
def make_frame(vis, name, h, radius, o=1.0):
    """Add a red-green-blue triad to the Meschat visualizer.

  Args:
    vis (MeshCat Visualizer): the visualizer
    name (string): name for this frame (should be unique)
    h (float): height of frame visualization
    radius (float): radius of frame visualization
    o (float): opacity
  """
    vis[name]['x'].set_object(
        g.Cylinder(height=h, radius=radius),
        g.MeshLambertMaterial(color=0xff0000, reflectivity=0.8, opacity=o))
    rotate_x = mtf.rotation_matrix(np.pi / 2.0, [0, 0, 1])
    rotate_x[0, 3] = h / 2
    vis[name]['x'].set_transform(rotate_x)

    vis[name]['y'].set_object(
        g.Cylinder(height=h, radius=radius),
        g.MeshLambertMaterial(color=0x00ff00, reflectivity=0.8, opacity=o))
    rotate_y = mtf.rotation_matrix(np.pi / 2.0, [0, 1, 0])
    rotate_y[1, 3] = h / 2
    vis[name]['y'].set_transform(rotate_y)

    vis[name]['z'].set_object(
        g.Cylinder(height=h, radius=radius),
        g.MeshLambertMaterial(color=0x0000ff, reflectivity=0.8, opacity=o))
    rotate_z = mtf.rotation_matrix(np.pi / 2.0, [1, 0, 0])
    rotate_z[2, 3] = h / 2
    vis[name]['z'].set_transform(rotate_z)
コード例 #4
0
def plot_surface(meshcat, X, Y, Z, color=0xdd9999, wireframe=False):
    (rows, cols) = Z.shape

    vertices = np.empty((rows * cols, 3), dtype=np.float32)
    vertices[:, 0] = X.reshape((-1))
    vertices[:, 1] = Y.reshape((-1))
    vertices[:, 2] = Z.reshape((-1))

    # Vectorized faces code from https://stackoverflow.com/questions/44934631/making-grid-triangular-mesh-quickly-with-numpy  # noqa
    faces = np.empty((rows - 1, cols - 1, 2, 3), dtype=np.uint32)
    r = np.arange(rows * cols).reshape(rows, cols)
    faces[:, :, 0, 0] = r[:-1, :-1]
    faces[:, :, 1, 0] = r[:-1, 1:]
    faces[:, :, 0, 1] = r[:-1, 1:]
    faces[:, :, 1, 1] = r[1:, 1:]
    faces[:, :, :, 2] = r[1:, :-1, None]
    faces.shape = (-1, 3)

    if isinstance(color, int):
        meshcat.set_object(
            g.TriangularMeshGeometry(vertices, faces),
            g.MeshLambertMaterial(color=color, wireframe=wireframe))
    else:
        meshcat.set_object(
            TriangularMeshGeometry(vertices, faces, color),
            g.MeshLambertMaterial(vertexColors=True, wireframe=wireframe))
コード例 #5
0
 def _get_shape_material(self, shape):
     texture = shape.get_user_data().get(
         'visual_mesh_texture',
         None) if shape.get_user_data() is not None else None
     if texture is not None:
         return g.MeshLambertMaterial(map=texture, opacity=1.)
     clr = [int(v) for v in self.get_shape_color(shape=shape)]
     color = int(clr[0]) * 256**2 + int(clr[1]) * 256 + int(clr[2])
     return g.MeshLambertMaterial(color=color, opacity=clr[3] / 255.)
コード例 #6
0
 def add_geometry(self, geometry, pathname, transform):
     vis = self.vis
     material = g.MeshLambertMaterial(reflectivity=1.0, sides=0)
     material.transparency = True
     material.opacity = 0.5
     if isinstance(geometry, Sphere):
         sphere = geometry
         vis[pathname].set_object(
             g.Sphere(sphere.radius),
             material)
         vis[pathname].set_transform(transform)
     elif isinstance(geometry, Cylinder):
         cyl = geometry
         vis[pathname].set_object(
             g.Cylinder(cyl.length, cyl.radius),
             material
         )
         # meshcat cylinder is aligned along y-axis. Align along z then apply the
         # node's transform as normal.
         vis[pathname].set_transform(
             transform.dot(
                 tf.rotation_matrix(np.radians(-90), [1, 0, 0])
             )
         )
     elif isinstance(geometry, Mesh):
             obj = meshcat.geometry.StlMeshGeometry.from_stream(
                 io.BytesIO(trimesh.exchange.stl.export_stl(geometry.trimesh))
             )
             vis[pathname].set_object(obj, material)
             vis[pathname].set_transform(transform)
     else:
         raise NotImplementedError("Cannot yet add {} to visualiser".format(type(geometry)))
コード例 #7
0
def convert_visuals_pb(vis, urdfLinks, urdfJoints, p0, texture_path):
    b2vis = {}
    uid = -1
    print("num_links=", len(urdfLinks))
    for link in urdfLinks:
        print("num_visuals=", len(link.urdf_visual_shapes))
        for v in link.urdf_visual_shapes:
            print("v.geom_type=", v.geom_type)
            if v.geom_type == p0.GEOM_MESH:
                print("mesh filename=", v.geom_meshfilename)
                print("geom_meshscale=", v.geom_meshscale)

                vis_name = link.link_name + str(uid)
                b2v = VisualLinkInfo()
                b2v.vis_name = vis_name
                b2v.uid = uid
                b2v.origin_rpy = v.origin_rpy
                b2v.origin_xyz = v.origin_xyz
                b2v.inertia_xyz = link.urdf_inertial.origin_xyz
                b2v.inertia_rpy = link.urdf_inertial.origin_rpy

                vis[vis_name].set_object(
                    g.ObjMeshGeometry.from_file(v.geom_meshfilename),
                    g.MeshLambertMaterial(map=g.ImageTexture(
                        image=g.PngImage.from_file(texture_path))))
                v.uid = uid
                b2vis[v.uid] = b2v
                uid += 1

    print("num_joints=", len(urdfJoints))
    return b2vis
コード例 #8
0
def viewer_draw_mesh(viewer, mesh, color=None, id=None):
    if color == None:
        color = 0x777777
    if id == None:
        id = str(uuid.uuid1())
    geo = mesh2mcg(mesh)
    mat = mcg.MeshLambertMaterial(color=color)
    viewer[id].set_object(mcg.Mesh(geo, mat))
コード例 #9
0
def PlotTrajectoryMeshcat(x, t, vis, wpts_list = None):
    # initialize
    vis.delete()

    # plot waypoints
    if not(wpts_list is None):
        for i, wpts in enumerate(wpts_list):
            vis["wpt_%d" % i].set_object(geometry.Sphere(0.03),
                                         geometry.MeshLambertMaterial(color=0xffff00))
            T_wp = tf.translation_matrix(wpts)
            vis["wpt_%d" % i].set_transform(T_wp)


    d_prop = 0.10 # propeller diameter
    vis["quad"]["CG"].set_object(geometry.Sphere(0.03),
                                     geometry.MeshLambertMaterial(color=0x00ffff))
    vis["quad"]["body"].set_object(geometry.Box([0.2, 0.1, 0.1]),
                                   geometry.MeshLambertMaterial(color=0x404040))
    vis["quad"]["prop0"].set_object(geometry.Cylinder(0.01, d_prop),
                                    geometry.MeshLambertMaterial(color=0x00ff00))
    vis["quad"]["prop1"].set_object(geometry.Cylinder(0.01, d_prop),
                                    geometry.MeshLambertMaterial(color=0xff0000))
    vis["quad"]["prop2"].set_object(geometry.Cylinder(0.01, d_prop),
                                    geometry.MeshLambertMaterial(color=0xffffff))
    vis["quad"]["prop3"].set_object(geometry.Cylinder(0.01, d_prop),
                                    geometry.MeshLambertMaterial(color=0xffffff))

    Rx_prop = CalcRx(np.pi/2)
    TB = tf.translation_matrix([0,0,-0.05])
    T0 = tf.translation_matrix([l, -l, 0])
    T1 = tf.translation_matrix([l, l, 0])
    T2 = tf.translation_matrix([-l, l, 0])
    T3 = tf.translation_matrix([-l, -l, 0])
    T0[0:3,0:3] = Rx_prop
    T1[0:3,0:3] = Rx_prop
    T2[0:3,0:3] = Rx_prop
    T3[0:3,0:3] = Rx_prop

    vis["quad"]["body"].set_transform(TB)
    vis["quad"]["prop0"].set_transform(T0)
    vis["quad"]["prop1"].set_transform(T1)
    vis["quad"]["prop2"].set_transform(T2)
    vis["quad"]["prop3"].set_transform(T3)

    # visualize trajectory
    time.sleep(1.0)
    N = len(x)
    if not (t is None):
        assert N == len(t)

    for i, xi in enumerate(x):
        xyz = xi[0:3]
        rpy = xi[3:6]
        R_WB = CalcR_WB(rpy)
        T = tf.translation_matrix(xyz)
        T[0:3,0:3] = R_WB
        vis["quad"].set_transform(T)
        if i < N-1  and not(t is None):
            dt = t[i+1] - t[i]
        time.sleep(dt)
コード例 #10
0
ファイル: meshcat.py プロジェクト: vitroid/genice-meshcat
def draw_bond(v, label, p1, d, radius, color=0xffffff):
    H = np.linalg.norm(d)
    R = np.linalg.norm(d[:2])
    e = d / H
    x = np.array([0, 1, 0])
    rot = -acos(e @ x)
    if -1 < e @ x < 1:
        axis = np.cross(e, x)
        v[label].set_object(
            g.Mesh(g.Cylinder(H, radius), g.MeshLambertMaterial(color=color)))
        v[label].set_transform(
            tf.translation_matrix(p1).dot(
                tf.rotation_matrix(rot, axis).dot(
                    tf.translation_matrix([0, H / 2, 0]))))
    else:
        v[label].set_object(
            g.Mesh(g.Cylinder(H, radius), g.MeshLambertMaterial(color=color)))
        v[label].set_transform(tf.translation_matrix(p1 + d / 2))
コード例 #11
0
    def show(self, chain, showMeshes=False):
        if 'google.colab' in sys.modules:
            server_args = ['--ngrok_http_tunnel']
            # Start a single meshcat server instance to use for the remainder of this notebook.
            from meshcat.servers.zmqserver import start_zmq_server_as_subprocess
            proc, zmq_url, web_url = start_zmq_server_as_subprocess(
                server_args=server_args)
            vis = meshcat.Visualizer(zmq_url=zmq_url)
        else:
            vis = meshcat.Visualizer().open()

        if showMeshes:
            for i, link in enumerate(chain.linkArray):
                if link.meshObj == None:
                    print("No mesh: " + link.name)
                    continue
                boxVis = vis["link:" + link.name]

                boxVis.set_object(
                    link.meshObj,
                    g.MeshLambertMaterial(color=0xffffff, reflectivity=0.8))
                rotationMatrix = np.pad(link.absoluteOrientation, [(0, 1),
                                                                   (0, 1)],
                                        mode='constant')
                rotationMatrix[-1][-1] = 1
                boxVis.set_transform(
                    tf.translation_matrix(link.absoluteBase) @ rotationMatrix)

        else:

            for i, link in enumerate(chain.linkArray):

                boxVis = vis["link:" + link.name]
                if link.primitiveObj != None:
                    if isinstance(link.primitiveObj, primitives.Box):
                        box = meshcat.geometry.Box(link.primitiveObj.size)
                        boxVis.set_object(box)
                    if isinstance(link.primitiveObj, primitives.Cylinder):
                        cylinder = meshcat.geometry.Cylinder(
                            link.primitiveObj.length, link.primitiveObj.radius)
                        boxVis.set_object(cylinder)
                    if isinstance(link.primitiveObj, primitives.Sphere):
                        sphere = meshcat.geometry.Sphere(
                            link.primitiveObj.radius)
                        boxVis.set_object(cylinder)
                    rotationMatrix = np.pad(link.absoluteOrientation, [(0, 1),
                                                                       (0, 1)],
                                            mode='constant')
                    rotationMatrix[-1][-1] = 1
                    boxVis.set_transform(
                        tf.translation_matrix(link.absoluteBase)
                        @ rotationMatrix)

            boxVis = vis["skeleton"]
            boxVis.set_object(
                g.Line(g.PointsGeometry(chain.get_vertex_coords().T)))
コード例 #12
0
def viewer_draw_sphere(viewer, sphere, color=None, id=None):
    import meshcat.transformations as tf
    if color == None:
        color = 0x777777
    if id == None:
        id = str(uuid.uuid1())
    s = mcg.Sphere(sphere.radius)
    viewer[id].set_object(s), mcg.MeshLambertMaterial(color=color)
    viewer[id].set_transform(tf.translation_matrix(list(sphere.point)))
    return id
コード例 #13
0
    def runTest(self):
        """
        Test that we can render meshes from raw vertices and faces as
        numpy arrays
        """
        v = self.vis["triangular_mesh"]
        v.set_transform(tf.rotation_matrix(np.pi / 2, [0., 0, 1]))
        vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]])
        faces = np.array([[0, 1, 2], [3, 0, 2]])
        v.set_object(g.TriangularMeshGeometry(vertices, faces),
                     g.MeshLambertMaterial(color=0xeedd22, wireframe=True))

        v = self.vis["triangular_mesh_w_vertex_coloring"]
        v.set_transform(
            tf.translation_matrix([1, 0, 0]).dot(
                tf.rotation_matrix(np.pi / 2, [0, 0, 1])))
        colors = vertices
        v.set_object(g.TriangularMeshGeometry(vertices, faces, colors),
                     g.MeshLambertMaterial(vertexColors=True, wireframe=True))
コード例 #14
0
def meshcat_draw_lights(vis, light_locations, light_attenuations):
    N = light_locations.shape[1]
    colors = np.zeros((3, N))
    colors[2, :] = light_attenuations
    for k in range(N):
        vis["lights"]["%d" % k].set_object(
            g.Sphere(radius=0.05),
            g.MeshLambertMaterial(
                             color=0xffffff,
                             reflectivity=0.8))
        vis["lights"]["%d" % k].set_transform(
            tf.translation_matrix(light_locations[:, k]))
コード例 #15
0
def meshcat_draw_frustrum(vis, TF, K, near_distance, far_distance, w, h):
    # TODO(gizatt): This obviously isn't right -- the projected
    # light doesn't match the drawn view frustrum.
    # Not dealing with, for now; I think the issue is a combination
    # of bad intrinsics and bugs related to flipped image coordinates
    # somewhere along the pipeline.
    image_bbox_verts = np.array([
        [0., w, w, 0.],
        [0., 0., h, h]
    ])
    TF_inv = np.eye(4)
    TF_inv[:3, :3] = TF[:3, :3].T
    TF_inv[:3, 3] = -TF_inv[:3, :3].dot(TF[:3, 3])
    TF = TF_inv
            
    N = image_bbox_verts.shape[1]
    Kinv = np.linalg.inv(K)
    def project_bbox_verts(dist):
        homog = np.concatenate(
            [image_bbox_verts*dist, dist*np.ones((1, N))],
            axis=0
        )
        pts = np.dot(Kinv, homog)
        return ((TF[:3, :3].dot(pts)).T + TF[:3, 3]).T
    near_pts = project_bbox_verts(near_distance)
    far_pts= project_bbox_verts(far_distance)
    near_colors = np.zeros((3, N))
    near_colors[1, :] = 1.
    far_colors = np.zeros((3, N))
    far_colors[2, :] = 1.
    
    vis['frustrum']['near'].set_object(g.LineLoop(
        g.PointsGeometry(near_pts, color=near_colors),
        g.MeshBasicMaterial(vertexColors=True, linewidth=0.1)))
    vis['frustrum']['far'].set_object(g.LineLoop(
        g.PointsGeometry(far_pts, color=far_colors),
        g.MeshBasicMaterial(vertexColors=True, linewidth=0.1)))
    connecting = np.zeros((3, N*2))
    connecting[:, ::2] = near_pts
    connecting[:, 1::2] = far_pts
    connecting_colors = np.zeros((3, N*2))
    connecting_colors[:, ::2] = near_colors
    connecting_colors[:, 1::2] = far_colors
    vis['frustrum']['connecting'].set_object(g.LineSegments(
        g.PointsGeometry(connecting, color=connecting_colors),
        g.MeshBasicMaterial(vertexColors=True, linewidth=1.)
    ))

    # Draw a little box for the projector :)
    vis['projector'].set_object(
        g.Box([0.1, 0.1, 0.1]),
        g.MeshLambertMaterial(
            color=0xaaffaa))
コード例 #16
0
def plot_mathematical_program(meshcat, prog, X, Y, result=None):
    assert prog.num_vars() == 2
    assert X.size == Y.size

    N = X.size
    values = np.vstack((X.reshape(-1), Y.reshape(-1)))
    costs = prog.GetAllCosts()

    # Vectorized multiply for the quadratic form.
    # Z = (D*np.matmul(Q,D)).sum(0).reshape(nx, ny)

    if costs:
        Z = prog.EvalBindingVectorized(costs[0], values)
        for b in costs[1:]:
            Z = Z + prog.EvalBindingVectorized(b, values)

    cv = meshcat["constraint"]
    for binding in prog.GetAllConstraints():
        c = binding.evaluator()
        var_indices = [
            int(prog.decision_variable_index()[v.get_id()])
            for v in binding.variables()
        ]
        satisfied = np.array(
            c.CheckSatisfiedVectorized(values[var_indices, :],
                                       0.001)).reshape(1, -1)
        if costs:
            Z[~satisfied] = np.nan

        # Special case linear constraints
        if False:  # isinstance(c, LinearConstraint):
            # TODO: take these as (optional) arguments to avoid computing them
            # inefficiently.
            xmin = np.min(X.reshape(-1))
            xmax = np.max(X.reshape(-1))
            ymin = np.min(Y.reshape(-1))
            ymax = np.max(Y.reshape(-1))
            A = c.A()
            lower = c.lower_bound()
            upper = c.upper_bound()
            # find line / box intersections
            # https://gist.github.com/ChickenProp/3194723
        else:
            v = cv[str(binding)]
            Zc = np.zeros(Z.shape)
            Zc[satisfied] = np.nan
            plot_surface(v,
                         X,
                         Y,
                         Zc.reshape((X.shape[1], X.shape[0])),
                         color=0x9999dd)

    if costs:
        plot_surface(meshcat["objective"],
                     X,
                     Y,
                     Z.reshape(X.shape[1], X.shape[0]),
                     wireframe=True)

    if result:
        v = meshcat["solution"]
        v.set_object(g.Sphere(0.1), g.MeshLambertMaterial(color=0x99ff99))
        x_solution = result.get_x_val()
        v.set_transform(
            tf.translation_matrix(
                [x_solution[0], x_solution[1],
                 result.get_optimal_cost()]))
コード例 #17
0
    def __init__(self,
                 mbp,
                 sg,
                 all_manipulable_body_ids=[],
                 zmq_url="default"):
        LeafSystem.__init__(self)
        self.all_manipulable_body_ids = all_manipulable_body_ids
        self.set_name('HydraInteractionLeafSystem')

        # Pose bundle (from SceneGraph) input port.
        #default_sg_context = sg.CreateDefaultContext()
        #print("Default sg context: ", default_sg_context)
        #query_object = sg.get_query_output_port().Eval(default_sg_context)
        #print("Query object: ", query_object)
        #self.DeclareAbstractInputPort("query_object",
        #                              AbstractValue.Make(query_object))
        self.pose_bundle_input_port = self.DeclareAbstractInputPort(
            "pose_bundle", AbstractValue.Make(PoseBundle(0)))
        self.robot_state_input_port = self.DeclareVectorInputPort(
            "robot_state",
            BasicVector(mbp.num_positions() + mbp.num_velocities()))
        self.spatial_forces_output_port = self.DeclareAbstractOutputPort(
            "spatial_forces_vector",
            lambda: AbstractValue.Make(VectorExternallyAppliedSpatialForced()),
            self.DoCalcAbstractOutput)
        self.DeclarePeriodicPublish(0.01, 0.0)

        if zmq_url == "default":
            zmq_url = "tcp://127.0.0.1:6000"
        if zmq_url is not None:
            print("Connecting to meshcat-server at zmq_url=" + zmq_url + "...")
        self.vis = meshcat.Visualizer(zmq_url=zmq_url)
        fwd_pt_in_hydra_frame = RigidTransform(p=[0.0, 0.0, 0.0])
        self.vis["hydra_origin"]["hand"].set_object(
            meshcat_geom.ObjMeshGeometry.from_file(
                os.path.join(os.getcwd(), "hand-regularfinal-scaled-1.obj")))

        self.vis["hydra_origin"]["hand"].set_transform(
            meshcat_tf.compose_matrix(scale=[0.001, 0.001, 0.001],
                                      angles=[np.pi / 2, 0., np.pi / 2],
                                      translate=[-0.25, 0., 0.]))
        #self.vis["hydra_origin"]["center"].set_object(meshcat_geom.Sphere(0.02))
        #self.vis["hydra_origin"]["center"].set_transform(meshcat_tf.translation_matrix([-0.025, 0., 0.]))
        #self.vis["hydra_origin"]["mid"].set_object(meshcat_geom.Sphere(0.015))
        #self.vis["hydra_origin"]["mid"].set_transform(meshcat_tf.translation_matrix([0.0, 0., 0.]))
        #self.vis["hydra_origin"]["fwd"].set_object(meshcat_geom.Sphere(0.01))
        #self.vis["hydra_origin"]["fwd"].set_transform(fwd_pt_in_hydra_frame.matrix())
        #self.vis["hydra_grab"].set_object(meshcat_geom.Sphere(0.01),
        #                                  meshcat_geom.MeshLambertMaterial(
        #                                     color=0xff22dd,
        #                                     alphaMap=0.1))
        self.vis["hydra_grab"]["grab_point"].set_object(
            meshcat_geom.Sphere(0.01),
            meshcat_geom.MeshLambertMaterial(color=0xff22dd, alphaMap=0.1))
        # Hide it sketchily
        self.vis["hydra_grab"].set_transform(
            meshcat_tf.translation_matrix([0., 0., -1000.]))

        # State for selecting objects
        self.grab_needs_update = False
        self.grab_in_progress = False
        self.grab_update_hydra_pose = None
        self.selected_body = None
        self.selected_pose_in_body_frame = None
        self.desired_pose_in_world_frame = None
        self.stop = False
        self.freeze_rotation = False
        self.previously_freezing_rotation = False

        # Set up subscription to Razer Hydra
        self.mbp = mbp
        self.mbp_context = mbp.CreateDefaultContext()
        self.sg = sg
        self.hasNewMessage = False
        self.lastMsg = None
        self.hydra_origin = RigidTransform(p=[1.0, 0., -0.1],
                                           rpy=RollPitchYaw([0., 0., 0.]))
        self.hydra_prescale = 3.0

        self.callback_lock = Lock()
        self.hydraSubscriber = rospy.Subscriber("/hydra_calib",
                                                razer_hydra.msg.Hydra,
                                                self.callback,
                                                queue_size=1)
        print("Waiting for hydra startup...")
        while not self.hasNewMessage and not rospy.is_shutdown():
            rospy.sleep(0.01)
        print("Got hydra.")
コード例 #18
0
def plot_mathematical_program(meshcat,
                              prog,
                              X,
                              Y,
                              result=None,
                              point_size=0.05):
    assert prog.num_vars() == 2
    assert X.size == Y.size

    N = X.size
    values = np.vstack((X.reshape(-1), Y.reshape(-1)))
    costs = prog.GetAllCosts()

    # Vectorized multiply for the quadratic form.
    # Z = (D*np.matmul(Q,D)).sum(0).reshape(nx, ny)

    if costs:
        Z = prog.EvalBindingVectorized(costs[0], values)
        for b in costs[1:]:
            Z = Z + prog.EvalBindingVectorized(b, values)

    cv = meshcat["constraints"]
    for binding in prog.GetAllConstraints():
        if isinstance(
                binding.evaluator(),
                pydrake.solvers.mathematicalprogram.BoundingBoxConstraint):
            c = binding.evaluator()
            var_indices = [
                int(prog.decision_variable_index()[v.get_id()])
                for v in binding.variables()
            ]
            satisfied = np.array(
                c.CheckSatisfiedVectorized(values[var_indices, :],
                                           0.001)).reshape(1, -1)
            if costs:
                Z[~satisfied] = np.nan

            v = cv[type(c).__name__]
            Zc = np.zeros(Z.shape)
            Zc[satisfied] = np.nan
            plot_surface(v,
                         X,
                         Y,
                         Zc.reshape((X.shape[1], X.shape[0])),
                         color=0xff3333,
                         wireframe=True)
        else:
            Zc = prog.EvalBindingVectorized(binding, values)
            evaluator = binding.evaluator()
            low = evaluator.lower_bound()
            up = evaluator.upper_bound()
            cvb = cv[type(evaluator).__name__]
            for index in range(Zc.shape[0]):
                color = np.repeat([[0.3], [0.3], [1.0]], N, axis=1)
                infeasible = np.logical_or(Zc[index, :] < low[index],
                                           Zc[index, :] > up[index])
                color[0, infeasible] = 1.0
                color[2, infeasible] = 0.3
                plot_surface(cvb[str(index)],
                             X,
                             Y,
                             Zc[index, :].reshape(X.shape[1], X.shape[0]),
                             color=color,
                             wireframe=True)

    if costs:
        plot_surface(meshcat["objective"],
                     X,
                     Y,
                     Z.reshape(X.shape[1], X.shape[0]),
                     color=0x77cc77,
                     wireframe=True)

    if result:
        v = meshcat["solution"]
        v.set_object(g.Sphere(point_size),
                     g.MeshLambertMaterial(color=0x55ff55))
        x_solution = result.get_x_val()
        v.set_transform(
            tf.translation_matrix(
                [x_solution[0], x_solution[1],
                 result.get_optimal_cost()]))
コード例 #19
0
    def runTest(self):
        self.vis.delete()
        v = self.vis["shapes"]
        v.set_transform(tf.translation_matrix([1., 0, 0]))
        v["box"].set_object(g.Box([1.0, 0.2, 0.3]))
        v["box"].delete()
        v["box"].set_object(g.Box([0.1, 0.2, 0.3]))
        v["box"].set_transform(tf.translation_matrix([0.05, 0.1, 0.15]))
        v["cylinder"].set_object(g.Cylinder(0.2, 0.1), g.MeshLambertMaterial(color=0x22dd22))
        v["cylinder"].set_transform(tf.translation_matrix([0, 0.5, 0.1]).dot(tf.rotation_matrix(-np.pi / 2, [1, 0, 0])))
        v["sphere"].set_object(g.Mesh(g.Sphere(0.15), g.MeshLambertMaterial(color=0xff11dd)))
        v["sphere"].set_transform(tf.translation_matrix([0, 1, 0.15]))
        v["ellipsoid"].set_object(g.Ellipsoid([0.3, 0.1, 0.1]))
        v["ellipsoid"].set_transform(tf.translation_matrix([0, 1.5, 0.1]))

        v["transparent_ellipsoid"].set_object(g.Mesh(
            g.Ellipsoid([0.3, 0.1, 0.1]),
            g.MeshLambertMaterial(color=0xffffff,
                                  opacity=0.5)))
        v["transparent_ellipsoid"].set_transform(tf.translation_matrix([0, 2.0, 0.1]))

        v = self.vis["meshes/valkyrie/head"]
        v.set_object(g.Mesh(
            g.ObjMeshGeometry.from_file(os.path.join(meshcat.viewer_assets_path(), "data/head_multisense.obj")),
            g.MeshLambertMaterial(
                map=g.ImageTexture(
                    image=g.PngImage.from_file(os.path.join(meshcat.viewer_assets_path(), "data/HeadTextureMultisense.png"))
                )
            )
        ))
        v.set_transform(tf.translation_matrix([0, 0.5, 0.5]))

        v = self.vis["meshes/convex"]
        v["obj"].set_object(g.Mesh(g.ObjMeshGeometry.from_file(os.path.join(meshcat.viewer_assets_path(), "../tests/data/mesh_0_convex_piece_0.obj"))))
        v["stl_ascii"].set_object(g.Mesh(g.StlMeshGeometry.from_file(os.path.join(meshcat.viewer_assets_path(), "../tests/data/mesh_0_convex_piece_0.stl_ascii"))))
        v["stl_ascii"].set_transform(tf.translation_matrix([0, -0.5, 0]))
        v["stl_binary"].set_object(g.Mesh(g.StlMeshGeometry.from_file(os.path.join(meshcat.viewer_assets_path(), "../tests/data/mesh_0_convex_piece_0.stl_binary"))))
        v["stl_binary"].set_transform(tf.translation_matrix([0, -1, 0]))
        v["dae"].set_object(g.Mesh(g.DaeMeshGeometry.from_file(os.path.join(meshcat.viewer_assets_path(), "../tests/data/mesh_0_convex_piece_0.dae"))))
        v["dae"].set_transform(tf.translation_matrix([0, -1.5, 0]))


        v = self.vis["points"]
        v.set_transform(tf.translation_matrix([0, 2, 0]))
        verts = np.random.rand(3, 1000000)
        colors = verts
        v["random"].set_object(g.PointCloud(verts, colors))
        v["random"].set_transform(tf.translation_matrix([-0.5, -0.5, 0]))

        v = self.vis["lines"]
        v.set_transform(tf.translation_matrix(([-2, -3, 0])))

        vertices = np.random.random((3, 10)).astype(np.float32)
        v["line_segments"].set_object(g.LineSegments(g.PointsGeometry(vertices)))

        v["line"].set_object(g.Line(g.PointsGeometry(vertices)))
        v["line"].set_transform(tf.translation_matrix([0, 1, 0]))

        v["line_loop"].set_object(g.LineLoop(g.PointsGeometry(vertices)))
        v["line_loop"].set_transform(tf.translation_matrix([0, 2, 0]))

        v["line_loop_with_material"].set_object(g.LineLoop(g.PointsGeometry(vertices), g.LineBasicMaterial(color=0xff0000)))
        v["line_loop_with_material"].set_transform(tf.translation_matrix([0, 3, 0]))

        colors = vertices  # Color each line by treating its xyz coordinates as RGB colors
        v["line_with_vertex_colors"].set_object(g.Line(g.PointsGeometry(vertices, colors), g.LineBasicMaterial(vertexColors=True)))
        v["line_with_vertex_colors"].set_transform(tf.translation_matrix([0, 4, 0]))

        v["triad"].set_object(g.LineSegments(
            g.PointsGeometry(position=np.array([
                [0, 0, 0], [1, 0, 0],
                [0, 0, 0], [0, 1, 0],
                [0, 0, 0], [0, 0, 1]]).astype(np.float32).T,
                color=np.array([
                [1, 0, 0], [1, 0.6, 0],
                [0, 1, 0], [0.6, 1, 0],
                [0, 0, 1], [0, 0.6, 1]]).astype(np.float32).T
            ),
            g.LineBasicMaterial(vertexColors=True)))
        v["triad"].set_transform(tf.translation_matrix(([0, 5, 0])))

        v["triad_function"].set_object(g.triad(0.5))
        v["triad_function"].set_transform(tf.translation_matrix([0, 6, 0]))
コード例 #20
0
ファイル: meshcat.py プロジェクト: vitroid/genice-meshcat
def draw_atom(v, label, atom, radius, color=0xffffff):
    v[label].set_object(
        g.Mesh(g.Sphere(radius), g.MeshLambertMaterial(color=color)))
    v[label].set_transform(tf.translation_matrix(atom))
コード例 #21
0
    def animate(self, chain, states, framerate=5, showMeshes=False):
        if 'google.colab' in sys.modules:
            server_args = ['--ngrok_http_tunnel']
            # Start a single meshcat server instance to use for the remainder of this notebook.
            from meshcat.servers.zmqserver import start_zmq_server_as_subprocess
            proc, zmq_url, web_url = start_zmq_server_as_subprocess(
                server_args=server_args)
            vis = meshcat.Visualizer(zmq_url=zmq_url)
        else:
            vis = meshcat.Visualizer().open()

        anim = Animation()

        vertices = chain.get_vertex_coords()

        if showMeshes:
            for i, link in enumerate(chain.linkArray):
                if link.meshObj == None:
                    print("No mesh: " + link.name)
                    continue
                boxVis = vis["link:" + link.name]

                boxVis.set_object(
                    link.meshObj,
                    g.MeshLambertMaterial(color=0xffffff, reflectivity=0.8))
                rotationMatrix = np.pad(link.absoluteOrientation, [(0, 1),
                                                                   (0, 1)],
                                        mode='constant')
                rotationMatrix[-1][-1] = 1
                boxVis.set_transform(
                    tf.translation_matrix(link.absoluteBase) @ rotationMatrix)

            for i in range(len(states)):
                chain.update(states[i])
                with anim.at_frame(vis, framerate * i) as frame:
                    for i, link in enumerate(chain.linkArray):
                        if link.meshObj == None:
                            continue

                        boxVis = frame["link:" + link.name]
                        rotationMatrix = np.pad(link.absoluteOrientation,
                                                [(0, 1), (0, 1)],
                                                mode='constant')
                        rotationMatrix[-1][-1] = 1
                        boxVis.set_transform(
                            tf.translation_matrix(link.absoluteBase)
                            @ rotationMatrix)

        else:
            for i in range(int(vertices.shape[0] / 2)):
                p1 = vertices[2 * i]
                p2 = vertices[2 * i + 1]

                cylinder_transform = getCylinderTransform(p1, p2)
                boxVis = vis["link" + str(i)]
                boxVis.set_object(g.Cylinder(1, 0.01))
                boxVis.set_transform(cylinder_transform)

            for i in range(len(states)):
                chain.update(states[i])
                with anim.at_frame(vis, framerate * i) as frame:
                    vertices = chain.get_vertex_coords()

                    for i in range(int(vertices.shape[0] / 2)):

                        p1 = vertices[2 * i]
                        p2 = vertices[2 * i + 1]

                        cylinder_transform = getCylinderTransform(p1, p2)
                        boxVis = frame["link" + str(i)]
                        boxVis.set_transform(cylinder_transform)

        vis.set_animation(anim)
コード例 #22
0
    def load(self, context=None):
        """
        Loads ``meshcat`` visualization elements.

        Precondition:
            Either the context is a valid Context for this system with the
            geometry_query port connected or the ``scene_graph`` passed in the
            constructor must be a valid SceneGraph.
        """
        if self._delete_prefix_on_load:
            self.vis[self.prefix].delete()

        if context and self.get_geometry_query_input_port().HasValue(context):
            inspector = self.get_geometry_query_input_port().Eval(
                context).inspector()
        elif self._scene_graph:
            inspector = self._scene_graph.model_inspector()
        else:
            raise RuntimeError(
                "You must provide a valid Context for this system with the "
                "geometry_query port connected or the ``scene_graph`` passed "
                "in the constructor must be a valid SceneGraph.")

        vis = self.vis[self.prefix]
        for frame_id in inspector.all_frame_ids():
            count = inspector.NumGeometriesForFrameWithRole(
                frame_id, Role.kIllustration)
            if count == 0:
                continue
            if frame_id == inspector.world_frame_id():
                name = "world"
            else:
                # Note: MBP declares frames with SceneGraph using `::`, we
                # replace those with `/` here to expose the full tree to
                # meshcat.
                name = (inspector.GetOwningSourceName(frame_id) + "/" +
                        inspector.GetName(frame_id).replace("::", "/"))

            frame_vis = vis[name]
            for g_id in inspector.GetGeometries(frame_id, Role.kIllustration):
                color = 0xe5e5e5  # default color
                alpha = 1.0
                props = inspector.GetIllustrationProperties(g_id)
                if props and props.HasProperty("phong", "diffuse"):
                    rgba = props.GetProperty("phong", "diffuse")
                    # Convert Rgba from [0-1] to hex 0xRRGGBB.
                    color = int(255 * rgba.r()) * 256**2
                    color += int(255 * rgba.g()) * 256
                    color += int(255 * rgba.b())
                    alpha = rgba.a()

                material = g.MeshLambertMaterial(color=color,
                                                 transparent=alpha != 1.,
                                                 opacity=alpha)

                shape = inspector.GetShape(g_id)
                X_FG = inspector.GetPoseInFrame(g_id).GetAsMatrix4()
                if isinstance(shape, Box):
                    geom = g.Box(
                        [shape.width(),
                         shape.depth(),
                         shape.height()])
                elif isinstance(shape, Sphere):
                    geom = g.Sphere(shape.radius())
                elif isinstance(shape, Cylinder):
                    geom = g.Cylinder(shape.length(), shape.radius())
                    # In Drake, cylinders are along +z
                    # In meshcat, cylinders are along +y

                    R_GC = RotationMatrix.MakeXRotation(np.pi / 2.0).matrix()
                    X_FG[0:3, 0:3] = X_FG[0:3, 0:3].dot(R_GC)
                elif isinstance(shape, Mesh):
                    geom = g.ObjMeshGeometry.from_file(shape.filename()[0:-3] +
                                                       "obj")
                    # Attempt to find a texture for the object by looking for
                    # an identically-named *.png next to the model.
                    # TODO(gizatt): Support .MTLs and prefer them over png,
                    # since they're both more expressive and more standard.
                    # TODO(gizatt): In the long term, this kind of material
                    # information should be gleaned from the SceneGraph
                    # constituents themselves, so that we visualize what the
                    # simulation is *actually* reasoning about rather than what
                    # files happen to be present.
                    candidate_texture_path = shape.filename()[0:-3] + "png"
                    if os.path.exists(candidate_texture_path):
                        material = g.MeshLambertMaterial(map=g.ImageTexture(
                            image=g.PngImage.from_file(
                                candidate_texture_path)))
                    # Make the uuid's deterministic for mesh geometry, to
                    # support caching at the zmqserver.  This means that
                    # multiple (identical) geometries may have the same UUID,
                    # but testing suggests that meshcat + three.js are ok with
                    # it.
                    geom.uuid = str(
                        uuid.uuid5(uuid.NAMESPACE_X500,
                                   geom.contents + "mesh"))
                    material.uuid = str(
                        uuid.uuid5(uuid.NAMESPACE_X500,
                                   geom.contents + "material"))
                    X_FG = X_FG.dot(tf.scale_matrix(shape.scale()))
                else:
                    warnings.warn(f"Unsupported shape {shape} ignored")
                    continue
                geometry_vis = frame_vis[str(g_id.get_value())]
                geometry_vis.set_object(geom, material)
                geometry_vis.set_transform(X_FG)

            if frame_id != inspector.world_frame_id():
                self._dynamic_frames.append({
                    "id": frame_id,
                    "name": name,
                })
コード例 #23
0
ファイル: ray-cylinder.py プロジェクト: tnakaicode/RayTrace
    (-0.9082895433880116, 0.41834209132239775, -0.2183420913223977),
    (0.5, 0.7, -0.5)
)
surface_and_bottom = (ray_origin, ray_direction, expected)
# surface and top
ray_origin = (-2, 0.2, 0.0)
ray_direction = norm((1.0, 0.2, 0.2))
expected = (
    (-0.9082895433880116, 0.41834209132239775, 0.2183420913223977),
    (0.5, 0.7, 0.5)
)
surface_and_top = (ray_origin, ray_direction, expected)
tests = (touching, end_caps_only, surface_and_bottom, surface_and_top)

# Place cylinder in scene
material = g.MeshLambertMaterial(reflectivity=1.0, sides=0)
vis['cyl'].set_object(
    g.Cylinder(length, radius),
    material)
vis['cyl'].set_transform(
    tf.translation_matrix([0.0, 0.0, 0.0]).dot(
        tf.rotation_matrix(np.radians(-90), [1, 0, 0]))
)

# Visualise test rays
for idx, (ray_origin, ray_direction, expected) in enumerate(tests):
    ray_inf = np.array(ray_origin) + 5.0 * np.array(ray_direction)
    vertices = np.column_stack((ray_origin, ray_inf))

    red_material = g.MeshLambertMaterial(
        reflectivity=1.0, sides=0, color=0xff0000)
コード例 #24
0
    def load(self, context=None):
        """
        Loads ``meshcat`` visualization elements.

        Precondition:
            Either the context is a valid Context for this system with the
            geometry_query port connected or the ``scene_graph`` passed in the
            constructor must be a valid SceneGraph.
        """
        if self._delete_prefix_on_load:
            self.vis[self.prefix].delete()

        if context and self.get_geometry_query_input_port().HasValue(context):
            inspector = self.get_geometry_query_input_port().Eval(
                context).inspector()
        elif self._scene_graph:
            inspector = self._scene_graph.model_inspector()
        else:
            raise RuntimeError(
                "You must provide a valid Context for this system with the "
                "geometry_query port connected or the ``scene_graph`` passed "
                "in the constructor must be a valid SceneGraph.")

        vis = self.vis[self.prefix]
        # Make a fixed-seed generator for random colors for bodies.
        color_generator = np.random.RandomState(seed=42)
        for frame_id in inspector.GetAllFrameIds():
            count = inspector.NumGeometriesForFrameWithRole(
                frame_id, self._role)
            if count == 0:
                continue
            if frame_id == inspector.world_frame_id():
                name = "world"
            else:
                # Note: MBP declares frames with SceneGraph using `::`, we
                # replace those with `/` here to expose the full tree to
                # meshcat.
                name = (inspector.GetOwningSourceName(frame_id) + "/" +
                        inspector.GetName(frame_id).replace("::", "/"))

            frame_vis = vis[name]
            for g_id in inspector.GetGeometries(frame_id, self._role):
                color = 0xe5e5e5  # default color
                alpha = 1.0
                hydro_mesh = None
                if self._role == Role.kIllustration:
                    props = inspector.GetIllustrationProperties(g_id)
                    if props and props.HasProperty("phong", "diffuse"):
                        rgba = props.GetProperty("phong", "diffuse")
                        # Convert Rgba from [0-1] to hex 0xRRGGBB.
                        color = int(255 * rgba.r()) * 256**2
                        color += int(255 * rgba.g()) * 256
                        color += int(255 * rgba.b())
                        alpha = rgba.a()
                elif self._role == Role.kProximity:
                    # Pick a random color to make collision geometry
                    # visually distinguishable.
                    color = color_generator.randint(2**(24))
                    if self._prefer_hydro:
                        hydro_mesh = inspector. \
                            maybe_get_hydroelastic_mesh(g_id)

                material = g.MeshLambertMaterial(color=color,
                                                 transparent=alpha != 1.,
                                                 opacity=alpha)

                shape = inspector.GetShape(g_id)
                X_FG = inspector.GetPoseInFrame(g_id).GetAsMatrix4()
                if hydro_mesh is not None:
                    # We've got a hydroelastic mesh to load.
                    surface_mesh = hydro_mesh
                    if isinstance(hydro_mesh, VolumeMesh):
                        surface_mesh = ConvertVolumeToSurfaceMesh(hydro_mesh)
                    v_count = len(surface_mesh.triangles()) * 3
                    vertices = np.empty((v_count, 3), dtype=float)
                    normals = np.empty((v_count, 3), dtype=float)

                    mesh_verts = surface_mesh.vertices()
                    v = 0
                    for face in surface_mesh.triangles():
                        p_MA = mesh_verts[int(face.vertex(0))]
                        p_MB = mesh_verts[int(face.vertex(1))]
                        p_MC = mesh_verts[int(face.vertex(2))]
                        vertices[v, :] = tuple(p_MA)
                        vertices[v + 1, :] = tuple(p_MB)
                        vertices[v + 2, :] = tuple(p_MC)

                        p_AB_M = p_MB - p_MA
                        p_AC_M = p_MC - p_MA
                        n_M = np.cross(p_AB_M, p_AC_M)
                        nhat_M = n_M / np.sqrt(n_M.dot(n_M))

                        normals[v, :] = nhat_M
                        normals[v + 1, :] = nhat_M
                        normals[v + 2, :] = nhat_M

                        v += 3
                    geom = HydroTriSurface(vertices, normals)
                elif isinstance(shape, Box):
                    geom = g.Box(
                        [shape.width(),
                         shape.depth(),
                         shape.height()])
                elif isinstance(shape, Sphere):
                    geom = g.Sphere(shape.radius())
                elif isinstance(shape, Cylinder):
                    geom = g.Cylinder(shape.length(), shape.radius())
                    # In Drake, cylinders are along +z
                    # In meshcat, cylinders are along +y

                    R_GC = RotationMatrix.MakeXRotation(np.pi / 2.0).matrix()
                    X_FG[0:3, 0:3] = X_FG[0:3, 0:3].dot(R_GC)
                elif isinstance(shape, (Mesh, Convex)):
                    geom = g.ObjMeshGeometry.from_file(shape.filename()[0:-3] +
                                                       "obj")
                    # Attempt to find a texture for the object by looking for
                    # an identically-named *.png next to the model.
                    # TODO(gizatt): Support .MTLs and prefer them over png,
                    # since they're both more expressive and more standard.
                    # TODO(gizatt): In the long term, this kind of material
                    # information should be gleaned from the SceneGraph
                    # constituents themselves, so that we visualize what the
                    # simulation is *actually* reasoning about rather than what
                    # files happen to be present.
                    candidate_texture_path = shape.filename()[0:-3] + "png"
                    if os.path.exists(candidate_texture_path):
                        material = g.MeshLambertMaterial(map=g.ImageTexture(
                            image=g.PngImage.from_file(
                                candidate_texture_path)))
                    # Make the uuid's deterministic for mesh geometry, to
                    # support caching at the zmqserver.  This means that
                    # multiple (identical) geometries may have the same UUID,
                    # but testing suggests that meshcat + three.js are ok with
                    # it.
                    geom.uuid = str(
                        uuid.uuid5(uuid.NAMESPACE_X500,
                                   geom.contents + "mesh"))
                    material.uuid = str(
                        uuid.uuid5(uuid.NAMESPACE_X500,
                                   geom.contents + "material"))
                    X_FG = X_FG.dot(tf.scale_matrix(shape.scale()))
                else:
                    warnings.warn(f"Unsupported shape {shape} ignored")
                    continue
                geometry_vis = frame_vis[str(g_id.get_value())]
                geometry_vis.set_object(geom, material)
                geometry_vis.set_transform(X_FG)

                if frame_id in self.frames_to_draw:
                    AddTriad(self.vis,
                             name=name,
                             prefix=self.prefix + "/" + name,
                             length=self.axis_length,
                             radius=self.axis_radius,
                             opacity=self.frames_opacity)
                    self.frames_to_draw.remove(frame_id)

            if frame_id != inspector.world_frame_id():
                self._dynamic_frames.append({
                    "id": frame_id,
                    "name": name,
                })

        # Loop through the input frames_to_draw list and warn the user if the
        # frame_id does not exist in the scene graph.
        for frame_id in self.frames_to_draw:
            warnings.warn(f"Non-existent frame {frame_id} ignored")
            continue
コード例 #25
0
def draw_tree(tree, vis, prefix="", draw_regions=False):
    # Given a scene tree (nx.DiGraph), draw it in the
    # specified meshcat visualizer.
    
    # Draw the scene geometry flat, to keep TFs easy.
    name_prefix = prefix + "scene"
    vis[name_prefix].delete()
    k = 0
    for node in tree.nodes:
        name = name_prefix + "/%s_%03d" % (node.__class__.__name__, k)
        if node.geometry is not None:
            color = node.geometry_color
            alpha = 1.0
            vis[name].set_object(
                node.geometry,
                meshcat_geom.MeshLambertMaterial(color=color, opacity=alpha, transparent=(alpha != 1.))
            )
            tf = node.tf.GetAsMatrix4()
            geom_tf = node.geometry_tf.GetAsMatrix4()
            tf = tf.dot(geom_tf)
            tf[:3, :3] = tf[:3, :3].dot(np.diag(node.geometry_scale))
            print(tf)
            vis[name].set_transform(tf)
            k += 1
    
    # Draw the tree structure.
    tree_prefix = prefix + "tree"
    vis[tree_prefix].delete()
    k = 0
    for node in tree.nodes:
        name = tree_prefix + "/" + node.__class__.__name__ + "_%03d" % k
        k += 1
        # Draw node as randomly colored sphere
        color = random.randint(0, 0xFFFFFF)
        alpha = 0.5
        vis[name]["triad"].set_object(
            meshcat_geom.triad(scale=0.1)
        )
        vis[name]["sphere"].set_object(
            meshcat_geom.Sphere(0.01),
            meshcat_geom.MeshToonMaterial(color=color, opacity=alpha, transparent=(alpha != 1.))
        )
        vis[name].set_transform(node.tf.GetAsMatrix4())
        # Draw children
        verts = []
        for child in tree.successors(node):
            # Draw link to child
            verts.append(node.tf.translation()),
            verts.append(child.tf.translation())
        if len(verts) > 0:
            verts = np.vstack(verts).T
            # Don't want this as a direct child or it'll inherit the transform
            vis[name + "_child_connections"].set_object(
                meshcat_geom.Line(meshcat_geom.PointsGeometry(verts),
                                  meshcat_geom.LineBasicMaterial(linewidth=50, color=color)))
        
        if draw_regions:
            # Draw the child regions for each child
            if isinstance(node, (AndNode, OrNode, RepeatingSetNode)):
                for info_k, child_info in enumerate(node.child_infos):
                    region_name = "child_region_%03d" % info_k
                    lb = child_info.child_xyz_bounds.xyz_min
                    ub = child_info.child_xyz_bounds.xyz_max
                    vis[name][region_name].set_object(
                        meshcat_geom.Box(ub - lb),
                        meshcat_geom.MeshToonMaterial(color=0x111111, opacity=0.1, transparent=True)
                    )
                    tf = RigidTransform(p=(ub+lb)/2)
                    vis[name][region_name].set_transform(tf.GetAsMatrix4())