28template <std::
floating_po
int T>
29std::array<T, 6> compute_bbox_of_entity(
const mesh::Mesh<T>& mesh,
int dim,
33 std::span<const T> xg = mesh.geometry().x();
36 std::span<const std::int32_t> entity(&index, 1);
37 const std::vector<std::int32_t> vertex_indices
41 std::span<T, 3> b0(b.data(), 3);
42 std::span<T, 3> b1(b.data() + 3, 3);
44 std::copy_n(std::next(xg.begin(), 3 * vertex_indices.front()), 3, b0.begin());
45 std::copy_n(std::next(xg.begin(), 3 * vertex_indices.front()), 3, b1.begin());
48 for (std::int32_t local_vertex : vertex_indices)
50 for (std::size_t j = 0; j < 3; ++j)
52 b0[j] = std::min(b0[j], xg[3 * local_vertex + j]);
53 b1[j] = std::max(b1[j], xg[3 * local_vertex + j]);
63template <std::
floating_po
int T>
64std::array<T, 6> compute_bbox_of_bboxes(
65 std::span<
const std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes)
68 std::array<T, 6> b = leaf_bboxes.front().first;
69 for (
auto [box, _] : leaf_bboxes)
71 std::transform(box.cbegin(), std::next(box.cbegin(), 3), b.cbegin(),
72 b.begin(), [](
auto a,
auto b) { return std::min(a, b); });
73 std::transform(std::next(box.cbegin(), 3), box.cend(),
74 std::next(b.cbegin(), 3), std::next(b.begin(), 3),
75 [](
auto a,
auto b) { return std::max(a, b); });
81template <std::
floating_po
int T>
82std::int32_t _build_from_leaf(
83 std::span<std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes,
84 std::vector<int>& bboxes, std::vector<T>& bbox_coordinates)
86 if (leaf_bboxes.size() == 1)
91 const auto [b, entity_index] = leaf_bboxes.front();
94 bboxes.push_back(entity_index);
95 bboxes.push_back(entity_index);
96 std::copy_n(b.begin(), 6, std::back_inserter(bbox_coordinates));
97 return bboxes.size() / 2 - 1;
102 std::array b = compute_bbox_of_bboxes<T>(leaf_bboxes);
105 std::array<T, 3> b_diff;
106 std::transform(std::next(b.cbegin(), 3), b.cend(), b.cbegin(),
107 b_diff.begin(), std::minus<T>());
108 const std::size_t axis = std::distance(
109 b_diff.begin(), std::max_element(b_diff.begin(), b_diff.end()));
111 auto middle = std::next(leaf_bboxes.begin(), leaf_bboxes.size() / 2);
112 std::nth_element(leaf_bboxes.begin(), middle, leaf_bboxes.end(),
113 [axis](
auto& p0,
auto& p1) ->
bool
115 auto x0 = p0.first[axis] + p0.first[3 + axis];
116 auto x1 = p1.first[axis] + p1.first[3 + axis];
121 assert(!leaf_bboxes.empty());
122 std::size_t part = leaf_bboxes.size() / 2;
124 = _build_from_leaf(leaf_bboxes.first(part), bboxes, bbox_coordinates);
125 std::int32_t bbox1 = _build_from_leaf(
126 leaf_bboxes.last(leaf_bboxes.size() - part), bboxes, bbox_coordinates);
129 bboxes.push_back(bbox0);
130 bboxes.push_back(bbox1);
131 std::copy_n(b.begin(), 6, std::back_inserter(bbox_coordinates));
132 return bboxes.size() / 2 - 1;
136template <std::
floating_po
int T>
137std::pair<std::vector<std::int32_t>, std::vector<T>> build_from_leaf(
138 std::vector<std::pair<std::array<T, 6>, std::int32_t>>& leaf_bboxes)
140 std::vector<std::int32_t> bboxes;
141 std::vector<T> bbox_coordinates;
142 impl_bb::_build_from_leaf<T>(leaf_bboxes, bboxes, bbox_coordinates);
143 return {std::move(bboxes), std::move(bbox_coordinates)};
146template <std::
floating_po
int T>
148_build_from_point(std::span<std::pair<std::array<T, 3>, std::int32_t>> points,
149 std::vector<std::int32_t>& bboxes,
150 std::vector<T>& bbox_coordinates)
153 if (points.size() == 1)
158 const std::int32_t c1 = points[0].second;
159 bboxes.push_back(c1);
160 bboxes.push_back(c1);
161 bbox_coordinates.insert(bbox_coordinates.end(), points[0].first.begin(),
162 points[0].first.end());
163 bbox_coordinates.insert(bbox_coordinates.end(), points[0].first.begin(),
164 points[0].first.end());
165 return bboxes.size() / 2 - 1;
169 auto [min, max] = std::ranges::minmax_element(points);
170 std::array<T, 3> b0 = min->first;
171 std::array<T, 3> b1 = max->first;
174 std::array<T, 3> b_diff;
175 std::ranges::transform(b1, b0, b_diff.begin(), std::minus<T>());
176 const std::size_t axis
177 = std::distance(b_diff.begin(), std::ranges::max_element(b_diff));
179 auto middle = std::next(points.begin(), points.size() / 2);
180 std::nth_element(points.begin(), middle, points.end(),
181 [axis](
auto& p0,
auto&& p1) ->
bool
182 { return p0.first[axis] < p1.first[axis]; });
185 assert(!points.empty());
186 std::size_t part = points.size() / 2;
188 = _build_from_point(points.first(part), bboxes, bbox_coordinates);
189 std::int32_t bbox1 = _build_from_point(points.last(points.size() - part),
190 bboxes, bbox_coordinates);
193 bboxes.push_back(bbox0);
194 bboxes.push_back(bbox1);
195 bbox_coordinates.insert(bbox_coordinates.end(), b0.begin(), b0.end());
196 bbox_coordinates.insert(bbox_coordinates.end(), b1.begin(), b1.end());
197 return bboxes.size() / 2 - 1;
204template <std::
floating_po
int T>
218 const std::int32_t num_entities = map->size_local() + map->num_ghosts();
219 std::vector<std::int32_t> r(num_entities);
220 std::iota(r.begin(), r.end(), 0);
235 std::optional<std::span<const std::int32_t>> entities
241 mesh.topology_mutable()->create_entities(
tdim);
245 std::span<const std::int32_t> entities_span;
246 std::optional<std::vector<std::int32_t>> local_range(std::nullopt);
248 entities_span = entities.value();
251 local_range.emplace(range(*mesh.topology_mutable(),
tdim));
252 entities_span = std::span<const std::int32_t>(local_range->data(),
253 local_range->size());
258 throw std::runtime_error(
259 "Dimension must be non-negative and less than or "
260 "equal to the topological dimension of the mesh");
263 mesh.topology_mutable()->create_connectivity(
tdim, mesh.topology()->dim());
266 std::vector<std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes;
267 leaf_bboxes.reserve(entities_span.size());
268 for (std::int32_t e : entities_span)
270 std::array<T, 6> b = impl_bb::compute_bbox_of_entity(mesh,
tdim, e);
271 std::transform(b.cbegin(), std::next(b.cbegin(), 3), b.begin(),
272 [padding](
auto x) { return x - padding; });
273 std::transform(std::next(b.begin(), 3), b.end(), std::next(b.begin(), 3),
274 [padding](
auto x) { return x + padding; });
275 leaf_bboxes.emplace_back(b, e);
279 if (!leaf_bboxes.empty())
280 std::tie(_bboxes, _bbox_coordinates)
281 = impl_bb::build_from_leaf(leaf_bboxes);
283 spdlog::info(
"Computed bounding box tree with {} nodes for {} entities",
296 impl_bb::_build_from_point(std::span(points), _bboxes, _bbox_coordinates);
299 spdlog::info(
"Computed bounding box tree with {} nodes for {} points.",
326 std::copy_n(_bbox_coordinates.data() + 6 * node, 6, x.begin());
343 constexpr T max_val = std::numeric_limits<T>::max();
344 std::array<T, 6> send_bbox
345 = {max_val, max_val, max_val, max_val, max_val, max_val};
347 std::copy_n(std::prev(_bbox_coordinates.end(), 6), 6, send_bbox.begin());
348 std::vector<T> recv_bbox(mpi_size * 6);
352 std::vector<std::pair<std::array<T, 6>, std::int32_t>> _recv_bbox(mpi_size);
353 for (std::size_t i = 0; i < _recv_bbox.size(); ++i)
355 std::copy_n(std::next(recv_bbox.begin(), 6 * i), 6,
356 _recv_bbox[i].first.begin());
357 _recv_bbox[i].second = i;
360 auto [global_bboxes, global_coords] = impl_bb::build_from_leaf(_recv_bbox);
362 std::move(global_coords));
364 spdlog::info(
"Computed global bounding box tree with {} boxes.",
371 std::int32_t
num_bboxes()
const {
return _bboxes.size() / 2; }
374 int tdim()
const {
return _tdim; }
380 tree_print(s, _bboxes.size() / 2 - 1);
391 std::array<std::int32_t, 2>
bbox(std::size_t node)
const
393 assert(2 * node + 1 < _bboxes.size());
394 return {_bboxes[2 * node], _bboxes[2 * node + 1]};
400 std::vector<T>&& bbox_coords)
401 : _tdim(0), _bboxes(bboxes), _bbox_coordinates(bbox_coords)
410 void tree_print(std::stringstream& s, std::int32_t i)
const
413 for (std::size_t j = 0; j < 2; ++j)
415 for (std::size_t k = 0; k < 3; ++k)
416 s << _bbox_coordinates[6 * i + j * 3 + k] <<
" ";
423 if (_bboxes[2 * i] == _bboxes[2 * i + 1])
424 s <<
"leaf containing entity (" << _bboxes[2 * i + 1] <<
")";
428 tree_print(s, _bboxes[2 * i]);
430 tree_print(s, _bboxes[2 * i + 1]);
436 std::vector<std::int32_t> _bboxes;
439 std::vector<T> _bbox_coordinates;