| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dorogin_v_bin_img_conv_hull/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <functional> | ||
| 8 | #include <ranges> | ||
| 9 | #include <stack> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "dorogin_v_bin_img_conv_hull/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace dorogin_v_bin_img_conv_hull { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | constexpr uint8_t kThreshold = 128; | ||
| 20 | |||
| 21 | inline bool IsInside(int col, int row, int width, int height) { | ||
| 22 | 5856 | return col >= 0 && row >= 0 && col < width && row < height; | |
| 23 | } | ||
| 24 | |||
| 25 | int64_t Cross(const Point &a, const Point &b, const Point &c) { | ||
| 26 | 2416 | int64_t x1 = b.x - a.x; | |
| 27 | 2416 | int64_t y1 = b.y - a.y; | |
| 28 | 2416 | int64_t x2 = c.x - a.x; | |
| 29 | 2416 | int64_t y2 = c.y - a.y; | |
| 30 |
2/2✓ Branch 0 taken 560 times.
✓ Branch 1 taken 648 times.
|
1208 | return (x1 * y2) - (y1 * x2); |
| 31 | } | ||
| 32 | |||
| 33 | } // namespace | ||
| 34 | |||
| 35 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | DoroginVBinImgConvHullSeq::DoroginVBinImgConvHullSeq(const InType &in) : w_(in) { |
| 36 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 37 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | GetInput() = in; |
| 38 | 40 | } | |
| 39 | |||
| 40 | 40 | bool DoroginVBinImgConvHullSeq::ValidationImpl() { | |
| 41 | const auto &in = GetInput(); | ||
| 42 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | if (in.width <= 0 || in.height <= 0) { |
| 43 | return false; | ||
| 44 | } | ||
| 45 | 40 | return in.pixels.size() == static_cast<size_t>(in.width) * static_cast<size_t>(in.height); | |
| 46 | } | ||
| 47 | |||
| 48 | 40 | bool DoroginVBinImgConvHullSeq::PreProcessingImpl() { | |
| 49 | 40 | w_ = GetInput(); | |
| 50 | ThresholdImage(); | ||
| 51 | 40 | return true; | |
| 52 | } | ||
| 53 | |||
| 54 | 40 | bool DoroginVBinImgConvHullSeq::RunImpl() { | |
| 55 | 40 | FindComponents(); | |
| 56 | |||
| 57 | w_.convex_hulls.clear(); | ||
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 40 times.
|
88 | for (const auto &comp : w_.components) { |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (comp.empty()) { |
| 61 | ✗ | continue; | |
| 62 | } | ||
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (comp.size() < 3) { |
| 65 | 24 | w_.convex_hulls.push_back(comp); | |
| 66 | } else { | ||
| 67 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
48 | w_.convex_hulls.push_back(BuildHull(comp)); |
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | 40 | GetOutput() = w_; | |
| 72 | 40 | return true; | |
| 73 | } | ||
| 74 | |||
| 75 | 40 | bool DoroginVBinImgConvHullSeq::PostProcessingImpl() { | |
| 76 | 40 | return true; | |
| 77 | } | ||
| 78 | |||
| 79 | ✗ | size_t DoroginVBinImgConvHullSeq::Index(int col, int row, int width) { | |
| 80 |
4/4✓ Branch 0 taken 736 times.
✓ Branch 1 taken 1360 times.
✓ Branch 2 taken 1568 times.
✓ Branch 3 taken 1312 times.
|
4976 | return (static_cast<size_t>(row) * static_cast<size_t>(width)) + static_cast<size_t>(col); |
| 81 | } | ||
| 82 | |||
| 83 | ✗ | void DoroginVBinImgConvHullSeq::ThresholdImage() { | |
| 84 |
2/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1360 times.
✓ Branch 3 taken 736 times.
|
2096 | std::ranges::transform(w_.pixels, w_.pixels.begin(), [](uint8_t p) { return p > kThreshold ? 255 : 0; }); |
| 85 | ✗ | } | |
| 86 | |||
| 87 | 48 | void DoroginVBinImgConvHullSeq::ExploreComponent(int start_col, int start_row, int width, int height, | |
| 88 | std::vector<bool> &visited, std::vector<Point> &component) { | ||
| 89 | std::stack<Point> stack; | ||
| 90 | stack.emplace(start_col, start_row); | ||
| 91 | |||
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | visited[Index(start_col, start_row, width)] = true; |
| 93 | |||
| 94 | 48 | const std::array<int, 4> dx{1, -1, 0, 0}; | |
| 95 | 48 | const std::array<int, 4> dy{0, 0, 1, -1}; | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 736 times.
✓ Branch 1 taken 48 times.
|
784 | while (!stack.empty()) { |
| 98 |
1/2✓ Branch 0 taken 736 times.
✗ Branch 1 not taken.
|
736 | Point p = stack.top(); |
| 99 | stack.pop(); | ||
| 100 | |||
| 101 | component.push_back(p); | ||
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 2944 times.
✓ Branch 1 taken 736 times.
|
3680 | for (size_t dir = 0; dir < dx.size(); ++dir) { |
| 104 | 2944 | const int nx = p.x + dx.at(dir); | |
| 105 |
2/2✓ Branch 0 taken 2912 times.
✓ Branch 1 taken 32 times.
|
2944 | const int ny = p.y + dy.at(dir); |
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2880 times.
|
2912 | if (!IsInside(nx, ny, width, height)) { |
| 108 | 2256 | continue; | |
| 109 | } | ||
| 110 | |||
| 111 | size_t idx = Index(nx, ny, width); | ||
| 112 |
2/2✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 1312 times.
|
2880 | if (visited[idx]) { |
| 113 | 1568 | continue; | |
| 114 | } | ||
| 115 |
2/2✓ Branch 0 taken 624 times.
✓ Branch 1 taken 688 times.
|
1312 | if (w_.pixels[idx] == 0) { |
| 116 | 624 | continue; | |
| 117 | } | ||
| 118 | |||
| 119 | visited[idx] = true; | ||
| 120 | stack.emplace(nx, ny); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | 48 | } | |
| 124 | |||
| 125 | 40 | void DoroginVBinImgConvHullSeq::FindComponents() { | |
| 126 | 40 | const int width = w_.width; | |
| 127 | 40 | const int height = w_.height; | |
| 128 | |||
| 129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
|
40 | std::vector<bool> visited(static_cast<size_t>(width) * height, false); |
| 130 | w_.components.clear(); | ||
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 40 times.
|
296 | for (int row = 0; row < height; ++row) { |
| 133 |
2/2✓ Branch 0 taken 2096 times.
✓ Branch 1 taken 256 times.
|
2352 | for (int col = 0; col < width; ++col) { |
| 134 | size_t idx = Index(col, row, width); | ||
| 135 | |||
| 136 |
4/4✓ Branch 0 taken 736 times.
✓ Branch 1 taken 1360 times.
✓ Branch 2 taken 688 times.
✓ Branch 3 taken 48 times.
|
2096 | if (w_.pixels[idx] == 0 || visited[idx]) { |
| 137 | 2048 | continue; | |
| 138 | } | ||
| 139 | |||
| 140 | 48 | std::vector<Point> comp; | |
| 141 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | ExploreComponent(col, row, width, height, visited, comp); |
| 142 | |||
| 143 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | w_.components.push_back(std::move(comp)); |
| 144 | } | ||
| 145 | } | ||
| 146 | 40 | } | |
| 147 | |||
| 148 | 24 | std::vector<Point> DoroginVBinImgConvHullSeq::BuildHull(const std::vector<Point> &points) { | |
| 149 | 24 | std::vector<Point> pts = points; | |
| 150 | |||
| 151 | std::ranges::sort(pts, std::less<>{}); | ||
| 152 | auto unique_end = std::ranges::unique(pts).begin(); | ||
| 153 | pts.erase(unique_end, pts.end()); | ||
| 154 | |||
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (pts.size() < 3) { |
| 156 | ✗ | return pts; | |
| 157 | } | ||
| 158 | |||
| 159 | 24 | std::vector<Point> hull; | |
| 160 | |||
| 161 |
2/2✓ Branch 0 taken 712 times.
✓ Branch 1 taken 24 times.
|
736 | for (const auto &p : pts) { |
| 162 |
4/4✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 648 times.
✓ Branch 3 taken 560 times.
|
1360 | while (hull.size() >= 2 && Cross(hull[hull.size() - 2], hull.back(), p) <= 0) { |
| 163 | hull.pop_back(); | ||
| 164 | } | ||
| 165 | hull.push_back(p); | ||
| 166 | } | ||
| 167 | |||
| 168 | size_t lsize = hull.size(); | ||
| 169 |
2/2✓ Branch 0 taken 688 times.
✓ Branch 1 taken 24 times.
|
712 | for (int i = static_cast<int>(pts.size()) - 2; i >= 0; --i) { |
| 170 |
4/4✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 648 times.
|
1336 | while (hull.size() > lsize && Cross(hull[hull.size() - 2], hull.back(), pts[i]) <= 0) { |
| 171 | hull.pop_back(); | ||
| 172 | } | ||
| 173 |
2/2✓ Branch 0 taken 664 times.
✓ Branch 1 taken 24 times.
|
688 | hull.push_back(pts[i]); |
| 174 | } | ||
| 175 | |||
| 176 | hull.pop_back(); | ||
| 177 | return hull; | ||
| 178 | } | ||
| 179 | |||
| 180 | } // namespace dorogin_v_bin_img_conv_hull | ||
| 181 |