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