cmtkUniformVolume_Crop.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2731 $
00026 //
00027 //  $LastChangedDate: 2011-01-13 16:22:47 -0800 (Thu, 13 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkUniformVolume.h"
00034 
00035 namespace
00036 cmtk
00037 {
00038 
00041 
00042 void
00043 UniformVolume::SetHighResCropRegion
00044 ( const Self::CoordinateRegionType& crop )
00045 {
00046   if ( !this->m_HighResCropRegion )
00047     this->m_HighResCropRegion = Self::CoordinateRegionType::SmartPtr( new CoordinateRegionType );
00048 
00049   *this->m_HighResCropRegion = crop;
00050 
00051   for ( int dim = 0; dim<3; ++dim )
00052     {
00053     this->CropRegion().From()[dim] = std::max<Self::IndexType::ValueType>( static_cast<Self::IndexType::ValueType>( (crop.From()[dim] - this->m_Offset[dim]) / this->m_Delta[dim] ), 0 );
00054 //    this->CropRegion().From()[dim] = std::max<Self::IndexType::ValueType>( static_cast<Self::IndexType::ValueType>( ceil( (crop.From()[dim] - this->m_Offset[dim]) / this->m_Delta[dim] ) ), 0 );
00055     this->CropRegion().To()[dim] = 1 + std::min<Self::IndexType::ValueType>( static_cast<Self::IndexType::ValueType>( (crop.To()[dim] - this->m_Offset[dim]) / this->m_Delta[dim] ), this->m_Dims[dim]-1 );
00056     }
00057 }
00058 
00059 const UniformVolume::CoordinateRegionType
00060 UniformVolume::GetHighResCropRegion
00061 () const
00062 {
00063   if ( this->m_HighResCropRegion )
00064     {
00065     return *this->m_HighResCropRegion;
00066     }
00067   else
00068     {
00069     UniformVolume::CoordinateRegionType region;
00070     
00071     for ( int dim = 0; dim<3; ++dim )
00072       {
00073       region.From()[dim] = this->m_Offset[dim] + this->m_Delta[dim] * (this->CropRegion().From()[dim]); // take a half pixel off to move between pixels
00074       region.To()[dim] = this->m_Offset[dim] + this->m_Delta[dim] * (this->CropRegion().To()[dim]-1); // add a hald pixel to move between pixels
00075 //      region.From()[dim] = this->m_Offset[dim] + this->m_Delta[dim] * (this->CropRegion().From()[dim]-0.5); // take a half pixel off to move between pixels
00076 //      region.To()[dim] = this->m_Offset[dim] + this->m_Delta[dim] * (this->CropRegion().To()[dim]-1+0.5); // add a hald pixel to move between pixels
00077       }
00078     return region;
00079     }
00080 }
00081 
00082 UniformVolume::SmartPtr
00083 UniformVolume::GetCroppedVolume() const
00084 {
00085   const Self::IndexType cropDims = this->CropRegion().To() - this->CropRegion().From();
00086   
00087   Self::CoordinateVectorType cropSize( cropDims );
00088   for ( size_t i = 0; i < 3; ++i )
00089     (cropSize[i] -= 1) *= this->m_Delta[i];
00090   
00091   Self::SmartPtr volume( new UniformVolume( cropDims, cropSize ) );
00092   
00093   // get cropped data.
00094   TypedArray::SmartPtr croppedData( this->GetCroppedData() );
00095   volume->SetData( croppedData );
00096 
00097   // prepare new index-to-physical transformation.
00098   volume->m_IndexToPhysicalMatrix = this->m_IndexToPhysicalMatrix;
00099   for ( int i = 0; i < 3; ++i )
00100     for ( int j = 0; j < 3; ++j )
00101       volume->m_IndexToPhysicalMatrix[3][i] += this->CropRegion().From()[j] * volume->m_IndexToPhysicalMatrix[j][i];
00102   
00103   // use m_Offset to keep track of new volume origin
00104   Self::CoordinateVectorType volumeOffset = this->m_Offset;
00105   for ( int i = 0; i < 3; ++i )
00106     volumeOffset[i] += (this->CropRegion().From()[i] * this->m_Delta[i]);
00107   volume->SetOffset( volumeOffset );
00108 
00109   if ( this->m_HighResCropRegion )
00110     volume->SetHighResCropRegion( *this->m_HighResCropRegion );
00111   
00112   volume->m_MetaInformation[META_IMAGE_ORIENTATION]  = this->m_MetaInformation[META_IMAGE_ORIENTATION];
00113   volume->m_MetaInformation[META_IMAGE_ORIENTATION_ORIGINAL]  = this->m_MetaInformation[META_IMAGE_ORIENTATION_ORIGINAL];
00114 
00115   volume->m_MetaInformation[META_SPACE]  = this->m_MetaInformation[META_SPACE];
00116 
00117   return volume;
00118 }
00119 
00120 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines