Sunday, October 3, 2010

Spherical Coordinate Conversion with up = Y

There are many Spherical to Cartesian Coordinate conversion around the internet. But as I already explained about the confusion, it might be hard for you to find this error if you haven't notice it. My program (unluckily) was made by programmer (me), so the up value is y instead of the mathematician standard z. So I have to change the conversion formula a little bit.

This is the standard formula for up=z:

Vector3D Vector3D::GetSphericalCoordinate() {
    double xOutput = sqrt(x*x + y*y + z*z);

    double yOutput = acos(z/xOutput);
    yOutput = yOutput * 180.0 / 3.14159265;
   
    double zOutput = atan2(y, x);
    zOutput = zOutput * 180.0 / 3.14159265;

    return Vector3D(xOutput, yOutput, zOutput);
}

Since the y and z value is rotated, here is the formula for up=y:

Vector3D Vector3D::GetSphericalCoordinate() {
    double xOutput = sqrt(x*x + y*y + z*z);

    double yOutput = acos(y/xOutput);
    yOutput = yOutput * 180.0 / 3.14159265;
   
    double zOutput = atan2(z, x);
    zOutput = zOutput * 180.0 / 3.14159265;

    return Vector3D(xOutput, yOutput, zOutput);
}

I use this second code for my program. We also have to do the same modification for spherical to cartesian coordinate. Instead of using this for up=z:

double xOutput = x * getSinValue(y) * getCosValue(z);
double yOutput = x * getSinValue(y) * getSinValue(z);
double zOutput = x * getCosValue(y);


I did this for up=y:

double xOutput = x * getSinValue(y) * getCosValue(z);
double zOutput = x * getSinValue(y) * getSinValue(z);
double yOutput = x * getCosValue(y);
   
Notice that my phi and theta is y and z respectively. So I represent my spherical coordinate as a 3D Vector (radius, phi, theta). Phi is identical to elevation, and theta is the azimuth.

No comments: