Example #1
0
    def test_triangle_surface_mesh(self):
        # Create a mesh out of two triangles forming a quad.
        #
        #     0______1
        #      |b  /|      Two triangles: a and b.
        #      |  / |      Four vertices: 0, 1, 2, and 3.
        #      | /a |
        #      |/___|
        #     2      3

        t_a = mut.SurfaceTriangle(v0=3, v1=1, v2=2)
        t_b = mut.SurfaceTriangle(v0=2, v1=1, v2=0)
        self.assertEqual(t_a.vertex(0), 3)
        self.assertEqual(t_b.vertex(1), 1)

        v0 = (-1, 1, 0)
        v1 = (1, 1, 0)
        v2 = (-1, -1, 0)
        v3 = (1, -1, 0)

        self.assertListEqual(list(v0), [-1, 1, 0])

        mesh = mut.TriangleSurfaceMesh(triangles=(t_a, t_b),
                                       vertices=(v0, v1, v2, v3))
        self.assertEqual(len(mesh.triangles()), 2)
        self.assertEqual(len(mesh.vertices()), 4)
        self.assertListEqual(list(mesh.centroid()), [0, 0, 0])
        self.assertListEqual(list(mesh.element_centroid(t=1)),
                             [-1 / 3.0, 1 / 3.0, 0])
    def test_triangle_surface_mesh(self):
        # Create a mesh out of two triangles forming a quad.
        #
        #     0______1
        #      |b  /|      Two triangles: a and b.
        #      |  / |      Four vertices: 0, 1, 2, and 3.
        #      | /a |
        #      |/___|
        #     2      3

        t_a = mut.SurfaceTriangle(v0=3, v1=1, v2=2)
        t_b = mut.SurfaceTriangle(v0=2, v1=1, v2=0)
        self.assertEqual(t_a.vertex(0), 3)
        self.assertEqual(t_b.vertex(1), 1)

        v0 = (-1, 1, 0)
        v1 = (1, 1, 0)
        v2 = (-1, -1, 0)
        v3 = (1, -1, 0)

        self.assertListEqual(list(v0), [-1, 1, 0])

        dut = mut.TriangleSurfaceMesh(triangles=(t_a, t_b),
                                      vertices=(v0, v1, v2, v3))

        # Sanity check every accessor.
        self.assertIsInstance(dut.element(e=0), mut.SurfaceTriangle)
        self.assertIsInstance(dut.vertex(v=0), np.ndarray)
        self.assertIsInstance(dut.num_vertices(), int)
        self.assertIsInstance(dut.num_elements(), int)
        self.assertIsInstance(dut.num_triangles(), int)
        self.assertIsInstance(dut.area(t=0), float)
        self.assertIsInstance(dut.total_area(), float)
        self.assertIsInstance(dut.face_normal(t=0), np.ndarray)
        self.assertIsInstance(dut.element_centroid(t=0), np.ndarray)
        self.assertIsInstance(dut.centroid(), np.ndarray)

        # Sanity check some calculations
        self.assertIsInstance(dut.CalcBoundingBox(), tuple)
        self.assertTrue(dut.Equal(mesh=dut))
        self.assertIsInstance(
            dut.CalcCartesianFromBarycentric(element_index=1,
                                             b_Q=[1 / 3.0, 1 / 3.0, 1 / 3.0]),
            np.ndarray)
        self.assertIsInstance(
            dut.CalcBarycentric(p_MQ=[-1 / 3.0, 1 / 3.0, 0], t=1), np.ndarray)

        self.assertEqual(len(dut.triangles()), 2)
        self.assertEqual(len(dut.vertices()), 4)
        self.assertListEqual(list(dut.centroid()), [0, 0, 0])
        self.assertListEqual(list(dut.element_centroid(t=1)),
                             [-1 / 3.0, 1 / 3.0, 0])

        # Now check the SurfaceTriangle bindings.
        triangle0 = dut.element(e=0)
        self.assertEqual(triangle0.num_vertices(), 3)
        self.assertEqual(triangle0.vertex(i=0), 3)
Example #3
0
    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")
Example #4
0
 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()