@thi.ng/morton

This project is part of the
@thi.ng/umbrella monorepo.
About
Z-order curve / Morton encoding, decoding & range extraction for arbitrary dimensions.
References:
Status
STABLE - used in production
Related packages
Installation
yarn add @thi.ng/morton
Package sizes (gzipped): ESM: 2.0KB / CJS: 2.1KB / UMD: 2.1KB
Dependencies
API
Generated API docs
ZCurve class
The ZCurve
class provides nD encoding/decoding and Z index range
extraction (for a given nD bounding box). The maximum per-component
value range is 32 bits (unsigned!).
Note: All Z indices are encoded as ES
BigInt
and therefore only available in environments where BigInt
s are already
supported. No polyfill is provided!
TypeScript projects using this class should set their compile target (in
tsconfig.json
) to "ESNext"
to enable BigInt
support.
const z = new ZCurve(3, 16);
z.encode([60000, 30000, 20000]);
z.decode(67807501328384n);
[...z.range([2, 2, 0], [3, 6, 1])]
[...z.range([2, 2, 0], [3, 6, 1])].map((i) => z.decode(i));
Low level (2D / 3D only)
Source
import * as m from "@thi.ng/morton";
m.mux2(23, 42);
m.demux2(2461)
m.muxScaled2(0.25, 0.75)
m.demuxScaled2(m.muxScaled2(0.25, 0.75))
Authors
Karsten Schmidt
License
© 2015 - 2020 Karsten Schmidt // Apache Software License 2.0