@@ -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+
2489def max_distance_ray_cast_convex (
2590 region : NII ,
2691 start_coord : COORDINATE | np .ndarray ,
0 commit comments