2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2019-07-31 22:15:22 -07:00
|
|
|
#include <wpi/math>
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
#include "frc/geometry/Rotation2d.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
static constexpr double kEpsilon = 1E-9;
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, RadiansToDegrees) {
|
2021-01-25 21:41:34 -08:00
|
|
|
const Rotation2d rot1{units::radian_t(wpi::math::pi / 3)};
|
|
|
|
|
const Rotation2d rot2{units::radian_t(wpi::math::pi / 4)};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2021-01-25 21:41:34 -08:00
|
|
|
EXPECT_NEAR(rot1.Degrees().to<double>(), 60.0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(rot2.Degrees().to<double>(), 45.0, kEpsilon);
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, DegreesToRadians) {
|
2021-01-25 21:41:34 -08:00
|
|
|
const auto rot1 = Rotation2d(45.0_deg);
|
|
|
|
|
const auto rot2 = Rotation2d(30.0_deg);
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2021-01-25 21:41:34 -08:00
|
|
|
EXPECT_NEAR(rot1.Radians().to<double>(), wpi::math::pi / 4.0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(rot2.Radians().to<double>(), wpi::math::pi / 6.0, kEpsilon);
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, RotateByFromZero) {
|
|
|
|
|
const Rotation2d zero;
|
2019-08-17 01:00:33 -04:00
|
|
|
auto sum = zero + Rotation2d(90.0_deg);
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2019-08-17 01:00:33 -04:00
|
|
|
EXPECT_NEAR(sum.Radians().to<double>(), wpi::math::pi / 2.0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(sum.Degrees().to<double>(), 90.0, kEpsilon);
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, RotateByNonZero) {
|
2019-08-17 01:00:33 -04:00
|
|
|
auto rot = Rotation2d(90.0_deg);
|
|
|
|
|
rot += Rotation2d(30.0_deg);
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2019-08-17 01:00:33 -04:00
|
|
|
EXPECT_NEAR(rot.Degrees().to<double>(), 120.0, kEpsilon);
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Minus) {
|
2021-01-25 21:41:34 -08:00
|
|
|
const auto rot1 = Rotation2d(70.0_deg);
|
|
|
|
|
const auto rot2 = Rotation2d(30.0_deg);
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2021-01-25 21:41:34 -08:00
|
|
|
EXPECT_NEAR((rot1 - rot2).Degrees().to<double>(), 40.0, kEpsilon);
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
2019-09-08 14:20:26 -04:00
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Equality) {
|
2021-01-25 21:41:34 -08:00
|
|
|
const auto rot1 = Rotation2d(43_deg);
|
|
|
|
|
const auto rot2 = Rotation2d(43_deg);
|
|
|
|
|
EXPECT_EQ(rot1, rot2);
|
|
|
|
|
|
|
|
|
|
const auto rot3 = Rotation2d(-180_deg);
|
|
|
|
|
const auto rot4 = Rotation2d(180_deg);
|
|
|
|
|
EXPECT_EQ(rot3, rot4);
|
2019-09-08 14:20:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Inequality) {
|
2021-01-25 21:41:34 -08:00
|
|
|
const auto rot1 = Rotation2d(43_deg);
|
|
|
|
|
const auto rot2 = Rotation2d(43.5_deg);
|
|
|
|
|
EXPECT_NE(rot1, rot2);
|
2019-09-08 14:20:26 -04:00
|
|
|
}
|