cmtkTypedArraySimilarityMemory.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2010 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 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: 2022 $
00026 //
00027 //  $LastChangedDate: 2010-07-21 15:26:03 -0700 (Wed, 21 Jul 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 #include "cmtkTypedArraySimilarityMemory.h"
00033 
00034 namespace
00035 cmtk
00036 {
00037 
00040 const Types::DataItemRange
00041 TypedArraySimilarityMemory::GetRangeX
00042 ( const TypedArray* array, const size_t numBins )
00043 {
00044   if ( ! this->ValidX )
00045     this->NumberBinsX = numBins;
00046 
00047   if ( ! this->ValidX || this->RepeatCheck ) 
00048     {
00049     const Types::DataItemRange range = array->GetRange();
00050     if ( ! this->ValidX ) 
00051       {
00052       this->RangeX.m_LowerBound = range.m_LowerBound;
00053       this->RangeX.m_UpperBound = range.m_UpperBound;
00054       this->ValidX = true;
00055       } 
00056     else
00057       if ( (range.m_LowerBound < this->RangeX.m_LowerBound) || (range.m_UpperBound > this->RangeX.m_UpperBound ) ) 
00058         {
00059         Types::DataItem binDelta = (this->RangeX.m_UpperBound - this->RangeX.m_LowerBound) / (this->NumberBinsX - 1);
00060         
00061         if ( range.m_LowerBound < this->RangeX.m_LowerBound ) 
00062           {
00063           const size_t addBins = 1 + static_cast<size_t>( (this->RangeX.m_LowerBound - range.m_LowerBound) / binDelta);
00064           this->RangeX.m_LowerBound -= ( binDelta * addBins );
00065           this->NumberBinsY += addBins;
00066           }
00067         
00068         if ( range.m_UpperBound > this->RangeX.m_UpperBound ) 
00069           {
00070           const size_t addBins = 1 + static_cast<size_t>( (range.m_UpperBound - this->RangeX.m_UpperBound) / binDelta);
00071           this->RangeX.m_UpperBound += ( binDelta * addBins );
00072           this->NumberBinsY += addBins;
00073           }
00074         }
00075     }
00076   
00077   return this->RangeX;
00078 }
00079 
00080 const Types::DataItemRange
00081 TypedArraySimilarityMemory::GetRangeY
00082 ( const TypedArray* array, const size_t numBins )
00083 {
00084   if ( ! this->ValidY )
00085     this->NumberBinsY = numBins;
00086 
00087   if ( ! this->ValidY || this->RepeatCheck ) 
00088     {
00089     const Types::DataItemRange range = array->GetRange();
00090     if ( ! this->ValidY ) 
00091       {
00092       this->RangeY.m_LowerBound = range.m_LowerBound;
00093       this->RangeY.m_UpperBound = range.m_UpperBound;
00094       this->ValidY = true;
00095       } 
00096     else
00097       if ( (range.m_LowerBound < this->RangeY.m_LowerBound) || (range.m_UpperBound > this->RangeY.m_UpperBound ) ) 
00098         {
00099         Types::DataItem binDelta = (this->RangeY.m_UpperBound - this->RangeY.m_LowerBound) / (this->NumberBinsY - 1);
00100         
00101         if ( range.m_LowerBound < this->RangeY.m_LowerBound ) 
00102           {
00103           const size_t addBins = 1 + static_cast<size_t>( (this->RangeY.m_LowerBound - range.m_LowerBound) / binDelta);
00104           this->RangeY.m_LowerBound -= ( binDelta * addBins );
00105           this->NumberBinsY += addBins;
00106           }
00107         
00108         if ( range.m_UpperBound > this->RangeY.m_UpperBound ) 
00109           {
00110           const size_t addBins = 1 + static_cast<size_t>( (range.m_UpperBound - this->RangeY.m_UpperBound) / binDelta);
00111           this->RangeY.m_UpperBound += ( binDelta * addBins );
00112           this->NumberBinsY += addBins;
00113           }
00114         }
00115     }
00116   
00117   return this->RangeY;
00118 }
00119 
00120 JointHistogram<unsigned int>::SmartPtr
00121 TypedArraySimilarityMemory::CreateHistogram
00122 ( const TypedArray* array0, const TypedArray* array1 )
00123 {
00124   const unsigned int dataSize = array0->GetDataSize();
00125   const size_t numBins = std::max<unsigned>( std::min<unsigned>( static_cast<unsigned>( sqrt( (float)dataSize ) ), this->MaxNumBins ), this->MinNumBins );
00126   
00127   Types::DataItemRange rangeX = this->GetRangeX( array0, numBins );
00128   Types::DataItemRange rangeY = this->GetRangeY( array1, numBins );
00129 
00130   JointHistogram<unsigned int>::SmartPtr histogram( new JointHistogram<unsigned int>( this->NumberBinsX, this->NumberBinsY ) );
00131   
00132   histogram->SetRangeX( rangeX );
00133   histogram->SetRangeY( rangeY );
00134 
00135   return histogram;
00136 }
00137 
00138 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines