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