/
polytech_kolomna
/
polygon2d
Обзор
Документация
Войти
/
polytech_kolomna
/
polygon2d
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
math
tests/tests_math/tests_contour2d.cpp
202 строки
8 KB
Aleksandr Mahrov
fix
29 окт 2025, 15:32
29 окт 2025, 15:32
fdb0171
Код
Авторство
О чём код?
#include <gtest/gtest.h> #include "contour2d.h" #include "BoundedCurve2D.h" #include "Line2D.h" #include "Point2D.h" #include "Vector2D.h" #include <memory> #include <cmath> static std::shared_ptr<BoundedCurve2D> MakeBoundedLine( const Point2D& p1, const Point2D& p2) { Vector2D dir = p2 - p1; std::shared_ptr<Line2D> line = std::make_shared<Line2D>(p1, dir); return std::make_shared<BoundedCurve2D>(line, 0.0, 1.0); } // пустой контур TEST(Contour2D, Empty) { Contour2D c({}); EXPECT_TRUE(c.IsEmpty()); } // контур с кривыми TEST(Contour2D, HasCurves) { std::shared_ptr<BoundedCurve2D> b = MakeBoundedLine(Point2D{0, 0}, Point2D{1, 0}); Contour2D c({ b }); EXPECT_FALSE(c.IsEmpty()); } // замкнутый квадрат TEST(Contour2D, Closed) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine(Point2D{0,0}, Point2D{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine(Point2D{1,0}, Point2D{1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine(Point2D{1,1}, Point2D{0,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine(Point2D{0,1}, Point2D{0,0}); Contour2D c({ b1, b2, b3, b4 }); EXPECT_TRUE(c.IsClosed()); } // разомкнутый контур TEST(Contour2D, Open) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine(Point2D{0,0}, Point2D{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine(Point2D{1,0}, Point2D{1,1}); Contour2D c({ b1, b2 }); EXPECT_FALSE(c.IsClosed()); } // почти замкнутый контур //TEST(Contour2D, Almost closed) //{ // std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {1,0}); // std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,0}, {1,1}); // std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,1}, {0,1}); // std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0,1}, {0,0.000000001}); // почти замкнутый // // Contour2D c({b1, b2, b3, b4}); // EXPECT_TRUE(c.IsClosed()); //} // контур с одной перевёрнутой кривой не должен считаться замкнутым TEST(Contour2D, OneCurveReversed) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,0}, {1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,1}, {0,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0,0}, {0,1}); // перевёрнуто направление Contour2D c({b1, b2, b3, b4}); EXPECT_FALSE(c.IsClosed()); } // Замкнутый квадрат - все кривые соединены TEST(Contour2D, NoGaps) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine(Point2D{0,0}, Point2D{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine(Point2D{1,0}, Point2D{1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine(Point2D{1,1}, Point2D{0,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine(Point2D{0,1}, Point2D{0,0}); Contour2D c({b1, b2, b3, b4}); EXPECT_TRUE(c.IsClosed()); } // Две отдельные кривые - есть разрыв TEST(Contour2D, WithGaps) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine(Point2D{0,0}, Point2D{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine(Point2D{2,0}, Point2D{2,1}); Contour2D c({b1, b2}); EXPECT_FALSE(c.IsClosed()); } // Кривые в разных местах - не соединены TEST(Contour2D, DisconnectedCurves) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({2,0}, {3,0}); Contour2D c({b1, b2}); EXPECT_FALSE(c.IsClosed()); } // квадрат без самопересечений TEST(Contour2D, Square) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,0},{1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,1},{0,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0,1},{0,0}); Contour2D c({b1,b2,b3,b4}); EXPECT_FALSE(c.HasSelfIntersections()); } // контур с пересекающимися диагоналями TEST(Contour2D, SelfIntersections) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{2,2}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({2,2},{0,2}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({0,2},{2,0}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({2,0},{0,0}); Contour2D c({b1,b2,b3,b4}); EXPECT_TRUE(c.HasSelfIntersections()); } // контур с перекрывающимися отрезками TEST(Contour2D, OverlappingSegments) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,0}, {1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,1}, {0,0}); // диагональ через центр std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0,0}, {1,0}); // тот же отрезок, что b1 Contour2D c({b1,b2,b3,b4}); EXPECT_TRUE(c.HasSelfIntersections()); } // фигура восьмёрка с самопересечением TEST(Contour2D, FigureEight) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {1,1}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,1}, {0,2}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({0,2}, {1,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({1,1}, {0,0}); Contour2D c({b1,b2,b3,b4}); EXPECT_TRUE(c.HasSelfIntersections()); } // контур с несколькими точками пересечения TEST(Contour2D, MultipleIntersections) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {2,2}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({2,2}, {1,0}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,0}, {0,2}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0,2}, {2,0}); std::shared_ptr<BoundedCurve2D> b5 = MakeBoundedLine({2,0}, {0,0}); Contour2D c({b1,b2,b3,b4,b5}); EXPECT_TRUE(c.HasSelfIntersections()); } // контур с нормальными кривыми TEST(Contour2D, Curves) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{1,0}); Contour2D c({b1}); EXPECT_FALSE(c.HasDegenerateCurves()); } // контур с вырожденной кривой(нулевой длины) TEST(Contour2D, DegenerateCurves) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{0,0}); Contour2D c({b1}); EXPECT_TRUE(c.HasDegenerateCurves()); } // контур с почти нулевой длиной TEST(Contour2D, AlmostZeroLength) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0}, {1e-12, 1e-12}); Contour2D c({b1}); EXPECT_TRUE(c.HasDegenerateCurves()); } // идеальный контур(замкнут, без разрывов, без самопересечений) TEST(Contour2D, IsIdeal) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,0},{1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,1},{0,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0,1},{0,0}); Contour2D c({b1,b2,b3,b4}); EXPECT_TRUE(c.IsIdeal()); } // контур с разрывами не должен считаться идеальным TEST(Contour2D, NoIdeal) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{1,0}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,0},{1,1}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({1,1},{0.2,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({0.2,1},{0,0}); Contour2D c({b1,b2,b3,b4}); EXPECT_FALSE(c.IsIdeal()); } // контур с самопересечением не должен считаться идеальным TEST(Contour2D, NoIdealIntersection) { std::shared_ptr<BoundedCurve2D> b1 = MakeBoundedLine({0,0},{1,1}); std::shared_ptr<BoundedCurve2D> b2 = MakeBoundedLine({1,1},{0,2}); std::shared_ptr<BoundedCurve2D> b3 = MakeBoundedLine({0,2},{-1,1}); std::shared_ptr<BoundedCurve2D> b4 = MakeBoundedLine({-1,1},{0,0}); Contour2D c({b1,b2,b3,b4}); EXPECT_FALSE(c.IsIdeal()); }