Reorg util/math -> math.

This commit is contained in:
Kyle Isom 2019-08-03 13:44:43 -07:00
parent 0dc47cdbce
commit de4dd70407
7 changed files with 133 additions and 89 deletions

View File

@ -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 #ifndef __WRMATH_GEOM_ORIENTATION_H
#define __WRMATH_GEOM_ORIENTATION_H #define __WRMATH_GEOM_ORIENTATION_H
@ -13,12 +16,6 @@ namespace wr {
namespace geom { 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_i = 0;
constexpr uint8_t Basis_j = 1; constexpr uint8_t Basis_j = 1;
constexpr uint8_t Basis_k = 2; constexpr uint8_t Basis_k = 2;

View File

@ -9,7 +9,7 @@
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>
#include <wrmath/util/math.h> #include <wrmath/math.h>
namespace wr { namespace wr {
@ -29,18 +29,18 @@ public:
* The default constructor creates a zero vector for a given * The default constructor creates a zero vector for a given
* type and size. * 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 * If given an initializer_list, the vector is created with
* those values. There must be exactly N elements in the list. * those values. There must be exactly N elements in the list.
* @param ilst * @param ilstutil
*/ */
Vector(std::initializer_list<T> ilst) Vector(std::initializer_list<T> ilst)
{ {
assert(ilst.size() == N); assert(ilst.size() == N);
wr::util::DefaultEpsilon(this->epsilon); wr::math::DefaultEpsilon(this->epsilon);
std::copy(ilst.begin(), ilst.end(), this->arr.begin()); std::copy(ilst.begin(), ilst.end(), this->arr.begin());
} }
@ -81,7 +81,7 @@ public:
isZero() const isZero() const
{ {
for (size_t i = 0; i < N; i++) { 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; return false;
} }
} }
@ -107,7 +107,7 @@ public:
bool bool
isUnitVector() const 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); 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; return true;
} }
@ -163,7 +163,7 @@ public:
return true; 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<T, N> &other) const { bool operator==(const Vector<T, N> &other) const {
for (size_t i = 0; i<N; i++) { for (size_t i = 0; i<N; i++) {
if (!wr::util::WithinTolerance(this->arr[i], other.arr[i], this->epsilon)) { if (!wr::math::WithinTolerance(this->arr[i], other.arr[i], this->epsilon)) {
return false; return false;
} }
} }

43
include/wrmath/math.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef __WRMATH_UTIL_MATH_H
#define __WRMATH_UTIL_MATH_H
#include <cmath>
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 <typename T>
static T
WithinTolerance(T a, T b, T epsilon)
{
return std::abs(a - b) < epsilon;
}
} // namespace math
} // namespace wr
#endif // __WRMATH_UTIL_MATH_H

View File

@ -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 <typename T>
static T
WithinTolerance(T a, T b, T epsilon)
{
return std::abs(a - b) < epsilon;
}
} // namespace util
} // namespace wr
#endif // __WRMATH_UTIL_MATH_H

52
src/math.cc Normal file
View File

@ -0,0 +1,52 @@
#include <wrmath/math.h>
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

View File

@ -6,34 +6,6 @@ namespace wr {
namespace geom { 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 float
Heading2f(Vector2f vec) Heading2f(Vector2f vec)
{ {

View File

@ -1,4 +1,5 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <wrmath/math.h>
#include <wrmath/geom/vector.h> #include <wrmath/geom/vector.h>
#include <wrmath/geom/orientation.h> #include <wrmath/geom/orientation.h>
@ -6,11 +7,29 @@ using namespace std;
using namespace wr; using namespace wr;
TEST(UnitConversions, RadiansToDegreesF)
{
for (int i = 0; i < 360; i++) {
auto deg = static_cast<float>(i);
EXPECT_FLOAT_EQ(math::RadiansToDegreesF(math::DegreesToRadiansF(deg)), deg);
}
}
TEST(UnitConversions, RadiansToDegreesD)
{
for (int i = 0; i < 360; i++) {
auto deg = static_cast<double>(i);
EXPECT_FLOAT_EQ(math::RadiansToDegreesD(math::DegreesToRadiansD(deg)), deg);
}
}
TEST(Orientation2f, Heading) TEST(Orientation2f, Heading)
{ {
geom::Vector2f a {2.0, 2.0}; 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}; 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}; 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}; 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);
} }