| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "urin_o_graham_passage/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> // Добавлено для size_t | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "urin_o_graham_passage/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace urin_o_graham_passage { | ||
| 11 | |||
| 12 | ✗ | UrinOGrahamPassageSEQ::UrinOGrahamPassageSEQ(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | ✗ | GetInput() = in; | |
| 15 | ✗ | GetOutput() = OutType(); | |
| 16 | ✗ | } | |
| 17 | |||
| 18 | ✗ | bool UrinOGrahamPassageSEQ::ValidationImpl() { | |
| 19 | const auto &points = GetInput(); | ||
| 20 | |||
| 21 | ✗ | if (points.size() < 3) { | |
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 | const Point &first = points[0]; | ||
| 26 | ✗ | for (size_t i = 1; i < points.size(); ++i) { | |
| 27 | if (points[i] != first) { | ||
| 28 | return true; // Нашли разную точку - валидация пройдена | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | return false; // Все точки одинаковые | ||
| 33 | } | ||
| 34 | |||
| 35 | ✗ | bool UrinOGrahamPassageSEQ::PreProcessingImpl() { | |
| 36 | GetOutput().clear(); | ||
| 37 | ✗ | return true; | |
| 38 | } | ||
| 39 | |||
| 40 | ✗ | Point UrinOGrahamPassageSEQ::FindLowestPoint(const InType &points) { | |
| 41 | ✗ | Point lowest = points[0]; | |
| 42 | |||
| 43 | ✗ | for (size_t i = 1; i < points.size(); ++i) { | |
| 44 | ✗ | if (points[i].y < lowest.y - 1e-10 || | |
| 45 | ✗ | (std::abs(points[i].y - lowest.y) < 1e-10 && points[i].x < lowest.x - 1e-10)) { | |
| 46 | ✗ | lowest = points[i]; | |
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | ✗ | return lowest; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | double UrinOGrahamPassageSEQ::PolarAngle(const Point &base, const Point &p) { | |
| 54 | ✗ | const double dx = p.x - base.x; | |
| 55 | ✗ | const double dy = p.y - base.y; | |
| 56 | |||
| 57 | ✗ | if (std::abs(dx) < 1e-10 && std::abs(dy) < 1e-10) { | |
| 58 | return -1e10; | ||
| 59 | } | ||
| 60 | |||
| 61 | ✗ | return std::atan2(dy, dx); | |
| 62 | } | ||
| 63 | |||
| 64 | ✗ | int UrinOGrahamPassageSEQ::Orientation(const Point &p, const Point &q, const Point &r) { | |
| 65 | ✗ | const double val = ((q.x - p.x) * (r.y - p.y)) - ((q.y - p.y) * (r.x - p.x)); | |
| 66 | |||
| 67 | ✗ | if (std::abs(val) < 1e-10) { | |
| 68 | return 0; | ||
| 69 | } | ||
| 70 | ✗ | return (val > 0) ? 1 : -1; | |
| 71 | } | ||
| 72 | |||
| 73 | ✗ | double UrinOGrahamPassageSEQ::DistanceSquared(const Point &p1, const Point &p2) { | |
| 74 | ✗ | const double dx = p2.x - p1.x; | |
| 75 | ✗ | const double dy = p2.y - p1.y; | |
| 76 | ✗ | return (dx * dx) + (dy * dy); // Добавлены скобки для читаемости | |
| 77 | } | ||
| 78 | |||
| 79 | ✗ | bool UrinOGrahamPassageSEQ::RunImpl() { | |
| 80 | const InType &points = GetInput(); | ||
| 81 | |||
| 82 | ✗ | if (points.size() < 3) { | |
| 83 | return false; | ||
| 84 | } | ||
| 85 | |||
| 86 | ✗ | const Point p0 = FindLowestPoint(points); | |
| 87 | |||
| 88 | ✗ | std::vector<Point> other_points = PrepareOtherPoints(points, p0); | |
| 89 | ✗ | if (other_points.empty()) { | |
| 90 | return false; | ||
| 91 | } | ||
| 92 | |||
| 93 | ✗ | if (AreAllCollinear(p0, other_points)) { | |
| 94 | ✗ | GetOutput() = {p0, other_points.back()}; | |
| 95 | ✗ | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | ✗ | GetOutput() = BuildConvexHull(p0, other_points); | |
| 99 | ✗ | return true; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | std::vector<Point> UrinOGrahamPassageSEQ::PrepareOtherPoints(const InType &points, const Point &p0) { | |
| 103 | ✗ | std::vector<Point> other_points; | |
| 104 | ✗ | other_points.reserve(points.size() - 1); | |
| 105 | |||
| 106 | ✗ | for (const auto &point : points) { | |
| 107 | if (point != p0) { | ||
| 108 | other_points.push_back(point); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | ✗ | std::ranges::sort(other_points, [&p0](const Point &a, const Point &b) { | |
| 113 | ✗ | double angle_a = PolarAngle(p0, a); | |
| 114 | ✗ | double angle_b = PolarAngle(p0, b); | |
| 115 | |||
| 116 | ✗ | if (std::abs(angle_a - angle_b) < 1e-10) { | |
| 117 | ✗ | return DistanceSquared(p0, a) < DistanceSquared(p0, b); | |
| 118 | } | ||
| 119 | ✗ | return angle_a < angle_b; | |
| 120 | }); | ||
| 121 | |||
| 122 | ✗ | return other_points; | |
| 123 | } | ||
| 124 | |||
| 125 | ✗ | bool UrinOGrahamPassageSEQ::AreAllCollinear(const Point &p0, const std::vector<Point> &points) { | |
| 126 | ✗ | for (size_t i = 1; i < points.size(); ++i) { | |
| 127 | if (Orientation(p0, points[0], points[i]) != 0) { | ||
| 128 | return false; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | return true; | ||
| 132 | } | ||
| 133 | |||
| 134 | ✗ | std::vector<Point> UrinOGrahamPassageSEQ::BuildConvexHull(const Point &p0, const std::vector<Point> &points) { | |
| 135 | ✗ | std::vector<Point> hull; | |
| 136 | ✗ | hull.reserve(points.size() + 1); | |
| 137 | hull.push_back(p0); | ||
| 138 | hull.push_back(points[0]); | ||
| 139 | |||
| 140 | ✗ | for (size_t i = 1; i < points.size(); ++i) { | |
| 141 | ✗ | while (hull.size() >= 2) { | |
| 142 | const Point &p = hull[hull.size() - 2]; | ||
| 143 | const Point &q = hull.back(); | ||
| 144 | if (Orientation(p, q, points[i]) <= 0) { | ||
| 145 | hull.pop_back(); | ||
| 146 | } else { | ||
| 147 | break; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | hull.push_back(points[i]); | ||
| 151 | } | ||
| 152 | |||
| 153 | ✗ | return hull; | |
| 154 | } | ||
| 155 | |||
| 156 | ✗ | bool UrinOGrahamPassageSEQ::PostProcessingImpl() { | |
| 157 | ✗ | return !GetOutput().empty(); | |
| 158 | } | ||
| 159 | |||
| 160 | } // namespace urin_o_graham_passage | ||
| 161 |