Skip to main content

parry2d/utils/
morton.rs

1//! Morton encoding of 3D vectors.
2
3// From https://github.com/DGriffin91/obvhs/tree/main/src/ploc/morton.rs
4// MIT/Apache 2 license.
5
6//---------------------------------------------------
7// --- 21 bit resolution per channel morton curve ---
8//---------------------------------------------------
9
10use crate::math::Vector;
11
12#[inline]
13fn split_by_3_u64(a: u32) -> u64 {
14    let mut x = a as u64 & 0x1fffff; // we only look at the first 21 bits
15    x = (x | x << 32) & 0x1f00000000ffff;
16    x = (x | x << 16) & 0x1f0000ff0000ff;
17    x = (x | x << 8) & 0x100f00f00f00f00f;
18    x = (x | x << 4) & 0x10c30c30c30c30c3;
19    x = (x | x << 2) & 0x1249249249249249;
20    x
21}
22
23#[inline]
24/// Encode x,y,z position into a u64 morton value.
25/// Input should be 0..=2u32.pow(21) (or 1u32 << 21)
26fn morton_encode_u64(x: u32, y: u32, z: u32) -> u64 {
27    split_by_3_u64(x) | split_by_3_u64(y) << 1 | split_by_3_u64(z) << 2
28}
29
30#[inline]
31/// Encode a 3D position into a u64 morton value.
32/// Input should be 0.0..=1.0
33#[cfg_attr(feature = "f64", expect(clippy::unnecessary_cast))]
34pub fn morton_encode_u64_unorm(p: Vector) -> u64 {
35    let scale = (1u64 << 21) as f64;
36    let x = (p.x as f64 * scale) as u32;
37    let y = (p.y as f64 * scale) as u32;
38    #[cfg(feature = "dim2")]
39    return morton_encode_u64(x, y, 0);
40    #[cfg(feature = "dim3")]
41    {
42        let z = (p.z as f64 * scale) as u32;
43        morton_encode_u64(x, y, z)
44    }
45}