KiCad PCB EDA Suite
mortoncodes.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
5 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
31#include "mortoncodes.h"
32
33
34// "Insert" a 0 bit after each of the 16 low bits of x
35uint32_t Part1By1( uint32_t x )
36{
37 x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
38 x = ( x ^ ( x << 8 ) ) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
39 x = ( x ^ ( x << 4 ) ) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
40 x = ( x ^ ( x << 2 ) ) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
41 x = ( x ^ ( x << 1 ) ) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
42
43 return x;
44}
45
46
47// "Insert" two 0 bits after each of the 10 low bits of x
48uint32_t Part1By2( uint32_t x )
49{
50 x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
51 x = ( x ^ ( x << 16 ) ) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
52 x = ( x ^ ( x << 8 ) ) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
53 x = ( x ^ ( x << 4 ) ) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
54 x = ( x ^ ( x << 2 ) ) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
55
56 return x;
57}
58
59
60// Inverse of Part1By1 - "delete" all odd-indexed bits
61uint32_t Compact1By1( uint32_t x )
62{
63 x &= 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
64 x = ( x ^ ( x >> 1 ) ) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
65 x = ( x ^ ( x >> 2 ) ) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
66 x = ( x ^ ( x >> 4 ) ) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
67 x = ( x ^ ( x >> 8 ) ) & 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
68
69 return x;
70}
71
72
73// Inverse of Part1By2 - "delete" all bits not at positions divisible by 3
74uint32_t Compact1By2( uint32_t x )
75{
76 x &= 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
77 x = ( x ^ ( x >> 2 ) ) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
78 x = ( x ^ ( x >> 4 ) ) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
79 x = ( x ^ ( x >> 8 ) ) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
80 x = ( x ^ ( x >> 16 ) ) & 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
81
82 return x;
83}
84
85
86uint32_t EncodeMorton2( uint32_t x, uint32_t y )
87{
88 return ( Part1By1( y ) << 1 ) + Part1By1( x );
89}
90
91
92uint32_t EncodeMorton3( uint32_t x, uint32_t y, uint32_t z )
93{
94 return ( Part1By2( z ) << 2 ) + ( Part1By2( y ) << 1 ) + Part1By2( x );
95}
96
97
98uint32_t DecodeMorton2X( uint32_t code )
99{
100 return Compact1By1( code >> 0 );
101}
102
103
104uint32_t DecodeMorton2Y( uint32_t code )
105{
106 return Compact1By1( code >> 1 );
107}
108
109
110uint32_t DecodeMorton3X( uint32_t code )
111{
112 return Compact1By2( code >> 0 );
113}
114
115
116uint32_t DecodeMorton3Y( uint32_t code )
117{
118 return Compact1By2( code >> 1 );
119}
120
121
122uint32_t DecodeMorton3Z( uint32_t code )
123{
124 return Compact1By2( code >> 2 );
125}
uint32_t Compact1By2(uint32_t x)
Definition: mortoncodes.cpp:74
uint32_t DecodeMorton3Z(uint32_t code)
uint32_t DecodeMorton3Y(uint32_t code)
uint32_t EncodeMorton2(uint32_t x, uint32_t y)
Definition: mortoncodes.cpp:86
uint32_t DecodeMorton2Y(uint32_t code)
uint32_t DecodeMorton2X(uint32_t code)
Definition: mortoncodes.cpp:98
uint32_t Part1By2(uint32_t x)
Definition: mortoncodes.cpp:48
uint32_t DecodeMorton3X(uint32_t code)
uint32_t Compact1By1(uint32_t x)
Definition: mortoncodes.cpp:61
uint32_t EncodeMorton3(uint32_t x, uint32_t y, uint32_t z)
Definition: mortoncodes.cpp:92
uint32_t Part1By1(uint32_t x)
Definition: mortoncodes.cpp:35
Implements Morton Codes https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ http://www....