Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

Testing/Code/Common/itkVectorImageTest.cxx

00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkVectorImageTest.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/01/26 23:43:03 $
00007   Version:   $Revision: 1.16 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #if defined(_MSC_VER)
00018 #pragma warning ( disable : 4786 )
00019 #endif
00020 
00021 #include <iostream>
00022 #include "itkVariableLengthVector.h"
00023 #include "itkImage.h"
00024 #include "itkVectorImage.h"
00025 #include "itkFixedArray.h"
00026 #include "itkTimeProbe.h"
00027 #include "itkVectorImageToImageAdaptor.h"
00028 #include "itkImageRegionConstIterator.h"
00029 #include "itkImageRegionIterator.h"
00030 #include "itkImageLinearConstIteratorWithIndex.h"
00031 #include "itkImageLinearIteratorWithIndex.h"
00032 #include "itkConstNeighborhoodIterator.h"
00033 #include "itkConstShapedNeighborhoodIterator.h"
00034 #include "itkShapedNeighborhoodIterator.h"
00035 #include "itkNeighborhoodIterator.h"
00036 #include "itkImageRegionIteratorWithIndex.h"
00037 #include "itkImageRegionConstIteratorWithIndex.h"
00038 #include "itkImageFileWriter.h"
00039 #include "itkImageFileReader.h"
00040 
00041 
00042 // This test tests:
00043 // VectorImage 
00044 // -----------
00045 //  - Iterators on the VectorImage, 
00046 //  - Timing tests comparing itk::VectorImage to similar itk::Image using 
00047 //    FixedArray and VariableLengthVector 
00048 //  - IO support for VectorImage.
00049 
00050 int itkVectorImageTest( int, char* argv[] )
00051 {
00052   bool failed = false;
00053 
00054   const unsigned int VectorLength = 6;
00055   const unsigned int Dimension    = 3;
00056   typedef float PixelType;
00057 
00058   {
00059   // Test 1.
00060   //
00061   // Create an Image of VariableLengthVector, FixedArray, VectorImage of length 6 and compare
00062   // times.
00063   //   
00064   // Three images.. for crude timing analysis.
00065 
00066   typedef itk::Image< itk::VariableLengthVector< PixelType >, Dimension > VariableLengthVectorImageType;
00067   typedef itk::Image< itk::FixedArray< PixelType, VectorLength >, 
00068                                       Dimension > FixedArrayImageType;
00069   typedef itk::VectorImage< PixelType, Dimension >   VectorImageType;
00070   
00071   
00072   
00073   // Using image of VariableLengthVector< PixelType >
00074   {
00075   typedef itk::VariableLengthVector< PixelType > InternalPixelType;
00076 
00077   itk::TimeProbe clock;
00078   clock.Start();
00079   
00080   VariableLengthVectorImageType::Pointer image = VariableLengthVectorImageType::New();
00081   VariableLengthVectorImageType::IndexType start;
00082   InternalPixelType f( VectorLength );
00083   for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
00084   start[0] =   0;  // first index on X
00085   start[1] =   0;  // first index on Y
00086   start[2] =   0;  // first index on Z
00087   VariableLengthVectorImageType::SizeType  size;
00088   size[0]  = 50;  // size along X
00089   size[1]  = 50;  // size along Y
00090   size[2]  = 50;  // size along Z
00091   VariableLengthVectorImageType::RegionType region;
00092   region.SetSize( size );
00093   region.SetIndex( start );
00094   image->SetRegions( region );
00095   image->Allocate();
00096   image->FillBuffer( f );
00097 
00098   clock.Stop();
00099   double timeTaken = clock.GetMeanTime();
00100   std::cout << "Allocating an image of itk::VariableLengthVector of length " <<  VectorLength 
00101           << " with image size " << size << " took " << timeTaken << " s." << std::endl;
00102 
00103     // Const iterator over the image...
00104     {
00105     clock.Start();
00106     typedef itk::ImageRegionConstIterator< VariableLengthVectorImageType > IteratorType;
00107     IteratorType it( image, image->GetBufferedRegion() );
00108     it.Begin();
00109     while( !it.IsAtEnd() )
00110       {
00111       it.Get();
00112       ++it;
00113       }
00114     clock.Stop();
00115     std::cout << "ConstIterator Get() over the entire image took : " << 
00116       clock.GetMeanTime() << " s." << std::endl;
00117     }
00118   }
00119 
00120   
00121   // Using image of FixedArray< PixelType, VectorLength >
00122   {
00123   itk::TimeProbe clock;
00124   clock.Start();
00125   
00126   typedef itk::FixedArray< PixelType, VectorLength > InternalPixelType;
00127   
00128   FixedArrayImageType::Pointer image = FixedArrayImageType::New();
00129   FixedArrayImageType::IndexType start;
00130   InternalPixelType f;
00131   for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
00132   start[0] =   0;  // first index on X
00133   start[1] =   0;  // first index on Y
00134   start[2] =   0;  // first index on Z
00135   FixedArrayImageType::SizeType  size;
00136   size[0]  = 50;  // size along X
00137   size[1]  = 50;  // size along Y
00138   size[2]  = 50;  // size along Z
00139   FixedArrayImageType::RegionType region;
00140   region.SetSize( size );
00141   region.SetIndex( start );
00142   image->SetRegions( region );
00143   image->Allocate();
00144   image->FillBuffer( f );
00145 
00146   clock.Stop();
00147   double timeTaken = clock.GetMeanTime();
00148   std::cout << "Allocating an image of itk::FixedArray of length " <<  VectorLength 
00149           << " with image size " << size << " took " << timeTaken << " s." << std::endl;
00150 
00151     {
00152     // Test and compare times with iterators
00153     //
00154     // First set some pixel
00155     for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i*0.1; }
00156     FixedArrayImageType::IndexType idx;
00157     idx[0]=4; idx[1]=4;idx[2]=12; 
00158     image->SetPixel( idx, f );
00159     }
00160 
00161     {
00162     clock.Start();
00163     typedef itk::ImageRegionConstIterator< FixedArrayImageType > IteratorType;
00164     IteratorType it( image, image->GetBufferedRegion() );
00165     it.Begin();
00166     while( !it.IsAtEnd() )
00167       {
00168       it.Get();
00169       ++it;
00170       }
00171     clock.Stop();
00172     std::cout << "ConstIterator Get() over the entire image took : " << 
00173       clock.GetMeanTime() << " s." << std::endl;
00174     }
00175   }
00176 
00177   // Using VectorImage< PixelType, Dimension > 
00178   {
00179   itk::TimeProbe clock;
00180   clock.Start();
00181   
00182   VectorImageType::Pointer vectorImage = VectorImageType::New();
00183   VectorImageType::IndexType start;
00184   itk::VariableLengthVector< PixelType > f( VectorLength );
00185   for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
00186   start[0] =   0;  // first index on X
00187   start[1] =   0;  // first index on Y
00188   start[2] =   0;  // first index on Z
00189   VectorImageType::SizeType  size;
00190   size[0]  = 50;  // size along X
00191   size[1]  = 50;  // size along Y
00192   size[2]  = 50;  // size along Z
00193   VectorImageType::RegionType region;
00194   region.SetSize( size );
00195   region.SetIndex( start );
00196   vectorImage->SetVectorLength( VectorLength );
00197   vectorImage->SetRegions( region );
00198   vectorImage->Allocate();
00199   vectorImage->FillBuffer( f );
00200 
00201   clock.Stop();
00202   double timeTaken = clock.GetMeanTime();
00203   std::cout << "Allocating an image of itk::VectorImage with pixels length " <<  VectorLength 
00204      << " with image size " << size << " took " << timeTaken << " s." << std::endl;
00205 
00206 
00207   // Iterator tests on the vector image.
00208   //
00209   // Const iterator over the vector image...
00210   {
00211     clock.Start();
00212     typedef itk::ImageRegionConstIterator< VectorImageType > IteratorType;
00213     IteratorType it( vectorImage, vectorImage->GetBufferedRegion() );
00214     it.Begin();
00215     while( !it.IsAtEnd() )
00216       {
00217       it.Get();
00218       ++it;
00219       }
00220     clock.Stop();
00221     std::cout << "ConstIterator Get() over the entire vectorImage took : " << 
00222       clock.GetMeanTime() << " s." << std::endl;
00223   }
00224 
00225 
00226   
00227   std::cout << "---------------------------------------------------------------" << std::endl;
00228   std::cout << "Testing VectorImageToImageAdaptor to extract a component from the vector image" << std::endl;
00229 
00230 
00231   const unsigned int componentToExtract = 4;
00232   typedef itk::VectorImageToImageAdaptor< PixelType, Dimension > AdaptorType;
00233   AdaptorType::Pointer vectorImageToImageAdaptor = AdaptorType::New();
00234   vectorImageToImageAdaptor->SetExtractComponentIndex( componentToExtract );
00235   if( vectorImageToImageAdaptor->GetExtractComponentIndex() != componentToExtract )
00236     {
00237     std::cerr << "[FAILED]" << std::endl;
00238     
00239     } 
00240     
00241   vectorImageToImageAdaptor->SetImage( vectorImage );
00242   vectorImageToImageAdaptor->Update();
00243  
00244   typedef itk::Image< PixelType , Dimension > AdaptedImageType;
00245   AdaptedImageType::IndexType index;
00246   index[0]=10;
00247   index[1]=10;
00248   index[2]=10;
00249 
00250   if(   (vectorImageToImageAdaptor->GetPixel(index) !=  vectorImage->GetPixel( index )[componentToExtract]) 
00251      || (vectorImage->GetPixel( index )[componentToExtract] != componentToExtract ))
00252     {
00253     std::cerr << "[FAILED]" << std::endl;
00254     failed = true;
00255     }
00256   else 
00257     {
00258     std::cout << "[PASSED]" << std::endl;
00259     }
00260   }
00261 
00262   // Test with Region and Linear iterators...
00263   {
00264     // Create a  small image
00265     VectorImageType::Pointer vectorImage = VectorImageType::New();
00266     VectorImageType::IndexType start;
00267     itk::VariableLengthVector< PixelType > f( VectorLength );
00268     itk::VariableLengthVector< PixelType > ZeroPixel( VectorLength );
00269     ZeroPixel.Fill( itk::NumericTraits< PixelType >::Zero );
00270     for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
00271     start[0] =   0;  // first index on X
00272     start[1] =   0;  // first index on Y
00273     start[2] =   0;  // first index on Z
00274     VectorImageType::SizeType  size;
00275     size[0]  = 10;  // size along X
00276     size[1]  = 10;  // size along Y
00277     size[2]  = 5;  // size along Z
00278     VectorImageType::RegionType region( start, size );
00279     vectorImage->SetVectorLength( VectorLength );
00280     vectorImage->SetRegions( region );
00281     vectorImage->Allocate();
00282     vectorImage->FillBuffer( ZeroPixel );
00283 
00284     start[0]=3; start[1]=3;start[2]=2;
00285     size[0]=4;size[1]=4;size[2]=2;
00286     VectorImageType::RegionType subRegion( start, size );
00287     typedef itk::ImageRegionIterator< VectorImageType > ImageRegionIteratorType;
00288     ImageRegionIteratorType rit( vectorImage, subRegion );
00289     rit.GoToBegin();
00290 
00291     while( !rit.IsAtEnd() )
00292       {
00293       rit.Set( f );
00294       ++rit;
00295       }
00296     
00297     typedef itk::ImageRegionConstIterator< VectorImageType > ConstIteratorType;
00298     ConstIteratorType cit( vectorImage, vectorImage->GetBufferedRegion() );
00299     unsigned long ctr = 0;
00300     cit.Begin();
00301     while( !cit.IsAtEnd() )
00302       {
00303       itk::VariableLengthVector< PixelType > value = cit.Get();
00304       ++cit;
00305       if( ctr == (3*10*10 + 5*10 + 5) )
00306         {
00307         if( value != f )
00308           { 
00309           std::cerr << 
00310             "ImageRegionConstIteratorTest on VectorImage [FAILED]" << std::endl;
00311           failed = true;
00312           }
00313         }
00314       ++ctr;
00315       }
00316     std::cout << "ImageRegionConstIteratorTest on VectorImage [PASSED]" << std::endl;
00317 
00318     
00319     {
00320     // Test itkImageLinearIteratorWithIndex
00321     typedef itk::ImageLinearConstIteratorWithIndex< VectorImageType > LinearConstIteratorType;
00322     typedef itk::ImageLinearIteratorWithIndex< VectorImageType > LinearIteratorType;
00323     
00324     LinearConstIteratorType lcit( vectorImage, vectorImage->GetBufferedRegion() );
00325     lcit.SetDirection( 2 );
00326     lcit.GoToBegin();
00327     itk::VariableLengthVector< PixelType > value;
00328     while( !lcit.IsAtEnd() )
00329       {
00330       while( !lcit.IsAtEndOfLine() )
00331         {
00332         value = lcit.Get();  
00333         if( subRegion.IsInside( lcit.GetIndex() ) )
00334           {
00335           if( value!=f )
00336             {
00337             std::cerr << 
00338               "ImageLinearConstIteratorWithIndex on VectorImage [FAILED]" << std::endl;
00339             failed = true;
00340             }
00341           }
00342         else
00343           {
00344           if( value!=ZeroPixel )
00345             {
00346             std::cerr << 
00347               "ImageLinearConstIteratorWithIndex on VectorImage [FAILED]" << std::endl;
00348             failed = true;
00349             }
00350           }            
00351         ++lcit;
00352         }
00353       lcit.NextLine();
00354       } 
00355 
00356     VectorImageType::IndexType idx;
00357     idx[0]=1;idx[1]=1;idx[2]=1;
00358     LinearIteratorType lit( vectorImage, vectorImage->GetBufferedRegion() );
00359     lit.SetIndex( idx );
00360     lit.Set( f );
00361     
00362     lcit.SetIndex( idx );
00363     value = lcit.Get();
00364     if( value != f )
00365       {
00366       std::cerr << 
00367         "ImageLinearConstIteratorWithIndex on VectorImage [FAILED]" << std::endl;
00368       failed = true;
00369       }
00370     
00371     std::cout << "ImageLinearConstIteratorWithIndex on VectorImage [PASSED]" << std::endl;
00372     std::cout << "ImageLinearIteratorWithIndex on VectorImage [PASSED]" << std::endl;
00373     }
00374 
00375   }
00376 
00377   }
00378 
00379 
00380   // Test IO support.
00381   {
00382   // Create an image using itk::Vector
00383   typedef itk::Vector< PixelType, VectorLength > VectorPixelType;
00384   typedef itk::Image< itk::Vector< PixelType, VectorLength >, 
00385                                       Dimension > VectorImageType;
00386   VectorImageType::Pointer image = VectorImageType::New();
00387   VectorImageType::IndexType start;
00388   start[0]=0; start[1]=0; start[2]=0;
00389   VectorImageType::SizeType size;
00390   size[0]=5; size[1]=5; size[2]=5;
00391   VectorImageType::RegionType region( start, size );
00392   image->SetRegions( region );
00393   image->Allocate();
00394   
00395   typedef itk::ImageRegionIteratorWithIndex< VectorImageType > IteratorType;
00396   IteratorType it( image, region );
00397   it.GoToBegin();
00398 
00399   while( !it.IsAtEnd() )
00400     {
00401     VectorPixelType f;
00402     f[0] = it.GetIndex()[0];
00403     f[1] = it.GetIndex()[1];
00404     f[2] = it.GetIndex()[2];
00405     f[3] = it.GetIndex()[0];
00406     f[4] = it.GetIndex()[1];
00407     f[5] = it.GetIndex()[2];
00408     it.Set( f );
00409     ++it;
00410     }
00411 
00412   typedef itk::ImageFileWriter< VectorImageType > WriterType;
00413   WriterType::Pointer writer = WriterType::New();
00414   writer->SetInput( image );
00415   writer->SetFileName( argv[2] );
00416   writer->Update();
00417 
00418   writer->SetFileName( argv[1] );
00419   writer->Update();
00420   }
00421   
00422   {
00423   // Now read it as a itk::VectorImage.
00424   typedef itk::VectorImage< PixelType, Dimension > VectorImageType;
00425   typedef itk::ImageFileReader< VectorImageType >  ReaderType; 
00426   ReaderType::Pointer reader = ReaderType::New();
00427   reader->SetFileName( argv[1] );
00428   reader->Update();
00429 
00430   VectorImageType::Pointer vectorImage = reader->GetOutput();
00431   
00432   typedef itk::ImageRegionConstIteratorWithIndex< VectorImageType > IteratorType;
00433   IteratorType cit( vectorImage, vectorImage->GetBufferedRegion() );
00434   cit.GoToBegin();
00435 
00436   bool failed1 = false;
00437   while( !cit.IsAtEnd() )
00438     {
00439     if(   (cit.Get()[0] != cit.GetIndex()[0])
00440        || (cit.Get()[1] != cit.GetIndex()[1])
00441        || (cit.Get()[2] != cit.GetIndex()[2])
00442        || (cit.Get()[3] != cit.GetIndex()[0])
00443        || (cit.Get()[4] != cit.GetIndex()[1])
00444        || (cit.Get()[5] != cit.GetIndex()[2]))
00445       {
00446       failed1 = true;
00447       }
00448     ++cit;
00449     }
00450 
00451   if( failed1 )
00452     {
00453     std::cerr << "Read VectorImage [FAILED]" << std::endl;
00454     failed = true;
00455     }
00456   else
00457     {
00458     std::cout << "Read VectorImage [PASSED]" << std::endl;
00459     }
00460 
00461   
00462   // Now write this out this VectorImage and read it again
00463   typedef itk::ImageFileWriter< VectorImageType > WriterType;
00464   WriterType::Pointer writer = WriterType::New();
00465   writer->SetInput( vectorImage );
00466   writer->SetFileName(argv[1]);
00467   writer->Update();
00468   }
00469   
00470   
00471   {
00472   // Now read it as a itk::VectorImage.
00473   typedef itk::VectorImage< PixelType, Dimension > VectorImageType;
00474   typedef itk::ImageFileReader< VectorImageType >  ReaderType;  
00475   ReaderType::Pointer reader = ReaderType::New();
00476   reader->SetFileName( argv[1] );
00477   reader->Update();
00478 
00479   VectorImageType::Pointer vectorImage = reader->GetOutput();
00480   
00481   typedef itk::ImageRegionConstIteratorWithIndex< VectorImageType > IteratorType;
00482   IteratorType cit( vectorImage, vectorImage->GetBufferedRegion() );
00483   cit.GoToBegin();
00484 
00485   bool failed1 = false;
00486   while( !cit.IsAtEnd() )
00487     {
00488     if(   (cit.Get()[0] != cit.GetIndex()[0])
00489        || (cit.Get()[1] != cit.GetIndex()[1])
00490        || (cit.Get()[2] != cit.GetIndex()[2])
00491        || (cit.Get()[3] != cit.GetIndex()[0])
00492        || (cit.Get()[4] != cit.GetIndex()[1])
00493        || (cit.Get()[5] != cit.GetIndex()[2]))
00494       {
00495       failed1 = true;
00496       }
00497     ++cit;
00498     }
00499 
00500   if( failed1 )
00501     {
00502     std::cerr << "Write VectorImage [FAILED]" << std::endl;
00503     failed = true;
00504     }
00505   else
00506     {
00507     std::cout << "Write VectorImage [PASSED]" << std::endl;
00508     }
00509 
00510   
00511     {
00512     // Check support for Neighborhood Iterators
00513     //
00514     // 1. Test ConstNeighborhoodIterator
00515     // 
00516     std::cout << "Testing ConstNeighborhoodIterator...." << std::endl;
00517     
00518     typedef itk::ConstNeighborhoodIterator< VectorImageType > 
00519                                          ConstNeighborhoodIteratorType;
00520     ConstNeighborhoodIteratorType::RadiusType radius;
00521     radius[0] = radius[1] = radius[2] = 1;
00522 
00523     ConstNeighborhoodIteratorType::RegionType region 
00524                             = vectorImage->GetBufferedRegion();
00525     ConstNeighborhoodIteratorType::SizeType size;
00526     size[0]= size[1] = size[2] = 4;
00527     ConstNeighborhoodIteratorType::IndexType index;
00528     index[0] = index[1] = index[2] = 1; 
00529     region.SetIndex(index); 
00530     region.SetSize( size );
00531     
00532     ConstNeighborhoodIteratorType cNit(radius, vectorImage, region);
00533 
00534     // Move Iterator to a point and see if it reads out the right value
00535     //
00536     const unsigned int centerIndex = static_cast< unsigned int >(
00537                 ((radius[0]*2+1)*(radius[1]*2+1)*(radius[2]*2+1))/2);
00538 
00539     ConstNeighborhoodIteratorType::IndexType location;
00540     location[0] = 1; location[1] = 2;  location[2] = 3;
00541     cNit.SetLocation( location );
00542     
00543     if( cNit.GetPixel(centerIndex) != vectorImage->GetPixel( location ) )
00544       {
00545       std::cerr << "  SetLocation [FAILED]" << std::endl;
00546       failed=true;
00547       }
00548 
00549     // Test GoToBegin()
00550     cNit.GoToBegin();
00551     if( cNit.GetPixel(centerIndex) != vectorImage->GetPixel( index ) )
00552       {
00553       std::cerr << "  GoToBegin [FAILED]" << std::endl;
00554       failed=true;
00555       }
00556 
00557     // Test GoToEnd()
00558     cNit.GoToEnd(); 
00559     --cNit;
00560     ConstNeighborhoodIteratorType::IndexType endIndex;
00561     endIndex[0] = endIndex[1] = endIndex[2] = 4;
00562     if( cNit.GetPixel(centerIndex) != vectorImage->GetPixel( endIndex ) )
00563       {
00564       std::cerr << "  GoToEnd [FAILED]" << std::endl;
00565       failed=true;
00566       }
00567 
00568     // Test IsAtEnd()
00569     if( !((++cNit).IsAtEnd()) )
00570       {
00571       std::cerr << "  IsAtEnd() [FAILED]" << std::endl;  
00572       failed = true;
00573       }
00574     cNit.GoToBegin();
00575     unsigned int numPixelsTraversed = size[0]*size[1]*size[2];
00576     while (! cNit.IsAtEnd())
00577       {
00578       ++cNit;
00579       --numPixelsTraversed;
00580       }
00581     if( numPixelsTraversed ) { std::cerr << "  IsAtEnd() [FAILED]" << std::endl; }
00582 
00583     // Test operator-
00584     --cNit;
00585     ConstNeighborhoodIteratorType::OffsetType offset;
00586     offset[0] = offset[1] = offset[2] = 1;
00587     cNit -= offset;
00588     itk::VariableLengthVector< PixelType > pixel = cNit.GetCenterPixel();
00589     itk::VariableLengthVector< PixelType > correctAnswer( VectorLength );
00590     correctAnswer.Fill( 3 );
00591     if( pixel != correctAnswer ) 
00592       {
00593       std::cerr << "  operator- [FAILED]" << std::endl;
00594       failed = true;
00595       }
00596 
00597     // Test GetNeighborhood()
00598     cNit.SetLocation( location );
00599     ConstNeighborhoodIteratorType::NeighborhoodType 
00600                     neighborhood = cNit.GetNeighborhood();
00601     //const unsigned int neighborhoodSize = neighborhood.Size();
00602     //for( unsigned int i=0; i< neighborhoodSize; i++)
00603     //  { std::cout << neighborhood[i] << std::endl; }
00604     if( (neighborhood[0][0] != 0) || (neighborhood[0][5] != 2))
00605       {
00606       std::cerr << "  GetNeighborhood() on ConstNeighborhoodIterator [FAILED]" << std::endl;
00607       failed = true;
00608       }
00609 
00610 
00611 
00612     // 
00613     // 2. Test NeighborhoodIterator on VectorImage
00614     //
00615     
00616     std::cout << "Testing NeighborhoodIterator..." << std::endl;
00617     
00618     typedef itk::NeighborhoodIterator< VectorImageType > NeighborhoodIteratorType;
00619     NeighborhoodIteratorType nit(radius, vectorImage, region);
00620     nit.SetLocation( location );
00621     itk::VariableLengthVector< PixelType > p( VectorLength );
00622     p.Fill( 100.0 );
00623     nit.SetNext( 1, 1, p );
00624     
00625     // Test SetNext()
00626     NeighborhoodIteratorType::IndexType index1;
00627     index1 = location; index1[1] = location[1] + 1;
00628     nit.SetLocation( index1 );
00629 
00630     if( nit.GetCenterPixel() != p )
00631       {
00632       std::cerr << "  SetNext() [FAILED]" << std::endl;
00633       failed = true;
00634       }
00635     p[0] = p[3] = (float)index1[0];  
00636     p[1] = p[4] = (float)index1[1]; 
00637     p[2] = p[5] = (float)index1[2]; 
00638     nit.SetCenterPixel( p );
00639     if( nit.GetCenterPixel() != p )
00640       {
00641       std::cerr << "  SetCenterPixel() [FAILED]" << std::endl;
00642       failed = true;
00643       }
00644     
00645     // Test SetNeighborhood() and GetPrevious()
00646     nit.SetLocation( index1 );
00647     nit.SetNeighborhood( neighborhood );
00648     p[0] = p[3] = 1; p[1] = p[4] = 2; p[2] = p[5] = 2;
00649     if( nit.GetPrevious( 2, 1 ) != p )
00650       {
00651       std::cerr << "  SetNeighborhood() or GetPrevious() [FAILED]" << std::endl;
00652       failed = true;
00653       }
00654     
00655 
00656     // 
00657     // 3. Testing ConstShapedNeighborhoodIterator on VectorImage
00658     //
00659 
00660     // Go back to original image, where pixel values tell us the indices
00661     std::cout << "Testing ConstShapedNeighborhoodIterator on VectorImage..." 
00662                                                                   << std::endl;
00663     reader->SetFileName( "dummy.nrrd");
00664     reader->SetFileName( argv[1] );
00665     reader->Update();
00666     vectorImage = reader->GetOutput(); 
00667 
00668     typedef itk::ConstShapedNeighborhoodIterator< VectorImageType > 
00669                                    ConstShapedNeighborhoodIteratorType;
00670     ConstShapedNeighborhoodIteratorType cSnit( radius, vectorImage, region );
00671     cSnit.SetLocation( location );
00672     ConstShapedNeighborhoodIteratorType::OffsetType offset1;
00673     offset1[0] = 0; offset1[1] = 0; offset1[2] = 0;
00674     cSnit.ActivateOffset( offset1 ); //activate the center
00675     // activate the top plane
00676     offset1[0] = 0; offset1[1] = 0; offset1[2] = -1;
00677     cSnit.ActivateOffset( offset1 ); 
00678     offset1[0] = 0; offset1[1] = 1; offset1[2] = -1;
00679     cSnit.ActivateOffset( offset1 ); 
00680     offset1[0] = 0; offset1[1] = -1; offset1[2] = -1;
00681     cSnit.ActivateOffset( offset1 ); 
00682     offset1[0] = 1; offset1[1] = 0; offset1[2] = -1;
00683     cSnit.ActivateOffset( offset1 ); 
00684     offset1[0] = 1; offset1[1] = 1; offset1[2] = -1;
00685     cSnit.ActivateOffset( offset1 ); 
00686     offset1[0] = 1; offset1[1] = -1; offset1[2] = -1;
00687     cSnit.ActivateOffset( offset1 ); 
00688     offset1[0] = -1; offset1[1] = 0; offset1[2] = -1;
00689     cSnit.ActivateOffset( offset1 ); 
00690     offset1[0] = -1; offset1[1] = 1; offset1[2] = -1;
00691     cSnit.ActivateOffset( offset1 ); 
00692     offset1[0] = -1; offset1[1] = -1; offset1[2] = -1;
00693     cSnit.ActivateOffset( offset1 ); 
00694  
00695     ConstShapedNeighborhoodIteratorType::IndexListType l
00696                                   = cSnit.GetActiveIndexList();
00697     ConstShapedNeighborhoodIteratorType::IndexListType::const_iterator 
00698                                                         ali = l.begin();
00699     while (ali != l.end())
00700       {
00701       std::cout << *ali << " ";
00702       ++ali;
00703       }
00704     std::cout << std::endl;
00705 
00706     ConstShapedNeighborhoodIteratorType::ConstIterator ci = cSnit.Begin();
00707     while (! ci.IsAtEnd())
00708       {
00709       ConstShapedNeighborhoodIteratorType::OffsetType offset2 = ci.GetNeighborhoodOffset();
00710       if( (offset2[0] == -1) && (offset2[1]== -1) && (offset2[2]== -1) )
00711         { 
00712         if( ci.GetNeighborhoodIndex() != 0 )
00713           {
00714           failed = true;
00715           std::cerr << "GetNeighborhoodOffset() on ConstShapedNeighborhoodIterato [FAILED]" 
00716                                                                               << std::endl;
00717           }
00718         if( (ci.Get()[0]!=0) || (ci.Get()[1]!=1) || (ci.Get()[2]!=2) )
00719           {
00720           failed=true;
00721           std::cerr 
00722             << "ConstShapedNeighborhoodIterator returned incorrect index [FAILED]" 
00723                                                                         << std::endl;
00724           }
00725         }
00726       ci++;
00727       }
00728 
00729     //
00730     // 4. Test ShapedNeighborhoodIterator 
00731     //
00732     typedef itk::ShapedNeighborhoodIterator< VectorImageType > 
00733                                       ShapedNeighborhoodIteratorType;
00734     ShapedNeighborhoodIteratorType sNit( radius, vectorImage, region );
00735     
00736     offset1[0] = 0; offset1[1] = 0; offset1[2] = 0;
00737     sNit.ActivateOffset( offset1 ); //activate the center
00738     // activate the top plane
00739     offset1[0] = 0; offset1[1] = 0; offset1[2] = -1;
00740     sNit.ActivateOffset( offset1 ); 
00741     offset1[0] = 0; offset1[1] = 1; offset1[2] = -1;
00742     sNit.ActivateOffset( offset1 ); 
00743     offset1[0] = 0; offset1[1] = -1; offset1[2] = -1;
00744     sNit.ActivateOffset( offset1 ); 
00745     offset1[0] = 1; offset1[1] = 0; offset1[2] = -1;
00746     sNit.ActivateOffset( offset1 ); 
00747     offset1[0] = 1; offset1[1] = 1; offset1[2] = -1;
00748     sNit.ActivateOffset( offset1 ); 
00749     offset1[0] = 1; offset1[1] = -1; offset1[2] = -1;
00750     sNit.ActivateOffset( offset1 ); 
00751     offset1[0] = -1; offset1[1] = 0; offset1[2] = -1;
00752     sNit.ActivateOffset( offset1 ); 
00753     offset1[0] = -1; offset1[1] = 1; offset1[2] = -1;
00754     sNit.ActivateOffset( offset1 ); 
00755     offset1[0] = -1; offset1[1] = -1; offset1[2] = -1;
00756     sNit.ActivateOffset( offset1 ); 
00757     
00758     sNit.SetLocation( location );
00759     ShapedNeighborhoodIteratorType::Iterator shit = sNit.Begin();
00760     shit = sNit.Begin(); 
00761     p[0] = p[3] = 10; p[1] = p[4] = 20; p[2] = p[5] = 30;
00762     shit.Set( p );
00763     index[0]=location[0]-1; index[1]=location[1]-1; index[2]=location[2]-1;
00764     cNit.SetLocation( index );
00765     if( cNit.GetCenterPixel() != p )
00766       {
00767       std::cerr << "ShapedNeighborhoodIterator Set() [FAILED]" << std::endl;
00768       failed=true;
00769       }
00770 
00771     
00772     }  // End Testing Neighborhood Iterators on VectorImage
00773 
00774   }
00775 
00776   if( failed )
00777     {
00778     return EXIT_FAILURE;
00779     }
00780 
00781   return EXIT_SUCCESS;
00782 }
00783 
00784   

Generated at Sun Mar 11 23:25:30 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000