morton-arithmetic 0.1.0
Fast Morton (Z-order) codes with O(1) arithmetic
Loading...
Searching...
No Matches
morton::Morton< Dim, Bits > Class Template Reference

A Morton (Z-order) code interleaving Dim unsigned coordinates of Bits bits each. More...

#include <morton.hpp>

Public Types

using code_type = detail::uint_for_t< Dim *Bits >
 
using coord_type = detail::uint_for_t< Bits >
 

Public Member Functions

MORTON_HD constexpr Morton () noexcept
 
MORTON_HD constexpr code_type code () const noexcept
 
MORTON_HD constexpr coord_type get (unsigned d) const
 Decode coordinate of a single axis.
 
MORTON_HD constexpr std::array< coord_type, Dimdecode () const
 Decode all coordinates.
 
MORTON_HD constexpr void set (unsigned d, coord_type value)
 Replace one axis' coordinate (other axes untouched). Wraps mod 2^Bits.
 
MORTON_HD constexpr void add (unsigned d, coord_type k)
 Add k to axis d (wraps). O(1), branchless.
 
MORTON_HD constexpr void sub (unsigned d, coord_type k)
 Subtract k from axis d (wraps). O(1), branchless.
 
MORTON_HD constexpr void inc (unsigned d)
 Increment axis d by one. O(1).
 
MORTON_HD constexpr void dec (unsigned d)
 Decrement axis d by one. O(1).
 
MORTON_HD constexpr Morton neighbor (unsigned d, int dir) const
 Morton code of the neighbour one step along ± axis d (wraps).
 
MORTON_HD constexpr void add_sat (unsigned d, coord_type k)
 Add k to axis d, clamping at coord_max instead of wrapping.
 
MORTON_HD constexpr void sub_sat (unsigned d, coord_type k)
 Subtract k from axis d, clamping at 0 instead of wrapping.
 
MORTON_HD constexpr bool try_add (unsigned d, coord_type k)
 Add k to axis d only if it does not overflow past coord_max.
 
MORTON_HD constexpr bool try_sub (unsigned d, coord_type k)
 Subtract k from axis d only if it does not underflow past 0.
 
MORTON_HD constexpr std::array< Morton, 2 *Dimface_neighbors () const
 The 2*Dim von Neumann (face) neighbours, ordered {axis0-, axis0+, axis1-, axis1+, ...}.
 
MORTON_HD constexpr std::array< Morton, detail::pow3(Dim) - 1 > all_neighbors () const
 The 3^Dim - 1 Moore neighbours (all cells differing by -1/0/+1 on each axis, excluding self), in odometer order.
 
MORTON_HD constexpr Morton ancestor (unsigned level) const
 Ancestor cell origin at the given level (clears the low level*Dim bits).
 
MORTON_HD constexpr unsigned child_index (unsigned level) const
 Index (0 .
 
MORTON_HD constexpr Morton child (unsigned level, unsigned octant) const
 The octant-th child origin of a cell that is an ancestor at level (i.e.
 
MORTON_HD constexpr Mortonoperator++ () noexcept
 
MORTON_HD constexpr Morton operator++ (int) noexcept
 
MORTON_HD constexpr Mortonoperator-- () noexcept
 
MORTON_HD constexpr Morton operator-- (int) noexcept
 

Static Public Member Functions

static MORTON_HD constexpr Morton from_code (code_type raw) noexcept
 Wrap a raw, already-interleaved code value.
 
template<typename... Cs, typename = std::enable_if_t<sizeof...(Cs) == Dim>>
static MORTON_HD constexpr Morton encode (Cs... coords)
 Encode Dim coordinates into a Morton code.
 
static MORTON_HD constexpr Morton encode (const std::array< coord_type, Dim > &c)
 Encode from an array of coordinates.
 
static MORTON_HD constexpr code_type deposit (coord_type x, unsigned d)
 
static MORTON_HD constexpr coord_type extract (code_type c, unsigned d)
 
static MORTON_HD constexpr code_type axis_mask (unsigned d)
 Mask of bits belonging to axis d.
 

Static Public Attributes

static constexpr unsigned dimensions = Dim
 
static constexpr unsigned bits_per_axis = Bits
 
static constexpr unsigned code_bits = Dim * Bits
 
static constexpr code_type field_mask
 Mask of all valid code bits (handles code_bits == width without UB).
 
static constexpr coord_type coord_max
 Largest representable coordinate value per axis.
 
static constexpr unsigned octants = unsigned(1) << Dim
 

Friends

MORTON_HD constexpr bool operator== (Morton a, Morton b) noexcept
 
MORTON_HD constexpr bool operator!= (Morton a, Morton b) noexcept
 
MORTON_HD constexpr bool operator< (Morton a, Morton b) noexcept
 
MORTON_HD constexpr bool operator<= (Morton a, Morton b) noexcept
 
MORTON_HD constexpr bool operator> (Morton a, Morton b) noexcept
 
MORTON_HD constexpr bool operator>= (Morton a, Morton b) noexcept
 

Detailed Description

template<unsigned Dim, unsigned Bits>
class morton::Morton< Dim, Bits >

A Morton (Z-order) code interleaving Dim unsigned coordinates of Bits bits each.

The whole code occupies Dim * Bits bits and is stored in the smallest unsigned integer that fits.

Member Typedef Documentation

◆ code_type

template<unsigned Dim, unsigned Bits>
using morton::Morton< Dim, Bits >::code_type = detail::uint_for_t<Dim * Bits>

◆ coord_type

template<unsigned Dim, unsigned Bits>
using morton::Morton< Dim, Bits >::coord_type = detail::uint_for_t<Bits>

Constructor & Destructor Documentation

◆ Morton()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr morton::Morton< Dim, Bits >::Morton ( )
inlineconstexprnoexcept

Member Function Documentation

◆ from_code()

template<unsigned Dim, unsigned Bits>
static MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::from_code ( code_type  raw)
inlinestaticconstexprnoexcept

Wrap a raw, already-interleaved code value.

◆ encode() [1/2]

template<unsigned Dim, unsigned Bits>
template<typename... Cs, typename = std::enable_if_t<sizeof...(Cs) == Dim>>
static MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::encode ( Cs...  coords)
inlinestaticconstexpr

Encode Dim coordinates into a Morton code.

◆ encode() [2/2]

template<unsigned Dim, unsigned Bits>
static MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::encode ( const std::array< coord_type, Dim > &  c)
inlinestaticconstexpr

Encode from an array of coordinates.

◆ code()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr code_type morton::Morton< Dim, Bits >::code ( ) const
inlineconstexprnoexcept

◆ get()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr coord_type morton::Morton< Dim, Bits >::get ( unsigned  d) const
inlineconstexpr

Decode coordinate of a single axis.

◆ decode()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr std::array< coord_type, Dim > morton::Morton< Dim, Bits >::decode ( ) const
inlineconstexpr

Decode all coordinates.

◆ set()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::set ( unsigned  d,
coord_type  value 
)
inlineconstexpr

Replace one axis' coordinate (other axes untouched). Wraps mod 2^Bits.

◆ add()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::add ( unsigned  d,
coord_type  k 
)
inlineconstexpr

Add k to axis d (wraps). O(1), branchless.

◆ sub()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::sub ( unsigned  d,
coord_type  k 
)
inlineconstexpr

Subtract k from axis d (wraps). O(1), branchless.

◆ inc()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::inc ( unsigned  d)
inlineconstexpr

Increment axis d by one. O(1).

◆ dec()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::dec ( unsigned  d)
inlineconstexpr

Decrement axis d by one. O(1).

◆ neighbor()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::neighbor ( unsigned  d,
int  dir 
) const
inlineconstexpr

Morton code of the neighbour one step along ± axis d (wraps).

◆ add_sat()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::add_sat ( unsigned  d,
coord_type  k 
)
inlineconstexpr

Add k to axis d, clamping at coord_max instead of wrapping.

◆ sub_sat()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr void morton::Morton< Dim, Bits >::sub_sat ( unsigned  d,
coord_type  k 
)
inlineconstexpr

Subtract k from axis d, clamping at 0 instead of wrapping.

◆ try_add()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool morton::Morton< Dim, Bits >::try_add ( unsigned  d,
coord_type  k 
)
inlineconstexpr

Add k to axis d only if it does not overflow past coord_max.

Returns false (and leaves the code unchanged) if it would.

◆ try_sub()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool morton::Morton< Dim, Bits >::try_sub ( unsigned  d,
coord_type  k 
)
inlineconstexpr

Subtract k from axis d only if it does not underflow past 0.

◆ face_neighbors()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr std::array< Morton, 2 *Dim > morton::Morton< Dim, Bits >::face_neighbors ( ) const
inlineconstexpr

The 2*Dim von Neumann (face) neighbours, ordered {axis0-, axis0+, axis1-, axis1+, ...}.

Wraps at the grid edge.

◆ all_neighbors()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr std::array< Morton, detail::pow3(Dim) - 1 > morton::Morton< Dim, Bits >::all_neighbors ( ) const
inlineconstexpr

The 3^Dim - 1 Moore neighbours (all cells differing by -1/0/+1 on each axis, excluding self), in odometer order.

Wraps at the grid edge.

◆ ancestor()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::ancestor ( unsigned  level) const
inlineconstexpr

Ancestor cell origin at the given level (clears the low level*Dim bits).

level 0 is *this.

◆ child_index()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr unsigned morton::Morton< Dim, Bits >::child_index ( unsigned  level) const
inlineconstexpr

Index (0 .

. 2^Dim-1) of this cell within its parent at level+1, i.e. the Dim interleaved bits just above the level boundary.

◆ child()

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::child ( unsigned  level,
unsigned  octant 
) const
inlineconstexpr

The octant-th child origin of a cell that is an ancestor at level (i.e.

set the Dim bits at position (level-1)*Dim). Requires level >= 1.

◆ operator++() [1/2]

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton & morton::Morton< Dim, Bits >::operator++ ( )
inlineconstexprnoexcept

◆ operator++() [2/2]

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::operator++ ( int  )
inlineconstexprnoexcept

◆ operator--() [1/2]

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton & morton::Morton< Dim, Bits >::operator-- ( )
inlineconstexprnoexcept

◆ operator--() [2/2]

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr Morton morton::Morton< Dim, Bits >::operator-- ( int  )
inlineconstexprnoexcept

◆ deposit()

template<unsigned Dim, unsigned Bits>
static MORTON_HD constexpr code_type morton::Morton< Dim, Bits >::deposit ( coord_type  x,
unsigned  d 
)
inlinestaticconstexpr

◆ extract()

template<unsigned Dim, unsigned Bits>
static MORTON_HD constexpr coord_type morton::Morton< Dim, Bits >::extract ( code_type  c,
unsigned  d 
)
inlinestaticconstexpr

◆ axis_mask()

template<unsigned Dim, unsigned Bits>
static MORTON_HD constexpr code_type morton::Morton< Dim, Bits >::axis_mask ( unsigned  d)
inlinestaticconstexpr

Mask of bits belonging to axis d.

Friends And Related Symbol Documentation

◆ operator==

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool operator== ( Morton< Dim, Bits a,
Morton< Dim, Bits b 
)
friend

◆ operator!=

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool operator!= ( Morton< Dim, Bits a,
Morton< Dim, Bits b 
)
friend

◆ operator<

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool operator< ( Morton< Dim, Bits a,
Morton< Dim, Bits b 
)
friend

◆ operator<=

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool operator<= ( Morton< Dim, Bits a,
Morton< Dim, Bits b 
)
friend

◆ operator>

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool operator> ( Morton< Dim, Bits a,
Morton< Dim, Bits b 
)
friend

◆ operator>=

template<unsigned Dim, unsigned Bits>
MORTON_HD constexpr bool operator>= ( Morton< Dim, Bits a,
Morton< Dim, Bits b 
)
friend

Member Data Documentation

◆ dimensions

template<unsigned Dim, unsigned Bits>
constexpr unsigned morton::Morton< Dim, Bits >::dimensions = Dim
staticconstexpr

◆ bits_per_axis

template<unsigned Dim, unsigned Bits>
constexpr unsigned morton::Morton< Dim, Bits >::bits_per_axis = Bits
staticconstexpr

◆ code_bits

template<unsigned Dim, unsigned Bits>
constexpr unsigned morton::Morton< Dim, Bits >::code_bits = Dim * Bits
staticconstexpr

◆ field_mask

template<unsigned Dim, unsigned Bits>
constexpr code_type morton::Morton< Dim, Bits >::field_mask
staticconstexpr
Initial value:
= (code_bits >= code_type_bits)
: code_type((code_type(1) << code_bits) - 1)
detail::uint_for_t< Dim *Bits > code_type
Definition morton.hpp:214
static constexpr unsigned code_bits
Definition morton.hpp:212

Mask of all valid code bits (handles code_bits == width without UB).

◆ coord_max

template<unsigned Dim, unsigned Bits>
constexpr coord_type morton::Morton< Dim, Bits >::coord_max
staticconstexpr
Initial value:
= (Bits >= coord_type_bits)
: coord_type((coord_type(1) << Bits) - 1)
detail::uint_for_t< Bits > coord_type
Definition morton.hpp:215
MORTON_HD constexpr void add(unsigned d, coord_type k)
Add k to axis d (wraps). O(1), branchless.
Definition morton.hpp:290

Largest representable coordinate value per axis.

◆ octants

template<unsigned Dim, unsigned Bits>
constexpr unsigned morton::Morton< Dim, Bits >::octants = unsigned(1) << Dim
staticconstexpr

The documentation for this class was generated from the following file: