| 1 |
rschregle |
2.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 1.2 2015/11/02 17:29:14 taschreg Exp taschreg $ |
| 12 |
|
|
*/ |
| 13 |
|
|
|
| 14 |
|
|
#ifndef OOC_MORTON_H |
| 15 |
|
|
#define OOC_MORTON_H |
| 16 |
|
|
|
| 17 |
|
|
#include "fvect.h" |
| 18 |
|
|
#include <stdint.h> |
| 19 |
|
|
|
| 20 |
|
|
|
| 21 |
|
|
/* Type to represent Morton codes (Z-curve indices) as sort keys */ |
| 22 |
|
|
typedef uint64_t OOC_MortonIdx; |
| 23 |
|
|
|
| 24 |
|
|
|
| 25 |
|
|
/* Number of bits per dimension for Morton code (Z-curve index) used to |
| 26 |
|
|
* sort records. This corresponds to the maximum number of bounding box |
| 27 |
|
|
* subdivisions which can be resolved per axis; further subdivisions will |
| 28 |
|
|
* map to the same Morton code, thus invalidating the sort! */ |
| 29 |
|
|
#define OOC_MORTON_BITS 21 |
| 30 |
|
|
#define OOC_MORTON_MAX (((OOC_MortonIdx)1 << OOC_MORTON_BITS) - 1) |
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
|
|
/* Interleave lower OOC_MORTON_BITS bits of k with 00, resulting in |
| 34 |
|
|
* 3*OOC_MORTON_BITS bits. Optimised "bitmask hack" version. This code |
| 35 |
|
|
* taken from: |
| 36 |
|
|
* http://www.forceflow.be/2013/10/07/ |
| 37 |
|
|
* morton-encodingdecoding-through-bit-interleaving-implementations/ */ |
| 38 |
|
|
#define OOC_BitInterleave(k) \ |
| 39 |
|
|
((k) &= OOC_MORTON_MAX, \ |
| 40 |
|
|
(k) = ((k) | (k) << 32) & 0x001f00000000ffff, \ |
| 41 |
|
|
(k) = ((k) | (k) << 16) & 0x001f0000ff0000ff, \ |
| 42 |
|
|
(k) = ((k) | (k) << 8) & 0x100f00f00f00f00f, \ |
| 43 |
|
|
(k) = ((k) | (k) << 4) & 0x10c30c30c30c30c3, \ |
| 44 |
|
|
(k) = ((k) | (k) << 2) & 0x1249249249249249) |
| 45 |
|
|
|
| 46 |
|
|
|
| 47 |
|
|
/* Compute Morton code (Z-curve index) of length 3 * OOC_MORTON_BITS bits |
| 48 |
|
|
* for 3D key within bounding box defined by org and scaled to maximum |
| 49 |
|
|
* index with scale */ |
| 50 |
|
|
OOC_MortonIdx OOC_Key2Morton (const FVECT key, const FVECT org, |
| 51 |
|
|
RREAL scale); |
| 52 |
|
|
#endif |
| 53 |
|
|
|