# Source code for psychopy.tools.coordinatetools

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Part of the PsychoPy library
# Copyright (C) 2002-2018 Jonathan Peirce (C) 2019-2021 Open Science Tools Ltd.

"""Functions and classes related to coordinate system conversion
"""
from __future__ import absolute_import, division, print_function

from past.utils import old_div
import numpy

[docs]def cart2pol(x, y, units='deg'):
"""Convert from cartesian to polar coordinates.

:usage:

theta, radius = cart2pol(x, y, units='deg')

units refers to the units (rad or deg) for theta that should be returned
"""
theta = numpy.arctan2(y, x)
if units in ('deg', 'degs'):
theta = theta * 180 / numpy.pi

"""Convert from polar to cartesian coordinates.

usage::

"""
if units in ('deg', 'degs'):
theta = theta * numpy.pi / 180.0

return xx, yy

[docs]def cart2sph(z, y, x):
"""Convert from cartesian coordinates (x,y,z) to spherical (elevation,
azimuth, radius). Output is in degrees.

usage:
OR

If working in DKL space, z = Luminance, y = S and x = LM
"""
width = len(z)

elevation = numpy.empty([width, width])
azimuth = numpy.empty([width, width])

radius = numpy.sqrt(x**2 + y**2 + z**2)
azimuth = numpy.arctan2(y, x)
# Calculating the elevation from x,y up
elevation = numpy.arctan2(z, numpy.sqrt(x**2 + y**2))

# convert azimuth and elevation angles into degrees
azimuth *= (old_div(180.0, numpy.pi))
elevation *= (old_div(180.0, numpy.pi))

sphere = numpy.rollaxis(sphere, 0, 3)

return sphere

[docs]def sph2cart(*args):
"""Convert from spherical coordinates (elevation, azimuth, radius)
to cartesian (x,y,z).

usage:
OR
"""
if len(args) == 1:  # received an Nx3 array
elev = args[0][0, :]
azim = args[0][1, :]
returnAsArray = True
elif len(args) == 3:
elev = args[0]
azim = args[1]