FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
voxel_map.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
7#include <kernel/geometry/voxel_map.hpp>
8
9// includes, system
10#include <cstring>
11
12// includes, thirdparty
13#ifdef FEAT_HAVE_ZLIB
14#include <zlib.h>
15#endif // FEAT_HAVE_ZLIB
16
17namespace FEAT
18{
19 namespace Geometry
20 {
22 typedef std::int64_t i64;
24 typedef std::uint64_t u64;
25
27 _create_stage(0),
28 _bbox_min(),
29 _bbox_max(),
30 _num_points(),
31 _stride_line(0u),
32 _stride_plane(0u),
33 _num_lines(0u),
34 _num_planes(0u),
35 _voxel_map(),
36 _out_of_bounds_value(true),
37 _coverage(0u)
38 {
39 }
40
42 _create_stage(other._create_stage),
43 _bbox_min(other._bbox_min),
44 _bbox_max(other._bbox_max),
45 _num_points(other._num_points),
46 _stride_line(other._stride_line),
47 _stride_plane(other._stride_plane),
48 _num_lines(other._num_lines),
49 _num_planes(other._num_planes),
50 _voxel_map(std::forward<std::vector<char>>(other._voxel_map)),
51 _out_of_bounds_value(other._out_of_bounds_value),
52 _coverage(other._coverage)
53 {
54 }
55
57 VoxelMap& VoxelMap::operator=(VoxelMap&& other)
58 {
59 if(this == &other)
60 return *this;
61
62 _create_stage = other._create_stage;
63 _bbox_min = other._bbox_min;
64 _bbox_max = other._bbox_max;
65 _num_points = other._num_points;
66 _stride_line = other._stride_line;
67 _stride_plane = other._stride_plane;
68 _num_lines = other._num_lines;
69 _num_planes = other._num_planes;
70 _voxel_map = std::forward<std::vector<char>>(other._voxel_map);
71 _out_of_bounds_value = other._out_of_bounds_value;
72 _coverage = other._coverage;
73
74 return *this;
75 }
76
79 {
80 }
81
82 void VoxelMap::set_bounding_box_2d(Real x_min, Real x_max, Real y_min, Real y_max)
83 {
84 XASSERTM(x_min < x_max, "invalid X dimensions for voxel map bounding box");
85 XASSERTM(y_min < y_max, "invalid Y dimensions for voxel map bounding box");
86 _bbox_min[0u] = i64(x_min * Real(unit_size));
87 _bbox_max[0u] = i64(x_max * Real(unit_size));
88 _bbox_min[1u] = i64(y_min * Real(unit_size));
89 _bbox_max[1u] = i64(y_max * Real(unit_size));
90 _bbox_min[2u] = _bbox_max[2u] = i64(0);
91 XASSERTM(_bbox_min[0u] + 1 < _bbox_max[0u], "bounding box is too small in X dimension");
92 XASSERTM(_bbox_min[1u] + 1 < _bbox_max[1u], "bounding box is too small in Y dimension");
93 }
94
95 void VoxelMap::set_bounding_box_3d(Real x_min, Real x_max, Real y_min, Real y_max, Real z_min, Real z_max)
96 {
97 XASSERTM(x_min < x_max, "invalid X dimensions for voxel map bounding box");
98 XASSERTM(y_min < y_max, "invalid Y dimensions for voxel map bounding box");
99 XASSERTM(z_min < z_max, "invalid Z dimensions for voxel map bounding box");
100 _bbox_min[0u] = i64(x_min * Real(unit_size));
101 _bbox_max[0u] = i64(x_max * Real(unit_size));
102 _bbox_min[1u] = i64(y_min * Real(unit_size));
103 _bbox_max[1u] = i64(y_max * Real(unit_size));
104 _bbox_min[2u] = i64(z_min * Real(unit_size));
105 _bbox_max[2u] = i64(z_max * Real(unit_size));
106 XASSERTM(_bbox_min[0u] + 1 < _bbox_max[0u], "bounding box is too small in X dimension");
107 XASSERTM(_bbox_min[1u] + 1 < _bbox_max[1u], "bounding box is too small in Y dimension");
108 XASSERTM(_bbox_min[2u] + 1 < _bbox_max[2u], "bounding box is too small in Z dimension");
109 }
110
111 void VoxelMap::set_num_points(Index num_x, Index num_y, Index num_z)
112 {
113 XASSERT(num_x > 1ull);
114 XASSERT(num_y > 1ull);
115 XASSERT((num_z > 1ull) || (_bbox_min[2u] == _bbox_max[2u]));
116 _num_points[0] = num_x;
117 _num_points[1] = num_y;
118 _num_points[2] = (num_z > 0u ? num_z : u64(1));
119
120 // compute other quantities
125
126 // allocate voxel map
128 }
129
131 {
132 XASSERTM(max_res > 1E-12, "invalid resolution for voxel map");
134 u64(_bbox_max[0u] - _bbox_min[0u]) / u64(max_res * Real(unit_size)) + 1u,
135 u64(_bbox_max[1u] - _bbox_min[1u]) / u64(max_res * Real(unit_size)) + 1u,
136 u64(_bbox_max[2u] - _bbox_min[2u]) / u64(max_res * Real(unit_size)) + 1u);
137 }
138
139 void VoxelMap::compute_map_from_formula_2d(const Dist::Comm& comm, const String& formula, bool gather_to_all)
140 {
141#ifdef FEAT_HAVE_FPARSER
142 VoxelFormulaMasker<2> masker(formula);
143 this->_compute_voxel_map(comm, masker, gather_to_all);
144#else
145 XABORTM("FEAT is not build and lined against FPARSER third-party library!");
146 (void)comm;
147 (void)formula;
148 (void)gather_to_all;
149#endif
150 }
151
152 void VoxelMap::compute_map_from_formula_3d(const Dist::Comm& comm, const String& formula, bool gather_to_all)
153 {
154#ifdef FEAT_HAVE_FPARSER
155 VoxelFormulaMasker<3> masker(formula);
156 this->_compute_voxel_map(comm, masker, gather_to_all);
157#else
158 XABORTM("FEAT is not build and lined against FPARSER third-party library!");
159 (void)comm;
160 (void)formula;
161 (void)gather_to_all;
162#endif
163 }
164
165 void VoxelMap::compute_map_from_off_3d(const Dist::Comm& comm, const String& filename, bool invert, bool gather_to_all)
166 {
167 XASSERTM(_bbox_min[2u] < _bbox_max[2u], "CGAL OFF voxel map creation is only available in 3D");
168 std::stringstream sstr;
169 DistFileIO::read_common(sstr, filename, comm);
170 this->compute_map_from_off_3d(comm, sstr, invert, gather_to_all);
171 }
172
173 void VoxelMap::compute_map_from_off_3d(const Dist::Comm& comm, std::istream& is, bool invert, bool gather_to_all)
174 {
175 XASSERTM(_bbox_min[2u] < _bbox_max[2u], "CGAL OFF voxel map creation is only available in 3D");
176#ifdef FEAT_HAVE_CGAL
178 VoxelCGALMasker<double> cgal_masker(cgal_wrapper, invert);
179 this->_compute_voxel_map(comm, cgal_masker, gather_to_all);
180#else
181 XABORTM("FEAT is not build and linked against CGAL third-party library!");
182 (void)comm;
183 (void)is;
184 (void)invert;
185 (void)gather_to_all;
186#endif
187 }
188
189 VoxelMap::WriteResult VoxelMap::write(const String& filename, const u64 compress_block_size, const int compress_level) const
190 {
191 std::ofstream ofs(filename, std::ios_base::out | std::ios_base::binary);
192 if(!ofs.is_open() || !ofs.good())
193 throw FileNotCreated(filename);
194
195 return write(ofs, compress_block_size, compress_level);
196 }
197
198 VoxelMap::WriteResult VoxelMap::write(std::ostream& os, const u64 compress_block_size, const int compress_level) const
199 {
200 std::size_t written = 0u;
201
202 // set up file header
203 FileHeader header;
204 memset(&header, 0, sizeof(header));
205 header.magic_number = magic_number;
206 header.header_size = sizeof(header);
207 header.min_x = _bbox_min[0];
208 header.max_x = _bbox_max[0];
209 header.num_x = _num_points[0];
210 header.stride_line = _stride_line;
211 header.min_y = _bbox_min[1];
212 header.max_y = _bbox_max[1];
213 header.num_y = _num_points[1];
214 header.stride_plane = _stride_plane;
215 header.min_z = _bbox_min[2];
216 header.max_z = _bbox_max[2];
217 header.num_z = _num_points[2];
218 header.stride_volume = _stride_plane * _num_points[2];
219 header.coverage = _coverage;
220
221 // _voxel_map may contain trailing padding bytes
222 XASSERT(header.stride_volume <= _voxel_map.size());
223
224 // do we have to compress the map?
225#ifdef FEAT_HAVE_ZLIB
226 if(compress_block_size > 0u)
227 {
228 XASSERTM(compress_block_size < 1024u, "maximum compression block size is 1024 MB");
229
230 // compute number of planes per compression block
231 header.planes_per_block = (compress_block_size*1024ull*1024ull) / _stride_plane;
232 XASSERTM(header.planes_per_block > 0ull, "compression block size is to small to hold a single plane!");
233
234 header.num_blocks = (_num_planes + header.planes_per_block - 1u) / header.planes_per_block;
235 XASSERTM(header.num_blocks > 0ull, "invalid number of compression blocks!");
236 }
237#endif // FEAT_HAVE_ZLIB
238
239 // write header
240 os.write(reinterpret_cast<char*>(&header), sizeof(header));
241 written += sizeof(header);
242
243 // write uncompressed voxel map data
244 if(header.num_blocks == 0ull)
245 {
246 // write uncompressed voxel map data
247 os.write(reinterpret_cast<const char*>(_voxel_map.data()), std::streamsize(header.stride_volume));
248 return WriteResult(written + header.stride_volume);
249 }
250
251#ifdef FEAT_HAVE_ZLIB
252 // ensure that a compression block is not larger than 1 GB
253 XASSERTM(header.planes_per_block * _stride_plane < 1073741824ull, "voxel map plane compression block size is too big");
254
255 // compression buffers
256 std::vector<std::vector<char>> compress_buffers(header.num_blocks);
257
258 // compression block entries
259 std::vector<u64> compress_blocks(header.num_blocks, 0u);
260
261 // get compression level
262 const int cmp_lvl = Math::max(Math::min(compress_level, 9), 0);
263
264 // accumulated ZLIB results
265 int failures = 0;
266
267 // loop over all compression blocks
268 FEAT_PRAGMA_OMP(parallel for schedule(dynamic,1) reduction(+:failures))
269 for(u64 iblock = 0; iblock < header.num_blocks; ++iblock)
270 {
271 // compute first and last planes
272 u64 first_plane = iblock * header.planes_per_block;
273 u64 block_bytes = Math::min(header.planes_per_block, _num_planes - first_plane) * _stride_plane;
274
275 // estimate compression buffer size
276 u64 buffer_size = u64(::compressBound(uLong(block_bytes)));
277
278 // allocate compression buffer
279 compress_buffers[iblock].resize(buffer_size);
280
281 // compression buffer size
282 uLongf dest_size = uLongf(buffer_size);
283
284 // compress via ZLIB
285 int rtn = ::compress2(reinterpret_cast<Bytef*>(compress_buffers[iblock].data()), &dest_size,
286 reinterpret_cast<const Bytef*>(&_voxel_map[first_plane * _stride_plane]), uLongf(block_bytes), cmp_lvl);
287
288 // failure?
289 if(rtn != Z_OK)
290 {
291 ++failures;
292 continue;
293 }
294
295 // save compression buffer size
296 compress_blocks[iblock] = u64(dest_size);
297 }
298
299 // did any compression fail?
300 XASSERTM(failures == 0, "ZLIB failed to compress at least one plane block!");
301
302 // write the compression block headers
303 os.write(reinterpret_cast<const char*>(compress_blocks.data()), std::streamsize(compress_blocks.size() * sizeof(u64)));
304 written += compress_blocks.size() * sizeof(u64);
305
306 // okay, loop over all compression blocks and write them out
307 u64 compress_size = 0u;
308 for(std::size_t i(0); i < compress_buffers.size(); ++i)
309 {
310 os.write(reinterpret_cast<const char*>(compress_buffers[i].data()), std::streamsize(compress_blocks[i]));
311 written += compress_blocks[i];
312 compress_size += compress_blocks[i];
313 }
314
315 // return result
316 return WriteResult(written, compress_block_size, u64(compress_level), compress_buffers.size(), compress_size);
317#else
318 // we should never arrive in this #else case even if compiling without zlib
319 (void)compress_block_size;
320 (void)compress_level;
321 XABORTM("INTERNAL ERROR");
322#endif // FEAT_HAVE_ZLIB
323 }
324
326 {
327 BinaryStream stream;
328 DistFileIO::read_common(stream, filename, comm);
329 return read(stream, filename);
330 }
331
332 VoxelMap::ReadResult VoxelMap::read(std::istream& is, const String& filename)
333 {
334 // set up file header
335 FileHeader header;
336 memset(&header, 0, sizeof(header));
337
338 // try to read header
339 if(!is.read(reinterpret_cast<char*>(&header), sizeof(header)).good())
340 throw VoxelMapFileError(filename, "Failed to read voxel map header");
341
342 u64 bytes_read = sizeof(header);
343
344 // check magic number
345 if(header.magic_number != magic_number)
346 throw VoxelMapFileError(filename, "File does not seems to be a voxel map file");
347
348 // check header size
349 if(header.header_size != header_size)
350 throw VoxelMapFileError(filename, String("invalid header size; expected ") + stringify(header_size) + " but got " + stringify(header.header_size));
351
352 // perform some sanity checks
353 if((header.num_x > 0u) && (header.max_x <= header.min_x))
354 throw VoxelMapFileError(filename, String("invalid X dimensions: x_max <= x_min"));
355 if((header.num_y > 0u) && (header.max_y <= header.min_y))
356 throw VoxelMapFileError(filename, String("invalid Y dimensions: y_max <= y_min"));
357 if((header.num_z > 0u) && (header.max_z <= header.min_z))
358 throw VoxelMapFileError(filename, String("invalid Z dimensions: z_max <= z_min"));
359 if((header.num_x == 0u) && (header.max_x != header.min_x))
360 throw VoxelMapFileError(filename, String("invalid X dimensions: x_max != x_min for num_x = 0"));
361 if((header.num_y == 0u) && (header.max_y != header.min_y))
362 throw VoxelMapFileError(filename, String("invalid Y dimensions: y_max != y_min for num_y = 0"));
363 if((header.num_z == 0u) && (header.max_z != header.min_z))
364 throw VoxelMapFileError(filename, String("invalid Z dimensions: z_max != z_min for num_z = 0"));
365 if(header.stride_line*8u < header.num_x)
366 throw VoxelMapFileError(filename, String("invalid line stride: too small for a single X-line"));
367 if((header.stride_line & 0xF) != 0)
368 throw VoxelMapFileError(filename, String("invalid line stride: not a multiple of 16"));
369 if(header.stride_plane < header.num_y * header.stride_line)
370 throw VoxelMapFileError(filename, String("invalid plane stride: too small for a single XY-plane"));
371 if((header.stride_plane & 0xF) != 0)
372 throw VoxelMapFileError(filename, String("invalid plane stride: not a multiple of 16"));
373 if(header.stride_volume < header.num_z * header.stride_plane)
374 throw VoxelMapFileError(filename, String("invalid volume stride: too small for a single XYZ-volume"));
375 if((header.stride_volume & 0xF) != 0)
376 throw VoxelMapFileError(filename, String("invalid plane stride: not a multiple of 16"));
377
378 // ok, extract the vital data
379 _bbox_min[0u] = header.min_x;
380 _bbox_max[0u] = header.max_x;
381 _bbox_min[1u] = header.min_y;
382 _bbox_max[1u] = header.max_y;
383 _bbox_min[2u] = header.min_z;
384 _bbox_max[2u] = header.max_z;
385 _num_points[0u] = header.num_x;
386 _num_points[1u] = header.num_y;
387 _num_points[2u] = header.num_z;
388 _stride_line = header.stride_line;
389 _stride_plane = header.stride_plane;
392 _coverage = header.coverage;
393
394 // allocate voxel map
395 _voxel_map.resize(header.stride_volume);
396
397 // don't we have compression blocks?
398 if(header.num_blocks == 0u)
399 {
400 // just read the raw map data
401 if(!is.read(_voxel_map.data(), std::streamsize(header.stride_volume)).good())
402 throw VoxelMapFileError(filename, "Failed to read uncompressed voxel map buffer");
403 bytes_read += u64(is.gcount());
404 return ReadResult(bytes_read);
405 }
406
407 // we're dealing with a ZLIB compressed voxel map
408
409#ifdef FEAT_HAVE_ZLIB
410 // read compression blocks
411 std::vector<u64> blocks(header.num_blocks);
412 if(!is.read(reinterpret_cast<char*>(blocks.data()), std::streamsize(header.num_blocks * sizeof(u64))).good())
413 throw VoxelMapFileError(filename, "Failed to read voxel map compression blocks");
414 bytes_read += u64(is.gcount());
415
416 // compute buffer offsets and total buffer size
417 std::vector<std::size_t> buffer_offset(header.num_blocks + 1u);
418 buffer_offset[0u] = 0u;
419 for(std::size_t i(0); i < blocks.size(); ++i)
420 buffer_offset[i+1u] = buffer_offset[i] + blocks[i];
421
422 // read compression buffer
423 std::vector<char> compression_buffer(buffer_offset.back());
424 if(!is.read(compression_buffer.data(), std::streamsize(compression_buffer.size())).good())
425 throw VoxelMapFileError(filename, "Failed to read compressed voxel map buffer");
426 bytes_read += u64(is.gcount());
427
428 // accumulated ZLIB results
429 int failures = 0;
430
431 // loop over all compression blocks
432 FEAT_PRAGMA_OMP(parallel for schedule(dynamic,1) reduction(+:failures))
433 for(u64 iblock = 0; iblock < header.num_blocks; ++iblock)
434 {
435 u64 first_plane = iblock * header.planes_per_block;
436 u64 block_bytes = Math::min(header.planes_per_block, _num_planes - first_plane) * _stride_plane;
437 u64 compress_size = blocks[iblock];
438
439 // decompress
440 uLongf dest_size = uLongf(block_bytes);
441 int rtn = ::uncompress(reinterpret_cast<Bytef*>(&_voxel_map[first_plane * _stride_plane]), &dest_size,
442 reinterpret_cast<const Bytef*>(&compression_buffer[buffer_offset[iblock]]), uLong(compress_size));
443
444 if(rtn != Z_OK)
445 {
446 ++failures;
447 continue;
448 }
449 }
450
451 // did any compression fail?
452 XASSERTM(failures == 0, "ZLIB failed to decompress at least one plane block!");
453 return ReadResult(bytes_read, header.num_blocks, compression_buffer.size());
454#else
455 XABORTM("Cannot read compressed voxel map file, because FEAT was compiled without the ZLIB third-party library");
456#endif // FEAT_HAVE_ZLIB
457 }
458
459 void VoxelMap::export_to_bmp(const String& filename_prefix) const
460 {
461 XASSERTM(_num_points[2] <= 20000, "voxel map is too big for BMP export!");
462
463 for(u64 iplane(0); iplane < _num_planes; ++iplane)
464 {
465 String filename = filename_prefix + "." + stringify(iplane).pad_front(5, '0') + ".bmp";
466 std::ofstream ofs(filename, std::ios_base::binary);
467 _export_plane_to_bmp(ofs, iplane);
468 ofs.close();
469 }
470 }
471
472 void VoxelMap::export_plane_to_bmp(const String& filename, Real z_coord) const
473 {
474 std::ofstream ofs(filename, std::ios_base::binary);
475 export_plane_to_bmp(ofs, z_coord);
476 ofs.close();
477 }
478
479 void VoxelMap::export_plane_to_bmp(std::ostream& os, Real z_coord) const
480 {
482 }
483
484 void VoxelMap::render_to_bmp(const String& filename_prefix, Index width, Index height, Index depth) const
485 {
486 XASSERTM(depth <= 20000, "render width is too big for BMP export!");
487 for(i64 iplane(0); iplane < i64(depth); ++iplane)
488 {
489 String filename = filename_prefix + "." + stringify(iplane).pad_front(5, '0') + ".bmp";
490 std::ofstream ofs(filename, std::ios_base::binary);
491 i64 z_min = _bbox_min[2] + ( iplane * (_bbox_max[2] - _bbox_min[1])) / i64(depth);
492 i64 z_max = _bbox_min[2] + ((iplane+1) * (_bbox_max[2] - _bbox_min[1])) / i64(depth);
493 _render_plane_to_bmp(ofs, width, height, z_min, z_max);
494 ofs.close();
495 }
496 }
497
498 void VoxelMap::render_plane_to_bmp(const String& filename, Index width, Index height, Real z_min, Real z_max) const
499 {
500 std::ofstream ofs(filename, std::ios_base::binary);
501 render_plane_to_bmp(ofs, width, height, z_min, z_max);
502 ofs.close();
503 }
504
505 void VoxelMap::render_plane_to_bmp(std::ostream& os, Index width, Index height, Real z_min, Real z_max) const
506 {
507 _render_plane_to_bmp(os, width, height, i64(z_min * Real(unit_size)), i64(z_max * Real(unit_size)));
508 }
509
511 {
512 if(_num_points[2] > 0u)
513 return Real(_bbox_max[2] - _bbox_min[2]) * Real(_bbox_max[1] - _bbox_min[1]) * Real(_bbox_max[0] - _bbox_min[0]) / Math::cub(Real(unit_size));
514 if(_num_points[1] > 0u)
515 return Real(_bbox_max[1] - _bbox_min[1]) * Real(_bbox_max[0] - _bbox_min[0]) / Math::sqr(Real(unit_size));
516 if(_num_points[0] > 0u)
517 return Real(_bbox_max[0] - _bbox_min[0]) / Real(unit_size);
518 return Real(0);
519 }
520
522 {
523 if(_voxel_map.empty())
524 return u64(0);
525
526 u64 count(0u), n(_voxel_map.size()), nv(get_num_voxels());
527 FEAT_PRAGMA_OMP(parallel for reduction(+:count))
528 for(u64 i = 0u; i < n; ++i)
529 {
530 for(int k = 0; k < 8; ++k)
531 count += u64((_voxel_map[i] >> k) & 1);
532 }
533
534 // convert coverage relative to unit size
535 return (count*unit_size) / nv;
536 }
537
538 u64 VoxelMap::_map_coord_idx_nearest(i64 xyz, std::size_t sdim) const
539 {
540 // The input coordinate X is expected to be in the range of the bounding box [BMIN,BMAX], which is discretized
541 // by the NP voxel coordinates in the range {0, 1, ..., NP-1}, so applying standard linear transformation from
542 // the interval [BMIN, BMAX] to [0, NP-1] yields the formula
543 //
544 // (NP - 1) * (X - BMIN)
545 // X -> 0 + -----------------------
546 // BMAX - BMIN
547 //
548 // However, we are working with integer arithmetic here, so the fraction will be handled by integer division,
549 // which truncates all decimal places, so we have to add 1/2 to the result of the fraction to achieve a
550 // "round-to-nearest" voxel behavior. We exploit that 1/2 = (BMAX - BMIN) / (2*BMAX - 2*MIN) and so we get
551 //
552 // (NP - 1) * (X - BMIN) BMAX - BMIN
553 // X -> ----------------------- + -----------------
554 // BMAX - BMIN 2*BMAX - 2*BMIN
555 //
556 // which is equivalent to
557 //
558 // 2 * (NP - 1) * (X - BMIN) + BMAX - BMIN
559 // X -> -----------------------------------------
560 // 2 * BMAX - 2 * BMIN
561 //
562 i64 idx = (2*(i64(_num_points[sdim]) - 1) * (xyz - _bbox_min[sdim]) + _bbox_max[sdim] - _bbox_min[sdim]) / (2*_bbox_max[sdim] - 2*_bbox_min[sdim]);
563
564 // also check whether the index is out of bounds
565 return (0 <= idx) && (idx < i64(this->_num_points[sdim])) ? u64(idx) : ~u64(0);
566 }
567
568 i64 VoxelMap::_map_coord_idx_lower(i64 xyz, std::size_t sdim) const
569 {
570 // The input coordinate X is expected to be in the range of the bounding box [BMIN,BMAX], which is discretized
571 // by the NP voxel coordinates in the range {0, 1, ..., NP-1}, so applying standard linear transformation from
572 // the interval [BMIN, BMAX] to [0, NP-1] yields the formula
573 //
574 // (NP - 1) * (X - BMIN)
575 // X -> ----------------------- =: IDX
576 // BMAX - BMIN
577 //
578 // We are working with integer arithmetic here, so the fraction will be handled by integer division,
579 // which truncates all decimal places, which is what we want in this case to obtain the lower voxel index.
580 return ((i64(_num_points[sdim]) - 1) * (xyz - _bbox_min[sdim])) / (_bbox_max[sdim] - _bbox_min[sdim]);
581 }
582
583 i64 VoxelMap::_map_coord_idx_upper(i64 xyz, std::size_t sdim) const
584 {
585 // The input coordinate X is expected to be in the range of the bounding box [BMIN,BMAX], which is discretized
586 // by the NP voxel coordinates in the range {0, 1, ..., NP-1}, so applying standard linear transformation from
587 // the interval [BMIN, BMAX] to [0, NP-1] yields the formula
588 //
589 // (NP - 1) * (X - BMIN)
590 // X -> -----------------------
591 // BMAX - BMIN
592 //
593 // We are working with integer arithmetic here, so the fraction will be handled by integer division,
594 // which truncates all decimal places, so to round an integer fraction A/B up to the next integer, we have to
595 // add (B-1) to the numerator to obtain (A+B-1)/B, which in our case results in the formula
596 //
597 // (NP - 1) * (X - BMIN) + BMAX - BMIN - 1
598 // X -> ----------------------------------------- =: IDX
599 // BMAX - BMIN
600 //
601 return ((i64(_num_points[sdim]) - 1) * (xyz - _bbox_min[sdim]) + _bbox_max[sdim] - _bbox_min[sdim] - 1) / (_bbox_max[sdim] - _bbox_min[sdim]);
602 }
603
604 Real VoxelMap::_calc_sample_rate(i64 xyz, i64 idx, std::size_t sdim) const
605 {
606 // the sample rate is defined as sr(xyz,idx) := 1 - |xyz - V(idx)| / |V(idx+1) - V(idx)| where
607 //
608 // V(idx+1) - V(idx) = (V(n) - V(0)) / (n-1)
609 // and
610 // V(idx) = V(0) + idx*(V(idx+1) - V(idx)) = V(0) + idx*(V(n) - V(0)) / (n-1)
611 //
612 // so in total we have
613 //
614 // sr(xyz,idx) = 1 - |xyz - V(idx)| / |V(idx+1) - V(idx)|
615 // = 1 - |(xyz - V(idx)) / (V(idx+1) - V(idx))|
616 // = 1 - |(xyz - V(0) - idx*(V(n) - V(0)) / (n-1)) / ((V(n) - V(0)) / (n-1))|
617 // = 1 - |((xyz - V(0))*(n-1) - idx*(V(n) - V(0))) / (V(n) - V(0))|
618 // = 1 - |(xyz - V(0))*(n-1) / (V(n) - V(0)) - idx)|
619 //
620 return Real(1) - Math::abs(Real(_num_points[sdim] - 1) * Real(xyz - _bbox_min[sdim]) / Real(_bbox_max[sdim] - _bbox_min[sdim]) - Real(idx));
621 }
622
623 bool VoxelMap::_check_point(i64 xidx, i64 yidx, i64 zidx) const
624 {
625 if((xidx < i64(0)) || (i64(_num_points[0]) <= xidx))
627 if((yidx < i64(0)) || (i64(_num_points[1]) <= yidx))
629 if((zidx < i64(0)) || (i64(_num_points[2]) <= zidx))
631 return ((this->_voxel_map[u64(zidx)*this->_stride_plane + u64(yidx)*this->_stride_line + u64(xidx>>3)] >> (xidx & 7)) & 1) != 0;
632 }
633
634 bool VoxelMap::_check_point_nearest(const std::array<i64, 1>& p) const
635 {
636 u64 xidx = _map_coord_idx_nearest(p[0], 0u);
637 if(xidx == ~u64(0))
639 return (this->_voxel_map[(xidx >> 3)] >> (xidx & 0x7)) & 0x1;
640 }
641
642 bool VoxelMap::_check_point_nearest(const std::array<i64, 2>& p) const
643 {
644 u64 xidx = _map_coord_idx_nearest(p[0], 0u);
645 u64 yidx = _map_coord_idx_nearest(p[1], 1u);
646 if((xidx|yidx) == ~u64(0))
648 return (this->_voxel_map[yidx * this->_stride_line + (xidx >> 3)] >> (xidx & 0x7)) & 0x1;
649 }
650
651 bool VoxelMap::_check_point_nearest(const std::array<i64, 3>& p) const
652 {
653 u64 xidx = _map_coord_idx_nearest(p[0], 0u);
654 u64 yidx = _map_coord_idx_nearest(p[1], 1u);
655 u64 zidx = _map_coord_idx_nearest(p[2], 2u);
656 if((xidx|yidx|zidx) == ~u64(0))
658 u64 line = zidx * this->_num_points[1] + yidx;
659 return (this->_voxel_map[line * this->_stride_line + (xidx >> 3)] >> (xidx & 0x7)) & 0x1;
660 }
661
662 bool VoxelMap::_check_box(const std::array<u64, 1>& box_min, const std::array<u64, 1>& box_max) const
663 {
664 for(u64 xidx(box_min[0]); xidx <= box_max[0]; ++xidx)
665 {
666 if((this->_voxel_map[(xidx >> 3)] >> (xidx & 0x7)) & 0x1)
667 return true;
668 }
669 return false;
670 }
671
672 bool VoxelMap::_check_box(const std::array<u64, 2>& box_min, const std::array<u64, 2>& box_max) const
673 {
674 for(u64 yidx(box_min[1]); yidx <= box_max[1]; ++yidx)
675 {
676 for(u64 xidx(box_min[0]); xidx <= box_max[0]; ++xidx)
677 {
678 if((this->_voxel_map[yidx * this->_stride_line + (xidx >> 3)] >> (xidx & 0x7)) & 0x1)
679 return true;
680 }
681 }
682 return false;
683 }
684
685 bool VoxelMap::_check_box(const std::array<u64, 3>& box_min, const std::array<u64, 3>& box_max) const
686 {
687 for(u64 zidx(box_min[2]); zidx <= box_max[2]; ++zidx)
688 {
689 for(u64 yidx(box_min[1]); yidx <= box_max[1]; ++yidx)
690 {
691 u64 line = zidx * this->_num_points[1] + yidx;
692 for(u64 xidx(box_min[0]); xidx <= box_max[0]; ++xidx)
693 {
694 if((this->_voxel_map[line * this->_stride_line + (xidx >> 3)] >> (xidx & 0x7)) & 0x1)
695 return true;
696 }
697 }
698 }
699 return false;
700 }
701
702 Real VoxelMap::_sample_point_1d_x(i64 px, i64 xidx, i64 yidx, i64 zidx) const
703 {
704 Real v0 = Real(_check_point(xidx, yidx, zidx));
705 Real v1 = Real(_check_point(xidx+1, yidx, zidx));
706 return v0 + (v1 - v0) * _calc_sample_rate(px, xidx, 0u);
707 }
708
709 Real VoxelMap::_sample_point_1d_y(i64 py, i64 xidx, i64 yidx, i64 zidx) const
710 {
711 Real v0 = Real(_check_point(xidx, yidx, zidx));
712 Real v1 = Real(_check_point(xidx, yidx+1, zidx));
713 return v0 + (v1 - v0) * _calc_sample_rate(py, yidx, 1u);
714 }
715
716 Real VoxelMap::_sample_point_1d_z(i64 pz, i64 xidx, i64 yidx, i64 zidx) const
717 {
718 Real v0 = Real(_check_point(xidx, yidx, zidx));
719 Real v1 = Real(_check_point(xidx, yidx, zidx+1));
720 return v0 + (v1 - v0) * _calc_sample_rate(pz, zidx, 2u);
721 }
722
723 Real VoxelMap::_sample_point_2d(i64 px, i64 py, i64 xidx, i64 yidx, i64 zidx) const
724 {
725 Real v0 = _sample_point_1d_x(px, xidx, yidx, zidx);
726 Real v1 = _sample_point_1d_x(px, xidx, yidx+1, zidx);
727 return v0 + (v1 - v0) * _calc_sample_rate(py, yidx, 1u);
728 }
729
730 Real VoxelMap::_sample_point_3d(i64 px, i64 py, i64 pz, i64 xidx, i64 yidx, i64 zidx) const
731 {
732 Real v0 = _sample_point_2d(px, py, xidx, yidx, zidx);
733 Real v1 = _sample_point_2d(px, py, xidx, yidx, zidx+1);
734 return v0 + (v1 - v0) * _calc_sample_rate(pz, zidx, 2u);
735 }
736
737 Real VoxelMap::_sample_point(const std::array<i64, 1>& p) const
738 {
739 return _sample_point_1d_x(p[0], _map_coord_idx_lower(p[0], 0u));
740 }
741
742 Real VoxelMap::_sample_point(const std::array<i64, 2>& p) const
743 {
744 return _sample_point_2d(p[0], p[1], _map_coord_idx_lower(p[0], 0u), _map_coord_idx_lower(p[1], 1u));
745 }
746
747 Real VoxelMap::_sample_point(const std::array<i64, 3>& p) const
748 {
749 return _sample_point_3d(p[0], p[1], p[2], _map_coord_idx_lower(p[0], 0u),
750 _map_coord_idx_lower(p[1], 1u), _map_coord_idx_lower(p[2], 2u));
751 }
752
753 Real VoxelMap::_sample_box_1d(i64 px0, i64 px1, i64 xidx0, i64 xidx1, i64 yidx, i64 zidx) const
754 {
755 ASSERT(xidx0 < xidx1);
756
757 // is the box completely left or completely right of the voxel map domain?
758 if(xidx1 < i64(0))
760 if(i64(_num_points[0]) <= xidx0)
762
763 // sample the two voxel map entries left and right of px0
764 Real v00 = _check_point(xidx0, yidx, zidx);
765 Real v01 = _check_point(xidx0+1, yidx, zidx);
766
767 // sample the two voxel map entries left and right of px1
768 Real v10 = _check_point(xidx1-1, yidx, zidx);
769 Real v11 = _check_point(xidx1, yidx, zidx);
770
771 // compute sample rate of px0 w.r.t. left voxel V(xidx0) and of px1 w.r.t. right voxel V(xidx1)
772 Real s0 = _calc_sample_rate(px0, xidx0, 0);
773 Real s1 = _calc_sample_rate(px1, xidx1, 0);
774
775 // compute interpolated values v0 and v1 of px0 and px1, respectively, from their neighboring voxel values
776 Real v0 = v01 + (v00 - v01) * s0;
777 Real v1 = v10 + (v11 - v10) * s1;
778
779 // if the entire box [px0, px1] is between two neighboring voxels, we can integrate the interpolated voxel
780 // values v0 and v1 at px0 and px1 via the trapezoidal rule by simply computing the average of the two values:
781 if(xidx1 == xidx0 + 1)
782 return Real(0.5) * (v0 + v1);
783
784 // in any other case, we have to integrate the voxel values over the interval [px0,px1], where v0 and v1
785 // denote the previously interpolated voxel values at the interval endpoints px0 and px1, respectively:
786 //
787 // v0 v1
788 // --V------+======V=============V=====...=====V=============V======+------V
789 // xidx0 px0 xidx0+1 ... ... xidx1-1 px1 xidx1
790 // |--s0--| |--s1--|
791 //
792 // we compute the integral in three steps and then compute the sum of those three sub-integrals:
793 // 1. compute the integral over the left-most sub-interval [px0,V(xidx0+1)] via the trapezoidal rule
794 // 2. compute the integral over the right-most sub-interval [V(xidx1-1),px1] via the trapezoidal rule
795 // 3. compute the integral over the inner intervals [V(idx0+1),V(idx1-1)] via the summed trapezoidal rule
796 //
797 // for the outer two sub-interval we have that the length of...
798 // ...the left-most sub-interval [px0,V(xidx0+1)] is given by the sample rate s0 of px0 w.r.t. V(idx0)
799 // ...the right-most sub-interval [V(xidx1-1),px1] is given by the sample rate s1 of px1 w.r.t. V(idx1)
800 //
801 // therefore, the integral of
804 // ...the inner interval equals to V(idx0+1)/2 + V(idx1-1)/2 + sum_{xidx0+1<=k<=idx1-1} V(k)
805 //
806 // which results in
807 //
808 // s0*(v0+V(idx0+1))/2 + V(idx0+1)/2 + sum_{xidx0+1 < k < idx1-1} V(k) + V(idx1-1)/2 + s1*(v1+V(idx1-1))/2
809 // \___________________/ \___________________________________________________________/ \___________________/
810 // left-most interval inner intervals right-most interval
811 //
812 // which in the case where idx0+1 < idx1-1 is equal to
813 //
814 // (s0*v0)/2 + (s0+1)*V(idx0+1)/2 + sum_{xidx0+1<k<idx1-1} V(k) + (s1+1)*V(idx1-1)/2 + (s1*v1)/2
815 //
816 // there is one final special case where idx0+1 = idx1-1, i.e. there is just a single voxel between the
817 // two points px0 and px1 -- or in other words: there are no inner intervals -- and therefore we just have
818 // to add up the two integrals over the outer most sub-intervals, so for this case we get
819 //
820 // s0*(v0+V(idx0+1))/2 + s1*(v1+V(idx1-1))/2 = (s0*v0 + s1*v1 + (s0+s1)*V(idx0+1))/2
821
822 // add scaled endpoint values for trapezoidal rule of first and last intervals of length s0 and s1
823 Real result = Real(0.5) * (s0*v0 + s1*v1);
824
825 // add first and last inner voxel and scale properly to compensate for the following summed trapezoidal rule
826 if(xidx1 == xidx0 + 2)
827 {
828 // no inner X-intervals
829 result += Real(0.5) * (s0 + s1) * _check_point(xidx0+1, yidx, zidx);
830 }
831 else
832 {
833 // at least one inner X-interval exists
834 result += Real(0.5) * (s0 + Real(1)) * _check_point(xidx0+1, yidx, zidx);
835 result += Real(0.5) * (s1 + Real(1)) * _check_point(xidx1-1, yidx, zidx);
836 }
837
838 // now add all inner voxel values for summed trapezoidal rule
839 for(i64 i = xidx0+2; i < xidx1-1; ++i)
840 result += _check_point(i, yidx, zidx);
841
842 // finally divide by the total integration interval width
843 return result / (Real(xidx1 - xidx0 - 2) + s0 + s1);
844 }
845
846 Real VoxelMap::_sample_box_2d(i64 px0, i64 px1, i64 py0, i64 py1, i64 xidx0, i64 xidx1, i64 yidx0, i64 yidx1, i64 zidx) const
847 {
848 ASSERT(yidx0 < yidx1);
849
850 // is the box completely below or completely above of the voxel map domain?
851 if(yidx1 < i64(0))
853 if(i64(_num_points[1]) <= yidx0)
855
856 // sample the two X-parallel voxel map lines below and above of py0
857 Real v00 = _sample_box_1d(px0, px1, xidx0, xidx1, yidx0, zidx);
858 Real v01 = _sample_box_1d(px0, px1, xidx0, xidx1, yidx0+1, zidx);
859
860 // sample the two X-parallel voxel map lines below and above of py1
861 Real v10 = _sample_box_1d(px0, px1, xidx0, xidx1, yidx1-1, zidx);
862 Real v11 = _sample_box_1d(px0, px1, xidx0, xidx1, yidx1, zidx);
863
864 // compute sample rate of py0 w.r.t. lower voxel V(yidx0) and of py1 w.r.t. upper voxel V(yidx1)
865 Real s0 = _calc_sample_rate(py0, yidx0, 1);
866 Real s1 = _calc_sample_rate(py1, yidx1, 1);
867
868 // compute interpolated values v0 and v1 of py0 and py1, respectively, from their neighboring voxel values
869 Real v0 = v01 + (v00 - v01) * s0;
870 Real v1 = v10 + (v11 - v10) * s1;
871
872 // if the entire box [py0, py1] is between two neighboring voxel lines, we can integrate the interpolated voxel
873 // line values v0 and v1 at py0 and py1 via the trapezoidal rule by simply computing the average of the two values:
874 if(yidx1 == yidx0 + 1)
875 return Real(0.5) * (v0 + v1);
876
877 // see the comments inside _sample_box_1d() for more details about what is happening here
878
879 // add scaled endpoint values for trapezoidal rule of first and last intervals of length s0 and s1
880 Real result = Real(0.5) * (s0*v0 + s1*v1);
881
882 // add first and last inner voxel rows and scale properly to compensate for the following summed trapezoidal rule
883 if(yidx1 == yidx0 + 2)
884 {
885 // no inner Y-intervals
886 result += Real(0.5) * (s0 + s1) * _sample_box_1d(px0, px1, xidx0, xidx1, yidx0+1, zidx);
887 }
888 else
889 {
890 // at least one inner Y-interval exists
891 result += Real(0.5) * (s0 + Real(1)) * _sample_box_1d(px0, px1, xidx0, xidx1, yidx0+1, zidx);
892 result += Real(0.5) * (s1 + Real(1)) * _sample_box_1d(px0, px1, xidx0, xidx1, yidx1-1, zidx);
893 }
894
895 // now add all inner X-parallel voxel line values for summed trapezoidal rule
896 for(i64 i = yidx0+2; i < yidx1-1; ++i)
897 result += _sample_box_1d(px0, px1, xidx0, xidx1, i, zidx);
898
899 // finally divide by the total integration interval height
900 return result / (Real(yidx1 - yidx0 - 2) + s0 +s1);
901 }
902
903 Real VoxelMap::_sample_box_3d(i64 px0, i64 px1, i64 py0, i64 py1, i64 pz0, i64 pz1, i64 xidx0, i64 xidx1, i64 yidx0, i64 yidx1, i64 zidx0, i64 zidx1) const
904 {
905 ASSERT(zidx0 < zidx1);
906
907 // is the box completely below or completely above of the voxel map domain?
908 if(zidx1 < i64(0))
910 if(i64(_num_points[2]) <= zidx0)
912
913 // sample the two XY-parallel voxel map planes in front and behind of pz0
914 Real v00 = _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx0);
915 Real v01 = _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx0+1);
916
917 // sample the two XY-parallel voxel map planes in front and behind of pz1
918 Real v10 = _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx1-1);
919 Real v11 = _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx1);
920
921 // compute sample rate of pz0 w.r.t. back voxel V(zidx0) and of pz1 w.r.t. front voxel V(zidx1)
922 Real s0 = _calc_sample_rate(pz0, zidx0, 2);
923 Real s1 = _calc_sample_rate(pz1, zidx1, 2);
924
925 // compute interpolated values v0 and v1 of pz0 and pz1, respectively, from their neighboring voxel values
926 Real v0 = v01 + (v00 - v01) * s0;
927 Real v1 = v10 + (v11 - v10) * s1;
928
929 // if the entire box [pz0, pz1] is between two neighboring voxel planes, we can integrate the interpolated voxel
930 // plane values v0 and v1 at pz0 and pz1 via the trapezoidal rule by simply computing the average of the two values:
931 if(zidx1 == zidx0 + 1)
932 return Real(0.5) * (v0 + v1);
933
934 // see the comments inside _sample_box_1d() for more details about what is happening here
935
936 // add scaled endpoint values for trapezoidal rule of first and last intervals of length s0 and s1
937 Real result = Real(0.5) * (s0*v0 + s1*v1);
938
939 // add first and last inner voxel planes and scale properly to compensate for the following summed trapezoidal rule
940 if(zidx1 == zidx0 + 2)
941 {
942 // no inner Z-intervals
943 result += Real(0.5) * (s0 + s1) * _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx0+1);
944 }
945 else
946 {
947 // at least one inner Z-interval exists
948 result += Real(0.5) * (s0 + Real(1)) * _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx0+1);
949 result += Real(0.5) * (s1 + Real(1)) * _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, zidx1-1);
950 }
951
952 // now add all inner XY-parallel voxel plane values for summed trapezoidal rule
953 for(i64 i = zidx0+2; i < zidx1-1; ++i)
954 result += _sample_box_2d(px0, px1, py0, py1, xidx0, xidx1, yidx0, yidx1, i);
955
956 // finally divide by the total integration interval depth
957 return result / (Real(zidx1 - zidx0 - 2) + s0 +s1);
958 }
959
960 Real VoxelMap::_sample_box(const std::array<i64, 1>& box_min, const std::array<i64, 1>& box_max) const
961 {
962 if(_num_points[0] > 1u)
963 return _sample_box_1d(box_min[0], box_max[0],
964 _map_coord_idx_lower(box_min[0], 0u), _map_coord_idx_upper(box_max[0], 0u));
965 else
966 return Real(0);
967 }
968
969 Real VoxelMap::_sample_box(const std::array<i64, 2>& box_min, const std::array<i64, 2>& box_max) const
970 {
971 if(_num_points[1] > 1u)
972 return _sample_box_2d(box_min[0], box_max[0], box_min[1], box_max[1],
973 _map_coord_idx_lower(box_min[0], 0u), _map_coord_idx_upper(box_max[0], 0u),
974 _map_coord_idx_lower(box_min[1], 1u), _map_coord_idx_upper(box_max[1], 1u));
975 else if(_num_points[0] > 1u)
976 return _sample_box_1d(box_min[0], box_max[0],
977 _map_coord_idx_lower(box_min[0], 0u), _map_coord_idx_upper(box_max[0], 0u));
978 else
979 return Real(0);
980 }
981
982 Real VoxelMap::_sample_box(const std::array<i64, 3>& box_min, const std::array<i64, 3>& box_max) const
983 {
984 if(_num_points[2] > 1u)
985 return _sample_box_3d(box_min[0], box_max[0], box_min[1], box_max[1], box_min[2], box_max[2],
986 _map_coord_idx_lower(box_min[0], 0u), _map_coord_idx_upper(box_max[0], 0u),
987 _map_coord_idx_lower(box_min[1], 1u), _map_coord_idx_upper(box_max[1], 1u),
988 _map_coord_idx_lower(box_min[2], 2u), _map_coord_idx_upper(box_max[2], 2u));
989 else if(_num_points[1] > 1u)
990 return _sample_box_2d(box_min[0], box_max[0], box_min[1], box_max[1],
991 _map_coord_idx_lower(box_min[0], 0u), _map_coord_idx_upper(box_max[0], 0u),
992 _map_coord_idx_lower(box_min[1], 1u), _map_coord_idx_upper(box_max[1], 1u));
993 else if(_num_points[0] > 1u)
994 return _sample_box_1d(box_min[0], box_max[0],
995 _map_coord_idx_lower(box_min[0], 0u), _map_coord_idx_upper(box_max[0], 0u));
996 else
997 return Real(0);
998 }
999
1000 void VoxelMap::_compress_voxel_map_line(const std::vector<int>& mask, const u64 line)
1001 {
1002 ASSERTM(line < this->_num_lines, "invalid voxel map line");
1003 const u64 n = u64(mask.size());
1004 const u64 off = line * this->_stride_line;
1005 for(u64 i(0); i < n; ++i)
1006 {
1007 _voxel_map[off + (i >> 3)] |= char(char(mask[i] != 0) << char(i & 0x7));
1008 }
1009 }
1010
1011 void VoxelMap::_export_plane_to_bmp(std::ostream& os, u64 plane) const
1012 {
1013 // maximum BMP resolutions seems to be around 30K, but we won't export more than 20K,
1014 // which results in a file size of roughly 50 MB per image
1015 XASSERTM(_num_points[0] <= 20000, "voxel map is too big for BMP export!");
1016 XASSERTM(_num_points[1] <= 20000, "voxel map is too big for BMP export!");
1017 XASSERTM(plane < _num_points[2], "invalid plane index");
1018
1019 typedef std::uint16_t u16;
1020 typedef std::uint32_t u32;
1021
1022 // get dimensions
1023 u32 width = u32(this->_num_points[0]);
1024 u32 height = u32(this->_num_points[1]);
1025
1026 // compute stride (multiple of 4 bytes)
1027 u32 stride = ((width + 31u) & ~31u) >> 3;
1028
1029 // BITMAPFILEHEADER: 14 bytes
1030 // write magic
1031 u16 magic = 0x4D42;
1032 os.write((char*)&magic, 2u);
1033 // write file size
1034 u32 filesize = 62u + height * stride;
1035 os.write((char*)&filesize, 4u);
1036 // write reserved bytes
1037 u32 zeros = 0u;
1038 os.write((char*)&zeros, 4u);
1039 // wite bit offset
1040 u32 offbits = 54u + 2u*4u;
1041 os.write((char*)&offbits, 4u);
1042
1043 // BITMAPINFOHEADER: 40 bytes
1044 // write header size
1045 u32 bisize = 40u;
1046 os.write((char*)&bisize, 4u);
1047 // write width and height
1048 os.write((char*)&width, 4u);
1049 os.write((char*)&height, 4u);
1050 // write plane count
1051 u16 planes = 1;
1052 os.write((char*)&planes, 2u);
1053 // write bit count
1054 u16 bitcount = 1;
1055 os.write((char*)&bitcount, 2u);
1056 // write compression
1057 os.write((char*)&zeros, 4u);
1058 // write image size
1059 u32 size_image = height * stride;
1060 os.write((char*)&size_image, 4u);
1061 // write pixels per meter (X and Y)
1062 u32 px_per_m = 3780;
1063 os.write((char*)&px_per_m, 4u);
1064 os.write((char*)&px_per_m, 4u);
1065 // number of colors
1066 u32 clr_used = 2u;
1067 os.write((char*)&clr_used, 4u);
1068 // number of important colors (WTF?)
1069 os.write((char*)&zeros, 4u);
1070
1071 // write black and white colors
1072 u32 clr_black = 0u, clr_white = ~u32(0);
1073 os.write((char*)&clr_black, 4u);
1074 os.write((char*)&clr_white, 4u);
1075
1076 std::vector<char> linebuffer(stride);
1077 char* oline = linebuffer.data();
1078
1079 // okay, let's loop over all domain nodes
1080 for(u32 i(0); i < height; ++i)
1081 {
1082 const char* iline = &_voxel_map.data()[plane*_stride_plane + i*_stride_line];
1083 for(u32 j(0); j < stride; ++j)
1084 for(int k(0); k < 8; ++k)
1085 (oline[j] <<= 1) |= ((iline[j] >> k) & 1); // all hail to bitshift!
1086 os.write(oline, stride);
1087 }
1088 }
1089
1090 void VoxelMap::_render_plane_to_bmp(std::ostream& os, Index width, Index height, i64 box_min_z, i64 box_max_z) const
1091 {
1092 // maximum BMP resolutions seems to be around 30K, but we won't export more than 20K,
1093 // which results in a file size of roughly 50 MB per image
1094 XASSERTM(width <= 20000, "render width is too big for BMP export!");
1095 XASSERTM(height <= 20000, "render width is too big for BMP export!");
1096 XASSERTM(box_min_z < box_max_z, "invalid depth for voxel map render!");
1097 XASSERTM(box_min_z >= _bbox_min[2], "invalid depth for voxel map render!");
1098 XASSERTM(box_max_z <= _bbox_max[2], "invalid depth for voxel map render!");
1099
1100 typedef std::uint8_t u8;
1101 typedef std::uint16_t u16;
1102 typedef std::uint32_t u32;
1103
1104 // get dimensions
1105 u32 width2 = u32(width);
1106 u32 height2 = u32(height);
1107
1108 // compute stride (multiple of 4 bytes)
1109 u32 stride = ((width2*8 + 31u) & ~31u) >> 3;
1110
1111 // BITMAPFILEHEADER: 14 bytes
1112 // write magic
1113 u16 magic = 0x4D42;
1114 os.write((char*)&magic, 2u);
1115 // write file size
1116 u32 filesize = 54u + height2 * stride + 256u*4u;
1117 os.write((char*)&filesize, 4u);
1118 // write reserved bytes
1119 u32 zeros = 0u;
1120 os.write((char*)&zeros, 4u);
1121 // wite bit offset
1122 u32 offbits = 54u + 256u*4u;
1123 os.write((char*)&offbits, 4u);
1124
1125 // BITMAPINFOHEADER: 40 bytes
1126 // write header size
1127 u32 bisize = 40u;
1128 os.write((char*)&bisize, 4u);
1129 // write width and height
1130 os.write((char*)&width2, 4u);
1131 os.write((char*)&height2, 4u);
1132 // write plane count
1133 u16 planes = 1;
1134 os.write((char*)&planes, 2u);
1135 // write bit count
1136 u16 bitcount = 8;
1137 os.write((char*)&bitcount, 2u);
1138 // write compression
1139 os.write((char*)&zeros, 4u);
1140 // write image size
1141 u32 size_image = height2 * stride;
1142 os.write((char*)&size_image, 4u);
1143 // write pixels per meter (X and Y)
1144 u32 px_per_m = 3780;
1145 os.write((char*)&px_per_m, 4u);
1146 os.write((char*)&px_per_m, 4u);
1147 // number of colors
1148 u32 clr_used = 256u;
1149 os.write((char*)&clr_used, 4u);
1150 // number of important colors (WTF?)
1151 os.write((char*)&zeros, 4u);
1152
1153 // write colors
1154 std::vector<u32> colors(256);
1155 for(u32 i = 0u; i < 256u; ++i)
1156 {
1157 colors[i] = i | (i << 8) | (i << 16);
1158 }
1159 os.write((char*)colors.data(), 256u*4u);
1160
1161 std::vector<u8> linebuffer(stride);
1162 u8* oline = linebuffer.data();
1163
1164 // set up bounding box
1165 std::array<i64, 3u> box_min, box_max;
1166 box_min[2u] = box_min_z;
1167 box_max[2u] = box_max_z;
1168
1169 const i64 ih = i64(height);
1170 const i64 jw = i64(width);
1171
1172 // okay, let's loop over all domain nodes
1173 for(i64 i(0); i < ih; ++i)
1174 {
1175 box_min[1u] = i64(_bbox_min[1] + (i * (_bbox_max[1] - _bbox_min[1])) / ih);
1176 box_max[1u] = i64(_bbox_min[1] + ((i+1) * (_bbox_max[1] - _bbox_min[1])) / ih);
1177 for(i64 j(0); j < jw; ++j)
1178 {
1179 box_min[0u] = i64(_bbox_min[0] + (j * (_bbox_max[0] - _bbox_min[0])) / jw);
1180 box_max[0u] = i64(_bbox_min[0] + ((j+1) * (_bbox_max[0] - _bbox_min[0])) / jw);
1181 Real w = _sample_box(box_min, box_max) * 255.0;
1182 oline[j] = (w <= 0.0 ? 0 : (w >= 255.0 ? 255 : u8(w)));
1183 }
1184 os.write((char*)oline, stride);
1185 }
1186 }
1187 } // namespace Geometry
1188} // namespace FEAT
#define ASSERT(expr)
Debug-Assertion macro definition.
Definition: assertion.hpp:229
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Binary Stream class.
Communicator class.
Definition: dist.hpp:1657
static void read_common(std::stringstream &stream, const String &filename, const Dist::Comm &comm, int root_rank=0)
Reads a common text file for all ranks.
File-Not-Created exception.
Definition: exception.hpp:223
Wrapper for the CGAL Library.
Definition: cgal.hpp:173
Voxel masker implementation for CGAL wrapper.
Definition: voxel_map.hpp:144
Voxel masker implementation for function parser expression tests.
Definition: voxel_map.hpp:304
helper class for read function result
Definition: voxel_map.hpp:502
helper class for write function result
Definition: voxel_map.hpp:462
Error class for VoxelMap related file errors.
Definition: voxel_map.hpp:37
i64 _map_coord_idx_lower(i64 xyz, std::size_t sdim) const
Maps a unit coordinate to a X-/Y-/Z-index of the lower voxel in the voxel map.
Definition: voxel_map.cpp:568
bool _check_point_nearest(const std::array< i64, 1 > &p) const
Checks the nearest voxel map entry for a given 1D point.
Definition: voxel_map.cpp:634
static constexpr u64 header_size
size of voxel map header in bytes
Definition: voxel_map.hpp:417
Real _sample_box_1d(i64 px0, i64 px1, i64 xidx0, i64 xidx1, i64 yidx=i64(0), i64 zidx=i64(0)) const
Helper function for _sample_box: samples a 1D edge in X line.
Definition: voxel_map.cpp:753
Real _calc_sample_rate(i64 xyz, i64 idx, std::size_t sdim) const
Helper function for _sample_voxel_map_Xd: compute the sample rate in a given dimension.
Definition: voxel_map.cpp:604
std::vector< char > _voxel_map
the actual voxel map; its size may be larger than necessary due to padding
Definition: voxel_map.hpp:545
void export_to_bmp(const String &filename_prefix) const
Exports the voxel map to a sequence of monochrome BMP image files.
Definition: voxel_map.cpp:459
bool _check_point(i64 xidx, i64 yidx, i64 zidx) const
Helper function for _sample_point: checks a single voxel point value.
Definition: voxel_map.cpp:623
WriteResult write(const String &filename, const u64 compress_block_size=128u, const int compress_level=9) const
Writes the voxel map into a file that can later be read in.
Definition: voxel_map.cpp:189
void render_to_bmp(const String &filename_prefix, Index width, Index height, Index depth) const
Renders the voxel map to a sequence of gray-scale BMP image files.
Definition: voxel_map.cpp:484
void _export_plane_to_bmp(std::ostream &os, u64 plane) const
Exports a single plane of the voxel map to a monochrome BMP image file.
Definition: voxel_map.cpp:1011
i64 _map_coord_idx_upper(i64 xyz, std::size_t sdim) const
Maps a unit coordinate to a X-/Y-/Z-index of the upper voxel in the voxel map.
Definition: voxel_map.cpp:583
Real _sample_point_1d_y(i64 py, i64 xidx, i64 yidx, i64 zidx=i64(0)) const
Helper function for _sample_point: samples a 1D edge in Y-parallel line.
Definition: voxel_map.cpp:709
Real _sample_box_2d(i64 px0, i64 px1, i64 py0, i64 py1, i64 xidx0, i64 xidx1, i64 yidx0, i64 yidx1, i64 zidx=i64(0)) const
Helper function for _sample_box: samples a 2D rectangle in XY plane.
Definition: voxel_map.cpp:846
std::array< u64, 3u > _num_points
number of points in each dimension
Definition: voxel_map.hpp:535
Real _sample_box_3d(i64 px0, i64 px1, i64 py0, i64 py1, i64 pz0, i64 pz1, i64 xidx0, i64 xidx1, i64 yidx0, i64 yidx1, i64 zidx0, i64 zidx1) const
Helper function for _sample_box: samples a 3D cuboid in XYZ volume.
Definition: voxel_map.cpp:903
void _compress_voxel_map_line(const std::vector< int > &mask, const u64 line)
Compresses a voxel map line into the voxel map.
Definition: voxel_map.cpp:1000
u64 _compute_domain_coverage() const
Computes the domain coverage of the voxel map.
Definition: voxel_map.cpp:521
void export_plane_to_bmp(const String &filename, Real z_coord) const
Exports a single plane of the voxel map to a monochrome BMP image file.
Definition: voxel_map.cpp:472
void _compute_voxel_map(const Dist::Comm &comm, VoxelMasker< CoordType, dim_ > &masker, bool gather_to_all)
Computes the voxel map for the domain.
Definition: voxel_map.hpp:1623
static constexpr u64 magic_number
voxel map magic number
Definition: voxel_map.hpp:414
Real get_bounding_box_volume() const
Returns the volume of the 1D/2D/3D voxel map bounding box.
Definition: voxel_map.cpp:510
u64 _num_planes
number of planes; 2D = 1; 3D = num_points[3]
Definition: voxel_map.hpp:543
std::uint64_t u64
unsigned 64-bit integer type
Definition: voxel_map.hpp:411
Real _sample_point_1d_z(i64 pz, i64 xidx, i64 yidx, i64 zidx) const
Helper function for _sample_point: samples a 1D edge in Z-parallel line.
Definition: voxel_map.cpp:716
Index get_num_voxels() const
Returns the total number of voxels in the voxel map.
Definition: voxel_map.hpp:658
void set_resolution(Real max_res)
Sets the resolution for the voxel map, i.e. the maximum voxel size in each dimension.
Definition: voxel_map.cpp:130
u64 _stride_line
stride of a single voxel line in X dimension
Definition: voxel_map.hpp:537
void compute_map_from_formula_3d(const Dist::Comm &comm, const String &formula, bool gather_to_all=true)
Creates the voxel map based on a 3D formula by utilizing MPI parallelization.
Definition: voxel_map.cpp:152
void set_num_points(Index num_x, Index num_y, Index num_z=0u)
Sets the number of points of the voxel map in each dimension.
Definition: voxel_map.cpp:111
virtual ~VoxelMap()
virtual destructor
Definition: voxel_map.cpp:78
void render_plane_to_bmp(const String &filename, Index width, Index height, Real z_min, Real z_max) const
Renders a plane range of voxel map to a single gray-scale BMP image file.
Definition: voxel_map.cpp:498
std::int64_t i64
signed 64-bit integer type
Definition: voxel_map.hpp:408
Real _sample_point_1d_x(i64 px, i64 xidx, i64 yidx=i64(0), i64 zidx=i64(0)) const
Helper function for _sample_point: samples a 1D edge in X-parallel line.
Definition: voxel_map.cpp:702
void compute_map_from_off_3d(const Dist::Comm &comm, const String &filename, bool invert, bool gather_to_all=true)
Creates the voxel map based on a 3D OFF model handled by CGAL by utilizing MPI parallelization.
Definition: voxel_map.cpp:165
u64 _stride_plane
stride of a single plane in XY dimensions
Definition: voxel_map.hpp:539
u64 _coverage
the coverage of the domain, relative to unit size
Definition: voxel_map.hpp:549
void set_bounding_box_2d(Real x_min, Real x_max, Real y_min, Real y_max)
Sets the bounding box for a 2D voxel map.
Definition: voxel_map.cpp:82
Real _sample_point_2d(i64 px, i64 py, i64 xidx, i64 yidx, i64 zidx=i64(0)) const
Helper function for _sample_point: samples a 2D square in XY-parallel plane.
Definition: voxel_map.cpp:723
static constexpr i64 unit_size
size of a real unit (meter) in our internal units (nanometers)
Definition: voxel_map.hpp:420
bool _out_of_bounds_value
the out-of-bounds-value for the voxel map
Definition: voxel_map.hpp:547
ReadResult read(const Dist::Comm &comm, const String &filename)
Reads a voxel map from a file.
Definition: voxel_map.cpp:325
std::array< i64, 3u > _bbox_min
bounding box dimensions of domain
Definition: voxel_map.hpp:533
bool _check_box(const std::array< u64, 1 > &box_min, const std::array< u64, 1 > &box_max) const
Checks whether a 1D box contains at least one active voxel.
Definition: voxel_map.cpp:662
void compute_map_from_formula_2d(const Dist::Comm &comm, const String &formula, bool gather_to_all=true)
Creates the voxel map based on a 2D formula by utilizing MPI parallelization.
Definition: voxel_map.cpp:139
Real _sample_point(const std::array< i64, 1 > &p) const
Samples the voxel map for a given 1D point by multi-linear interpolation.
Definition: voxel_map.cpp:737
int _create_stage
current creation stage
Definition: voxel_map.hpp:531
u64 _map_coord_idx_nearest(i64 xyz, std::size_t sdim) const
Maps a unit coordinate to a X-/Y-/Z-index of the closest voxel in the voxel map.
Definition: voxel_map.cpp:538
VoxelMap()
standard constructor
Definition: voxel_map.cpp:26
Real _sample_point_3d(i64 px, i64 py, i64 pz, i64 xidx, i64 yidx, i64 zidx) const
Helper function for _sample_point: samples a 3D cube in XYZ volume.
Definition: voxel_map.cpp:730
void set_bounding_box_3d(Real x_min, Real x_max, Real y_min, Real y_max, Real z_min, Real z_max)
Sets the bounding box for a 3D voxel map.
Definition: voxel_map.cpp:95
void _render_plane_to_bmp(std::ostream &os, Index width, Index height, i64 box_min_z, i64 box_max_z) const
Renders a plane range of voxel map to a single gray-scale BMP image file.
Definition: voxel_map.cpp:1090
static u64 calc_line_stride(u64 num_x)
ensure that we're not compiling on some freak architecture...
Definition: voxel_map.hpp:448
Real _sample_box(const std::array< i64, 1 > &box_min, const std::array< i64, 1 > &box_max) const
Samples a 1D box of the voxel map.
Definition: voxel_map.cpp:960
u64 _num_lines
number of lines; 2D = num_points[2]; 3D: = num_points[2] * num_points[3]
Definition: voxel_map.hpp:541
String class implementation.
Definition: string.hpp:47
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
Definition: string.hpp:393
std::uint64_t u64
unsigned 64-bit integer type
Definition: voxel_map.cpp:24
@ other
generic/other permutation strategy
std::int64_t i64
signed 64-bit integer type
Definition: voxel_map.cpp:22
T_ abs(T_ x)
Returns the absolute value.
Definition: math.hpp:275
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
T_ cub(T_ x)
Returns the cube of a value.
Definition: math.hpp:109
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
FEAT namespace.
Definition: adjactor.hpp:12
double Real
Real data type.
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.
header structure of voxel map file header
Definition: voxel_map.hpp:424