difftomo 0.1.0__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.
- difftomo/CT/Cpp_files/ARTAlgo.cpp +380 -0
- difftomo/CT/Cpp_files/SiddonAlgorithm.cpp +325 -0
- difftomo/CT/Cpp_files/bindings_cpu.cpp +27 -0
- difftomo/CT/Cpp_files/bindings_cuda.cpp +17 -0
- difftomo/CT/Cuda_files/SiddonAlgorithm.cu +316 -0
- difftomo/CT/Iterative/ClassicAlgorithm.py +244 -0
- difftomo/CT/Iterative/DIP.py +42 -0
- difftomo/CT/Iterative/TV_Minimisation.py +300 -0
- difftomo/CT/Iterative/__init__.py +5 -0
- difftomo/CT/Models/DuDoTransModel.py +1192 -0
- difftomo/CT/Models/FBPConvNet.py +82 -0
- difftomo/CT/Models/REDCNN.py +62 -0
- difftomo/CT/Models/RadonPrior.py +92 -0
- difftomo/CT/Models/__init__.py +6 -0
- difftomo/CT/Models/diffusion_utils.py +0 -0
- difftomo/CT/Models/utils.py +49 -0
- difftomo/CT/Projector/FanBeam.py +490 -0
- difftomo/CT/Projector/ParallelBeam.py +474 -0
- difftomo/CT/Projector/__init__.py +5 -0
- difftomo/CT/Projector/build.py +13 -0
- difftomo/CT/_extensions.py +94 -0
- difftomo/CT/header_files/Algorithms.cuh +13 -0
- difftomo/CT/header_files/Algorithms.h +23 -0
- difftomo/__init__.py +0 -0
- difftomo/data/__init__.py +5 -0
- difftomo/data/carved_cheese.py +90 -0
- difftomo/data/image/shepp_logan.npy +0 -0
- difftomo/data/phantom.py +15 -0
- difftomo/data/walnut.py +50 -0
- difftomo/utils.py +132 -0
- difftomo-0.1.0.dist-info/METADATA +74 -0
- difftomo-0.1.0.dist-info/RECORD +35 -0
- difftomo-0.1.0.dist-info/WHEEL +5 -0
- difftomo-0.1.0.dist-info/licenses/LICENSE +202 -0
- difftomo-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
#include "header_files/Algorithms.h"
|
|
2
|
+
#include <algorithm>
|
|
3
|
+
#include <thread>
|
|
4
|
+
#include <torch/extension.h>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
using std::max;
|
|
8
|
+
using std::min;
|
|
9
|
+
|
|
10
|
+
float aTa_norm(int projections, int detector_bins, float detector_spacing,
|
|
11
|
+
int height, int width, int ray_index, float coverage,
|
|
12
|
+
float distance, char type) {
|
|
13
|
+
|
|
14
|
+
int projection_index = ray_index / detector_bins;
|
|
15
|
+
int detector_index = ray_index % detector_bins;
|
|
16
|
+
|
|
17
|
+
float bx = -(height / 2.0f);
|
|
18
|
+
float by = -(width / 2.0f);
|
|
19
|
+
float u = detector_spacing * (detector_index - detector_bins / 2.0f);
|
|
20
|
+
|
|
21
|
+
// thread starts executing here so calculate ray for this detector index and
|
|
22
|
+
// projection index
|
|
23
|
+
float p1[2];
|
|
24
|
+
float p2[2];
|
|
25
|
+
float angle;
|
|
26
|
+
|
|
27
|
+
if (type == 'p') {
|
|
28
|
+
angle = projection_index * coverage / projections;
|
|
29
|
+
|
|
30
|
+
p1[0] = distance * cosf(angle) - u * sinf(angle);
|
|
31
|
+
p1[1] = distance * sinf(angle) + u * cosf(angle);
|
|
32
|
+
|
|
33
|
+
p2[0] = -distance * cosf(angle) - u * sinf(angle);
|
|
34
|
+
p2[1] = -distance * sinf(angle) + u * cosf(angle);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
else if (type == 'f') {
|
|
38
|
+
angle = projection_index * coverage / projections;
|
|
39
|
+
|
|
40
|
+
p1[0] = distance * sinf(angle);
|
|
41
|
+
p1[1] = distance * cosf(angle);
|
|
42
|
+
|
|
43
|
+
p2[0] = -distance * sinf(angle) + u * cosf(angle);
|
|
44
|
+
p2[1] = -distance * cosf(angle) - u * sinf(angle);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
float dx = p2[0] - p1[0];
|
|
48
|
+
float dy = p2[1] - p1[1];
|
|
49
|
+
float d_conv = sqrtf(dx * dx + dy * dy);
|
|
50
|
+
|
|
51
|
+
// Parametric boundaries for x and y planes
|
|
52
|
+
float ax_min = (std::abs(dx) > 1e-6f)
|
|
53
|
+
? min((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
54
|
+
: -1e9f;
|
|
55
|
+
float ax_max = (std::abs(dx) > 1e-6f)
|
|
56
|
+
? max((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
57
|
+
: 1e9f;
|
|
58
|
+
|
|
59
|
+
float ay_min = (std::abs(dy) > 1e-6f)
|
|
60
|
+
? min((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
61
|
+
: -1e9f;
|
|
62
|
+
float ay_max = (std::abs(dy) > 1e-6f)
|
|
63
|
+
? max((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
64
|
+
: 1e9f;
|
|
65
|
+
|
|
66
|
+
float alpha_min = max(0.0f, max(ax_min, ay_min));
|
|
67
|
+
float alpha_max = min(1.0f, min(ax_max, ay_max));
|
|
68
|
+
|
|
69
|
+
float d_ax = (std::abs(dx) > 1e-6f) ? 1.0f / std::abs(dx) : 1e9f;
|
|
70
|
+
float d_ay = (std::abs(dy) > 1e-6f) ? 1.0f / std::abs(dy) : 1e9f;
|
|
71
|
+
|
|
72
|
+
// Initial alpha values
|
|
73
|
+
float ax_next = 1e9f;
|
|
74
|
+
if (std::abs(dx) > 1e-6f) {
|
|
75
|
+
float rx_entry = p1[0] + alpha_min * dx - bx;
|
|
76
|
+
int next_i = (dx > 0) ? (int)floorf(rx_entry) + 1 : (int)floorf(rx_entry);
|
|
77
|
+
ax_next = (bx + (float)next_i - p1[0]) / dx;
|
|
78
|
+
if (ax_next <= alpha_min)
|
|
79
|
+
ax_next += d_ax;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
float ay_next = 1e9f;
|
|
83
|
+
if (std::abs(dy) > 1e-6f) {
|
|
84
|
+
float ry_entry = p1[1] + alpha_min * dy - by;
|
|
85
|
+
int next_j = (dy > 0) ? (int)floorf(ry_entry) + 1 : (int)floorf(ry_entry);
|
|
86
|
+
ay_next = (by + (float)next_j - p1[1]) / dy;
|
|
87
|
+
if (ay_next <= alpha_min)
|
|
88
|
+
ay_next += d_ay;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
float alpha_curr = alpha_min;
|
|
92
|
+
float sum = 0.0f;
|
|
93
|
+
|
|
94
|
+
// Ray marching loop
|
|
95
|
+
while (alpha_curr < alpha_max - 1e-6f) {
|
|
96
|
+
float alpha_next = min(alpha_max, min(ax_next, ay_next));
|
|
97
|
+
float mid_alpha = (alpha_curr + alpha_next) * 0.5f;
|
|
98
|
+
|
|
99
|
+
int img_i = (int)floorf(p1[0] + mid_alpha * dx - bx);
|
|
100
|
+
int img_j = (int)floorf(p1[1] + mid_alpha * dy - by);
|
|
101
|
+
|
|
102
|
+
if (img_i >= 0 && img_i < height && img_j >= 0 && img_j < width) {
|
|
103
|
+
float length = (alpha_next - alpha_curr) * d_conv;
|
|
104
|
+
sum += length * length;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (ax_next < ay_next) {
|
|
108
|
+
ax_next += d_ax;
|
|
109
|
+
} else {
|
|
110
|
+
ay_next += d_ay;
|
|
111
|
+
}
|
|
112
|
+
alpha_curr = alpha_next;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return sum;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
float aTx_k(float *image, int projections, int detector_bins,
|
|
119
|
+
float detector_spacing, int batches, int channels, int height,
|
|
120
|
+
int width, int batch_index, int channel_index, int ray_index,
|
|
121
|
+
float coverage, float distance, char type) {
|
|
122
|
+
|
|
123
|
+
int projection_index = ray_index / detector_bins;
|
|
124
|
+
int detector_index = ray_index % detector_bins;
|
|
125
|
+
float bx = -(height / 2.0f);
|
|
126
|
+
float by = -(width / 2.0f);
|
|
127
|
+
float u = detector_spacing * (detector_index - detector_bins / 2.0f);
|
|
128
|
+
|
|
129
|
+
// thread starts executing here so calculate ray for this detector index and
|
|
130
|
+
// projection index
|
|
131
|
+
float p1[2];
|
|
132
|
+
float p2[2];
|
|
133
|
+
float angle;
|
|
134
|
+
|
|
135
|
+
if (type == 'p') {
|
|
136
|
+
angle = projection_index * coverage / projections;
|
|
137
|
+
|
|
138
|
+
p1[0] = distance * cosf(angle) - u * sinf(angle);
|
|
139
|
+
p1[1] = distance * sinf(angle) + u * cosf(angle);
|
|
140
|
+
|
|
141
|
+
p2[0] = -distance * cosf(angle) - u * sinf(angle);
|
|
142
|
+
p2[1] = -distance * sinf(angle) + u * cosf(angle);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
else if (type == 'f') {
|
|
146
|
+
angle = projection_index * coverage / projections;
|
|
147
|
+
|
|
148
|
+
p1[0] = distance * sinf(angle);
|
|
149
|
+
p1[1] = distance * cosf(angle);
|
|
150
|
+
|
|
151
|
+
p2[0] = -distance * sinf(angle) + u * cosf(angle);
|
|
152
|
+
p2[1] = -distance * cosf(angle) - u * sinf(angle);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
float dx = p2[0] - p1[0];
|
|
156
|
+
float dy = p2[1] - p1[1];
|
|
157
|
+
float d_conv = sqrtf(dx * dx + dy * dy);
|
|
158
|
+
|
|
159
|
+
// Parametric boundaries for x and y planes
|
|
160
|
+
float ax_min = (std::abs(dx) > 1e-6f)
|
|
161
|
+
? min((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
162
|
+
: -1e9f;
|
|
163
|
+
float ax_max = (std::abs(dx) > 1e-6f)
|
|
164
|
+
? max((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
165
|
+
: 1e9f;
|
|
166
|
+
|
|
167
|
+
float ay_min = (std::abs(dy) > 1e-6f)
|
|
168
|
+
? min((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
169
|
+
: -1e9f;
|
|
170
|
+
float ay_max = (std::abs(dy) > 1e-6f)
|
|
171
|
+
? max((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
172
|
+
: 1e9f;
|
|
173
|
+
|
|
174
|
+
float alpha_min = max(0.0f, max(ax_min, ay_min));
|
|
175
|
+
float alpha_max = min(1.0f, min(ax_max, ay_max));
|
|
176
|
+
|
|
177
|
+
float d_ax = (std::abs(dx) > 1e-6f) ? 1.0f / std::abs(dx) : 1e9f;
|
|
178
|
+
float d_ay = (std::abs(dy) > 1e-6f) ? 1.0f / std::abs(dy) : 1e9f;
|
|
179
|
+
|
|
180
|
+
// Initial alpha values
|
|
181
|
+
float ax_next = 1e9f;
|
|
182
|
+
if (std::abs(dx) > 1e-6f) {
|
|
183
|
+
float rx_entry = p1[0] + alpha_min * dx - bx;
|
|
184
|
+
int next_i = (dx > 0) ? (int)floorf(rx_entry) + 1 : (int)floorf(rx_entry);
|
|
185
|
+
ax_next = (bx + (float)next_i - p1[0]) / dx;
|
|
186
|
+
if (ax_next <= alpha_min)
|
|
187
|
+
ax_next += d_ax;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
float ay_next = 1e9f;
|
|
191
|
+
if (std::abs(dy) > 1e-6f) {
|
|
192
|
+
float ry_entry = p1[1] + alpha_min * dy - by;
|
|
193
|
+
int next_j = (dy > 0) ? (int)floorf(ry_entry) + 1 : (int)floorf(ry_entry);
|
|
194
|
+
ay_next = (by + (float)next_j - p1[1]) / dy;
|
|
195
|
+
if (ay_next <= alpha_min)
|
|
196
|
+
ay_next += d_ay;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
float alpha_curr = alpha_min;
|
|
200
|
+
float sum = 0.0f;
|
|
201
|
+
|
|
202
|
+
// Ray marching loop
|
|
203
|
+
while (alpha_curr < alpha_max - 1e-6f) {
|
|
204
|
+
float alpha_next = min(alpha_max, min(ax_next, ay_next));
|
|
205
|
+
float mid_alpha = (alpha_curr + alpha_next) * 0.5f;
|
|
206
|
+
|
|
207
|
+
int img_i = (int)floorf(p1[0] + mid_alpha * dx - bx);
|
|
208
|
+
int img_j = (int)floorf(p1[1] + mid_alpha * dy - by);
|
|
209
|
+
|
|
210
|
+
if (img_i >= 0 && img_i < height && img_j >= 0 && img_j < width) {
|
|
211
|
+
float length = (alpha_next - alpha_curr) * d_conv;
|
|
212
|
+
sum += length *
|
|
213
|
+
image[batch_index * channels * height * width +
|
|
214
|
+
channel_index * height * width + img_i * width + img_j];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (ax_next < ay_next) {
|
|
218
|
+
ax_next += d_ax;
|
|
219
|
+
} else {
|
|
220
|
+
ay_next += d_ay;
|
|
221
|
+
}
|
|
222
|
+
alpha_curr = alpha_next;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return sum;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
void y_ai(float val, float *output, int projections, int detector_bins,
|
|
229
|
+
float detector_spacing, int channels, int height, int width,
|
|
230
|
+
int batch_index, int channel_index, int ray_index, float coverage,
|
|
231
|
+
float distance, char type) {
|
|
232
|
+
int projection_index = ray_index / detector_bins;
|
|
233
|
+
int detector_index = ray_index % detector_bins;
|
|
234
|
+
|
|
235
|
+
// ray origin
|
|
236
|
+
float bx = -(height / 2.0f);
|
|
237
|
+
float by = -(width / 2.0f);
|
|
238
|
+
float u = detector_spacing * (detector_index - detector_bins / 2.0f);
|
|
239
|
+
|
|
240
|
+
// thread starts executing here so calculate ray for this detector index and
|
|
241
|
+
// projection index
|
|
242
|
+
float p1[2];
|
|
243
|
+
float p2[2];
|
|
244
|
+
float angle;
|
|
245
|
+
|
|
246
|
+
if (type == 'p') {
|
|
247
|
+
angle = projection_index * coverage / projections;
|
|
248
|
+
|
|
249
|
+
p1[0] = distance * cosf(angle) - u * sinf(angle);
|
|
250
|
+
p1[1] = distance * sinf(angle) + u * cosf(angle);
|
|
251
|
+
|
|
252
|
+
p2[0] = -distance * cosf(angle) - u * sinf(angle);
|
|
253
|
+
p2[1] = -distance * sinf(angle) + u * cosf(angle);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
else if (type == 'f') {
|
|
257
|
+
angle = projection_index * coverage / projections;
|
|
258
|
+
|
|
259
|
+
p1[0] = distance * sinf(angle);
|
|
260
|
+
p1[1] = distance * cosf(angle);
|
|
261
|
+
|
|
262
|
+
p2[0] = -distance * sinf(angle) + u * cosf(angle);
|
|
263
|
+
p2[1] = -distance * cosf(angle) - u * sinf(angle);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
float dx = p2[0] - p1[0];
|
|
267
|
+
float dy = p2[1] - p1[1];
|
|
268
|
+
float d_conv = sqrtf(dx * dx + dy * dy);
|
|
269
|
+
float d_ax = (std::abs(dx) > 1e-6f) ? 1.0f / std::abs(dx) : 1e9f;
|
|
270
|
+
float d_ay = (std::abs(dy) > 1e-6f) ? 1.0f / std::abs(dy) : 1e9f;
|
|
271
|
+
|
|
272
|
+
float ax_min = (std::abs(dx) > 1e-6f)
|
|
273
|
+
? min((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
274
|
+
: -1e9f;
|
|
275
|
+
float ax_max = (std::abs(dx) > 1e-6f)
|
|
276
|
+
? max((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
277
|
+
: 1e9f;
|
|
278
|
+
|
|
279
|
+
float ay_min = (std::abs(dy) > 1e-6f)
|
|
280
|
+
? min((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
281
|
+
: -1e9f;
|
|
282
|
+
float ay_max = (std::abs(dy) > 1e-6f)
|
|
283
|
+
? max((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
284
|
+
: 1e9f;
|
|
285
|
+
|
|
286
|
+
float alpha_min = max(0.0f, max(ax_min, ay_min));
|
|
287
|
+
float alpha_max = min(1.0f, min(ax_max, ay_max));
|
|
288
|
+
|
|
289
|
+
float ax_next = 1e9f;
|
|
290
|
+
if (std::abs(dx) > 1e-6f) {
|
|
291
|
+
float rx_entry = p1[0] + alpha_min * dx - bx;
|
|
292
|
+
int next_i = (dx > 0) ? (int)floorf(rx_entry) + 1 : (int)floorf(rx_entry);
|
|
293
|
+
ax_next = (bx + (float)next_i - p1[0]) / dx;
|
|
294
|
+
if (ax_next <= alpha_min)
|
|
295
|
+
ax_next += d_ax;
|
|
296
|
+
}
|
|
297
|
+
float ay_next = 1e9f;
|
|
298
|
+
if (std::abs(dy) > 1e-6f) {
|
|
299
|
+
float ry_entry = p1[1] + alpha_min * dy - by;
|
|
300
|
+
int next_j = (dy > 0) ? (int)floorf(ry_entry) + 1 : (int)floorf(ry_entry);
|
|
301
|
+
ay_next = (by + (float)next_j - p1[1]) / dy;
|
|
302
|
+
if (ay_next <= alpha_min)
|
|
303
|
+
ay_next += d_ay;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
float sum = 0.0f;
|
|
307
|
+
float alpha_curr = alpha_min;
|
|
308
|
+
|
|
309
|
+
// --- ray march: scatter instead of gather ---
|
|
310
|
+
while (alpha_curr < alpha_max - 1e-6f) {
|
|
311
|
+
float alpha_next = min(alpha_max, min(ax_next, ay_next));
|
|
312
|
+
float mid_alpha = (alpha_curr + alpha_next) * 0.5f;
|
|
313
|
+
|
|
314
|
+
int img_i = (int)floorf(p1[0] + mid_alpha * dx - bx);
|
|
315
|
+
int img_j = (int)floorf(p1[1] + mid_alpha * dy - by);
|
|
316
|
+
|
|
317
|
+
if (img_i >= 0 && img_i < height && img_j >= 0 && img_j < width) {
|
|
318
|
+
float length = (alpha_next - alpha_curr) * d_conv;
|
|
319
|
+
output[batch_index * channels * height * width +
|
|
320
|
+
channel_index * height * width + img_i * width + img_j] +=
|
|
321
|
+
length * val;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (ax_next < ay_next)
|
|
325
|
+
ax_next += d_ax;
|
|
326
|
+
else
|
|
327
|
+
ay_next += d_ay;
|
|
328
|
+
alpha_curr = alpha_next;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
torch::Tensor ARTReconstruction(torch::Tensor sinogram,
|
|
335
|
+
const std::vector<int64_t> &image_shape,
|
|
336
|
+
int projections, int detector_bins,
|
|
337
|
+
int iterations, float relaxation_parameter,
|
|
338
|
+
float detector_spacing, float coverage,
|
|
339
|
+
float distance, char type)
|
|
340
|
+
|
|
341
|
+
{
|
|
342
|
+
int batches = sinogram.size(0);
|
|
343
|
+
int channels = sinogram.size(1);
|
|
344
|
+
int height = image_shape[0];
|
|
345
|
+
int width = image_shape[1];
|
|
346
|
+
float eps = 1e-6f;
|
|
347
|
+
float norm, lam_n, residual;
|
|
348
|
+
auto output_k = torch::zeros({batches, channels, height, width},
|
|
349
|
+
torch::TensorOptions().dtype(torch::kFloat32));
|
|
350
|
+
|
|
351
|
+
float *sinogram_ptr = sinogram.data_ptr<float>();
|
|
352
|
+
int n_rays = projections * detector_bins;
|
|
353
|
+
std::vector<float> norms(n_rays);
|
|
354
|
+
for (int r = 0; r < n_rays; ++r)
|
|
355
|
+
norms[r] = aTa_norm(projections, detector_bins, detector_spacing, height,
|
|
356
|
+
width, r, coverage, distance, type);
|
|
357
|
+
|
|
358
|
+
for (int iter = 0; iter < iterations; ++iter) {
|
|
359
|
+
for (int i = 0; i < batches * channels * projections * detector_bins; ++i) {
|
|
360
|
+
int batch_index = i / (channels * projections * detector_bins);
|
|
361
|
+
int channel_index = (i / (projections * detector_bins)) % channels;
|
|
362
|
+
int ray_index = i % (projections * detector_bins);
|
|
363
|
+
norm = norms[ray_index];
|
|
364
|
+
if (norm < eps) {
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
lam_n = relaxation_parameter / norm;
|
|
368
|
+
float aTx =
|
|
369
|
+
aTx_k(output_k.data_ptr<float>(), projections, detector_bins,
|
|
370
|
+
detector_spacing, batches, channels, height, width, batch_index,
|
|
371
|
+
channel_index, ray_index, coverage, distance, type);
|
|
372
|
+
float scale = lam_n * (sinogram_ptr[i] - aTx);
|
|
373
|
+
y_ai(scale, output_k.data_ptr<float>(), projections, detector_bins,
|
|
374
|
+
detector_spacing, channels, height, width, batch_index,
|
|
375
|
+
channel_index, ray_index, coverage, distance, type);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return output_k;
|
|
380
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
#include "header_files/Algorithms.h"
|
|
2
|
+
#include <algorithm>
|
|
3
|
+
#include <omp.h>
|
|
4
|
+
#include <thread>
|
|
5
|
+
#include <torch/extension.h>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
using std::max;
|
|
9
|
+
using std::min;
|
|
10
|
+
|
|
11
|
+
std::mutex image_mutex;
|
|
12
|
+
|
|
13
|
+
void update_pixel(float *image, int pixel_index, float contribution) {
|
|
14
|
+
std::lock_guard<std::mutex> lock(image_mutex);
|
|
15
|
+
image[pixel_index] += contribution;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
void forward_siddon(float *image, float *output, int batches, int channels,
|
|
19
|
+
int height, int width, int projections, int detector_bins,
|
|
20
|
+
float detector_spacing, float coverage, float distance,
|
|
21
|
+
char type) {
|
|
22
|
+
|
|
23
|
+
int thread_index = omp_get_thread_num();
|
|
24
|
+
int num_threads = omp_get_num_threads();
|
|
25
|
+
|
|
26
|
+
for (int offset = thread_index;
|
|
27
|
+
offset < batches * channels * projections * detector_bins;
|
|
28
|
+
offset += num_threads) {
|
|
29
|
+
|
|
30
|
+
int batch_index = offset / (channels * projections * detector_bins);
|
|
31
|
+
int channel_index =
|
|
32
|
+
(offset - batch_index * channels * projections * detector_bins) /
|
|
33
|
+
(projections * detector_bins);
|
|
34
|
+
int projection_index =
|
|
35
|
+
(offset - batch_index * channels * projections * detector_bins -
|
|
36
|
+
channel_index * projections * detector_bins) /
|
|
37
|
+
detector_bins;
|
|
38
|
+
int detector_index = offset -
|
|
39
|
+
batch_index * channels * projections * detector_bins -
|
|
40
|
+
channel_index * projections * detector_bins -
|
|
41
|
+
projection_index * detector_bins;
|
|
42
|
+
|
|
43
|
+
if (projection_index >= projections || detector_index >= detector_bins) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
float bx = -(height / 2.0f);
|
|
48
|
+
float by = -(width / 2.0f);
|
|
49
|
+
float u = detector_spacing * (detector_index - detector_bins / 2.0f);
|
|
50
|
+
|
|
51
|
+
float p1[2];
|
|
52
|
+
float p2[2];
|
|
53
|
+
float angle;
|
|
54
|
+
|
|
55
|
+
if (type == 'p') {
|
|
56
|
+
angle = projection_index * coverage / projections;
|
|
57
|
+
|
|
58
|
+
p1[0] = distance * cosf(angle) - u * sinf(angle);
|
|
59
|
+
p1[1] = distance * sinf(angle) + u * cosf(angle);
|
|
60
|
+
|
|
61
|
+
p2[0] = -distance * cosf(angle) - u * sinf(angle);
|
|
62
|
+
p2[1] = -distance * sinf(angle) + u * cosf(angle);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
else if (type == 'f') {
|
|
66
|
+
angle = projection_index * coverage / projections;
|
|
67
|
+
|
|
68
|
+
p1[0] = distance * sinf(angle);
|
|
69
|
+
p1[1] = distance * cosf(angle);
|
|
70
|
+
|
|
71
|
+
p2[0] = -distance * sinf(angle) + u * cosf(angle);
|
|
72
|
+
p2[1] = -distance * cosf(angle) - u * sinf(angle);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
float dx = p2[0] - p1[0];
|
|
76
|
+
float dy = p2[1] - p1[1];
|
|
77
|
+
float d_conv = sqrtf(dx * dx + dy * dy);
|
|
78
|
+
|
|
79
|
+
// Parametric boundaries for x and y planes
|
|
80
|
+
float ax_min = (std::abs(dx) > 1e-6f)
|
|
81
|
+
? min((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
82
|
+
: -1e9f;
|
|
83
|
+
float ax_max = (std::abs(dx) > 1e-6f)
|
|
84
|
+
? max((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
85
|
+
: 1e9f;
|
|
86
|
+
|
|
87
|
+
float ay_min = (std::abs(dy) > 1e-6f)
|
|
88
|
+
? min((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
89
|
+
: -1e9f;
|
|
90
|
+
float ay_max = (std::abs(dy) > 1e-6f)
|
|
91
|
+
? max((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
92
|
+
: 1e9f;
|
|
93
|
+
|
|
94
|
+
float alpha_min = max(0.0f, max(ax_min, ay_min));
|
|
95
|
+
float alpha_max = min(1.0f, min(ax_max, ay_max));
|
|
96
|
+
|
|
97
|
+
float d_ax = (std::abs(dx) > 1e-6f) ? 1.0f / std::abs(dx) : 1e9f;
|
|
98
|
+
float d_ay = (std::abs(dy) > 1e-6f) ? 1.0f / std::abs(dy) : 1e9f;
|
|
99
|
+
|
|
100
|
+
// Initial alpha values
|
|
101
|
+
float ax_next = 1e9f;
|
|
102
|
+
if (std::abs(dx) > 1e-6f) {
|
|
103
|
+
float rx_entry = p1[0] + alpha_min * dx - bx;
|
|
104
|
+
int next_i = (dx > 0) ? (int)floorf(rx_entry) + 1 : (int)floorf(rx_entry);
|
|
105
|
+
ax_next = (bx + (float)next_i - p1[0]) / dx;
|
|
106
|
+
if (ax_next <= alpha_min)
|
|
107
|
+
ax_next += d_ax;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
float ay_next = 1e9f;
|
|
111
|
+
if (std::abs(dy) > 1e-6f) {
|
|
112
|
+
float ry_entry = p1[1] + alpha_min * dy - by;
|
|
113
|
+
int next_j = (dy > 0) ? (int)floorf(ry_entry) + 1 : (int)floorf(ry_entry);
|
|
114
|
+
ay_next = (by + (float)next_j - p1[1]) / dy;
|
|
115
|
+
if (ay_next <= alpha_min)
|
|
116
|
+
ay_next += d_ay;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
float alpha_curr = alpha_min;
|
|
120
|
+
float sum = 0.0f;
|
|
121
|
+
|
|
122
|
+
// Ray marching loop
|
|
123
|
+
while (alpha_curr < alpha_max - 1e-6f) {
|
|
124
|
+
float alpha_next = min(alpha_max, min(ax_next, ay_next));
|
|
125
|
+
float mid_alpha = (alpha_curr + alpha_next) * 0.5f;
|
|
126
|
+
|
|
127
|
+
int img_i = (int)floorf(p1[0] + mid_alpha * dx - bx);
|
|
128
|
+
int img_j = (int)floorf(p1[1] + mid_alpha * dy - by);
|
|
129
|
+
|
|
130
|
+
if (img_i >= 0 && img_i < height && img_j >= 0 && img_j < width) {
|
|
131
|
+
float length = (alpha_next - alpha_curr) * d_conv;
|
|
132
|
+
sum += length * image[img_i * width + img_j];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (ax_next < ay_next) {
|
|
136
|
+
ax_next += d_ax;
|
|
137
|
+
} else {
|
|
138
|
+
ay_next += d_ay;
|
|
139
|
+
}
|
|
140
|
+
alpha_curr = alpha_next;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
output[batch_index * channels * projections * detector_bins +
|
|
144
|
+
channel_index * projections * detector_bins +
|
|
145
|
+
projection_index * detector_bins + detector_index] = sum;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
void backward_siddon(float *sinogram, float *output, int batches, int channels,
|
|
152
|
+
int height, int width, int projections, int detector_bins,
|
|
153
|
+
float detector_spacing, float coverage, float distance,
|
|
154
|
+
char type) {
|
|
155
|
+
|
|
156
|
+
int thread_index = omp_get_thread_num();
|
|
157
|
+
int num_threads = omp_get_num_threads();
|
|
158
|
+
|
|
159
|
+
for (int offset = thread_index;
|
|
160
|
+
offset < batches * channels * projections * detector_bins;
|
|
161
|
+
offset += num_threads) {
|
|
162
|
+
|
|
163
|
+
int batch_index = offset / (channels * projections * detector_bins);
|
|
164
|
+
int channel_index =
|
|
165
|
+
(offset - batch_index * channels * projections * detector_bins) /
|
|
166
|
+
(projections * detector_bins);
|
|
167
|
+
int projection_index =
|
|
168
|
+
(offset - batch_index * channels * projections * detector_bins -
|
|
169
|
+
channel_index * projections * detector_bins) /
|
|
170
|
+
detector_bins;
|
|
171
|
+
int detector_index = offset -
|
|
172
|
+
batch_index * channels * projections * detector_bins -
|
|
173
|
+
channel_index * projections * detector_bins -
|
|
174
|
+
projection_index * detector_bins;
|
|
175
|
+
|
|
176
|
+
if (projection_index >= projections || detector_index >= detector_bins)
|
|
177
|
+
return;
|
|
178
|
+
|
|
179
|
+
float bx = -(height / 2.0f);
|
|
180
|
+
float by = -(width / 2.0f);
|
|
181
|
+
float u = detector_spacing * (detector_index - detector_bins / 2.0f);
|
|
182
|
+
|
|
183
|
+
float p1[2];
|
|
184
|
+
float p2[2];
|
|
185
|
+
float angle;
|
|
186
|
+
|
|
187
|
+
if (type == 'p') {
|
|
188
|
+
angle = projection_index * coverage / projections;
|
|
189
|
+
|
|
190
|
+
p1[0] = distance * cosf(angle) - u * sinf(angle);
|
|
191
|
+
p1[1] = distance * sinf(angle) + u * cosf(angle);
|
|
192
|
+
|
|
193
|
+
p2[0] = -distance * cosf(angle) - u * sinf(angle);
|
|
194
|
+
p2[1] = -distance * sinf(angle) + u * cosf(angle);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
else if (type == 'f') {
|
|
198
|
+
angle = projection_index * coverage / projections;
|
|
199
|
+
|
|
200
|
+
p1[0] = distance * sinf(angle);
|
|
201
|
+
p1[1] = distance * cosf(angle);
|
|
202
|
+
|
|
203
|
+
p2[0] = -distance * sinf(angle) + u * cosf(angle);
|
|
204
|
+
p2[1] = -distance * cosf(angle) - u * sinf(angle);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
float dx = p2[0] - p1[0];
|
|
208
|
+
float dy = p2[1] - p1[1];
|
|
209
|
+
float d_conv = sqrtf(dx * dx + dy * dy);
|
|
210
|
+
float d_ax = (std::abs(dx) > 1e-6f) ? 1.0f / std::abs(dx) : 1e9f;
|
|
211
|
+
float d_ay = (std::abs(dy) > 1e-6f) ? 1.0f / std::abs(dy) : 1e9f;
|
|
212
|
+
|
|
213
|
+
float ax_min = (std::abs(dx) > 1e-6f)
|
|
214
|
+
? min((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
215
|
+
: -1e9f;
|
|
216
|
+
float ax_max = (std::abs(dx) > 1e-6f)
|
|
217
|
+
? max((bx - p1[0]) / dx, (bx + height - p1[0]) / dx)
|
|
218
|
+
: 1e9f;
|
|
219
|
+
|
|
220
|
+
float ay_min = (std::abs(dy) > 1e-6f)
|
|
221
|
+
? min((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
222
|
+
: -1e9f;
|
|
223
|
+
float ay_max = (std::abs(dy) > 1e-6f)
|
|
224
|
+
? max((by - p1[1]) / dy, (by + width - p1[1]) / dy)
|
|
225
|
+
: 1e9f;
|
|
226
|
+
|
|
227
|
+
float alpha_min = max(0.0f, max(ax_min, ay_min));
|
|
228
|
+
float alpha_max = min(1.0f, min(ax_max, ay_max));
|
|
229
|
+
|
|
230
|
+
float ax_next = 1e9f;
|
|
231
|
+
if (std::abs(dx) > 1e-6f) {
|
|
232
|
+
float rx_entry = p1[0] + alpha_min * dx - bx;
|
|
233
|
+
int next_i = (dx > 0) ? (int)floorf(rx_entry) + 1 : (int)floorf(rx_entry);
|
|
234
|
+
ax_next = (bx + (float)next_i - p1[0]) / dx;
|
|
235
|
+
if (ax_next <= alpha_min)
|
|
236
|
+
ax_next += d_ax;
|
|
237
|
+
}
|
|
238
|
+
float ay_next = 1e9f;
|
|
239
|
+
if (std::abs(dy) > 1e-6f) {
|
|
240
|
+
float ry_entry = p1[1] + alpha_min * dy - by;
|
|
241
|
+
int next_j = (dy > 0) ? (int)floorf(ry_entry) + 1 : (int)floorf(ry_entry);
|
|
242
|
+
ay_next = (by + (float)next_j - p1[1]) / dy;
|
|
243
|
+
if (ay_next <= alpha_min)
|
|
244
|
+
ay_next += d_ay;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
float val = sinogram[batch_index * channels * projections * detector_bins +
|
|
248
|
+
channel_index * projections * detector_bins +
|
|
249
|
+
projection_index * detector_bins + detector_index];
|
|
250
|
+
float alpha_curr = alpha_min;
|
|
251
|
+
|
|
252
|
+
// --- ray march: scatter instead of gather ---
|
|
253
|
+
while (alpha_curr < alpha_max - 1e-6f) {
|
|
254
|
+
float alpha_next = min(alpha_max, min(ax_next, ay_next));
|
|
255
|
+
float mid_alpha = (alpha_curr + alpha_next) * 0.5f;
|
|
256
|
+
|
|
257
|
+
int img_i = (int)floorf(p1[0] + mid_alpha * dx - bx);
|
|
258
|
+
int img_j = (int)floorf(p1[1] + mid_alpha * dy - by);
|
|
259
|
+
|
|
260
|
+
if (img_i >= 0 && img_i < height && img_j >= 0 && img_j < width) {
|
|
261
|
+
float length = (alpha_next - alpha_curr) * d_conv;
|
|
262
|
+
output[batch_index * channels * height * width +
|
|
263
|
+
channel_index * height * width + img_i * width + img_j] +=
|
|
264
|
+
length * val;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (ax_next < ay_next)
|
|
268
|
+
ax_next += d_ax;
|
|
269
|
+
else
|
|
270
|
+
ay_next += d_ay;
|
|
271
|
+
alpha_curr = alpha_next;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
torch::Tensor projector_forward_cpu(torch::Tensor image, int projections,
|
|
279
|
+
int detector_bins, float detector_spacing,
|
|
280
|
+
float coverage, float distance, char type,
|
|
281
|
+
int num_threads) {
|
|
282
|
+
int batches = image.size(0);
|
|
283
|
+
int channels = image.size(1);
|
|
284
|
+
int height = image.size(2);
|
|
285
|
+
int width = image.size(3);
|
|
286
|
+
auto output = torch::zeros(
|
|
287
|
+
{batches, channels, projections, detector_bins},
|
|
288
|
+
torch::TensorOptions().dtype(torch::kFloat32).device(image.device()));
|
|
289
|
+
|
|
290
|
+
#pragma omp parallel num_threads(num_threads)
|
|
291
|
+
{
|
|
292
|
+
int thread_idx = omp_get_thread_num();
|
|
293
|
+
forward_siddon(image.data_ptr<float>(), output.data_ptr<float>(), batches,
|
|
294
|
+
channels, height, width, projections, detector_bins,
|
|
295
|
+
detector_spacing, coverage, distance, type);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return output;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
torch::Tensor projector_backward_cpu(torch::Tensor sinogram,
|
|
302
|
+
const std::vector<int64_t> &image_shape,
|
|
303
|
+
int projections, int detector_bins,
|
|
304
|
+
float detector_spacing, float coverage,
|
|
305
|
+
float distance, char type,
|
|
306
|
+
int num_threads) {
|
|
307
|
+
int batches = sinogram.size(0);
|
|
308
|
+
int channels = sinogram.size(1);
|
|
309
|
+
int height = image_shape[2];
|
|
310
|
+
int width = image_shape[3];
|
|
311
|
+
auto output = torch::zeros(
|
|
312
|
+
{num_threads, batches, channels, height, width},
|
|
313
|
+
torch::TensorOptions().dtype(torch::kFloat32).device(sinogram.device()));
|
|
314
|
+
|
|
315
|
+
#pragma omp parallel num_threads(num_threads)
|
|
316
|
+
{
|
|
317
|
+
int thread_idx = omp_get_thread_num();
|
|
318
|
+
backward_siddon(sinogram.data_ptr<float>(),
|
|
319
|
+
output[thread_idx].data_ptr<float>(), batches, channels,
|
|
320
|
+
height, width, projections, detector_bins, detector_spacing,
|
|
321
|
+
coverage, distance, type);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return output.sum(0);
|
|
325
|
+
}
|