Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CompressedLeafBVH.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
17#include "shambase/integer.hpp"
20
21template<class Tmorton, class Tvec, u32 dim>
23 make_empty(sham::DeviceScheduler_ptr dev_sched) {
24 StackEntry stack_loc{};
25 return {
27 KarrasRadixTree::make_empty(dev_sched),
29}
30
31template<class Tmorton, class Tvec, u32 dim>
33 sham::DeviceBuffer<Tvec> &positions,
34 u32 obj_cnt,
35 const shammath::AABB<Tvec> &bounding_box,
36 u32 compression_level) {
38
39 if (obj_cnt == 0) {
41 "obj_cnt is 0, cannot build a CompressedLeafBVH");
42 }
43
44 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
45
46 u32 roundup_pow2 = shambase::roundup_pow2(obj_cnt);
47
48 auto set = shamtree::morton_code_set_from_positions<Tmorton, Tvec, dim>(
49 dev_sched,
50 bounding_box,
51 positions,
52 obj_cnt,
53 roundup_pow2,
54 std::move(reduced_morton_set.morton_codes_set.sorted_morton_codes));
55
56 auto sorted_set = shamtree::sort_morton_set(
57 dev_sched,
58 std::move(set),
59 std::move(reduced_morton_set.morton_codes_set.map_morton_id_to_obj_id));
60
61 auto reduced_set = shamtree::reduce_morton_set(
62 dev_sched,
63 std::move(sorted_set),
64 compression_level,
65 std::move(reduced_morton_set.buf_reduc_index_map),
66 std::move(reduced_morton_set.reduced_morton_codes));
67
68 auto tree = shamtree::karras_tree_from_morton_set(
69 dev_sched,
70 reduced_set.reduced_morton_codes.get_size(),
71 reduced_set.reduced_morton_codes,
72 std::move(structure));
73
74 this->reduced_morton_set = std::move(reduced_set);
75 this->structure = std::move(tree);
76}
77
78template<class Tmorton, class Tvec, u32 dim>
80 sham::DeviceBuffer<Tvec> &positions,
81 u32 obj_cnt,
82 const shammath::AABB<Tvec> &bounding_box,
83 u32 compression_level) {
84
85 this->internal_rebuild_from_positions_no_aabb(
86 positions, obj_cnt, bounding_box, compression_level);
87
89 this->structure,
90 this->reduced_morton_set.get_leaf_cell_iterator(),
91 std::move(this->aabbs),
92 positions);
93
94 this->aabbs = std::move(tree_aabbs);
95}
96
97template<class Tmorton, class Tvec, u32 dim>
101 u32 obj_cnt,
102 shammath::AABB<Tvec> &bounding_box,
103 u32 compression_level) {
104
105 this->internal_rebuild_from_positions_no_aabb(min, obj_cnt, bounding_box, compression_level);
106
108 this->structure,
109 this->reduced_morton_set.get_leaf_cell_iterator(),
110 std::move(this->aabbs),
111 min,
112 max);
113
114 this->aabbs = std::move(tree_aabbs);
115}
116
117template<class Tmorton, class Tvec, u32 dim>
119 sham::DeviceBuffer<Tvec> &positions,
120 const shammath::AABB<Tvec> &bounding_box,
121 u32 compression_level) {
122 this->rebuild_from_positions(positions, positions.get_size(), bounding_box, compression_level);
123}
124
125template<class Tmorton, class Tvec, u32 dim>
129 shammath::AABB<Tvec> &bounding_box,
130 u32 compression_level) {
131 if (min.get_size() != max.get_size()) {
133 "min and max must have the same size");
134 }
135 this->rebuild_from_position_range(min, max, min.get_size(), bounding_box, compression_level);
136}
137
KarrasRadixTreeAABB< Tvec > compute_tree_aabb_from_position_ranges(const KarrasRadixTree &tree, const LeafCellIterator &cell_it, KarrasRadixTreeAABB< Tvec > &&recycled_tree_aabb, sham::DeviceBuffer< Tvec > &min, sham::DeviceBuffer< Tvec > &max)
same but for position ranges
KarrasRadixTreeAABB< Tvec > compute_tree_aabb_from_positions(const KarrasRadixTree &tree, const LeafCellIterator &cell_it, KarrasRadixTreeAABB< Tvec > &&recycled_tree_aabb, sham::DeviceBuffer< Tvec > &positions)
Compute the AABB of all cells in the tree from positions.
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
size_t get_size() const
Gets the number of elements in the buffer.
A Compressed Leaf Bounding Volume Hierarchy (CLBVH) for neighborhood queries.
void rebuild_from_positions(sham::DeviceBuffer< Tvec > &positions, const shammath::AABB< Tvec > &bounding_box, u32 compression_level)
rebuild the BVH from the given positions
static CompressedLeafBVH make_empty(sham::DeviceScheduler_ptr dev_sched)
make an empty BVH
Class representing a set of Morton codes with associated bounding box and position data that was redu...
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
constexpr T roundup_pow2(T v) noexcept
round up to the next power of two Source : https://graphics.stanford.edu/~seander/bithacks....
Definition integer.hpp:92
This file contains the definition for the stacktrace related functionality.
#define __shamrock_stack_entry()
Macro to create a stack entry.
Axis-Aligned bounding box.
Definition AABB.hpp:99