diffct-mlx 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffct_mlx/__about__.py +3 -0
- diffct_mlx/__init__.py +223 -0
- diffct_mlx/constants.py +38 -0
- diffct_mlx/geometry.py +802 -0
- diffct_mlx/kernels/__init__.py +39 -0
- diffct_mlx/kernels/cone_beam.py +708 -0
- diffct_mlx/kernels/fan_beam.py +502 -0
- diffct_mlx/kernels/parallel_beam.py +431 -0
- diffct_mlx/phantoms/__init__.py +5 -0
- diffct_mlx/phantoms/shepp_logan.py +118 -0
- diffct_mlx/projectors.py +1084 -0
- diffct_mlx/real_measured_data_helper.py +601 -0
- diffct_mlx/reconstruction_algorithms/__init__.py +85 -0
- diffct_mlx/reconstruction_algorithms/_analytic.py +101 -0
- diffct_mlx/reconstruction_algorithms/_core.py +872 -0
- diffct_mlx/reconstruction_algorithms/asd_pocs.py +114 -0
- diffct_mlx/reconstruction_algorithms/awtv_pocs.py +106 -0
- diffct_mlx/reconstruction_algorithms/cases.py +988 -0
- diffct_mlx/reconstruction_algorithms/dart.py +424 -0
- diffct_mlx/reconstruction_algorithms/fbp.py +69 -0
- diffct_mlx/reconstruction_algorithms/fdk.py +67 -0
- diffct_mlx/reconstruction_algorithms/sart.py +66 -0
- diffct_mlx/reconstruction_algorithms/sirt.py +64 -0
- diffct_mlx/reconstruction_algorithms/tv_pocs.py +85 -0
- diffct_mlx/regularizers.py +122 -0
- diffct_mlx/tv_gradients.py +74 -0
- diffct_mlx/utils.py +64 -0
- diffct_mlx-1.0.1.dist-info/METADATA +315 -0
- diffct_mlx-1.0.1.dist-info/RECORD +31 -0
- diffct_mlx-1.0.1.dist-info/WHEEL +4 -0
- diffct_mlx-1.0.1.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
"""Metal kernels for 3D cone beam projections.
|
|
2
|
+
|
|
3
|
+
This module contains Metal kernel source strings implementing the Siddon
|
|
4
|
+
ray-tracing method for 3D cone beam forward projection and backprojection,
|
|
5
|
+
optimized for Apple Silicon GPUs.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import mlx.core as mx
|
|
9
|
+
|
|
10
|
+
# ---------------------------------------------------------------------------
|
|
11
|
+
# Constants for Metal kernels
|
|
12
|
+
# ---------------------------------------------------------------------------
|
|
13
|
+
_EPSILON_STR = "1e-6f"
|
|
14
|
+
_INF_STR = "1e30f"
|
|
15
|
+
|
|
16
|
+
# ============================================================================
|
|
17
|
+
# 3D Cone Beam Forward Projection Metal Kernel
|
|
18
|
+
# ============================================================================
|
|
19
|
+
|
|
20
|
+
_CONE_3D_FORWARD_SOURCE = """
|
|
21
|
+
// Thread position: (iview, iu, iv)
|
|
22
|
+
uint iview = thread_position_in_grid.x;
|
|
23
|
+
uint iu = thread_position_in_grid.y;
|
|
24
|
+
uint iv = thread_position_in_grid.z;
|
|
25
|
+
|
|
26
|
+
// Read scalar parameters
|
|
27
|
+
int n_views = params[0];
|
|
28
|
+
int n_u = params[1];
|
|
29
|
+
int n_v = params[2];
|
|
30
|
+
int Nx = params[3];
|
|
31
|
+
int Ny = params[4];
|
|
32
|
+
int Nz = params[5];
|
|
33
|
+
|
|
34
|
+
float du = fparams[0];
|
|
35
|
+
float dv = fparams[1];
|
|
36
|
+
float cx = fparams[2];
|
|
37
|
+
float cy = fparams[3];
|
|
38
|
+
float cz = fparams[4];
|
|
39
|
+
float voxel_spacing = fparams[5];
|
|
40
|
+
|
|
41
|
+
if ((int)iview >= n_views || (int)iu >= n_u || (int)iv >= n_v) return;
|
|
42
|
+
|
|
43
|
+
float eps = """ + _EPSILON_STR + """;
|
|
44
|
+
|
|
45
|
+
// === 3D CONE BEAM GEOMETRY SETUP ===
|
|
46
|
+
float src_x = src_pos[iview * 3 + 0] / voxel_spacing;
|
|
47
|
+
float src_y = src_pos[iview * 3 + 1] / voxel_spacing;
|
|
48
|
+
float src_z = src_pos[iview * 3 + 2] / voxel_spacing;
|
|
49
|
+
|
|
50
|
+
float det_cx = det_center[iview * 3 + 0] / voxel_spacing;
|
|
51
|
+
float det_cy = det_center[iview * 3 + 1] / voxel_spacing;
|
|
52
|
+
float det_cz = det_center[iview * 3 + 2] / voxel_spacing;
|
|
53
|
+
|
|
54
|
+
float u_vec_x = det_u_vec_arr[iview * 3 + 0];
|
|
55
|
+
float u_vec_y = det_u_vec_arr[iview * 3 + 1];
|
|
56
|
+
float u_vec_z = det_u_vec_arr[iview * 3 + 2];
|
|
57
|
+
|
|
58
|
+
float v_vec_x = det_v_vec_arr[iview * 3 + 0];
|
|
59
|
+
float v_vec_y = det_v_vec_arr[iview * 3 + 1];
|
|
60
|
+
float v_vec_z = det_v_vec_arr[iview * 3 + 2];
|
|
61
|
+
|
|
62
|
+
float u_offset = ((float)iu - ((float)n_u - 1.0f) * 0.5f) * du / voxel_spacing;
|
|
63
|
+
float v_offset = ((float)iv - ((float)n_v - 1.0f) * 0.5f) * dv / voxel_spacing;
|
|
64
|
+
|
|
65
|
+
float det_x = det_cx + u_offset * u_vec_x + v_offset * v_vec_x;
|
|
66
|
+
float det_y = det_cy + u_offset * u_vec_y + v_offset * v_vec_y;
|
|
67
|
+
float det_z = det_cz + u_offset * u_vec_z + v_offset * v_vec_z;
|
|
68
|
+
|
|
69
|
+
// === 3D RAY DIRECTION ===
|
|
70
|
+
float dir_x = det_x - src_x;
|
|
71
|
+
float dir_y = det_y - src_y;
|
|
72
|
+
float dir_z = det_z - src_z;
|
|
73
|
+
float length = metal::sqrt(dir_x*dir_x + dir_y*dir_y + dir_z*dir_z);
|
|
74
|
+
if (length < eps) {
|
|
75
|
+
sino[iview * n_u * n_v + iu * n_v + iv] = 0.0f;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
float inv_len = 1.0f / length;
|
|
79
|
+
dir_x *= inv_len;
|
|
80
|
+
dir_y *= inv_len;
|
|
81
|
+
dir_z *= inv_len;
|
|
82
|
+
|
|
83
|
+
// === 3D RAY-VOLUME INTERSECTION ===
|
|
84
|
+
float t_min = -""" + _INF_STR + """;
|
|
85
|
+
float t_max = """ + _INF_STR + """;
|
|
86
|
+
|
|
87
|
+
if (metal::abs(dir_x) > eps) {
|
|
88
|
+
float tx1 = (-cx - src_x) / dir_x;
|
|
89
|
+
float tx2 = ( cx - src_x) / dir_x;
|
|
90
|
+
t_min = metal::max(t_min, metal::min(tx1, tx2));
|
|
91
|
+
t_max = metal::min(t_max, metal::max(tx1, tx2));
|
|
92
|
+
} else if (src_x < -cx || src_x > cx) {
|
|
93
|
+
sino[iview * n_u * n_v + iu * n_v + iv] = 0.0f;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (metal::abs(dir_y) > eps) {
|
|
98
|
+
float ty1 = (-cy - src_y) / dir_y;
|
|
99
|
+
float ty2 = ( cy - src_y) / dir_y;
|
|
100
|
+
t_min = metal::max(t_min, metal::min(ty1, ty2));
|
|
101
|
+
t_max = metal::min(t_max, metal::max(ty1, ty2));
|
|
102
|
+
} else if (src_y < -cy || src_y > cy) {
|
|
103
|
+
sino[iview * n_u * n_v + iu * n_v + iv] = 0.0f;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (metal::abs(dir_z) > eps) {
|
|
108
|
+
float tz1 = (-cz - src_z) / dir_z;
|
|
109
|
+
float tz2 = ( cz - src_z) / dir_z;
|
|
110
|
+
t_min = metal::max(t_min, metal::min(tz1, tz2));
|
|
111
|
+
t_max = metal::min(t_max, metal::max(tz1, tz2));
|
|
112
|
+
} else if (src_z < -cz || src_z > cz) {
|
|
113
|
+
sino[iview * n_u * n_v + iu * n_v + iv] = 0.0f;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (t_min >= t_max) {
|
|
118
|
+
sino[iview * n_u * n_v + iu * n_v + iv] = 0.0f;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// === 3D SIDDON TRAVERSAL ===
|
|
123
|
+
float accum = 0.0f;
|
|
124
|
+
float t = t_min;
|
|
125
|
+
|
|
126
|
+
int ix = (int)metal::floor(src_x + t * dir_x + cx);
|
|
127
|
+
int iy = (int)metal::floor(src_y + t * dir_y + cy);
|
|
128
|
+
int iz = (int)metal::floor(src_z + t * dir_z + cz);
|
|
129
|
+
|
|
130
|
+
int step_x = (dir_x >= 0.0f) ? 1 : -1;
|
|
131
|
+
int step_y = (dir_y >= 0.0f) ? 1 : -1;
|
|
132
|
+
int step_z = (dir_z >= 0.0f) ? 1 : -1;
|
|
133
|
+
|
|
134
|
+
float inv_dir_x = (metal::abs(dir_x) > eps) ? (1.0f / dir_x) : 0.0f;
|
|
135
|
+
float inv_dir_y = (metal::abs(dir_y) > eps) ? (1.0f / dir_y) : 0.0f;
|
|
136
|
+
float inv_dir_z = (metal::abs(dir_z) > eps) ? (1.0f / dir_z) : 0.0f;
|
|
137
|
+
float dt_x = (metal::abs(dir_x) > eps) ? metal::abs(inv_dir_x) : """ + _INF_STR + """;
|
|
138
|
+
float dt_y = (metal::abs(dir_y) > eps) ? metal::abs(inv_dir_y) : """ + _INF_STR + """;
|
|
139
|
+
float dt_z = (metal::abs(dir_z) > eps) ? metal::abs(inv_dir_z) : """ + _INF_STR + """;
|
|
140
|
+
|
|
141
|
+
float txn = (metal::abs(dir_x) > eps) ? ((float)(ix + (step_x > 0 ? 1 : 0)) - cx - src_x) * inv_dir_x : """ + _INF_STR + """;
|
|
142
|
+
float tyn = (metal::abs(dir_y) > eps) ? ((float)(iy + (step_y > 0 ? 1 : 0)) - cy - src_y) * inv_dir_y : """ + _INF_STR + """;
|
|
143
|
+
float tzn = (metal::abs(dir_z) > eps) ? ((float)(iz + (step_z > 0 ? 1 : 0)) - cz - src_z) * inv_dir_z : """ + _INF_STR + """;
|
|
144
|
+
|
|
145
|
+
while (t < t_max) {
|
|
146
|
+
if (ix >= 0 && ix < Nx && iy >= 0 && iy < Ny && iz >= 0 && iz < Nz) {
|
|
147
|
+
float t_next = metal::min(metal::min(metal::min(txn, tyn), tzn), t_max);
|
|
148
|
+
float seg_len = t_next - t;
|
|
149
|
+
|
|
150
|
+
if (seg_len > eps) {
|
|
151
|
+
// Trilinear interpolation at midpoint
|
|
152
|
+
float t_mid = t + seg_len * 0.5f;
|
|
153
|
+
float mid_x = src_x + t_mid * dir_x + cx;
|
|
154
|
+
float mid_y = src_y + t_mid * dir_y + cy;
|
|
155
|
+
float mid_z = src_z + t_mid * dir_z + cz;
|
|
156
|
+
|
|
157
|
+
int ix0 = (int)metal::floor(mid_x);
|
|
158
|
+
int iy0 = (int)metal::floor(mid_y);
|
|
159
|
+
int iz0 = (int)metal::floor(mid_z);
|
|
160
|
+
float dx = mid_x - (float)ix0;
|
|
161
|
+
float dy = mid_y - (float)iy0;
|
|
162
|
+
float dz = mid_z - (float)iz0;
|
|
163
|
+
|
|
164
|
+
ix0 = metal::max(0, metal::min(ix0, Nx - 2));
|
|
165
|
+
iy0 = metal::max(0, metal::min(iy0, Ny - 2));
|
|
166
|
+
iz0 = metal::max(0, metal::min(iz0, Nz - 2));
|
|
167
|
+
|
|
168
|
+
float omdx = 1.0f - dx;
|
|
169
|
+
float omdy = 1.0f - dy;
|
|
170
|
+
float omdz = 1.0f - dz;
|
|
171
|
+
|
|
172
|
+
// Volume layout: (Nx, Ny, Nz) = WHD permuted
|
|
173
|
+
int base = ix0 * Ny * Nz + iy0 * Nz + iz0;
|
|
174
|
+
int yz_stride = Ny * Nz;
|
|
175
|
+
|
|
176
|
+
float val = vol[base] * omdx * omdy * omdz +
|
|
177
|
+
vol[base + yz_stride] * dx * omdy * omdz +
|
|
178
|
+
vol[base + Nz] * omdx * dy * omdz +
|
|
179
|
+
vol[base + 1] * omdx * omdy * dz +
|
|
180
|
+
vol[base + yz_stride + Nz] * dx * dy * omdz +
|
|
181
|
+
vol[base + yz_stride + 1] * dx * omdy * dz +
|
|
182
|
+
vol[base + Nz + 1] * omdx * dy * dz +
|
|
183
|
+
vol[base + yz_stride + Nz + 1] * dx * dy * dz;
|
|
184
|
+
|
|
185
|
+
accum += val * seg_len * voxel_spacing;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 3D boundary crossing
|
|
190
|
+
if (txn <= tyn && txn <= tzn) {
|
|
191
|
+
t = txn;
|
|
192
|
+
ix += step_x;
|
|
193
|
+
txn += dt_x;
|
|
194
|
+
} else if (tyn <= txn && tyn <= tzn) {
|
|
195
|
+
t = tyn;
|
|
196
|
+
iy += step_y;
|
|
197
|
+
tyn += dt_y;
|
|
198
|
+
} else {
|
|
199
|
+
t = tzn;
|
|
200
|
+
iz += step_z;
|
|
201
|
+
tzn += dt_z;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
sino[iview * n_u * n_v + iu * n_v + iv] = accum;
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
cone_3d_forward_kernel = mx.fast.metal_kernel(
|
|
209
|
+
name="cone_3d_forward",
|
|
210
|
+
input_names=["vol", "src_pos", "det_center", "det_u_vec_arr", "det_v_vec_arr", "params", "fparams"],
|
|
211
|
+
output_names=["sino"],
|
|
212
|
+
source=_CONE_3D_FORWARD_SOURCE,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# ============================================================================
|
|
216
|
+
# 3D Cone Beam Backprojection Metal Kernel
|
|
217
|
+
# ============================================================================
|
|
218
|
+
|
|
219
|
+
_CONE_3D_BACKWARD_SOURCE = """
|
|
220
|
+
// Thread position: (iview, iu, iv)
|
|
221
|
+
uint iview = thread_position_in_grid.x;
|
|
222
|
+
uint iu = thread_position_in_grid.y;
|
|
223
|
+
uint iv = thread_position_in_grid.z;
|
|
224
|
+
|
|
225
|
+
// Read scalar parameters
|
|
226
|
+
int n_views = params[0];
|
|
227
|
+
int n_u = params[1];
|
|
228
|
+
int n_v = params[2];
|
|
229
|
+
int Nx = params[3];
|
|
230
|
+
int Ny = params[4];
|
|
231
|
+
int Nz = params[5];
|
|
232
|
+
|
|
233
|
+
float du = fparams[0];
|
|
234
|
+
float dv = fparams[1];
|
|
235
|
+
float cx = fparams[2];
|
|
236
|
+
float cy = fparams[3];
|
|
237
|
+
float cz = fparams[4];
|
|
238
|
+
float voxel_spacing = fparams[5];
|
|
239
|
+
|
|
240
|
+
if ((int)iview >= n_views || (int)iu >= n_u || (int)iv >= n_v) return;
|
|
241
|
+
|
|
242
|
+
float eps = """ + _EPSILON_STR + """;
|
|
243
|
+
|
|
244
|
+
float g = sino[iview * n_u * n_v + iu * n_v + iv];
|
|
245
|
+
|
|
246
|
+
// === 3D CONE BEAM GEOMETRY SETUP ===
|
|
247
|
+
float src_x = src_pos[iview * 3 + 0] / voxel_spacing;
|
|
248
|
+
float src_y = src_pos[iview * 3 + 1] / voxel_spacing;
|
|
249
|
+
float src_z = src_pos[iview * 3 + 2] / voxel_spacing;
|
|
250
|
+
|
|
251
|
+
float det_cx = det_center_arr[iview * 3 + 0] / voxel_spacing;
|
|
252
|
+
float det_cy = det_center_arr[iview * 3 + 1] / voxel_spacing;
|
|
253
|
+
float det_cz = det_center_arr[iview * 3 + 2] / voxel_spacing;
|
|
254
|
+
|
|
255
|
+
float u_vec_x = det_u_vec_arr[iview * 3 + 0];
|
|
256
|
+
float u_vec_y = det_u_vec_arr[iview * 3 + 1];
|
|
257
|
+
float u_vec_z = det_u_vec_arr[iview * 3 + 2];
|
|
258
|
+
|
|
259
|
+
float v_vec_x = det_v_vec_arr[iview * 3 + 0];
|
|
260
|
+
float v_vec_y = det_v_vec_arr[iview * 3 + 1];
|
|
261
|
+
float v_vec_z = det_v_vec_arr[iview * 3 + 2];
|
|
262
|
+
|
|
263
|
+
float u_offset = ((float)iu - ((float)n_u - 1.0f) * 0.5f) * du / voxel_spacing;
|
|
264
|
+
float v_offset = ((float)iv - ((float)n_v - 1.0f) * 0.5f) * dv / voxel_spacing;
|
|
265
|
+
|
|
266
|
+
float det_x = det_cx + u_offset * u_vec_x + v_offset * v_vec_x;
|
|
267
|
+
float det_y = det_cy + u_offset * u_vec_y + v_offset * v_vec_y;
|
|
268
|
+
float det_z = det_cz + u_offset * u_vec_z + v_offset * v_vec_z;
|
|
269
|
+
|
|
270
|
+
// === 3D RAY DIRECTION ===
|
|
271
|
+
float dir_x = det_x - src_x;
|
|
272
|
+
float dir_y = det_y - src_y;
|
|
273
|
+
float dir_z = det_z - src_z;
|
|
274
|
+
float length = metal::sqrt(dir_x*dir_x + dir_y*dir_y + dir_z*dir_z);
|
|
275
|
+
if (length < eps) return;
|
|
276
|
+
float inv_len = 1.0f / length;
|
|
277
|
+
dir_x *= inv_len;
|
|
278
|
+
dir_y *= inv_len;
|
|
279
|
+
dir_z *= inv_len;
|
|
280
|
+
|
|
281
|
+
// === 3D RAY-VOLUME INTERSECTION ===
|
|
282
|
+
float t_min = -""" + _INF_STR + """;
|
|
283
|
+
float t_max = """ + _INF_STR + """;
|
|
284
|
+
|
|
285
|
+
if (metal::abs(dir_x) > eps) {
|
|
286
|
+
float tx1 = (-cx - src_x) / dir_x;
|
|
287
|
+
float tx2 = ( cx - src_x) / dir_x;
|
|
288
|
+
t_min = metal::max(t_min, metal::min(tx1, tx2));
|
|
289
|
+
t_max = metal::min(t_max, metal::max(tx1, tx2));
|
|
290
|
+
} else if (src_x < -cx || src_x > cx) { return; }
|
|
291
|
+
|
|
292
|
+
if (metal::abs(dir_y) > eps) {
|
|
293
|
+
float ty1 = (-cy - src_y) / dir_y;
|
|
294
|
+
float ty2 = ( cy - src_y) / dir_y;
|
|
295
|
+
t_min = metal::max(t_min, metal::min(ty1, ty2));
|
|
296
|
+
t_max = metal::min(t_max, metal::max(ty1, ty2));
|
|
297
|
+
} else if (src_y < -cy || src_y > cy) { return; }
|
|
298
|
+
|
|
299
|
+
if (metal::abs(dir_z) > eps) {
|
|
300
|
+
float tz1 = (-cz - src_z) / dir_z;
|
|
301
|
+
float tz2 = ( cz - src_z) / dir_z;
|
|
302
|
+
t_min = metal::max(t_min, metal::min(tz1, tz2));
|
|
303
|
+
t_max = metal::min(t_max, metal::max(tz1, tz2));
|
|
304
|
+
} else if (src_z < -cz || src_z > cz) { return; }
|
|
305
|
+
|
|
306
|
+
if (t_min >= t_max) return;
|
|
307
|
+
|
|
308
|
+
// === 3D SIDDON TRAVERSAL ===
|
|
309
|
+
float t = t_min;
|
|
310
|
+
int ix = (int)metal::floor(src_x + t * dir_x + cx);
|
|
311
|
+
int iy = (int)metal::floor(src_y + t * dir_y + cy);
|
|
312
|
+
int iz = (int)metal::floor(src_z + t * dir_z + cz);
|
|
313
|
+
|
|
314
|
+
int step_x = (dir_x >= 0.0f) ? 1 : -1;
|
|
315
|
+
int step_y = (dir_y >= 0.0f) ? 1 : -1;
|
|
316
|
+
int step_z = (dir_z >= 0.0f) ? 1 : -1;
|
|
317
|
+
|
|
318
|
+
float inv_dir_x = (metal::abs(dir_x) > eps) ? (1.0f / dir_x) : 0.0f;
|
|
319
|
+
float inv_dir_y = (metal::abs(dir_y) > eps) ? (1.0f / dir_y) : 0.0f;
|
|
320
|
+
float inv_dir_z = (metal::abs(dir_z) > eps) ? (1.0f / dir_z) : 0.0f;
|
|
321
|
+
float dt_x = (metal::abs(dir_x) > eps) ? metal::abs(inv_dir_x) : """ + _INF_STR + """;
|
|
322
|
+
float dt_y = (metal::abs(dir_y) > eps) ? metal::abs(inv_dir_y) : """ + _INF_STR + """;
|
|
323
|
+
float dt_z = (metal::abs(dir_z) > eps) ? metal::abs(inv_dir_z) : """ + _INF_STR + """;
|
|
324
|
+
|
|
325
|
+
float txn = (metal::abs(dir_x) > eps) ? ((float)(ix + (step_x > 0 ? 1 : 0)) - cx - src_x) * inv_dir_x : """ + _INF_STR + """;
|
|
326
|
+
float tyn = (metal::abs(dir_y) > eps) ? ((float)(iy + (step_y > 0 ? 1 : 0)) - cy - src_y) * inv_dir_y : """ + _INF_STR + """;
|
|
327
|
+
float tzn = (metal::abs(dir_z) > eps) ? ((float)(iz + (step_z > 0 ? 1 : 0)) - cz - src_z) * inv_dir_z : """ + _INF_STR + """;
|
|
328
|
+
|
|
329
|
+
int yz_stride = Ny * Nz;
|
|
330
|
+
|
|
331
|
+
while (t < t_max) {
|
|
332
|
+
if (ix >= 0 && ix < Nx && iy >= 0 && iy < Ny && iz >= 0 && iz < Nz) {
|
|
333
|
+
float t_next = metal::min(metal::min(metal::min(txn, tyn), tzn), t_max);
|
|
334
|
+
float seg_len = t_next - t;
|
|
335
|
+
|
|
336
|
+
if (seg_len > eps) {
|
|
337
|
+
float t_mid = t + seg_len * 0.5f;
|
|
338
|
+
float mid_x = src_x + t_mid * dir_x + cx;
|
|
339
|
+
float mid_y = src_y + t_mid * dir_y + cy;
|
|
340
|
+
float mid_z = src_z + t_mid * dir_z + cz;
|
|
341
|
+
|
|
342
|
+
int ix0 = (int)metal::floor(mid_x);
|
|
343
|
+
int iy0 = (int)metal::floor(mid_y);
|
|
344
|
+
int iz0 = (int)metal::floor(mid_z);
|
|
345
|
+
float dx = mid_x - (float)ix0;
|
|
346
|
+
float dy = mid_y - (float)iy0;
|
|
347
|
+
float dz = mid_z - (float)iz0;
|
|
348
|
+
|
|
349
|
+
ix0 = metal::max(0, metal::min(ix0, Nx - 2));
|
|
350
|
+
iy0 = metal::max(0, metal::min(iy0, Ny - 2));
|
|
351
|
+
iz0 = metal::max(0, metal::min(iz0, Nz - 2));
|
|
352
|
+
|
|
353
|
+
float omdx = 1.0f - dx;
|
|
354
|
+
float omdy = 1.0f - dy;
|
|
355
|
+
float omdz = 1.0f - dz;
|
|
356
|
+
float cval = g * seg_len;
|
|
357
|
+
|
|
358
|
+
int base = ix0 * yz_stride + iy0 * Nz + iz0;
|
|
359
|
+
|
|
360
|
+
// Atomic backprojection with trilinear weights
|
|
361
|
+
atomic_fetch_add_explicit(&grad_vol[base], cval * omdx * omdy * omdz, memory_order_relaxed);
|
|
362
|
+
atomic_fetch_add_explicit(&grad_vol[base + yz_stride], cval * dx * omdy * omdz, memory_order_relaxed);
|
|
363
|
+
atomic_fetch_add_explicit(&grad_vol[base + Nz], cval * omdx * dy * omdz, memory_order_relaxed);
|
|
364
|
+
atomic_fetch_add_explicit(&grad_vol[base + 1], cval * omdx * omdy * dz, memory_order_relaxed);
|
|
365
|
+
atomic_fetch_add_explicit(&grad_vol[base + yz_stride + Nz], cval * dx * dy * omdz, memory_order_relaxed);
|
|
366
|
+
atomic_fetch_add_explicit(&grad_vol[base + yz_stride + 1], cval * dx * omdy * dz, memory_order_relaxed);
|
|
367
|
+
atomic_fetch_add_explicit(&grad_vol[base + Nz + 1], cval * omdx * dy * dz, memory_order_relaxed);
|
|
368
|
+
atomic_fetch_add_explicit(&grad_vol[base + yz_stride + Nz + 1], cval * dx * dy * dz, memory_order_relaxed);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (txn <= tyn && txn <= tzn) {
|
|
373
|
+
t = txn;
|
|
374
|
+
ix += step_x;
|
|
375
|
+
txn += dt_x;
|
|
376
|
+
} else if (tyn <= txn && tyn <= tzn) {
|
|
377
|
+
t = tyn;
|
|
378
|
+
iy += step_y;
|
|
379
|
+
tyn += dt_y;
|
|
380
|
+
} else {
|
|
381
|
+
t = tzn;
|
|
382
|
+
iz += step_z;
|
|
383
|
+
tzn += dt_z;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
"""
|
|
387
|
+
|
|
388
|
+
cone_3d_backward_kernel = mx.fast.metal_kernel(
|
|
389
|
+
name="cone_3d_backward",
|
|
390
|
+
input_names=["sino", "src_pos", "det_center_arr", "det_u_vec_arr", "det_v_vec_arr", "params", "fparams"],
|
|
391
|
+
output_names=["grad_vol"],
|
|
392
|
+
source=_CONE_3D_BACKWARD_SOURCE,
|
|
393
|
+
atomic_outputs=True,
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
# ============================================================================
|
|
397
|
+
# 3D Cone Beam Footprint-Matched Projector Pair
|
|
398
|
+
# ============================================================================
|
|
399
|
+
|
|
400
|
+
_CONE_3D_FOOTPRINT_FORWARD_SOURCE = """
|
|
401
|
+
// Thread position: (iview, iy, iz)
|
|
402
|
+
uint iview = thread_position_in_grid.x;
|
|
403
|
+
uint iy = thread_position_in_grid.y;
|
|
404
|
+
uint iz = thread_position_in_grid.z;
|
|
405
|
+
|
|
406
|
+
int n_views = params[0];
|
|
407
|
+
int n_u = params[1];
|
|
408
|
+
int n_v = params[2];
|
|
409
|
+
int Nx = params[3];
|
|
410
|
+
int Ny = params[4];
|
|
411
|
+
int Nz = params[5];
|
|
412
|
+
|
|
413
|
+
float du = fparams[0];
|
|
414
|
+
float dv = fparams[1];
|
|
415
|
+
float cx = fparams[2];
|
|
416
|
+
float cy = fparams[3];
|
|
417
|
+
float cz = fparams[4];
|
|
418
|
+
float voxel_spacing = fparams[5];
|
|
419
|
+
|
|
420
|
+
if ((int)iview >= n_views || (int)iy >= Ny || (int)iz >= Nz) return;
|
|
421
|
+
|
|
422
|
+
float eps = """ + _EPSILON_STR + """;
|
|
423
|
+
float center_u = ((float)n_u - 1.0f) * 0.5f;
|
|
424
|
+
float center_v = ((float)n_v - 1.0f) * 0.5f;
|
|
425
|
+
int yz_stride = Ny * Nz;
|
|
426
|
+
|
|
427
|
+
float src_x = src_pos[iview * 3 + 0];
|
|
428
|
+
float src_y = src_pos[iview * 3 + 1];
|
|
429
|
+
float src_z = src_pos[iview * 3 + 2];
|
|
430
|
+
|
|
431
|
+
float det_cx = det_center[iview * 3 + 0];
|
|
432
|
+
float det_cy = det_center[iview * 3 + 1];
|
|
433
|
+
float det_cz = det_center[iview * 3 + 2];
|
|
434
|
+
|
|
435
|
+
float u_vec_x = det_u_vec_arr[iview * 3 + 0];
|
|
436
|
+
float u_vec_y = det_u_vec_arr[iview * 3 + 1];
|
|
437
|
+
float u_vec_z = det_u_vec_arr[iview * 3 + 2];
|
|
438
|
+
|
|
439
|
+
float v_vec_x = det_v_vec_arr[iview * 3 + 0];
|
|
440
|
+
float v_vec_y = det_v_vec_arr[iview * 3 + 1];
|
|
441
|
+
float v_vec_z = det_v_vec_arr[iview * 3 + 2];
|
|
442
|
+
|
|
443
|
+
float n_x = u_vec_y * v_vec_z - u_vec_z * v_vec_y;
|
|
444
|
+
float n_y = u_vec_z * v_vec_x - u_vec_x * v_vec_z;
|
|
445
|
+
float n_z = u_vec_x * v_vec_y - u_vec_y * v_vec_x;
|
|
446
|
+
|
|
447
|
+
float plane_dx = det_cx - src_x;
|
|
448
|
+
float plane_dy = det_cy - src_y;
|
|
449
|
+
float plane_dz = det_cz - src_z;
|
|
450
|
+
float plane_dist = plane_dx * n_x + plane_dy * n_y + plane_dz * n_z;
|
|
451
|
+
if (metal::abs(plane_dist) <= eps) return;
|
|
452
|
+
|
|
453
|
+
float py = (((float)iy + 0.5f) - cy) * voxel_spacing;
|
|
454
|
+
float pz = (((float)iz + 0.5f) - cz) * voxel_spacing;
|
|
455
|
+
|
|
456
|
+
for (int ix = 0; ix < Nx; ++ix) {
|
|
457
|
+
int base = ix * yz_stride + (int)iy * Nz + (int)iz;
|
|
458
|
+
float val = vol[base];
|
|
459
|
+
if (metal::abs(val) <= eps) continue;
|
|
460
|
+
|
|
461
|
+
float px = (((float)ix + 0.5f) - cx) * voxel_spacing;
|
|
462
|
+
float rx = px - src_x;
|
|
463
|
+
float ry = py - src_y;
|
|
464
|
+
float rz = pz - src_z;
|
|
465
|
+
|
|
466
|
+
float denom = rx * n_x + ry * n_y + rz * n_z;
|
|
467
|
+
if (metal::abs(denom) <= eps) continue;
|
|
468
|
+
|
|
469
|
+
float t = plane_dist / denom;
|
|
470
|
+
if (t <= 0.0f) continue;
|
|
471
|
+
|
|
472
|
+
float qx = src_x + t * rx;
|
|
473
|
+
float qy = src_y + t * ry;
|
|
474
|
+
float qz = src_z + t * rz;
|
|
475
|
+
|
|
476
|
+
float rel_x = qx - det_cx;
|
|
477
|
+
float rel_y = qy - det_cy;
|
|
478
|
+
float rel_z = qz - det_cz;
|
|
479
|
+
float u0 = rel_x * u_vec_x + rel_y * u_vec_y + rel_z * u_vec_z;
|
|
480
|
+
float v0 = rel_x * v_vec_x + rel_y * v_vec_y + rel_z * v_vec_z;
|
|
481
|
+
|
|
482
|
+
float ru = rx * u_vec_x + ry * u_vec_y + rz * u_vec_z;
|
|
483
|
+
float rv = rx * v_vec_x + ry * v_vec_y + rz * v_vec_z;
|
|
484
|
+
float inv_denom = 1.0f / denom;
|
|
485
|
+
|
|
486
|
+
float grad_ux = t * u_vec_x - t * ru * inv_denom * n_x;
|
|
487
|
+
float grad_uy = t * u_vec_y - t * ru * inv_denom * n_y;
|
|
488
|
+
float grad_uz = t * u_vec_z - t * ru * inv_denom * n_z;
|
|
489
|
+
|
|
490
|
+
float grad_vx = t * v_vec_x - t * rv * inv_denom * n_x;
|
|
491
|
+
float grad_vy = t * v_vec_y - t * rv * inv_denom * n_y;
|
|
492
|
+
float grad_vz = t * v_vec_z - t * rv * inv_denom * n_z;
|
|
493
|
+
|
|
494
|
+
float half_u = 0.5f * voxel_spacing * (
|
|
495
|
+
metal::abs(grad_ux) + metal::abs(grad_uy) + metal::abs(grad_uz)
|
|
496
|
+
);
|
|
497
|
+
float half_v = 0.5f * voxel_spacing * (
|
|
498
|
+
metal::abs(grad_vx) + metal::abs(grad_vy) + metal::abs(grad_vz)
|
|
499
|
+
);
|
|
500
|
+
half_u = metal::max(half_u, 0.5f * du);
|
|
501
|
+
half_v = metal::max(half_v, 0.5f * dv);
|
|
502
|
+
|
|
503
|
+
float width_u = metal::max(2.0f * half_u, du);
|
|
504
|
+
float width_v = metal::max(2.0f * half_v, dv);
|
|
505
|
+
|
|
506
|
+
float support_u = half_u + 0.5f * du;
|
|
507
|
+
float support_v = half_v + 0.5f * dv;
|
|
508
|
+
|
|
509
|
+
int iu_min = (int)metal::floor((u0 - support_u) / du + center_u);
|
|
510
|
+
int iu_max = (int)metal::ceil((u0 + support_u) / du + center_u);
|
|
511
|
+
int iv_min = (int)metal::floor((v0 - support_v) / dv + center_v);
|
|
512
|
+
int iv_max = (int)metal::ceil((v0 + support_v) / dv + center_v);
|
|
513
|
+
|
|
514
|
+
iu_min = metal::max(0, iu_min);
|
|
515
|
+
iv_min = metal::max(0, iv_min);
|
|
516
|
+
iu_max = metal::min(n_u - 1, iu_max);
|
|
517
|
+
iv_max = metal::min(n_v - 1, iv_max);
|
|
518
|
+
|
|
519
|
+
float fp_u_lo = u0 - half_u;
|
|
520
|
+
float fp_u_hi = u0 + half_u;
|
|
521
|
+
float fp_v_lo = v0 - half_v;
|
|
522
|
+
float fp_v_hi = v0 + half_v;
|
|
523
|
+
float scaled_val = val * voxel_spacing;
|
|
524
|
+
|
|
525
|
+
for (int iu = iu_min; iu <= iu_max; ++iu) {
|
|
526
|
+
float pixel_u = ((float)iu - center_u) * du;
|
|
527
|
+
float pixel_u_lo = pixel_u - 0.5f * du;
|
|
528
|
+
float pixel_u_hi = pixel_u + 0.5f * du;
|
|
529
|
+
float overlap_u = metal::min(pixel_u_hi, fp_u_hi) - metal::max(pixel_u_lo, fp_u_lo);
|
|
530
|
+
if (overlap_u <= eps) continue;
|
|
531
|
+
float wu = overlap_u / width_u;
|
|
532
|
+
|
|
533
|
+
for (int iv = iv_min; iv <= iv_max; ++iv) {
|
|
534
|
+
float pixel_v = ((float)iv - center_v) * dv;
|
|
535
|
+
float pixel_v_lo = pixel_v - 0.5f * dv;
|
|
536
|
+
float pixel_v_hi = pixel_v + 0.5f * dv;
|
|
537
|
+
float overlap_v = metal::min(pixel_v_hi, fp_v_hi) - metal::max(pixel_v_lo, fp_v_lo);
|
|
538
|
+
if (overlap_v <= eps) continue;
|
|
539
|
+
float wv = overlap_v / width_v;
|
|
540
|
+
|
|
541
|
+
atomic_fetch_add_explicit(
|
|
542
|
+
&sino[iview * n_u * n_v + iu * n_v + iv],
|
|
543
|
+
scaled_val * wu * wv,
|
|
544
|
+
memory_order_relaxed
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
"""
|
|
550
|
+
|
|
551
|
+
cone_3d_footprint_forward_kernel = mx.fast.metal_kernel(
|
|
552
|
+
name="cone_3d_footprint_forward",
|
|
553
|
+
input_names=["vol", "src_pos", "det_center", "det_u_vec_arr", "det_v_vec_arr", "params", "fparams"],
|
|
554
|
+
output_names=["sino"],
|
|
555
|
+
source=_CONE_3D_FOOTPRINT_FORWARD_SOURCE,
|
|
556
|
+
atomic_outputs=True,
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
_CONE_3D_FOOTPRINT_BACKWARD_SOURCE = """
|
|
560
|
+
// Thread position: (ix, iy, iz)
|
|
561
|
+
uint ix = thread_position_in_grid.x;
|
|
562
|
+
uint iy = thread_position_in_grid.y;
|
|
563
|
+
uint iz = thread_position_in_grid.z;
|
|
564
|
+
|
|
565
|
+
int n_views = params[0];
|
|
566
|
+
int n_u = params[1];
|
|
567
|
+
int n_v = params[2];
|
|
568
|
+
int Nx = params[3];
|
|
569
|
+
int Ny = params[4];
|
|
570
|
+
int Nz = params[5];
|
|
571
|
+
|
|
572
|
+
float du = fparams[0];
|
|
573
|
+
float dv = fparams[1];
|
|
574
|
+
float cx = fparams[2];
|
|
575
|
+
float cy = fparams[3];
|
|
576
|
+
float cz = fparams[4];
|
|
577
|
+
float voxel_spacing = fparams[5];
|
|
578
|
+
|
|
579
|
+
if ((int)ix >= Nx || (int)iy >= Ny || (int)iz >= Nz) return;
|
|
580
|
+
|
|
581
|
+
float eps = """ + _EPSILON_STR + """;
|
|
582
|
+
float center_u = ((float)n_u - 1.0f) * 0.5f;
|
|
583
|
+
float center_v = ((float)n_v - 1.0f) * 0.5f;
|
|
584
|
+
int yz_stride = Ny * Nz;
|
|
585
|
+
|
|
586
|
+
float px = (((float)ix + 0.5f) - cx) * voxel_spacing;
|
|
587
|
+
float py = (((float)iy + 0.5f) - cy) * voxel_spacing;
|
|
588
|
+
float pz = (((float)iz + 0.5f) - cz) * voxel_spacing;
|
|
589
|
+
|
|
590
|
+
float accum = 0.0f;
|
|
591
|
+
|
|
592
|
+
for (int iview = 0; iview < n_views; ++iview) {
|
|
593
|
+
float src_x = src_pos[iview * 3 + 0];
|
|
594
|
+
float src_y = src_pos[iview * 3 + 1];
|
|
595
|
+
float src_z = src_pos[iview * 3 + 2];
|
|
596
|
+
|
|
597
|
+
float det_cx = det_center_arr[iview * 3 + 0];
|
|
598
|
+
float det_cy = det_center_arr[iview * 3 + 1];
|
|
599
|
+
float det_cz = det_center_arr[iview * 3 + 2];
|
|
600
|
+
|
|
601
|
+
float u_vec_x = det_u_vec_arr[iview * 3 + 0];
|
|
602
|
+
float u_vec_y = det_u_vec_arr[iview * 3 + 1];
|
|
603
|
+
float u_vec_z = det_u_vec_arr[iview * 3 + 2];
|
|
604
|
+
|
|
605
|
+
float v_vec_x = det_v_vec_arr[iview * 3 + 0];
|
|
606
|
+
float v_vec_y = det_v_vec_arr[iview * 3 + 1];
|
|
607
|
+
float v_vec_z = det_v_vec_arr[iview * 3 + 2];
|
|
608
|
+
|
|
609
|
+
float n_x = u_vec_y * v_vec_z - u_vec_z * v_vec_y;
|
|
610
|
+
float n_y = u_vec_z * v_vec_x - u_vec_x * v_vec_z;
|
|
611
|
+
float n_z = u_vec_x * v_vec_y - u_vec_y * v_vec_x;
|
|
612
|
+
|
|
613
|
+
float plane_dx = det_cx - src_x;
|
|
614
|
+
float plane_dy = det_cy - src_y;
|
|
615
|
+
float plane_dz = det_cz - src_z;
|
|
616
|
+
float plane_dist = plane_dx * n_x + plane_dy * n_y + plane_dz * n_z;
|
|
617
|
+
if (metal::abs(plane_dist) <= eps) continue;
|
|
618
|
+
|
|
619
|
+
float rx = px - src_x;
|
|
620
|
+
float ry = py - src_y;
|
|
621
|
+
float rz = pz - src_z;
|
|
622
|
+
float denom = rx * n_x + ry * n_y + rz * n_z;
|
|
623
|
+
if (metal::abs(denom) <= eps) continue;
|
|
624
|
+
|
|
625
|
+
float t = plane_dist / denom;
|
|
626
|
+
if (t <= 0.0f) continue;
|
|
627
|
+
|
|
628
|
+
float qx = src_x + t * rx;
|
|
629
|
+
float qy = src_y + t * ry;
|
|
630
|
+
float qz = src_z + t * rz;
|
|
631
|
+
|
|
632
|
+
float rel_x = qx - det_cx;
|
|
633
|
+
float rel_y = qy - det_cy;
|
|
634
|
+
float rel_z = qz - det_cz;
|
|
635
|
+
float u0 = rel_x * u_vec_x + rel_y * u_vec_y + rel_z * u_vec_z;
|
|
636
|
+
float v0 = rel_x * v_vec_x + rel_y * v_vec_y + rel_z * v_vec_z;
|
|
637
|
+
|
|
638
|
+
float ru = rx * u_vec_x + ry * u_vec_y + rz * u_vec_z;
|
|
639
|
+
float rv = rx * v_vec_x + ry * v_vec_y + rz * v_vec_z;
|
|
640
|
+
float inv_denom = 1.0f / denom;
|
|
641
|
+
|
|
642
|
+
float grad_ux = t * u_vec_x - t * ru * inv_denom * n_x;
|
|
643
|
+
float grad_uy = t * u_vec_y - t * ru * inv_denom * n_y;
|
|
644
|
+
float grad_uz = t * u_vec_z - t * ru * inv_denom * n_z;
|
|
645
|
+
|
|
646
|
+
float grad_vx = t * v_vec_x - t * rv * inv_denom * n_x;
|
|
647
|
+
float grad_vy = t * v_vec_y - t * rv * inv_denom * n_y;
|
|
648
|
+
float grad_vz = t * v_vec_z - t * rv * inv_denom * n_z;
|
|
649
|
+
|
|
650
|
+
float half_u = 0.5f * voxel_spacing * (
|
|
651
|
+
metal::abs(grad_ux) + metal::abs(grad_uy) + metal::abs(grad_uz)
|
|
652
|
+
);
|
|
653
|
+
float half_v = 0.5f * voxel_spacing * (
|
|
654
|
+
metal::abs(grad_vx) + metal::abs(grad_vy) + metal::abs(grad_vz)
|
|
655
|
+
);
|
|
656
|
+
half_u = metal::max(half_u, 0.5f * du);
|
|
657
|
+
half_v = metal::max(half_v, 0.5f * dv);
|
|
658
|
+
|
|
659
|
+
float width_u = metal::max(2.0f * half_u, du);
|
|
660
|
+
float width_v = metal::max(2.0f * half_v, dv);
|
|
661
|
+
|
|
662
|
+
float support_u = half_u + 0.5f * du;
|
|
663
|
+
float support_v = half_v + 0.5f * dv;
|
|
664
|
+
|
|
665
|
+
int iu_min = (int)metal::floor((u0 - support_u) / du + center_u);
|
|
666
|
+
int iu_max = (int)metal::ceil((u0 + support_u) / du + center_u);
|
|
667
|
+
int iv_min = (int)metal::floor((v0 - support_v) / dv + center_v);
|
|
668
|
+
int iv_max = (int)metal::ceil((v0 + support_v) / dv + center_v);
|
|
669
|
+
|
|
670
|
+
iu_min = metal::max(0, iu_min);
|
|
671
|
+
iv_min = metal::max(0, iv_min);
|
|
672
|
+
iu_max = metal::min(n_u - 1, iu_max);
|
|
673
|
+
iv_max = metal::min(n_v - 1, iv_max);
|
|
674
|
+
|
|
675
|
+
float fp_u_lo = u0 - half_u;
|
|
676
|
+
float fp_u_hi = u0 + half_u;
|
|
677
|
+
float fp_v_lo = v0 - half_v;
|
|
678
|
+
float fp_v_hi = v0 + half_v;
|
|
679
|
+
|
|
680
|
+
for (int iu = iu_min; iu <= iu_max; ++iu) {
|
|
681
|
+
float pixel_u = ((float)iu - center_u) * du;
|
|
682
|
+
float pixel_u_lo = pixel_u - 0.5f * du;
|
|
683
|
+
float pixel_u_hi = pixel_u + 0.5f * du;
|
|
684
|
+
float overlap_u = metal::min(pixel_u_hi, fp_u_hi) - metal::max(pixel_u_lo, fp_u_lo);
|
|
685
|
+
if (overlap_u <= eps) continue;
|
|
686
|
+
float wu = overlap_u / width_u;
|
|
687
|
+
|
|
688
|
+
for (int iv = iv_min; iv <= iv_max; ++iv) {
|
|
689
|
+
float pixel_v = ((float)iv - center_v) * dv;
|
|
690
|
+
float pixel_v_lo = pixel_v - 0.5f * dv;
|
|
691
|
+
float pixel_v_hi = pixel_v + 0.5f * dv;
|
|
692
|
+
float overlap_v = metal::min(pixel_v_hi, fp_v_hi) - metal::max(pixel_v_lo, fp_v_lo);
|
|
693
|
+
if (overlap_v <= eps) continue;
|
|
694
|
+
float wv = overlap_v / width_v;
|
|
695
|
+
accum += sino[iview * n_u * n_v + iu * n_v + iv] * wu * wv * voxel_spacing;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
grad_vol[(int)ix * yz_stride + (int)iy * Nz + (int)iz] = accum;
|
|
701
|
+
"""
|
|
702
|
+
|
|
703
|
+
cone_3d_footprint_backward_kernel = mx.fast.metal_kernel(
|
|
704
|
+
name="cone_3d_footprint_backward",
|
|
705
|
+
input_names=["sino", "src_pos", "det_center_arr", "det_u_vec_arr", "det_v_vec_arr", "params", "fparams"],
|
|
706
|
+
output_names=["grad_vol"],
|
|
707
|
+
source=_CONE_3D_FOOTPRINT_BACKWARD_SOURCE,
|
|
708
|
+
)
|