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

Generated at Wed Nov 5 20:13:06 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000