OpenWalnut 1.2.5
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 #include "../kernel/WKernel.h"
00030 
00031 #include "WDataSetSegmentation.h"
00032 
00033 // prototype instance as singleton
00034 boost::shared_ptr< WPrototyped > WDataSetSegmentation::m_prototype = boost::shared_ptr< WPrototyped >();
00035 
00036 WDataSetSegmentation::WDataSetSegmentation( boost::shared_ptr< WDataSetScalar > whiteMatter,
00037                                             boost::shared_ptr< WDataSetScalar > grayMatter,
00038                                             boost::shared_ptr< WDataSetScalar > cerebrospinalFluid )
00039     : WDataSetSingle( convert( whiteMatter, grayMatter, cerebrospinalFluid ), whiteMatter->getGrid() )
00040 {
00041     boost::shared_ptr< WGrid > grid( whiteMatter->getGrid() );
00042 }
00043 
00044 WDataSetSegmentation::WDataSetSegmentation( boost::shared_ptr< WValueSetBase > segmentation,
00045                                             boost::shared_ptr< WGrid > grid )
00046     : WDataSetSingle( segmentation, grid )
00047 {
00048 //     countVoxel();
00049 }
00050 
00051 WDataSetSegmentation::WDataSetSegmentation()
00052     : WDataSetSingle()
00053 {
00054     // default constructor used by the prototype mechanism
00055 }
00056 
00057 WDataSetSegmentation::~WDataSetSegmentation()
00058 {
00059 }
00060 
00061 
00062 const std::string WDataSetSegmentation::getName() const
00063 {
00064     return "WDataSetSegmentation";
00065 }
00066 
00067 const std::string WDataSetSegmentation::getDescription() const
00068 {
00069     return "Segmentation of brain into white and gray matter, and CSF.";
00070 }
00071 
00072 WDataSetSingle::SPtr WDataSetSegmentation::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
00073 {
00074     return WDataSetSingle::SPtr( new WDataSetSegmentation( newValueSet, getGrid() ) );
00075 }
00076 
00077 WDataSetSingle::SPtr WDataSetSegmentation::clone( boost::shared_ptr< WGrid > newGrid ) const
00078 {
00079     return WDataSetSingle::SPtr( new WDataSetSegmentation( getValueSet(), newGrid ) );
00080 }
00081 
00082 WDataSetSingle::SPtr WDataSetSegmentation::clone() const
00083 {
00084     return WDataSetSingle::SPtr( new WDataSetSegmentation( getValueSet(), getGrid() ) );
00085 }
00086 
00087 boost::shared_ptr< WPrototyped > WDataSetSegmentation::getPrototype()
00088 {
00089     if ( !m_prototype )
00090     {
00091         m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetSegmentation() );
00092     }
00093 
00094     return m_prototype;
00095 }
00096 
00097 // uint WDataSetSegmentation::xsize() const
00098 // {
00099 //   return m_xsize;
00100 // }
00101 
00102 // uint WDataSetSegmentation::ysize() const
00103 // {b
00104 //   return m_ysize;
00105 // }
00106 
00107 // uint WDataSetSegmentation::zsize() const
00108 // {
00109 //   return m_zsize;
00110 // }
00111 
00112 float WDataSetSegmentation::getWMProbability( int x, int y, int z ) const
00113 {
00114   boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
00115   size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00116 
00117   return WDataSetSingle::getValueAt( whiteMatter + ( 3*id ) );
00118 }
00119 
00120 float WDataSetSegmentation::getGMProbability( int x, int y, int z ) const
00121 {
00122   boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
00123   size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00124 
00125   return WDataSetSingle::getValueAt( grayMatter + ( 3*id ) );
00126 }
00127 
00128 float WDataSetSegmentation::getCSFProbability( int x, int y, int z ) const
00129 {
00130     boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
00131     size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00132 
00133     return WDataSetSingle::getValueAt( csf + ( 3*id ) );
00134 }
00135 
00136 boost::shared_ptr< WValueSetBase > WDataSetSegmentation::convert( boost::shared_ptr< WDataSetScalar > whiteMatter,
00137                                                                   boost::shared_ptr< WDataSetScalar > grayMatter,
00138                                                                   boost::shared_ptr< WDataSetScalar > cerebrospinalFluid )
00139 {
00140     // valid pointer?
00141     WAssert( whiteMatter, "No white matter data given." );
00142     WAssert( grayMatter, "No gray matter data given." );
00143     WAssert( cerebrospinalFluid, "No CSF data given." );
00144 
00145     // check for same dimension of all three tissue types
00146     boost::shared_ptr< WGridRegular3D > wm_grid = boost::shared_dynamic_cast< WGridRegular3D >( whiteMatter->getGrid() );
00147     boost::shared_ptr< WGridRegular3D > gm_grid = boost::shared_dynamic_cast< WGridRegular3D >( grayMatter->getGrid() );
00148     boost::shared_ptr< WGridRegular3D > csf_grid = boost::shared_dynamic_cast< WGridRegular3D >( cerebrospinalFluid->getGrid() );
00149 
00150     WAssert( ( wm_grid->getNbCoordsX() == gm_grid->getNbCoordsX() ) &&  ( gm_grid->getNbCoordsX() == csf_grid->getNbCoordsX() ),
00151              "Different X size of GrayMatter, WhiteMatter or CSF-Input" );
00152     WAssert( ( wm_grid->getNbCoordsY() == gm_grid->getNbCoordsY() ) &&  ( gm_grid->getNbCoordsY() == csf_grid->getNbCoordsY() ),
00153              "Different Y size of GrayMatter, WhiteMatter or CSF-Input" );
00154     WAssert( ( wm_grid->getNbCoordsZ() == gm_grid->getNbCoordsZ() ) &&  ( gm_grid->getNbCoordsZ() == csf_grid->getNbCoordsZ() ),
00155              "Different Z size of GrayMatter, WhiteMatter or CSF-Input" );
00156 
00157     boost::shared_ptr< WValueSetBase > segmentation;
00158     std::vector< boost::shared_ptr< WDataSetScalar > > dataSets;
00159     dataSets.push_back( whiteMatter );
00160     dataSets.push_back( grayMatter );
00161     dataSets.push_back( cerebrospinalFluid );
00162 
00163     switch( whiteMatter->getValueSet()->getDataType() )
00164     {
00165     case W_DT_UNSIGNED_CHAR:
00166     {
00167         boost::shared_ptr< std::vector< unsigned char > > data( new std::vector< unsigned char > );
00168         *data = copyDataSetsToArray< unsigned char >( dataSets );
00169         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< unsigned char >( 1, dataSets.size(), data, W_DT_UNSIGNED_CHAR ) );
00170         break;
00171     }
00172     case W_DT_INT16:
00173     {
00174         boost::shared_ptr< std::vector< int16_t > > data( new std::vector< int16_t > );
00175         *data = copyDataSetsToArray< int16_t >( dataSets );
00176         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< int16_t >( 1, dataSets.size(), data, W_DT_INT16 ) );
00177         break;
00178     }
00179     case W_DT_SIGNED_INT:
00180     {
00181         boost::shared_ptr< std::vector< int32_t > > data( new std::vector< int32_t > );
00182         *data = copyDataSetsToArray< int32_t >( dataSets );
00183         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< int32_t >( 1, dataSets.size(), data, W_DT_SIGNED_INT ) );
00184         break;
00185     }
00186     case W_DT_FLOAT:
00187     {
00188         boost::shared_ptr< std::vector< float > > data( new std::vector< float > );
00189         *data = copyDataSetsToArray< float >( dataSets );
00190         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< float >( 1, dataSets.size(), data, W_DT_FLOAT ) );
00191         break;
00192     }
00193     case W_DT_DOUBLE:
00194     {
00195         boost::shared_ptr< std::vector< double > > data( new std::vector< double > );
00196         *data = copyDataSetsToArray< double >( dataSets );
00197         segmentation = boost::shared_ptr< WValueSetBase >( new WValueSet< double >( 1, dataSets.size(), data, W_DT_DOUBLE ) );
00198         break;
00199     }
00200     default:
00201         WAssert( false, "Unknown data type in dataset." );
00202     }
00203     return segmentation;
00204 }
00205 
00206 // void WDataSetSegmentation::countVoxel() const
00207 // {
00208 //   size_t WMVoxel = 0;
00209 //   size_t GMVoxel = 0;
00210 //   size_t CSFVoxel = 0;
00211 //   boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( getGrid() );
00212 //   for( size_t x = 0; x < grid->getNbCoordsX(); x++ )
00213 //     for( size_t y = 0; y < grid->getNbCoordsY(); y++ )
00214 //       for( size_t z = 0; z < grid->getNbCoordsZ(); z++ )
00215 //       {
00216 // //         std::cerr << getWMProbability( x, y, z ) << std::endl;
00217 //         if ( getWMProbability( x, y, z ) > 0.95 ) WMVoxel++;
00218 //         if ( getGMProbability( x, y, z ) > 0.95 ) GMVoxel++;
00219 //         if ( getCSFProbability( x, y, z ) > 0.95 ) CSFVoxel++;
00220 //       }
00221 //   std::cerr << "WMVoxel: " << WMVoxel << std::endl;
00222 //   std::cerr << "GMVoxel: " << GMVoxel << std::endl;
00223 //   std::cerr << "CSFVoxel: " << CSFVoxel << std::endl;
00224 // }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends