From 6b3bdb2631a4834ddcdc80805bdb5339a70a0d8e Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Tue, 5 May 2026 09:14:35 -0400 Subject: [PATCH] Remove redundant calculate_true_anomaly() function It was a duplicate of the true_anomaly calculation already performed by cartesian_to_orbital_elements(). Added a note in old_tests for future refactor suggesting the replacement usage. --- old_tests/test_periapsis_burn.cpp | 11 +++++++ src/orbital_mechanics.cpp | 49 ------------------------------- src/orbital_mechanics.h | 3 -- 3 files changed, 11 insertions(+), 52 deletions(-) diff --git a/old_tests/test_periapsis_burn.cpp b/old_tests/test_periapsis_burn.cpp index 9b50f6c..aa3d7d1 100644 --- a/old_tests/test_periapsis_burn.cpp +++ b/old_tests/test_periapsis_burn.cpp @@ -8,6 +8,17 @@ #include "../src/orbital_mechanics.h" #include +// NOTE: calculate_true_anomaly() was removed from the public API. +// It was redundant with cartesian_to_orbital_elements() which already +// computes true_anomaly as part of OrbitalElements. +// +// Suggested replacement for later refactor: +// OrbitalElements orbit = cartesian_to_orbital_elements(r, v, parent->mass); +// double true_anomaly = orbit.true_anomaly; +// +// Or even simpler — since the sim already calls cartesian_to_orbital_elements() +// each frame, just read craft->orbit.true_anomaly directly. + // Test prograde burn at periapsis (true anomaly = 0) // Verifies that the maneuver executes correctly when starting at periapsis TEST_CASE("Prograde burn at periapsis preserves periapsis distance", "[maneuver][periapsis]") { diff --git a/src/orbital_mechanics.cpp b/src/orbital_mechanics.cpp index fb1d124..f23b992 100644 --- a/src/orbital_mechanics.cpp +++ b/src/orbital_mechanics.cpp @@ -458,52 +458,3 @@ Vec3 calculate_eccentricity_vector(Vec3 r, Vec3 v, Vec3 h, double mu) { Vec3 r_over_mag = vec3_scale(r, 1.0 / r_mag); return vec3_sub(v_cross_h_over_mu, r_over_mag); } - -// Calculate true anomaly from position and velocity vectors -double calculate_true_anomaly(Vec3 r, Vec3 v, Vec3 e_vec, double e_mag, double r_mag) { - // For near-circular orbits, eccentricity vector is near-zero - // Compute true anomaly as the angle in the orbital plane - if (e_mag < 1e-10) { - Vec3 h = vec3_cross(r, v); - double h_mag = vec3_magnitude(h); - if (h_mag < 1e-10) return 0.0; - - // Create a coordinate system in the orbital plane - Vec3 z_hat = vec3_scale(h, 1.0 / h_mag); - // Choose x-axis as cross product of Z (world up) and orbit normal - // This gives a consistent reference direction in the orbital plane - Vec3 world_z = {0.0, 0.0, 1.0}; - Vec3 x_hat = vec3_cross(world_z, z_hat); - double x_hat_mag = vec3_magnitude(x_hat); - if (x_hat_mag < 1e-10) { - // Orbit is equatorial, use world X as reference - x_hat = (Vec3){1.0, 0.0, 0.0}; - } else { - x_hat = vec3_scale(x_hat, 1.0 / x_hat_mag); - } - Vec3 y_hat = vec3_cross(z_hat, x_hat); - - // Project position onto this orbital plane coordinate system - double x_proj = vec3_dot(r, x_hat); - double y_proj = vec3_dot(r, y_hat); - - // True anomaly is the angle in the orbital plane - double nu = atan2(y_proj, x_proj); - if (nu < 0) nu += 2.0 * M_PI; - return nu; - } - - // Standard calculation using eccentricity vector - double cos_nu = vec3_dot(e_vec, r) / (e_mag * r_mag); - cos_nu = fmax(-1.0, fmin(1.0, cos_nu)); - double nu = acos(cos_nu); - - // Determine correct quadrant using cross product - Vec3 r_cross_v = vec3_cross(r, v); - double r_cross_v_dot_e = vec3_dot(r_cross_v, e_vec); - if (r_cross_v_dot_e < 0) { - nu = 2.0 * M_PI - nu; - } - - return nu; -} diff --git a/src/orbital_mechanics.h b/src/orbital_mechanics.h index 8b622da..e66a835 100644 --- a/src/orbital_mechanics.h +++ b/src/orbital_mechanics.h @@ -56,7 +56,4 @@ double angular_distance(double a, double b); // Calculate eccentricity vector from state vectors Vec3 calculate_eccentricity_vector(Vec3 r, Vec3 v, Vec3 h, double mu); -// Calculate true anomaly from position and velocity vectors -double calculate_true_anomaly(Vec3 r, Vec3 v, Vec3 e_vec, double e_mag, double r_mag); - #endif