-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprocessor.h
363 lines (316 loc) · 10.1 KB
/
processor.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
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
#ifndef PROCESSOR_H
#define PROCESSOR_H
#include "traversal.h"
#include "factory.h"
#include "storage.h"
#include "writer.h"
#include "progress_writer.h"
#include "volume.h"
#include "voxelizer.h"
#include <TopoDS_Shape.hxx>
#include <TopoDS_Compound.hxx>
#include <thread>
#ifdef WITH_IFC
#ifdef IFCOPENSHELL_08
#include <ifcgeom/IfcGeomElement.h>
typedef IfcGeom::BRepElement elem_t;
#else
#ifdef IFCOPENSHELL_07
#include <ifcparse/IfcLogger.h> // @todo < commit in IfcopenShell
#include <ifcgeom_schema_agnostic/IfcGeomElement.h>
typedef IfcGeom::BRepElement elem_t;
#else
#include <map> // @todo < commit in IfcopenShell
#include <ifcparse/IfcLogger.h> // @todo < commit in IfcopenShell
#include <ifcgeom/IfcGeomElement.h>
typedef IfcGeom::BRepElement<double> elem_t;
#endif
#endif
#endif
typedef TopoDS_Shape geometry_t;
typedef std::vector<std::pair<std::pair<void*, int>, TopoDS_Compound > > geometry_collection_t;
class voxelization_mode {
public:
int tag;
voxelization_mode(int t) : tag(t) {}
};
class MERGED : public voxelization_mode {
public:
MERGED() : voxelization_mode(1) {}
};
class SEPARATE : public voxelization_mode {
public:
SEPARATE() : voxelization_mode(2) {}
};
class SEPARATE_MINUS : public voxelization_mode {
public:
SEPARATE_MINUS() : voxelization_mode(3) {}
};
enum VOXEL_VALUATION {
CONSTANT_ONE,
PRODUCT_ID
};
class fill_volume_t {
public:
bool y;
VOXEL_VALUATION value;
fill_volume_t(bool b, VOXEL_VALUATION v = CONSTANT_ONE) : y(b), value(v) {}
};
class VOLUME : public fill_volume_t {
public:
VOLUME() : fill_volume_t(true) {}
};
class SURFACE : public fill_volume_t {
public:
SURFACE() : fill_volume_t(false) {}
};
class VOLUME_PRODUCT_ID : public fill_volume_t {
public:
VOLUME_PRODUCT_ID() : fill_volume_t(true, PRODUCT_ID) {}
};
class output {
private:
std::string path_;
const abstract_voxel_storage* voxels_;
std::vector<std::function<regular_voxel_storage*(regular_voxel_storage*)>> post_processors_;
public:
int mode;
output(const MERGED& mode, const std::string& path = "")
: mode(mode.tag), path_(path), voxels_(0) {}
output(const SEPARATE& mode, const std::string& path)
: mode(mode.tag), path_(path), voxels_(0) {}
output(const SEPARATE_MINUS& mode, const abstract_voxel_storage* voxels, const std::string& path)
: mode(mode.tag), path_(path), voxels_(voxels) {}
output parallelize() const {
if (mode == 1) {
return output(MERGED());
} else if (mode == 2 || mode == 3) {
return *this;
} else {
throw std::runtime_error("invalid mode");
}
}
static void write(const std::string& filename, abstract_voxel_storage* voxels) {
voxel_writer writer;
writer.SetVoxels(voxels);
writer.Write(filename);
}
static void interpolate(char* buff, const std::string& path, int i) {
int r = snprintf(buff, 255, path.c_str(), i);
if (r < 0 || r >= 255) {
std::cerr << std::endl << "error allocating buffer" << std::endl;
abort();
}
}
void intermediate_result(int i, abstract_voxel_storage* a, abstract_voxel_storage* b) const {
if (mode == 1) {
a->boolean_union_inplace(b);
} else if (mode == 2 || mode == 3) {
char buff[255];
interpolate(buff, path_, i);
if (mode == 3) {
b->boolean_subtraction_inplace(voxels_);
}
write(buff, b);
}
}
abstract_voxel_storage* final_result(abstract_voxel_storage* a, abstract_voxel_storage* b) const {
if (mode == 1 && path_.size()) {
regular_voxel_storage* tmp = (regular_voxel_storage*)a;
int i = 0;
for (auto& fn : post_processors_) {
write(path_ + "-step-" + boost::lexical_cast<std::string>(i++), tmp);
// std::cerr << "Step " << i << " bounds:" << tmp->bounds()[0].format() << " - " << a->bounds()[1].format() << std::endl;
regular_voxel_storage* b = fn(tmp);
if (tmp != a) {
delete tmp;
}
tmp = b;
}
// std::cerr << "Final bounds:" << tmp->bounds()[0].format() << " - " << a->bounds()[1].format() << std::endl;
write(path_, tmp);
return tmp;
}
return nullptr;
}
template <typename Fn>
output& post(Fn p) {
post_processors_.emplace_back(p);
return *this;
}
};
class abstract_processor {
public:
virtual void process(geometry_collection_t::const_iterator start, geometry_collection_t::const_iterator end, const fill_volume_t& volume, const output& output) = 0;
virtual ~abstract_processor() {}
virtual abstract_voxel_storage* voxels() = 0;
};
class processor : public abstract_processor {
private:
bit_t use_bits;
factory factory_;
abstract_voxel_storage* voxels_;
abstract_voxel_storage* voxels_temp_;
std::function<void(int)> progress_;
int nx_, ny_, nz_;
double x1_, y1_, z1_, d_;
bool use_copy_, use_scanline_;
public:
processor(double x1, double y1, double z1, double d, int nx, int ny, int nz, size_t chunk_size, const std::function<void(int)>& progress)
: factory_(factory().chunk_size(chunk_size))
, voxels_(factory_.create(x1, y1, z1, d, nx, ny, nz))
, voxels_temp_(factory_.create(x1, y1, z1, d, nx, ny, nz))
, progress_(progress)
, nx_(nx), ny_(ny), nz_(nz)
, x1_(x1), y1_(y1), z1_(z1), d_(d)
, use_copy_(false)
, use_scanline_(true) {}
processor(abstract_voxel_storage* storage, const std::function<void(int)>& progress)
: voxels_(storage)
, voxels_temp_(storage->empty_copy_as(&use_bits))
, progress_(progress)
, use_copy_(true)
, use_scanline_(true) {}
bool& use_scanline() { return use_scanline_; }
~processor() {
if (!use_copy_) {
delete voxels_;
}
delete voxels_temp_;
}
void process(geometry_collection_t::const_iterator a, geometry_collection_t::const_iterator b, const fill_volume_t& volume, const output& output) {
int N = std::distance(a, b);
int n = 0;
for (geometry_collection_t::const_iterator it = a; it < b; ++it) {
abstract_voxel_storage* to_write = volume.y ? voxels_temp_ : voxels_;
voxelizer voxeliser(it->second, (regular_voxel_storage*)to_write, !use_scanline_, !use_scanline_);
voxeliser.epsilon() = 1e-9;
voxeliser.Convert();
if (volume.y) {
auto filled = traversal_voxel_filler_inverse_with_input()((regular_voxel_storage*)to_write);
if (volume.value == PRODUCT_ID) {
// @todo this still needs to be generalized
if (voxels_->value_bits() != 32) {
throw std::runtime_error("Unable to assign product ids to this voxel value type");
}
uint32_t v = it->first.second;
for (auto& ijk : *filled) {
voxels_->Set(ijk, &v);
}
} else {
output.intermediate_result(it->first.second, voxels_, filled);
}
delete filled;
if (use_copy_) {
auto tmp = voxels_temp_->empty_copy();
delete voxels_temp_;
voxels_temp_ = tmp;
} else {
delete voxels_temp_;
voxels_temp_ = factory_.create(x1_, y1_, z1_, d_, nx_, ny_, nz_);
}
}
n++;
progress_(n * 100 / N);
}
auto post_result = output.final_result(voxels_, voxels_temp_);
if (post_result && post_result != voxels_) {
delete voxels_;
voxels_ = post_result;
}
}
abstract_voxel_storage* voxels() { return voxels_; }
};
class threaded_processor : public abstract_processor {
double x1_, y1_, z1_, d_;
int nx_, ny_, nz_;
std::vector<processor*> cs;
size_t num_threads_;
progress_writer& p_;
size_t chunk_size_;
abstract_voxel_storage* result_;
abstract_voxel_storage* voxels_;
public:
threaded_processor(double x1, double y1, double z1, double d, int nx, int ny, int nz, size_t chunk_size, progress_writer& p)
: x1_(x1), y1_(y1), z1_(z1), d_(d), nx_(nx), ny_(ny), nz_(nz), chunk_size_(chunk_size), num_threads_(std::thread::hardware_concurrency()), p_(p), result_(nullptr), voxels_(nullptr) {}
threaded_processor(double x1, double y1, double z1, double d, int nx, int ny, int nz, size_t chunk_size, size_t num_threads, progress_writer& p)
: x1_(x1), y1_(y1), z1_(z1), d_(d), nx_(nx), ny_(ny), nz_(nz), chunk_size_(chunk_size), num_threads_(num_threads > 0 ? num_threads : std::thread::hardware_concurrency()), p_(p), result_(nullptr), voxels_(nullptr) {}
threaded_processor(abstract_voxel_storage* storage, size_t num_threads, progress_writer& progress)
: voxels_(storage)
, num_threads_(num_threads)
, p_(progress)
, result_(nullptr)
{}
~threaded_processor() {
for (auto it = cs.begin(); it != cs.end(); ++it) {
// delete *it;
}
}
void process(geometry_collection_t::const_iterator start, geometry_collection_t::const_iterator end, const fill_volume_t& volume, const output& output) {
const size_t N = std::distance(start, end);
const double d = (double)N / num_threads_;
std::vector<std::thread> ts;
cs.resize(num_threads_);
ts.reserve(num_threads_);
auto progress = p_.thread(num_threads_);
size_t x0 = 0;
bool first = true;
for (size_t i = 0; i < num_threads_; ++i) {
size_t x1 = (size_t)floor((i + 1) * d);
if (x1 == x0) continue;
first = false;
if (i == num_threads_ - 1) {
x1 = N;
}
if (voxels_) {
auto local_storage = first ? voxels_ : voxels_->empty_copy();
ts.emplace_back(std::thread([this, local_storage, x0, x1, i, &start, &volume, &output, &progress]() {
processor* vf = new processor(local_storage, [i, &progress](int p) {
progress(i, p);
});
cs[i] = vf;
vf->process(start + x0, start + x1, volume, output.parallelize());
}));
} else {
// @todo refactor this.
ts.emplace_back(std::thread([this, x0, x1, i, &start, &volume, &output, &progress]() {
processor* vf = new processor(x1_, y1_, z1_, d_, nx_, ny_, nz_, chunk_size_, [i, &progress](int p) {
progress(i, p);
});
cs[i] = vf;
vf->process(start + x0, start + x1, volume, output.parallelize());
}));
}
x0 = x1;
}
for (auto jt = ts.begin(); jt != ts.end(); ++jt) {
jt->join();
}
if (output.mode == 1) {
abstract_voxel_storage* first = 0;
for (auto jt = cs.begin(); jt != cs.end(); ++jt) {
if (*jt) {
if (first == 0) {
first = (**jt).voxels();
} else {
first->boolean_union_inplace((**jt).voxels());
delete *jt;
}
}
}
if (first) {
progress.end();
result_ = first;
auto post_process_result = output.final_result(first, first);
if (post_process_result) {
result_ = post_process_result;
}
}
}
progress.end();
}
abstract_voxel_storage* voxels() {
return result_;
}
};
#endif