-
Notifications
You must be signed in to change notification settings - Fork 274
/
bart.c
202 lines (176 loc) · 8.51 KB
/
bart.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) 2015, UChicago Argonne, LLC. All rights reserved.
// Copyright 2015. UChicago Argonne, LLC. This software was produced
// under U.S. Government contract DE-AC02-06CH11357 for Argonne National
// Laboratory (ANL), which is operated by UChicago Argonne, LLC for the
// U.S. Department of Energy. The U.S. Government has rights to use,
// reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
// UChicago Argonne, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
// ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is
// modified to produce derivative works, such modified software should
// be clearly marked, so as not to confuse it with the version available
// from ANL.
// Additionally, redistribution and use in source and binary forms, with
// or without modification, are permitted provided that the following
// conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
// * Neither the name of UChicago Argonne, LLC, Argonne National
// Laboratory, ANL, the U.S. Government, nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY UChicago Argonne, LLC AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UChicago
// Argonne, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include "utils.h"
void
bart(const float* data, int dy, int dt, int dx, const float* center,
const float* theta, float* recon, int ngridx, int ngridy, int num_iter,
int num_block,
const float* ind_block) // TODO: I think ind_block should be int*
{
float* gridx = (float*) malloc((ngridx + 1) * sizeof(float));
float* gridy = (float*) malloc((ngridy + 1) * sizeof(float));
float* coordx = (float*) malloc((ngridy + 1) * sizeof(float));
float* coordy = (float*) malloc((ngridx + 1) * sizeof(float));
float* ax = (float*) malloc((ngridx + ngridy) * sizeof(float));
float* ay = (float*) malloc((ngridx + ngridy) * sizeof(float));
float* bx = (float*) malloc((ngridx + ngridy) * sizeof(float));
float* by = (float*) malloc((ngridx + ngridy) * sizeof(float));
float* coorx = (float*) malloc((ngridx + ngridy) * sizeof(float));
float* coory = (float*) malloc((ngridx + ngridy) * sizeof(float));
float* dist = (float*) malloc((ngridx + ngridy) * sizeof(float));
int* indi = (int*) malloc((ngridx + ngridy) * sizeof(int));
float* simdata = (float*) malloc((dy * dt * dx) * sizeof(float));
float* sum_dist = (float*) malloc((ngridx * ngridy) * sizeof(float));
float* update = (float*) malloc((ngridx * ngridy) * sizeof(float));
assert(coordx != NULL && coordy != NULL && ax != NULL && ay != NULL &&
by != NULL && bx != NULL && coorx != NULL && coory != NULL &&
dist != NULL && indi != NULL && simdata != NULL &&
sum_dist != NULL && update != NULL);
int s, q, p, d, i, n, os;
int quadrant;
float theta_p, sin_p, cos_p;
float mov, xi, yi;
int asize, bsize, csize;
float upd;
int ind_data, ind_recon;
float sum_dist2;
int subset_ind1, subset_ind2;
for(i = 0; i < num_iter; i++)
{
// initialize simdata to zero
memset(simdata, 0, dy * dt * dx * sizeof(float));
// For each slice
for(s = 0; s < dy; s++)
{
preprocessing(ngridx, ngridy, dx, center[s], &mov, gridx,
gridy); // Outputs: mov, gridx, gridy
subset_ind1 = dt / num_block;
subset_ind2 = subset_ind1;
// For each ordered-subset num_subset
for(os = 0; os < num_block + 1; os++)
{
if(os == num_block)
{
subset_ind2 = dt % num_block;
}
// initialize sum_dist and update to zero
memset(sum_dist, 0, (ngridx * ngridy) * sizeof(float));
memset(update, 0, (ngridx * ngridy) * sizeof(float));
// For each projection angle
for(q = 0; q < subset_ind2; q++)
{
p = ind_block[q + os * subset_ind1];
// Calculate the sin and cos values
// of the projection angle and find
// at which quadrant on the cartesian grid.
theta_p = fmodf(theta[p], 2.0f * (float) M_PI);
quadrant = calc_quadrant(theta_p);
sin_p = sinf(theta_p);
cos_p = cosf(theta_p);
// For each detector pixel
for(d = 0; d < dx; d++)
{
// Calculate coordinates
xi = -ngridx - ngridy;
yi = 0.5f * (1 - dx) + d + mov;
calc_coords(ngridx, ngridy, xi, yi, sin_p, cos_p, gridx,
gridy, coordx, coordy);
// Merge the (coordx, gridy) and (gridx, coordy)
trim_coords(ngridx, ngridy, coordx, coordy, gridx,
gridy, &asize, ax, ay, &bsize, bx, by);
// Sort the array of intersection points (ax, ay) and
// (bx, by). The new sorted intersection points are
// stored in (coorx, coory). Total number of points
// are csize.
sort_intersections(quadrant, asize, ax, ay, bsize, bx,
by, &csize, coorx, coory);
// Calculate the distances (dist) between the
// intersection points (coorx, coory). Find the
// indices of the pixels on the reconstruction grid.
calc_dist(ngridx, ngridy, csize, coorx, coory, indi,
dist);
// Calculate simdata
calc_simdata(s, p, d, ngridx, ngridy, dt, dx, csize,
indi, dist, recon,
simdata); // Output: simdata
// Calculate dist*dist
sum_dist2 = 0.0f;
for(n = 0; n < csize - 1; n++)
{
sum_dist2 += dist[n] * dist[n];
sum_dist[indi[n]] += dist[n];
}
// Update
if(sum_dist2 != 0.0f)
{
ind_data = d + p * dx + s * dt * dx;
upd = (data[ind_data] - simdata[ind_data]) /
sum_dist2;
for(n = 0; n < csize - 1; n++)
{
update[indi[n]] += upd * dist[n];
}
}
}
}
for(n = 0; n < ngridx * ngridy; n++)
{
if(sum_dist[n] != 0.0)
{
ind_recon = s * ngridx * ngridy;
recon[n + ind_recon] += update[n] / sum_dist[n];
}
}
}
}
}
free(gridx);
free(gridy);
free(coordx);
free(coordy);
free(ax);
free(ay);
free(bx);
free(by);
free(coorx);
free(coory);
free(dist);
free(indi);
free(simdata);
free(sum_dist);
free(update);
}