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>
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <numbers>
|
2019-07-31 22:15:22 -07:00
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
#include "frc/geometry/Rotation2d.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2022-11-14 15:25:15 -07:00
|
|
|
TEST(Rotation2dTest, InScope) {
|
|
|
|
|
const auto rot1 = Rotation2d{units::radian_t{std::numbers::pi * 5 / 2}};
|
|
|
|
|
const auto rot2 = Rotation2d{units::radian_t{std::numbers::pi * 7 / 2}};
|
|
|
|
|
const auto rot3 = Rotation2d{270_deg};
|
|
|
|
|
|
|
|
|
|
EXPECT_DOUBLE_EQ(std::numbers::pi / 2, rot1.Radians().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(-std::numbers::pi / 2, rot2.Radians().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(-90, rot3.Degrees().value());
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
TEST(Rotation2dTest, RadiansToDegrees) {
|
2022-10-15 16:33:14 -07:00
|
|
|
const Rotation2d rot1{units::radian_t{std::numbers::pi / 3.0}};
|
|
|
|
|
const Rotation2d rot2{units::radian_t{std::numbers::pi / 4.0}};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
EXPECT_DOUBLE_EQ(60.0, rot1.Degrees().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(45.0, rot2.Degrees().value());
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, DegreesToRadians) {
|
2022-05-06 08:41:23 -07:00
|
|
|
const auto rot1 = Rotation2d{45_deg};
|
|
|
|
|
const auto rot2 = Rotation2d{30_deg};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
EXPECT_DOUBLE_EQ(std::numbers::pi / 4.0, rot1.Radians().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(std::numbers::pi / 6.0, rot2.Radians().value());
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, RotateByFromZero) {
|
|
|
|
|
const Rotation2d zero;
|
2022-08-17 13:42:36 -07:00
|
|
|
auto rotated = zero + Rotation2d{90_deg};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
EXPECT_DOUBLE_EQ(std::numbers::pi / 2.0, rotated.Radians().value());
|
2022-05-06 08:41:23 -07:00
|
|
|
EXPECT_DOUBLE_EQ(90.0, rotated.Degrees().value());
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, RotateByNonZero) {
|
2022-05-06 08:41:23 -07:00
|
|
|
auto rot = Rotation2d{90_deg};
|
|
|
|
|
rot = rot + Rotation2d{30_deg};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
EXPECT_DOUBLE_EQ(120.0, rot.Degrees().value());
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Minus) {
|
2022-05-06 08:41:23 -07:00
|
|
|
const auto rot1 = Rotation2d{70_deg};
|
|
|
|
|
const auto rot2 = Rotation2d{30_deg};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
EXPECT_DOUBLE_EQ(40.0, (rot1 - rot2).Degrees().value());
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
2019-09-08 14:20:26 -04:00
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Equality) {
|
2022-05-06 08:41:23 -07:00
|
|
|
auto rot1 = Rotation2d{43_deg};
|
|
|
|
|
auto rot2 = Rotation2d{43_deg};
|
2021-01-25 21:41:34 -08:00
|
|
|
EXPECT_EQ(rot1, rot2);
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
rot1 = Rotation2d{-180_deg};
|
|
|
|
|
rot2 = Rotation2d{180_deg};
|
|
|
|
|
EXPECT_EQ(rot1, rot2);
|
2019-09-08 14:20:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Inequality) {
|
2022-05-06 08:41:23 -07:00
|
|
|
const auto rot1 = Rotation2d{43_deg};
|
|
|
|
|
const auto rot2 = Rotation2d{43.5_deg};
|
2021-01-25 21:41:34 -08:00
|
|
|
EXPECT_NE(rot1, rot2);
|
2019-09-08 14:20:26 -04:00
|
|
|
}
|
2022-10-31 11:17:00 -05:00
|
|
|
|
|
|
|
|
TEST(Rotation2dTest, Constexpr) {
|
|
|
|
|
constexpr Rotation2d defaultCtor;
|
2022-11-14 15:25:15 -07:00
|
|
|
constexpr Rotation2d radianCtor{0.25_rad};
|
|
|
|
|
constexpr Rotation2d degreeCtor{-90_deg};
|
2022-10-31 11:17:00 -05:00
|
|
|
constexpr Rotation2d rotation45{45_deg};
|
|
|
|
|
constexpr Rotation2d cartesianCtor{3.5, -3.5};
|
|
|
|
|
|
|
|
|
|
constexpr auto negated = -radianCtor;
|
|
|
|
|
constexpr auto multiplied = radianCtor * 2;
|
|
|
|
|
constexpr auto subtracted = cartesianCtor - degreeCtor;
|
|
|
|
|
|
|
|
|
|
static_assert(defaultCtor.Radians() == 0_rad);
|
2022-11-14 15:25:15 -07:00
|
|
|
static_assert(degreeCtor.Degrees() == -90_deg);
|
|
|
|
|
static_assert(negated.Radians() == -0.25_rad);
|
|
|
|
|
static_assert(multiplied.Radians() == 0.5_rad);
|
2022-10-31 11:17:00 -05:00
|
|
|
static_assert(subtracted == rotation45);
|
|
|
|
|
static_assert(radianCtor != degreeCtor);
|
|
|
|
|
}
|