PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Morton.h
Go to the documentation of this file.
1
10
11#ifndef PBAT_GEOMETRY_MORTON_H
12#define PBAT_GEOMETRY_MORTON_H
13
14#include "pbat/HostDevice.h"
15
16#include <algorithm>
17#include <array>
18#include <cstdint>
19#include <type_traits>
20
21namespace pbat {
22namespace geometry {
23
24using MortonCodeType = std::uint32_t;
25
35PBAT_HOST_DEVICE inline MortonCodeType ExpandBits(MortonCodeType v)
36{
37 v = (v * 0x00010001u) & 0xFF0000FFu;
38 v = (v * 0x00000101u) & 0x0F00F00Fu;
39 v = (v * 0x00000011u) & 0xC30C30C3u;
40 v = (v * 0x00000005u) & 0x49249249u;
41 return v;
42}
43
44namespace detail {
45
46template <class Point>
47concept CMorton3dPoint = requires(Point p)
48{
49 {
50 p[0]
51 } -> std::convertible_to<float>;
52 {
53 p[1]
54 } -> std::convertible_to<float>;
55 {
56 p[2]
57 } -> std::convertible_to<float>;
58};
59
60} // namespace detail
61
68template <detail::CMorton3dPoint Point>
69[[maybe_unused]] PBAT_HOST_DEVICE inline MortonCodeType Morton3D(Point x)
70{
71 using namespace std;
72 using ScalarType = std::remove_cvref_t<decltype(x[0])>;
74 min(max(x[0] * ScalarType(1024), ScalarType(0)), ScalarType(1023))));
76 min(max(x[1] * ScalarType(1024), ScalarType(0)), ScalarType(1023))));
78 min(max(x[2] * ScalarType(1024), ScalarType(0)), ScalarType(1023))));
79 return xx * 4 + yy * 2 + zz;
80}
81
82} // namespace geometry
83} // namespace pbat
84
85#endif // PBAT_GEOMETRY_MORTON_H
Geometric queries, quantities and data structures.
Definition AabbKdTreeHierarchy.h:23
PBAT_HOST_DEVICE MortonCodeType ExpandBits(MortonCodeType v)
Expands a 10-bit integer into 30 bits by inserting 2 zeros after each bit.
Definition Morton.h:35
std::uint32_t MortonCodeType
Type used to represent Morton codes.
Definition Morton.h:24
PBAT_HOST_DEVICE MortonCodeType Morton3D(Point x)
Calculates a 30-bit Morton code for the given 3D point located within the unit cube [0,...
Definition Morton.h:69
The main namespace of the library.
Definition Aliases.h:15