OpenWalnut  1.5.0dev
WDataSetSegmentation.cpp
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 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WDataSetScalar.h"
30 #include "WDataSetSegmentation.h"
31 
32 // prototype instance as singleton
33 std::shared_ptr< WPrototyped > WDataSetSegmentation::m_prototype = std::shared_ptr< WPrototyped >();
34 
35 WDataSetSegmentation::WDataSetSegmentation( std::shared_ptr< WDataSetScalar > whiteMatter,
36  std::shared_ptr< WDataSetScalar > grayMatter,
37  std::shared_ptr< WDataSetScalar > cerebrospinalFluid )
38  : WDataSetSingle( convert( whiteMatter, grayMatter, cerebrospinalFluid ), whiteMatter->getGrid() )
39 {
40  std::shared_ptr< WGrid > grid( whiteMatter->getGrid() );
41 }
42 
43 WDataSetSegmentation::WDataSetSegmentation( std::shared_ptr< WValueSetBase > segmentation,
44  std::shared_ptr< WGrid > grid )
45  : WDataSetSingle( segmentation, grid )
46 {
47 // countVoxel();
48 }
49 
51  : WDataSetSingle()
52 {
53  // default constructor used by the prototype mechanism
54 }
55 
57 {
58 }
59 
60 
61 const std::string WDataSetSegmentation::getName() const
62 {
63  return "WDataSetSegmentation";
64 }
65 
66 const std::string WDataSetSegmentation::getDescription() const
67 {
68  return "Segmentation of brain into white and gray matter, and CSF.";
69 }
70 
71 WDataSetSingle::SPtr WDataSetSegmentation::clone( std::shared_ptr< WValueSetBase > newValueSet, std::shared_ptr< WGrid > newGrid ) const
72 {
73  return WDataSetSingle::SPtr( new WDataSetSegmentation( newValueSet, newGrid ) );
74 }
75 
76 WDataSetSingle::SPtr WDataSetSegmentation::clone( std::shared_ptr< WValueSetBase > newValueSet ) const
77 {
78  return WDataSetSingle::SPtr( new WDataSetSegmentation( newValueSet, getGrid() ) );
79 }
80 
81 WDataSetSingle::SPtr WDataSetSegmentation::clone( std::shared_ptr< WGrid > newGrid ) const
82 {
83  return WDataSetSingle::SPtr( new WDataSetSegmentation( getValueSet(), newGrid ) );
84 }
85 
87 {
89 }
90 
91 std::shared_ptr< WPrototyped > WDataSetSegmentation::getPrototype()
92 {
93  if( !m_prototype )
94  {
95  m_prototype = std::shared_ptr< WPrototyped >( new WDataSetSegmentation() );
96  }
97 
98  return m_prototype;
99 }
100 
101 // uint WDataSetSegmentation::xsize() const
102 // {
103 // return m_xsize;
104 // }
105 
106 // uint WDataSetSegmentation::ysize() const
107 // {b
108 // return m_ysize;
109 // }
110 
111 // uint WDataSetSegmentation::zsize() const
112 // {
113 // return m_zsize;
114 // }
115 
116 float WDataSetSegmentation::getWMProbability( int x, int y, int z ) const
117 {
118  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
119  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
120 
121  return WDataSetSingle::getSingleRawValue( whiteMatter + ( 3*id ) );
122 }
123 
124 float WDataSetSegmentation::getGMProbability( int x, int y, int z ) const
125 {
126  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
127  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
128 
129  return WDataSetSingle::getSingleRawValue( grayMatter + ( 3*id ) );
130 }
131 
132 float WDataSetSegmentation::getCSFProbability( int x, int y, int z ) const
133 {
134  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
135  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
136 
137  return WDataSetSingle::getSingleRawValue( csf + ( 3*id ) );
138 }
139 
140 std::shared_ptr< WValueSetBase > WDataSetSegmentation::convert( std::shared_ptr< WDataSetScalar > whiteMatter,
141  std::shared_ptr< WDataSetScalar > grayMatter,
142  std::shared_ptr< WDataSetScalar > cerebrospinalFluid )
143 {
144  // valid pointer?
145  WAssert( whiteMatter, "No white matter data given." );
146  WAssert( grayMatter, "No gray matter data given." );
147  WAssert( cerebrospinalFluid, "No CSF data given." );
148 
149  // check for same dimension of all three tissue types
150  std::shared_ptr< WGridRegular3D > wm_grid = std::dynamic_pointer_cast< WGridRegular3D >( whiteMatter->getGrid() );
151  std::shared_ptr< WGridRegular3D > gm_grid = std::dynamic_pointer_cast< WGridRegular3D >( grayMatter->getGrid() );
152  std::shared_ptr< WGridRegular3D > csf_grid = std::dynamic_pointer_cast< WGridRegular3D >( cerebrospinalFluid->getGrid() );
153 
154  WAssert( ( wm_grid->getNbCoordsX() == gm_grid->getNbCoordsX() ) && ( gm_grid->getNbCoordsX() == csf_grid->getNbCoordsX() ),
155  "Different X size of GrayMatter, WhiteMatter or CSF-Input" );
156  WAssert( ( wm_grid->getNbCoordsY() == gm_grid->getNbCoordsY() ) && ( gm_grid->getNbCoordsY() == csf_grid->getNbCoordsY() ),
157  "Different Y size of GrayMatter, WhiteMatter or CSF-Input" );
158  WAssert( ( wm_grid->getNbCoordsZ() == gm_grid->getNbCoordsZ() ) && ( gm_grid->getNbCoordsZ() == csf_grid->getNbCoordsZ() ),
159  "Different Z size of GrayMatter, WhiteMatter or CSF-Input" );
160 
161  std::shared_ptr< WValueSetBase > segmentation;
162  std::vector< std::shared_ptr< WDataSetScalar > > dataSets;
163  dataSets.push_back( whiteMatter );
164  dataSets.push_back( grayMatter );
165  dataSets.push_back( cerebrospinalFluid );
166 
167  switch( whiteMatter->getValueSet()->getDataType() )
168  {
169  case W_DT_UNSIGNED_CHAR:
170  {
171  std::shared_ptr< std::vector< unsigned char > > data( new std::vector< unsigned char > );
172  *data = copyDataSetsToArray< unsigned char >( dataSets );
173  segmentation = std::shared_ptr< WValueSetBase >( new WValueSet< unsigned char >( 1, dataSets.size(), data, W_DT_UNSIGNED_CHAR ) );
174  break;
175  }
176  case W_DT_INT16:
177  {
178  std::shared_ptr< std::vector< int16_t > > data( new std::vector< int16_t > );
179  *data = copyDataSetsToArray< int16_t >( dataSets );
180  segmentation = std::shared_ptr< WValueSetBase >( new WValueSet< int16_t >( 1, dataSets.size(), data, W_DT_INT16 ) );
181  break;
182  }
183  case W_DT_SIGNED_INT:
184  {
185  std::shared_ptr< std::vector< int32_t > > data( new std::vector< int32_t > );
186  *data = copyDataSetsToArray< int32_t >( dataSets );
187  segmentation = std::shared_ptr< WValueSetBase >( new WValueSet< int32_t >( 1, dataSets.size(), data, W_DT_SIGNED_INT ) );
188  break;
189  }
190  case W_DT_FLOAT:
191  {
192  std::shared_ptr< std::vector< float > > data( new std::vector< float > );
193  *data = copyDataSetsToArray< float >( dataSets );
194  segmentation = std::shared_ptr< WValueSetBase >( new WValueSet< float >( 1, dataSets.size(), data, W_DT_FLOAT ) );
195  break;
196  }
197  case W_DT_DOUBLE:
198  {
199  std::shared_ptr< std::vector< double > > data( new std::vector< double > );
200  *data = copyDataSetsToArray< double >( dataSets );
201  segmentation = std::shared_ptr< WValueSetBase >( new WValueSet< double >( 1, dataSets.size(), data, W_DT_DOUBLE ) );
202  break;
203  }
204  default:
205  WAssert( false, "Unknown data type in dataset." );
206  }
207  return segmentation;
208 }
209 
210 // void WDataSetSegmentation::countVoxel() const
211 // {
212 // size_t WMVoxel = 0;
213 // size_t GMVoxel = 0;
214 // size_t CSFVoxel = 0;
215 // std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( getGrid() );
216 // for( size_t x = 0; x < grid->getNbCoordsX(); x++ )
217 // for( size_t y = 0; y < grid->getNbCoordsY(); y++ )
218 // for( size_t z = 0; z < grid->getNbCoordsZ(); z++ )
219 // {
220 // // std::cerr << getWMProbability( x, y, z ) << std::endl;
221 // if ( getWMProbability( x, y, z ) > 0.95 ) WMVoxel++;
222 // if ( getGMProbability( x, y, z ) > 0.95 ) GMVoxel++;
223 // if ( getCSFProbability( x, y, z ) > 0.95 ) CSFVoxel++;
224 // }
225 // std::cerr << "WMVoxel: " << WMVoxel << std::endl;
226 // std::cerr << "GMVoxel: " << GMVoxel << std::endl;
227 // std::cerr << "CSFVoxel: " << CSFVoxel << std::endl;
228 // }
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.
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...
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< WGrid > m_grid
Stores the reference of the WGrid of this DataSetSingle instance.
std::shared_ptr< WGrid > getGrid() const
double getSingleRawValue(size_t id) const
Get the raw scalar value stored at id-th position of the raw array of the value set.
std::shared_ptr< WDataSetSingle > SPtr
Convenience typedef for a std::shared_ptr.
Base Class for all value set types.
Definition: WValueSet.h:47