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 #include <boost/shared_ptr.hpp>
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( boost::shared_ptr< WValueSetBase > segmentation, boost::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( boost::shared_ptr< WDataSetScalar > whiteMatter,
64  boost::shared_ptr< WDataSetScalar > grayMatter,
65  boost::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 even if you just have a WDataSetSingle.
126  *
127  * \param newValueSet the new valueset.
128  *
129  * \return the clone
130  */
131  virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WValueSetBase > newValueSet ) const;
132 
133  /**
134  * 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
135  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
136  *
137  * \param newGrid the new grid.
138  *
139  * \return the clone
140  */
141  virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WGrid > newGrid ) const;
142 
143  /**
144  * Creates a copy (clone) of this instance. Unlike copy construction, this is a very useful function if you
145  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
146  *
147  * \return the clone
148  */
149  virtual WDataSetSingle::SPtr clone() const;
150 
151  /**
152  * Returns a prototype instantiated with the true type of the deriving class.
153  *
154  * \return the prototype.
155  */
156  static boost::shared_ptr< WPrototyped > getPrototype();
157 
158  /**
159  * Enumerator for the three different classification types.
160  */
162  {
163  whiteMatter = 0,
164  grayMatter = 1,
165  csf = 2
166  };
167 
168  // double getValueAt( int x, int y, int z );
169 
170  // void countVoxel() const;
171 
172 protected:
173  /**
174  * The prototype as singleton.
175  */
176  static boost::shared_ptr< WPrototyped > m_prototype;
177 
178 private:
179  /**
180  * This helper function converts the probabilities given by three separate WDataSetScalars to one WValueSetBase.
181  *
182  * \param whiteMatter the probabilities for white matter.
183  * \param grayMatter the probabilities for gray matter.
184  * \param cerebrospinalFluid the probabilities for cerebrospinal fluid.
185  *
186  * \return The probabilities in one value set.
187  */
188  static boost::shared_ptr< WValueSetBase > convert( boost::shared_ptr< WDataSetScalar > whiteMatter,
189  boost::shared_ptr< WDataSetScalar > grayMatter,
190  boost::shared_ptr< WDataSetScalar > cerebrospinalFluid );
191 
192  /**
193  * This helper function copies the content of several WDataSetScalars to one std::vector.
194  *
195  * \param dataSets the std::vector of data WDataSetScalars.
196  *
197  * \return The WDataSetScalars merged to a std::vector.
198  */
199  template< typename T > static std::vector< T > copyDataSetsToArray( const std::vector< boost::shared_ptr< WDataSetScalar > > &dataSets );
200 };
201 
202 template< typename T > std::vector< T >
203 WDataSetSegmentation::copyDataSetsToArray( const std::vector< boost::shared_ptr< WDataSetScalar > > &dataSets )
204 {
205  const size_t voxelDim = dataSets.size();
206  size_t countVoxels = 0;
207  if( !dataSets.empty() ) countVoxels = ( *dataSets.begin() )->getValueSet()->size();
208 
209  std::vector< T > data( countVoxels * voxelDim );
210 
211  // loop over images
212  size_t dimIndex = 0;
213  for( std::vector< boost::shared_ptr< WDataSetScalar > >::const_iterator it = dataSets.begin(); it != dataSets.end(); it++ )
214  {
215  for( size_t voxelNumber = 0; voxelNumber < countVoxels; voxelNumber++ )
216  {
217  data[ voxelNumber * voxelDim + dimIndex ] = ( boost::static_pointer_cast< WDataSetSingle > ( *it ) )->getValueAt< T >( voxelNumber );
218  }
219  dimIndex++;
220  }
221  return data;
222 }
223 
224 
225 // template < typename T > T WDataSetSegmentation::getValueAt( int x, int y, int z )
226 // {
227 // boost::shared_ptr< WValueSet< T > > vs = boost::dynamic_pointer_cast< WValueSet< T > >( m_valueSet );
228 // boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
229 //
230 // size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
231 //
232 // T v = vs->getScalar( id );
233 // return v;
234 // }
235 
236 
237 #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.