forked from BRAINSia/BRAINSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDWIConvertUtils.cxx
276 lines (255 loc) · 6.55 KB
/
DWIConvertUtils.cxx
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// Created by Hans Johnson on 10/8/16.
//
#include "DWIConvertUtils.h"
#if 0
//NOT USED!
void ConvertBvecsToFromFSL(DWIMetaDataDictionaryValidator::GradientTableType& bVecs)
{
static const double FSLDesiredDirectionFlipsWRTLPS[4] = {1, -1, 1, 1};
for(DWIMetaDataDictionaryValidator::GradientTableType::iterator it = bVecs.begin(); it != bVecs.end(); ++it)
{
for(size_t i=0; i < 3; ++i)
{
(*it)[i] *= FSLDesiredDirectionFlipsWRTLPS[i];
}
}
}
#endif
void
PrintVec( const vnl_vector_fixed< double, 3 > & vec )
{
std::cerr << "[";
for ( unsigned i = 0; i < vec.size(); ++i )
{
PrintVec< double >( vec[i] );
if ( i < vec.size() - 1 )
{
std::cerr << " ";
}
}
std::cerr << "]" << std::endl;
}
void
PrintVec( const DWIMetaDataDictionaryValidator::GradientTableType & vec )
{
std::cerr << "[";
for ( unsigned i = 0; i < vec.size(); ++i )
{
PrintVec( vec[i] );
if ( i < vec.size() - 1 )
{
std::cerr << " ";
}
}
std::cerr << "]" << std::endl;
}
bool
CloseEnough( const vnl_vector_fixed< double, 3 > & a, const vnl_vector_fixed< double, 3 > & b, double magdiv )
{
if ( a.size() != b.size() )
{
std::cerr << "Vector size mismatch: " << a.size() << " " << b.size() << std::endl;
return false;
}
for ( unsigned i = 0; i < a.size(); ++i )
{
if ( !CloseEnough( a[i], b[i], magdiv ) )
{
std::cerr << "Value mismatch" << std::endl;
return false;
}
}
return true;
}
void
normalize( const DWIMetaDataDictionaryValidator::GradientDirectionType & vec, double * normedVec )
{
double norm = 0.0;
for ( unsigned j = 0; j < 3; ++j )
{
normedVec[j] = vec[j];
norm += vec[j] * vec[j];
}
norm = std::sqrt( norm );
if ( norm < 0.00001 ) // Only norm if not equal to zero
{
for ( unsigned j = 0; j < 3; ++j )
{
normedVec[j] = 0.0;
}
}
else if ( std::abs( 1.0 - norm ) > 1e-4 ) // Only normalize if not very close to 1
{
for ( unsigned j = 0; j < 3; ++j )
{
normedVec[j] /= norm;
}
}
}
int
WriteBVectors( const DWIMetaDataDictionaryValidator::GradientTableType & bVectors, const std::string & filename )
{
itk::NumberToString< double > DoubleConvert;
std::ofstream bVecFile;
bVecFile.open( filename.c_str(), std::ios::out | std::ios::binary );
bVecFile.precision( 17 ); // Max double precision
if ( !bVecFile.is_open() || !bVecFile.good() )
{
return EXIT_FAILURE;
}
// Matching formatting from dcm2niix that seems to be gaining acceptance as
// the format for these files
//
// The lines [1,2,3] is the [x,y,z]component of the gradient vector for each gradient image
for ( unsigned int index = 0; index < 3; ++index )
{
for ( unsigned int k = 0; k < bVectors.size(); ++k )
{
const char * const spacer = ( k == bVectors.size() - 1 ) ? "" : " ";
double normedVec[3];
normalize( bVectors[k], normedVec );
bVecFile << DoubleConvert( normedVec[index] ) << spacer;
}
bVecFile << std::endl;
}
bVecFile.close();
return EXIT_SUCCESS;
}
int
ReadBVals( std::vector< double > & bVals, unsigned int & bValCount, const std::string & bValFilename )
{
std::ifstream bValFile( bValFilename.c_str(), std::ifstream::in );
if ( !bValFile.good() )
{
std::cerr << "Failed to open " << bValFilename << std::endl;
return EXIT_FAILURE;
}
bVals.clear();
bValCount = 0;
while ( !bValFile.eof() )
{
double x;
bValFile >> x;
if ( bValFile.fail() )
{
break;
}
bValCount++;
bVals.push_back( x );
}
return EXIT_SUCCESS;
}
static bool
bVecFileIsHorizontalBy3Rows( const std::string & bVecFilename )
{
std::ifstream bVecFile( bVecFilename.c_str(), std::ifstream::in );
if ( !bVecFile.good() )
{
std::cerr << "Failed to open " << bVecFilename << std::endl;
return EXIT_FAILURE;
}
std::string line;
size_t num_lines = 0;
while ( std::getline( bVecFile, line, '\n' ) )
{
if ( line.size() > 5 )
{
num_lines++;
}
}
if ( num_lines > 5 ) // A minimum of 6 lines are needed for vertical format.
{
return false; // When more than 5 lines are found, assume vertical format
}
return true; // When less than 5 lines are found, assume horizontal format (only 3 lines needed)
}
int
ReadBVecs( DWIMetaDataDictionaryValidator::GradientTableType & bVecs, unsigned int & bVecCount,
const std::string & bVecFilename )
{
bool internal_bvec_organization_check = bVecFileIsHorizontalBy3Rows( bVecFilename );
std::ifstream bVecFile( bVecFilename.c_str(), std::ifstream::in );
if ( !bVecFile.good() )
{
std::cerr << "Failed to open " << bVecFilename << std::endl;
return EXIT_FAILURE;
}
bVecs.clear();
bVecCount = 0;
if ( internal_bvec_organization_check )
{
std::vector< std::vector< double > > bVecst( 3 );
for ( unsigned i = 0; i < 3; i++ )
{
std::string bvect;
std::getline( bVecFile, bvect );
bool error = false;
if ( bVecFile.fail() )
{
return EXIT_FAILURE;
}
std::istringstream issLineToString( bvect );
for ( std::istream_iterator< std::string > it = std::istream_iterator< std::string >( issLineToString );
it != std::istream_iterator< std::string >();
it++ )
{
std::istringstream iss( *it );
double val;
iss >> val;
if ( iss.fail() )
{
error = true;
break;
}
bVecst[i].push_back( val );
}
if ( error )
{
break;
}
}
for ( unsigned int i = 1; i < 3; i++ )
{
if ( bVecst[i].size() != bVecst[0].size() )
{
return EXIT_FAILURE;
}
}
bVecCount = bVecst[0].size();
// Needed to convert to/from FSL to Dicom internal conventions
for ( unsigned int i = 0; i < bVecCount; i++ )
{
double list[] = { bVecst[0][i], bVecst[1][i], bVecst[2][i] };
DWIMetaDataDictionaryValidator::GradientDirectionType x;
x[0] = list[0];
x[1] = list[1];
x[2] = list[2];
bVecs.push_back( x );
}
}
else // BVECTOR FILE AS COLUMNS
{
while ( !bVecFile.eof() )
{
DWIMetaDataDictionaryValidator::GradientDirectionType x;
for ( unsigned i = 0; i < 3; ++i )
{
double val;
bVecFile >> val;
if ( bVecFile.fail() )
{
break;
}
x[i] = ( val );
}
if ( bVecFile.fail() )
{
break;
}
bVecCount++;
bVecs.push_back( x );
}
}
return EXIT_SUCCESS;
}