DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
BoundingBoxTree.h
1// Copyright (C) 2013-2022 Chris N. Richardson, Anders Logg, Garth N. Wells,
2// Jørgen S. Dokken, Sarah Roggendorf
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include <algorithm>
11#include <array>
12#include <cassert>
13#include <cstdint>
14#include <dolfinx/mesh/utils.h>
15#include <mpi.h>
16#include <optional>
17#include <span>
18#include <string>
19#include <vector>
20
22{
23namespace impl_bb
24{
25//-----------------------------------------------------------------------------
26// Compute bounding box of mesh entity. The bounding box is defined by (lower
27// left corner, top right corner). Storage flattened row-major
28template <std::floating_point T>
29std::array<T, 6> compute_bbox_of_entity(const mesh::Mesh<T>& mesh, int dim,
30 std::int32_t index)
31{
32 // Get the geometrical indices for the mesh entity
33 std::span<const T> xg = mesh.geometry().x();
34
35 // FIXME: return of small dynamic array is expensive
36 std::span<const std::int32_t> entity(&index, 1);
37 const std::vector<std::int32_t> vertex_indices
38 = mesh::entities_to_geometry(mesh, dim, entity, false);
39
40 std::array<T, 6> b;
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());
45
46 // Compute min and max over vertices
47 for (std::int32_t local_vertex : vertex_indices)
48 {
49 for (std::size_t j = 0; j < 3; ++j)
50 {
51 b0[j] = std::min(b0[j], xg[3 * local_vertex + j]);
52 b1[j] = std::max(b1[j], xg[3 * local_vertex + j]);
53 }
54 }
55
56 return b;
57}
58//-----------------------------------------------------------------------------
59// Compute bounding box of bounding boxes. Each bounding box is defined as a
60// tuple (corners, entity_index). The corners of the bounding box is flattened
61// row-major as (lower left corner, top right corner).
62template <std::floating_point T>
63std::array<T, 6> compute_bbox_of_bboxes(
64 std::span<const std::pair<std::array<T, 6>, std::int32_t>> leaf_bboxes)
65{
66 // Compute min and max over remaining boxes
67 std::array<T, 6> b = leaf_bboxes.front().first;
68 for (auto [box, _] : leaf_bboxes)
69 {
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); });
75 }
76
77 return b;
78}
79//------------------------------------------------------------------------------
80template <std::floating_point 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)
84{
85 if (leaf_bboxes.size() == 1)
86 {
87 // Reached leaf
88
89 // Get bounding box coordinates for leaf
90 const auto [b, entity_index] = leaf_bboxes.front();
91
92 // Store bounding box data
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;
97 }
98 else
99 {
100 // Compute bounding box of all bounding boxes
101 std::array b = compute_bbox_of_bboxes<T>(leaf_bboxes);
102
103 // Sort bounding boxes along longest axis
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()));
109
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
113 {
114 auto x0 = p0.first[axis] + p0.first[3 + axis];
115 auto x1 = p1.first[axis] + p1.first[3 + axis];
116 return x0 < x1;
117 });
118
119 // Split bounding boxes into two groups and call recursively
120 assert(!leaf_bboxes.empty());
121 std::size_t part = leaf_bboxes.size() / 2;
122 std::int32_t bbox0
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);
126
127 // Store bounding box data. Note that root box will be added last.
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;
132 }
133}
134//-----------------------------------------------------------------------------
135template <std::floating_point 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)
138{
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)};
143}
144//-----------------------------------------------------------------------------
145template <std::floating_point T>
146std::int32_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)
150{
151 // Reached leaf
152 if (points.size() == 1)
153 {
154 // Store bounding box data
155
156 // Index of entity contained in leaf
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;
165 }
166
167 // Compute bounding box of all points
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;
171
172 // Sort bounding boxes along longest axis
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));
177
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]; });
182
183 // Split bounding boxes into two groups and call recursively
184 assert(!points.empty());
185 std::size_t part = points.size() / 2;
186 std::int32_t bbox0
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);
190
191 // Store bounding box data. Note that root box will be added last.
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;
197}
198//-----------------------------------------------------------------------------
199} // namespace impl_bb
200
203template <std::floating_point T>
205{
206private:
212 static std::vector<std::int32_t> range(mesh::Topology& topology, int tdim)
213 {
214 topology.create_entities(tdim);
215 auto map = topology.index_map(tdim);
216 assert(map);
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);
220 return r;
221 }
222
223public:
234 std::optional<std::span<const std::int32_t>> entities
235 = std::nullopt,
236 double padding = 0)
237 : _tdim(tdim)
238 {
239 // Initialize entities of given dimension if they don't exist
240 mesh.topology_mutable()->create_entities(tdim);
241
242 // Get input entities. If not provided, get all local entities of the given
243 // dimension (including ghosts)
244 std::span<const std::int32_t> entities_span;
245 std::optional<std::vector<std::int32_t>> local_range(std::nullopt);
246 if (entities)
247 entities_span = entities.value();
248 else
249 {
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());
253 }
254
255 if (tdim < 0 or tdim > mesh.topology()->dim())
256 {
257 throw std::runtime_error(
258 "Dimension must be non-negative and less than or "
259 "equal to the topological dimension of the mesh");
260 }
261
262 mesh.topology_mutable()->create_connectivity(tdim, mesh.topology()->dim());
263
264 // Create bounding boxes for all mesh entities (leaves)
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)
268 {
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);
275 }
276
277 // Recursively build the bounding box tree from the leaves
278 if (!leaf_bboxes.empty())
279 std::tie(_bboxes, _bbox_coordinates)
280 = impl_bb::build_from_leaf(leaf_bboxes);
281
282 spdlog::info("Computed bounding box tree with {} nodes for {} entities",
283 num_bboxes(), entities_span.size());
284 }
285
288 BoundingBoxTree(std::vector<std::pair<std::array<T, 3>, std::int32_t>> points)
289 : _tdim(0)
290 {
291 // Recursively build the bounding box tree from the leaves
292 if (!points.empty())
293 {
294 _bboxes.clear();
295 impl_bb::_build_from_point(std::span(points), _bboxes, _bbox_coordinates);
296 }
297
298 spdlog::info("Computed bounding box tree with {} nodes for {} points.",
299 num_bboxes(), points.size());
300 }
301
304
306 BoundingBoxTree(const BoundingBoxTree& tree) = delete;
307
310
312 BoundingBoxTree& operator=(const BoundingBoxTree& other) = default;
313
315 ~BoundingBoxTree() = default;
316
322 std::array<T, 6> get_bbox(std::size_t node) const
323 {
324 std::array<T, 6> x;
325 std::copy_n(_bbox_coordinates.data() + 6 * node, 6, x.begin());
326 return x;
327 }
328
335 {
336 // Build tree for each rank
337 const int mpi_size = dolfinx::MPI::size(comm);
338
339 // Send root node coordinates to all processes
340 // This is to counteract the fact that a process might have 0 bounding box
341 // causing false positives on process collisions around (0,0,0)
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};
345 if (num_bboxes() > 0)
346 std::copy_n(std::prev(_bbox_coordinates.end(), 6), 6, send_bbox.begin());
347 std::vector<T> recv_bbox(mpi_size * 6);
348 MPI_Allgather(send_bbox.data(), 6, dolfinx::MPI::mpi_t<T>, recv_bbox.data(),
349 6, dolfinx::MPI::mpi_t<T>, comm);
350
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)
353 {
354 std::copy_n(std::next(recv_bbox.begin(), 6 * i), 6,
355 _recv_bbox[i].first.begin());
356 _recv_bbox[i].second = i;
357 }
358
359 auto [global_bboxes, global_coords] = impl_bb::build_from_leaf(_recv_bbox);
360 BoundingBoxTree global_tree(std::move(global_bboxes),
361 std::move(global_coords));
362
363 spdlog::info("Computed global bounding box tree with {} boxes.",
364 global_tree.num_bboxes());
365
366 return global_tree;
367 }
368
370 std::int32_t num_bboxes() const { return _bboxes.size() / 2; }
371
373 int tdim() const { return _tdim; }
374
376 std::string str() const
377 {
378 std::stringstream s;
379 tree_print(s, _bboxes.size() / 2 - 1);
380 return s.str();
381 }
382
390 std::array<std::int32_t, 2> bbox(std::size_t node) const
391 {
392 assert(2 * node + 1 < _bboxes.size());
393 return {_bboxes[2 * node], _bboxes[2 * node + 1]};
394 }
395
396private:
397 // Constructor
398 BoundingBoxTree(std::vector<std::int32_t>&& bboxes,
399 std::vector<T>&& bbox_coords)
400 : _tdim(0), _bboxes(bboxes), _bbox_coordinates(bbox_coords)
401 {
402 // Do nothing
403 }
404
405 // Topological dimension of leaf entities
406 int _tdim;
407
408 // Print out recursively, for debugging
409 void tree_print(std::stringstream& s, std::int32_t i) const
410 {
411 s << "[";
412 for (std::size_t j = 0; j < 2; ++j)
413 {
414 for (std::size_t k = 0; k < 3; ++k)
415 s << _bbox_coordinates[6 * i + j * 3 + k] << " ";
416 if (j == 0)
417 s << "]->"
418 << "[";
419 }
420 s << "]\n";
421
422 if (_bboxes[2 * i] == _bboxes[2 * i + 1])
423 s << "leaf containing entity (" << _bboxes[2 * i + 1] << ")";
424 else
425 {
426 s << "{";
427 tree_print(s, _bboxes[2 * i]);
428 s << ", \n";
429 tree_print(s, _bboxes[2 * i + 1]);
430 s << "}\n";
431 }
432 }
433
434 // List of bounding boxes (parent-child-entity relations)
435 std::vector<std::int32_t> _bboxes;
436
437 // List of bounding box coordinates
438 std::vector<T> _bbox_coordinates;
439};
440} // namespace dolfinx::geometry
Definition BoundingBoxTree.h:205
BoundingBoxTree(std::vector< std::pair< std::array< T, 3 >, std::int32_t > > points)
Definition BoundingBoxTree.h:288
BoundingBoxTree & operator=(const BoundingBoxTree &other)=default
Copy assignment.
BoundingBoxTree & operator=(BoundingBoxTree &&other)=default
Move assignment.
BoundingBoxTree create_global_tree(MPI_Comm comm) const
Definition BoundingBoxTree.h:334
int tdim() const
Topological dimension of leaf entities.
Definition BoundingBoxTree.h:373
BoundingBoxTree(BoundingBoxTree &&tree)=default
Move constructor.
std::int32_t num_bboxes() const
Return number of bounding boxes.
Definition BoundingBoxTree.h:370
std::array< T, 6 > get_bbox(std::size_t node) const
Return bounding box coordinates for a given node in the tree,.
Definition BoundingBoxTree.h:322
std::array< std::int32_t, 2 > bbox(std::size_t node) const
Definition BoundingBoxTree.h:390
BoundingBoxTree(const BoundingBoxTree &tree)=delete
Copy constructor.
BoundingBoxTree(const mesh::Mesh< T > &mesh, int tdim, std::optional< std::span< const std::int32_t > > entities=std::nullopt, double padding=0)
Definition BoundingBoxTree.h:233
~BoundingBoxTree()=default
Destructor.
std::string str() const
Print out for debugging.
Definition BoundingBoxTree.h:376
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:45
std::shared_ptr< const common::IndexMap > index_map(int dim) const
Get the IndexMap that described the parallel distribution of the mesh entities.
Definition Topology.cpp:821
std::int32_t create_entities(int dim)
Create entities of given topological dimension.
Definition Topology.cpp:837
Functions supporting mesh operations.
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:282
int size(MPI_Comm comm)
Definition MPI.cpp:72
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:22
std::vector< std::int32_t > entities_to_geometry(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities, bool permute=false)
Compute the geometry degrees of freedom associated with the closure of a given set of cell entities.
Definition utils.h:632