-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvoxec.cpp
438 lines (375 loc) · 14.4 KB
/
voxec.cpp
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// @todo erode for angle slabs
// @todo finish memory mapped chunked storage and make default
#include "voxec.h"
#include "progress.h"
#include "json_logger.h"
#include <random>
voxel_operation_map::map_t& voxel_operation_map::map() {
static voxel_operation_map::map_t m;
static bool initialized = false;
if (!initialized) {
#ifdef WITH_IFC
m.insert(std::make_pair(std::string("parse"), &instantiate<op_parse_ifc_file>));
m.insert(std::make_pair(std::string("create_geometry"), &instantiate<op_create_geometry>));
#endif
m.insert(std::make_pair(std::string("voxelize"), &instantiate<op_voxelize>));
// m.insert(std::make_pair(std::string("voxelize_ids"), &instantiate<op_voxelize_ids>));
m.insert(std::make_pair(std::string("fill_gaps"), &instantiate<op_fill_gaps>));
m.insert(std::make_pair(std::string("offset"), &instantiate<op_offset>));
m.insert(std::make_pair(std::string("offset_xy"), &instantiate<op_offset_xy>));
m.insert(std::make_pair(std::string("outmost"), &instantiate<op_outmost>));
m.insert(std::make_pair(std::string("count"), &instantiate<op_count>));
m.insert(std::make_pair(std::string("volume"), &instantiate<op_volume>));
m.insert(std::make_pair(std::string("volume2"), &instantiate<op_volume_2>));
m.insert(std::make_pair(std::string("exterior"), &instantiate<op_volume_inverted>));
m.insert(std::make_pair(std::string("invert"), &instantiate<op_invert>));
m.insert(std::make_pair(std::string("copy"), &instantiate<op_copy>));
m.insert(std::make_pair(std::string("fill_volume"), &instantiate<op_fill_volume>));
m.insert(std::make_pair(std::string("union"), &instantiate<op_union>));
m.insert(std::make_pair(std::string("subtract"), &instantiate<op_subtract>));
m.insert(std::make_pair(std::string("intersect"), &instantiate<op_intersect>));
m.insert(std::make_pair(std::string("shift"), &instantiate<op_shift>));
m.insert(std::make_pair(std::string("sweep"), &instantiate<op_sweep>));
m.insert(std::make_pair(std::string("traverse"), &instantiate<op_traverse>));
m.insert(std::make_pair(std::string("constant_like"), &instantiate<op_constant_like>));
m.insert(std::make_pair(std::string("resample"), &instantiate<op_resample>));
m.insert(std::make_pair(std::string("collapse"), &instantiate<op_collapse>));
m.insert(std::make_pair(std::string("collapse_count"), &instantiate<op_collapse_count>));
m.insert(std::make_pair(std::string("print_components"), &instantiate<op_print_components>));
m.insert(std::make_pair(std::string("count_components"), &instantiate<op_count_components>));
m.insert(std::make_pair(std::string("print_values"), &instantiate<op_print_values>));
m.insert(std::make_pair(std::string("describe_components"), &instantiate<op_describe_components>));
m.insert(std::make_pair(std::string("describe_group_by"), &instantiate<op_describe_group_by>));
m.insert(std::make_pair(std::string("keep_components"), &instantiate<op_keep_components>));
m.insert(std::make_pair(std::string("dump_surfaces"), &instantiate<op_dump_surfaces>));
m.insert(std::make_pair(std::string("json_stats"), &instantiate<op_json_stats>));
m.insert(std::make_pair(std::string("greater_than"), &instantiate<op_greater>));
m.insert(std::make_pair(std::string("less_than"), &instantiate<op_less>));
#ifdef WITH_IFC
m.insert(std::make_pair(std::string("export_ifc"), &instantiate<op_export_ifc>));
m.insert(std::make_pair(std::string("export_json"), &instantiate<op_export_elements>));
m.insert(std::make_pair(std::string("filter_attributes"), &instantiate<op_create_attr_filter>));
m.insert(std::make_pair(std::string("filter_properties"), &instantiate<op_create_prop_filter>));
#endif
m.insert(std::make_pair(std::string("zeros"), &instantiate<op_zeros>));
m.insert(std::make_pair(std::string("plane"), &instantiate<op_plane<>>));
m.insert(std::make_pair(std::string("halfspace"), &instantiate<op_plane<1>>));
m.insert(std::make_pair(std::string("mesh"), &instantiate<op_mesh>));
m.insert(std::make_pair(std::string("export_csv"), &instantiate<op_export_csv<>>));
m.insert(std::make_pair(std::string("export_point_cloud"), &instantiate<op_export_csv<1>>));
m.insert(std::make_pair(std::string("local_sweep"), &instantiate<op_local_sweep<0>>));
m.insert(std::make_pair(std::string("local_move"), &instantiate<op_local_sweep<1>>));
m.insert(std::make_pair(std::string("repeat_slice"), &instantiate<op_repeat_slice>));
m.insert(std::make_pair(std::string("component_foreach"), &instantiate<op_component_foreach>));
m.insert(std::make_pair(std::string("normal_estimate"), &instantiate<op_normal_estimate>));
m.insert(std::make_pair(std::string("dimensionality_estimate"), &instantiate<op_dimensionality_estimate>));
m.insert(std::make_pair(std::string("segment"), &instantiate<op_segment>));
m.insert(std::make_pair(std::string("keep_neighbours"), &instantiate<op_keep_neighbours>));
m.insert(std::make_pair(std::string("count_neighbours"), &instantiate<op_count_neighbours>));
m.insert(std::make_pair(std::string("free"), &instantiate<op_free>));
m.insert(std::make_pair(std::string("assert"), &instantiate<op_assert>));
m.insert(std::make_pair(std::string("set"), &instantiate<op_set>));
initialized = true;
}
return m;
}
std::ostream& operator<<(std::ostream& a, const std::vector<std::string>& b) {
a << "{";
bool first = true;
for (auto& v : b) {
if (!first) {
a << ",";
}
first = false;
a << v;
}
a << "}";
return a;
}
std::ostream& operator<<(std::ostream& a, const boost::variant<double, int, std::string, std::vector<std::string>>& b) {
// @todo must be a better way
switch (b.which()) {
case 0:
a << boost::get<double>(b);
break;
case 1:
a << boost::get<int>(b);
break;
case 2:
a << boost::get<std::string>(b);
break;
case 3:
a << boost::get<std::vector<std::string>>(b);
break;
default:
break;
}
return a;
}
std::ostream& operator<<(std::ostream& a, const function_call_type& b) {
a << boost::fusion::at_c<0>(b) << "(";
bool first = true;
for (auto& arg : boost::fusion::at_c<1>(b)) {
if (!first) {
a << ",";
}
first = false;
if (arg.has_keyword()) {
a << *boost::fusion::at_c<0>(arg) << "=";
}
a << boost::fusion::at_c<1>(arg);
}
a << ")";
return a;
}
std::ostream& operator<<(std::ostream& a, const statement_type& b) {
std::string assignee = boost::fusion::at_c<0>(b);
if (assignee.size()) {
a << assignee << " = ";
}
a << boost::fusion::at_c<1>(b);
return a;
}
class log_visitor {
private:
json_logger::meta_value v_;
public:
const json_logger::meta_value& value() const { return v_; }
typedef void result_type;
void operator()(const boost::blank&) {
v_ = std::string("void");
}
void operator()(int v) {
v_ = (long) v;
}
void operator()(double v) {
v_ = v;
}
void operator()(const std::string& v) {
if (v.at(0) == '"') {
v_ = v.substr(1, v.size() - 2);
}
else {
v_ = v;
}
}
void operator()(const function_arg_value_type& v) {
v.apply_visitor(*this);
}
void operator()(filtered_files_t const v) { }
void operator()(geometry_collection_t* const v) { }
void operator()(abstract_voxel_storage* const v) { }
};
template <typename T, typename U, typename Ts, typename Us>
class assigner {
Ts& ts_;
Us& us_;
public:
assigner(Ts& ts, Us& us)
: ts_(ts)
, us_(us)
{}
void operator()(const T& t) {
ts_.push_back(t);
}
void operator()(const U& u) {
// @nb overwritten on purpose
us_[u.name()] = u;
}
};
void map_arguments(const std::vector<std::string> parameters, scope_map& context, const std::vector<function_arg_type>& args) {
// @todo revisit the similarities with voxel_operation_runner
bool has_keyword = false;
std::map<std::string, function_arg_value_type> function_values;
size_t position = 0;
for (auto& arg : args) {
if (has_keyword && !arg.has_keyword()) {
throw std::runtime_error("Keyword argument supplied after positional argument");
}
has_keyword = arg.has_keyword();
if (has_keyword) {
if (function_values.find(*arg.keyword()) != function_values.end()) {
throw std::runtime_error("Value for " + *arg.keyword() + "supplied multiple times");
}
function_values[*arg.keyword()] = arg.value();
}
else {
if (position >= parameters.size()) {
throw std::runtime_error("Too many positional arguments");
}
function_values[parameters[position]] = arg.value();
}
position++;
}
for (auto& pname : parameters) {
if (function_values.find(pname) == function_values.end()) {
throw std::runtime_error("No value supplied for " + pname);
}
symbol_value_assignment_visitor v(&context[pname], "*", context);
function_values[pname].apply_visitor(v);
}
for (auto& arg : args) {
if (arg.has_keyword() && context.find(*arg.keyword()) == context.end()) {
throw std::runtime_error("Keyword argument " + *arg.keyword() + " unused");
}
}
}
void execute_statements(scope_map& context, const std::map<std::string, function_def_type>& functions, const std::vector<statement_type>& statements, bool silent, std::unique_ptr<application_progress>& ap, std::function<void(float)> apfn, std::string prefix="", bool no_vox=false, bool with_pointcloud=false) {
size_t n = 0;
long HAS_VOXELS = boost::mpl::distance<
boost::mpl::begin<symbol_value::types>::type,
boost::mpl::find<symbol_value::types, abstract_voxel_storage*>::type
>::type::value;
for (auto it = statements.begin(); it != statements.end(); ++it) {
auto& st = *it;
// const bool is_last = it == --statements.end();
auto statement_string = boost::lexical_cast<std::string>(st);
json_logger::message(json_logger::LOG_NOTICE, "executing {statement}", {
{"statement", {{"text", statement_string}}}
});
auto fnit = functions.find(st.call().name());
if (fnit != functions.end()) {
auto context_copy = context;
// @todo needs to be set or does (inherited?) assignment op take care of this?
context_copy.functions = context.functions;
map_arguments(
fnit->second.args(),
context_copy,
st.call().args()
);
std::unique_ptr<application_progress> ap_unused;
execute_statements(
context_copy,
functions,
fnit->second.statements(),
silent,
ap_unused,
[](float) {},
boost::lexical_cast<std::string>(n) + ".",
no_vox,
with_pointcloud
);
if (context_copy.find(fnit->second.result_identifier()) == context_copy.end()) {
throw scope_map::not_in_scope("Undefined variable " + fnit->second.result_identifier());
}
context[st.assignee()] = context_copy[fnit->second.result_identifier()];
} else {
voxel_operation_runner op(st);
op.application_progress_callback = apfn;
op.silent = silent;
context[st.assignee()] = op.run(st, context);
}
if (context[st.assignee()].which() == HAS_VOXELS) {
auto voxels = boost::get<abstract_voxel_storage*>(context[st.assignee()]);
if (!no_vox) {
voxel_writer w;
w.SetVoxels(voxels);
w.Write(prefix + boost::lexical_cast<std::string>(n) + ".vox");
}
if (with_pointcloud) {
export_csv_or_obj(1, (regular_voxel_storage*)voxels, st.assignee() + ".obj");
}
/*
// @nb automatic meshing of last operand output no longer supported
if (with_mesh && is_last) {
std::string ofn = boost::lexical_cast<std::string>(n) + ".obj";
std::ofstream ofs(ofn.c_str());
((regular_voxel_storage*)voxels)->obj_export(ofs);
}
*/
if (dynamic_cast<abstract_chunked_voxel_storage*>(voxels)) {
json_logger::message(json_logger::LOG_NOTICE, "storing {value} in {variable}", {
{"variable", {{"name", st.assignee()}}},
{"value", dump_info(voxels)},
});
}
}
if (ap) {
ap->finished();
}
n++;
}
if (statements.size()) {
log_visitor v;
context[statements.back().assignee()].apply_visitor(v);
std::string msg = prefix.empty()
? std::string("script finished with {variable}")
: std::string("function finished with {variable}");
json_logger::message(json_logger::LOG_NOTICE, msg, {
{"variable", {{"value", v.value()}}},
});
}
else {
json_logger::message(json_logger::LOG_WARNING, "no operations in input", {});
}
}
scope_map run(const std::vector<statement_or_function_def>& statements_and_functions, double size, size_t threads, size_t chunk_size, bool with_mesh, bool with_progress_on_cout, bool no_vox, bool with_pointcloud) {
scope_map context;
std::unique_ptr<progress_bar> pb;
std::unique_ptr<application_progress> ap;
std::function<void(float)> pfn = [&pb](float f) { (*pb)(f); };
std::function<void(float)> apfn = [&ap](float f) { (*ap)(f); };
if (with_progress_on_cout) {
pb = std::make_unique<progress_bar>(std::cout, progress_bar::DOTS);
}
else {
pb = std::make_unique<progress_bar>(std::cout);
}
// split into statements and function calls
std::vector<statement_type> statements;
std::map<std::string, function_def_type> functions;
assigner<
statement_type,
function_def_type,
decltype(statements),
decltype(functions)> vis(
statements,
functions
);
std::for_each(statements_and_functions.begin(), statements_and_functions.end(), boost::apply_visitor(vis));
std::vector<float> estimates(statements.size(), 1.f);
ap = std::make_unique<application_progress>(estimates, pfn);
function_arg_value_type threads_ = (int)threads;
context["THREADS"] = threads_;
function_arg_value_type chunk_size_ = (int)chunk_size;
context["CHUNKSIZE"] = chunk_size_;
function_arg_value_type vsize_ = size;
context["VOXELSIZE"] = vsize_;
// @todo not very clean, but pass to context so that op_component_foreach can invoke them.
// functions should probably just be stored in scope_map as first-class citizens?
context.functions = &functions;
execute_statements(context, functions, statements, with_progress_on_cout, ap, apfn, "", no_vox, with_pointcloud);
return context;
}
void invoke_function_by_name(scope_map& context, const std::string& function_name) {
auto& functions = *context.functions;
auto fnit = functions.find(function_name);
if (fnit == functions.end()) {
throw scope_map::not_in_scope("Undefined function " + function_name);
}
std::random_device rng;
std::uniform_int_distribution<short> index_dist('A', 'Z');
std::string exec_id;
exec_id.reserve(8);
for (int i = 0; i < 7; ++i) {
exec_id.push_back((std::string::value_type)index_dist(rng));
}
exec_id.push_back('.');
std::unique_ptr<application_progress> ap_unused;
execute_statements(
context,
functions,
fnit->second.statements(),
true, /* @todo silent */
ap_unused,
[](float) {},
exec_id
);
}
size_t padding_ = 32;
size_t get_padding() {
return padding_;
}
void set_padding(size_t v) {
padding_ = v;
}