Example #1
0
    def test_spherical_to_cartesian(self):
        """
        Test :func:`colour.algebra.coordinates.transformations.\
spherical_to_cartesian` definition.
        """

        np.testing.assert_almost_equal(
            spherical_to_cartesian(
                np.array([6.78232998, 0.48504979, 0.32175055])),
            np.array([3.00000000, 0.99999999, 6.00000000]),
            decimal=7,
        )

        np.testing.assert_almost_equal(
            spherical_to_cartesian(
                np.array([18.38477631, 0.51501513, 1.68145355])),
            np.array([-1.00000003, 9.00000007, 15.99999996]),
            decimal=7,
        )

        np.testing.assert_almost_equal(
            spherical_to_cartesian(
                np.array([19.64342307, 0.33250603, -0.14626640])),
            np.array([6.34339996, -0.93449999, 18.56750001]),
            decimal=7,
        )
    def test_n_dimensional_spherical_to_cartesian(self):
        """
        Tests :func:`colour.algebra.coordinates.transformations.\
spherical_to_cartesian` definition n-dimensional arrays support.
        """

        a_i = np.array([6.78232998, 1.08574654, 0.32175055])
        a_o = np.array([3, 1, 6])
        np.testing.assert_almost_equal(
            spherical_to_cartesian(a_i),
            a_o,
            decimal=7)

        a_i = np.tile(a_i, (6, 1))
        a_o = np.tile(a_o, (6, 1))
        np.testing.assert_almost_equal(
            spherical_to_cartesian(a_i),
            a_o,
            decimal=7)

        a_i = np.reshape(a_i, (2, 3, 3))
        a_o = np.reshape(a_o, (2, 3, 3))
        np.testing.assert_almost_equal(
            spherical_to_cartesian(a_i),
            a_o,
            decimal=7)
Example #3
0
    def test_n_dimensional_spherical_to_cartesian(self):
        """
        Tests :func:`colour.algebra.coordinates.transformations.\
spherical_to_cartesian` definition n-dimensional arrays support.
        """

        vector_i = np.array([6.78232998, 1.08574654, 0.32175055])
        vector_o = np.array([3, 1, 6])
        np.testing.assert_almost_equal(
            spherical_to_cartesian(vector_i),
            vector_o,
            decimal=7)

        vector_i = np.tile(vector_i, (6, 1))
        vector_o = np.tile(vector_o, (6, 1))
        np.testing.assert_almost_equal(
            spherical_to_cartesian(vector_i),
            vector_o,
            decimal=7)

        vector_i = np.reshape(vector_i, (2, 3, 3))
        vector_o = np.reshape(vector_o, (2, 3, 3))
        np.testing.assert_almost_equal(
            spherical_to_cartesian(vector_i),
            vector_o,
            decimal=7)
    def test_nan_spherical_to_cartesian(self):
        """
        Tests :func:`colour.algebra.coordinates.transformations.\
spherical_to_cartesian` definition nan support.
        """

        cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
        cases = set(permutations(cases * 3, r=3))
        for case in cases:
            a_i = np.array(case)
            spherical_to_cartesian(a_i)
Example #5
0
    def test_nan_spherical_to_cartesian(self):
        """
        Tests :func:`colour.algebra.coordinates.transformations.\
spherical_to_cartesian` definition nan support.
        """

        cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
        cases = set(permutations(cases * 3, r=3))
        for case in cases:
            a_i = np.array(case)
            spherical_to_cartesian(a_i)
Example #6
0
    def test_spherical_to_cartesian(self):
        """
        Tests
        :func:`colour.algebra.coordinates.transformations.spherical_to_cartesian`  # noqa
        definition.
        """

        np.testing.assert_almost_equal(spherical_to_cartesian(
            (6.78232998, 1.08574654, 0.32175055)),
                                       np.array([3., 0.99999999, 6.]),
                                       decimal=7)
        np.testing.assert_almost_equal(
            spherical_to_cartesian((18.38477631, 1.05578119, 1.68145355)),
            np.array([-1.00000003, 9.00000007, 15.99999996]),
            decimal=7)
        np.testing.assert_almost_equal(
            spherical_to_cartesian((19.64342307, 1.2382903, -0.1462664)),
            np.array([6.34339996, -0.93449999, 18.56750001]),
            decimal=7)
Example #7
0
    def test_spherical_to_cartesian(self):
        """
        Tests
        :func:`colour.algebra.coordinates.transformations.spherical_to_cartesian`  # noqa
        definition.
        """

        np.testing.assert_almost_equal(
            spherical_to_cartesian((6.78232998, 1.08574654, 0.32175055)),
            np.array([3., 0.99999999, 6.]),
            decimal=7)
        np.testing.assert_almost_equal(
            spherical_to_cartesian((18.38477631, 1.05578119, 1.68145355)),
            np.array([-1.00000003, 9.00000007, 15.99999996]),
            decimal=7)
        np.testing.assert_almost_equal(
            spherical_to_cartesian((19.64342307, 1.2382903, -0.1462664)),
            np.array([6.34339996, -0.93449999, 18.56750001]),
            decimal=7)
Example #8
0
    def test_n_dimensional_spherical_to_cartesian(self):
        """
        Test :func:`colour.algebra.coordinates.transformations.\
spherical_to_cartesian` definition n-dimensional arrays support.
        """

        a_i = np.array([6.78232998, 0.48504979, 0.32175055])
        a_o = spherical_to_cartesian(a_i)

        a_i = np.tile(a_i, (6, 1))
        a_o = np.tile(a_o, (6, 1))
        np.testing.assert_almost_equal(spherical_to_cartesian(a_i),
                                       a_o,
                                       decimal=7)

        a_i = np.reshape(a_i, (2, 3, 3))
        a_o = np.reshape(a_o, (2, 3, 3))
        np.testing.assert_almost_equal(spherical_to_cartesian(a_i),
                                       a_o,
                                       decimal=7)
Example #9
0
def primitive_vertices_sphere(radius=0.5,
                              segments=8,
                              intermediate=False,
                              origin=np.array([0, 0, 0]),
                              axis='+z'):
    """
    Returns the vertices of a latitude-longitude sphere primitive.

    Parameters
    ----------
    radius: numeric, optional
        Sphere radius.
    segments: numeric, optional
        Latitude-longitude segments, if the ``intermediate`` argument is
        *True*, then the sphere will have one less segment along its longitude.
    intermediate: bool, optional
        Whether to generate the sphere vertices at the center of the faces
        outlined by the segments of a regular sphere generated without
        the ``intermediate`` argument set to *True*. The resulting sphere is
        inscribed on the regular sphere faces but possesses the same poles.
    origin: array_like, optional
        Sphere origin on the construction plane.
    axis : array_like, optional
        **{'+z', '+x', '+y', 'yz', 'xz', 'xy'}**,
        Axis (or normal of the plane) the poles of the sphere will be aligned
        with.

    Returns
    -------
    ndarray
        Sphere primitive vertices.

    Notes
    -----
    -   The sphere poles have latitude segments count - 1 co-located vertices.

    Examples
    --------
    >>> primitive_vertices_sphere(segments=4)  # doctest: +ELLIPSIS
    array([[[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [ -3.5355339...e-01,  -4.3297802...e-17,   3.5355339...e-01],
            [ -5.0000000...e-01,  -6.1232340...e-17,   3.0616170...e-17],
            [ -3.5355339...e-01,  -4.3297802...e-17,  -3.5355339...e-01],
            [ -6.1232340...e-17,  -7.4987989...e-33,  -5.0000000...e-01]],
    <BLANKLINE>
           [[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [  2.1648901...e-17,  -3.5355339...e-01,   3.5355339...e-01],
            [  3.0616170...e-17,  -5.0000000...e-01,   3.0616170...e-17],
            [  2.1648901...e-17,  -3.5355339...e-01,  -3.5355339...e-01],
            [  3.7493994...e-33,  -6.1232340...e-17,  -5.0000000...e-01]],
    <BLANKLINE>
           [[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [  3.5355339...e-01,   0.0000000...e+00,   3.5355339...e-01],
            [  5.0000000...e-01,   0.0000000...e+00,   3.0616170...e-17],
            [  3.5355339...e-01,   0.0000000...e+00,  -3.5355339...e-01],
            [  6.1232340...e-17,   0.0000000...e+00,  -5.0000000...e-01]],
    <BLANKLINE>
           [[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [  2.1648901...e-17,   3.5355339...e-01,   3.5355339...e-01],
            [  3.0616170...e-17,   5.0000000...e-01,   3.0616170...e-17],
            [  2.1648901...e-17,   3.5355339...e-01,  -3.5355339...e-01],
            [  3.7493994...e-33,   6.1232340...e-17,  -5.0000000...e-01]]])
    """

    axis = PLANE_TO_AXIS_MAPPING.get(axis, axis).lower()

    if not intermediate:
        theta = np.tile(np.radians(np.linspace(0, 180, segments + 1)),
                        (segments + 1, 1))
        phi = np.transpose(
            np.tile(np.radians(np.linspace(-180, 180, segments + 1)),
                    (segments + 1, 1)))
    else:
        theta = np.tile(
            np.radians(np.linspace(0, 180, segments * 2 + 1)[1::2][1:-1]),
            (segments + 1, 1))
        theta = np.hstack([
            zeros([segments + 1, 1]),
            theta,
            full([segments + 1, 1], np.pi),
        ])
        phi = np.transpose(
            np.tile(
                np.radians(np.linspace(-180, 180, segments + 1)) +
                np.radians(360 / segments / 2), (segments, 1)))

    rho = ones(phi.shape) * radius
    rho_theta_phi = tstack([rho, theta, phi])

    vertices = spherical_to_cartesian(rho_theta_phi)

    # Removing extra longitude vertices.
    vertices = vertices[:-1, :, :]

    if axis == '+z':
        pass
    elif axis == '+y':
        vertices = np.roll(vertices, 2, -1)
    elif axis == '+x':
        vertices = np.roll(vertices, 1, -1)
    else:
        raise ValueError('Axis must be one of "{0}"!'.format(
            ['+x', '+y', '+z']))

    vertices += origin

    return vertices
Example #10
0
def primitive_vertices_sphere(
    radius: Floating = 0.5,
    segments: Integer = 8,
    intermediate: Boolean = False,
    origin: ArrayLike = np.array([0, 0, 0]),
    axis: Union[Literal["+z", "+x", "+y", "yz", "xz", "xy"], str] = "+z",
) -> NDArray:
    """
    Return the vertices of a latitude-longitude sphere primitive.

    Parameters
    ----------
    radius
        Sphere radius.
    segments
        Latitude-longitude segments, if the ``intermediate`` argument is
        *True*, then the sphere will have one less segment along its longitude.
    intermediate
        Whether to generate the sphere vertices at the center of the faces
        outlined by the segments of a regular sphere generated without
        the ``intermediate`` argument set to *True*. The resulting sphere is
        inscribed on the regular sphere faces but possesses the same poles.
    origin
        Sphere origin on the construction plane.
    axis
        Axis (or normal of the plane) the poles of the sphere will be aligned
        with.

    Returns
    -------
    :class:`numpy.ndarray`
        Sphere primitive vertices.

    Notes
    -----
    -   The sphere poles have latitude segments count - 1 co-located vertices.

    Examples
    --------
    >>> primitive_vertices_sphere(segments=4)  # doctest: +ELLIPSIS
    array([[[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [ -3.5355339...e-01,  -4.3297802...e-17,   3.5355339...e-01],
            [ -5.0000000...e-01,  -6.1232340...e-17,   3.0616170...e-17],
            [ -3.5355339...e-01,  -4.3297802...e-17,  -3.5355339...e-01],
            [ -6.1232340...e-17,  -7.4987989...e-33,  -5.0000000...e-01]],
    <BLANKLINE>
           [[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [  2.1648901...e-17,  -3.5355339...e-01,   3.5355339...e-01],
            [  3.0616170...e-17,  -5.0000000...e-01,   3.0616170...e-17],
            [  2.1648901...e-17,  -3.5355339...e-01,  -3.5355339...e-01],
            [  3.7493994...e-33,  -6.1232340...e-17,  -5.0000000...e-01]],
    <BLANKLINE>
           [[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [  3.5355339...e-01,   0.0000000...e+00,   3.5355339...e-01],
            [  5.0000000...e-01,   0.0000000...e+00,   3.0616170...e-17],
            [  3.5355339...e-01,   0.0000000...e+00,  -3.5355339...e-01],
            [  6.1232340...e-17,   0.0000000...e+00,  -5.0000000...e-01]],
    <BLANKLINE>
           [[  0.0000000...e+00,   0.0000000...e+00,   5.0000000...e-01],
            [  2.1648901...e-17,   3.5355339...e-01,   3.5355339...e-01],
            [  3.0616170...e-17,   5.0000000...e-01,   3.0616170...e-17],
            [  2.1648901...e-17,   3.5355339...e-01,  -3.5355339...e-01],
            [  3.7493994...e-33,   6.1232340...e-17,  -5.0000000...e-01]]])
    """

    axis = MAPPING_PLANE_TO_AXIS.get(axis, axis).lower()
    axis = validate_method(axis, ["+x", "+y", "+z"],
                           '"{0}" axis invalid, it must be one of {1}!')

    if not intermediate:
        theta = np.tile(
            np.radians(np.linspace(0, 180, segments + 1)),
            (int(segments) + 1, 1),
        )
        phi = np.transpose(
            np.tile(
                np.radians(np.linspace(-180, 180, segments + 1)),
                (int(segments) + 1, 1),
            ))
    else:
        theta = np.tile(
            np.radians(np.linspace(0, 180, segments * 2 + 1)[1::2][1:-1]),
            (int(segments) + 1, 1),
        )
        theta = np.hstack([
            zeros((segments + 1, 1)),
            theta,
            full((segments + 1, 1), np.pi),
        ])
        phi = np.transpose(
            np.tile(
                np.radians(np.linspace(-180, 180, segments + 1)) +
                np.radians(360 / segments / 2),
                (int(segments), 1),
            ))

    rho = ones(phi.shape) * radius
    rho_theta_phi = tstack([rho, theta, phi])

    vertices = spherical_to_cartesian(rho_theta_phi)

    # Removing extra longitude vertices.
    vertices = vertices[:-1, :, :]

    if axis == "+z":
        pass
    elif axis == "+y":
        vertices = np.roll(vertices, 2, -1)
    elif axis == "+x":
        vertices = np.roll(vertices, 1, -1)

    vertices += origin

    return vertices