cmtkVoxelMatchingAffineFunctional.h

Go to the documentation of this file.
00001 /*
00002 //  Copyright 1997-2009 Torsten Rohlfing
00003 //
00004 //  Copyright 2004-2010 SRI International
00005 //
00006 //  This file is part of the Computational Morphometry Toolkit.
00007 //
00008 //  http://www.nitrc.org/projects/cmtk/
00009 //
00010 //  The Computational Morphometry Toolkit is free software: you can
00011 //  redistribute it and/or modify it under the terms of the GNU General Public
00012 //  License as published by the Free Software Foundation, either version 3 of
00013 //  the License, or (at your option) any later version.
00014 //
00015 //  The Computational Morphometry Toolkit is distributed in the hope that it
00016 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License along
00021 //  with the Computational Morphometry Toolkit.  If not, see
00022 //  <http://www.gnu.org/licenses/>.
00023 //
00024 //  $Revision: 2752 $
00025 //
00026 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00027 //
00028 //  $LastChangedBy: torstenrohlfing $
00029 //
00030 */
00031 
00032 #ifndef __cmtkVoxelMatchingAffineFunctional_h_included_
00033 #define __cmtkVoxelMatchingAffineFunctional_h_included_
00034 
00035 #include <cmtkconfig.h>
00036 
00037 #include <Registration/cmtkVoxelMatchingFunctional.h>
00038 
00039 #include <Base/cmtkVector.h>
00040 #include <Base/cmtkAffineXform.h>
00041 #include <Base/cmtkVolume.h>
00042 #include <Base/cmtkUniformVolume.h>
00043 #include <Base/cmtkVolumeClipping.h>
00044 #include <Base/cmtkMathUtil.h>
00045 #include <Base/cmtkTypes.h>
00046 
00047 #include <System/cmtkException.h>
00048 #include <System/cmtkThreadPool.h>
00049 
00050 #include <cassert>
00051 
00052 namespace
00053 cmtk
00054 {
00055 
00058 
00061 class VoxelMatchingAffineFunctional : 
00063   public VoxelMatchingFunctional 
00064 {
00065 public:
00067   typedef VoxelMatchingAffineFunctional Self;
00068 
00070   typedef VoxelMatchingFunctional Superclass;
00071 
00073   typedef SmartPointer<Self> SmartPtr;
00074 
00076   virtual void GetParamVector ( CoordinateVector& v )  
00077   {
00078     this->m_AffineXform->GetParamVector( v );
00079   }
00080 
00082   virtual Types::Coordinate GetParamStep( const size_t idx, const Types::Coordinate mmStep = 1 ) const 
00083   {
00084     return this->m_AffineXform->GetParamStep( idx, Vector3D( FloatingSize ), mmStep );
00085   }
00086 
00088   virtual size_t ParamVectorDim() const 
00089   {
00090     return this->m_AffineXform->ParamVectorDim();
00091   }
00092 
00094   virtual size_t VariableParamVectorDim() const 
00095   {
00096     return this->m_AffineXform->VariableParamVectorDim();
00097   }
00098 
00099 protected:
00101   AffineXform::SmartPtr m_AffineXform;
00102 
00104   VolumeClipping Clipper;
00105 
00122   int ClipZ ( const VolumeClipping& clipper, const Vector3D& origin, DataGrid::IndexType::ValueType& start, DataGrid::IndexType::ValueType &end ) const
00123   {
00124     // perform clipping
00125     Types::Coordinate fromFactor, toFactor;
00126     if (! clipper.ClipZ( fromFactor, toFactor, origin ) )
00127       return 0;
00128 
00129     // there is an intersection: Look up the corresponding grid indices
00130     start = static_cast<DataGrid::IndexType::ValueType>( (ReferenceDims[2]-1)*fromFactor );
00131     end = 1+std::min( (int)(ReferenceDims[2]-1), (int)(1 + ((ReferenceDims[2]-1)*toFactor) ) );
00132     
00133     // finally, apply cropping boundaries of the reference volume
00134     start = std::max<DataGrid::IndexType::ValueType>( start, this->m_ReferenceCropRegion.From()[2] );
00135     end = std::min<DataGrid::IndexType::ValueType>( end, this->m_ReferenceCropRegion.To()[2] );
00136     
00137     // return 1 iff index range is non-empty.
00138     return (start < end );
00139   }
00140 
00167   int ClipX ( const VolumeClipping& clipper, const Vector3D& origin, DataGrid::IndexType::ValueType& start, DataGrid::IndexType::ValueType &end ) const
00168   {
00169     // perform clipping
00170     Types::Coordinate fromFactor, toFactor;
00171     if ( ! clipper.ClipX( fromFactor, toFactor, origin, 0, 2, false, true ) )
00172       return 0;
00173 
00174     fromFactor = std::min<Types::Coordinate>( 1.0, fromFactor );
00175               
00176     // there is an intersection: Look up the corresponding grid indices
00177     start = std::max( 0, (int)((ReferenceDims[0]-1)*fromFactor)-1 );
00178     while ( ( start*ReferenceGrid->m_Delta[0] < fromFactor*ReferenceSize[0]) && ( start < ReferenceDims[0] ) ) 
00179       ++start;
00180     
00181     if ( (toFactor > 1.0) || (start == ReferenceDims[0]) ) 
00182       {
00183       end = ReferenceDims[0];
00184       } 
00185     else
00186       {
00187       end = std::min( ReferenceDims[0]-2, (int)(1 + (ReferenceDims[0]-1)*toFactor));
00188       while ( end*ReferenceGrid->m_Delta[0] > toFactor*ReferenceSize[0] ) // 'if' not sufficient!       
00189         --end;
00190       ++end; // otherwise end=1+min(...) and ...[0][end-1] above!!
00191       }
00192     
00193     // finally, apply cropping boundaries of the reference volume
00194     start = std::max<DataGrid::IndexType::ValueType>( start, this->m_ReferenceCropRegion.From()[0] );
00195     end = std::min<DataGrid::IndexType::ValueType>( end, this->m_ReferenceCropRegion.To()[0] );
00196     
00197     // return 1 iff index range is non-empty.
00198     return (start < end );
00199   }
00200 
00217   int ClipY ( const VolumeClipping& clipper, const Vector3D& origin, DataGrid::IndexType::ValueType& start, DataGrid::IndexType::ValueType &end ) const
00218   {
00219     // perform clipping
00220     Types::Coordinate fromFactor, toFactor;
00221     if ( !clipper.ClipY( fromFactor, toFactor, origin ) )
00222       return 0;
00223 
00224     // there is an intersection: Look up the corresponding grid indices
00225     start = static_cast<DataGrid::IndexType::ValueType>( (ReferenceDims[1]-1)*fromFactor );
00226     
00227     if ( toFactor > 1.0 ) 
00228       {
00229       end = ReferenceDims[1];
00230       } 
00231     else
00232       {
00233       end = 1+std::min( ReferenceDims[1]-1, (int)(1+(ReferenceDims[1]-1)*toFactor ) );
00234       }
00235     // finally, apply cropping boundaries of the reference volume
00236     start = std::max<DataGrid::IndexType::ValueType>( start, this->m_ReferenceCropRegion.From()[1] );
00237     end = std::min<DataGrid::IndexType::ValueType>( end, this->m_ReferenceCropRegion.To()[1] );
00238     
00239     // return 1 iff index range is non-empty.
00240     return (start < end );
00241   }
00242 
00243 public:
00245   VoxelMatchingAffineFunctional( UniformVolume::SmartPtr refVolume, UniformVolume::SmartPtr modVolume, AffineXform::SmartPtr& affineXform ) 
00246     : VoxelMatchingFunctional( refVolume, modVolume ),
00247       m_AffineXform( affineXform )    
00248   {}
00249   
00251   virtual ~VoxelMatchingAffineFunctional() {}
00252 
00259   static VoxelMatchingAffineFunctional* Create( const int metric, UniformVolume::SmartPtr& refVolume, UniformVolume::SmartPtr& modVolume, AffineXform::SmartPtr& affineXform );
00260 
00261 };
00262 
00264 
00265 } // namespace cmtk
00266 
00267 #endif // #ifndef __cmtkVoxelMatchingAffineFunctional_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines