Reputation: 143
I want to map cartesian coordinates (x,y,z) to bispherical coordinates (sigma,tau,phi) and back accourding to wikipedia.
Here are my functions:
public static Vector3 ProjectBisphericalToCartesian(Vector3 bispherical, double a)
{
var sigma = (double)bispherical.X;
var tau = (double)bispherical.Y;
var phi = (double)bispherical.Z;
var d = Math.Cosh(tau) - Math.Cos(sigma);
var s = a / d;
var x = s * Math.Sin(sigma) * Math.Cos(phi);
var y = s * Math.Sin(sigma) * Math.Sin(phi);
var z = s * Math.Sinh(tau);
return new Vector3(
(float)x,
(float)y,
(float)z
);
}
public static Vector3 ProjectCartesianToBispherical(Vector3 cartesian, double a)
{
var x = (double)cartesian.X;
var y = (double)cartesian.Y;
var z = (double)cartesian.Z;
var R = Math.Sqrt(x * x + y * y + z * z);
var s = R * R + a * a;
var t = 2.0 * a * z;
var Q = Math.Sqrt(s * s - t * t);
var sigma = Math.Acos((R * R - a * a) / Q);
var tau = Asinh(t / Q);
var phi = Math.Atan(y / x);
return new Vector3(
(float)sigma,
(float)tau,
(float)phi
);
}
// sinh^-1 ("areasinus hyperbolicus")
private static double Asinh(double x)
{
return Math.Log(x + Math.Sqrt(x * x + 1.0));
}
I test the function by generating random numbers for (x,y,z) and a, convert them to bispherical coordinates and back to cartesian and check whether or not they are equal to the original cartesian corrdinates (up to some small tolerance).
It turns out that some cartesian coordinates flip the sign of its x and y component (z is okay) while other's do not. For example {X:-5,3434 Y:2,569566 Z:-1,195607} flips the sign of the x and y component while {X:7,586471 Y:-6,154929 Z:1,494778} works fine.
I can not find an error in my code unfortunately. The issue appears to be caused by negative x values in the original cartesian coordinates which will cause the sign of x and y to be flipped after conversion and back. I just don't see why that happens.
Does anybody have an idea what is going on here ?
Upvotes: 1
Views: 176
Reputation: 143
Okay, I got it. Wolfram was mentioning something about half-planes so it occurred to me that using Atan2(y, x) instead of Atan(y/x) could fix the issue and it did.
Upvotes: 0
Reputation: 1197
Well, I don’t actually have the answer for you but maybe by putting our heads together we can do it.
First of all it looks to me like you did correctly transcribe the Wikipedia formulas. So what kind of a problem is this? Either the Math functions don’t do exactly the same thing as the functions referenced in Wikipedia, or else there is an error in Wikipedia.
At a guess I would not think that the methods in Math are differently defined. These functions have been around a long time and are very conventional. Maybe Wikipedia is wrong.
The type of error you have is a sign error. So you should look at what happens when the signs of one or two of the inputs changes. For example if we flip the sign of x then the sign of phi changes (it moves 180 degrees) in the conversion to bispherical. Correspondingly in the conversion to Cartesian, if we flip the sign of phi then the sign of x changes due to the cos(phi).
However if we flip the signs of both x and y then the sign of phi does not change. Neither do the other bispherical calculations change since they only use x and y within squaring. On the other hand if we change the sign of phi in the conversion to Cartesian, the signs of x and y do change. So there are two points in the Cartesian system that map to the same bispherical coordinates, while the two corresponding bisperical coordinates do map back to different Cartesian coordinates. It would seem that you need to introduce a sign in the conversion of x and y to phi. (Or, conceivably a sign in one of the other bispherical coordinates to make up for the lack of sign in phi.)
Upvotes: 2