File: dijkstra_splitter.cc

package info (click to toggle)
wsclean 3.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,296 kB
  • sloc: cpp: 129,246; python: 22,066; sh: 360; ansic: 230; makefile: 185
file content (296 lines) | stat: -rw-r--r-- 9,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: LGPL-3.0-only

#include "dijkstra_splitter.h"

#include <cassert>
#include <cmath>
#include <limits>
#include <queue>

#include <aocommon/uvector.h>

namespace radler::math {

void DijkstraSplitter::AddVerticalDivider(const float* image, float* scratch,
                                          float* output, std::size_t x1,
                                          std::size_t x2) const {
  DivideVertically(image, scratch, x1, x2);
  for (std::size_t y = 0; y != height_; ++y) {
    for (std::size_t i = y * width_ + x1; i != y * width_ + x2; ++i) {
      output[i] += scratch[i];
    }
  }
}

void DijkstraSplitter::AddHorizontalDivider(const float* image, float* scratch,
                                            float* output, std::size_t y1,
                                            std::size_t y2) const {
  DivideHorizontally(image, scratch, y1, y2);
  for (std::size_t i = y1 * width_; i != y2 * width_; ++i) {
    output[i] += scratch[i];
  }
}

void DijkstraSplitter::DivideVertically(const float* image, float* output,
                                        std::size_t x1, std::size_t x2) const {
  using VisitSet = std::priority_queue<Visit>;
  VisitSet visits;

  for (std::size_t x = x1; x != x2; ++x) {
    Visit visit;
    visit.distance = 0.0;
    visit.to = Coord(x, 0);
    visit.from = Coord(x, 0);
    visits.push(visit);
  }
  aocommon::UVector<Coord> path((x2 - x1) * height_);
  FillColumns(output, x1, x2, std::numeric_limits<float>::max());
  Visit visit;
  while (!visits.empty()) {
    visit = visits.top();
    visits.pop();
    const std::size_t x = visit.to.x;
    const std::size_t y = visit.to.y;
    if (y == height_) break;
    const float current_distance = output[x + y * width_];
    const float new_distance =
        visit.distance + std::fabs(image[x + y * width_]);
    if (new_distance < current_distance) {
      output[x + y * width_] = new_distance;
      path[x - x1 + y * (x2 - x1)] = visit.from;
      visit.distance = new_distance;
      visit.from = visit.to;
      if (x > x1) {
        visit.to = Coord(x - 1, y + 1);
        visits.push(visit);
        visit.to = Coord(x - 1, y);
        visits.push(visit);
      }
      visit.to = Coord(x, y + 1);
      visits.push(visit);
      if (x < x2 - 1) {
        visit.to = Coord(x + 1, y + 1);
        visits.push(visit);
        visit.to = Coord(x + 1, y);
        visits.push(visit);
      }
    }
  }
  FillColumns(output, x1, x2, 0.0);
  Coord path_coord = visit.from;
  while (path_coord.y > 0) {
    output[path_coord.x + path_coord.y * width_] = 1.0;
    path_coord = path[path_coord.x - x1 + path_coord.y * (x2 - x1)];
  }
  output[path_coord.x] = 1.0;
}

void DijkstraSplitter::DivideHorizontally(const float* image, float* output,
                                          std::size_t y1,
                                          std::size_t y2) const {
  using VisitSet = std::priority_queue<Visit>;
  VisitSet visits;

  for (std::size_t y = y1; y != y2; ++y) {
    Visit visit;
    visit.distance = 0.0;
    visit.to = Coord(0, y);
    visit.from = Coord(0, y);
    visits.push(visit);
  }
  aocommon::UVector<Coord> path(width_ * (y2 - y1));
  std::fill(output + y1 * width_, output + y2 * width_,
            std::numeric_limits<float>::max());
  Visit visit;
  while (!visits.empty()) {
    visit = visits.top();
    visits.pop();
    const std::size_t x = visit.to.x;
    const std::size_t y = visit.to.y;
    if (x == width_) break;
    const float current_distance = output[x + y * width_];
    const float new_distance =
        visit.distance + std::fabs(image[x + y * width_]);
    if (new_distance < current_distance) {
      output[x + y * width_] = new_distance;
      path[x + (y - y1) * width_] = visit.from;
      visit.distance = new_distance;
      visit.from = visit.to;
      if (y > y1) {
        visit.to = Coord(x + 1, y - 1);
        visits.push(visit);
        visit.to = Coord(x, y - 1);
        visits.push(visit);
      }
      visit.to = Coord(x + 1, y);
      visits.push(visit);
      if (y < y2 - 1) {
        visit.to = Coord(x + 1, y + 1);
        visits.push(visit);
        visit.to = Coord(x, y + 1);
        visits.push(visit);
      }
    }
  }
  std::fill(output + y1 * width_, output + y2 * width_, 0.0);
  Coord path_coord = visit.from;
  while (path_coord.x > 0) {
    output[path_coord.x + path_coord.y * width_] = 1.0;
    path_coord = path[path_coord.x + (path_coord.y - y1) * width_];
  }
  output[path_coord.y * width_] = 1.0;
}

void DijkstraSplitter::FloodVerticalArea(const float* subdivision,
                                         std::size_t subimage_x, bool* mask,
                                         std::size_t& x,
                                         std::size_t& subwidth) const {
  std::fill(mask, mask + width_ * height_, false);
  x = width_;
  std::size_t x2 = 0;
  for (std::size_t y = 0; y != height_; ++y) {
    const float* division_row = &subdivision[y * width_];
    bool* mask_row = &mask[y * width_];
    int x_iter = subimage_x;
    // Move to the left until a border is hit
    while (x_iter >= 0 && division_row[x_iter] == 0.0) {
      mask_row[x_iter] = true;
      --x_iter;
    }
    // Continue to the left through the border
    while (x_iter >= 0 && division_row[x_iter] != 0.0) {
      mask_row[x_iter] = true;
      --x_iter;
    }
    x = std::min<std::size_t>(x, x_iter + 1);
    x_iter = subimage_x + 1;
    // Move to the right until a border is hit
    while (static_cast<std::size_t>(x_iter) < width_ &&
           division_row[x_iter] == 0.0) {
      mask_row[x_iter] = true;
      ++x_iter;
    }
    x2 = std::max<std::size_t>(x2, x_iter);
  }
  if (x2 < x) {
    subwidth = 0;
  } else {
    subwidth = x2 - x;
  }
}

void DijkstraSplitter::FloodHorizontalArea(const float* subdivision,
                                           std::size_t subimage_y, bool* mask,
                                           std::size_t& y,
                                           std::size_t& subheight) const {
  std::fill(mask, mask + width_ * height_, false);
  y = height_;
  std::size_t y2 = 0;
  for (std::size_t x = 0; x != width_; ++x) {
    int y_iter = subimage_y;
    // Move up until a border is hit
    while (y_iter >= 0 && subdivision[y_iter * width_ + x] == 0.0) {
      mask[y_iter * width_ + x] = true;
      --y_iter;
    }
    // Continue to the left through the border
    while (y_iter >= 0 && subdivision[y_iter * width_ + x] != 0.0) {
      mask[y_iter * width_ + x] = true;
      --y_iter;
    }
    y = std::min<std::size_t>(y, y_iter + 1);
    y_iter = subimage_y + 1;
    // Move to the right until a border is hit
    while (static_cast<std::size_t>(y_iter) < height_ &&
           subdivision[y_iter * width_ + x] == 0.0) {
      mask[y_iter * width_ + x] = true;
      ++y_iter;
    }
    y2 = std::max<std::size_t>(y2, y_iter);
  }
  if (y2 < y) {
    subheight = 0;
  } else {
    subheight = y2 - y;
  }
}

void DijkstraSplitter::GetBoundingMask(const bool* vertical_mask,
                                       std::size_t vertical_mask_x,
                                       std::size_t vertical_mask_width,
                                       const bool* horizontal_mask, bool* mask,
                                       std::size_t& sub_x, std::size_t& sub_y,
                                       std::size_t& subwidth,
                                       std::size_t& subheight) const {
  sub_x = vertical_mask_width + vertical_mask_x;
  sub_y = height_;
  std::size_t sub_x2 = 0;
  std::size_t sub_y2 = 0;
  // The reason why values outside the bounding box are changed (see function
  // doc) is that we search for the bounding box (over the entire height of the
  // image) and update the mask at the same time. This could be changed by
  // separating this in two loops, but it's not required by the caller.
  for (std::size_t y = 0; y != height_; ++y) {
    const bool* vertical_mask_row = &vertical_mask[y * vertical_mask_width];
    const bool* horizontal_mask_row = &horizontal_mask[y * width_];
    bool* mask_row = &mask[y * width_];
    for (std::size_t x = 0; x != vertical_mask_width; ++x) {
      std::size_t hx = x + vertical_mask_x;
      if (vertical_mask_row[x] && horizontal_mask_row[hx]) {
        mask_row[hx] = true;
        sub_x = std::min(hx, sub_x);
        sub_y = std::min(y, sub_y);
        sub_x2 = std::max(hx, sub_x2);
        sub_y2 = y;  // y increases monotonically, so y >= sub_y2
      } else {
        mask_row[hx] = false;
      }
    }
  }
  if (sub_x2 < sub_x) {
    subwidth = 0;
    subheight = 0;
  } else {
    subwidth = sub_x2 + 1 - sub_x;
    subheight = sub_y2 + 1 - sub_y;
  }
  // If dimensions start off even, keep subimages even too
  if (width_ % 2 == 0) {
    if (subwidth % 2 != 0) {
      ++subwidth;
      if (subwidth + sub_x >= width_) {
        --sub_x;
        for (size_t y = sub_y; y != sub_y + subheight; ++y) {
          mask[sub_x + y * width_] = false;
        }
      } else {
        for (size_t y = sub_y; y != sub_y + subheight; ++y) {
          mask[sub_x + subwidth - 1 + y * width_] = false;
        }
      }
    }
  }
  if (height_ % 2 == 0) {
    if (subheight % 2 != 0) {
      ++subheight;
      if (subheight + sub_y >= height_) {
        --sub_y;
        std::fill_n(&mask[sub_y * width_ + sub_x], subwidth, false);
      } else {
        std::fill_n(&mask[(sub_y + subheight - 1) * width_ + sub_x], subwidth,
                    false);
      }
    }
  }
}

void DijkstraSplitter::FillColumns(float* output, std::size_t x_start,
                                   std::size_t x_end, float new_value) const {
  assert(x_start <= x_end);
  for (std::size_t y = 0; y != height_; ++y) {
    std::fill(output + width_ * y + x_start, output + width_ * y + x_end,
              new_value);
  }
}

}  // namespace radler::math