OpenWalnut  1.4.0
WDataSetSegmentation.h
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 #ifndef WDATASETSEGMENTATION_H
00026 #define WDATASETSEGMENTATION_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 
00033 #include "WDataSet.h"
00034 #include "WDataSetScalar.h"
00035 #include "WDataSetSingle.h"
00036 
00037 
00038 /**
00039  * A dataset that stores the segmentation of the brain into CSF, white and gray matter.
00040  *
00041  * It also offers some convenience functions for this task.
00042  *
00043  * \ingroup dataHandler
00044  */
00045 class WDataSetSegmentation : public WDataSetSingle
00046 {
00047 public:
00048     /**
00049      * Constructs an instance out of a value set and a grid.
00050      *
00051      * \param segmentation the value set to use
00052      * \param grid the grid which maps world space to the value set
00053      */
00054     WDataSetSegmentation( boost::shared_ptr< WValueSetBase > segmentation, boost::shared_ptr< WGrid > grid );
00055 
00056     /**
00057      * Constructs an instance out of three WDataSetScalar.
00058      *
00059      * \param whiteMatter the value set to use
00060      * \param grayMatter the value set to use
00061      * \param cerebrospinalFluid the value set to use
00062      */
00063     WDataSetSegmentation( boost::shared_ptr< WDataSetScalar > whiteMatter,
00064                           boost::shared_ptr< WDataSetScalar > grayMatter,
00065                           boost::shared_ptr< WDataSetScalar > cerebrospinalFluid );
00066     /**
00067      * Construct an empty and unusable instance. This is useful for prototypes.
00068      */
00069     WDataSetSegmentation();
00070 
00071     /**
00072      * Destroys this DataSet instance
00073      */
00074     virtual ~WDataSetSegmentation();
00075 
00076     /**
00077      * Returns the white matter probability for the given cell.
00078      *
00079      * \param x, y, z The coordinates in grid space.
00080      *
00081      * \return white matter probability.
00082      */
00083     float getWMProbability( int x, int y, int z ) const;
00084 
00085     /**
00086      * Returns the gray matter probability for the given cell.
00087      *
00088      * \param x, y, z The coordinates in grid space.
00089      *
00090      * \return gray matter probability.
00091      */
00092     float getGMProbability( int x, int y, int z ) const;
00093 
00094     /**
00095      * Returns the cerebrospinal fluid probability for the given cell.
00096      *
00097      * \param x, y, z The coordinates in grid space.
00098      *
00099      * \return cerebrospinal fluid probability.
00100      */
00101     float getCSFProbability( int x, int y, int z ) const;
00102 
00103     // template < typename T > T getWMValueAt( int x, int y, int z ) const;
00104 
00105     // template < typename T > T getGMValueAt( int x, int y, int z ) const;
00106 
00107     // template < typename T > T getCSFValueAt( int x, int y, int z ) const;
00108 
00109     /**
00110      * Gets the name of this prototype.
00111      *
00112      * \return the name.
00113      */
00114     virtual const std::string getName() const;
00115 
00116     /**
00117      * Gets the description for this prototype.
00118      *
00119      * \return the description
00120      */
00121     virtual const std::string getDescription() const;
00122 
00123     /**
00124      * Creates a copy (clone) of this instance but allows one to change the valueset. Unlike copy construction, this is a very useful function if you
00125      * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
00126      *
00127      * \param newValueSet the new valueset.
00128      *
00129      * \return the clone
00130      */
00131     virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WValueSetBase > newValueSet ) const;
00132 
00133     /**
00134      * Creates a copy (clone) of this instance but allows one to change the grid. Unlike copy construction, this is a very useful function if you
00135      * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
00136      *
00137      * \param newGrid the new grid.
00138      *
00139      * \return the clone
00140      */
00141     virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WGrid > newGrid ) const;
00142 
00143     /**
00144      * Creates a copy (clone) of this instance. Unlike copy construction, this is a very useful function if you
00145      * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
00146      *
00147      * \return the clone
00148      */
00149     virtual WDataSetSingle::SPtr clone() const;
00150 
00151     /**
00152      * Returns a prototype instantiated with the true type of the deriving class.
00153      *
00154      * \return the prototype.
00155      */
00156     static boost::shared_ptr< WPrototyped > getPrototype();
00157 
00158     /**
00159      * Enumerator for the three different classification types.
00160      */
00161     enum matterType
00162     {
00163       whiteMatter = 0,
00164       grayMatter = 1,
00165       csf = 2
00166     };
00167 
00168     // double getValueAt( int x, int y, int z );
00169 
00170     // void countVoxel() const;
00171 
00172 protected:
00173     /**
00174      * The prototype as singleton.
00175      */
00176     static boost::shared_ptr< WPrototyped > m_prototype;
00177 
00178 private:
00179     /**
00180      * This helper function converts the probabilities given by three separate WDataSetScalars to one WValueSetBase.
00181      *
00182      * \param whiteMatter the probabilities for white matter.
00183      * \param grayMatter the probabilities for gray matter.
00184      * \param cerebrospinalFluid the probabilities for cerebrospinal fluid.
00185      *
00186      * \return The probabilities in one value set.
00187      */
00188     static boost::shared_ptr< WValueSetBase > convert( boost::shared_ptr< WDataSetScalar > whiteMatter,
00189                                                        boost::shared_ptr< WDataSetScalar > grayMatter,
00190                                                        boost::shared_ptr< WDataSetScalar > cerebrospinalFluid );
00191 
00192     /**
00193      * This helper function copies the content of several WDataSetScalars to one std::vector.
00194      *
00195      * \param dataSets the std::vector of data WDataSetScalars.
00196      *
00197      * \return The WDataSetScalars merged to a std::vector.
00198      */
00199     template< typename T > static std::vector< T > copyDataSetsToArray( const std::vector< boost::shared_ptr< WDataSetScalar > > &dataSets );
00200 };
00201 
00202 template< typename T > std::vector< T >
00203 WDataSetSegmentation::copyDataSetsToArray( const std::vector< boost::shared_ptr< WDataSetScalar > > &dataSets )
00204 {
00205     const size_t voxelDim = dataSets.size();
00206     size_t countVoxels = 0;
00207     if( !dataSets.empty() ) countVoxels = ( *dataSets.begin() )->getValueSet()->size();
00208 
00209     std::vector< T > data( countVoxels * voxelDim );
00210 
00211     // loop over images
00212     size_t dimIndex = 0;
00213     for( std::vector< boost::shared_ptr< WDataSetScalar > >::const_iterator it = dataSets.begin(); it != dataSets.end(); it++ )
00214     {
00215       for( size_t voxelNumber = 0; voxelNumber < countVoxels; voxelNumber++ )
00216       {
00217         data[ voxelNumber * voxelDim + dimIndex ] =  ( boost::static_pointer_cast< WDataSetSingle > ( *it ) )->getValueAt< T >( voxelNumber );
00218       }
00219       dimIndex++;
00220     }
00221     return data;
00222 }
00223 
00224 
00225 // template < typename T > T WDataSetSegmentation::getValueAt( int x, int y, int z )
00226 // {
00227 //     boost::shared_ptr< WValueSet< T > > vs = boost::dynamic_pointer_cast< WValueSet< T > >( m_valueSet );
00228 //     boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
00229 //
00230 //     size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
00231 //
00232 //     T v = vs->getScalar( id );
00233 //     return v;
00234 // }
00235 
00236 
00237 #endif  // WDATASETSEGMENTATION_H