From de4dd704077b806310a7d97a8ce08e0aeada5a76 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sat, 3 Aug 2019 13:44:43 -0700 Subject: [PATCH] Reorg util/math -> math. --- include/wrmath/geom/orientation.h | 15 ++++----- include/wrmath/geom/vector.h | 18 +++++------ include/wrmath/math.h | 43 +++++++++++++++++++++++++ include/wrmath/util/math.h | 39 ----------------------- src/math.cc | 52 +++++++++++++++++++++++++++++++ src/orientation.cc | 28 ----------------- test/orientation_test.cc | 27 +++++++++++++--- 7 files changed, 133 insertions(+), 89 deletions(-) create mode 100644 include/wrmath/math.h delete mode 100644 include/wrmath/util/math.h create mode 100644 src/math.cc diff --git a/include/wrmath/geom/orientation.h b/include/wrmath/geom/orientation.h index 2cc4252..831ffa6 100644 --- a/include/wrmath/geom/orientation.h +++ b/include/wrmath/geom/orientation.h @@ -1,6 +1,9 @@ -// -// Created by kyle on 8/3/19. -// +/** + * orientation.h concerns itself with computing the orientation of some + * vector with respect to a reference plane that is assumed to be the + * of the Earth. + */ + #ifndef __WRMATH_GEOM_ORIENTATION_H #define __WRMATH_GEOM_ORIENTATION_H @@ -13,12 +16,6 @@ namespace wr { namespace geom { -float RadiansToDegreesF(float rads); -double RadiansToDegreesD(double rads); -float DegreesToRadiansF(float degrees); -double DegreesToRadiansD(double degrees); - - constexpr uint8_t Basis_i = 0; constexpr uint8_t Basis_j = 1; constexpr uint8_t Basis_k = 2; diff --git a/include/wrmath/geom/vector.h b/include/wrmath/geom/vector.h index 46784a4..1c13b55 100644 --- a/include/wrmath/geom/vector.h +++ b/include/wrmath/geom/vector.h @@ -9,7 +9,7 @@ #include #include -#include +#include namespace wr { @@ -29,18 +29,18 @@ public: * The default constructor creates a zero vector for a given * type and size. */ - Vector() { wr::util::DefaultEpsilon(this->epsilon); } + Vector() { wr::math::DefaultEpsilon(this->epsilon); } /** * If given an initializer_list, the vector is created with * those values. There must be exactly N elements in the list. - * @param ilst + * @param ilstutil */ Vector(std::initializer_list ilst) { assert(ilst.size() == N); - wr::util::DefaultEpsilon(this->epsilon); + wr::math::DefaultEpsilon(this->epsilon); std::copy(ilst.begin(), ilst.end(), this->arr.begin()); } @@ -81,7 +81,7 @@ public: isZero() const { for (size_t i = 0; i < N; i++) { - if (!wr::util::WithinTolerance(this->arr[i], (T)0.0, this->epsilon)) { + if (!wr::math::WithinTolerance(this->arr[i], (T)0.0, this->epsilon)) { return false; } } @@ -107,7 +107,7 @@ public: bool isUnitVector() const { - return wr::util::WithinTolerance(this->magnitude(), (T)1.0, this->epsilon); + return wr::math::WithinTolerance(this->magnitude(), (T)1.0, this->epsilon); } @@ -142,7 +142,7 @@ public: } T angle = this->angle(other); - if (wr::util::WithinTolerance(angle, (T)0.0, this->epsilon)) { + if (wr::math::WithinTolerance(angle, (T)0.0, this->epsilon)) { return true; } @@ -163,7 +163,7 @@ public: return true; } - return wr::util::WithinTolerance(*this * other, (T)0.0, this->epsilon); + return wr::math::WithinTolerance(*this * other, (T)0.0, this->epsilon); } @@ -287,7 +287,7 @@ public: */ bool operator==(const Vector &other) const { for (size_t i = 0; iarr[i], other.arr[i], this->epsilon)) { + if (!wr::math::WithinTolerance(this->arr[i], other.arr[i], this->epsilon)) { return false; } } diff --git a/include/wrmath/math.h b/include/wrmath/math.h new file mode 100644 index 0000000..a1da156 --- /dev/null +++ b/include/wrmath/math.h @@ -0,0 +1,43 @@ +#ifndef __WRMATH_UTIL_MATH_H +#define __WRMATH_UTIL_MATH_H + + +#include + + +namespace wr { +namespace math { + + +/** + * Convert radians to degrees. + * @param rads the angle in radians + * @return the angle in degrees, + */ +float RadiansToDegreesF(float rads); +double RadiansToDegreesD(double rads); +float DegreesToRadiansF(float degrees); +double DegreesToRadiansD(double degrees); + + + +const double Epsilon_double = 0.0001; +const float Epsilon_float = 0.0001; + + +void DefaultEpsilon(double &epsilon); +void DefaultEpsilon(float &epsilon); + +template +static T +WithinTolerance(T a, T b, T epsilon) +{ + return std::abs(a - b) < epsilon; +} + + +} // namespace math +} // namespace wr + + +#endif // __WRMATH_UTIL_MATH_H diff --git a/include/wrmath/util/math.h b/include/wrmath/util/math.h deleted file mode 100644 index fea12b0..0000000 --- a/include/wrmath/util/math.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef __WRMATH_UTIL_MATH_H -#define __WRMATH_UTIL_MATH_H - - -namespace wr { -namespace util { - - -const double Epsilon_double = 0.0001; -const float Epsilon_float = 0.0001; - - -static void -DefaultEpsilon(double &epsilon) -{ - epsilon = Epsilon_double; -} - - -static void -DefaultEpsilon(float &epsilon) -{ - epsilon = Epsilon_float; -} - - -template -static T -WithinTolerance(T a, T b, T epsilon) -{ - return std::abs(a - b) < epsilon; -} - - -} // namespace util -} // namespace wr - - -#endif // __WRMATH_UTIL_MATH_H diff --git a/src/math.cc b/src/math.cc new file mode 100644 index 0000000..3d6d2b3 --- /dev/null +++ b/src/math.cc @@ -0,0 +1,52 @@ +#include + + +namespace wr { +namespace math { + + +float +RadiansToDegreesF(float rads) +{ + return rads * (180.0 / M_PI); +} + + +double +RadiansToDegreesD(double rads) +{ + return rads * (180.0 / M_PI); +} + + +float +DegreesToRadiansF(float degrees) +{ + return degrees * M_PI / 180.0; +} + + +double +DegreesToRadiansD(double degrees) +{ + return degrees * M_PI / 180.0; +} + + +void +DefaultEpsilon(double &epsilon) +{ + epsilon = Epsilon_double; +} + + +void +DefaultEpsilon(float &epsilon) +{ + epsilon = Epsilon_float; +} + + +} // namespace math +} // namespace wr + diff --git a/src/orientation.cc b/src/orientation.cc index d5dda1a..3d33bde 100644 --- a/src/orientation.cc +++ b/src/orientation.cc @@ -6,34 +6,6 @@ namespace wr { namespace geom { -float -RadiansToDegreesF(float rads) -{ - return rads * (180.0 / M_PI); -} - - -double -RadiansToDegreesD(double rads) -{ - return rads * (180.0 / M_PI); -} - - -float -DegreesToRadiansF(float degrees) -{ - return degrees * M_PI / 180.0; -} - - -double -DegreesToRadiansD(double degrees) -{return degrees * M_PI / 180.0; - return degrees * M_PI / 180.0; -} - - float Heading2f(Vector2f vec) { diff --git a/test/orientation_test.cc b/test/orientation_test.cc index 47a5904..ac57eae 100644 --- a/test/orientation_test.cc +++ b/test/orientation_test.cc @@ -1,4 +1,5 @@ #include +#include #include #include @@ -6,11 +7,29 @@ using namespace std; using namespace wr; +TEST(UnitConversions, RadiansToDegreesF) +{ + for (int i = 0; i < 360; i++) { + auto deg = static_cast(i); + EXPECT_FLOAT_EQ(math::RadiansToDegreesF(math::DegreesToRadiansF(deg)), deg); + } +} + + +TEST(UnitConversions, RadiansToDegreesD) +{ + for (int i = 0; i < 360; i++) { + auto deg = static_cast(i); + EXPECT_FLOAT_EQ(math::RadiansToDegreesD(math::DegreesToRadiansD(deg)), deg); + } +} + + TEST(Orientation2f, Heading) { geom::Vector2f a {2.0, 2.0}; - EXPECT_FLOAT_EQ(geom::Heading2f(a), geom::DegreesToRadiansF(45)); + EXPECT_FLOAT_EQ(geom::Heading2f(a), math::DegreesToRadiansF(45)); } @@ -18,7 +37,7 @@ TEST(Orientation3f, Heading) { geom::Vector3f a {2.0, 2.0, 2.0}; - EXPECT_FLOAT_EQ(geom::Heading3f(a), geom::DegreesToRadiansF(45)); + EXPECT_FLOAT_EQ(geom::Heading3f(a), math::DegreesToRadiansF(45)); } @@ -26,7 +45,7 @@ TEST(Orientation2d, Heading) { geom::Vector2d a {2.0, 2.0}; - EXPECT_NEAR(geom::Heading2d(a), geom::DegreesToRadiansF(45), 0.000001); + EXPECT_NEAR(geom::Heading2d(a), math::DegreesToRadiansF(45), 0.000001); } @@ -34,7 +53,7 @@ TEST(Orientation3d, Heading) { geom::Vector3d a {2.0, 2.0, 2.0}; - EXPECT_NEAR(geom::Heading3d(a), geom::DegreesToRadiansF(45), 0.000001); + EXPECT_NEAR(geom::Heading3d(a), math::DegreesToRadiansF(45), 0.000001); }