2015-11-03 08:17:28 -04:00
|
|
|
#include <AP_gtest.h>
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
#define SQRT_2 1.4142135623730951f
|
|
|
|
|
|
|
|
TEST(VectorTest, Rotations)
|
|
|
|
{
|
|
|
|
unsigned rotation_count = 0;
|
|
|
|
|
2015-12-28 15:20:45 -04:00
|
|
|
#define TEST_ROTATION(rotation, _x, _y, _z) { \
|
|
|
|
const float accuracy = 1.0e-6; \
|
2015-11-03 08:17:28 -04:00
|
|
|
Vector3f v(1, 1, 1); \
|
|
|
|
v.rotate(rotation); \
|
2015-12-28 15:20:45 -04:00
|
|
|
Vector3f expected(_x, _y, _z); \
|
|
|
|
EXPECT_NEAR(expected.length(), v.length(), accuracy); \
|
|
|
|
EXPECT_FLOAT_EQ(expected.x, v.x); \
|
|
|
|
EXPECT_FLOAT_EQ(expected.y, v.y); \
|
|
|
|
EXPECT_FLOAT_EQ(expected.z, v.z); \
|
2015-11-03 08:17:28 -04:00
|
|
|
rotation_count++; \
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ROTATION(ROTATION_NONE, 1, 1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_45, 0, SQRT_2, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_90, -1, 1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_135, -SQRT_2, 0, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_180, -1, -1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_225, 0, -SQRT_2, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_270, 1, -1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_YAW_315, SQRT_2, 0, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180, 1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_YAW_45, SQRT_2, 0, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_YAW_90, 1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_YAW_135, 0, SQRT_2, -1);
|
|
|
|
TEST_ROTATION(ROTATION_PITCH_180, -1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_YAW_225, -SQRT_2, 0, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_YAW_270, -1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_YAW_315, 0, -SQRT_2, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90, 1, -1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_YAW_45, SQRT_2, 0, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_YAW_90, 1, 1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_YAW_135, 0, SQRT_2, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270, 1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270_YAW_45, 0, SQRT_2, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270_YAW_90, -1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270_YAW_135, -SQRT_2, 0, -1);
|
|
|
|
TEST_ROTATION(ROTATION_PITCH_90, 1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_PITCH_270, -1, 1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_PITCH_180_YAW_90, -1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_PITCH_180_YAW_270, 1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_PITCH_90, 1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_PITCH_90, -1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270_PITCH_90, -1, 1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_PITCH_180, -1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270_PITCH_180, -1, 1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_PITCH_270, -1, -1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_180_PITCH_270, 1, -1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_270_PITCH_270, 1, 1, 1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_PITCH_180_YAW_90, 1, -1, -1);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_YAW_270, -1, -1, 1);
|
2015-12-28 15:20:45 -04:00
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_PITCH_68_YAW_293, -0.4066309f, -1.5839677f, -0.5706992f);
|
2018-03-08 19:08:18 -04:00
|
|
|
TEST_ROTATION(ROTATION_PITCH_315, 0, 1, SQRT_2);
|
|
|
|
TEST_ROTATION(ROTATION_ROLL_90_PITCH_315, 0, -1, SQRT_2);
|
2015-11-03 08:17:28 -04:00
|
|
|
|
|
|
|
EXPECT_EQ(ROTATION_MAX, rotation_count) << "All rotations are expect to be tested";
|
|
|
|
}
|
|
|
|
|
2016-04-16 07:00:08 -03:00
|
|
|
TEST(MathTest, IsZero)
|
|
|
|
{
|
2016-05-02 15:40:53 -03:00
|
|
|
EXPECT_FALSE(is_zero(0.1));
|
|
|
|
EXPECT_FALSE(is_zero(0.0001));
|
|
|
|
EXPECT_TRUE(is_zero(0.f));
|
|
|
|
EXPECT_TRUE(is_zero(FLT_MIN));
|
|
|
|
EXPECT_TRUE(is_zero(-FLT_MIN));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MathTest, IsEqual)
|
|
|
|
{
|
2016-12-17 14:17:11 -04:00
|
|
|
EXPECT_FALSE(is_equal(1, 0));
|
|
|
|
EXPECT_TRUE(is_equal(1, 1));
|
2016-05-02 15:40:53 -03:00
|
|
|
EXPECT_FALSE(is_equal(0.1, 0.10001));
|
|
|
|
EXPECT_FALSE(is_equal(0.1, -0.1001));
|
|
|
|
EXPECT_TRUE(is_equal(0.f, 0.0f));
|
|
|
|
EXPECT_FALSE(is_equal(1.f, 1.f + FLT_EPSILON));
|
2017-01-25 04:50:52 -04:00
|
|
|
EXPECT_TRUE(is_equal(1.f, 1.f + FLT_EPSILON / 2.f));
|
|
|
|
EXPECT_TRUE(is_equal(1.f, (float)(1.f - DBL_EPSILON)));
|
|
|
|
|
|
|
|
// false because the common type is double
|
2017-02-02 02:42:36 -04:00
|
|
|
EXPECT_FALSE(is_equal(double(1.), 1 + 2 * std::numeric_limits<double>::epsilon()));
|
2017-01-25 04:50:52 -04:00
|
|
|
|
|
|
|
// true because the common type is float
|
|
|
|
EXPECT_TRUE(is_equal(1.f, (float)(1. + std::numeric_limits<double>::epsilon())));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MathTest, Square)
|
|
|
|
{
|
|
|
|
float sq_0 = sq(0);
|
|
|
|
float sq_1 = sq(1);
|
|
|
|
float sq_2 = sq(2);
|
|
|
|
|
|
|
|
EXPECT_EQ(0.f, sq_0);
|
|
|
|
EXPECT_EQ(1.f, sq_1);
|
|
|
|
EXPECT_EQ(4.f, sq_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MathTest, Norm)
|
|
|
|
{
|
2017-06-20 21:38:26 -03:00
|
|
|
float norm_1 = norm(1, 4.2);
|
2016-04-16 06:58:46 -03:00
|
|
|
float norm_2 = norm(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
|
2017-06-20 21:38:26 -03:00
|
|
|
float norm_3 = norm(0, 5.3);
|
2016-04-16 06:58:46 -03:00
|
|
|
float norm_4 = norm(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
|
|
|
|
float norm_5 = norm(3,4);
|
|
|
|
float norm_6 = norm(4,3,12);
|
|
|
|
|
2017-06-20 21:38:26 -03:00
|
|
|
EXPECT_FLOAT_EQ(norm_1, 4.3174066f);
|
2016-04-16 06:58:46 -03:00
|
|
|
EXPECT_EQ(norm_2, 4.f);
|
2017-06-20 21:38:26 -03:00
|
|
|
EXPECT_EQ(norm_3, 5.3f);
|
2016-04-16 06:58:46 -03:00
|
|
|
EXPECT_EQ(norm_4, 0.f);
|
|
|
|
EXPECT_EQ(norm_5, 5.f);
|
|
|
|
EXPECT_EQ(norm_6, 13.f);
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MathTest, Constrain)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
if (i < 250) {
|
|
|
|
EXPECT_EQ(250, constrain_float(i, 250, 500));
|
|
|
|
EXPECT_EQ(250, constrain_int16(i, 250, 500));
|
|
|
|
EXPECT_EQ(250, constrain_int32(i, 250, 500));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(250, constrain_int64(i, 250, 500));
|
2016-04-16 07:00:08 -03:00
|
|
|
} else if (i > 500) {
|
|
|
|
EXPECT_EQ(500, constrain_float(i, 250, 500));
|
|
|
|
EXPECT_EQ(500, constrain_int16(i, 250, 500));
|
|
|
|
EXPECT_EQ(500, constrain_int32(i, 250, 500));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(500, constrain_int64(i, 250, 500));
|
2016-04-16 07:00:08 -03:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(i, constrain_float(i, 250, 500));
|
|
|
|
EXPECT_EQ(i, constrain_int16(i, 250, 500));
|
|
|
|
EXPECT_EQ(i, constrain_int32(i, 250, 500));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(i, constrain_int64(i, 250, 500));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= 1000; i++) {
|
|
|
|
int c = i - 1000;
|
|
|
|
if (c < -250) {
|
|
|
|
EXPECT_EQ(-250, constrain_float(c, -250, -50));
|
|
|
|
EXPECT_EQ(-250, constrain_int16(c, -250, -50));
|
|
|
|
EXPECT_EQ(-250, constrain_int32(c, -250, -50));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(-250, constrain_int64(c, -250, -50));
|
2016-04-16 07:00:08 -03:00
|
|
|
} else if(c > -50) {
|
|
|
|
EXPECT_EQ(-50, constrain_float(c, -250, -50));
|
|
|
|
EXPECT_EQ(-50, constrain_int16(c, -250, -50));
|
|
|
|
EXPECT_EQ(-50, constrain_int32(c, -250, -50));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(-50, constrain_int64(c, -250, -50));
|
2016-04-16 07:00:08 -03:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(c, constrain_float(c, -250, -50));
|
|
|
|
EXPECT_EQ(c, constrain_int16(c, -250, -50));
|
|
|
|
EXPECT_EQ(c, constrain_int32(c, -250, -50));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(c, constrain_int64(c, -250, -50));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= 2000; i++) {
|
|
|
|
int c = i - 1000;
|
|
|
|
if (c < -250) {
|
|
|
|
EXPECT_EQ(-250, constrain_float(c, -250, 50));
|
|
|
|
EXPECT_EQ(-250, constrain_int16(c, -250, 50));
|
|
|
|
EXPECT_EQ(-250, constrain_int32(c, -250, 50));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(-250, constrain_int64(c, -250, 50));
|
2016-04-16 07:00:08 -03:00
|
|
|
} else if(c > 50) {
|
|
|
|
EXPECT_EQ(50, constrain_float(c, -250, 50));
|
|
|
|
EXPECT_EQ(50, constrain_int16(c, -250, 50));
|
|
|
|
EXPECT_EQ(50, constrain_int32(c, -250, 50));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(50, constrain_int64(c, -250, 50));
|
2016-04-16 07:00:08 -03:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(c, constrain_float(c, -250, 50));
|
|
|
|
EXPECT_EQ(c, constrain_int16(c, -250, 50));
|
|
|
|
EXPECT_EQ(c, constrain_int32(c, -250, 50));
|
2018-04-17 00:54:06 -03:00
|
|
|
EXPECT_EQ(c, constrain_int64(c, -250, 50));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 16:17:05 -04:00
|
|
|
|
|
|
|
EXPECT_EQ(20.0, constrain_value(20.0, 19.9, 20.1));
|
|
|
|
EXPECT_EQ(20.0, constrain_value(20.0f, 19.9f, 20.1f));
|
|
|
|
|
|
|
|
EXPECT_EQ(19.9, constrain_value(19.9, 19.9, 20.1));
|
|
|
|
EXPECT_EQ(19.9f, constrain_value(19.9f, 19.9f, 20.1f));
|
|
|
|
|
|
|
|
EXPECT_EQ(19.9, constrain_value(19.8, 19.9, 20.1));
|
|
|
|
EXPECT_EQ(19.9f, constrain_value(19.8f, 19.9f, 20.1f));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MathWrapTest, Angle180)
|
|
|
|
{
|
|
|
|
// Full circle test
|
|
|
|
for (int32_t i = 0; i < 36000; i += 100) {
|
|
|
|
if (i < 18000) {
|
|
|
|
// smaller pole position
|
|
|
|
EXPECT_EQ(i, wrap_180_cd(i));
|
|
|
|
EXPECT_EQ(-i, wrap_180_cd(-i));
|
|
|
|
} else if (i == 18000) {
|
|
|
|
// hit pole position -180/+180 degree
|
|
|
|
EXPECT_EQ(i, wrap_180_cd(i));
|
|
|
|
EXPECT_EQ(i, wrap_180_cd(-i));
|
|
|
|
} else {
|
|
|
|
// bigger pole position
|
|
|
|
EXPECT_EQ(-(36000 - i), wrap_180_cd(i));
|
|
|
|
EXPECT_EQ(36000 - i, wrap_180_cd(-i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:35:18 -03:00
|
|
|
EXPECT_EQ(4500.f, wrap_180_cd(4500.f));
|
|
|
|
EXPECT_EQ(9000.f, wrap_180_cd(9000.f));
|
|
|
|
EXPECT_EQ(18000.f, wrap_180_cd(18000.f));
|
|
|
|
EXPECT_EQ(-17990.f, wrap_180_cd(18010.f));
|
|
|
|
EXPECT_EQ(-9000.f, wrap_180_cd(27000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(36000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(72000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(360000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(720000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(-3600000000.f));
|
|
|
|
|
|
|
|
EXPECT_EQ(-4500.f, wrap_180_cd(-4500.f));
|
|
|
|
EXPECT_EQ(-9000.f, wrap_180_cd(-9000.f));
|
|
|
|
EXPECT_EQ(18000.f, wrap_180_cd(-18000.f));
|
|
|
|
EXPECT_EQ(17990.f, wrap_180_cd(-18010.f));
|
|
|
|
EXPECT_EQ(9000.f, wrap_180_cd(-27000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(-36000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_180_cd(-72000.f));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MathWrapTest, Angle360)
|
|
|
|
{
|
|
|
|
// Full circle test
|
|
|
|
for (int32_t i = 0; i <= 36000; i += 100) {
|
|
|
|
if (i == 0) {
|
|
|
|
// hit pole position
|
|
|
|
EXPECT_EQ(i, wrap_360_cd(i));
|
|
|
|
EXPECT_EQ(i, wrap_360_cd(-i));
|
|
|
|
} else if (i < 36000) {
|
|
|
|
// between pole position
|
|
|
|
EXPECT_EQ(i, wrap_360_cd(i));
|
|
|
|
EXPECT_EQ(36000 - i, wrap_360_cd(-i));
|
|
|
|
} else if (i == 36000) {
|
|
|
|
// hit pole position
|
|
|
|
EXPECT_EQ(0, wrap_360_cd(i));
|
|
|
|
EXPECT_EQ(0, wrap_360_cd(-i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:35:18 -03:00
|
|
|
EXPECT_EQ(4500.f, wrap_360_cd(4500.f));
|
|
|
|
EXPECT_EQ(9000.f, wrap_360_cd(9000.f));
|
|
|
|
EXPECT_EQ(18000.f, wrap_360_cd(18000.f));
|
|
|
|
EXPECT_EQ(27000.f, wrap_360_cd(27000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_360_cd(36000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_360_cd(72000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_360_cd(360000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_360_cd(720000.f));
|
|
|
|
EXPECT_EQ( 0.f, wrap_360_cd(-3600000000.f));
|
|
|
|
|
|
|
|
EXPECT_EQ(31500.f, wrap_360_cd(-4500.f));
|
|
|
|
EXPECT_EQ(27000.f, wrap_360_cd(-9000.f));
|
|
|
|
EXPECT_EQ(18000.f, wrap_360_cd(-18000.f));
|
|
|
|
EXPECT_EQ(9000.f, wrap_360_cd(-27000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_360_cd(-36000.f));
|
|
|
|
EXPECT_EQ(0.f, wrap_360_cd(-72000.f));
|
2016-04-16 07:00:08 -03:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:23:12 -03:00
|
|
|
TEST(MathWrapTest, AnglePI)
|
|
|
|
{
|
|
|
|
const float accuracy = 1.0e-5;
|
|
|
|
|
|
|
|
EXPECT_NEAR(M_PI, wrap_PI(M_PI), accuracy);
|
|
|
|
EXPECT_NEAR(0.f, wrap_PI(M_2PI), accuracy);
|
|
|
|
EXPECT_NEAR(0, wrap_PI(M_PI * 10), accuracy);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MathWrapTest, Angle2PI)
|
|
|
|
{
|
|
|
|
const float accuracy = 1.0e-5;
|
|
|
|
|
|
|
|
EXPECT_NEAR(M_PI, wrap_2PI(M_PI), accuracy);
|
|
|
|
EXPECT_NEAR(0.f, wrap_2PI(M_2PI), accuracy);
|
|
|
|
EXPECT_NEAR(0.f, wrap_2PI(M_PI * 10), accuracy);
|
|
|
|
EXPECT_NEAR(0.f, wrap_2PI(0.f), accuracy);
|
|
|
|
EXPECT_NEAR(M_PI, wrap_2PI(-M_PI), accuracy);
|
|
|
|
EXPECT_NEAR(0, wrap_2PI(-M_2PI), accuracy);
|
|
|
|
}
|
|
|
|
|
2015-11-03 08:17:28 -04:00
|
|
|
AP_GTEST_MAIN()
|