| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "orehov_n_jarvis_pass/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <set> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "oneapi/tbb.h" | ||
| 9 | #include "orehov_n_jarvis_pass/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace orehov_n_jarvis_pass { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | 20 | struct BestState { | |
| 16 | Point point; | ||
| 17 | bool valid = false; | ||
| 18 | }; | ||
| 19 | |||
| 20 | bool IsBetterPoint(Point current, Point candidate, Point best) { | ||
| 21 | double orient = OrehovNJarvisPassTBB::CheckLeft(current, best, candidate); | ||
| 22 | 120 | if (orient > 0.0) { | |
| 23 | return true; | ||
| 24 | } | ||
| 25 |
2/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
84 | if (orient == 0.0) { |
| 26 | double dx_c = candidate.x - current.x; | ||
| 27 | double dy_c = candidate.y - current.y; | ||
| 28 | 24 | double dist_c = (dx_c * dx_c) + (dy_c * dy_c); | |
| 29 | |||
| 30 | double dx_b = best.x - current.x; | ||
| 31 | double dy_b = best.y - current.y; | ||
| 32 | 24 | double dist_b = (dx_b * dx_b) + (dy_b * dy_b); | |
| 33 | |||
| 34 | return dist_c > dist_b; | ||
| 35 | } | ||
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 | BestState FindInitialBestState(const std::vector<Point> &input, Point current) { | ||
| 40 | BestState init; | ||
| 41 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | for (const auto &p : input) { |
| 42 | if (!(p == current)) { | ||
| 43 | 20 | init.point = p; | |
| 44 | 20 | init.valid = true; | |
| 45 | 20 | break; | |
| 46 | } | ||
| 47 | } | ||
| 48 | return init; | ||
| 49 | } | ||
| 50 | |||
| 51 | ✗ | BestState ReduceBestStates(const BestState &a, const BestState &b, Point current) { | |
| 52 | ✗ | if (!a.valid) { | |
| 53 | ✗ | return b; | |
| 54 | } | ||
| 55 | ✗ | if (!b.valid) { | |
| 56 | ✗ | return a; | |
| 57 | } | ||
| 58 | ✗ | return BestState{.point = IsBetterPoint(current, b.point, a.point) ? b.point : a.point, .valid = true}; | |
| 59 | } | ||
| 60 | |||
| 61 | } // namespace | ||
| 62 | |||
| 63 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | OrehovNJarvisPassTBB::OrehovNJarvisPassTBB(const InType &in) { |
| 64 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 65 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetInput() = in; |
| 66 | 12 | GetOutput() = std::vector<Point>(); | |
| 67 | 12 | } | |
| 68 | |||
| 69 | 12 | bool OrehovNJarvisPassTBB::ValidationImpl() { | |
| 70 | 12 | return (!GetInput().empty()); | |
| 71 | } | ||
| 72 | |||
| 73 | 12 | bool OrehovNJarvisPassTBB::PreProcessingImpl() { | |
| 74 | 12 | std::set<Point> tmp(GetInput().begin(), GetInput().end()); | |
| 75 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | input_.assign(tmp.begin(), tmp.end()); |
| 76 | 12 | return true; | |
| 77 | } | ||
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | bool OrehovNJarvisPassTBB::RunImpl() { |
| 80 |
4/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
|
12 | if (input_.size() == 1 || input_.size() == 2) { |
| 81 | 8 | res_ = input_; | |
| 82 | 8 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | 4 | Point current = FindFirstElem(); | |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | res_.push_back(current); |
| 87 | |||
| 88 | while (true) { | ||
| 89 | 20 | Point next = FindNext(current); | |
| 90 | if (next == res_[0]) { | ||
| 91 | break; | ||
| 92 | } | ||
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
|
16 | current = next; |
| 95 | res_.push_back(next); | ||
| 96 | 16 | } | |
| 97 | |||
| 98 | 4 | return true; | |
| 99 | } | ||
| 100 | |||
| 101 | 20 | Point OrehovNJarvisPassTBB::FindNext(Point current) const { | |
| 102 | const size_t n = input_.size(); | ||
| 103 | |||
| 104 | BestState init = FindInitialBestState(input_, current); | ||
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!init.valid) { |
| 106 | ✗ | return current; | |
| 107 | } | ||
| 108 | |||
| 109 | 140 | auto body = [&](const tbb::blocked_range<size_t> &r, BestState local) -> BestState { | |
| 110 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 140 times.
|
280 | for (size_t i = r.begin(); i != r.end(); ++i) { |
| 111 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 108 times.
|
140 | const Point &p = input_[i]; |
| 112 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 108 times.
|
140 | if (p == current) { |
| 113 | 20 | continue; | |
| 114 | } | ||
| 115 |
2/4✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
144 | if (!local.valid || IsBetterPoint(current, p, local.point)) { |
| 116 | 36 | local.point = p; | |
| 117 | local.valid = true; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | 140 | return local; | |
| 121 | 20 | }; | |
| 122 | |||
| 123 | 20 | auto reduce = [&](const BestState &a, const BestState &b) -> BestState { return ReduceBestStates(a, b, current); }; | |
| 124 | |||
| 125 | 20 | BestState result = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, n), init, body, reduce); | |
| 126 | 20 | return result.point; | |
| 127 | } | ||
| 128 | |||
| 129 | ✗ | double OrehovNJarvisPassTBB::CheckLeft(Point a, Point b, Point c) { | |
| 130 |
2/4✓ Branch 0 taken 84 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
120 | return ((b.x - a.x) * (c.y - a.y)) - ((b.y - a.y) * (c.x - a.x)); |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | Point OrehovNJarvisPassTBB::FindFirstElem() const { | |
| 134 | ✗ | Point current = input_[0]; | |
| 135 |
2/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 4 times.
|
32 | for (auto f : input_) { |
| 136 |
2/12✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 28 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
28 | if (f.x < current.x || (f.y < current.y && f.x == current.x)) { |
| 137 | current = f; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | ✗ | return current; | |
| 141 | } | ||
| 142 | |||
| 143 | ✗ | double OrehovNJarvisPassTBB::Distance(Point a, Point b) { | |
| 144 | ✗ | return std::sqrt(std::pow(a.y - b.y, 2) + std::pow(a.x - b.x, 2)); | |
| 145 | } | ||
| 146 | |||
| 147 | 12 | bool OrehovNJarvisPassTBB::PostProcessingImpl() { | |
| 148 | 12 | GetOutput() = res_; | |
| 149 | 12 | return true; | |
| 150 | } | ||
| 151 | |||
| 152 | } // namespace orehov_n_jarvis_pass | ||
| 153 |