-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcollapse.h
119 lines (96 loc) · 2.48 KB
/
collapse.h
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
#ifndef COLLAPSE_H
#define COLLAPSE_H
#include "storage.h"
class collapse {
public:
abstract_voxel_storage* until = nullptr;
boost::optional<int> max_depth;
regular_voxel_storage* operator()(abstract_voxel_storage* storage, int dx, int dy, int dz) {
int d[3] = { dx, dy, dz };
int nonzero = 0;
int D = -1;
for (int i = 0; i < 3; ++i) {
if (d[i] != 0) {
nonzero++;
D = i;
}
}
if (nonzero != 1 && D != 2 && dz != -1) {
throw std::runtime_error("Only collapse over negative Z is implemented");
}
regular_voxel_storage* collapsed = (regular_voxel_storage*)storage->empty_copy();
auto bounds = storage->bounds();
size_t i0, j0, k0, i1, j1, k1;
bounds[0].tie(i0, j0, k0);
bounds[1].tie(i1, j1, k1);
vec_n<3, size_t> ijk;
; for (ijk.get(0) = i0; ijk.get(0) <= i1; ++ijk.get(0)) {
for (ijk.get(1) = j0; ijk.get(1) <= j1; ++ijk.get(1)) {
for (ijk.get(2) = k0; ijk.get(2) <= k1;) {
if (storage->Get(ijk)) {
collapsed->Set(ijk);
while (storage->Get(ijk)) {
if (++ijk.get(2) > k1) {
break;
}
}
} else {
++ijk.get(2);
}
}
}
}
return collapsed;
}
};
// @todo merge with above
class collapse_count {
public:
abstract_voxel_storage* until = nullptr;
boost::optional<int> max_depth;
regular_voxel_storage* operator()(abstract_voxel_storage* storage, int dx, int dy, int dz) {
int d[3] = { dx, dy, dz };
int nonzero = 0;
int D = -1;
for (int i = 0; i < 3; ++i) {
if (d[i] != 0) {
nonzero++;
D = i;
}
}
if (nonzero != 1 && D != 2 && dz != -1) {
throw std::runtime_error("Only collapse over negative Z is implemented");
}
// @todo maybe create some static instances of these types, or in a map?
voxel_uint32_t vut;
regular_voxel_storage* collapsed = (regular_voxel_storage*)storage->empty_copy_as(&vut);
auto bounds = storage->bounds();
size_t i0, j0, k0, i1, j1, k1;
bounds[0].tie(i0, j0, k0);
bounds[1].tie(i1, j1, k1);
uint32_t count = 0;
vec_n<3, size_t> ijk;
; for (ijk.get(0) = i0; ijk.get(0) <= i1; ++ijk.get(0)) {
for (ijk.get(1) = j0; ijk.get(1) <= j1; ++ijk.get(1)) {
for (ijk.get(2) = k0; ijk.get(2) <= k1;) {
if (storage->Get(ijk)) {
count = 0;
auto ijk_0 = ijk;
while (storage->Get(ijk)) {
++count;
if (++ijk.get(2) > k1) {
break;
}
}
collapsed->Set(ijk_0, &count);
}
else {
++ijk.get(2);
}
}
}
}
return collapsed;
}
};
#endif