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