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.
|
2020-03-14 22:01:52 -07:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
#include "frc/geometry/Pose2d.h"
|
|
|
|
|
#include "frc/geometry/Rotation2d.h"
|
|
|
|
|
#include "frc/geometry/Transform2d.h"
|
|
|
|
|
#include "frc/geometry/Translation2d.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
TEST(Transform2dTest, Inverse) {
|
2021-08-23 20:22:48 -07:00
|
|
|
const Pose2d initial{1_m, 2_m, 45_deg};
|
|
|
|
|
const Transform2d transform{{5_m, 0_m}, 5_deg};
|
2020-03-14 22:01:52 -07:00
|
|
|
|
|
|
|
|
auto transformed = initial + transform;
|
|
|
|
|
auto untransformed = transformed + transform.Inverse();
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
EXPECT_NEAR(initial.X().value(), untransformed.X().value(), 1e-9);
|
|
|
|
|
EXPECT_DOUBLE_EQ(initial.Y().value(), untransformed.Y().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(initial.Rotation().Degrees().value(),
|
|
|
|
|
untransformed.Rotation().Degrees().value());
|
2020-03-14 22:01:52 -07:00
|
|
|
}
|
2021-08-23 20:22:48 -07:00
|
|
|
|
|
|
|
|
TEST(Transform2dTest, Composition) {
|
|
|
|
|
const Pose2d initial{1_m, 2_m, 45_deg};
|
|
|
|
|
const Transform2d transform1{{5_m, 0_m}, 5_deg};
|
|
|
|
|
const Transform2d transform2{{0_m, 2_m}, 5_deg};
|
|
|
|
|
|
|
|
|
|
auto transformedSeparate = initial + transform1 + transform2;
|
|
|
|
|
auto transformedCombined = initial + (transform1 + transform2);
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
EXPECT_DOUBLE_EQ(transformedSeparate.X().value(),
|
|
|
|
|
transformedCombined.X().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(transformedSeparate.Y().value(),
|
|
|
|
|
transformedCombined.Y().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(transformedSeparate.Rotation().Degrees().value(),
|
|
|
|
|
transformedCombined.Rotation().Degrees().value());
|
2021-08-23 20:22:48 -07:00
|
|
|
}
|
2022-10-31 11:17:00 -05:00
|
|
|
|
|
|
|
|
TEST(Transform2dTest, Constexpr) {
|
|
|
|
|
constexpr Transform2d defaultCtor;
|
|
|
|
|
constexpr Transform2d translationRotationCtor{Translation2d{},
|
|
|
|
|
Rotation2d{10_deg}};
|
|
|
|
|
constexpr auto multiplied = translationRotationCtor * 5;
|
|
|
|
|
constexpr auto divided = translationRotationCtor / 2;
|
|
|
|
|
|
|
|
|
|
static_assert(defaultCtor.Translation().X() == 0_m);
|
|
|
|
|
static_assert(translationRotationCtor.X() == 0_m);
|
|
|
|
|
static_assert(translationRotationCtor.Y() == 0_m);
|
|
|
|
|
static_assert(multiplied.Rotation().Degrees() == 50_deg);
|
|
|
|
|
static_assert(translationRotationCtor.Inverse().Rotation().Degrees() ==
|
|
|
|
|
(-10_deg));
|
|
|
|
|
static_assert(translationRotationCtor.Inverse().X() == 0_m);
|
|
|
|
|
static_assert(translationRotationCtor.Inverse().Y() == 0_m);
|
|
|
|
|
static_assert(divided.Rotation().Degrees() == 5_deg);
|
|
|
|
|
}
|