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 auto b0 = std::span(b).template subspan<0, 3>();
42 auto b1 = std::span(b).template subspan<3, 3>();
43 std::copy_n(std::next(xg.begin(), 3 * vertex_indices.front()), 3, b0.begin());
44 std::copy_n(std::next(xg.begin(), 3 * vertex_indices.front()), 3, b1.begin());
47 for (std::int32_t local_vertex : vertex_indices)
49 for (std::size_t j = 0; j < 3; ++j)
51 b0[j] = std::min(b0[j], xg[3 * local_vertex + j]);
52 b1[j] = std::max(b1[j], xg[3 * local_vertex + j]);
62template <std::
floating_po
int T>
63std::array<T, 6> compute_bbox_of_bboxes(
64 std::span<
const std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes)
67 std::array<T, 6> b = leaf_bboxes.front().first;
68 for (
auto [box, _] : leaf_bboxes)
70 std::transform(box.cbegin(), std::next(box.cbegin(), 3), b.cbegin(),
71 b.begin(), [](
auto a,
auto b) { return std::min(a, b); });
72 std::transform(std::next(box.cbegin(), 3), box.cend(),
73 std::next(b.cbegin(), 3), std::next(b.begin(), 3),
74 [](
auto a,
auto b) { return std::max(a, b); });
80template <std::
floating_po
int T>
81std::int32_t _build_from_leaf(
82 std::span<std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes,
83 std::vector<int>& bboxes, std::vector<T>& bbox_coordinates)
85 if (leaf_bboxes.size() == 1)
90 const auto [b, entity_index] = leaf_bboxes.front();
93 bboxes.push_back(entity_index);
94 bboxes.push_back(entity_index);
95 std::copy_n(b.begin(), 6, std::back_inserter(bbox_coordinates));
96 return bboxes.size() / 2 - 1;
101 std::array b = compute_bbox_of_bboxes<T>(leaf_bboxes);
104 std::array<T, 3> b_diff;
105 std::transform(std::next(b.cbegin(), 3), b.cend(), b.cbegin(),
106 b_diff.begin(), std::minus<T>());
107 const std::size_t axis = std::distance(
108 b_diff.begin(), std::max_element(b_diff.begin(), b_diff.end()));
110 auto middle = std::next(leaf_bboxes.begin(), leaf_bboxes.size() / 2);
111 std::nth_element(leaf_bboxes.begin(), middle, leaf_bboxes.end(),
112 [axis](
auto& p0,
auto& p1) ->
bool
114 auto x0 = p0.first[axis] + p0.first[3 + axis];
115 auto x1 = p1.first[axis] + p1.first[3 + axis];
120 assert(!leaf_bboxes.empty());
121 std::size_t part = leaf_bboxes.size() / 2;
123 = _build_from_leaf(leaf_bboxes.first(part), bboxes, bbox_coordinates);
124 std::int32_t bbox1 = _build_from_leaf(
125 leaf_bboxes.last(leaf_bboxes.size() - part), bboxes, bbox_coordinates);
128 bboxes.push_back(bbox0);
129 bboxes.push_back(bbox1);
130 std::copy_n(b.begin(), 6, std::back_inserter(bbox_coordinates));
131 return bboxes.size() / 2 - 1;
135template <std::
floating_po
int T>
136std::pair<std::vector<std::int32_t>, std::vector<T>> build_from_leaf(
137 std::vector<std::pair<std::array<T, 6>, std::int32_t>>& leaf_bboxes)
139 std::vector<std::int32_t> bboxes;
140 std::vector<T> bbox_coordinates;
141 impl_bb::_build_from_leaf<T>(leaf_bboxes, bboxes, bbox_coordinates);
142 return {std::move(bboxes), std::move(bbox_coordinates)};
145template <std::
floating_po
int T>
147_build_from_point(std::span<std::pair<std::array<T, 3>, std::int32_t>> points,
148 std::vector<std::int32_t>& bboxes,
149 std::vector<T>& bbox_coordinates)
152 if (points.size() == 1)
157 const std::int32_t c1 = points[0].second;
158 bboxes.push_back(c1);
159 bboxes.push_back(c1);
160 bbox_coordinates.insert(bbox_coordinates.end(), points[0].first.begin(),
161 points[0].first.end());
162 bbox_coordinates.insert(bbox_coordinates.end(), points[0].first.begin(),
163 points[0].first.end());
164 return bboxes.size() / 2 - 1;
168 auto [min, max] = std::ranges::minmax_element(points);
169 std::array<T, 3> b0 = min->first;
170 std::array<T, 3> b1 = max->first;
173 std::array<T, 3> b_diff;
174 std::ranges::transform(b1, b0, b_diff.begin(), std::minus<T>());
175 const std::size_t axis
176 = std::distance(b_diff.begin(), std::ranges::max_element(b_diff));
178 auto middle = std::next(points.begin(), points.size() / 2);
179 std::nth_element(points.begin(), middle, points.end(),
180 [axis](
auto& p0,
auto&& p1) ->
bool
181 { return p0.first[axis] < p1.first[axis]; });
184 assert(!points.empty());
185 std::size_t part = points.size() / 2;
187 = _build_from_point(points.first(part), bboxes, bbox_coordinates);
188 std::int32_t bbox1 = _build_from_point(points.last(points.size() - part),
189 bboxes, bbox_coordinates);
192 bboxes.push_back(bbox0);
193 bboxes.push_back(bbox1);
194 bbox_coordinates.insert(bbox_coordinates.end(), b0.begin(), b0.end());
195 bbox_coordinates.insert(bbox_coordinates.end(), b1.begin(), b1.end());
196 return bboxes.size() / 2 - 1;
203template <std::
floating_po
int T>
217 const std::int32_t num_entities = map->size_local() + map->num_ghosts();
218 std::vector<std::int32_t> r(num_entities);
219 std::iota(r.begin(), r.end(), 0);
234 std::optional<std::span<const std::int32_t>> entities
240 mesh.topology_mutable()->create_entities(
tdim);
244 std::span<const std::int32_t> entities_span;
245 std::optional<std::vector<std::int32_t>> local_range(std::nullopt);
247 entities_span = entities.value();
250 local_range.emplace(range(*mesh.topology_mutable(),
tdim));
251 entities_span = std::span<const std::int32_t>(local_range->data(),
252 local_range->size());
257 throw std::runtime_error(
258 "Dimension must be non-negative and less than or "
259 "equal to the topological dimension of the mesh");
262 mesh.topology_mutable()->create_connectivity(
tdim, mesh.topology()->dim());
265 std::vector<std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes;
266 leaf_bboxes.reserve(entities_span.size());
267 for (std::int32_t e : entities_span)
269 std::array<T, 6> b = impl_bb::compute_bbox_of_entity(mesh,
tdim, e);
270 std::transform(b.cbegin(), std::next(b.cbegin(), 3), b.begin(),
271 [padding](
auto x) { return x - padding; });
272 std::transform(std::next(b.begin(), 3), b.end(), std::next(b.begin(), 3),
273 [padding](
auto x) { return x + padding; });
274 leaf_bboxes.emplace_back(b, e);
278 if (!leaf_bboxes.empty())
279 std::tie(_bboxes, _bbox_coordinates)
280 = impl_bb::build_from_leaf(leaf_bboxes);
282 spdlog::info(
"Computed bounding box tree with {} nodes for {} entities",
295 impl_bb::_build_from_point(std::span(points), _bboxes, _bbox_coordinates);
298 spdlog::info(
"Computed bounding box tree with {} nodes for {} points.",
325 std::copy_n(_bbox_coordinates.data() + 6 * node, 6, x.begin());
342 constexpr T max_val = std::numeric_limits<T>::max();
343 std::array<T, 6> send_bbox
344 = {max_val, max_val, max_val, max_val, max_val, max_val};
346 std::copy_n(std::prev(_bbox_coordinates.end(), 6), 6, send_bbox.begin());
347 std::vector<T> recv_bbox(mpi_size * 6);
351 std::vector<std::pair<std::array<T, 6>, std::int32_t>> _recv_bbox(mpi_size);
352 for (std::size_t i = 0; i < _recv_bbox.size(); ++i)
354 std::copy_n(std::next(recv_bbox.begin(), 6 * i), 6,
355 _recv_bbox[i].first.begin());
356 _recv_bbox[i].second = i;
359 auto [global_bboxes, global_coords] = impl_bb::build_from_leaf(_recv_bbox);
361 std::move(global_coords));
363 spdlog::info(
"Computed global bounding box tree with {} boxes.",
370 std::int32_t
num_bboxes()
const {
return _bboxes.size() / 2; }
373 int tdim()
const {
return _tdim; }
379 tree_print(s, _bboxes.size() / 2 - 1);
390 std::array<std::int32_t, 2>
bbox(std::size_t node)
const
392 assert(2 * node + 1 < _bboxes.size());
393 return {_bboxes[2 * node], _bboxes[2 * node + 1]};
399 std::vector<T>&& bbox_coords)
400 : _tdim(0), _bboxes(bboxes), _bbox_coordinates(bbox_coords)
409 void tree_print(std::stringstream& s, std::int32_t i)
const
412 for (std::size_t j = 0; j < 2; ++j)
414 for (std::size_t k = 0; k < 3; ++k)
415 s << _bbox_coordinates[6 * i + j * 3 + k] <<
" ";
422 if (_bboxes[2 * i] == _bboxes[2 * i + 1])
423 s <<
"leaf containing entity (" << _bboxes[2 * i + 1] <<
")";
427 tree_print(s, _bboxes[2 * i]);
429 tree_print(s, _bboxes[2 * i + 1]);
435 std::vector<std::int32_t> _bboxes;
438 std::vector<T> _bbox_coordinates;