ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkOffset.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkOffset_h
00019 #define __itkOffset_h
00020 
00021 #include "itkSize.h"
00022 
00023 #include <memory>
00024 
00025 namespace itk
00026 {
00027 namespace Functor
00028 {
00029 template< unsigned int VOffsetDimension >
00030 class OffsetLexicographicCompare;
00031 }
00032 
00054 template< unsigned int VOffsetDimension = 2 >
00055 class Offset
00056 {
00057 public:
00059   typedef Offset Self;
00060 
00062   itkStaticConstMacro(Dimension, unsigned int, VOffsetDimension);
00063 
00065   static unsigned int GetOffsetDimension() { return VOffsetDimension; }
00066 
00068   typedef   Offset< VOffsetDimension > OffsetType;
00069   typedef   itk::OffsetValueType       OffsetValueType;
00070 
00072   typedef Functor::OffsetLexicographicCompare< VOffsetDimension > LexicographicCompare;
00073 
00075   const Self
00076   operator+(const Self & offset) const
00077   {
00078     Self result;
00079 
00080     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00081           { result[i] = m_Offset[i] + offset[i]; }
00082     return result;
00083   }
00084 
00086   const Self
00087   operator+(const Size< VOffsetDimension > & size) const
00088   {
00089     Self result;
00090 
00091     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00092           { result[i] = m_Offset[i] + size[i]; }
00093     return result;
00094   }
00095 
00097   const Self &
00098   operator+=(const Size< VOffsetDimension > & size)
00099   {
00100     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00101           { m_Offset[i] += size[i]; }
00102     return *this;
00103   }
00105 
00107   const Self &
00108   operator-=(const Size< VOffsetDimension > & size)
00109   {
00110     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00111           { m_Offset[i] -= size[i]; }
00112     return *this;
00113   }
00115 
00117   const Self
00118   operator-(const Self & vec)
00119   {
00120     Self result;
00121 
00122     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00123           { result[i] = m_Offset[i] - vec.m_Offset[i]; }
00124     return result;
00125   }
00126 
00128   const Self &
00129   operator+=(const Self & vec)
00130   {
00131     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00132           { m_Offset[i] += vec.m_Offset[i]; }
00133     return *this;
00134   }
00136 
00138   const Self &
00139   operator-=(const Self & vec)
00140   {
00141     for ( unsigned int i = 0; i < VOffsetDimension; i++ )
00142           { m_Offset[i] -= vec.m_Offset[i]; }
00143     return *this;
00144   }
00146 
00148   bool
00149   operator==(const Self & vec) const
00150   {
00151     bool same = 1;
00152 
00153     for ( unsigned int i = 0; i < VOffsetDimension && same; i++ )
00154           { same = ( m_Offset[i] == vec.m_Offset[i] ); }
00155     return same;
00156   }
00157 
00159   bool
00160   operator!=(const Self & vec) const
00161   {
00162     bool same = 1;
00163 
00164     for ( unsigned int i = 0; i < VOffsetDimension && same; i++ )
00165           { same = ( m_Offset[i] == vec.m_Offset[i] ); }
00166     return !same;
00167   }
00168 
00171   OffsetValueType & operator[](unsigned int dim)
00172   { return m_Offset[dim]; }
00173 
00177   OffsetValueType operator[](unsigned int dim) const
00178   { return m_Offset[dim]; }
00179 
00182   const OffsetValueType * GetOffset() const { return m_Offset; }
00183 
00188   void SetOffset(const OffsetValueType val[VOffsetDimension])
00189   { memcpy(m_Offset, val, sizeof( OffsetValueType ) * VOffsetDimension); }
00190 
00194   static Self GetBasisOffset(unsigned int dim);
00195 
00198   void Fill(OffsetValueType value)
00199   { for ( unsigned int i = 0; i < VOffsetDimension; ++i ) { m_Offset[i] = value; } }
00200 
00206   OffsetValueType m_Offset[VOffsetDimension];
00207 
00208 // force gccxml to find the constructors found before the internal upgrade to
00209 // gcc 4.2
00210 #if defined( CABLE_CONFIGURATION )
00211   Offset();                     //purposely not implemented
00212   Offset(const Self &);         //purposely not implemented
00213   void operator=(const Self &); //purposely not implemented
00214 
00215 #endif
00216 };
00217 
00218 namespace Functor
00219 {
00228 template< unsigned int VOffsetDimension >
00229 class OffsetLexicographicCompare
00230 {
00231 public:
00232   bool operator()(Offset< VOffsetDimension > const & l,
00233                   Offset< VOffsetDimension > const & r) const
00234   {
00235     for ( unsigned int i = 0; i < VOffsetDimension; ++i )
00236       {
00237       if ( l.m_Offset[i] < r.m_Offset[i] )
00238         {
00239         return true;
00240         }
00241       else if ( l.m_Offset[i] > r.m_Offset[i] )
00242         {
00243         return false;
00244         }
00245       }
00246     return false;
00247   }
00248 };
00249 }
00250 
00251 template< unsigned int VOffsetDimension >
00252 Offset< VOffsetDimension >
00253 Offset< VOffsetDimension >
00254 ::GetBasisOffset(unsigned int dim)
00255 {
00256   Self ind;
00257 
00258   memset(ind.m_Offset, 0, sizeof( OffsetValueType ) * VOffsetDimension);
00259   ind.m_Offset[dim] = 1;
00260   return ind;
00261 }
00262 
00263 template< unsigned int VOffsetDimension >
00264 std::ostream & operator<<(std::ostream & os, const Offset< VOffsetDimension > & ind)
00265 {
00266   os << "[";
00267   unsigned int dimlim = VOffsetDimension - 1;
00268   for ( unsigned int i = 0; i < dimlim; ++i )
00269     {
00270     os << ind[i] << ", ";
00271     }
00272   if ( VOffsetDimension >= 1 )
00273     {
00274     os << ind[VOffsetDimension - 1];
00275     }
00276   os << "]";
00277   return os;
00278 }
00279 } // end namespace itk
00280 
00281 #endif
00282