Skip to content

szaghi/MORTIF

Repository files navigation

MORTIF

MORTon Indexer (Z-order) Fortran environment

a pure Fortran 2003+ library to encode and decode multidimensional integer indexes into Morton's Z-order.

GitHub tag GitHub issues CI Coverage License

🔢 2D encoding
morton2D maps two 32-bit indexes → one 64-bit Morton code
📐 3D encoding
morton3D maps three 32-bit indexes → one 64-bit code (up to 21 bits/axis)
🔄 Lossless decoding
demorton2D/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.


Authors

Contributions are welcome — see the Contributing page.

Copyrights

This project is distributed under a multi-licensing system:

Anyone interested in using, developing, or contributing to this project is welcome — pick the license that best fits your needs.


Quick start

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 0

Elemental 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.


Install

FoBiS

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 library

As a project dependency — declare MORTIF in your fobos and run fetch:

[dependencies]
deps_dir = src/third_party
MORTIF = https://github.com/szaghi/MORTIF
FoBiS.py fetch           # fetch and build
FoBiS.py fetch --update  # re-fetch and rebuild

CMake

git clone https://github.com/szaghi/MORTIF --recursive && cd MORTIF
cmake -B build -DMORTIF_ENABLE_TESTS=ON && cmake --build build && ctest --test-dir build

As a CMake subdirectory:

add_subdirectory(MORTIF)
target_link_libraries(your_target MORTIF::MORTIF)

About

MORTon Indexer (Z-order) Fortran environment

Topics

Resources

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors