| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kondrashova_v_marking_components_seq/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <array> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <queue> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "kondrashova_v_marking_components_seq/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace kondrashova_v_marking_components_seq { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | KondrashovaVTaskSEQ::KondrashovaVTaskSEQ(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 32 | GetOutput() = {}; | |
| 18 | 32 | } | |
| 19 | |||
| 20 | 32 | bool KondrashovaVTaskSEQ::ValidationImpl() { | |
| 21 | 32 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 | 32 | bool KondrashovaVTaskSEQ::PreProcessingImpl() { | |
| 25 | const auto &in = GetInput(); | ||
| 26 | |||
| 27 | 32 | width_ = in.width; | |
| 28 | 32 | height_ = in.height; | |
| 29 | 32 | image_ = in.data; | |
| 30 | |||
| 31 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
32 | if (width_ > 0 && height_ > 0 && static_cast<int>(image_.size()) == width_ * height_) { |
| 32 | ✗ | labels_1d_.assign(static_cast<size_t>(width_) * static_cast<size_t>(height_), 0); | |
| 33 | } else { | ||
| 34 | labels_1d_.clear(); | ||
| 35 | } | ||
| 36 | |||
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | GetOutput().count = 0; |
| 38 | GetOutput().labels.clear(); | ||
| 39 | 32 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | namespace { | ||
| 43 | |||
| 44 | ✗ | void BfsComponent(int start_i, int start_j, int width, int height, int label, const std::vector<uint8_t> &image, | |
| 45 | std::vector<int> &labels_1d) { | ||
| 46 | ✗ | const std::array<int, 4> dx = {-1, 1, 0, 0}; | |
| 47 | ✗ | const std::array<int, 4> dy = {0, 0, -1, 1}; | |
| 48 | |||
| 49 | ✗ | auto inside = [&](int xi, int yi) { return (xi >= 0 && xi < height && yi >= 0 && yi < width); }; | |
| 50 | |||
| 51 | std::queue<std::pair<int, int>> q; | ||
| 52 | q.emplace(start_i, start_j); | ||
| 53 | ✗ | labels_1d[(static_cast<size_t>(start_i) * static_cast<size_t>(width)) + static_cast<size_t>(start_j)] = label; | |
| 54 | |||
| 55 | ✗ | while (!q.empty()) { | |
| 56 | auto [cx, cy] = q.front(); | ||
| 57 | q.pop(); | ||
| 58 | |||
| 59 | ✗ | for (int ki = 0; ki < 4; ++ki) { | |
| 60 | ✗ | int nx = cx + dx.at(ki); | |
| 61 | ✗ | int ny = cy + dy.at(ki); | |
| 62 | |||
| 63 | ✗ | if (!inside(nx, ny)) { | |
| 64 | ✗ | continue; | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | auto nidx = (static_cast<size_t>(nx) * static_cast<size_t>(width)) + static_cast<size_t>(ny); | |
| 68 | ✗ | if (image[nidx] == 0 && labels_1d[nidx] == 0) { | |
| 69 | ✗ | labels_1d[nidx] = label; | |
| 70 | q.emplace(nx, ny); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | ✗ | } | |
| 75 | |||
| 76 | } // namespace | ||
| 77 | |||
| 78 | 32 | bool KondrashovaVTaskSEQ::RunImpl() { | |
| 79 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
32 | if (width_ <= 0 || height_ <= 0 || image_.empty()) { |
| 80 | 32 | GetOutput().count = 0; | |
| 81 | 32 | return true; | |
| 82 | } | ||
| 83 | |||
| 84 | int current_label = 0; | ||
| 85 | |||
| 86 | ✗ | for (int ii = 0; ii < height_; ++ii) { | |
| 87 | ✗ | for (int jj = 0; jj < width_; ++jj) { | |
| 88 | ✗ | auto idx = (static_cast<size_t>(ii) * static_cast<size_t>(width_)) + static_cast<size_t>(jj); | |
| 89 | ✗ | if (image_[idx] == 0 && labels_1d_[idx] == 0) { | |
| 90 | ✗ | ++current_label; | |
| 91 | ✗ | BfsComponent(ii, jj, width_, height_, current_label, image_, labels_1d_); | |
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | ✗ | GetOutput().count = current_label; | |
| 97 | ✗ | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | 32 | bool KondrashovaVTaskSEQ::PostProcessingImpl() { | |
| 101 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
32 | if (width_ <= 0 || height_ <= 0) { |
| 102 | GetOutput().labels.clear(); | ||
| 103 | 32 | return true; | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | GetOutput().labels.assign(height_, std::vector<int>(width_, 0)); | |
| 107 | |||
| 108 | ✗ | for (int ii = 0; ii < height_; ++ii) { | |
| 109 | ✗ | for (int jj = 0; jj < width_; ++jj) { | |
| 110 | ✗ | auto idx = (static_cast<size_t>(ii) * static_cast<size_t>(width_)) + static_cast<size_t>(jj); | |
| 111 | ✗ | GetOutput().labels[ii][jj] = labels_1d_[idx]; | |
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | return true; | ||
| 116 | } | ||
| 117 | |||
| 118 | } // namespace kondrashova_v_marking_components_seq | ||
| 119 |