forked from VCG/compresso
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cc3d.hpp
400 lines (327 loc) · 9.4 KB
/
cc3d.hpp
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Connected Components for 2D images.
* Author: William Silversmith
* Affiliation: Seung Lab, Princeton University
* Date: August 2018 - June 2019, 2021
*
* ----
* LICENSE
*
* This is a special reduced feature version of cc3d
* that includes only the logic needed for CCL 4-connected.
* cc3d is ordinarily licensed as GPL v3. Get the full
* version of cc3d here:
*
* https://github.com/seung-lab/connected-components-3d
*
* License: GNU Lesser General Public License v3
* Copyright 2021 William Silversmith
*
* See COPYING.LESSER for that license. If the file is missing,
* you can find the text here:
* https://www.gnu.org/licenses/lgpl-3.0.en.html
*/
#ifndef CC3D_SPECIAL_4_HPP
#define CC3D_SPECIAL_4_HPP
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <stdexcept>
namespace cc3d {
static size_t _dummy_N;
template <typename T>
class DisjointSet {
public:
T *ids;
size_t length;
DisjointSet () {
length = 65536; // 2^16, some "reasonable" starting size
ids = new T[length]();
if (!ids) {
throw std::runtime_error("Failed to allocate memory for the Union-Find datastructure for connected components.");
}
}
DisjointSet (size_t len) {
length = len;
ids = new T[length]();
if (!ids) {
throw std::runtime_error("Failed to allocate memory for the Union-Find datastructure for connected components.");
}
}
DisjointSet (const DisjointSet &cpy) {
length = cpy.length;
ids = new T[length]();
if (!ids) {
throw std::runtime_error("Failed to allocate memory for the Union-Find datastructure for connected components.");
}
for (int i = 0; i < length; i++) {
ids[i] = cpy.ids[i];
}
}
~DisjointSet () {
delete []ids;
}
T root (T n) {
T i = ids[n];
while (i != ids[i]) {
ids[i] = ids[ids[i]]; // path compression
i = ids[i];
}
return i;
}
bool find (T p, T q) {
return root(p) == root(q);
}
void add(T p) {
if (p >= length) {
printf("Connected Components Error: Label %d cannot be mapped to union-find array of length %lu.\n", p, length);
throw "maximum length exception";
}
if (ids[p] == 0) {
ids[p] = p;
}
}
void unify (T p, T q) {
if (p == q) {
return;
}
T i = root(p);
T j = root(q);
if (i == 0) {
add(p);
i = p;
}
if (j == 0) {
add(q);
j = q;
}
ids[i] = j;
}
void print() {
for (int i = 0; i < 15; i++) {
printf("%d, ", ids[i]);
}
printf("\n");
}
// would be easy to write remove.
// Will be O(n).
};
// This is the second raster pass of the two pass algorithm family.
// The input array (output_labels) has been assigned provisional
// labels and this resolves them into their final labels. We
// modify this pass to also ensure that the output labels are
// numbered from 1 sequentially.
template <typename OUT = uint32_t>
OUT* relabel(
OUT* out_labels, const int64_t voxels,
const int64_t num_labels, DisjointSet<uint32_t> &equivalences,
size_t &N = _dummy_N, OUT start_label = 1
) {
OUT label;
OUT* renumber = new OUT[num_labels + 1]();
OUT next_label = start_label;
for (int64_t i = 1; i <= num_labels; i++) {
label = equivalences.root(i);
if (renumber[label] == 0) {
renumber[label] = next_label;
renumber[i] = next_label;
next_label++;
}
else {
renumber[i] = renumber[label];
}
}
// Raster Scan 2: Write final labels based on equivalences
N = next_label - start_label;
if (N < static_cast<size_t>(num_labels) || start_label != 1) {
for (int64_t loc = 0; loc < voxels; loc++) {
out_labels[loc] = renumber[out_labels[loc]];
}
}
delete[] renumber;
return out_labels;
}
template <typename OUT = uint32_t>
OUT* connected_components2d_4(
bool* in_labels,
const int64_t sx, const int64_t sy, const int64_t sz,
size_t max_labels, OUT *out_labels = NULL,
size_t &N = _dummy_N, OUT start_label = 1
) {
const int64_t sxy = sx * sy;
const int64_t voxels = sx * sy * sz;
max_labels++;
max_labels = std::min(max_labels, static_cast<size_t>(voxels) + 1); // + 1L for an array with no zeros
max_labels = std::min(max_labels, static_cast<size_t>(std::numeric_limits<OUT>::max()));
DisjointSet<uint32_t> equivalences(max_labels);
if (out_labels == NULL) {
out_labels = new OUT[voxels]();
}
/*
Layout of forward pass mask.
A is the current location.
D C
B A
*/
// const int64_t A = 0;
const int64_t B = -1;
const int64_t C = -sx;
const int64_t D = -1-sx;
int64_t loc = 0;
OUT next_label = 0;
// Raster Scan 1: Set temporary labels and
// record equivalences in a disjoint set.
bool cur = 0;
for (int64_t z = 0; z < sz; z++) {
for (int64_t y = 0; y < sy; y++) {
for (int64_t x = 0; x < sx; x++) {
loc = x + sx * y + sxy * z;
cur = in_labels[loc];
if (cur) {
continue;
}
if (x > 0 && !in_labels[loc + B]) {
out_labels[loc] = out_labels[loc + B];
if (y > 0 && in_labels[loc + D] && !in_labels[loc + C]) {
equivalences.unify(out_labels[loc], out_labels[loc + C]);
}
}
else if (y > 0 && !in_labels[loc + C]) {
out_labels[loc] = out_labels[loc + C];
}
else {
next_label++;
out_labels[loc] = next_label;
equivalences.add(out_labels[loc]);
}
}
}
}
return relabel<OUT>(out_labels, voxels, next_label, equivalences, N, start_label);
}
template <typename OUT = uint32_t>
OUT* connected_components3d_6(
bool* in_labels,
const int64_t sx, const int64_t sy, const int64_t sz,
size_t max_labels,
OUT *out_labels = NULL, size_t &N = _dummy_N
) {
const int64_t sxy = sx * sy;
const int64_t voxels = sxy * sz;
if (out_labels == NULL) {
out_labels = new OUT[voxels]();
}
if (max_labels == 0) {
return out_labels;
}
max_labels++; // corrects Cython estimation
max_labels = std::min(max_labels, static_cast<size_t>(voxels) + 1); // + 1L for an array with no zeros
max_labels = std::min(max_labels, static_cast<size_t>(std::numeric_limits<OUT>::max()));
DisjointSet<OUT> equivalences(max_labels);
/*
Layout of forward pass mask (which faces backwards).
N is the current location.
z = -1 z = 0
A B C J K L y = -1
D E F M N y = 0
G H I y = +1
-1 0 +1 -1 0 <-- x axis
*/
// Z - 1
const int64_t B = -sx - sxy;
const int64_t E = -sxy;
const int64_t D = -1 - sxy;
// Current Z
const int64_t K = -sx;
const int64_t M = -1;
const int64_t J = -1 - sx;
// N = 0;
int64_t loc = 0;
OUT next_label = 0;
// Raster Scan 1: Set temporary labels and
// record equivalences in a disjoint set.
for (int64_t z = 0; z < sz; z++) {
for (int64_t y = 0; y < sy; y++) {
for (int64_t x = 0; x < sx; x++) {
loc = x + sx * (y + sy * z);
const bool cur = in_labels[loc];
if (cur) {
continue;
}
if (x > 0 && !in_labels[loc + M]) {
out_labels[loc] = out_labels[loc + M];
if (y > 0 && !in_labels[loc + K] && in_labels[loc + J]) {
equivalences.unify(out_labels[loc], out_labels[loc + K]);
if (z > 0 && !in_labels[loc + E]) {
if (in_labels[loc + D] && in_labels[loc + B]) {
equivalences.unify(out_labels[loc], out_labels[loc + E]);
}
}
}
else if (z > 0 && !in_labels[loc + E] && in_labels[loc + D]) {
equivalences.unify(out_labels[loc], out_labels[loc + E]);
}
}
else if (y > 0 && !in_labels[loc + K]) {
out_labels[loc] = out_labels[loc + K];
if (z > 0 && !in_labels[loc + E] && in_labels[loc + B]) {
equivalences.unify(out_labels[loc], out_labels[loc + E]);
}
}
else if (z > 0 && !in_labels[loc + E]) {
out_labels[loc] = out_labels[loc + E];
}
else {
next_label++;
out_labels[loc] = next_label;
equivalences.add(out_labels[loc]);
}
}
}
}
if (next_label <= 1) {
N = next_label;
return out_labels;
}
return relabel<OUT>(out_labels, voxels, next_label, equivalences, N, 1);
}
template <typename OUT = uint64_t>
OUT* connected_components(
bool* in_labels,
const int64_t sx, const int64_t sy, const int64_t sz,
std::vector<uint64_t> &num_components_per_slice,
const size_t connectivity = 4, size_t &N = _dummy_N
) {
const int64_t sxy = sx * sy;
const int64_t voxels = sxy * sz;
size_t max_labels = voxels;
OUT* out_labels = new OUT[voxels]();
N = 0;
if (connectivity == 4) {
max_labels = static_cast<size_t>((sxy + 2) / 2);
for (int64_t z = 0; z < sz; z++) {
size_t tmp_N = 0;
connected_components2d_4<OUT>(
(in_labels + sxy * z), sx, sy, 1,
max_labels, (out_labels + sxy * z),
tmp_N, N + 1
);
num_components_per_slice[z] = tmp_N;
N += tmp_N;
}
}
else if (connectivity == 6) {
max_labels = static_cast<size_t>(((sx + 1) * (sy + 1) * (sz + 1)) / 2);
connected_components3d_6<OUT>(
in_labels, sx, sy, sz,
max_labels, out_labels, N
);
}
else {
throw std::runtime_error("Only 4 and 6 connectivities are supported.");
}
return out_labels;
}
};
#endif