-
Notifications
You must be signed in to change notification settings - Fork 165
/
fovshift.c
148 lines (97 loc) · 3.35 KB
/
fovshift.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
/* Copyright 2022. Martin Uecker.
* All rights reserved. Use of this source code is governed by
* a BSD-style license which can be found in the LICENSE file.
*
* Authors:
* 2020-2022 Martin Uecker
*/
#include <stdbool.h>
#include <complex.h>
#include <stdlib.h>
#include "num/multind.h"
#include "num/flpmath.h"
#include "num/filter.h"
#include "num/init.h"
#include "misc/mmio.h"
#include "misc/opts.h"
#include "misc/misc.h"
#include "misc/mri.h"
static complex float* noncart_shift(long odims[DIMS], const long sdims[DIMS], const complex float* shift, const long tdims[DIMS], const complex float* tdata)
{
md_max_dims(DIMS, ~0ul, odims, tdims, sdims);
md_select_dims(DIMS, ~1u, odims, odims);
complex float* odata = md_alloc(DIMS, odims, CFL_SIZE);
md_ztenmul(DIMS, odims, odata, tdims, tdata, sdims, shift);
md_zsmul(DIMS, odims, odata, odata, +2.i * M_PI);
md_zexp(DIMS, odims, odata, odata);
return odata;
}
static const char help_str[] = "Shifts FOV.";
int main_fovshift(int argc, char* argv[argc])
{
const char* in_file = NULL;
const char* out_file = NULL;
struct arg_s args[] = {
ARG_INFILE(true, &in_file, "input"),
ARG_OUTFILE(true, &out_file, "output"),
};
float shift[3] = { 0., 0., 0. };
const char* shift_file = NULL;
const char* traj_file = NULL;
bool pixel = false;
const struct opt_s opts[] = {
OPT_INFILE('t', &traj_file, "file", "k-space trajectory"),
OPT_INFILE('S', &shift_file, "file", "FOV shift"),
OPT_FLVEC3('s', &shift, "X:Y:Z", "FOV shift"),
OPT_SET('p', &pixel, "interpret FOV shift in units of pixel instead of units of FoV")
};
cmdline(&argc, argv, ARRAY_SIZE(args), args, help_str, ARRAY_SIZE(opts), opts);
num_init();
long sdims[DIMS] = {3, [ 1 ... DIMS - 1 ] = 1 };
complex float* cshift;;
if (NULL == shift_file) {
cshift = anon_cfl(NULL, DIMS, sdims);
for (int i = 0; i < 3; i++)
cshift[i] = shift[i];
} else {
cshift = load_cfl(shift_file, DIMS, sdims);
}
long idims[DIMS];
complex float* idata = load_cfl(in_file, DIMS, idims);
if (pixel) {
if (NULL != traj_file)
error("Shift in units of pixel only possible for Cartesian k-space!\n");
complex float scale[3];
for (int i = 0; i < 3; i++)
scale[i] = 1. / idims[i];
long scl_strs[DIMS] = { CFL_SIZE, [ 1 ... DIMS - 1 ] = 0 };
md_zmul2(DIMS, sdims, MD_STRIDES(DIMS, sdims, CFL_SIZE), cshift,
MD_STRIDES(DIMS, sdims, CFL_SIZE), cshift,
scl_strs, scale);
}
long pdims[DIMS];
complex float* phase;
if (NULL != traj_file) {
long tdims[DIMS];
complex float* tdata = load_cfl(traj_file, DIMS, tdims);
phase = noncart_shift(pdims, sdims, cshift, tdims, tdata);
unmap_cfl(DIMS, tdims, tdata);
} else {
if (1 != md_nontriv_dims(DIMS, sdims))
error("Cartesian fovshift only supports the first three dimensions.\nUse bart looping for higher dimensions.\n");
md_select_dims(DIMS, FFT_FLAGS, pdims, idims);
phase = md_alloc(DIMS, pdims, CFL_SIZE);
for (int i = 0; i < 3; i++)
shift[i] = (float)idims[i] * cshift[i];
linear_phase(3, pdims, shift, phase);
}
complex float* odata = create_cfl(out_file, DIMS, idims);
md_zmul2(DIMS, idims, MD_STRIDES(DIMS, idims, CFL_SIZE), odata,
MD_STRIDES(DIMS, idims, CFL_SIZE), idata,
MD_STRIDES(DIMS, pdims, CFL_SIZE), phase);
md_free(phase);
unmap_cfl(DIMS, idims, idata);
unmap_cfl(DIMS, idims, odata);
unmap_cfl(DIMS, sdims, cshift);
return 0;
}