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