-
Notifications
You must be signed in to change notification settings - Fork 11
/
gilbert_d2xyz.py
executable file
·221 lines (185 loc) · 8.54 KB
/
gilbert_d2xyz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2024 abetusk
def gilbert_d2xyz(idx, width, height, depth):
"""
Generalized Hilbert ('Gilbert') space-filling curve for arbitrary-sized
3D rectangular grids. Generates discrete 3D coordinates to fill a cuboid
of size (width x height x depth). Even sizes are recommended in 3D.
"""
if width >= height and width >= depth:
return gilbert_d2xyz_r(idx, 0,
0, 0, 0,
width, 0, 0,
0, height, 0,
0, 0, depth)
elif height >= width and height >= depth:
return gilbert_d2xyz_r(idx, 0,
0, 0, 0,
0, height, 0,
width, 0, 0,
0, 0, depth)
else: # depth >= width and depth >= height
return gilbert_d2xyz_r(idx, 0,
0, 0, 0,
0, 0, depth,
width, 0, 0,
0, height, 0)
def sgn(x):
return -1 if x < 0 else (1 if x > 0 else 0)
def gilbert_d2xyz_r(dst_idx, cur_idx,
x, y, z,
ax, ay, az,
bx, by, bz,
cx, cy, cz):
w = abs(ax + ay + az)
h = abs(bx + by + bz)
d = abs(cx + cy + cz)
(dax, day, daz) = (sgn(ax), sgn(ay), sgn(az)) # unit major direction ("right")
(dbx, dby, dbz) = (sgn(bx), sgn(by), sgn(bz)) # unit ortho direction ("forward")
(dcx, dcy, dcz) = (sgn(cx), sgn(cy), sgn(cz)) # unit ortho direction ("up")
_dx = dax + dbx + dcx
_dy = day + dby + dcy
_dz = daz + dbz + dcz
_di = dst_idx - cur_idx
# trivial row/column fills
if h == 1 and d == 1:
return (x + dax*_di, y + day*_di, z + daz*_di)
if w == 1 and d == 1:
return (x + dbx*_di, y + dby*_di, z + dbz*_di)
if w == 1 and h == 1:
return (x + dcx*_di, y + dcy*_di, z + dcz*_di)
(ax2, ay2, az2) = (ax//2, ay//2, az//2)
(bx2, by2, bz2) = (bx//2, by//2, bz//2)
(cx2, cy2, cz2) = (cx//2, cy//2, cz//2)
w2 = abs(ax2 + ay2 + az2)
h2 = abs(bx2 + by2 + bz2)
d2 = abs(cx2 + cy2 + cz2)
# prefer even steps
if (w2 % 2) and (w > 2): (ax2, ay2, az2) = (ax2 + dax, ay2 + day, az2 + daz)
if (h2 % 2) and (h > 2): (bx2, by2, bz2) = (bx2 + dbx, by2 + dby, bz2 + dbz)
if (d2 % 2) and (d > 2): (cx2, cy2, cz2) = (cx2 + dcx, cy2 + dcy, cz2 + dcz)
# wide case, split in w only
if (2*w > 3*h) and (2*w > 3*d):
nxt_idx = cur_idx + abs( (ax2 + ay2 + az2)*(bx + by + bz)*(cx + cy + cz) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x, y, z,
ax2, ay2, az2,
bx, by, bz,
cx, cy, cz)
cur_idx = nxt_idx
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+ax2, y+ay2, z+az2,
ax-ax2, ay-ay2, az-az2,
bx, by, bz,
cx, cy, cz)
# do not split in d
elif 3*h > 4*d:
nxt_idx = cur_idx + abs( (bx2 + by2 + bz2)*(cx + cy + cz)*(ax2 + ay2 + az2) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x, y, z,
bx2, by2, bz2,
cx, cy, cz,
ax2, ay2, az2)
cur_idx = nxt_idx
nxt_idx = cur_idx + abs( (ax + ay + az)*((bx - bx2) + (by - by2) + (bz - bz2))*(cx + cy + cz) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+bx2, y+by2, z+bz2,
ax, ay, az,
bx-bx2, by-by2, bz-bz2,
cx, cy, cz)
cur_idx = nxt_idx
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+(ax-dax)+(bx2-dbx),
y+(ay-day)+(by2-dby),
z+(az-daz)+(bz2-dbz),
-bx2, -by2, -bz2,
cx, cy, cz,
-(ax-ax2), -(ay-ay2), -(az-az2))
# do not split in h
elif 3*d > 4*h:
nxt_idx = cur_idx + abs( (cx2 + cy2 + cz2)*(ax2 + ay2 + az2)*(bx + by + bz) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x, y, z,
cx2, cy2, cz2,
ax2, ay2, az2,
bx, by, bz)
cur_idx = nxt_idx
nxt_idx = cur_idx + abs( (ax + ay + az)*(bx + by + bz)*((cx - cx2) + (cy - cy2) + (cz - cz2)) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+cx2, y+cy2, z+cz2,
ax, ay, az,
bx, by, bz,
cx-cx2, cy-cy2, cz-cz2)
cur_idx = nxt_idx
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+(ax-dax)+(cx2-dcx),
y+(ay-day)+(cy2-dcy),
z+(az-daz)+(cz2-dcz),
-cx2, -cy2, -cz2,
-(ax-ax2), -(ay-ay2), -(az-az2),
bx, by, bz)
# regular case, split in all w/h/d
else:
nxt_idx = cur_idx + abs( (bx2 + by2 + bz2)*(cx2 + cy2 + cz2)*(ax2 + ay2 + az2) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x, y, z,
bx2, by2, bz2,
cx2, cy2, cz2,
ax2, ay2, az2)
cur_idx = nxt_idx
nxt_idx = cur_idx + abs( (cx + cy + cz)*(ax2 + ay2 + az2)*((bx - bx2) + (by - by2) + (bz - bz2)) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+bx2, y+by2, z+bz2,
cx, cy, cz,
ax2, ay2, az2,
bx-bx2, by-by2, bz-bz2)
cur_idx = nxt_idx
nxt_idx = cur_idx + abs( (ax + ay + az)*(-bx2 - by2 - bz2)*(-(cx - cx2) - (cy - cy2) - (cz - cz2)) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx, cur_idx,
x+(bx2-dbx)+(cx-dcx),
y+(by2-dby)+(cy-dcy),
z+(bz2-dbz)+(cz-dcz),
ax, ay, az,
-bx2, -by2, -bz2,
-(cx-cx2), -(cy-cy2), -(cz-cz2))
cur_idx = nxt_idx
nxt_idx = cur_idx + abs( (-cx - cy - cz)*(-(ax - ax2) - (ay - ay2) - (az - az2))*((bx - bx2) + (by - by2) + (bz - bz2)) )
if (cur_idx <= dst_idx) and (dst_idx < nxt_idx):
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+(ax-dax)+bx2+(cx-dcx),
y+(ay-day)+by2+(cy-dcy),
z+(az-daz)+bz2+(cz-dcz),
-cx, -cy, -cz,
-(ax-ax2), -(ay-ay2), -(az-az2),
bx-bx2, by-by2, bz-bz2)
cur_idx = nxt_idx
return gilbert_d2xyz_r(dst_idx,cur_idx,
x+(ax-dax)+(bx2-dbx),
y+(ay-day)+(by2-dby),
z+(az-daz)+(bz2-dbz),
-bx2, -by2, -bz2,
cx2, cy2, cz2,
-(ax-ax2), -(ay-ay2), -(az-az2))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('width', type=int)
parser.add_argument('height', type=int)
parser.add_argument('depth', type=int)
args = parser.parse_args()
w = args.width
h = args.height
d = args.depth
n = w*h*d
for idx in range(n):
(x,y,z) = gilbert_d2xyz(idx,w,h,d)
print(x,y,z)