1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkImageSpatialObjectTest.cxx,v $
Language: C++
Date: $Date: 2009-04-07 14:34:48 $
Version: $Revision: 1.17 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
// Disable warning for long symbol names in this file only
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif
/*
* This is a test file for the itkImageSpatialObject class.
* The suported pixel types does not include itkRGBPixel, itkRGBAPixel, etc...
* So far it only allows to manage images of simple types like unsigned short,
* unsigned int, or itk::Vector<...>.
*/
#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkAffineTransform.h"
#include "itkPoint.h"
#include "itkImageSpatialObject.h"
#include "itkLinearInterpolateImageFunction.h"
int itkImageSpatialObjectTest(int, char* [])
{
#define NDimensions 3
typedef double ScalarType;
typedef unsigned short Pixel;
typedef itk::Image<Pixel,NDimensions> ImageType;
typedef itk::ImageSpatialObject<NDimensions,Pixel> ImageSpatialObject;
typedef ImageSpatialObject::BoundingBoxType BoundingBox;
typedef itk::ImageRegionIterator<ImageType> Iterator;
typedef itk::Point<ScalarType,NDimensions> Point;
ImageType::Pointer image = ImageType::New();
ImageType::SizeType size = {{ 10, 10, 10 }};
ImageType::IndexType index = {{ 0, 0, 0 }};
ImageType::RegionType region;
ImageType::PointType origin;
origin.Fill(5);
region.SetSize(size);
region.SetIndex(index);
image->SetOrigin(origin);
image->SetLargestPossibleRegion(region);
image->SetBufferedRegion(region);
image->SetRequestedRegion(region);
image->Allocate();
Iterator it(image,region);
Pixel p =0;
for(; !it.IsAtEnd(); ++it, ++p)
{
it.Set(p);
}
it.GoToBegin();
ImageSpatialObject::Pointer imageSO = ImageSpatialObject::New();
imageSO->Print(std::cout);
imageSO->SetImage(image);
ImageSpatialObject::TransformType::OffsetType offset;
offset.Fill(5);
imageSO->GetObjectToParentTransform()->SetOffset(offset);
imageSO->ComputeObjectToWorldTransform();
Point q,r;
double returnedValue,expectedValue;
r.Fill(9);
q.Fill(15);
imageSO->ComputeBoundingBox();
std::cout << "Bounding Box = " << imageSO->GetBoundingBox()->GetBounds() << std::endl;
std::cout<<"IsInside()...";
if( imageSO->IsInside(r) || !imageSO->IsInside(q) )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
q.Fill(15.1);
expectedValue = 555;
try
{
imageSO->ValueAt(q,returnedValue);
}
catch( itk::ExceptionObject & e )
{
throw e;
}
std::cout<<"ValueAt()...";
if( returnedValue != expectedValue )
{
std::cout << "Expected: " << expectedValue << " returned: " << returnedValue << std::endl;
std::cout <<"[FAILED]: " << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
ImageSpatialObject::OutputVectorType derivative,expectedDerivative;
Pixel expectedPixel;
imageSO->DerivativeAt(q,1,derivative);
expectedPixel = 1;
expectedDerivative[0]=expectedPixel;
expectedPixel = 10;
expectedDerivative[1]=expectedPixel;
expectedPixel = 100;
expectedDerivative[2]=expectedPixel;
std::cout<<"DerivativeAt()...";
if( derivative != expectedDerivative )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
// Now testing the ValueAt() with an interpolator
typedef itk::LinearInterpolateImageFunction<ImageType> InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
imageSO->SetInterpolator(interpolator);
expectedValue = 566.1;
try
{
imageSO->ValueAt(q,returnedValue);
}
catch( itk::ExceptionObject & e )
{
throw e;
}
std::cout<<"ValueAt() with interpolator...";
if( vcl_fabs(returnedValue-expectedValue)>0.001 )
{
std::cout << "Expected: " << expectedValue << " returned: " << returnedValue << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
imageSO->DerivativeAt(q,1,derivative);
expectedDerivative[0]=1;
expectedDerivative[1]=10;
expectedDerivative[2]=100;
std::cout<<"DerivativeAt() with interpolator ...";
if( vcl_fabs(derivative[0]-expectedDerivative[0])>0.00001
|| vcl_fabs(derivative[1]-expectedDerivative[1])>0.00001
|| vcl_fabs(derivative[2]-expectedDerivative[2])>0.00001
)
{
std::cout << "Expected: " << derivative << " returned: " << expectedDerivative << std::endl;
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
imageSO->Print(std::cout);
return EXIT_SUCCESS;
}
|