OpenWalnut  1.5.0dev
WDataSetSegmentation.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 WDATASETSEGMENTATION_H
26 #define WDATASETSEGMENTATION_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 
33 #include "WDataSet.h"
34 #include "WDataSetScalar.h"
35 #include "WDataSetSingle.h"
36 
37 
38 /**
39  * A dataset that stores the segmentation of the brain into CSF, white and gray matter.
40  *
41  * It also offers some convenience functions for this task.
42  *
43  * \ingroup dataHandler
44  */
46 {
47 public:
48  /**
49  * Constructs an instance out of a value set and a grid.
50  *
51  * \param segmentation the value set to use
52  * \param grid the grid which maps world space to the value set
53  */
54  WDataSetSegmentation( std::shared_ptr< WValueSetBase > segmentation, std::shared_ptr< WGrid > grid );
55 
56  /**
57  * Constructs an instance out of three WDataSetScalar.
58  *
59  * \param whiteMatter the value set to use
60  * \param grayMatter the value set to use
61  * \param cerebrospinalFluid the value set to use
62  */
63  WDataSetSegmentation( std::shared_ptr< WDataSetScalar > whiteMatter,
64  std::shared_ptr< WDataSetScalar > grayMatter,
65  std::shared_ptr< WDataSetScalar > cerebrospinalFluid );
66  /**
67  * Construct an empty and unusable instance. This is useful for prototypes.
68  */
70 
71  /**
72  * Destroys this DataSet instance
73  */
74  virtual ~WDataSetSegmentation();
75 
76  /**
77  * Returns the white matter probability for the given cell.
78  *
79  * \param x, y, z The coordinates in grid space.
80  *
81  * \return white matter probability.
82  */
83  float getWMProbability( int x, int y, int z ) const;
84 
85  /**
86  * Returns the gray matter probability for the given cell.
87  *
88  * \param x, y, z The coordinates in grid space.
89  *
90  * \return gray matter probability.
91  */
92  float getGMProbability( int x, int y, int z ) const;
93 
94  /**
95  * Returns the cerebrospinal fluid probability for the given cell.
96  *
97  * \param x, y, z The coordinates in grid space.
98  *
99  * \return cerebrospinal fluid probability.
100  */
101  float getCSFProbability( int x, int y, int z ) const;
102 
103  // template < typename T > T getWMValueAt( int x, int y, int z ) const;
104 
105  // template < typename T > T getGMValueAt( int x, int y, int z ) const;
106 
107  // template < typename T > T getCSFValueAt( int x, int y, int z ) const;
108 
109  /**
110  * Gets the name of this prototype.
111  *
112  * \return the name.
113  */
114  virtual const std::string getName() const;
115 
116  /**
117  * Gets the description for this prototype.
118  *
119  * \return the description
120  */
121  virtual const std::string getDescription() const;
122 
123  /**
124  * Creates a copy (clone) of this instance but allows one to change the valueset. Unlike copy construction, this is a very useful function if you
125  * want to keep the dynamic type of your dataset.
126  *
127  * \param newValueSet the new valueset.
128  * \param newGrid the new grid.
129  *
130  * \return the clone
131  */
132  virtual WDataSetSingle::SPtr clone( std::shared_ptr< WValueSetBase > newValueSet, std::shared_ptr< WGrid > newGrid ) const;
133 
134  /**
135  * Creates a copy (clone) of this instance but allows one to change the valueset. Unlike copy construction, this is a very useful function if you
136  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
137  *
138  * \param newValueSet the new valueset.
139  *
140  * \return the clone
141  */
142  virtual WDataSetSingle::SPtr clone( std::shared_ptr< WValueSetBase > newValueSet ) const;
143 
144  /**
145  * Creates a copy (clone) of this instance but allows one to change the grid. Unlike copy construction, this is a very useful function if you
146  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
147  *
148  * \param newGrid the new grid.
149  *
150  * \return the clone
151  */
152  virtual WDataSetSingle::SPtr clone( std::shared_ptr< WGrid > newGrid ) const;
153 
154  /**
155  * Creates a copy (clone) of this instance. Unlike copy construction, this is a very useful function if you
156  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
157  *
158  * \return the clone
159  */
160  virtual WDataSetSingle::SPtr clone() const;
161 
162  /**
163  * Returns a prototype instantiated with the true type of the deriving class.
164  *
165  * \return the prototype.
166  */
167  static std::shared_ptr< WPrototyped > getPrototype();
168 
169  /**
170  * Enumerator for the three different classification types.
171  */
173  {
174  whiteMatter = 0,
175  grayMatter = 1,
176  csf = 2
177  };
178 
179  // double getValueAt( int x, int y, int z );
180 
181  // void countVoxel() const;
182 
183 protected:
184  /**
185  * The prototype as singleton.
186  */
187  static std::shared_ptr< WPrototyped > m_prototype;
188 
189 private:
190  /**
191  * This helper function converts the probabilities given by three separate WDataSetScalars to one WValueSetBase.
192  *
193  * \param whiteMatter the probabilities for white matter.
194  * \param grayMatter the probabilities for gray matter.
195  * \param cerebrospinalFluid the probabilities for cerebrospinal fluid.
196  *
197  * \return The probabilities in one value set.
198  */
199  static std::shared_ptr< WValueSetBase > convert( std::shared_ptr< WDataSetScalar > whiteMatter,
200  std::shared_ptr< WDataSetScalar > grayMatter,
201  std::shared_ptr< WDataSetScalar > cerebrospinalFluid );
202 
203  /**
204  * This helper function copies the content of several WDataSetScalars to one std::vector.
205  *
206  * \param dataSets the std::vector of data WDataSetScalars.
207  *
208  * \return The WDataSetScalars merged to a std::vector.
209  */
210  template< typename T > static std::vector< T > copyDataSetsToArray( const std::vector< std::shared_ptr< WDataSetScalar > > &dataSets );
211 };
212 
213 template< typename T > std::vector< T >
214 WDataSetSegmentation::copyDataSetsToArray( const std::vector< std::shared_ptr< WDataSetScalar > > &dataSets )
215 {
216  const size_t voxelDim = dataSets.size();
217  size_t countVoxels = 0;
218  if( !dataSets.empty() ) countVoxels = ( *dataSets.begin() )->getValueSet()->size();
219 
220  std::vector< T > data( countVoxels * voxelDim );
221 
222  // loop over images
223  size_t dimIndex = 0;
224  for( std::vector< std::shared_ptr< WDataSetScalar > >::const_iterator it = dataSets.begin(); it != dataSets.end(); it++ )
225  {
226  for( size_t voxelNumber = 0; voxelNumber < countVoxels; voxelNumber++ )
227  {
228  data[ voxelNumber * voxelDim + dimIndex ] = ( std::static_pointer_cast< WDataSetSingle > ( *it ) )->getSingleRawValue< T >( voxelNumber );
229  }
230  dimIndex++;
231  }
232  return data;
233 }
234 
235 
236 // template < typename T > T WDataSetSegmentation::getValueAt( int x, int y, int z )
237 // {
238 // std::shared_ptr< WValueSet< T > > vs = std::dynamic_pointer_cast< WValueSet< T > >( m_valueSet );
239 // std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
240 //
241 // size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
242 //
243 // T v = vs->getScalar( id );
244 // return v;
245 // }
246 
247 
248 #endif // WDATASETSEGMENTATION_H
A dataset that stores the segmentation of the brain into CSF, white and gray matter.
WDataSetSegmentation()
Construct an empty and unusable instance.
float getGMProbability(int x, int y, int z) const
Returns the gray matter probability for the given cell.
float getCSFProbability(int x, int y, int z) const
Returns the cerebrospinal fluid probability for the given cell.
virtual const std::string getDescription() const
Gets the description for this prototype.
static std::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
float getWMProbability(int x, int y, int z) const
Returns the white matter probability for the given cell.
static std::vector< T > copyDataSetsToArray(const std::vector< std::shared_ptr< WDataSetScalar > > &dataSets)
This helper function copies the content of several WDataSetScalars to one std::vector.
virtual const std::string getName() const
Gets the name of this prototype.
static std::shared_ptr< WValueSetBase > convert(std::shared_ptr< WDataSetScalar > whiteMatter, std::shared_ptr< WDataSetScalar > grayMatter, std::shared_ptr< WDataSetScalar > cerebrospinalFluid)
This helper function converts the probabilities given by three separate WDataSetScalars to one WValue...
matterType
Enumerator for the three different classification types.
static std::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
virtual ~WDataSetSegmentation()
Destroys this DataSet instance.
virtual WDataSetSingle::SPtr clone() const
Creates a copy (clone) of this instance.
A data set consisting of a set of values based on a grid.
std::shared_ptr< WValueSetBase > getValueSet() const
std::shared_ptr< WDataSetSingle > SPtr
Convenience typedef for a std::shared_ptr.