OpenWalnut  1.5.0dev
WReaderNIfTI.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WREADERNIFTI_H
26 #define WREADERNIFTI_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <nifti1_io.h> // NOLINT: brainlint thinks this is C System Header
33 
34 #include "core/common/math/WMatrix.h"
35 #include "core/dataHandler/WDataSet.h"
36 #include "core/dataHandler/io/WReader.h"
37 
38 /**
39  * Reader for the NIfTI file format. For NIfTI just see http://nifti.nimh.nih.gov/.
40  * \ingroup dataHandler
41  */
42 class WReaderNIfTI : public WReader // NOLINT
43 {
44 /**
45  * Only UnitTests may be friends.
46  */
47 friend class WReaderNIfTITest;
48 
49 public:
50  /**
51  * Constructs a loader to be executed in its own thread and ets the data needed
52  * for the loader when executed in its own thread.
53  * \param fileName this file will be loaded
54  */
55  explicit WReaderNIfTI( std::string fileName );
56 
57  /**
58  * Loads the dataset.
59  *
60  * \param dataSetType This parameter can be used to tell the function in advance how it should interprete the data.
61  *
62  * \return the dataset loaded.
63  */
64  virtual std::shared_ptr< WDataSet > load( DataSetType dataSetType = W_DATASET_NONE );
65 
66  /**
67  * Returns a standard transformation.
68  *
69  * \return A Wmatrix that represents the dataset's transformation.
70  */
72 
73  /**
74  * Returns the SForm transformation stored in the nifti file's header.
75  *
76  * \return A Wmatrix that represents the dataset's transformation.
77  */
79 
80  /**
81  * Returns the QForm transformation stored in the nifti file's header.
82  *
83  * \return A Wmatrix that represents the dataset's transformation.
84  */
86 
87 protected:
88  /**
89  * Shorthand type for a vector of gradients.
90  */
91  typedef std::shared_ptr< std::vector< WVector3d > > GradVec;
92 
93  /** Shorthand type for a vector of bvalues. The comopents are typically given as floats, as partical b-values start
94  * at 0 and are at maximum around 17000 (see: https://dx.doi.org/10.1002/mrm.20642).
95  */
96  typedef std::shared_ptr< std::vector< float > > BValues;
97 
98  /**
99  * Reads the additional bval file if available. The file format should be (ASCII file):
100  * - for each (x,y,z) component the bvalue must be repeated in such a form:
101  *
102  * bval for_x-component of the first gradient followed by a newline
103  * bval for x-component of the second gradient followed by a newline
104  * ...
105  * bval for x-compoennt of the last gradient followed by a newline
106  * bval for_y-component of the first gradient followed by a newline
107  * ...
108  * bval for y-compoennt of the last gradient followed by a newline
109  * bval for_z-component of the first gradient followed by a newline
110  * ...
111  * bval for z-compoennt of the last gradient followed by a newline
112  *
113  * For example: three gradients, with bvalues: 0, 1000, and 1000.
114  *
115  * 0
116  * 1000
117  * 1000
118  * 0
119  * 1000
120  * 1000
121  * 0
122  * 1000
123  * 1000
124  *
125  * for three gradients, where first all bvalues for all x-components must be given, each in a separate line, then all bavlues
126  * for all y-components, and finally the same for the z-components. Thus there should be three times of the number of
127  * gradients be present in such a file.
128  *
129  * \param vDim Nifti dimension
130  *
131  * \return Null ptr if not available or in case of error during read, otherwise the pointer of the vector of BValues.
132  */
133  BValues readBValuesIfAvailable( unsigned int vDim );
134 
135  /**
136  * Reads the additional gradient file if available. The file format should be (ASCII file):
137  * There are three lines, each containing the x-, y-, and z-components of the gradients.
138  * The first line contains all x-components of all gradients. Thus each lines should have the same number of values, each
139  * separated by whitespace.
140  *
141  * x-comp._of_the_first_gradient x-comp._of_the_second_gradient ... x-comp._of_the_last_gradient followed by a newline
142  * y-comp._of_the_first_gradient y-comp._of_the_second_gradient ... y-comp._of_the_last_gradient followed by a newline
143  * z-comp._of_the_first_gradient z-comp._of_the_second_gradient ... z-comp._of_the_last_gradient followed by a newline
144  *
145  * For example: three gradients:
146  *
147  * 0 -0.530466 -0.294864
148  * 0 -0.558441 -0.285613
149  * 0 0.637769 -0.911856
150  *
151  * \param vDim Nifti dimension
152  *
153  * \return Null ptr if not available or in case of error during read, otherwise the pointer of the vector of gradients.
154  */
155  GradVec readGradientsIfAvailable( unsigned int vDim );
156 
157 private:
158  /**
159  * This function allows one to copy the data given as a T*
160  * by niftilibio into a std::vector< T >
161  * \param dataArray data to copy
162  * \param countVoxels number of voxels stored in dataArray
163  * \param vDim number of values per voxel
164  *
165  * \return the copy
166  */
167  template < typename T > std::shared_ptr< std::vector< T > > copyArray( const T* dataArray, const size_t countVoxels, const size_t vDim );
168 
169  /**
170  * This function converts a 4x4 matrix from the NIfTI libs into the format
171  * used by OpenWalnut.
172  * \param in this matrix will be converted.
173  *
174  * \return the new matrix
175  */
176  WMatrix< double > convertMatrix( const mat44& in );
177 
178  //! the sform transform stored in the file header
180 
181  //! the qform transform stored in the file header
183 };
184 
185 #endif // WREADERNIFTI_H
test class the nifti reader class
Reader for the NIfTI file format.
Definition: WReaderNIfTI.h:43
WMatrix< double > getQFormTransform() const
Returns the QForm transformation stored in the nifti file's header.
WMatrix< double > convertMatrix(const mat44 &in)
This function converts a 4x4 matrix from the NIfTI libs into the format used by OpenWalnut.
WMatrix< double > m_sform
the sform transform stored in the file header
Definition: WReaderNIfTI.h:179
WReaderNIfTI(std::string fileName)
Constructs a loader to be executed in its own thread and ets the data needed for the loader when exec...
GradVec readGradientsIfAvailable(unsigned int vDim)
Reads the additional gradient file if available.
WMatrix< double > getSFormTransform() const
Returns the SForm transformation stored in the nifti file's header.
WMatrix< double > m_qform
the qform transform stored in the file header
Definition: WReaderNIfTI.h:182
std::shared_ptr< std::vector< T > > copyArray(const T *dataArray, const size_t countVoxels, const size_t vDim)
This function allows one to copy the data given as a T* by niftilibio into a std::vector< T >
std::shared_ptr< std::vector< WVector3d > > GradVec
Shorthand type for a vector of gradients.
Definition: WReaderNIfTI.h:91
std::shared_ptr< std::vector< float > > BValues
Shorthand type for a vector of bvalues.
Definition: WReaderNIfTI.h:96
WMatrix< double > getStandardTransform() const
Returns a standard transformation.
virtual std::shared_ptr< WDataSet > load(DataSetType dataSetType=W_DATASET_NONE)
Loads the dataset.
BValues readBValuesIfAvailable(unsigned int vDim)
Reads the additional bval file if available.
Read some data from a given file.
Definition: WReader.h:40
DataSetType
Data set types.