a pure Fortran 2003+ library to encode and decode multidimensional integer indexes into Morton's Z-order.
🔢 2D encodingmorton2D maps two 32-bit indexes → one 64-bit Morton code |
📐 3D encodingmorton3D maps three 32-bit indexes → one 64-bit code (up to 21 bits/axis) |
🔄 Lossless decodingdemorton2D/demorton3D are the exact inverse — bit-perfect round-trip |
⚡ Elemental interface All four procedures work on scalars and conformable arrays in one call |
|---|---|---|---|
| 🧩 Single module Entire library is a single mortif.f90 — easy to vendor |
📏 Configurable precision Optional b parameter restricts to 2/4/8/16/32 significant bits per axis |
🏎️ Zero overhead No range checks, no allocations, no system calls |
📦 Multiple build systems FoBiS, CMake, GNU Make |
For full documentation (guide, API reference, examples, etc...) see the MORTIF website.
- Stefano Zaghi — @szaghi
Contributions are welcome — see the Contributing page.
This project is distributed under a multi-licensing system:
- FOSS projects: GPL v3
- Closed source / commercial: BSD 2-Clause, BSD 3-Clause, or MIT
Anyone interested in using, developing, or contributing to this project is welcome — pick the license that best fits your needs.
use, intrinsic :: iso_fortran_env, only : int32, int64
use mortif
implicit none
integer(int32) :: i, j, k
integer(int64) :: code
code = morton3D(i=0_int32, j=1_int32, k=0_int32)
print '(A,I0)', "Morton code of {0,1,0}: ", code ! 2
call demorton3D(code=code, i=i, j=j, k=k)
print '(A,3(I0,1X))', "Decoded: ", i, j, k ! 0 1 0Elemental use on arrays:
use, intrinsic :: iso_fortran_env, only : int32, int64
use mortif
implicit none
integer(int32) :: ix(4) = [0, 1, 2, 3]
integer(int32) :: iy(4) = [0, 0, 1, 1]
integer(int64) :: codes(4)
codes = morton2D(i=ix, j=iy) ! [0, 1, 4, 5]See src/tests/ for correctness tests including extrema and 4096-point 16×16×16 grid validation.
Standalone — clone, fetch the dependency, and build:
git clone https://github.com/szaghi/MORTIF && cd MORTIF
FoBiS.py fetch # fetch PENF
FoBiS.py build -mode static-gnu # build static libraryAs a project dependency — declare MORTIF in your fobos and run fetch:
[dependencies]
deps_dir = src/third_party
MORTIF = https://github.com/szaghi/MORTIFFoBiS.py fetch # fetch and build
FoBiS.py fetch --update # re-fetch and rebuildgit clone https://github.com/szaghi/MORTIF --recursive && cd MORTIF
cmake -B build -DMORTIF_ENABLE_TESTS=ON && cmake --build build && ctest --test-dir buildAs a CMake subdirectory:
add_subdirectory(MORTIF)
target_link_libraries(your_target MORTIF::MORTIF)