Пример #1
0
def test_non_uniform_scale():
    cube_v = create_default_cube_verts()

    # Confidence check.
    np.testing.assert_array_equal(cube_v[0], [1.0, 0.0, 0.0])
    np.testing.assert_array_equal(cube_v[6], [5.0, 4.0, 4.0])

    forward, inverse = transform_matrix_for_non_uniform_scale(
        1.0, -2.0, 1.0, allow_flipping=True, ret_inverse_matrix=True
    )
    transformed_cube_v = apply_transform(forward)(cube_v)

    np.testing.assert_array_equal(transformed_cube_v[0], [1.0, 0.0, 0.0])
    np.testing.assert_array_equal(transformed_cube_v[6], [5.0, -8.0, 4.0])

    untransformed_cube_v = apply_transform(inverse)(transformed_cube_v)

    np.testing.assert_array_almost_equal(untransformed_cube_v, cube_v)

    np.testing.assert_array_equal(
        transform_matrix_for_non_uniform_scale(
            1.0, -2.0, 1.0, allow_flipping=True, ret_inverse_matrix=False
        ),
        forward,
    )
Пример #2
0
def test_apply_transform():
    scale_factor = np.array([3.0, 0.5, 2.0])
    transform = np.array(
        [
            [scale_factor[0], 0, 0, 0],
            [0, scale_factor[1], 0, 0],
            [0, 0, scale_factor[2], 0],
            [0, 0, 0, 1],
        ]
    )

    points = np.array([[1.0, 2.0, 3.0], [5.0, 0.0, 1.0]])
    expected_points = np.array([[3.0, 1.0, 6.0], [15.0, 0.0, 2.0]])

    transformer = apply_transform(transform)
    np.testing.assert_array_equal(transformer(points), expected_points)
    np.testing.assert_array_equal(transformer(points[1]), expected_points[1])

    expected_points_discarding_z = np.array([[3.0, 1.0], [15.0, 0.0]])
    np.testing.assert_array_equal(
        transformer(points, discard_z_coord=True),
        expected_points_discarding_z,
    )
    np.testing.assert_array_equal(
        transformer(points[1], discard_z_coord=True),
        expected_points_discarding_z[1],
    )
Пример #3
0
def test_inverse_orthographic_projection():
    world_coords = teapot_verts()
    canvas_coords = apply_transform(
        world_to_canvas_orthographic_projection(
            width=800,
            height=600,
            position=np.array([0.5, 1.5, 0.0]),
            target=np.array([-0.5, 1.0, -2.0]),
            zoom=100,
        ))(world_coords)
    untransformed_world_coords = apply_transform(
        world_to_canvas_orthographic_projection(
            width=800,
            height=600,
            position=np.array([0.5, 1.5, 0.0]),
            target=np.array([-0.5, 1.0, -2.0]),
            zoom=100,
            inverse=True,
        ))(canvas_coords)
    np.testing.assert_array_almost_equal(untransformed_world_coords,
                                         world_coords)
Пример #4
0
def test_uniform_scale():
    cube_v = create_default_cube_verts()

    # Confidence check.
    np.testing.assert_array_equal(cube_v[0], [1.0, 0.0, 0.0])
    np.testing.assert_array_equal(cube_v[6], [5.0, 4.0, 4.0])

    transformed_cube_v = apply_transform(
        transform_matrix_for_uniform_scale(-10.0, allow_flipping=True)
    )(cube_v)

    np.testing.assert_array_equal(transformed_cube_v[0], [-10.0, 0.0, 0.0])
    np.testing.assert_array_equal(transformed_cube_v[6], [-50.0, -40.0, -40.0])
Пример #5
0
def test_translate():
    cube_v = create_default_cube_verts()

    # Confidence check.
    np.testing.assert_array_equal(cube_v[0], [1.0, 0.0, 0.0])
    np.testing.assert_array_equal(cube_v[6], [5.0, 4.0, 4.0])

    transformed_cube_v = apply_transform(
        transform_matrix_for_translation(np.array([8.0, 6.0, 7.0]))
    )(cube_v)

    np.testing.assert_array_equal(transformed_cube_v[0], [9.0, 6.0, 7.0])
    np.testing.assert_array_equal(transformed_cube_v[6], [13.0, 10.0, 11.0])
Пример #6
0
def generate_teapot_image():
    from polliwog.transform._testing_helper import write_canvas_points_to_png

    world_coords = teapot_verts()
    canvas_coords = apply_transform(
        world_to_canvas_orthographic_projection(
            width=800,
            height=600,
            position=np.array([0.5, 1.5, 0.0]),
            target=np.array([-0.5, 1.0, -2.0]),
            zoom=100,
        ))(world_coords, discard_z_coord=True)

    write_canvas_points_to_png(canvas_coords, 800, 600, "projected.png")
Пример #7
0
def test_rotate():
    cube_v = create_default_cube_verts()
    ways_to_rotate_around_y_a_quarter_turn = [
        np.array([[0, 0, 1], [0, 1, 0], [-1, 0, 0]]),
        np.array([0, np.pi / 2, 0]),
    ]
    for rot in ways_to_rotate_around_y_a_quarter_turn:
        # Confidence check.
        np.testing.assert_array_equal(cube_v[0], [1.0, 0.0, 0.0])
        np.testing.assert_array_equal(cube_v[6], [5.0, 4.0, 4.0])

        transformed_cube_v = apply_transform(transform_matrix_for_rotation(rot))(cube_v)

        np.testing.assert_array_almost_equal(transformed_cube_v[0], [0.0, 0.0, -1.0])
        np.testing.assert_array_almost_equal(transformed_cube_v[6], [4, 4.0, -5.0])
Пример #8
0
def test_teapot_orthographic_projection():
    world_coords = teapot_verts()
    canvas_coords = apply_transform(
        world_to_canvas_orthographic_projection(
            width=800,
            height=600,
            position=np.array([0.5, 1.5, 0.0]),
            target=np.array([-0.5, 1.0, -2.0]),
            zoom=100,
        ))(world_coords, discard_z_coord=True)

    expected_head = np.array([[86.950483, 236.566495], [91.324232, 235.067513],
                              [84.079372, 238.229429]])
    expected_tail = np.array([[661.899462,
                               233.196566], [661.899462, 233.196566],
                              [662.424938, 233.68759]])
    np.testing.assert_array_almost_equal(canvas_coords[0:len(expected_head)],
                                         expected_head)
    np.testing.assert_array_almost_equal(canvas_coords[-len(expected_tail):],
                                         expected_tail)
Пример #9
0
def test_apply_transform_for_vectors():
    translation = np.array([3.0, 0.5, 2.0])
    transform = np.array(
        [
            [1, 0, 0, translation[0]],
            [0, 1, 0, translation[1]],
            [0, 0, 1, translation[2]],
            [0, 0, 0, 1],
        ]
    )

    points = np.array([[1.0, 2.0, 3.0], [5.0, 0.0, 1.0]])
    expected_points = np.array([[4.0, 2.5, 5.0], [8.0, 0.5, 3.0]])

    # Confidence check.
    transformer = apply_transform(transform)
    np.testing.assert_array_equal(transformer(points), expected_points)

    np.testing.assert_array_equal(
        transformer(points, treat_input_as_vector=True), points
    )