Browse Source

fix: Improve near-parabolic conversion numerical stability

- Add near-parabolic detection for |1-e| < 0.01
- Use cosine/sine formulation with atan2() to preserve quadrant
- Avoid catastrophic cancellation in sqrt((1+e)/(1-e)) factor
- Clamp values to [-1,1] to handle precision issues
- Maintain original formula for well-behaved orbits
main
cinnaboot 5 months ago
parent
commit
ed185dfbfe
  1. 18
      src/orbital_mechanics.cpp

18
src/orbital_mechanics.cpp

@ -89,7 +89,23 @@ double solve_kepler_hyperbolic(double mean_anomaly, double eccentricity) {
}
double eccentric_to_true_anomaly(double eccentric_anomaly, double eccentricity) {
// E to true anomaly conversion: tan(ν/2) = √((1+e)/(1-e)) · tan(E/2)
if (fabs(1.0 - eccentricity) < 0.01) {
double E = eccentric_anomaly;
double e = eccentricity;
double cos_E = cos(E);
double sin_E = sin(E);
double denominator = 1.0 - e * cos_E;
double cos_nu = (cos_E - e) / denominator;
double sin_nu = sin_E * sqrt(1.0 - e * e) / denominator;
cos_nu = fmax(-1.0, fmin(1.0, cos_nu));
sin_nu = fmax(-1.0, fmin(1.0, sin_nu));
return atan2(sin_nu, cos_nu);
}
double tan_half_E = tan(eccentric_anomaly / 2.0);
double tan_half_nu = sqrt((1.0 + eccentricity) / (1.0 - eccentricity)) * tan_half_E;
return 2.0 * atan(tan_half_nu);

Loading…
Cancel
Save