Ejemplo n.º 1
0
    def testCreation(self):
        vertices = np.array([[1, 1, 0], [1, 0, 0], [0, 1, 0]])
        indices = np.array([1, 0, 2])
        normals = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        mesh = Mesh(vertices, indices, normals)

        np.testing.assert_array_almost_equal(mesh.vertices,
                                             vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals, normals, decimal=5)
        np.testing.assert_array_equal(mesh.indices, [1, 0, 2])

        mesh = Mesh(vertices, indices, normals, clean=True)
        expected = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])

        np.testing.assert_array_almost_equal(mesh.vertices,
                                             vertices[[2, 1, 0]],
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals, expected, decimal=5)
        np.testing.assert_array_equal(mesh.indices, [1, 2, 0])

        v = np.array([[np.nan, 1, 0], [1, 0, 0], [0, 1, 0]])
        self.assertRaises(ValueError, Mesh, v, indices, normals, clean=True)

        n = np.array([[0, 1, 0], [1, -np.inf, 0], [0, 1, 0]])
        self.assertRaises(ValueError, Mesh, vertices, indices, n)
Ejemplo n.º 2
0
    def setUp(self):
        vertices = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        normals = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        indices = np.array([2, 1, 0])
        self.mesh_1 = Mesh(vertices, indices, normals)

        vertices = np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]])
        normals = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
        indices = np.array([1, 0, 2, 0, 1, 2])
        self.mesh_2 = Mesh(vertices, indices, normals)
Ejemplo n.º 3
0
    def testMeshPlaneIntersection(self):
        np.array([[1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                  [1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])

        vertices = np.array([[1.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 0.0],
                             [0.0, 1.0, 0.0]])
        indices = np.array([0, 1, 2, 0, 2, 3])

        mesh = Mesh(vertices, indices)

        # plane is above geometry
        plane = Plane.fromCoefficient(1.0, 0.0, 0.0, 2.0)
        segments = mesh_plane_intersection(mesh, plane)
        self.assertEqual(len(segments), 0)

        # plane is intersects edge
        plane = Plane.fromCoefficient(1.0, 0.0, 0.0, -1.0)
        segments = mesh_plane_intersection(mesh, plane)
        self.assertEqual(len(segments), 2)

        # plane is intersects face
        plane = Plane.fromCoefficient(1.0, 0.0, 0.0, -0.5)
        segments = mesh_plane_intersection(mesh, plane)
        self.assertEqual(len(segments), 4)

        # plane is flush with face
        # This is currently expected to return nothing
        plane = Plane.fromCoefficient(0.0, 0.0, 1.0, 0.0)
        segments = mesh_plane_intersection(mesh, plane)
        self.assertEqual(len(segments), 0)
Ejemplo n.º 4
0
    def testComputeNormals(self):
        vertices = np.array([[1, 1, 0], [1, 0, 0], [0, 1, 0]])
        indices = np.array([1, 0, 2])
        mesh = Mesh(vertices, indices)

        expected = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])

        # Check that correct normals are generated also vertices and indices are unchanged
        np.testing.assert_array_almost_equal(mesh.vertices,
                                             vertices[[2, 1, 0]],
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals, expected, decimal=5)
        np.testing.assert_array_equal(mesh.indices, [1, 2, 0])
Ejemplo n.º 5
0
    def testReadAndWriteBinaryStl(self):
        vertices = np.array([[1, 2, 0], [4, 5, 0], [7, 28, 0]])
        normals = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
        indices = np.array([0, 1, 2])
        mesh_to_write = Mesh(vertices, indices, normals)
        full_path = os.path.join(self.test_dir, "test.stl")
        writer.write_binary_stl(full_path, mesh_to_write)

        mesh_read_from_file = reader.read_3d_model(full_path)
        np.testing.assert_array_almost_equal(mesh_to_write.vertices,
                                             mesh_read_from_file.vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh_to_write.normals,
                                             mesh_read_from_file.normals,
                                             decimal=5)
        np.testing.assert_array_equal(mesh_to_write.indices,
                                      mesh_read_from_file.indices)
Ejemplo n.º 6
0
    def setUp(self):
        self.model = MainWindowModel()
        self.instrument = mock.create_autospec(Instrument)
        self.instrument.detectors = []

        read_inst_function = self.createPatch(
            "sscanss.app.window.model.read_instrument_description_file")
        read_inst_function.return_value = self.instrument

        validate_inst_function = self.createPatch(
            "sscanss.app.window.model.validate_instrument_scene_size")
        validate_inst_function.return_value = True

        vertices = np.array([[0, 0, 1], [1, 0, 0], [1, 0, 1]])
        normals = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
        indices = np.array([0, 1, 2])
        self.mesh = Mesh(vertices, indices, normals)
        # Create a temporary directory
        self.test_dir = tempfile.mkdtemp()
Ejemplo n.º 7
0
    def testHDFReadWrite(self, visual_fn, setting_cls):

        visual_fn.return_value = Mesh(
            np.array([[0, 0, 0], [0, 1, 0], [0, 1, 1]]),
            np.array([0, 1, 2]),
            np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0]]),
        )
        filename = self.writeTestFile("instrument.json", SAMPLE_IDF)
        instrument = read_instrument_description_file(filename)
        data = {
            "name":
            "Test Project",
            "instrument":
            instrument,
            "instrument_version":
            "1.0",
            "sample": {},
            "fiducials":
            np.recarray((0, ), dtype=[("points", "f4", 3), ("enabled", "?")]),
            "measurement_points":
            np.recarray((0, ), dtype=[("points", "f4", 3), ("enabled", "?")]),
            "measurement_vectors":
            np.empty((0, 3, 1), dtype=np.float32),
            "alignment":
            None,
        }

        filename = os.path.join(self.test_dir, "test.h5")

        writer.write_project_hdf(data, filename)
        result, instrument = reader.read_project_hdf(filename)

        self.assertEqual(__version__, result["version"])
        self.assertEqual(data["instrument_version"],
                         result["instrument_version"])
        self.assertEqual(data["name"], result["name"],
                         "Save and Load data are not Equal")
        self.assertEqual(data["instrument"].name, result["instrument"],
                         "Save and Load data are not Equal")
        self.assertDictEqual(result["sample"], {})
        self.assertTrue(result["fiducials"][0].size == 0
                        and result["fiducials"][1].size == 0)
        self.assertTrue(result["measurement_points"][0].size == 0
                        and result["measurement_points"][1].size == 0)
        self.assertTrue(result["measurement_vectors"].size == 0)
        self.assertIsNone(result["alignment"])
        self.assertEqual(result["settings"], {})

        sample_key = "a mesh"
        vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
        normals = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
        indices = np.array([0, 1, 2])
        mesh_to_write = Mesh(vertices, indices, normals)
        fiducials = np.rec.array(
            [([11.0, 12.0, 13.0], False), ([14.0, 15.0, 16.0], True),
             ([17.0, 18.0, 19.0], False)],
            dtype=[("points", "f4", 3), ("enabled", "?")],
        )
        points = np.rec.array(
            [([1.0, 2.0, 3.0], True), ([4.0, 5.0, 6.0], False),
             ([7.0, 8.0, 9.0], True)],
            dtype=[("points", "f4", 3), ("enabled", "?")],
        )
        vectors = np.zeros((3, 3, 2))
        vectors[:, :, 0] = [
            [0.0000076, 1.0000000, 0.0000480],
            [0.0401899, 0.9659270, 0.2556752],
            [0.1506346, 0.2589932, 0.9540607],
        ]

        vectors[:, :, 1] = [
            [0.1553215, -0.0000486, 0.9878640],
            [0.1499936, -0.2588147, 0.9542100],
            [0.0403915, -0.9658791, 0.2558241],
        ]
        base = Matrix44(np.random.random((4, 4)))
        stack_name = "Positioning Table + Huber Circle"
        new_collimator = "Snout 100mm"
        jaw_aperture = [7.0, 5.0]

        data = {
            "name": "demo",
            "instrument": instrument,
            "instrument_version": "1.1",
            "sample": {
                sample_key: mesh_to_write
            },
            "fiducials": fiducials,
            "measurement_points": points,
            "measurement_vectors": vectors,
            "alignment": np.identity(4),
        }

        instrument.loadPositioningStack(stack_name)
        instrument.positioning_stack.fkine([200.0, 0.0, 0.0, np.pi, 0.0])
        instrument.positioning_stack.links[0].ignore_limits = True
        instrument.positioning_stack.links[4].locked = True
        aux = instrument.positioning_stack.auxiliary[0]
        instrument.positioning_stack.changeBaseMatrix(aux, base)

        instrument.jaws.aperture = jaw_aperture
        instrument.jaws.positioner.fkine([-600.0])
        instrument.jaws.positioner.links[0].ignore_limits = True
        instrument.jaws.positioner.links[0].locked = True

        instrument.detectors["Detector"].current_collimator = new_collimator
        instrument.detectors["Detector"].positioner.fkine([np.pi / 2, 100.0])
        instrument.detectors["Detector"].positioner.links[
            0].ignore_limits = True
        instrument.detectors["Detector"].positioner.links[1].locked = True

        setting_cls.local = {"num": 1, "str": "string", "colour": (1, 1, 1, 1)}

        writer.write_project_hdf(data, filename)
        result, instrument2 = reader.read_project_hdf(filename)
        self.assertEqual(__version__, result["version"])
        self.assertEqual(data["name"], result["name"],
                         "Save and Load data are not Equal")
        self.assertEqual(data["instrument_version"],
                         result["instrument_version"])
        self.assertEqual(data["instrument"].name, result["instrument"],
                         "Save and Load data are not Equal")
        self.assertTrue(sample_key in result["sample"])
        np.testing.assert_array_almost_equal(fiducials.points,
                                             result["fiducials"][0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(points.points,
                                             result["measurement_points"][0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(
            result["sample"][sample_key].vertices, vertices, decimal=5)
        np.testing.assert_array_almost_equal(
            result["sample"][sample_key].indices, indices, decimal=5)
        np.testing.assert_array_almost_equal(
            result["sample"][sample_key].normals, normals, decimal=5)
        np.testing.assert_array_almost_equal(fiducials.points,
                                             result["fiducials"][0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(points.points,
                                             result["measurement_points"][0],
                                             decimal=5)
        np.testing.assert_array_equal(fiducials.enabled,
                                      result["fiducials"][1])
        np.testing.assert_array_equal(points.enabled,
                                      result["measurement_points"][1])
        np.testing.assert_array_almost_equal(vectors,
                                             result["measurement_vectors"],
                                             decimal=5)
        np.testing.assert_array_almost_equal(result["alignment"],
                                             np.identity(4),
                                             decimal=5)
        setting = result["settings"]
        self.assertEqual(setting["num"], 1)
        self.assertEqual(setting["str"], "string")
        self.assertEqual(tuple(setting["colour"]), (1, 1, 1, 1))

        self.assertEqual(instrument.positioning_stack.name,
                         instrument2.positioning_stack.name)
        np.testing.assert_array_almost_equal(
            instrument.positioning_stack.configuration,
            instrument2.positioning_stack.configuration,
            decimal=5)
        for link1, link2 in zip(instrument.positioning_stack.links,
                                instrument2.positioning_stack.links):
            self.assertEqual(link1.ignore_limits, link2.ignore_limits)
            self.assertEqual(link1.locked, link2.locked)
        for aux1, aux2 in zip(instrument.positioning_stack.auxiliary,
                              instrument2.positioning_stack.auxiliary):
            np.testing.assert_array_almost_equal(aux1.base,
                                                 aux2.base,
                                                 decimal=5)

        np.testing.assert_array_almost_equal(instrument.jaws.aperture,
                                             instrument2.jaws.aperture,
                                             decimal=5)
        np.testing.assert_array_almost_equal(
            instrument.jaws.aperture_lower_limit,
            instrument2.jaws.aperture_lower_limit,
            decimal=5)
        np.testing.assert_array_almost_equal(
            instrument.jaws.aperture_upper_limit,
            instrument2.jaws.aperture_upper_limit,
            decimal=5)
        np.testing.assert_array_almost_equal(
            instrument.jaws.positioner.configuration,
            instrument2.jaws.positioner.configuration,
            decimal=5)
        for link1, link2 in zip(instrument.jaws.positioner.links,
                                instrument2.jaws.positioner.links):
            self.assertEqual(link1.ignore_limits, link2.ignore_limits)
            self.assertEqual(link1.locked, link2.locked)

        detector1 = instrument.detectors["Detector"]
        detector2 = instrument2.detectors["Detector"]
        self.assertEqual(detector1.current_collimator.name,
                         detector2.current_collimator.name)
        np.testing.assert_array_almost_equal(
            detector1.positioner.configuration,
            detector2.positioner.configuration,
            decimal=5)
        for link1, link2 in zip(detector1.positioner.links,
                                detector2.positioner.links):
            self.assertEqual(link1.ignore_limits, link2.ignore_limits)
            self.assertEqual(link1.locked, link2.locked)

        data["measurement_vectors"] = np.ones((3, 3, 2))  # invalid normals
        writer.write_project_hdf(data, filename)
        self.assertRaises(ValueError, reader.read_project_hdf, filename)

        data["measurement_vectors"] = np.ones(
            (3, 6, 2))  # more vector than detectors
        writer.write_project_hdf(data, filename)
        self.assertRaises(ValueError, reader.read_project_hdf, filename)

        data["measurement_vectors"] = np.ones(
            (4, 3, 2))  # more vectors than points
        writer.write_project_hdf(data, filename)
        self.assertRaises(ValueError, reader.read_project_hdf, filename)
Ejemplo n.º 8
0
    def testSerialLink(self):
        with self.assertRaises(ValueError):
            # zero vector as Axis
            Link("", [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], Link.Type.Prismatic, 0,
                 0, 0)

        link_1 = Link("", [0.0, 0.0, 1.0], [0.0, 0.0, 0.0],
                      Link.Type.Prismatic, 0.0, 600.0, 0.0)
        np.testing.assert_array_almost_equal(np.identity(4),
                                             link_1.transformation_matrix,
                                             decimal=5)
        link_1.move(200)
        expected_result = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 200],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result,
                                             link_1.transformation_matrix,
                                             decimal=5)

        link_2 = Link("", [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], Link.Type.Revolute,
                      -np.pi, np.pi, np.pi / 2)
        expected_result = [[0, -1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result,
                                             link_2.transformation_matrix,
                                             decimal=5)
        link_2.move(0)
        np.testing.assert_array_almost_equal(np.identity(4),
                                             link_2.transformation_matrix,
                                             decimal=5)
        link_2.reset()
        np.testing.assert_array_almost_equal(expected_result,
                                             link_2.transformation_matrix,
                                             decimal=5)
        qv = link_2.quaternion_vector_pair
        np.testing.assert_array_almost_equal(qv.quaternion,
                                             [0.0, 0.0, 0.70711, 0.70711],
                                             decimal=5)
        np.testing.assert_array_almost_equal(qv.vector, [0.0, 0.0, 0.0],
                                             decimal=5)

        q1 = Link("", [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], Link.Type.Prismatic, 0,
                  600, 0)
        q2 = Link("", [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], Link.Type.Revolute,
                  -3.14, 3.14, 0)
        q3 = Link("", [0.0, 1.0, 0.0], [0.0, 0.0, 0.0], Link.Type.Prismatic,
                  -250, 250, 0)
        q4 = Link("", [1.0, 0.0, 0.0], [0.0, 0.0, 0.0], Link.Type.Prismatic,
                  -250, 250, 0)
        s = SerialManipulator("", [q1, q2, q3, q4])

        pose_0 = s.fkine([250, 1.57, 20, 30])
        np.testing.assert_array_almost_equal(s.configuration,
                                             [250, 1.57, 20, 30],
                                             decimal=5)
        s.reset()
        np.testing.assert_array_almost_equal(s.configuration, [0, 0, 0, 0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(np.identity(4), s.pose, decimal=5)
        self.assertEqual(s.link_count, 4)
        self.assertEqual(len(s.links), 4)

        model = s.model()  # should be empty since no mesh is provided
        self.assertEqual(model.meshes, [])
        self.assertEqual(model.transforms, [])

        expected_result = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 250],
                           [0, 0, 0, 1]]
        pose = s.fkine([250, 1.57, 20, 30], end_index=1)
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)

        expected_result = [[1, 0, 0, 30], [0, 1, 0, 20], [0, 0, 1, 0],
                           [0, 0, 0, 1]]
        pose = s.fkine([250, 1.57, 20, 30], start_index=2)
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)
        base = Matrix44([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -500],
                         [0, 0, 0, 1]])
        s.base = base
        pose = s.fkine([250, 1.57, 20, 30], start_index=2)
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)

        pose = s.fkine([250, 1.57, 20, 30], end_index=1)
        expected_result = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -250],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)
        pose = s.fkine([250, 1.57, 20, 30], end_index=1, include_base=False)
        expected_result = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 250],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)

        s.tool = base
        pose = s.fkine([250, 1.57, 20, 30])
        np.testing.assert_array_almost_equal(pose,
                                             base @ pose_0 @ base,
                                             decimal=5)

        vertices = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        normals = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        indices = np.array([0, 1, 2])
        mesh = Mesh(vertices, indices, normals)
        q1 = Link("", [0.0, 0.0, 1.0], [1.0, 0.0, 0.0],
                  Link.Type.Revolute,
                  -3.14,
                  3.14,
                  0,
                  mesh=mesh)
        q2 = Link("", [0.0, 0.0, 1.0], [1.0, 0.0, 0.0],
                  Link.Type.Revolute,
                  -3.14,
                  3.14,
                  0,
                  mesh=mesh)
        s = SerialManipulator("", [q1, q2], base_mesh=mesh)
        self.assertEqual(len(s.model(base).meshes), 3)
        self.assertEqual(len(s.model(base).transforms), 3)
        pose = s.fkine([np.pi / 2, -np.pi / 2])
        expected_result = [[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 0],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)

        s.set_points = [-np.pi / 2, np.pi / 4]
        self.assertAlmostEqual(s.links[0].set_point, -np.pi / 2, 5)
        self.assertAlmostEqual(s.links[1].set_point, np.pi / 4, 5)

        s.links[0].locked = True
        s.links[1].locked = True
        s.fkine([-np.pi / 2, np.pi / 2])
        expected_result = [[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 0],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result,
                                             s.pose,
                                             decimal=5)
        pose = s.fkine([-np.pi / 2, np.pi / 2], ignore_locks=True)
        expected_result = [[1, 0, 0, 1], [0, 1, 0, -1], [0, 0, 1, 0],
                           [0, 0, 0, 1]]
        np.testing.assert_array_almost_equal(expected_result, pose, decimal=5)
Ejemplo n.º 9
0
 def setUp(self):
     vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
     normals = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
     indices = np.array([0, 1, 2])
     self.mesh = Mesh(vertices, indices, normals)
Ejemplo n.º 10
0
class TestMeshClass(unittest.TestCase):
    def setUp(self):
        vertices = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        normals = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        indices = np.array([2, 1, 0])
        self.mesh_1 = Mesh(vertices, indices, normals)

        vertices = np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]])
        normals = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
        indices = np.array([1, 0, 2, 0, 1, 2])
        self.mesh_2 = Mesh(vertices, indices, normals)

    def testCreation(self):
        vertices = np.array([[1, 1, 0], [1, 0, 0], [0, 1, 0]])
        indices = np.array([1, 0, 2])
        normals = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        mesh = Mesh(vertices, indices, normals)

        np.testing.assert_array_almost_equal(mesh.vertices,
                                             vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals, normals, decimal=5)
        np.testing.assert_array_equal(mesh.indices, [1, 0, 2])

        mesh = Mesh(vertices, indices, normals, clean=True)
        expected = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])

        np.testing.assert_array_almost_equal(mesh.vertices,
                                             vertices[[2, 1, 0]],
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals, expected, decimal=5)
        np.testing.assert_array_equal(mesh.indices, [1, 2, 0])

        v = np.array([[np.nan, 1, 0], [1, 0, 0], [0, 1, 0]])
        self.assertRaises(ValueError, Mesh, v, indices, normals, clean=True)

        n = np.array([[0, 1, 0], [1, -np.inf, 0], [0, 1, 0]])
        self.assertRaises(ValueError, Mesh, vertices, indices, n)

    def testComputeNormals(self):
        vertices = np.array([[1, 1, 0], [1, 0, 0], [0, 1, 0]])
        indices = np.array([1, 0, 2])
        mesh = Mesh(vertices, indices)

        expected = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])

        # Check that correct normals are generated also vertices and indices are unchanged
        np.testing.assert_array_almost_equal(mesh.vertices,
                                             vertices[[2, 1, 0]],
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals, expected, decimal=5)
        np.testing.assert_array_equal(mesh.indices, [1, 2, 0])

    def testComputeBoundingBox(self):
        box = self.mesh_1.bounding_box
        np.testing.assert_array_almost_equal(box.max,
                                             np.array([7, 8, 9]),
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.min,
                                             np.array([1, 2, 3]),
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.center,
                                             np.array([4.0, 5.0, 6.0]),
                                             decimal=5)
        self.assertAlmostEqual(box.radius, 5.1961524, 5)

        box = self.mesh_2.bounding_box
        np.testing.assert_array_almost_equal(box.max,
                                             np.array([7, 8, 9]),
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.min,
                                             np.array([1, 2, 3]),
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.center,
                                             np.array([4.0, 5.0, 6.0]),
                                             decimal=5)
        self.assertAlmostEqual(box.radius, 5.1961524, 5)

    def testAppendAndSplit(self):
        self.mesh_1.append(self.mesh_2)

        vertices = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [7, 8, 9],
                             [4, 5, 6], [1, 2, 3]])
        normals = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 0],
                            [0, 0, 1], [1, 0, 0]])
        indices = np.array([2, 1, 0, 4, 3, 5, 3, 4, 5])

        np.testing.assert_array_almost_equal(self.mesh_1.vertices,
                                             vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(self.mesh_1.normals,
                                             normals,
                                             decimal=5)
        np.testing.assert_array_equal(self.mesh_1.indices, indices)

        split_mesh = self.mesh_1.remove(3)
        np.testing.assert_array_equal(self.mesh_1.indices, np.array([2, 1, 0]))
        np.testing.assert_array_equal(split_mesh.indices,
                                      np.array([1, 0, 2, 0, 1, 2]))
        np.testing.assert_array_almost_equal(self.mesh_1.vertices,
                                             vertices[0:3, :],
                                             decimal=5)
        np.testing.assert_array_almost_equal(split_mesh.vertices,
                                             vertices[3:, :],
                                             decimal=5)
        np.testing.assert_array_almost_equal(self.mesh_1.normals,
                                             normals[0:3, :],
                                             decimal=5)
        np.testing.assert_array_almost_equal(split_mesh.normals,
                                             normals[3:, :],
                                             decimal=5)

    def testTransform(self):
        angles = np.radians([30, 60, 90])
        matrix = matrix_from_xyz_eulers(Vector3(angles))
        self.mesh_1.rotate(matrix)

        expected_vertices = np.array([
            [1.59807621, -0.75, 3.29903811],
            [2.69615242, -0.20096189, 8.34807621],
            [3.79422863, 0.34807621, 13.39711432],
        ])
        expected_normals = np.array([[0.866025, -0.25, 0.433013],
                                     [-0.5, -0.433013, 0.75],
                                     [0, 0.866025, 0.5]])

        np.testing.assert_array_almost_equal(self.mesh_1.vertices,
                                             expected_vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(self.mesh_1.normals,
                                             expected_normals,
                                             decimal=5)
        np.testing.assert_array_equal(self.mesh_1.indices, np.array([2, 1, 0]))

        offset = Vector3([10, -11, 12])
        self.mesh_1.translate(offset)
        expected_vertices = np.array([
            [11.59807621, -11.75, 15.29903811],
            [12.69615242, -11.20096189, 20.34807621],
            [13.79422863, -10.6519237, 25.39711432],
        ])

        np.testing.assert_array_almost_equal(self.mesh_1.vertices,
                                             expected_vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(self.mesh_1.normals,
                                             expected_normals,
                                             decimal=5)
        np.testing.assert_array_equal(self.mesh_1.indices, np.array([2, 1, 0]))

        transform_matrix = np.eye(4, 4)
        transform_matrix[0:3, 0:3] = matrix.transpose()
        transform_matrix[0:3, 3] = -offset.dot(matrix)
        self.mesh_1.transform(transform_matrix)
        expected = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        np.testing.assert_array_almost_equal(self.mesh_1.vertices,
                                             expected,
                                             decimal=5)
        expected = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        np.testing.assert_array_almost_equal(self.mesh_1.normals,
                                             expected,
                                             decimal=5)
        np.testing.assert_array_equal(self.mesh_1.indices, np.array([2, 1, 0]))

    def testCopy(self):
        mesh = self.mesh_1.copy()
        np.testing.assert_array_almost_equal(mesh.vertices,
                                             self.mesh_1.vertices,
                                             decimal=5)
        np.testing.assert_array_almost_equal(mesh.normals,
                                             self.mesh_1.normals,
                                             decimal=5)
        np.testing.assert_array_equal(mesh.indices, self.mesh_1.indices)
        self.assertIsNot(mesh.vertices, self.mesh_1.vertices)
        self.assertIsNot(mesh.normals, self.mesh_1.normals)
        self.assertIsNot(mesh.indices, self.mesh_1.indices)

    def testMeshGroup(self):
        group = MeshGroup()
        self.assertEqual(group.meshes, [])
        self.assertEqual(group.transforms, [])

        group.addMesh(self.mesh_1)
        self.assertEqual(group.meshes, [self.mesh_1])
        self.assertEqual(len(group.transforms), 1)
        np.testing.assert_array_almost_equal(group.transforms[0],
                                             np.identity(4))

        matrix = np.ones((4, 4))
        group.addMesh(self.mesh_2, matrix)
        self.assertEqual(group.meshes, [self.mesh_1, self.mesh_2])
        self.assertEqual(len(group.transforms), 2)
        np.testing.assert_array_almost_equal(group.transforms[0],
                                             np.identity(4))
        np.testing.assert_array_almost_equal(group.transforms[1], matrix)

        group.merge(group)
        self.assertEqual(group.meshes,
                         [self.mesh_1, self.mesh_2, self.mesh_1, self.mesh_2])
        self.assertEqual(len(group.transforms), 4)
        np.testing.assert_array_almost_equal(group.transforms[0],
                                             np.identity(4))
        np.testing.assert_array_almost_equal(group.transforms[1], matrix)
        np.testing.assert_array_almost_equal(group.transforms[2],
                                             np.identity(4))
        np.testing.assert_array_almost_equal(group.transforms[3], matrix)

        self.assertEqual(group[3][0], self.mesh_2)
        np.testing.assert_array_almost_equal(group[3][1], matrix)
Ejemplo n.º 11
0
    def testNodeCreation(self):
        node = Node()
        self.assertEqual(node.vertices.size, 0)
        self.assertEqual(node.indices.size, 0)
        self.assertEqual(node.normals.size, 0)
        self.assertTrue(node.isEmpty())

        mesh = create_plane(
            Plane(np.array([1.0, 0.0, 0.0]), np.array([0.0, 0.0, 0.0])))
        node = Node(mesh)
        np.testing.assert_array_almost_equal(node.vertices, mesh.vertices)
        np.testing.assert_array_equal(node.indices, mesh.indices)
        np.testing.assert_array_almost_equal(node.normals, mesh.normals)

        node = SampleEntity({}).node()
        self.assertTrue(node.isEmpty())

        sample_mesh = Mesh(
            np.array([[0, 0, 0], [0, 1, 0], [0, 1, 1]]),
            np.array([0, 1, 2]),
            np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0]]),
        )
        node = SampleEntity({"demo": sample_mesh}).node()
        self.assertEqual(len(node.per_object_transform), 1)
        np.testing.assert_array_almost_equal(node.vertices,
                                             sample_mesh.vertices)
        np.testing.assert_array_equal(node.indices, sample_mesh.indices)
        np.testing.assert_array_almost_equal(node.normals, sample_mesh.normals)
        self.assertEqual(node.render_primitive, Node.RenderPrimitive.Triangles)

        points = np.rec.array(
            [([11.0, 12.0, 13.0], True), ([14.0, 15.0, 16.0], False),
             ([17.0, 18.0, 19.0], True)],
            dtype=[("points", "f4", 3), ("enabled", "?")],
        )

        size = np.array([0, 1, 2])
        volume = Volume(np.zeros([3, 4, 5], np.float32), size, size, size)
        self.assertEqual(volume.shape, (3, 4, 5))
        node = VolumeEntity(None).node()
        self.assertTrue(node.isEmpty())
        with mock.patch('sscanss.core.scene.node.Texture3D'), mock.patch(
                'sscanss.core.scene.node.Texture1D'):
            node = VolumeEntity(volume).node()
            np.testing.assert_array_almost_equal(node.top, [1.5, 2., 2.5])
            np.testing.assert_array_almost_equal(node.bottom,
                                                 [-1.5, -2., -2.5])
            box = node.bounding_box
            np.testing.assert_array_almost_equal(box.max, [2.5, 3., 3.5],
                                                 decimal=5)
            np.testing.assert_array_almost_equal(box.min, [-0.5, -1., -1.5],
                                                 decimal=5)
            np.testing.assert_array_almost_equal(box.center, [1., 1., 1.],
                                                 decimal=5)
            self.assertAlmostEqual(box.radius, 3.535533, 5)

        node = MeasurementPointEntity(np.array([])).node()
        self.assertTrue(node.isEmpty())
        node = MeasurementPointEntity(points).node()
        self.assertEqual(len(node.per_object_transform), points.size)

        node = MeasurementVectorEntity(np.array([]), np.array([]), 0).node()
        self.assertTrue(node.isEmpty())

        vectors = np.ones((3, 3, 2))
        node = MeasurementVectorEntity(points, vectors, 0).node()
        self.assertTrue(node.children[0].visible)
        self.assertFalse(node.children[1].visible)
        self.assertEqual(len(node.children), vectors.shape[2])

        node = PlaneEntity(
            Plane(np.array([1.0, 0.0, 0.0]), np.array([0.0, 0.0, 0.0])), 1.0,
            1.0).node()
        np.testing.assert_array_almost_equal(node.vertices, mesh.vertices)
        np.testing.assert_array_equal(node.indices, mesh.indices)
        np.testing.assert_array_almost_equal(node.normals, mesh.normals)