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>
|
|
|
|
|
|
|
|
|
|
#include "frc/geometry/Pose2d.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
static constexpr double kEpsilon = 1E-9;
|
|
|
|
|
|
|
|
|
|
TEST(Pose2dTest, TransformBy) {
|
2019-08-17 01:00:33 -04:00
|
|
|
const Pose2d initial{1_m, 2_m, Rotation2d(45.0_deg)};
|
|
|
|
|
const Transform2d transform{Translation2d{5.0_m, 0.0_m}, Rotation2d(5.0_deg)};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
const auto transformed = initial + transform;
|
|
|
|
|
|
2020-07-02 18:09:36 -07:00
|
|
|
EXPECT_NEAR(transformed.X().to<double>(), 1 + 5 / std::sqrt(2.0), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(transformed.Y().to<double>(), 2 + 5 / std::sqrt(2.0), kEpsilon);
|
2019-08-17 01:00:33 -04:00
|
|
|
EXPECT_NEAR(transformed.Rotation().Degrees().to<double>(), 50.0, kEpsilon);
|
2019-07-24 02:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Pose2dTest, RelativeTo) {
|
2019-08-17 01:00:33 -04:00
|
|
|
const Pose2d initial{0_m, 0_m, Rotation2d(45.0_deg)};
|
|
|
|
|
const Pose2d final{5_m, 5_m, Rotation2d(45.0_deg)};
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
const auto finalRelativeToInitial = final.RelativeTo(initial);
|
|
|
|
|
|
2020-07-02 18:09:36 -07:00
|
|
|
EXPECT_NEAR(finalRelativeToInitial.X().to<double>(), 5.0 * std::sqrt(2.0),
|
2019-08-17 01:00:33 -04:00
|
|
|
kEpsilon);
|
2020-07-02 18:09:36 -07:00
|
|
|
EXPECT_NEAR(finalRelativeToInitial.Y().to<double>(), 0.0, kEpsilon);
|
2019-08-17 01:00:33 -04:00
|
|
|
EXPECT_NEAR(finalRelativeToInitial.Rotation().Degrees().to<double>(), 0.0,
|
2019-07-24 02:57:39 -04:00
|
|
|
kEpsilon);
|
|
|
|
|
}
|
2019-09-08 14:20:26 -04:00
|
|
|
|
|
|
|
|
TEST(Pose2dTest, Equality) {
|
|
|
|
|
const Pose2d a{0_m, 5_m, Rotation2d(43_deg)};
|
|
|
|
|
const Pose2d b{0_m, 5_m, Rotation2d(43_deg)};
|
|
|
|
|
EXPECT_TRUE(a == b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Pose2dTest, Inequality) {
|
|
|
|
|
const Pose2d a{0_m, 5_m, Rotation2d(43_deg)};
|
|
|
|
|
const Pose2d b{0_m, 5_ft, Rotation2d(43_deg)};
|
|
|
|
|
EXPECT_TRUE(a != b);
|
|
|
|
|
}
|
2019-09-28 18:40:56 -04:00
|
|
|
|
|
|
|
|
TEST(Pose2dTest, Minus) {
|
|
|
|
|
const Pose2d initial{0_m, 0_m, Rotation2d(45.0_deg)};
|
|
|
|
|
const Pose2d final{5_m, 5_m, Rotation2d(45.0_deg)};
|
|
|
|
|
|
|
|
|
|
const auto transform = final - initial;
|
|
|
|
|
|
2020-07-02 18:09:36 -07:00
|
|
|
EXPECT_NEAR(transform.X().to<double>(), 5.0 * std::sqrt(2.0), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(transform.Y().to<double>(), 0.0, kEpsilon);
|
2019-09-28 18:40:56 -04:00
|
|
|
EXPECT_NEAR(transform.Rotation().Degrees().to<double>(), 0.0, kEpsilon);
|
|
|
|
|
}
|