KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
26
27#include "mortoncodes.h"
28
29
30// "Insert" a 0 bit after each of the 16 low bits of x
31uint32_t Part1By1( uint32_t x )
32{
33 x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
34 x = ( x ^ ( x << 8 ) ) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
35 x = ( x ^ ( x << 4 ) ) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
36 x = ( x ^ ( x << 2 ) ) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
37 x = ( x ^ ( x << 1 ) ) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
38
39 return x;
40}
41
42
43// "Insert" two 0 bits after each of the 10 low bits of x
44uint32_t Part1By2( uint32_t x )
45{
46 x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
47 x = ( x ^ ( x << 16 ) ) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
48 x = ( x ^ ( x << 8 ) ) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
49 x = ( x ^ ( x << 4 ) ) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
50 x = ( x ^ ( x << 2 ) ) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
51
52 return x;
53}
54
55
56// Inverse of Part1By1 - "delete" all odd-indexed bits
57uint32_t Compact1By1( uint32_t x )
58{
59 x &= 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
60 x = ( x ^ ( x >> 1 ) ) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
61 x = ( x ^ ( x >> 2 ) ) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
62 x = ( x ^ ( x >> 4 ) ) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
63 x = ( x ^ ( x >> 8 ) ) & 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
64
65 return x;
66}
67
68
69// Inverse of Part1By2 - "delete" all bits not at positions divisible by 3
70uint32_t Compact1By2( uint32_t x )
71{
72 x &= 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
73 x = ( x ^ ( x >> 2 ) ) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
74 x = ( x ^ ( x >> 4 ) ) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
75 x = ( x ^ ( x >> 8 ) ) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
76 x = ( x ^ ( x >> 16 ) ) & 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
77
78 return x;
79}
80
81
82uint32_t EncodeMorton2( uint32_t x, uint32_t y )
83{
84 return ( Part1By1( y ) << 1 ) + Part1By1( x );
85}
86
87
88uint32_t EncodeMorton3( uint32_t x, uint32_t y, uint32_t z )
89{
90 return ( Part1By2( z ) << 2 ) + ( Part1By2( y ) << 1 ) + Part1By2( x );
91}
92
93
94uint32_t DecodeMorton2X( uint32_t code )
95{
96 return Compact1By1( code >> 0 );
97}
98
99
100uint32_t DecodeMorton2Y( uint32_t code )
101{
102 return Compact1By1( code >> 1 );
103}
104
105
106uint32_t DecodeMorton3X( uint32_t code )
107{
108 return Compact1By2( code >> 0 );
109}
110
111
112uint32_t DecodeMorton3Y( uint32_t code )
113{
114 return Compact1By2( code >> 1 );
115}
116
117
118uint32_t DecodeMorton3Z( uint32_t code )
119{
120 return Compact1By2( code >> 2 );
121}
uint32_t Compact1By2(uint32_t x)
uint32_t DecodeMorton3Z(uint32_t code)
uint32_t DecodeMorton3Y(uint32_t code)
uint32_t EncodeMorton2(uint32_t x, uint32_t y)
uint32_t DecodeMorton2Y(uint32_t code)
uint32_t DecodeMorton2X(uint32_t code)
uint32_t Part1By2(uint32_t x)
uint32_t DecodeMorton3X(uint32_t code)
uint32_t Compact1By1(uint32_t x)
uint32_t EncodeMorton3(uint32_t x, uint32_t y, uint32_t z)
uint32_t Part1By1(uint32_t x)
Implements Morton Codes https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ http://www....