| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ilin_a_algorithm_graham/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <utility> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "ilin_a_algorithm_graham/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace ilin_a_algorithm_graham { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | double Orient(const Point &p, const Point &q, const Point &r) { | ||
| 14 | 400 | return ((q.x - p.x) * (r.y - p.y)) - ((q.y - p.y) * (r.x - p.x)); | |
| 15 | } | ||
| 16 | |||
| 17 | double DistanceSq(const Point &p, const Point &q) { | ||
| 18 | 32 | double dx = p.x - q.x; | |
| 19 | 32 | double dy = p.y - q.y; | |
| 20 | 32 | return (dx * dx) + (dy * dy); | |
| 21 | } | ||
| 22 | |||
| 23 | 24 | Point FindLowestLeftmost(const std::vector<Point> &points) { | |
| 24 | 24 | Point p0 = points[0]; | |
| 25 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
|
144 | for (size_t i = 1; i < points.size(); ++i) { |
| 26 |
4/6✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 96 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
120 | if (points[i].y < p0.y || (points[i].y == p0.y && points[i].x < p0.x)) { |
| 27 | ✗ | p0 = points[i]; | |
| 28 | } | ||
| 29 | } | ||
| 30 | 24 | return p0; | |
| 31 | } | ||
| 32 | |||
| 33 | class PointComparator { | ||
| 34 | public: | ||
| 35 | 24 | explicit PointComparator(const Point &p0) : p0_(p0) {} | |
| 36 | |||
| 37 | 264 | bool operator()(const Point &a, const Point &b) const { | |
| 38 | double o = Orient(p0_, a, b); | ||
| 39 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 32 times.
|
264 | if (o != 0.0) { |
| 40 | 232 | return o > 0; | |
| 41 | } | ||
| 42 | 32 | return DistanceSq(p0_, a) < DistanceSq(p0_, b); | |
| 43 | } | ||
| 44 | |||
| 45 | private: | ||
| 46 | Point p0_; | ||
| 47 | }; | ||
| 48 | } // namespace | ||
| 49 | |||
| 50 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | IlinAGrahamSEQ::IlinAGrahamSEQ(const InType &in) { |
| 51 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 52 | GetInput() = in; | ||
| 53 | 24 | } | |
| 54 | |||
| 55 | 24 | bool IlinAGrahamSEQ::ValidationImpl() { | |
| 56 | 24 | return !GetInput().points.empty(); | |
| 57 | } | ||
| 58 | |||
| 59 | 24 | bool IlinAGrahamSEQ::PreProcessingImpl() { | |
| 60 | 24 | points_ = GetInput().points; | |
| 61 | hull_.clear(); | ||
| 62 | 24 | return true; | |
| 63 | } | ||
| 64 | |||
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | bool IlinAGrahamSEQ::RunImpl() { |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (points_.size() < 3) { |
| 67 | ✗ | hull_ = points_; | |
| 68 | ✗ | return true; | |
| 69 | } | ||
| 70 | |||
| 71 | 24 | Point p0 = FindLowestLeftmost(points_); | |
| 72 | |||
| 73 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<Point> sorted; |
| 74 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | sorted.reserve(points_.size()); |
| 75 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
|
168 | for (const Point &p : points_) { |
| 76 |
4/4✓ Branch 0 taken 48 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 24 times.
|
144 | if (p.x != p0.x || p.y != p0.y) { |
| 77 | sorted.push_back(p); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | std::ranges::sort(sorted, PointComparator(p0)); | ||
| 82 | |||
| 83 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<Point> stack; |
| 84 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | stack.reserve(sorted.size() + 1); |
| 85 | stack.push_back(p0); | ||
| 86 | stack.push_back(sorted[0]); | ||
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
|
120 | for (size_t i = 1; i < sorted.size(); ++i) { |
| 89 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | while (stack.size() >= 2) { |
| 90 | 136 | Point p = stack[stack.size() - 2]; | |
| 91 | 136 | Point q = stack[stack.size() - 1]; | |
| 92 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 96 times.
|
136 | if (Orient(p, q, sorted[i]) <= 0.0) { |
| 93 | stack.pop_back(); | ||
| 94 | } else { | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | stack.push_back(sorted[i]); | ||
| 99 | } | ||
| 100 | |||
| 101 | 24 | hull_ = std::move(stack); | |
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | 24 | bool IlinAGrahamSEQ::PostProcessingImpl() { | |
| 106 | 24 | GetOutput() = hull_; | |
| 107 | 24 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | } // namespace ilin_a_algorithm_graham | ||
| 111 |