def test_rgba_api(self): r, g, b, a = 0.75, 0.5, 0.25, 1. color = mut.Rgba(r=r, g=g, b=b) self.assertEqual(color.r(), r) self.assertEqual(color.g(), g) self.assertEqual(color.b(), b) self.assertEqual(color.a(), a) self.assertEqual(color, mut.Rgba(r, g, b, a)) self.assertNotEqual(color, mut.Rgba(r, g, b, 0.)) self.assertEqual(repr(color), "Rgba(r=0.75, g=0.5, b=0.25, a=1.0)") color.set(r=1., g=1., b=1., a=0.) self.assertEqual(color, mut.Rgba(1., 1., 1., 0.))
def test_meshcat_visualizer(self, T): meshcat = mut.Meshcat() params = mut.MeshcatVisualizerParams() params.publish_period = 0.123 params.role = mut.Role.kIllustration params.default_color = mut.Rgba(0.5, 0.5, 0.5) params.prefix = "py_visualizer" params.delete_on_initialization_event = False self.assertNotIn("object at 0x", repr(params)) vis = mut.MeshcatVisualizer_[T](meshcat=meshcat, params=params) vis.Delete() self.assertIsInstance(vis.query_object_input_port(), InputPort_[T]) animation = vis.StartRecording(set_transforms_while_recording=True) self.assertIsInstance(animation, mut.MeshcatAnimation) self.assertEqual(animation, vis.get_mutable_recording()) vis.StopRecording() vis.PublishRecording() vis.DeleteRecording() builder = DiagramBuilder_[T]() scene_graph = builder.AddSystem(mut.SceneGraph_[T]()) mut.MeshcatVisualizer_[T].AddToBuilder(builder=builder, scene_graph=scene_graph, meshcat=meshcat, params=params) mut.MeshcatVisualizer_[T].AddToBuilder( builder=builder, query_object_port=scene_graph.get_query_output_port(), meshcat=meshcat, params=params)
def test_geometry_properties_api(self): # Test perception/ illustration properties (specifically Rgba). test_vector = [0., 0., 1., 1.] test_color = mut.Rgba(0., 0., 1., 1.) phong_props = mut.MakePhongIllustrationProperties(test_vector) self.assertIsInstance(phong_props, mut.IllustrationProperties) actual_color = phong_props.GetProperty("phong", "diffuse") self.assertEqual(actual_color, test_color) # Ensure that we can create it manually. phong_props = mut.IllustrationProperties() phong_props.AddProperty("phong", "diffuse", test_color) actual_color = phong_props.GetProperty("phong", "diffuse") self.assertEqual(actual_color, test_color) # Test proximity properties. prop = mut.ProximityProperties() self.assertEqual(str(prop), "[__default__]") default_group = prop.default_group_name() self.assertTrue(prop.HasGroup(group_name=default_group)) self.assertEqual(prop.num_groups(), 1) self.assertTrue(default_group in prop.GetGroupNames()) prop.AddProperty(group_name=default_group, name="test", value=3) self.assertTrue(prop.HasProperty(group_name=default_group, name="test")) self.assertEqual( prop.GetProperty(group_name=default_group, name="test"), 3) self.assertEqual( prop.GetPropertyOrDefault(group_name=default_group, name="empty", default_value=5), 5) group_values = prop.GetPropertiesInGroup(group_name=default_group) for name, value in group_values.items(): self.assertIsInstance(name, str) self.assertIsInstance(value, AbstractValue)
def test_render_engine_gl_params(self): # A default constructor exists. mut.render.RenderEngineGlParams() # The kwarg constructor also works. label = mut.render.RenderLabel(10) diffuse = mut.Rgba(1.0, 0.0, 0.0, 0.0) params = mut.render.RenderEngineGlParams( default_clear_color=diffuse, default_label=label, default_diffuse=diffuse, ) self.assertEqual(params.default_clear_color, diffuse) self.assertEqual(params.default_label, label) self.assertEqual(params.default_diffuse, diffuse)
def test_meshcat_point_cloud_visualizer(self, T): meshcat = mut.Meshcat() visualizer = mut.MeshcatPointCloudVisualizer_[T]( meshcat=meshcat, path="cloud", publish_period=1/12.0) visualizer.set_point_size(0.1) visualizer.set_default_rgba(mut.Rgba(0, 0, 1, 1)) context = visualizer.CreateDefaultContext() cloud = PointCloud(4) cloud.mutable_xyzs()[:] = np.zeros((3, 4)) visualizer.cloud_input_port().FixValue( context, AbstractValue.Make(cloud)) self.assertIsInstance(visualizer.pose_input_port(), InputPort_[T]) visualizer.Publish(context) visualizer.Delete() if T == float: ad_visualizer = visualizer.ToAutoDiffXd() self.assertIsInstance( ad_visualizer, mut.MeshcatPointCloudVisualizer_[AutoDiffXd])
def test_rgba_api(self): default_white = mut.Rgba() self.assertEqual(default_white, mut.Rgba(1, 1, 1, 1)) r, g, b, a = 0.75, 0.5, 0.25, 1. color = mut.Rgba(r=r, g=g, b=b) self.assertEqual(color.r(), r) self.assertEqual(color.g(), g) self.assertEqual(color.b(), b) self.assertEqual(color.a(), a) self.assertEqual(color, mut.Rgba(r, g, b, a)) self.assertNotEqual(color, mut.Rgba(r, g, b, 0.)) self.assertEqual(repr(color), "Rgba(r=0.75, g=0.5, b=0.25, a=1.0)") color.set(r=1., g=1., b=1., a=0.) self.assertEqual(color, mut.Rgba(1., 1., 1., 0.)) # Confirm value instantiation. Value[mut.Rgba]
def test_geometry_properties_api(self): # Test perception/ illustration properties (specifically Rgba). test_vector = [0., 0., 1., 1.] test_color = mut.Rgba(0., 0., 1., 1.) phong_props = mut.MakePhongIllustrationProperties(test_vector) self.assertIsInstance(phong_props, mut.IllustrationProperties) actual_color = phong_props.GetProperty("phong", "diffuse") self.assertEqual(actual_color, test_color) # Ensure that we can create it manually. phong_props = mut.IllustrationProperties() phong_props.AddProperty("phong", "diffuse", test_color) actual_color = phong_props.GetProperty("phong", "diffuse") self.assertEqual(actual_color, test_color) # Test proximity properties. prop = mut.ProximityProperties() self.assertEqual(str(prop), "[__default__]") default_group = prop.default_group_name() self.assertTrue(prop.HasGroup(group_name=default_group)) self.assertEqual(prop.num_groups(), 1) self.assertTrue(default_group in prop.GetGroupNames()) prop.AddProperty(group_name=default_group, name="test", value=3) self.assertTrue(prop.HasProperty(group_name=default_group, name="test")) self.assertEqual( prop.GetProperty(group_name=default_group, name="test"), 3) self.assertEqual( prop.GetPropertyOrDefault( group_name=default_group, name="empty", default_value=5), 5) group_values = prop.GetPropertiesInGroup(group_name=default_group) for name, value in group_values.items(): self.assertIsInstance(name, str) self.assertIsInstance(value, AbstractValue) # Remove the property. self.assertTrue(prop.RemoveProperty(group_name=default_group, name="test")) self.assertFalse(prop.HasProperty(group_name=default_group, name="test")) # Update a property. prop.AddProperty(group_name=default_group, name="to_update", value=17) self.assertTrue(prop.HasProperty(group_name=default_group, name="to_update")) self.assertEqual( prop.GetProperty(group_name=default_group, name="to_update"), 17) prop.UpdateProperty(group_name=default_group, name="to_update", value=20) self.assertTrue(prop.HasProperty(group_name=default_group, name="to_update")) self.assertEqual( prop.GetProperty(group_name=default_group, name="to_update"), 20) # Property copying. for PropertyType in [mut.ProximityProperties, mut.IllustrationProperties, mut.PerceptionProperties]: props = PropertyType() props.AddProperty("g", "p", 10) self.assertTrue(props.HasProperty("g", "p")) props_copy = PropertyType(other=props) self.assertTrue(props_copy.HasProperty("g", "p")) props_copy2 = copy.copy(props) self.assertTrue(props_copy2.HasProperty("g", "p")) props_copy3 = copy.deepcopy(props) self.assertTrue(props_copy3.HasProperty("g", "p"))
def test_drake_visualizer(self, T): # Test visualization API. SceneGraph = mut.SceneGraph_[T] DiagramBuilder = DiagramBuilder_[T] Simulator = Simulator_[T] lcm = DrakeLcm() role = mut.Role.kIllustration params = mut.DrakeVisualizerParams( publish_period=0.1, role=mut.Role.kIllustration, default_color=mut.Rgba(0.1, 0.2, 0.3, 0.4)) self.assertEqual(repr(params), "".join([ "DrakeVisualizerParams(" "publish_period=0.1, " "role=Role.kIllustration, " "default_color=Rgba(r=0.1, g=0.2, b=0.3, a=0.4))"])) # Add some subscribers to detect message broadcast. load_channel = "DRAKE_VIEWER_LOAD_ROBOT" draw_channel = "DRAKE_VIEWER_DRAW" load_subscriber = Subscriber( lcm, load_channel, lcmt_viewer_load_robot) draw_subscriber = Subscriber( lcm, draw_channel, lcmt_viewer_draw) # There are three ways to configure DrakeVisualizer. def by_hand(builder, scene_graph, params): visualizer = builder.AddSystem( mut.DrakeVisualizer_[T](lcm=lcm, params=params)) builder.Connect(scene_graph.get_query_output_port(), visualizer.query_object_input_port()) def auto_connect_to_system(builder, scene_graph, params): mut.DrakeVisualizer_[T].AddToBuilder(builder=builder, scene_graph=scene_graph, lcm=lcm, params=params) def auto_connect_to_port(builder, scene_graph, params): mut.DrakeVisualizer_[T].AddToBuilder( builder=builder, query_object_port=scene_graph.get_query_output_port(), lcm=lcm, params=params) for func in [by_hand, auto_connect_to_system, auto_connect_to_port]: # Build the diagram. builder = DiagramBuilder() scene_graph = builder.AddSystem(SceneGraph()) func(builder, scene_graph, params) # Simulate to t = 0 to send initial load and draw messages. diagram = builder.Build() Simulator(diagram).AdvanceTo(0) lcm.HandleSubscriptions(0) self.assertEqual(load_subscriber.count, 1) self.assertEqual(draw_subscriber.count, 1) load_subscriber.clear() draw_subscriber.clear() # Ad hoc broadcasting. scene_graph = SceneGraph() mut.DrakeVisualizer_[T].DispatchLoadMessage( scene_graph, lcm, params) lcm.HandleSubscriptions(0) self.assertEqual(load_subscriber.count, 1) self.assertEqual(draw_subscriber.count, 0) load_subscriber.clear() draw_subscriber.clear()
def test_meshcat(self): port = 7051 params = mut.MeshcatParams(host="*", port=port, web_url_pattern="http://host:{port}") meshcat = mut.Meshcat(params=params) self.assertEqual(meshcat.port(), port) with self.assertRaises(RuntimeError): meshcat2 = mut.Meshcat(port=port) self.assertIn("http", meshcat.web_url()) self.assertIn("ws", meshcat.ws_url()) meshcat.SetObject(path="/test/box", shape=mut.Box(1, 1, 1), rgba=mut.Rgba(.5, .5, .5)) meshcat.SetTransform(path="/test/box", X_ParentPath=RigidTransform()) meshcat.SetTransform(path="/test/box", matrix=np.eye(4)) self.assertTrue(meshcat.HasPath("/test/box")) cloud = PointCloud(4) cloud.mutable_xyzs()[:] = np.zeros((3, 4)) meshcat.SetObject(path="/test/cloud", cloud=cloud, point_size=0.01, rgba=mut.Rgba(.5, .5, .5)) mesh = mut.TriangleSurfaceMesh(triangles=[ mut.SurfaceTriangle(0, 1, 2), mut.SurfaceTriangle(3, 0, 2) ], vertices=[[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]) meshcat.SetObject(path="/test/triangle_surface_mesh", mesh=mesh, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetLine(path="/test/line", vertices=np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetLineSegments(path="/test/line_segments", start=np.eye(3), end=2 * np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetTriangleMesh(path="/test/triangle_mesh", vertices=np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]).T, faces=np.array([[0, 1, 2], [3, 0, 2]]).T, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetProperty(path="/Background", property="visible", value=True) meshcat.SetProperty(path="/Lights/DirectionalLight/<object>", property="intensity", value=1.0) meshcat.SetProperty(path="/Background", property="top_color", value=[0, 0, 0]) meshcat.Set2dRenderMode(X_WC=RigidTransform(), xmin=-1, xmax=1, ymin=-1, ymax=1) meshcat.ResetRenderMode() meshcat.AddButton(name="button") self.assertEqual(meshcat.GetButtonClicks(name="button"), 0) meshcat.DeleteButton(name="button") meshcat.AddSlider(name="slider", min=0, max=1, step=0.01, value=0.5) meshcat.SetSliderValue(name="slider", value=0.7) self.assertAlmostEqual(meshcat.GetSliderValue(name="slider"), 0.7, delta=1e-14) meshcat.DeleteSlider(name="slider") meshcat.DeleteAddedControls() self.assertIn("data:application/octet-binary;base64", meshcat.StaticHtml()) meshcat.Flush() # PerspectiveCamera camera = mut.Meshcat.PerspectiveCamera(fov=80, aspect=1.2, near=0.2, far=200, zoom=1.3) self.assertEqual(camera.fov, 80) self.assertEqual(camera.aspect, 1.2) self.assertEqual(camera.near, 0.2) self.assertEqual(camera.far, 200) self.assertEqual(camera.zoom, 1.3) self.assertEqual( repr(camera), "".join([ r"PerspectiveCamera(", r"fov=80.0, " r"aspect=1.2, ", r"near=0.2, ", r"far=200.0, ", r"zoom=1.3)" ])) meshcat.SetCamera(camera=camera, path="mypath") # OrthographicCamera camera = mut.Meshcat.OrthographicCamera(left=0.1, right=1.3, top=0.3, bottom=1.4, near=0.2, far=200, zoom=1.3) self.assertEqual(camera.left, 0.1) self.assertEqual(camera.right, 1.3) self.assertEqual(camera.top, 0.3) self.assertEqual(camera.bottom, 1.4) self.assertEqual(camera.near, 0.2) self.assertEqual(camera.far, 200) self.assertEqual(camera.zoom, 1.3) self.assertEqual( repr(camera), "".join([ r"OrthographicCamera(", r"left=0.1, " r"right=1.3, ", r"top=0.3, " r"bottom=1.4, ", r"near=0.2, ", r"far=200.0, ", r"zoom=1.3)" ])) meshcat.SetCamera(camera=camera, path="mypath")
def test_meshcat(self): meshcat = mut.Meshcat(port=7051) self.assertEqual(meshcat.port(), 7051) with self.assertRaises(RuntimeError): meshcat2 = mut.Meshcat(port=7051) self.assertIn("http", meshcat.web_url()) self.assertIn("ws", meshcat.ws_url()) meshcat.SetObject(path="/test/box", shape=mut.Box(1, 1, 1), rgba=mut.Rgba(.5, .5, .5)) meshcat.SetTransform(path="/test/box", X_ParentPath=RigidTransform()) meshcat.SetTransform(path="/test/box", matrix=np.eye(4)) self.assertTrue(meshcat.HasPath("/test/box")) cloud = PointCloud(4) cloud.mutable_xyzs()[:] = np.zeros((3, 4)) meshcat.SetObject(path="/test/cloud", cloud=cloud, point_size=0.01, rgba=mut.Rgba(.5, .5, .5)) mesh = mut.TriangleSurfaceMesh(triangles=[ mut.SurfaceTriangle(0, 1, 2), mut.SurfaceTriangle(3, 0, 2) ], vertices=[[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]) meshcat.SetObject(path="/test/triangle_surface_mesh", mesh=mesh, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetLine(path="/test/line", vertices=np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetLineSegments(path="/test/line_segments", start=np.eye(3), end=2 * np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetTriangleMesh(path="/test/triangle_mesh", vertices=np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]).T, faces=np.array([[0, 1, 2], [3, 0, 2]]).T, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetProperty(path="/Background", property="visible", value=True) meshcat.SetProperty(path="/Lights/DirectionalLight/<object>", property="intensity", value=1.0) meshcat.SetProperty(path="/Background", property="top_color", value=[0, 0, 0]) meshcat.Set2dRenderMode(X_WC=RigidTransform(), xmin=-1, xmax=1, ymin=-1, ymax=1) meshcat.ResetRenderMode() meshcat.AddButton(name="button") self.assertEqual(meshcat.GetButtonClicks(name="button"), 0) meshcat.DeleteButton(name="button") meshcat.AddSlider(name="slider", min=0, max=1, step=0.01, value=0.5) meshcat.SetSliderValue(name="slider", value=0.7) self.assertAlmostEqual(meshcat.GetSliderValue(name="slider"), 0.7, delta=1e-14) meshcat.DeleteSlider(name="slider") meshcat.DeleteAddedControls() self.assertIn("data:application/octet-binary;base64", meshcat.StaticHtml()) meshcat.Flush()