Add spherical/cartesian transforms

This commit is contained in:
Danny Robson 2011-10-29 23:14:07 +11:00
parent 16d05e9ea6
commit ea50f64857
2 changed files with 25 additions and 0 deletions

View File

@ -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 void
vector::sanity (void) const { vector::sanity (void) const {
check_soft (!std::isnan (x)); check_soft (!std::isnan (x));

View File

@ -53,6 +53,9 @@ namespace util {
vector& normalise (void); vector& normalise (void);
vector normalised (void) const; vector normalised (void) const;
static vector spherical_to_cartesian (const vector &);
static vector cartesian_to_spherical (const vector &);
void sanity (void) const; void sanity (void) const;
}; };
} }