cmtkSimpleLevelsetDevice.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 2010 SRI International
00004 //
00005 //  This file is part of the Computational Morphometry Toolkit.
00006 //
00007 //  http://www.nitrc.org/projects/cmtk/
00008 //
00009 //  The Computational Morphometry Toolkit is free software: you can
00010 //  redistribute it and/or modify it under the terms of the GNU General Public
00011 //  License as published by the Free Software Foundation, either version 3 of
00012 //  the License, or (at your option) any later version.
00013 //
00014 //  The Computational Morphometry Toolkit is distributed in the hope that it
00015 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00016 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU General Public License along
00020 //  with the Computational Morphometry Toolkit.  If not, see
00021 //  <http://www.gnu.org/licenses/>.
00022 //
00023 //  $Revision: 2398 $
00024 //
00025 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00026 //
00027 //  $LastChangedBy: torstenrohlfing $
00028 //
00029 */
00030 
00031 #include "cmtkSimpleLevelsetDevice.h"
00032 
00033 #include <Base/cmtkGaussianKernel.h>
00034 #include <Base/cmtkUnits.h>
00035 
00036 #include <System/cmtkProgress.h>
00037 
00038 #include <GPU/cmtkDeviceMemory.h>
00039 #include <GPU/cmtkDeviceUniformVolume.h>
00040 #include <GPU/cmtkDeviceUniformVolumeArray.h>
00041 #include <GPU/cmtkDeviceImageConvolution_kernels.h>
00042 #include <GPU/cmtkDeviceThresholdData_kernels.h>
00043 #include <GPU/cmtkSimpleLevelsetDevice_kernels.h>
00044 
00045 #include <vector>
00046 
00047 void
00048 cmtk::SimpleLevelsetDevice
00049 ::Evolve( const int numberOfIterations, const bool forceIterations )
00050 {
00051   FixedVector< 3, std::vector<float> > kernels;
00052 
00053   for ( int dim = 0; dim < 3; ++dim )
00054     {
00055     kernels[dim] = GaussianKernel<float>::GetSymmetricKernel( this->m_FilterSigma / this->m_Volume->Deltas()[dim], 0.01 /*maxError*/ );
00056     }
00057 
00058   const size_t numberOfPixels = this->m_Levelset->GetNumberOfPixels();
00059 
00060   DeviceUniformVolume::SmartPtr deviceVolume = DeviceUniformVolume::Create( *(this->m_Volume) );
00061   DeviceUniformVolumeArray::SmartPtr deviceLevelset = DeviceUniformVolumeArray::Create( *(this->m_Levelset) );
00062   
00063   DeviceMemory<float>::SmartPtr temporary = DeviceMemory<float>::Create( numberOfPixels );
00064 
00065   int nInsideOld = 0, nInside = 1;
00066 
00067   Progress::Begin( 0, numberOfIterations, 1, "Levelset Evolution" );
00068   for ( int it = 0; (it < numberOfIterations) && ((nInside!=nInsideOld) || forceIterations); ++it )
00069     {
00070     Progress::SetProgress( it );
00071 
00072     DeviceImageConvolution( temporary->Ptr(), this->m_Volume->GetDims().begin(), deviceLevelset->GetDeviceArrayPtr()->GetArrayOnDevice(), 
00073                             kernels[0].size(), &kernels[0][0], kernels[1].size(), &kernels[1][0], kernels[2].size(), &kernels[2][0] );
00074     
00075     float insideSum, outsideSum;
00076     SimpleLevelsetDeviceUpdateInsideOutside( temporary->Ptr(), deviceVolume->GetDataOnDevice().Ptr(), numberOfPixels, &insideSum, &outsideSum, &nInside );
00077 
00078     const int nOutside = numberOfPixels - nInside;
00079     SimpleLevelsetDeviceUpdateLevelset( temporary->Ptr(), deviceVolume->GetDataOnDevice().Ptr(), numberOfPixels, insideSum / nInside, outsideSum / nOutside, 1.0 * nInside / nOutside, this->m_TimeDelta, this->m_LevelsetThreshold );
00080 
00081     deviceLevelset->GetDeviceArrayPtr()->CopyOnDeviceToArray( temporary->Ptr() );
00082     }
00083 
00084   temporary->CopyToHost( this->m_Levelset->GetData()->GetDataPtr(), numberOfPixels );
00085 
00086   Progress::Done();
00087 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines