コード例 #1
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testVector(self):
        self.assertRaises(ValueError, Vector, -1)
        self.assertRaises(ValueError, Vector, 2, {"values": [2, 3, 4]})

        v = Vector(10)
        self.assertEqual(v.size, 10)
        self.assertEqual(len(v), 10)
        self.assertEqual(v[9], 0)
        v[9] = 7
        self.assertEqual(v[9], 7)

        v = Vector.create(2, [1, 2])
        self.assertEqual(v.size, 2)
        self.assertEqual(v.x, 1)
        self.assertTrue(isinstance(v, Vector2))

        v = Vector.create(3, [1, 2, 3])
        self.assertEqual(v.size, 3)
        self.assertEqual(v.z, 3)
        self.assertTrue(isinstance(v, Vector3))

        v = Vector.create(4, [1, 2, 3, 4])
        self.assertEqual(v.size, 4)
        self.assertEqual(v.w, 4)
        self.assertTrue(isinstance(v, Vector4))

        v = Vector.create(5, [1, 2, 3, 4, 5])
        self.assertTrue(isinstance(v, Vector))

        self.assertRaises(ValueError, lambda: Vector3() + Vector4())
        self.assertRaises(ValueError, lambda: Vector3() - Vector4())
        self.assertRaises(ValueError, lambda: Vector3() * Vector4())
        self.assertRaises(ValueError, lambda: Vector3() / Vector4())
        self.assertRaises(ValueError, lambda: Vector3() @ Vector4())
コード例 #2
0
    def mouseMoveEvent(self, event):
        self.mouse_moved.emit(event.pos())
        if event.buttons() == QtCore.Qt.NoButton:
            super().mouseMoveEvent(event)
            return

        is_rotating = event.buttons() == QtCore.Qt.RightButton and event.modifiers() == QtCore.Qt.NoModifier
        is_panning = ((event.buttons() == QtCore.Qt.RightButton and event.modifiers() == QtCore.Qt.ControlModifier)
                      or (event.buttons() == QtCore.Qt.MiddleButton and event.modifiers() == QtCore.Qt.NoModifier))

        pos = self.mapToScene(event.pos())
        if is_rotating:
            anchor = self.scene_transform.mapRect(self.anchor)
            adj_pos = pos - anchor.center()
            adj_last_pos = self.last_pos - anchor.center()

            if adj_pos.manhattanLength() < 0.1 or adj_last_pos.manhattanLength() < 0.1:
                return

            va = Vector3([adj_last_pos.x(), adj_last_pos.y(), 0.]).normalized
            vb = Vector3([adj_pos.x(), adj_pos.y(), 0.]).normalized
            angle = -math.acos(clamp(va | vb, -1.0, 1.0))

            if np.dot([0., 0., 1.], va ^ vb) > 0:
                angle = -angle

            self.rotateSceneItems(angle, anchor.center())
        elif is_panning:
            dx = pos.x() - self.last_pos.x()
            dy = pos.y() - self.last_pos.y()
            self.translateSceneItems(dx, dy)

        self.last_pos = pos
        super().mouseMoveEvent(event)
コード例 #3
0
    def testConstruction(self):
        max_position = np.array([1.0, 1.0, 1.0])
        min_position = np.array([-1.0, -1.0, -1.0])

        box = BoundingBox(max_position, min_position)
        max_pos, min_pos = box.bounds
        np.testing.assert_array_almost_equal(max_pos, max_position, decimal=5)
        np.testing.assert_array_almost_equal(min_pos, min_position, decimal=5)
        np.testing.assert_array_almost_equal(box.center, [0.0, 0.0, 0.0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.radius, 1.73205, decimal=5)

        max_position = Vector3([1.0, 2.0, 3.0])
        min_position = Vector3([-1.0, -2.0, -3.0])
        box = BoundingBox(max_position, min_position)
        np.testing.assert_array_almost_equal(box.max, max_position, decimal=5)
        self.assertIsNot(max_position,
                         box.max)  # make sure this are not the same object
        np.testing.assert_array_almost_equal(box.min, min_position, decimal=5)
        self.assertIsNot(min_position,
                         box.min)  # make sure this are not the same object
        np.testing.assert_array_almost_equal(box.center, [0.0, 0.0, 0.0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.radius, 3.74166, decimal=5)

        points = [[1.0, 1.0, 0.0], [-1.0, 0.0, -1.0], [0.0, -1.0, 1.0]]
        box = BoundingBox.fromPoints(points)
        np.testing.assert_array_almost_equal(box.max, [1.0, 1.0, 1.0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.min, [-1.0, -1.0, -1.0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.center, [0.0, 0.0, 0.0],
                                             decimal=5)
        np.testing.assert_array_almost_equal(box.radius, 1.73205, decimal=5)
コード例 #4
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testXYZEulersFromMatrix(self):
        matrix = Matrix33.identity()
        expected = Vector3([0.0, 0.0, 0.0])
        result = xyz_eulers_from_matrix(matrix)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        matrix = Matrix33([[0, -1, 0], [1, 0, 0], [0, 0, 1]])
        expected = Vector3(np.radians([0.0, 0.0, 90.0]))
        result = xyz_eulers_from_matrix(matrix)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        matrix = Matrix33([[0.4924039, -0.8528686, 0.1736482],
                           [0.7934120, 0.3578208, -0.4924039],
                           [0.3578208, 0.3802361, 0.8528686]])
        expected = Vector3(np.radians([30.0, 10.0, 60.0]))
        result = xyz_eulers_from_matrix(matrix)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        matrix = Matrix33([
            [-0.0000000, -0.0000000, -1.0000000],
            [-0.3420202, 0.9396926, -0.0000000],
            [0.9396926, 0.3420202, -0.0000000],
        ])
        expected = Vector3(np.radians([20.0, -90.0, 0.0]))
        result = xyz_eulers_from_matrix(matrix)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        matrix = Matrix33([[0.0000000, 0.0000000, 1.0000000],
                           [0.6427876, 0.7660444, -0.0000000],
                           [-0.7660444, 0.6427876, 0.0000000]])
        expected = Vector3(np.radians([40.0, 90, 0.0]))
        result = xyz_eulers_from_matrix(matrix)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)
コード例 #5
0
    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]))
コード例 #6
0
ファイル: tools.py プロジェクト: StephenNneji/SScanSS-2
    def __init__(self, angles, sample_key, presenter):
        super().__init__()
        self.angles = Vector3(np.radians(angles))
        self.key = sample_key
        self.model = presenter.model

        self.setText(f'Rotate Sample ({self.key})')
コード例 #7
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testAngleAxisToMatrix(self):
        axis = Vector3([1.0, 0.0, 0.0])
        expected = Matrix33.identity()
        result = angle_axis_to_matrix(0.0, axis)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        axis = Vector3([0.0, 1.0, 0.0])
        expected = Matrix33([[0, 0, 1], [0, 1, 0], [-1, 0, 0]])
        result = angle_axis_to_matrix(math.radians(90), axis)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        axis = Vector3([0.0, 0.707107, 0.707107])
        expected = Matrix33([[0.7071068, -0.5000000, 0.5000000],
                             [0.5000000, 0.8535534, 0.1464466],
                             [-0.5000000, 0.1464466, 0.8535534]])
        result = angle_axis_to_matrix(math.radians(45), axis)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)
コード例 #8
0
    def setPlane(self, selected_text):
        """Sets destination/final plane normal or shows inputs for custom normal

        :param selected_text: PlaneOptions
        :type selected_text: str
        """
        if selected_text == PlaneOptions.Custom.value:
            self.custom_plane_widget.setVisible(True)
            return
        elif selected_text == PlaneOptions.XY.value:
            self.final_plane_normal = Vector3([0., 0., 1.])
        elif selected_text == PlaneOptions.XZ.value:
            self.final_plane_normal = Vector3([0., 1., 0.])
        else:
            self.final_plane_normal = Vector3([1., 0., 0.])

        self.custom_plane_widget.setVisible(False)
コード例 #9
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testMatrixFromZYXEulers(self):
        eulers = Vector3([0.0, 0.0, 0.0])
        expected = Matrix33.identity()
        result = matrix_from_zyx_eulers(eulers)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        eulers = Vector3(np.radians([90.0, 0.0, 0.0]))
        expected = Matrix33([[0, -1, 0], [1, 0, 0], [0, 0, 1]])
        result = matrix_from_zyx_eulers(eulers)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        eulers = Vector3(np.radians([60.0, 10.0, 30.0]))
        expected = Matrix33([[0.4924039, -0.7065880, 0.5082046],
                             [0.8528686, 0.5082046, -0.1197639],
                             [-0.1736482, 0.4924039, 0.8528686]])
        result = matrix_from_zyx_eulers(eulers)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)
コード例 #10
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testMatrixFromXYZEulers(self):
        eulers = Vector3([0.0, 0.0, 0.0])
        expected = Matrix33.identity()
        result = matrix_from_xyz_eulers(eulers)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        eulers = Vector3(np.radians([0.0, 0.0, 90.0]))
        expected = Matrix33([[0, -1, 0], [1, 0, 0], [0, 0, 1]])
        result = matrix_from_xyz_eulers(eulers)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)

        eulers = Vector3(np.radians([30.0, 10.0, 60.0]))
        expected = Matrix33([[0.4924039, -0.8528686, 0.1736482],
                             [0.7934120, 0.3578208, -0.4924039],
                             [0.3578208, 0.3802361, 0.8528686]])
        result = matrix_from_xyz_eulers(eulers)
        np.testing.assert_array_almost_equal(result, expected, decimal=5)
コード例 #11
0
    def testPointSelection(self):
        start = Vector3([0.0, 0.0, 0.0])
        end = Vector3([0.0, 0.0, 10.0])
        faces = np.array([[-1.0, -0.5, 5.0, 0.5, 0.5, 5.0, 1.0, -0.5, 5.0],
                          [-1.0, -0.5, 7.0, 0.5, 0.5, 7.0, 1.0, -0.5, 7.0]])

        points = point_selection(start, end, faces)
        np.testing.assert_array_almost_equal(
            points, [[0.0, 0.0, 5.0], [0.0, 0.0, 7.0]], decimal=5)

        start = np.array([0.0, 0.0, 6.0])
        points = point_selection(start, end, faces)
        np.testing.assert_array_almost_equal(points, [[0.0, 0.0, 7.0]],
                                             decimal=5)

        start = np.array([-1.1, 0.0, 0.0])
        points = point_selection(start, end, faces)
        self.assertEqual(points.size, 0)
コード例 #12
0
    def testPathLengthCalculation(self):
        cube = create_cuboid(2, 4, 6)
        beam_axis = Vector3([1.0, 0.0, 0.0])
        gauge_volume = Vector3([0.0, 0.0, 0.0])
        diff_axis = [Vector3([0.0, 1.0, 0.0]), Vector3([0.0, 0.0, 1.0])]

        lengths = path_length_calculation(cube, gauge_volume, beam_axis,
                                          diff_axis)
        np.testing.assert_array_almost_equal(lengths, [4.0, 3.0], decimal=5)

        beam_axis = Vector3([0.0, -1.0, 0.0])
        lengths = path_length_calculation(cube, gauge_volume, beam_axis,
                                          diff_axis)
        np.testing.assert_array_almost_equal(lengths, [6.0, 5.0], decimal=5)

        # No hit
        beam_axis = Vector3([0.0, -1.0, 0.0])
        cube.vertices = cube.vertices - [0.0, 10.0, 0.0]
        lengths = path_length_calculation(cube, gauge_volume, beam_axis,
                                          diff_axis)
        np.testing.assert_array_almost_equal(lengths, [0.0, 0.0], decimal=5)

        # single detector
        diff_axis = [Vector3([0.0, 0.0, 1.0])]
        cube.vertices = cube.vertices + [0.0, 10.0, 0.0]
        length = path_length_calculation(cube, gauge_volume, beam_axis,
                                         diff_axis)
        self.assertAlmostEqual(*length, 5.0, 5)

        # beam outside at gauge volume
        cylinder = create_tube(2, 4, 6)
        beam_axis = Vector3([0.0, -1.0, 0.0])
        diff_axis = [Vector3([0.0, -1.0, 0.0])]
        length = path_length_calculation(cylinder, gauge_volume, beam_axis,
                                         diff_axis)
        self.assertAlmostEqual(*length, 0.0, 5)

        # beam cross more than a 2 faces
        cylinder.vertices = cylinder.vertices - [0.0, 3.0, 0.0]
        length = path_length_calculation(cylinder, gauge_volume, beam_axis,
                                         diff_axis)
        self.assertAlmostEqual(*length, 4.0, 5)

        # diff beam does not hit
        cube = create_cuboid(depth=0.0)
        cube.vertices = cube.vertices - [0.0, 1.0, 0.0]
        length = path_length_calculation(cube, gauge_volume, beam_axis,
                                         diff_axis)
        self.assertAlmostEqual(*length, 0.0, 5)
コード例 #13
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testPlane(self):
        normal = np.array([1.0, 0.0, 0.0])
        point = np.array([0.0, 0.0, 0.0])
        plane = Plane(normal, point)
        np.testing.assert_array_almost_equal(plane.normal, normal, decimal=5)
        np.testing.assert_array_almost_equal(plane.point, point, decimal=5)

        point_2 = np.array([0.0, 1.0, 0.0])
        point_3 = np.array([0.0, 1.0, 1.0])
        plane_2 = Plane.fromPlanarPoints(point, point_2, point_3)
        np.testing.assert_array_almost_equal(plane_2.normal, normal, decimal=5)
        np.testing.assert_array_almost_equal(plane_2.point, point, decimal=5)

        # bad normal
        self.assertRaises(ValueError, Plane, point, point)
        self.assertRaises(ValueError, Plane.fromCoefficient, 0, 0, 0, 0)
        self.assertRaises(ValueError, Plane.fromPlanarPoints, point, point_2,
                          point_2)

        self.assertAlmostEqual(plane.distanceFromOrigin(), 0.0, 5)
        plane = Plane(normal, normal)
        self.assertAlmostEqual(plane.distanceFromOrigin(), 1.0, 5)

        normal = np.array([1.0, 1.0, 0.0])
        plane = Plane(normal, point)  # normal vector should be normalize
        np.testing.assert_array_almost_equal(plane.normal,
                                             Vector3(normal).normalized,
                                             decimal=5)
        np.testing.assert_array_almost_equal(plane.point, point, decimal=5)

        plane_2 = Plane.fromCoefficient(1, 1, 0, 0)
        np.testing.assert_array_almost_equal(plane.normal,
                                             plane_2.normal,
                                             decimal=5)
        np.testing.assert_array_almost_equal(plane.point,
                                             plane_2.point,
                                             decimal=5)

        self.assertNotEqual(repr(plane_2), str(plane_2))

        points = [
            [20.8212362, -9.3734531, 70.5337096],
            [-56.8372955, -9.5676188, 46.7159424],
            [-72.2471016, -9.3954792, -0.1111806],
            [-49.1049504, -9.3734125, -54.1452751],
            [26.9184367, -9.1761998, -68.1036148],
        ]

        plane = Plane.fromBestFit(points)
        np.testing.assert_array_almost_equal(
            plane.normal, [0.0019415, -0.999997, -0.0014657], decimal=5)
        np.testing.assert_array_almost_equal(
            plane.point, [-26.089934, -9.377232, -1.022083], decimal=5)
        self.assertRaises(ValueError, Plane.fromBestFit, points[:2])
コード例 #14
0
    def setCustomPlane(self, is_valid):
        """Sets custom destination/final plane normal. Error is shown if normal is invalid

        :param is_valid: indicate if the custom plane normal is valid
        :type is_valid: bool
        """
        if is_valid:
            normal = Vector3(
                [self.x_axis.value, self.y_axis.value, self.z_axis.value])
            length = normal.length
            if length > 1e-5:
                self.final_plane_normal = normal / length
                self.x_axis.validation_label.setText('')
                return
            else:
                self.x_axis.validation_label.setText('Bad Normal')

        self.final_plane_normal = None
コード例 #15
0
ファイル: test_util.py プロジェクト: StephenNneji/SScanSS-2
    def testCameraClass(self):
        # create a camera with aspect ratio of 1 and 60 deg field of view
        camera = Camera(1, 60)

        position = Vector3([
            0,
            0,
            0,
        ])
        target = Vector3([5.0, 0.0, 0.0])
        up = Vector3([0.0, 1.0, 0.0])

        camera.lookAt(position, target, up)
        model_view = np.array([[0, -0, 1, 0.0], [0, 1, 0, 0.0],
                               [-1, 0, 0, 0.0], [0, 0, 0, 1.0]])
        np.testing.assert_array_almost_equal(model_view,
                                             camera.model_view,
                                             decimal=5)
        camera.lookAt(position, target)
        np.testing.assert_array_almost_equal(model_view,
                                             camera.model_view,
                                             decimal=5)

        perspective = np.array([[1.73205081, 0, 0, 0], [0, 1.73205081, 0, 0],
                                [0, 0, -1.0002, -0.20002], [0, 0, -1, 0]])
        np.testing.assert_array_almost_equal(perspective,
                                             camera.perspective,
                                             decimal=5)
        np.testing.assert_array_almost_equal(perspective,
                                             camera.projection,
                                             decimal=5)

        ortho = np.array([[17.3205081, 0, 0, 0], [0, 17.3205081, 0, 0],
                          [0, 0, -0.0020002, -1.0002], [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(ortho,
                                             camera.orthographic,
                                             decimal=5)
        camera.mode = Camera.Projection.Orthographic
        np.testing.assert_array_almost_equal(ortho,
                                             camera.projection,
                                             decimal=5)
        camera.mode = Camera.Projection.Perspective

        camera.reset()
        expected = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, -2],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)
        camera.lookAt(target, target)
        expected = np.array([[1, 0, 0, -5], [0, 1, 0, 0], [0, 0, 1, 0],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)

        camera.viewFrom(Directions.up)
        expected = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, -1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.rot_matrix,
                                             decimal=5)
        camera.viewFrom(Directions.down)
        expected = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.rot_matrix,
                                             decimal=5)
        camera.viewFrom(Directions.left)
        expected = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.rot_matrix,
                                             decimal=5)
        camera.viewFrom(Directions.right)
        expected = np.array([[0, -1, 0], [0, 0, 1], [-1, 0, 0]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.rot_matrix,
                                             decimal=5)
        camera.viewFrom(Directions.front)
        expected = np.array([[1, 0, 0], [0, 0, 1], [0, -1, 0]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.rot_matrix,
                                             decimal=5)
        camera.viewFrom(Directions.back)
        expected = np.array([[-1, 0, 0], [0, 0, 1], [0, 1, 0]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.rot_matrix,
                                             decimal=5)

        position = Vector3([0, 0, 0])
        target = Vector3([0.0, 5.0, 0.0])
        camera.lookAt(position, target)
        expected = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)

        camera.zoomToFit(target, 1.0)
        expected = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 3],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)

        camera.pan(-1, 1)
        expected = np.array([[1, 0, 0, 2], [0, 0, 1, 2], [0, -1, 0, 3],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)

        camera.zoom(1)
        expected = np.array([[1, 0, 0, 2], [0, 0, 1, 2], [0, -1, 0, 5],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)

        camera.rotate((0, 0), (0, 0))
        expected = np.array([[1, 0, 0, 2], [0, 0, 1, 2], [0, -1, 0, 5],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)
        camera.rotate((0, 0), (0.5, 0.5))
        expected = np.array([
            [0.8535533, -0.5, 0.1464466, 4.5],
            [0.1464466, 0.5, 0.8535533, -0.5],
            [-0.5, -0.707106, 0.5, 3.535533],
            [0, 0, 0, 1],
        ])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)

        camera = Camera(0.5, 45)
        camera.zoomToFit(target, 1)
        expected = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0.0691067],
                             [0, 0, 0, 1]])
        np.testing.assert_array_almost_equal(expected,
                                             camera.model_view,
                                             decimal=5)
コード例 #16
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testQuaternion(self):
        q = Quaternion.identity()
        q[1] = 1.0
        self.assertAlmostEqual(q.x, 0.0, 5)
        self.assertAlmostEqual(q.y, 1.0, 5)
        self.assertAlmostEqual(q.z, 0.0, 5)
        self.assertAlmostEqual(q.w, 1.0, 5)
        q.axis = [1.0, 1.0, 1.0]
        np.testing.assert_array_almost_equal(q.axis, [1.0, 1.0, 1.0],
                                             decimal=5)

        q.x = 1.0
        q.y = q[0]
        q.z = 1.0
        q.w = 0.0
        np.testing.assert_array_almost_equal(q, [1.0, 1.0, 1.0, 0.0],
                                             decimal=5)
        q = q.normalize()
        np.testing.assert_array_almost_equal(q,
                                             [0.57735, 0.57735, 0.57735, 0.0],
                                             decimal=5)

        matrix = Matrix33([[1, 0, 0], [0, 0, -1], [0, 1, 0]])
        q = Quaternion.fromMatrix(matrix)
        np.testing.assert_array_almost_equal([0.7071067, 0.0, 0.0],
                                             q.axis,
                                             decimal=5)
        np.testing.assert_array_almost_equal(matrix, q.toMatrix(), decimal=5)

        matrix = Matrix33([[-1, 0, 0], [0, -1, 0], [0, 0, 1]])
        q = Quaternion.fromMatrix(matrix)
        np.testing.assert_array_almost_equal([0.0, 0.0, 1.0],
                                             q.axis,
                                             decimal=5)
        np.testing.assert_array_almost_equal(matrix, q.toMatrix(), decimal=5)

        matrix = Matrix33([[0, 1, 0], [-1, 0, 0], [0, 0, 1]])
        q = Quaternion.fromMatrix(matrix)
        np.testing.assert_array_almost_equal([0.0, 0.0, -0.7071067],
                                             q.axis,
                                             decimal=5)
        np.testing.assert_array_almost_equal(matrix, q.toMatrix(), decimal=5)

        matrix = Matrix33([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
        q = Quaternion.fromMatrix(matrix)
        np.testing.assert_array_almost_equal([0.5, 0.5, 0.5],
                                             q.axis,
                                             decimal=5)
        np.testing.assert_array_almost_equal(matrix, q.toMatrix(), decimal=5)

        data = [-1, -1, -1, 1] * np.array(q)
        np.testing.assert_array_almost_equal(q.conjugate(), data, decimal=5)

        axis, angle = q.toAxisAngle()
        self.assertAlmostEqual(angle, 2.0943951, 5)
        np.testing.assert_array_almost_equal(axis, [0.57735, 0.57735, 0.57735],
                                             decimal=5)

        axis, angle = Quaternion().toAxisAngle()
        self.assertAlmostEqual(angle, np.pi, 5)
        np.testing.assert_array_almost_equal(axis, [0.0, 0.0, 0.0], decimal=5)
        array = np.array(Quaternion().normalize())
        np.testing.assert_array_almost_equal(array, [0.0, 0.0, 0.0, 0.0],
                                             decimal=5)

        qu = Quaternion.fromAxisAngle(Vector3([1.0, 0.0, 0.0]), 0.0)
        axis, angle = qu.toAxisAngle()
        self.assertAlmostEqual(angle, 0.0, 5)
        np.testing.assert_array_almost_equal(axis, [0.0, 0.0, 0.0], decimal=5)

        mm = Matrix33([[-0.2128074, 0.5013429, 0.8386706],
                       [0.9463776, -0.1077663, 0.3045583],
                       [0.2430686, 0.8585113, -0.4515262]])

        qq = Quaternion.fromMatrix(mm)
        p1 = qq.rotate([1, 2, 3])
        p2 = mm @ Vector3([1, 2, 3])
        np.testing.assert_array_almost_equal(p1, p2, decimal=5)
        np.testing.assert_array_almost_equal(mm, qq.toMatrix(), decimal=5)

        # test dot product
        self.assertAlmostEqual(q | qq, 0.9544055, 5)

        q = Quaternion(0.0, 1.0, 1.0, 1.0)
        np.testing.assert_array_almost_equal(
            q.inverse(), [-0.57735, -0.57735, -0.57735, 0.0], decimal=5)

        self.assertNotEqual(repr(qq), str(qq))
コード例 #17
0
ファイル: test_math.py プロジェクト: StephenNneji/SScanSS-2
    def testVector3(self):
        v = Vector3([4.0, 2.0, 3.0])

        v.x /= 4
        self.assertAlmostEqual(v.x, 1.0, 5)
        self.assertAlmostEqual(v.y, 2.0, 5)
        self.assertAlmostEqual(v.z, 3.0, 5)
        with self.assertRaises(AttributeError):
            v.w = 5

        np.testing.assert_array_almost_equal(v, [1.0, 2.0, 3.0], decimal=5)
        self.assertAlmostEqual(v.length, 3.7416573867739413, 5)
        v1 = v.normalized
        self.assertAlmostEqual(v1.length, 1.0, 5)
        v.normalize()
        np.testing.assert_array_almost_equal(
            v.xyz, [0.26726124, 0.53452248, 0.80178373], decimal=5)
        self.assertAlmostEqual(v1.length, 1.0, 5)

        vec = Vector3([1, 2, 3])
        self.assertEqual(vec.dot([1, 2, 3]), 14)
        self.assertEqual(vec | vec, 14)

        vec = Vector3([1, 2, 0])
        np.testing.assert_array_equal(vec.cross([4, 5, 0]), [0, 0, -3])
        vec = Vector3([1, 0, 0])
        np.testing.assert_array_equal(vec.cross([0, 1, 0]), [0, 0, 1])
        np.testing.assert_array_equal((vec ^ [0, 1, 0]), [0, 0, 1])

        v1 = Vector3([1, 2, 3])
        v2 = Vector3([4, 5, 6])
        v3 = v1 + v2
        np.testing.assert_array_equal(v3, [5, 7, 9])
        v3 = 2 + v2
        np.testing.assert_array_equal(v3, [6, 7, 8])
        v3 = v1 + 2
        np.testing.assert_array_equal(v3, [3, 4, 5])
        v3 = v1 - v2
        np.testing.assert_array_equal(v3, [-3, -3, -3])
        v3 = 2 - v2
        np.testing.assert_array_equal(v3, [-2, -3, -4])
        v3 = v1 - 2
        np.testing.assert_array_equal(v3, [-1, 0, 1])
        v3 = v1 * v2
        np.testing.assert_array_equal(v3, [4, 10, 18])
        v3 = 2 * v2
        np.testing.assert_array_equal(v3, [8, 10, 12])
        v3 = v1 * 2
        np.testing.assert_array_equal(v3, [2, 4, 6])
        v3 = v1 / v2
        np.testing.assert_array_almost_equal(v3, [0.25, 0.4, 0.5], decimal=5)
        v3 = 2 / v2
        np.testing.assert_array_almost_equal(v3, [0.5, 0.4, 0.333333],
                                             decimal=5)
        v3 = v1 / 2
        np.testing.assert_array_almost_equal(v3, [0.5, 1.0, 1.5], decimal=5)

        v = Vector3()
        v += v1
        np.testing.assert_array_equal(v, [1, 2, 3])
        v -= v1
        np.testing.assert_array_equal(v, [0, 0, 0])
        v1 *= Vector3([2, 2, 2])
        np.testing.assert_array_equal(v1, [2, 4, 6])
        v1 /= Vector3([0.5, 0.5, 0.5])
        np.testing.assert_array_equal(v1, [4, 8, 12])

        v = np.ones(3) + Vector3([-1, 0, 1])
        np.testing.assert_array_almost_equal(v, [0, 1, 2], decimal=5)
        self.assertTrue(isinstance(v, Vector3))

        v = np.ones(3) - Vector3([-1, 0, 1])
        np.testing.assert_array_almost_equal(v, [2, 1, 0], decimal=5)
        self.assertTrue(isinstance(v, Vector3))

        v = Vector3([-1, 0, 1]) - np.array([[1, 0, -1]])
        np.testing.assert_array_almost_equal(v, [[-2, 0, 2]], decimal=5)

        v = Vector3([-1, 0, 1]) + np.array([[1, 0, -1], [0, 1, -1]])
        np.testing.assert_array_almost_equal(v, [[0, 0, 0], [-1, 1, 0]],
                                             decimal=5)

        a = np.ones((4, 3)) - Vector3([1, 1, 1]) * 3
        np.testing.assert_array_almost_equal(a,
                                             np.ones((4, 3)) * -2,
                                             decimal=5)
        self.assertTrue(isinstance(a, np.ndarray))