diff --git a/vector.cpp b/vector.cpp index fcb1a0b4..82b7862c 100644 --- a/vector.cpp +++ b/vector.cpp @@ -162,6 +162,28 @@ vector::normalised (void) const { } +vector +vector::spherical_to_cartesian (const vector &s) { + return { + s.x * sin (s.y) * cos (s.z), + s.x * sin (s.y) * sin (s.z), + s.x * cos (s.y), + }; +} + + +vector +vector::cartesian_to_spherical (const vector &c) { + double mag = c.magnitude (); + + return { + mag, + acos (c.z / mag), + atan2 (c.y, c.x) + }; +} + + void vector::sanity (void) const { check_soft (!std::isnan (x)); diff --git a/vector.hpp b/vector.hpp index e07ac756..faf39ed6 100644 --- a/vector.hpp +++ b/vector.hpp @@ -53,6 +53,9 @@ namespace util { vector& normalise (void); vector normalised (void) const; + static vector spherical_to_cartesian (const vector &); + static vector cartesian_to_spherical (const vector &); + void sanity (void) const; }; }