I am implementing a type for Ogre 3D rendering engine to provide spherical coordinates. So far everything is working fine, until I try to build spherical coordinates from a cartesian vector. Here is the code I am trying right now, but it's not working correctly (changing phi and theta result in only half a sphere).
/** @return a relative spherical coordinate from a cartesian vector. */
static SphereVector from_cartesian( const Ogre::Vector3& cartesian )
{
using namespace Ogre;
SphereVector result;
result.radius = cartesian.length();
result.phi = cartesian.x > Real(0) || cartesian.x < Real(0) ? Math::ATan( cartesian.z / cartesian.x ) : Radian( Math::HALF_PI );
result.theta = result.radius > Real(0) || result.radius < Real(0) ? Math::ACos( cartesian.y / result.radius ) : Radian( 0 );
return result;
}
Here is the version currently in the repository:
static SphereVector from_cartesian( const Ogre::Vector3& cartesian )
{
using namespace Ogre;
SphereVector result;
result.radius = cartesian.length();
result.phi = cartesian.x > Real(0) || cartesian.x < Real(0) ? Math::ATan( cartesian.z / cartesian.x ) : Math::ATan( 0 );
result.theta = result.radius > Real(0) || result.radius < Real(0) ? Math::ACos( cartesian.y / result.radius ) : Math::ACos( 0 );
return result;
}
I have been searching for the right algorithm and I think I'm close but I can't figure out it correctly yet. My maths skills are not so good so I'm having trouble understanding what I'm doing wrong.
Any idea how it should be done?