Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/MortonCode.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Geometry/AABox.h>78JPH_NAMESPACE_BEGIN910class MortonCode11{12public:13/// First converts a floating point value in the range [0, 1] to a 10 bit fixed point integer.14/// Then expands a 10-bit integer into 30 bits by inserting 2 zeros after each bit.15static uint32 sExpandBits(float inV)16{17JPH_ASSERT(inV >= 0.0f && inV <= 1.0f);18uint32 v = uint32(inV * 1023.0f + 0.5f);19JPH_ASSERT(v < 1024);20v = (v * 0x00010001u) & 0xFF0000FFu;21v = (v * 0x00000101u) & 0x0F00F00Fu;22v = (v * 0x00000011u) & 0xC30C30C3u;23v = (v * 0x00000005u) & 0x49249249u;24return v;25}2627/// Calculate the morton code for inVector, given that all vectors lie in inVectorBounds28static uint32 sGetMortonCode(Vec3Arg inVector, const AABox &inVectorBounds)29{30// Convert to 10 bit fixed point31Vec3 scaled = (inVector - inVectorBounds.mMin) / inVectorBounds.GetSize();32uint x = sExpandBits(scaled.GetX());33uint y = sExpandBits(scaled.GetY());34uint z = sExpandBits(scaled.GetZ());35return (x << 2) + (y << 1) + z;36}37};3839JPH_NAMESPACE_END404142