cmtkDataGrid.txx

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: 2743 $
00026 //
00027 //  $LastChangedDate: 2011-01-14 13:57:55 -0800 (Fri, 14 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 namespace
00034 cmtk
00035 {
00036 
00039 
00040 template<class TAccumulator>
00041 ScalarImage*
00042 DataGrid::ComputeProjection
00043 ( const int axis ) const
00044 {
00045   unsigned int dims[2], depth, offset, incX, incY, incZ;
00046 
00047   switch ( axis ) 
00048     {
00049     case AXIS_X:
00050       dims[0] = this->m_Dims[1];
00051       dims[1] = this->m_Dims[2];
00052       depth = this->m_Dims[0];
00053       offset = 0;
00054       incX = this->m_Dims[0];
00055       incY = this->m_Dims[0] * this->m_Dims[1];
00056       incZ = 1;
00057       break;
00058     case AXIS_Y:
00059       dims[0] = this->m_Dims[0];
00060       dims[1] = this->m_Dims[2];
00061       depth = this->m_Dims[1];
00062       offset = 0;
00063       incX = 1;
00064       incY = this->m_Dims[0] * this->m_Dims[1];
00065       incZ = this->m_Dims[0];
00066       break;
00067     case AXIS_Z:
00068     default:
00069       dims[0] = this->m_Dims[0];
00070       dims[1] = this->m_Dims[1];
00071       depth = this->m_Dims[2];
00072       offset = 0;
00073       incX = 1;
00074       incY = this->m_Dims[0];
00075       incZ = this->m_Dims[0] * this->m_Dims[1];
00076       break;
00077     }
00078   
00079   const TypedArray* data = this->GetData();
00080   TypedArray::SmartPtr projectData = TypedArray::Create( data->GetType(), dims[0] * dims[1] );
00081   
00082   for ( unsigned int y = 0; y < dims[1]; ++y ) 
00083     {
00084     size_t projectOffset = y * dims[0];
00085     unsigned int offsetY = offset + incY;
00086     for ( unsigned int x = 0; x < dims[0]; ++x, ++projectOffset ) 
00087       {
00088       unsigned int offsetX = offset + incX;
00089       
00090       TAccumulator accu;
00091       Types::DataItem item;
00092       for ( unsigned int z = 0; z < depth; ++z, offset += incZ ) 
00093         {
00094         if ( data->Get( item, offset ) ) 
00095           {
00096           accu.AddValue( item );
00097           }
00098         }
00099       projectData->Set( accu.GetResult(), projectOffset );
00100       
00101       offset = offsetX;
00102       }
00103     offset = offsetY;
00104     }
00105   
00106   ScalarImage* projectImage = new ScalarImage( dims[0], dims[1] );
00107   projectImage->SetPixelData( TypedArray::SmartPtr( projectData ) );
00108   
00109   return projectImage;
00110 }
00111 
00112 template<class TData>
00113 TData
00114 DataGrid::TrilinearInterpolation
00115 ( const TData* dataPtr, 
00116   const int X, const int Y, const int Z,
00117   const Self::SpaceVectorType& Location, const Types::Coordinate* from, 
00118   const Types::Coordinate* to ) const
00119 {
00120   const size_t offset = X+this->m_Dims[0]*(Y+this->m_Dims[1]*Z);
00121   const TData* data = dataPtr + offset;
00122 
00123   const Types::Coordinate deltaX=1.0/(to[0]-from[0]), deltaY=1.0/(to[1]-from[1]), deltaZ=1.0/(to[2]-from[2]);
00124   
00125   const Types::Coordinate revX = deltaX*(Location[0]-from[0]);
00126   const Types::Coordinate revY = deltaY*(Location[1]-from[1]);
00127   const Types::Coordinate revZ = deltaZ*(Location[2]-from[2]);
00128   const Types::Coordinate offsX = 1-revX;
00129   const Types::Coordinate offsY = 1-revY;
00130   const Types::Coordinate offsZ = 1-revZ;
00131   
00132   return static_cast<TData>( offsZ*(offsY*(offsX*data[0] + revX*data[nextI])+ 
00133                                     revY*(offsX*data[nextJ]+ revX*data[nextIJ]))+
00134                              revZ*(offsY*(offsX*data[nextK]+ revX*data[nextIK])+ 
00135                                    revY*(offsX*data[nextJK]+ revX*data[nextIJK])));
00136 }
00137 
00138 template<class TData,class TOutputIterator>
00139 inline void
00140 DataGrid
00141 ::TrilinearInterpolation
00142 ( TOutputIterator result, const std::vector<TData*>& dataPtr, const int x, const int y, const int z,
00143   const Types::Coordinate fracX, const Types::Coordinate fracY, const Types::Coordinate fracZ ) const
00144 {
00145   const size_t offset = x + this->m_Dims[0] * ( y + this->m_Dims[1] * z);
00146 
00147   const Types::Coordinate offsX = 1.0-fracX;
00148   const Types::Coordinate offsY = 1.0-fracY;
00149   const Types::Coordinate offsZ = 1.0-fracZ;
00150 
00151   const Types::Coordinate tmp0 = offsZ * offsY;
00152   const Types::Coordinate w0 = tmp0 * offsX;
00153   const Types::Coordinate w1 = tmp0 * fracX;
00154 
00155   const Types::Coordinate tmp1 = offsZ * fracY;
00156   const Types::Coordinate w2 = tmp1 * offsX;
00157   const Types::Coordinate w3 = tmp1 *  fracX;
00158 
00159   const Types::Coordinate tmp2 = fracZ * offsY;
00160   const Types::Coordinate w4 = tmp2 * offsX;
00161   const Types::Coordinate w5 = tmp2 * fracX;
00162 
00163   const Types::Coordinate tmp3 = fracZ *  fracY;
00164   const Types::Coordinate w6 = tmp3 * offsX;
00165   const Types::Coordinate w7 = tmp3 * fracX;
00166   
00167   for ( size_t i = 0; i < dataPtr.size(); ++i, ++result )
00168     {    
00169     const TData* data = dataPtr[i] + offset;
00170     *result = static_cast<TData>( w0 * data[0] +     w1 * data[nextI] +  w2 * data[nextJ] +  w3 * data[nextIJ] + w4 * data[nextK]+  w5 * data[nextIK] + w6 * data[nextJK] + w7 * data[nextIJK] );
00171     }
00172 }
00173 
00174 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines