OpenWalnut  1.4.0
WDataSetSegmentation.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "WDataSetScalar.h"
00029 
00030 #include "WDataSetSegmentation.h"
00031 
00032 // prototype instance as singleton
00033 boost::shared_ptr< WPrototyped > WDataSetSegmentation::m_prototype = boost::shared_ptr< WPrototyped >();
00034 
00035 WDataSetSegmentation::WDataSetSegmentation( boost::shared_ptr< WDataSetScalar > whiteMatter,
00036                                             boost::shared_ptr< WDataSetScalar > grayMatter,
00037                                             boost::shared_ptr< WDataSetScalar > cerebrospinalFluid )
00038     : WDataSetSingle( convert( whiteMatter, grayMatter, cerebrospinalFluid ), whiteMatter->getGrid() )
00039 {
00040     boost::shared_ptr< WGrid > grid( whiteMatter->getGrid() );
00041 }
00042 
00043 WDataSetSegmentation::WDataSetSegmentation( boost::shared_ptr< WValueSetBase > segmentation,
00044                                             boost::shared_ptr< WGrid > grid )
00045     : WDataSetSingle( segmentation, grid )
00046 {
00047 //     countVoxel();
00048 }
00049 
00050 WDataSetSegmentation::WDataSetSegmentation()
00051     : WDataSetSingle()
00052 {
00053     // default constructor used by the prototype mechanism
00054 }
00055 
00056 WDataSetSegmentation::~WDataSetSegmentation()
00057 {
00058 }
00059 
00060 
00061 const std::string WDataSetSegmentation::getName() const
00062 {
00063     return "WDataSetSegmentation";
00064 }
00065 
00066 const std::string WDataSetSegmentation::getDescription() const
00067 {
00068     return "Segmentation of brain into white and gray matter, and CSF.";
00069 }
00070 
00071 WDataSetSingle::SPtr WDataSetSegmentation::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
00072 {
00073     return WDataSetSingle::SPtr( new WDataSetSegmentation( newValueSet, getGrid() ) );
00074 }
00075 
00076 WDataSetSingle::SPtr WDataSetSegmentation::clone( boost::shared_ptr< WGrid > newGrid ) const
00077 {
00078     return WDataSetSingle::SPtr( new WDataSetSegmentation( getValueSet(), newGrid ) );
00079 }
00080 
00081 WDataSetSingle::SPtr WDataSetSegmentation::clone() const
00082 {
00083     return WDataSetSingle::SPtr( new WDataSetSegmentation( getValueSet(), getGrid() ) );
00084 }
00085 
00086 boost::shared_ptr< WPrototyped > WDataSetSegmentation::getPrototype()
00087 {
00088     if( !m_prototype )
00089     {
00090         m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetSegmentation() );
00091     }
00092 
00093     return m_prototype;
00094 }
00095 
00096 // uint WDataSetSegmentation::xsize() const
00097 // {
00098 //   return m_xsize;
00099 // }
00100 
00101 // uint WDataSetSegmentation::ysize() const
00102 // {b
00103 //   return m_ysize;
00104 // }
00105 
00106 // uint WDataSetSegmentation::zsize() const
00107 // {
00108 //   return m_zsize;
00109 // }
00110 
00111 float WDataSetSegmentation::getWMProbability( int x, int y, int z ) const
00112 {
00113   boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
00114   size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00115 
00116   return WDataSetSingle::getValueAt( whiteMatter + ( 3*id ) );
00117 }
00118 
00119 float WDataSetSegmentation::getGMProbability( int x, int y, int z ) const
00120 {
00121   boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
00122   size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00123 
00124   return WDataSetSingle::getValueAt( grayMatter + ( 3*id ) );
00125 }
00126 
00127 float WDataSetSegmentation::getCSFProbability( int x, int y, int z ) const
00128 {
00129     boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
00130     size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00131 
00132     return WDataSetSingle::getValueAt( csf + ( 3*id ) );
00133 }
00134 
00135 boost::shared_ptr< WValueSetBase > WDataSetSegmentation::convert( boost::shared_ptr< WDataSetScalar > whiteMatter,
00136                                                                   boost::shared_ptr< WDataSetScalar > grayMatter,
00137                                                                   boost::shared_ptr< WDataSetScalar > cerebrospinalFluid )
00138 {
00139     // valid pointer?
00140     WAssert( whiteMatter, "No white matter data given." );
00141     WAssert( grayMatter, "No gray matter data given." );
00142     WAssert( cerebrospinalFluid, "No CSF data given." );
00143 
00144     // check for same dimension of all three tissue types
00145     boost::shared_ptr< WGridRegular3D > wm_grid = boost::dynamic_pointer_cast< WGridRegular3D >( whiteMatter->getGrid() );
00146     boost::shared_ptr< WGridRegular3D > gm_grid = boost::dynamic_pointer_cast< WGridRegular3D >( grayMatter->getGrid() );
00147     boost::shared_ptr< WGridRegular3D > csf_grid = boost::dynamic_pointer_cast< WGridRegular3D >( cerebrospinalFluid->getGrid() );
00148 
00149     WAssert( ( wm_grid->getNbCoordsX() == gm_grid->getNbCoordsX() ) &&  ( gm_grid->getNbCoordsX() == csf_grid->getNbCoordsX() ),
00150              "Different X size of GrayMatter, WhiteMatter or CSF-Input" );
00151     WAssert( ( wm_grid->getNbCoordsY() == gm_grid->getNbCoordsY() ) &&  ( gm_grid->getNbCoordsY() == csf_grid->getNbCoordsY() ),
00152              "Different Y size of GrayMatter, WhiteMatter or CSF-Input" );
00153     WAssert( ( wm_grid->getNbCoordsZ() == gm_grid->getNbCoordsZ() ) &&  ( gm_grid->getNbCoordsZ() == csf_grid->getNbCoordsZ() ),
00154              "Different Z size of GrayMatter, WhiteMatter or CSF-Input" );
00155 
00156     boost::shared_ptr< WValueSetBase > segmentation;
00157     std::vector< boost::shared_ptr< WDataSetScalar > > dataSets;
00158     dataSets.push_back( whiteMatter );
00159     dataSets.push_back( grayMatter );
00160     dataSets.push_back( cerebrospinalFluid );
00161 
00162     switch( whiteMatter->getValueSet()->getDataType() )
00163     {
00164     case W_DT_UNSIGNED_CHAR:
00165     {
00166         boost::shared_ptr< std::vector< unsigned char > > data( new std::vector< unsigned char > );
00167         *data = copyDataSetsToArray< unsigned char >( dataSets );
00168         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< unsigned char >( 1, dataSets.size(), data, W_DT_UNSIGNED_CHAR ) );
00169         break;
00170     }
00171     case W_DT_INT16:
00172     {
00173         boost::shared_ptr< std::vector< int16_t > > data( new std::vector< int16_t > );
00174         *data = copyDataSetsToArray< int16_t >( dataSets );
00175         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< int16_t >( 1, dataSets.size(), data, W_DT_INT16 ) );
00176         break;
00177     }
00178     case W_DT_SIGNED_INT:
00179     {
00180         boost::shared_ptr< std::vector< int32_t > > data( new std::vector< int32_t > );
00181         *data = copyDataSetsToArray< int32_t >( dataSets );
00182         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< int32_t >( 1, dataSets.size(), data, W_DT_SIGNED_INT ) );
00183         break;
00184     }
00185     case W_DT_FLOAT:
00186     {
00187         boost::shared_ptr< std::vector< float > > data( new std::vector< float > );
00188         *data = copyDataSetsToArray< float >( dataSets );
00189         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< float >( 1, dataSets.size(), data, W_DT_FLOAT ) );
00190         break;
00191     }
00192     case W_DT_DOUBLE:
00193     {
00194         boost::shared_ptr< std::vector< double > > data( new std::vector< double > );
00195         *data = copyDataSetsToArray< double >( dataSets );
00196         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< double >( 1, dataSets.size(), data, W_DT_DOUBLE ) );
00197         break;
00198     }
00199     default:
00200         WAssert( false, "Unknown data type in dataset." );
00201     }
00202     return segmentation;
00203 }
00204 
00205 // void WDataSetSegmentation::countVoxel() const
00206 // {
00207 //   size_t WMVoxel = 0;
00208 //   size_t GMVoxel = 0;
00209 //   size_t CSFVoxel = 0;
00210 //   boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( getGrid() );
00211 //   for( size_t x = 0; x < grid->getNbCoordsX(); x++ )
00212 //     for( size_t y = 0; y < grid->getNbCoordsY(); y++ )
00213 //       for( size_t z = 0; z < grid->getNbCoordsZ(); z++ )
00214 //       {
00215 // //         std::cerr << getWMProbability( x, y, z ) << std::endl;
00216 //         if ( getWMProbability( x, y, z ) > 0.95 ) WMVoxel++;
00217 //         if ( getGMProbability( x, y, z ) > 0.95 ) GMVoxel++;
00218 //         if ( getCSFProbability( x, y, z ) > 0.95 ) CSFVoxel++;
00219 //       }
00220 //   std::cerr << "WMVoxel: " << WMVoxel << std::endl;
00221 //   std::cerr << "GMVoxel: " << GMVoxel << std::endl;
00222 //   std::cerr << "CSFVoxel: " << CSFVoxel << std::endl;
00223 // }