Sign in to follow this  

Converting Vector3D to Sphere Angles

This topic is 3776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, I have a problem in that I want to extract the angles (phi and theta) from a specified 3D vector. For example, if I pass in the vector(1.0, 0.0, 0.0) then this would return (phi = 90.0 and theta = 90.0). Similarly if I pass in (0.0, 0.0, -1.0) then the angles would be 90.0 and 180.0 respectively. Any advices appreciated. MAMEman.

Share this post


Link to post
Share on other sites
Quote:
Original post by mameman
I have a problem in that I want to extract the angles (phi and theta) from a specified 3D vector. For example, if I pass in the vector(1.0, 0.0, 0.0) then this would return (phi = 90.0 and theta = 90.0). Similarly if I pass in (0.0, 0.0, -1.0) then the angles would be 90.0 and 180.0 respectively.

Any advices appreciated.
This is a lazy answer, but here's the corresponding conversion function from the CML:

/* Convert Cartesian coordinates to spherical coordinates in R3 */
template < class VecT, typename Real > void
cartesian_to_spherical(const VecT& v, Real& radius, Real& theta, Real& phi,
size_t axis, SphericalType type,
Real tolerance = epsilon<Real>::placeholder())
{
typedef Real value_type;

/* Checking */
detail::CheckVec3(v);
detail::CheckIndex3(axis);

size_t i, j, k;
cyclic_permutation(axis, i, j, k); // i=axis,j=(i+1)%3,k=(j+1)%3

value_type len = length(v[j],v[k]); // length(x,y)=sqrt(x*x+y*y)
theta = len < tolerance ? value_type(0) : std::atan2(v[k],v[j]);
radius = length(v[i], len);
if (radius < tolerance) {
phi = value_type(0);
} else {
phi = std::atan2(len,v[i]);
if (type == latitude) {
phi = constants<value_type>::pi_over_2() - phi;
}
}
}




I can't quite tell from your example what conventions you're using, but in the function shown above, the axis argument indicates which cardinal axis should be considered 'up' for the purpose of the conversion (0=x, 1=y, 2=z), and the type argument refers to whether phi should be considered to be latitude or colatitude.

Given this information, you should be able to adapt the above function to your needs. For more info on the underlying math, just Google 'spherical coordinates'.

Share this post


Link to post
Share on other sites
@jyk. I always like those general solutions ;). Yes this has given me enough information to implement a solution for my particular problem. I can also make some assumptions in my case that reduces the maths to a few simple equations.

@wentworth. Yes you are correct. However my system is the one used by OGL i.e. z(coming out of the screen), y(up) and x(left). So in my case its theta = arctan(x/z) and phi = arcsin(r/rho) ;). Anyways thanks.

Cheers guys,

MAMEman.

Share this post


Link to post
Share on other sites

This topic is 3776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this