WDataSetSegmentation.h 7.78 KB
Newer Older
Stefan Philips's avatar
Stefan Philips committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WDATASETSEGMENTATION_H
#define WDATASETSEGMENTATION_H

#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>

#include "WDataSet.h"
#include "WDataSetScalar.h"
#include "WDataSetSingle.h"


/**
 * A dataset that stores the segmentation of the brain into CSF, white and gray matter.
 *
 * It also offers some convenience functions for this task.
 *
 * \ingroup dataHandler
 */
class WDataSetSegmentation : public WDataSetSingle
{
public:

    /**
     * Constructs an instance out of a value set and a grid.
     *
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
52 53
     * \param segmentation the value set to use
     * \param grid the grid which maps world space to the value set
Stefan Philips's avatar
Stefan Philips committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
     */
    WDataSetSegmentation( boost::shared_ptr< WValueSetBase > segmentation, boost::shared_ptr< WGrid > grid );

    /**
     * Constructs an instance out of three WDataSetScalar.
     *
     * \param whiteMatter the value set to use
     * \param grayMatter the value set to use
     * \param cerebrospinalFluid the value set to use
     */
    WDataSetSegmentation( boost::shared_ptr< WDataSetScalar > whiteMatter,
                          boost::shared_ptr< WDataSetScalar > grayMatter,
                          boost::shared_ptr< WDataSetScalar > cerebrospinalFluid );
    /**
     * Construct an empty and unusable instance. This is useful for prototypes.
     */
    WDataSetSegmentation();

    /**
     * Destroys this DataSet instance
     */
    virtual ~WDataSetSegmentation();

    /**
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
78 79 80 81 82
     * Returns the white matter probability for the given cell.
     *
     * \param x, y, z The coordinates in grid space.
     *
     * \return white matter probability.
Stefan Philips's avatar
Stefan Philips committed
83
     */
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
84
    float getWMProbability( int x, int y, int z ) const;
Stefan Philips's avatar
Stefan Philips committed
85 86

    /**
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
87 88 89
     * Returns the gray matter probability for the given cell.
     *
     * \param x, y, z The coordinates in grid space.
Stefan Philips's avatar
Stefan Philips committed
90
     *
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
91
     * \return gray matter probability.
Stefan Philips's avatar
Stefan Philips committed
92 93
     */
    float getGMProbability( int x, int y, int z ) const;
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
94 95 96 97 98 99 100 101

    /**
     * Returns the cerebrospinal fluid probability for the given cell.
     *
     * \param x, y, z The coordinates in grid space.
     *
     * \return cerebrospinal fluid probability.
     */
Stefan Philips's avatar
Stefan Philips committed
102 103
    float getCSFProbability( int x, int y, int z ) const;

Stefan Philips's avatar
[DOC]  
Stefan Philips committed
104
    // template < typename T > T getWMValueAt( int x, int y, int z ) const;
Stefan Philips's avatar
Stefan Philips committed
105

Stefan Philips's avatar
[DOC]  
Stefan Philips committed
106
    // template < typename T > T getGMValueAt( int x, int y, int z ) const;
Stefan Philips's avatar
Stefan Philips committed
107

Stefan Philips's avatar
[DOC]  
Stefan Philips committed
108
    // template < typename T > T getCSFValueAt( int x, int y, int z ) const;
Stefan Philips's avatar
Stefan Philips committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    /**
     * Gets the name of this prototype.
     *
     * \return the name.
     */
    virtual const std::string getName() const;

    /**
     * Gets the description for this prototype.
     *
     * \return the description
     */
    virtual const std::string getDescription() const;

    /**
125
     * 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
Stefan Philips's avatar
Stefan Philips committed
126 127 128 129 130 131 132 133 134
     * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
     *
     * \param newValueSet the new valueset.
     *
     * \return the clone
     */
    virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WValueSetBase > newValueSet ) const;

    /**
135
     * 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
Stefan Philips's avatar
Stefan Philips committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
     * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
     *
     * \param newGrid the new grid.
     *
     * \return the clone
     */
    virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WGrid > newGrid ) const;

    /**
     * Creates a copy (clone) of this instance. Unlike copy construction, this is a very useful function if you
     * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
     *
     * \return the clone
     */
    virtual WDataSetSingle::SPtr clone() const;

    /**
     * Returns a prototype instantiated with the true type of the deriving class.
     *
     * \return the prototype.
     */
    static boost::shared_ptr< WPrototyped > getPrototype();

Stefan Philips's avatar
[DOC]  
Stefan Philips committed
159 160 161
    /**
     * Enumerator for the three different classification types.
     */
Stefan Philips's avatar
Stefan Philips committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    enum matterType
    {
      whiteMatter = 0,
      grayMatter = 1,
      csf = 2
    };

    // double getValueAt( int x, int y, int z );

    // void countVoxel() const;

protected:
    /**
     * The prototype as singleton.
     */
    static boost::shared_ptr< WPrototyped > m_prototype;

private:
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
180
    /**
181
     * This helper function converts the probabilities given by three separate WDataSetScalars to one WValueSetBase.
Stefan Philips's avatar
[DOC]  
Stefan Philips committed
182 183 184 185 186 187 188
     *
     * \param whiteMatter the probabilities for white matter.
     * \param grayMatter the probabilities for gray matter.
     * \param cerebrospinalFluid the probabilities for cerebrospinal fluid.
     *
     * \return The probabilities in one value set.
     */
Stefan Philips's avatar
Stefan Philips committed
189 190 191 192
    static boost::shared_ptr< WValueSetBase > convert( boost::shared_ptr< WDataSetScalar > whiteMatter,
                                                       boost::shared_ptr< WDataSetScalar > grayMatter,
                                                       boost::shared_ptr< WDataSetScalar > cerebrospinalFluid );

Stefan Philips's avatar
[DOC]  
Stefan Philips committed
193 194 195 196 197 198 199
    /**
     * This helper function copies the content of several WDataSetScalars to one std::vector.
     *
     * \param dataSets the std::vector of data WDataSetScalars.
     *
     * \return The WDataSetScalars merged to a std::vector.
     */
Stefan Philips's avatar
Stefan Philips committed
200 201 202
    template< typename T > static std::vector< T > copyDataSetsToArray( const std::vector< boost::shared_ptr< WDataSetScalar > > &dataSets );
};

Stefan Philips's avatar
[DOC]  
Stefan Philips committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
template< typename T > std::vector< T >
WDataSetSegmentation::copyDataSetsToArray( const std::vector< boost::shared_ptr< WDataSetScalar > > &dataSets )
{
    const size_t voxelDim = dataSets.size();
    size_t countVoxels = 0;
    if ( !dataSets.empty() ) countVoxels = ( *dataSets.begin() )->getValueSet()->size();

    std::vector< T > data( countVoxels * voxelDim );

    // loop over images
    size_t dimIndex = 0;
    for ( std::vector< boost::shared_ptr< WDataSetScalar > >::const_iterator it = dataSets.begin(); it != dataSets.end(); it++ )
    {
      for( size_t voxelNumber = 0; voxelNumber < countVoxels; voxelNumber++ )
      {
        data[ voxelNumber * voxelDim + dimIndex ] =  ( boost::shared_static_cast< WDataSetSingle > ( *it ) )->getValueAt< T >( voxelNumber );
      }
      dimIndex++;
    }
    return data;
}


Stefan Philips's avatar
Stefan Philips committed
226 227 228 229 230 231 232 233 234 235 236 237 238
// template < typename T > T WDataSetSegmentation::getValueAt( int x, int y, int z )
// {
//     boost::shared_ptr< WValueSet< T > > vs = boost::shared_dynamic_cast< WValueSet< T > >( m_valueSet );
//     boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
//
//     size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
//
//     T v = vs->getScalar( id );
//     return v;
// }


#endif  // WDATASETSEGMENTATION_H