9 template <
typename entity_t,
typename value_t,
size_t DIM>
17 m_iwidth(1 / m_width) {}
19 template <
typename entity_t,
typename value_t,
size_t DIM>
23 m_vmin(center - size.get() * 0.5),
24 m_vmax(center + size.get() * 0.5),
27 m_iwidth(1 / m_width) {}
29 template <
typename entity_t,
typename value_t,
size_t DIM>
35 for (
size_t i = 0;
i < boxes.size();
i++) {
36 if (
i < boxes.size() - 1) {
38 boxes[
i]->setSkip(boxes[
i + 1]);
42 boxes[
i]->setSkip(
nullptr);
57 template <
typename entity_t,
typename value_t,
size_t DIM>
67 vertex_array_type::Constant(std::numeric_limits<value_type>::lowest()));
69 vertex_array_type::Constant(std::numeric_limits<value_type>::max()));
71 for (
size_t i = 0;
i < boxes.size();
i++) {
73 vmax = vmax.max(boxes[
i]->max().
array());
82 template <
typename entity_t,
typename value_t,
size_t DIM>
89 std::vector<const self_t*> box_ptrs;
90 box_ptrs.reserve(boxes.size());
91 std::transform(boxes.begin(), boxes.end(), std::back_inserter(box_ptrs),
92 [](
const auto*
box) {
return box; });
93 return wrap(box_ptrs, envelope);
96 template <
typename entity_t,
typename value_t,
size_t DIM>
103 std::vector<const self_t*> box_ptrs;
104 box_ptrs.reserve(boxes.size());
105 std::transform(boxes.begin(), boxes.end(), std::back_inserter(box_ptrs),
106 [](
auto&
box) {
return &
box; });
107 return wrap(box_ptrs, envelope);
110 template <
typename entity_t,
typename value_t,
size_t DIM>
114 return t.minCoeff() >= 0 && t.maxCoeff() < 1;
117 template <
typename entity_t,
typename value_t,
size_t DIM>
143 return tmin < tmax && tmax > 0.0;
146 template <
typename entity_t,
typename value_t,
size_t DIM>
147 template <
size_t s
ides>
150 const auto& normals = fr.
normals();
163 for (
size_t i = 0;
i < sides + 1;
i++) {
168 p_vtx = (normal.array() < 0).
template cast<value_type>() * fr_vmin +
169 (normal.array() >= 0).
template cast<value_type>() * fr_vmax;
174 if (p_vtx.dot(normal) < 0) {
185 template <
typename entity_t,
typename value_t,
size_t DIM>
191 if (m_right_child !=
nullptr) {
192 m_right_child->setSkip(skip);
196 template <
typename entity_t,
typename value_t,
size_t DIM>
202 template <
typename entity_t,
typename value_t,
size_t DIM>
208 template <
typename entity_t,
typename value_t,
size_t DIM>
210 return m_entity !=
nullptr;
213 template <
typename entity_t,
typename value_t,
size_t DIM>
219 template <
typename entity_t,
typename value_t,
size_t DIM>
221 const entity_t* entity) {
225 template <
typename entity_t,
typename value_t,
size_t DIM>
231 template <
typename entity_t,
typename value_t,
size_t DIM>
237 template <
typename entity_t,
typename value_t,
size_t DIM>
243 template <
typename entity_t,
typename value_t,
size_t DIM>
245 std::ostream&
os)
const {
248 for (
size_t i = 0;
i < DIM;
i++) {
256 for (
size_t i = 0;
i < DIM;
i++) {
265 for (
size_t i = 0;
i < DIM;
i++) {
277 template <
typename entity_t,
typename value_t,
size_t DIM>
278 template <
size_t D, std::enable_if_t<D == 3,
int>>
287 std::array<VertexType, 8>
vertices({{
288 {m_vmin.x(), m_vmin.y(), m_vmin.z()},
289 {m_vmin.x(), m_vmax.y(), m_vmin.z()},
290 {m_vmax.x(), m_vmax.y(), m_vmin.z()},
291 {m_vmax.x(), m_vmin.y(), m_vmin.z()},
292 {m_vmin.x(), m_vmin.y(), m_vmax.z()},
293 {m_vmin.x(), m_vmax.y(), m_vmax.z()},
294 {m_vmax.x(), m_vmax.y(), m_vmax.z()},
295 {m_vmax.x(), m_vmin.y(), m_vmax.z()},
301 for (
size_t i = 1;
i < 8;
i++) {
303 vmin = vmin.cwiseMin(vtx);
304 vmax = vmax.cwiseMax(vtx);
310 template <
typename entity_t,
typename value_t,
size_t DIM>
311 template <
size_t D, std::enable_if_t<D == 2,
int>>
316 const transform_type& trf)
const {
320 std::array<VertexType, 4>
vertices({{{m_vmin.x(), m_vmin.y()},
321 {m_vmin.x(), m_vmax.y()},
322 {m_vmax.x(), m_vmax.y()},
323 {m_vmax.x(), m_vmin.y()}}});
328 for (
size_t i = 1;
i < 4;
i++) {
330 vmin = vmin.cwiseMin(vtx);
331 vmax = vmax.cwiseMax(vtx);
337 template <
typename entity_t,
typename value_t,
size_t DIM>
340 std::tie(m_vmin, m_vmax) = transformVertices(trf);
343 template <
typename entity_t,
typename value_t,
size_t DIM>
348 std::tie(vmin, vmax) = transformVertices(trf);
349 return self_t(m_entity, vmin, vmax);
352 template <
typename entity_t,
typename value_t,
size_t DIM>
353 template <
size_t D, std::enable_if_t<D == 3,
int>>
357 static_assert(DIM == 3,
"PLY output only supported in 3D");
364 helper.
face(std::vector<VertexType>({trf *
a, trf *
b, trf *
c, trf * d}),
368 write({vmin.x(), vmin.y(), vmin.z()}, {vmin.x(), vmax.y(), vmin.z()},
369 {vmin.x(), vmax.y(), vmax.z()}, {vmin.x(), vmin.y(), vmax.z()});
371 write({vmax.x(), vmin.y(), vmin.z()}, {vmax.x(), vmax.y(), vmin.z()},
372 {vmax.x(), vmax.y(), vmax.z()}, {vmax.x(), vmin.y(), vmax.z()});
374 write({vmin.x(), vmin.y(), vmin.z()}, {vmax.x(), vmin.y(), vmin.z()},
375 {vmax.x(), vmin.y(), vmax.z()}, {vmin.x(), vmin.y(), vmax.z()});
377 write({vmin.x(), vmax.y(), vmin.z()}, {vmax.x(), vmax.y(), vmin.z()},
378 {vmax.x(), vmax.y(), vmax.z()}, {vmin.x(), vmax.y(), vmax.z()});
380 write({vmin.x(), vmin.y(), vmin.z()}, {vmax.x(), vmin.y(), vmin.z()},
381 {vmax.x(), vmax.y(), vmin.z()}, {vmin.x(), vmax.y(), vmin.z()});
383 write({vmin.x(), vmin.y(), vmax.z()}, {vmax.x(), vmin.y(), vmax.z()},
384 {vmax.x(), vmax.y(), vmax.z()}, {vmin.x(), vmax.y(), vmax.z()});
387 template <
typename entity_t,
typename value_t,
size_t DIM>
388 template <
size_t D, std::enable_if_t<D == 2,
int>>
392 static_assert(DIM == 2,
"SVG is only supported in 2D");
396 using transform_t = Eigen::Transform<value_t, DIM, Eigen::Affine>;
398 transform_t trf = transform_t::Identity();
400 trf = trf * Eigen::Scaling(
VertexType(1, -1));
407 os <<
"cx=\"" << p.x() <<
"\" cy=\"" << p.y() <<
"\" r=\"" <<
r <<
"\"";
408 os <<
" fill=\"" << color <<
"\"";
415 VertexType center = trf * center_ - size * 0.5;
418 os <<
"x=\"" << center.x() <<
"\" y=\"" << center.y() <<
"\" ";
419 os <<
"width=\"" << size.x() <<
"\" height=\"" << size.y() <<
"\"";
420 os <<
" fill=\"" << color <<
"\"";
427 os <<
"<text dominant-baseline=\"middle\" text-anchor=\"middle\" ";
428 os <<
"fill=\"" << color <<
"\" font-size=\"" <<
size <<
"\" ";
429 os <<
"x=\"" << center.x() <<
"\" y=\"" << center.y() <<
"\">";
430 os << text <<
"</text>\n";
433 draw_rect(
m_center, m_width, fillcolor);
434 draw_point(m_vmin,
"black", 2);
435 draw_point(m_vmax,
"black", 2);
436 draw_text(
m_center, label,
"white", 10);
441 template <
typename box_t>
445 const std::vector<box_t*>& lprims,
size_t depth) {
448 assert(lprims.size() > 0);
449 if (lprims.size() == 1) {
451 return lprims.front();
454 if (depth >= max_depth) {
456 auto bb = std::make_unique<box_t>(lprims, envelope);
458 return store.back().get();
461 std::array<std::vector<box_t*>, 8> octants;
464 std::tie(vmin, vmax) = box_t::wrap(lprims);
467 for (
auto*
box : lprims) {
469 if (ctr.x() < 0 && ctr.y() < 0 && ctr.z() < 0) {
470 octants[0].push_back(
box);
473 if (ctr.x() > 0 && ctr.y() < 0 && ctr.z() < 0) {
474 octants[1].push_back(
box);
477 if (ctr.x() < 0 && ctr.y() > 0 && ctr.z() < 0) {
478 octants[2].push_back(
box);
481 if (ctr.x() > 0 && ctr.y() > 0 && ctr.z() < 0) {
482 octants[3].push_back(
box);
486 if (ctr.x() < 0 && ctr.y() < 0 && ctr.z() > 0) {
487 octants[4].push_back(
box);
490 if (ctr.x() > 0 && ctr.y() < 0 && ctr.z() > 0) {
491 octants[5].push_back(
box);
494 if (ctr.x() < 0 && ctr.y() > 0 && ctr.z() > 0) {
495 octants[6].push_back(
box);
498 if (ctr.x() > 0 && ctr.y() > 0 && ctr.z() > 0) {
499 octants[7].push_back(
box);
504 octants[0].push_back(
box);
507 std::vector<box_t*> sub_octs;
508 for (
const auto& sub_prims : octants) {
509 if (sub_prims.size() <= 8) {
510 if (sub_prims.empty()) {
512 }
else if (sub_prims.size() == 1) {
513 sub_octs.push_back(sub_prims.front());
515 store.push_back(std::make_unique<box_t>(sub_prims, envelope));
516 sub_octs.push_back(
store.back().get());
525 if (sub_octs.size() == 1) {
526 return sub_octs.front();
529 auto bb = std::make_unique<box_t>(sub_octs, envelope);
531 return store.back().get();
534 template <
typename box_t>
536 const std::vector<box_t*>& prims,
size_t max_depth,
538 static_assert(
box_t::dim == 3,
"Octree can only be created in 3D");
548 template <
typename T,
typename U,
size_t V>