2024-06-04 21:27:32 -04: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.
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/math/geometry/Rectangle2d.hpp"
|
2024-06-04 21:27:32 -04:00
|
|
|
|
|
|
|
|
TEST(Rectangle2dTest, NewWithCorners) {
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d cornerA{1_m, 2_m};
|
|
|
|
|
constexpr wpi::math::Translation2d cornerB{4_m, 6_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::Rectangle2d rect{cornerA, cornerB};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
|
|
|
|
EXPECT_EQ(3.0, rect.XWidth().value());
|
|
|
|
|
EXPECT_EQ(4.0, rect.YWidth().value());
|
|
|
|
|
EXPECT_EQ(2.5, rect.Center().X().value());
|
|
|
|
|
EXPECT_EQ(4.0, rect.Center().Y().value());
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 23:01:18 -08:00
|
|
|
TEST(Rectangle2dTest, Intersects) {
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center{4_m, 3_m, 90_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect{center, 2_m, 3_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
EXPECT_TRUE(rect.Intersects(wpi::math::Translation2d{5.5_m, 4_m}));
|
|
|
|
|
EXPECT_TRUE(rect.Intersects(wpi::math::Translation2d{3_m, 2_m}));
|
|
|
|
|
EXPECT_FALSE(rect.Intersects(wpi::math::Translation2d{4_m, 1.5_m}));
|
|
|
|
|
EXPECT_FALSE(rect.Intersects(wpi::math::Translation2d{4_m, 3.5_m}));
|
2024-06-04 21:27:32 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-07 23:01:18 -08:00
|
|
|
TEST(Rectangle2dTest, Contains) {
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center{2_m, 3_m, 45_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect{center, 3_m, 1_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
EXPECT_TRUE(rect.Contains(wpi::math::Translation2d{2_m, 3_m}));
|
|
|
|
|
EXPECT_TRUE(rect.Contains(wpi::math::Translation2d{3_m, 4_m}));
|
|
|
|
|
EXPECT_FALSE(rect.Contains(wpi::math::Translation2d{3_m, 3_m}));
|
2024-06-04 21:27:32 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-07 23:01:18 -08:00
|
|
|
TEST(Rectangle2dTest, Distance) {
|
2024-06-04 21:27:32 -04:00
|
|
|
constexpr double kEpsilon = 1E-9;
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center{1_m, 2_m, 270_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect{center, 1_m, 2_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d point1{2.5_m, 2_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
EXPECT_NEAR(0.5, rect.Distance(point1).value(), kEpsilon);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d point2{1_m, 2_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
EXPECT_NEAR(0, rect.Distance(point2).value(), kEpsilon);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d point3{1_m, 1_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
EXPECT_NEAR(0.5, rect.Distance(point3).value(), kEpsilon);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d point4{-1_m, 2.5_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
EXPECT_NEAR(1, rect.Distance(point4).value(), kEpsilon);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 23:01:18 -08:00
|
|
|
TEST(Rectangle2dTest, Nearest) {
|
2024-06-04 21:27:32 -04:00
|
|
|
constexpr double kEpsilon = 1E-9;
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center{1_m, 1_m, 90_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect{center, 3_m, 4_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d point1{1_m, 3_m};
|
2024-12-07 23:01:18 -08:00
|
|
|
auto nearestPoint1 = rect.Nearest(point1);
|
2024-06-04 21:27:32 -04:00
|
|
|
EXPECT_NEAR(1.0, nearestPoint1.X().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(2.5, nearestPoint1.Y().value(), kEpsilon);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Translation2d point2{0_m, 0_m};
|
2024-12-07 23:01:18 -08:00
|
|
|
auto nearestPoint2 = rect.Nearest(point2);
|
2024-06-04 21:27:32 -04:00
|
|
|
EXPECT_NEAR(0.0, nearestPoint2.X().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(0.0, nearestPoint2.Y().value(), kEpsilon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Rectangle2dTest, Equals) {
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center1{2_m, 3_m, 0_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect1{center1, 5_m, 3_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center2{2_m, 3_m, 0_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect2{center2, 5_m, 3_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::math::Pose2d center3{2_m, 3_m, 0_deg};
|
|
|
|
|
constexpr wpi::math::Rectangle2d rect3{center3, 3_m, 3_m};
|
2024-06-04 21:27:32 -04:00
|
|
|
|
|
|
|
|
EXPECT_EQ(rect1, rect2);
|
|
|
|
|
EXPECT_NE(rect2, rect3);
|
|
|
|
|
}
|