forked from orzzzjq/Parallel-Banding-Algorithm-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
169 lines (129 loc) · 4.87 KB
/
main.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
import os, sys
os.add_dll_directory(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin")
sys.path.append(r'C:\Users\PRIA\source\repos\Parallel-Banding-Algorithm-plus-py\x64\Debug')
import pba2d
import pba3d
import numpy as np
def pytorch():
# conditional import because it takes long
import torch
cuda0 = torch.device('cuda:0')
tensor = torch.tensor([0, 0], dtype=torch.short, device=cuda0)
print(tensor)
def voronoi2d():
points = np.array([
[0, 1],
[2047, 2047],
])
# empty input array
arr = np.full([2048, 2048, 2], pba2d.MARKER, dtype=np.short)
# put the points at their positions
arr[points[:, 1], points[:, 0]] = points
print(arr)
# Compute 2D Voronoi diagram
# Input: a 2D texture. Each pixel is represented as two "short" integer.
# For each site at (x, y), the pixel at coordinate (x, y) should contain
# the pair (x, y). Pixels that are not sites should contain the pair (MARKER, MARKER)
# See original paper for the effect of the three parameters: m1, m2, m3
# Parameters must divide textureSize
pba2d.voronoi(arr, 32, 32, 2)
print(arr)
def distance2d():
points = np.array([
[0, 1],
[2047, 2047],
])
# empty input array
arrIn = np.full([2048, 2048, 2], pba2d.MARKER, dtype=np.short)
# put the points at their positions
arrIn[points[:, 1], points[:, 0]] = points
arrOut = np.full(arrIn.shape[:-1], 0, dtype=np.float32)
# Compute 2D Voronoi diagram
# Input: a 2D texture. Each pixel is represented as two "short" integer.
# For each site at (x, y), the pixel at coordinate (x, y) should contain
# the pair (x, y). Pixels that are not sites should contain the pair (MARKER, MARKER)
# See original paper for the effect of the three parameters: m1, m2, m3
# Parameters must divide textureSize
pba2d.distance(arrIn, arrOut, 32, 32, 2)
print(arrOut)
def voronoi3d(omega):
""" omega is np 3d-array """
def encoded(p):
return np.left_shift(p[..., 0], 20) | np.left_shift(p[..., 1], 10) | p[..., 2]
def decoded(p):
return np.stack([np.right_shift(p, 20) & 1023, np.right_shift(p, 10) & 1023, p & 1023], axis=-1)
"""points = np.array([
[0, 1, 2],
[511, 511, 511],
])"""
points = np.transpose(np.where(omega))
# empty input array
arr = np.full(omega.shape, pba3d.MARKER, dtype=int)
# put the points at their positions
arr[points[:, 2], points[:, 1], points[:, 0]] = encoded(points)
# cuda0 = torch.device('cuda:0')
# tensor = torch.tensor([0, 0], dtype=torch.short, device=cuda0)
print(points)
print(arr)
# print(tensor)
# Compute 3D Voronoi diagram
# Input: a 3D texture. Each pixel is an integer encoding 3 coordinates.
# For each site at (x, y, z), the pixel at coordinate (x, y, z) should contain
# the encoded coordinate (x, y, z). Pixels that are not sites should contain
# the integer MARKER. Use ENCODE (and DECODE) macro to encode (and decode).
# See our website for the effect of the three parameters:
# phase1Band, phase2Band, phase3Band
# Parameters must divide textureSize
pba3d.voronoi(arr, 1, 1, 2)
print(arr)
return arr
def distance3d():
""" omega is np 3d-array """
def encoded(p):
return np.left_shift(p[..., 0], 20) | np.left_shift(p[..., 1], 10) | p[..., 2]
def decoded(p):
return np.stack([np.right_shift(p, 20) & 1023, np.right_shift(p, 10) & 1023, p & 1023], axis=-1)
points = np.array([
[0, 1, 2],
[511, 511, 511],
])
# points = np.transpose(np.where(omega))
"""
# empty input array
arrIn = np.full([512, 512, 512], pba3d.MARKER, dtype=int)
# put the points at their positions
arrIn[points[:, 2], points[:, 1], points[:, 0]] = encoded(points)
arrOut = np.full(arrIn.shape, 0, dtype=np.float32)
# cuda0 = torch.device('cuda:0')
# tensor = torch.tensor([0, 0], dtype=torch.short, device=cuda0)
# print(points)
# print(arrIn)
# print(tensor)
# Compute 3D Voronoi diagram
# Input: a 3D texture. Each pixel is an integer encoding 3 coordinates.
# For each site at (x, y, z), the pixel at coordinate (x, y, z) should contain
# the encoded coordinate (x, y, z). Pixels that are not sites should contain
# the integer MARKER. Use ENCODE (and DECODE) macro to encode (and decode).
# See our website for the effect of the three parameters:
# phase1Band, phase2Band, phase3Band
# Parameters must divide textureSize
"""
arrIn = np.full([512, 512, 512], False, dtype=bool)
arrIn[points[:, 2], points[:, 1], points[:, 0]] = True
arrOut = np.full(arrIn.shape, 0, dtype=np.float32)
print(arrIn)
pba3d.distance(arrIn, arrOut, 1, 1, 2)
print(arrOut)
def voronoi_to_dist(voronoi):
""" voronoi is encoded """
def decoded_nonstacked(p):
return np.right_shift(p, 20) & 1023, np.right_shift(p, 10) & 1023, p & 1023
x_i, y_i, z_i = np.indices(voronoi.shape)
x_v, y_v, z_v = decoded_nonstacked(voronoi)
return np.sqrt((x_v - x_i) ** 2 + (y_v - y_i) ** 2 + (z_v - z_i) ** 2)
"""
arr = np.load("test.npy")
dist = voronoi_to_dist(arr)
np.save("out.npy", dist)
"""
distance3d()