| 1 |
/* |
| 2 |
====================================================================== |
| 3 |
Routines to generate and compare Morton Codes, i.e. indices on space |
| 4 |
filling Z-curves. |
| 5 |
|
| 6 |
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
| 7 |
(c) Lucerne University of Applied Sciences and Arts, |
| 8 |
supported by the Swiss National Science Foundation (SNSF, #147053) |
| 9 |
====================================================================== |
| 10 |
|
| 11 |
$Id: oocmorton.h,v 2.1 2016/05/17 17:39:47 rschregle Exp $ |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef OOC_MORTON_H |
| 15 |
#define OOC_MORTON_H |
| 16 |
|
| 17 |
#include "fvect.h" |
| 18 |
#include <stdint.h> |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
extern "C" { |
| 22 |
#endif |
| 23 |
|
| 24 |
/* Type to represent Morton codes (Z-curve indices) as sort keys */ |
| 25 |
typedef uint64_t OOC_MortonIdx; |
| 26 |
|
| 27 |
|
| 28 |
/* Number of bits per dimension for Morton code (Z-curve index) used to |
| 29 |
* sort records. This corresponds to the maximum number of bounding box |
| 30 |
* subdivisions which can be resolved per axis; further subdivisions will |
| 31 |
* map to the same Morton code, thus invalidating the sort! */ |
| 32 |
#define OOC_MORTON_BITS 21 |
| 33 |
#define OOC_MORTON_MAX (((OOC_MortonIdx)1 << OOC_MORTON_BITS) - 1) |
| 34 |
|
| 35 |
|
| 36 |
/* Interleave lower OOC_MORTON_BITS bits of k with 00, resulting in |
| 37 |
* 3*OOC_MORTON_BITS bits. Optimised "bitmask hack" version. This code |
| 38 |
* taken from: |
| 39 |
* http://www.forceflow.be/2013/10/07/ |
| 40 |
* morton-encodingdecoding-through-bit-interleaving-implementations/ */ |
| 41 |
#define OOC_BitInterleave(k) \ |
| 42 |
((k) &= OOC_MORTON_MAX, \ |
| 43 |
(k) = ((k) | (k) << 32) & 0x001f00000000ffff, \ |
| 44 |
(k) = ((k) | (k) << 16) & 0x001f0000ff0000ff, \ |
| 45 |
(k) = ((k) | (k) << 8) & 0x100f00f00f00f00f, \ |
| 46 |
(k) = ((k) | (k) << 4) & 0x10c30c30c30c30c3, \ |
| 47 |
(k) = ((k) | (k) << 2) & 0x1249249249249249) |
| 48 |
|
| 49 |
|
| 50 |
/* Compute Morton code (Z-curve index) of length 3 * OOC_MORTON_BITS bits |
| 51 |
* for 3D key within bounding box defined by org and scaled to maximum |
| 52 |
* index with scale */ |
| 53 |
OOC_MortonIdx OOC_Key2Morton (const FVECT key, const FVECT org, |
| 54 |
RREAL scale); |
| 55 |
|
| 56 |
#ifdef __cplusplus |
| 57 |
} |
| 58 |
#endif |
| 59 |
|
| 60 |
#endif |
| 61 |
|