| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "peryashkin_v_binary_component_contour_processing/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <queue> | ||
| 7 | #include <ranges> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "peryashkin_v_binary_component_contour_processing/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace peryashkin_v_binary_component_contour_processing { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | inline bool InBounds(int x, int y, int w, int h) { | ||
| 18 | 2968 | return x >= 0 && y >= 0 && x < w && y < h; | |
| 19 | } | ||
| 20 | |||
| 21 | inline std::int64_t Cross(const Point &o, const Point &a, const Point &b) { | ||
| 22 | 720 | return (static_cast<std::int64_t>(a.x - o.x) * static_cast<std::int64_t>(b.y - o.y)) - | |
| 23 | 720 | (static_cast<std::int64_t>(a.y - o.y) * static_cast<std::int64_t>(b.x - o.x)); | |
| 24 | } | ||
| 25 | |||
| 26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | inline std::vector<Point> ConvexHullMonotonicChain(std::vector<Point> pts) { |
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (pts.empty()) { |
| 28 | ✗ | return {}; | |
| 29 | } | ||
| 30 | |||
| 31 |
9/78✗ 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 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✓ Branch 67 taken 320 times.
✓ Branch 68 taken 88 times.
✓ Branch 69 taken 232 times.
✗ Branch 70 not taken.
✓ Branch 71 taken 88 times.
✓ Branch 72 taken 288 times.
✓ Branch 73 taken 320 times.
✓ Branch 74 taken 216 times.
✓ Branch 75 taken 104 times.
✗ Branch 76 not taken.
✓ Branch 77 taken 216 times.
|
928 | std::ranges::sort(pts, [](const Point &a, const Point &b) { return (a.x < b.x) || ((a.x == b.x) && (a.y < b.y)); }); |
| 32 | |||
| 33 | const auto uniq = | ||
| 34 |
3/8✓ Branch 0 taken 216 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 216 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
320 | std::ranges::unique(pts, [](const Point &a, const Point &b) { return (a.x == b.x) && (a.y == b.y); }); |
| 35 | pts.erase(uniq.begin(), pts.end()); | ||
| 36 | |||
| 37 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 48 times.
|
56 | if (pts.size() == 1) { |
| 38 | return pts; | ||
| 39 | } | ||
| 40 | |||
| 41 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<Point> lower; |
| 42 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | lower.reserve(pts.size()); |
| 43 |
2/2✓ Branch 0 taken 368 times.
✓ Branch 1 taken 48 times.
|
416 | for (const auto &p : pts) { |
| 44 |
4/4✓ Branch 0 taken 360 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 232 times.
✓ Branch 3 taken 128 times.
|
600 | while (lower.size() >= 2 && Cross(lower[lower.size() - 2], lower[lower.size() - 1], p) <= 0) { |
| 45 | lower.pop_back(); | ||
| 46 | } | ||
| 47 | lower.push_back(p); | ||
| 48 | } | ||
| 49 | |||
| 50 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<Point> upper; |
| 51 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | upper.reserve(pts.size()); |
| 52 |
2/2✓ Branch 0 taken 368 times.
✓ Branch 1 taken 48 times.
|
416 | for (std::size_t i = pts.size(); i-- > 0;) { |
| 53 | const auto &p = pts[i]; | ||
| 54 |
4/4✓ Branch 0 taken 360 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 232 times.
|
600 | while (upper.size() >= 2 && Cross(upper[upper.size() - 2], upper[upper.size() - 1], p) <= 0) { |
| 55 | upper.pop_back(); | ||
| 56 | } | ||
| 57 | upper.push_back(p); | ||
| 58 | } | ||
| 59 | |||
| 60 | lower.pop_back(); | ||
| 61 | upper.pop_back(); | ||
| 62 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | lower.insert(lower.end(), upper.begin(), upper.end()); |
| 63 | return lower; | ||
| 64 | } | ||
| 65 | |||
| 66 | // ---- Helpers to reduce cognitive complexity in ExtractComponents4 ---- | ||
| 67 | |||
| 68 | inline std::size_t Idx(int x, int y, int w) { | ||
| 69 |
5/6✓ Branch 0 taken 376 times.
✓ Branch 1 taken 1528 times.
✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 800 times.
✓ Branch 6 taken 624 times.
|
3384 | return (static_cast<std::size_t>(y) * static_cast<std::size_t>(w)) + static_cast<std::size_t>(x); |
| 70 | } | ||
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 1464 times.
✓ Branch 1 taken 40 times.
|
1504 | inline void TryPush4(const BinaryImage &img, int w, int h, int nx, int ny, std::vector<std::uint8_t> &vis, |
| 73 | std::queue<Point> &q) { | ||
| 74 |
2/2✓ Branch 0 taken 1424 times.
✓ Branch 1 taken 40 times.
|
1464 | if (!InBounds(nx, ny, w, h)) { |
| 75 | return; | ||
| 76 | } | ||
| 77 | const std::size_t nid = Idx(nx, ny, w); | ||
| 78 |
4/4✓ Branch 0 taken 800 times.
✓ Branch 1 taken 624 times.
✓ Branch 2 taken 320 times.
✓ Branch 3 taken 480 times.
|
1424 | if ((img.data[nid] == 1) && (vis[nid] == 0U)) { |
| 79 | 320 | vis[nid] = 1U; | |
| 80 | 320 | q.push(Point{.x = nx, .y = ny}); | |
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | 56 | inline std::vector<Point> BfsComponent4(const BinaryImage &img, int w, int h, int sx, int sy, | |
| 85 | std::vector<std::uint8_t> &vis) { | ||
| 86 | 56 | std::vector<Point> pts; | |
| 87 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | pts.reserve(128); |
| 88 | |||
| 89 | std::queue<Point> q; | ||
| 90 | |||
| 91 | const std::size_t sid = Idx(sx, sy, w); | ||
| 92 | 56 | vis[sid] = 1U; | |
| 93 | |||
| 94 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | q.push(Point{.x = sx, .y = sy}); |
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 376 times.
✓ Branch 1 taken 56 times.
|
432 | while (!q.empty()) { |
| 97 | 376 | const Point p = q.front(); | |
| 98 | q.pop(); | ||
| 99 | |||
| 100 | pts.push_back(p); | ||
| 101 | |||
| 102 |
1/2✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
|
376 | TryPush4(img, w, h, p.x + 1, p.y, vis, q); |
| 103 |
1/2✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
|
376 | TryPush4(img, w, h, p.x - 1, p.y, vis, q); |
| 104 |
1/2✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
|
376 | TryPush4(img, w, h, p.x, p.y + 1, vis, q); |
| 105 |
1/2✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
|
376 | TryPush4(img, w, h, p.x, p.y - 1, vis, q); |
| 106 | } | ||
| 107 | |||
| 108 | 56 | return pts; | |
| 109 | } | ||
| 110 | |||
| 111 | 56 | inline std::vector<std::vector<Point>> ExtractComponents4(const BinaryImage &img) { | |
| 112 | 56 | const int w = img.width; | |
| 113 | 56 | const int h = img.height; | |
| 114 | |||
| 115 | 56 | std::vector<std::vector<Point>> comps; | |
| 116 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | if ((w <= 0) || (h <= 0)) { |
| 117 | return comps; | ||
| 118 | } | ||
| 119 | |||
| 120 | 56 | const std::size_t n = static_cast<std::size_t>(w) * static_cast<std::size_t>(h); | |
| 121 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | std::vector<std::uint8_t> vis(n, 0U); |
| 122 | |||
| 123 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 56 times.
|
360 | for (int yy = 0; yy < h; ++yy) { |
| 124 |
2/2✓ Branch 0 taken 1904 times.
✓ Branch 1 taken 304 times.
|
2208 | for (int xx = 0; xx < w; ++xx) { |
| 125 | const std::size_t id = Idx(xx, yy, w); | ||
| 126 |
4/4✓ Branch 0 taken 376 times.
✓ Branch 1 taken 1528 times.
✓ Branch 2 taken 320 times.
✓ Branch 3 taken 56 times.
|
1904 | if ((img.data[id] == 0) || (vis[id] != 0U)) { |
| 127 | 1848 | continue; | |
| 128 | } | ||
| 129 |
1/4✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
112 | comps.push_back(BfsComponent4(img, w, h, xx, yy, vis)); |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | return comps; | ||
| 134 | ✗ | } | |
| 135 | |||
| 136 | 56 | inline OutType SolveSEQ(const BinaryImage &img) { | |
| 137 | 56 | auto comps = ExtractComponents4(img); | |
| 138 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | OutType hulls; |
| 139 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | hulls.reserve(comps.size()); |
| 140 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 56 times.
|
112 | for (auto &c : comps) { |
| 141 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
112 | hulls.push_back(ConvexHullMonotonicChain(std::move(c))); |
| 142 | } | ||
| 143 | 56 | return hulls; | |
| 144 | 56 | } | |
| 145 | |||
| 146 | } // namespace | ||
| 147 | |||
| 148 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | PeryashkinVBinaryComponentContourProcessingSEQ::PeryashkinVBinaryComponentContourProcessingSEQ(const InType &in) { |
| 149 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 150 | GetInput() = in; | ||
| 151 | GetOutput().clear(); | ||
| 152 | 56 | } | |
| 153 | |||
| 154 | 112 | bool PeryashkinVBinaryComponentContourProcessingSEQ::ValidationImpl() { | |
| 155 | const auto &in = GetInput(); | ||
| 156 |
2/4✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
|
112 | if (in.width <= 0 || in.height <= 0) { |
| 157 | return false; | ||
| 158 | } | ||
| 159 | 112 | const std::size_t need = static_cast<std::size_t>(in.width) * static_cast<std::size_t>(in.height); | |
| 160 | 112 | return in.data.size() == need; | |
| 161 | } | ||
| 162 | |||
| 163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | bool PeryashkinVBinaryComponentContourProcessingSEQ::PreProcessingImpl() { |
| 164 | local_out_.clear(); | ||
| 165 | 56 | return true; | |
| 166 | } | ||
| 167 | |||
| 168 | 56 | bool PeryashkinVBinaryComponentContourProcessingSEQ::RunImpl() { | |
| 169 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | if (!ValidationImpl()) { |
| 170 | return false; | ||
| 171 | } | ||
| 172 | |||
| 173 | 56 | local_out_ = SolveSEQ(GetInput()); | |
| 174 | 56 | return true; | |
| 175 | } | ||
| 176 | |||
| 177 | 56 | bool PeryashkinVBinaryComponentContourProcessingSEQ::PostProcessingImpl() { | |
| 178 | 56 | GetOutput() = local_out_; | |
| 179 | 56 | return true; | |
| 180 | } | ||
| 181 | |||
| 182 | } // namespace peryashkin_v_binary_component_contour_processing | ||
| 183 |