Skip to content

Commit

Permalink
Merge branch 'dev' into feat-py-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Jul 23, 2023
2 parents bc2a072 + abb640e commit 38237fb
Show file tree
Hide file tree
Showing 19 changed files with 1,556 additions and 1,717 deletions.
321 changes: 226 additions & 95 deletions tests/cpp/common_math_generators.hpp

Large diffs are not rendered by default.

96 changes: 95 additions & 1 deletion tests/cpp/common_math_helpers.hpp
Expand Up @@ -2,9 +2,12 @@
#include <math/vec2_t.hpp>
#include <math/vec3_t.hpp>
#include <math/vec4_t.hpp>
#include <math/mat2_t.hpp>
#include <math/mat3_t.hpp>
#include <math/quat_t.hpp>
#include <math/mat4_t.hpp>
#include <math/quat_t.hpp>
#include <math/euler_t.hpp>
#include "math/euler_t_decl.hpp"

// NOLINTNEXTLINE
#define gen_random_value(T, range_min, range_max, Nsamples) \
Expand All @@ -25,4 +28,95 @@ constexpr auto func_all_close(const ::math::Vector2<T>& vec, T x, T y, T eps)
func_value_close(vec.y(), y, eps);
}

template <typename T>
constexpr auto func_all_close(const ::math::Vector3<T>& vec, T x, T y, T z,
T eps) -> bool {
return func_value_close(vec.x(), x, eps) &&
func_value_close(vec.y(), y, eps) &&
func_value_close(vec.z(), z, eps);
}

template <typename T>
constexpr auto func_all_close(const ::math::Vector4<T>& vec, T x, T y, T z, T w,
T eps) -> bool {
return func_value_close(vec.x(), x, eps) &&
func_value_close(vec.y(), y, eps) &&
func_value_close(vec.z(), z, eps) &&
func_value_close(vec.w(), w, eps);
}

// clang-format off
template <typename T>
constexpr auto func_all_close(const ::math::Matrix2<T>& mat,
T x00, T x01,
T x10, T x11, T eps) -> bool {
return func_value_close<T>(mat(0, 0), x00, eps) &&
func_value_close<T>(mat(0, 1), x01, eps) &&
func_value_close<T>(mat(1, 0), x10, eps) &&
func_value_close<T>(mat(1, 1), x11, eps);
}

template <typename T>
constexpr auto func_all_close(const ::math::Matrix3<T>& mat,
T x00, T x01, T x02,
T x10, T x11, T x12,
T x20, T x21, T x22, T eps) -> bool {
return func_value_close<T>(mat(0, 0), x00, eps) &&
func_value_close<T>(mat(0, 1), x01, eps) &&
func_value_close<T>(mat(0, 2), x02, eps) &&

func_value_close<T>(mat(1, 0), x10, eps) &&
func_value_close<T>(mat(1, 1), x11, eps) &&
func_value_close<T>(mat(1, 2), x12, eps) &&

func_value_close<T>(mat(2, 0), x20, eps) &&
func_value_close<T>(mat(2, 1), x21, eps) &&
func_value_close<T>(mat(2, 2), x22, eps);
}

template <typename T>
constexpr auto func_all_close(const ::math::Matrix4<T>& mat,
T x00, T x01, T x02, T x03,
T x10, T x11, T x12, T x13,
T x20, T x21, T x22, T x23,
T x30, T x31, T x32, T x33, T eps) -> bool {
return func_value_close<T>(mat(0, 0), x00, eps) &&
func_value_close<T>(mat(0, 1), x01, eps) &&
func_value_close<T>(mat(0, 2), x02, eps) &&
func_value_close<T>(mat(0, 3), x03, eps) &&

func_value_close<T>(mat(1, 0), x10, eps) &&
func_value_close<T>(mat(1, 1), x11, eps) &&
func_value_close<T>(mat(1, 2), x12, eps) &&
func_value_close<T>(mat(1, 3), x13, eps) &&

func_value_close<T>(mat(2, 0), x20, eps) &&
func_value_close<T>(mat(2, 1), x21, eps) &&
func_value_close<T>(mat(2, 2), x22, eps) &&
func_value_close<T>(mat(2, 3), x23, eps) &&

func_value_close<T>(mat(3, 0), x30, eps) &&
func_value_close<T>(mat(3, 1), x31, eps) &&
func_value_close<T>(mat(3, 2), x32, eps) &&
func_value_close<T>(mat(3, 3), x33, eps);
}
// clang-format on

template <typename T>
constexpr auto func_all_close(const ::math::Quaternion<T>& quat, T w, T x, T y,
T z, T eps) -> bool {
return func_value_close<T>(quat.w(), w, eps) &&
func_value_close<T>(quat.x(), x, eps) &&
func_value_close<T>(quat.y(), y, eps) &&
func_value_close<T>(quat.z(), z, eps);
}

template <typename T>
constexpr auto func_all_close(const ::math::Euler<T>& euler, T ex, T ey, T ez,
T eps) -> bool {
return func_value_close<T>(euler.x, ex, eps) &&
func_value_close<T>(euler.y, ey, eps) &&
func_value_close<T>(euler.z, ez, eps);
}

} // namespace math
71 changes: 29 additions & 42 deletions tests/cpp/test_euler_type.cpp
@@ -1,6 +1,8 @@
#include <catch2/catch.hpp>
#include <math/euler_t.hpp>

#include "./common_math_helpers.hpp"

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-float-conversion"
Expand All @@ -14,41 +16,26 @@
#pragma warning(disable : 4305)
#endif

constexpr double RANGE_MIN = -10.0;
constexpr double RANGE_MAX = 10.0;

// NOLINTNEXTLINE
#define GenRandomValue(Type, Nsamples) \
GENERATE(take(Nsamples, random(static_cast<Type>(RANGE_MIN), \
static_cast<Type>(RANGE_MAX))))

template <typename T>
constexpr auto FuncClose(T a, T b, T eps) -> bool {
return ((a - b) < eps) && ((a - b) > -eps);
}

template <typename T>
auto FuncAllClose(const math::Euler<T>& euler, T x, T y, T z) -> bool {
constexpr T EPSILON = static_cast<T>(math::EPS);
// clang-format off
return FuncClose<T>(euler.x, x, EPSILON) &&
FuncClose<T>(euler.y, y, EPSILON) &&
FuncClose<T>(euler.z, z, EPSILON);
// clang-format on
}
constexpr double USER_RANGE_MIN = -10.0;
constexpr double USER_RANGE_MAX = 10.0;
constexpr double USER_EPSILON = 1e-5;

// NOLINTNEXTLINE
TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
math::float32_t, math::float64_t) {
::math::float32_t, ::math::float64_t) {
using T = TestType;
using Euler = math::Euler<T>;
using Mat3 = math::Matrix3<T>;
using Mat4 = math::Matrix4<T>;
using Quat = math::Quaternion<T>;
using Euler = ::math::Euler<T>;
using Mat3 = ::math::Matrix3<T>;
using Mat4 = ::math::Matrix4<T>;
using Quat = ::math::Quaternion<T>;

constexpr T EPSILON = static_cast<T>(USER_EPSILON);
constexpr T RANGE_MIN = static_cast<T>(USER_RANGE_MIN);
constexpr T RANGE_MAX = static_cast<T>(USER_RANGE_MAX);

SECTION("Default constructor") {
Euler e;
REQUIRE(FuncAllClose<T>(e, 0.0, 0.0, 0.0));
REQUIRE(::math::func_all_close<T>(e, 0.0, 0.0, 0.0, EPSILON));
// Default setting for order is Order::XYZ
REQUIRE(e.order == ::math::euler::Order::XYZ);
// Default setting for convention is Convention::INTRINSIC
Expand All @@ -57,12 +44,12 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",

SECTION("From single scalar argument") {
constexpr int NUM_SAMPLES = 8;
auto val_x = GenRandomValue(T, NUM_SAMPLES);
auto val_y = GenRandomValue(T, NUM_SAMPLES);
auto val_z = GenRandomValue(T, NUM_SAMPLES);
auto val_x = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES);
auto val_y = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES);
auto val_z = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES);

Euler e(val_x, val_y, val_z);
REQUIRE(FuncAllClose<T>(e, val_x, val_y, val_z));
REQUIRE(::math::func_all_close<T>(e, val_x, val_y, val_z, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -73,7 +60,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
{
Quat q(1.0, 0.0, 0.0, 0.0);
Euler e(q);
REQUIRE(FuncAllClose<T>(e, 0.0, 0.0, 0.0));
REQUIRE(::math::func_all_close<T>(e, 0.0, 0.0, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -85,7 +72,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto sin_half = std::sin(angle / 2.0);
Quat q(cos_half, sin_half, 0.0, 0.0);
Euler e(q);
REQUIRE(FuncAllClose<T>(e, angle, 0.0, 0.0));
REQUIRE(::math::func_all_close<T>(e, angle, 0.0, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -97,7 +84,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto sin_half = std::sin(angle / 2.0);
Quat q(cos_half, 0.0, sin_half, 0.0);
Euler e(q);
REQUIRE(FuncAllClose<T>(e, 0.0, angle, 0.0));
REQUIRE(::math::func_all_close<T>(e, 0.0, angle, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -109,7 +96,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto sin_half = std::sin(angle / 2.0);
Quat q(cos_half, 0.0, 0.0, sin_half);
Euler e(q);
REQUIRE(FuncAllClose<T>(e, 0.0, 0.0, angle));
REQUIRE(::math::func_all_close<T>(e, 0.0, 0.0, angle, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -122,7 +109,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto angle = ::math::PI / 4.0;
Mat3 rotmat = Mat3::RotationX(angle);
Euler e(rotmat);
REQUIRE(FuncAllClose<T>(e, angle, 0.0, 0.0));
REQUIRE(::math::func_all_close<T>(e, angle, 0.0, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -133,7 +120,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto angle = ::math::PI / 4.0;
Mat3 rotmat = Mat3::RotationY(angle);
Euler e(rotmat);
REQUIRE(FuncAllClose<T>(e, 0.0, angle, 0.0));
REQUIRE(::math::func_all_close<T>(e, 0.0, angle, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -144,7 +131,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto angle = ::math::PI / 4.0;
Mat3 rotmat = Mat3::RotationZ(angle);
Euler e(rotmat);
REQUIRE(FuncAllClose<T>(e, 0.0, 0.0, angle));
REQUIRE(::math::func_all_close<T>(e, 0.0, 0.0, angle, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -157,7 +144,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto angle = ::math::PI / 4.0;
Mat4 rotmat = Mat4::RotationX(angle);
Euler e(rotmat);
REQUIRE(FuncAllClose<T>(e, angle, 0.0, 0.0));
REQUIRE(::math::func_all_close<T>(e, angle, 0.0, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -168,7 +155,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto angle = ::math::PI / 4.0;
Mat4 rotmat = Mat4::RotationY(angle);
Euler e(rotmat);
REQUIRE(FuncAllClose<T>(e, 0.0, angle, 0.0));
REQUIRE(::math::func_all_close<T>(e, 0.0, angle, 0.0, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand All @@ -179,7 +166,7 @@ TEMPLATE_TEST_CASE("Euler class (euler_t) constructors", "[euler_t][template]",
auto angle = ::math::PI / 4.0;
Mat4 rotmat = Mat4::RotationZ(angle);
Euler e(rotmat);
REQUIRE(FuncAllClose<T>(e, 0.0, 0.0, angle));
REQUIRE(::math::func_all_close<T>(e, 0.0, 0.0, angle, EPSILON));
REQUIRE(e.order == ::math::euler::Order::XYZ);
REQUIRE(e.convention == ::math::euler::Convention::INTRINSIC);
}
Expand Down
70 changes: 28 additions & 42 deletions tests/cpp/test_mat2_functions.cpp
@@ -1,6 +1,9 @@
#include <catch2/catch.hpp>
#include <math/mat2_t.hpp>

#include "./common_math_helpers.hpp"
#include "./common_math_generators.hpp"

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-float-conversion"
Expand All @@ -14,75 +17,58 @@
#pragma warning(disable : 4305)
#endif

static constexpr double ANGLE_MIN = -math::PI;
static constexpr double ANGLE_MAX = math::PI;

// NOLINTNEXTLINE
#define GenRandomAngle(Type, Nsamples) \
GENERATE(take(Nsamples, random(static_cast<Type>(ANGLE_MIN), \
static_cast<Type>(ANGLE_MAX))))

static constexpr double SCALE_MIN = -10.0;
static constexpr double SCALE_MAX = 10.0;

// NOLINTNEXTLINE
#define GenRandomScale(Type, Nsamples) \
GENERATE(take(Nsamples, random(static_cast<Type>(SCALE_MIN), \
static_cast<Type>(SCALE_MAX))))
constexpr double USER_ANGLE_MIN = -::math::PI;
constexpr double USER_ANGLE_MAX = ::math::PI;
constexpr double USER_SCALE_MIN = -10.0;
constexpr double USER_SCALE_MAX = 10.0;
constexpr double USER_EPSILON = 1e-5;

template <typename T>
constexpr auto FuncClose(T a, T b, T eps) -> bool {
return ((a - b) < eps) && ((a - b) > -eps);
}

template <typename T>
auto FuncAllClose(const math::Matrix2<T>& mat, T x00, T x01, T x10, T x11)
-> bool {
constexpr T EPSILON = static_cast<T>(math::EPS);
return FuncClose(mat(0, 0), x00, EPSILON) &&
FuncClose(mat(0, 1), x01, EPSILON) &&
FuncClose(mat(1, 0), x10, EPSILON) &&
FuncClose(mat(1, 1), x11, EPSILON);
}
constexpr auto NUM_SAMPLES = 100;

// NOLINTNEXTLINE
TEMPLATE_TEST_CASE("Matrix2 class (mat2_t) factory functions",
"[mat2_t][funcs]", math::float32_t, math::float64_t) {
"[mat2_t][funcs]", ::math::float32_t, ::math::float64_t) {
using T = TestType;
using Matrix2 = math::Matrix2<T>;
using Vector2 = math::Vector2<T>;
using Matrix2 = ::math::Matrix2<T>;

constexpr T EPSILON = static_cast<T>(USER_EPSILON);
constexpr T ANGLE_MIN = static_cast<T>(USER_ANGLE_MIN);
constexpr T ANGLE_MAX = static_cast<T>(USER_ANGLE_MAX);
constexpr T SCALE_MIN = static_cast<T>(USER_SCALE_MIN);
constexpr T SCALE_MAX = static_cast<T>(USER_SCALE_MAX);

SECTION("Rotation matrix") {
auto angle = GenRandomAngle(T, 100);
auto angle = gen_random_value(T, ANGLE_MIN, ANGLE_MAX, NUM_SAMPLES);
auto rot_mat = Matrix2::Rotation(angle);

// clang-format off
REQUIRE(FuncAllClose<T>(rot_mat,
REQUIRE(::math::func_all_close<T>(rot_mat,
std::cos(angle), -std::sin(angle),
std::sin(angle), std::cos(angle)));
std::sin(angle), std::cos(angle), EPSILON));
// clang-format on
}

SECTION("Scale matrix - from scalars") {
auto scale_x = GenRandomScale(T, 100);
auto scale_y = GenRandomScale(T, 100);
auto scale_x = gen_random_value(T, SCALE_MIN, SCALE_MAX, NUM_SAMPLES);
auto scale_y = gen_random_value(T, SCALE_MIN, SCALE_MAX, NUM_SAMPLES);
auto scale_mat = Matrix2::Scale(scale_x, scale_y);

// clang-format off
REQUIRE(FuncAllClose<T>(scale_mat,
REQUIRE(::math::func_all_close<T>(scale_mat,
scale_x, 0.0,
0.0, scale_y));
0.0, scale_y, EPSILON));
// clang-format on
}

SECTION("Scale matrix - from vec2") {
auto scale = Vector2(GenRandomScale(T, 100), GenRandomScale(T, 100));
auto scale = GENERATE(
take(NUM_SAMPLES, ::math::random_vec2<T>(SCALE_MIN, SCALE_MAX)));
auto scale_mat = Matrix2::Scale(scale);

// clang-format off
REQUIRE(FuncAllClose<T>(scale_mat,
REQUIRE(::math::func_all_close<T>(scale_mat,
scale.x(), 0.0,
0.0, scale.y()));
0.0, scale.y(), EPSILON));
// clang-format on
}
}
Expand Down

0 comments on commit 38237fb

Please sign in to comment.