Skip to content

Commit b49a9d5

Browse files
authored
Merge pull request #95 from Hendrik-code/94
fixed binary operator
2 parents 6b50d2c + 9987054 commit b49a9d5

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

TPTBox/core/nii_wrapper_math.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __lshift__(self,p2):
8181
def __rshift__(self,p2):
8282
return self._binary_opt(p2,operator.rshift)
8383
def __and__(self,p2):
84-
return self._binary_opt(p2,operator.add)
84+
return self._binary_opt(p2,operator.and_)
8585
def __or__(self,p2):
8686
return self._binary_opt(p2,operator.or_)
8787
def __xor__(self,p2):

TPTBox/core/poi_fun/ray_casting.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,71 @@ def unit_vector(vector):
2121
return vector / np.linalg.norm(vector)
2222

2323

24+
# @njit(fastmath=True)
25+
def trilinear_interpolate(volume, x, y, z):
26+
xi, yi, zi = int(x), int(y), int(z)
27+
if xi < 0 or yi < 0 or zi < 0 or xi >= volume.shape[0] - 1 or yi >= volume.shape[1] - 1 or zi >= volume.shape[2] - 1:
28+
return 0.0
29+
30+
xd, yd, zd = x - xi, y - yi, z - zi
31+
c000 = volume[xi, yi, zi]
32+
c100 = volume[xi + 1, yi, zi]
33+
c010 = volume[xi, yi + 1, zi]
34+
c110 = volume[xi + 1, yi + 1, zi]
35+
c001 = volume[xi, yi, zi + 1]
36+
c101 = volume[xi + 1, yi, zi + 1]
37+
c011 = volume[xi, yi + 1, zi + 1]
38+
c111 = volume[xi + 1, yi + 1, zi + 1]
39+
40+
c00 = c000 * (1 - xd) + c100 * xd
41+
c01 = c001 * (1 - xd) + c101 * xd
42+
c10 = c010 * (1 - xd) + c110 * xd
43+
c11 = c011 * (1 - xd) + c111 * xd
44+
c0 = c00 * (1 - yd) + c10 * yd
45+
c1 = c01 * (1 - yd) + c11 * yd
46+
return c0 * (1 - zd) + c1 * zd
47+
48+
49+
# @njit(fastmath=True)
50+
def max_distance_ray_cast_convex_npfast(
51+
region_array: np.ndarray,
52+
start_coord: np.ndarray,
53+
direction_vector: np.ndarray,
54+
acc_delta=0.05,
55+
):
56+
# Normalize direction
57+
norm_vec = direction_vector / np.sqrt((direction_vector**2).sum())
58+
59+
# Quick exit if start point is outside
60+
if trilinear_interpolate(region_array, *start_coord) <= 0.5:
61+
return np.array(start_coord)
62+
63+
min_v = 0.0
64+
max_v = np.sum(region_array.shape)
65+
delta = max_v - min_v
66+
67+
while delta > acc_delta:
68+
mid = 0.5 * (max_v + min_v)
69+
x = start_coord[0] + norm_vec[0] * mid
70+
y = start_coord[1] + norm_vec[1] * mid
71+
z = start_coord[2] + norm_vec[2] * mid
72+
val = trilinear_interpolate(region_array, x, y, z)
73+
if val > 0.5:
74+
min_v = mid
75+
else:
76+
max_v = mid
77+
delta = max_v - min_v
78+
79+
dist = 0.5 * (min_v + max_v)
80+
return np.array(
81+
[
82+
start_coord[0] + norm_vec[0] * dist,
83+
start_coord[1] + norm_vec[1] * dist,
84+
start_coord[2] + norm_vec[2] * dist,
85+
]
86+
)
87+
88+
2489
def max_distance_ray_cast_convex(
2590
region: NII,
2691
start_coord: COORDINATE | np.ndarray,

0 commit comments

Comments
 (0)