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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkChangeInformationImageFilterTest.cxx
Language: C++
Date: $Date$
Version: $Revision$
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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include <iostream>
#include "itkChangeInformationImageFilter.h"
#include "itkImage.h"
const unsigned int ImageDimension = 3;
typedef itk::Image<float, ImageDimension> ImageType;
typedef ImageType::Pointer ImagePointer;
void PrintInformation(ImagePointer image1, ImagePointer image2)
{
unsigned int i, j;
std::cout << "Input " << " Output" << std::endl;
std::cout << "Origin" << " Origin" << std::endl;
for (i = 0; i < ImageDimension; i++)
{
std::cout << " " << image1->GetOrigin()[i] << " " << image2->GetOrigin()[i] << std::endl;
}
std::cout << "Spacing" << " Spacing" << std::endl;
for (i = 0; i < ImageDimension; i++)
{
std::cout << " " << image1->GetSpacing()[i] << " " << image2->GetSpacing()[i] << std::endl;
}
std::cout << "Direction" << " Direction" << std::endl;
for (i = 0; i < ImageDimension; i++)
{
std::cout << " ";
for (j = 0; j < ImageDimension; j++)
{
std::cout << image1->GetDirection()[i][j] << " ";
}
std::cout << " ";
for (j = 0; j < ImageDimension; j++)
{
std::cout << image2->GetDirection()[i][j] << " ";
}
std::cout << std::endl;
}
}
void PrintInformation3(ImagePointer image1, ImagePointer image2, ImagePointer image3)
{
unsigned int i, j;
std::cout << "Input " << " Output" << " Reference" << std::endl;
std::cout << "Origin" << " Origin" << " Origin" << std::endl;
for (i = 0; i < ImageDimension; i++)
{
std::cout << " " << image1->GetOrigin()[i] << " " << image2->GetOrigin()[i] << " " << image3->GetOrigin()[i] << std::endl;
}
std::cout << "Spacing" << " Spacing" << " Spacing" << std::endl;
for (i = 0; i < ImageDimension; i++)
{
std::cout << " " << image1->GetSpacing()[i] << " " << image2->GetSpacing()[i] << " " << image3->GetSpacing()[i] << std::endl;
}
std::cout << "Direction" << " Direction" << " Direction" << std::endl;
for (i = 0; i < ImageDimension; i++)
{
std::cout << " ";
for (j = 0; j < ImageDimension; j++)
{
std::cout << image1->GetDirection()[i][j] << " ";
}
std::cout << " ";
for (j = 0; j < ImageDimension; j++)
{
std::cout << image2->GetDirection()[i][j] << " ";
}
std::cout << " ";
for (j = 0; j < ImageDimension; j++)
{
std::cout << image3->GetDirection()[i][j] << " ";
}
std::cout << std::endl;
}
}
int itkChangeInformationImageFilterTest(int, char* [] )
{
typedef itk::Image<float, ImageDimension> ImageType;
typedef itk::ChangeInformationImageFilter<ImageType> FilterType;
typedef itk::FixedArray<double,ImageDimension> ArrayType;
ImageType::Pointer inputImage = ImageType::New();
ImageType::Pointer referenceImage = ImageType::New();
FilterType::Pointer filter = FilterType::New();
double spacing[ImageDimension] = {1, 2, 3};
double origin[ImageDimension] = {-100, -200, -300};
ImageType::DirectionType direction;
direction[0][0] = 1.0;
direction[1][0] = 0.0;
direction[2][0] = 0.0;
direction[0][1] = 0.0;
direction[1][1] = -1.0;
direction[2][1] = 0.0;
direction[0][2] = 0.0;
direction[1][2] = 0.0;
direction[2][2] = 1.0;
typedef itk::ImageRegion<ImageDimension> RegionType;
typedef itk::Size<ImageDimension> SizeType;
//typedef itk::ImageRegion<ImageDimension> RegionType;
SizeType size; size.Fill(20);
inputImage->SetRegions(size);
inputImage->Allocate();
ImageType::DirectionType referenceDirection;
referenceDirection[0][0] = 1.0;
referenceDirection[1][0] = 0.0;
referenceDirection[2][0] = 0.0;
referenceDirection[0][1] = 0.0;
referenceDirection[1][1] = -1.0;
referenceDirection[2][1] = 0.0;
referenceDirection[0][2] = 0.0;
referenceDirection[1][2] = 0.0;
referenceDirection[2][2] = 1.0;
double referenceOrigin[ImageDimension] = {-1000, -2000, -3000};
double referenceSpacing[ImageDimension] = {1000, 2000, 3000};
referenceImage->SetOrigin(referenceOrigin);
referenceImage->SetSpacing(referenceSpacing);
referenceImage->SetDirection(referenceDirection);
referenceImage->SetRegions(size);
referenceImage->Allocate();
inputImage->SetSpacing (spacing);
inputImage->SetOrigin (origin);
double newOrigin[ImageDimension] = {1000, 2000, 3000};
double newSpacing[ImageDimension] = {10, 20, 30};
long newOffset[ImageDimension] = {10, 20, 30};
ImageType::DirectionType newDirection;
newDirection[0][0] = 0.0;
newDirection[1][0] = 1.0;
newDirection[2][0] = 0.0;
newDirection[0][1] = -1.0;
newDirection[1][1] = 0.0;
newDirection[2][1] = 0.0;
newDirection[0][2] = 0.0;
newDirection[1][2] = 0.0;
newDirection[2][2] = -1.0;
filter->SetInput (inputImage);
filter->SetOutputSpacing (newSpacing);
filter->SetOutputOrigin (newOrigin);
filter->SetOutputOffset (newOffset);
filter->SetOutputDirection (newDirection);
filter->SetReferenceImage (referenceImage);
// Test GetObjectMacro
ImageType * referenceImage2 = filter->GetReferenceImage();
std::cout << "filter->GetReferenceImage(): " << referenceImage2 << std::endl;
// Test GetMacros
bool useReferenceImage = filter->GetUseReferenceImage();
std::cout << "filter->GetUseReferenceImage(): " << useReferenceImage << std::endl;
const ArrayType outputSpacing = filter->GetOutputSpacing();
std::cout << "filter->GetOutputSpacing(): " << outputSpacing << std::endl;
const ArrayType outputOrigin = filter->GetOutputOrigin();
std::cout << "filter->GetOutputOrigin(): " << outputOrigin << std::endl;
const ImageType::DirectionType outputDirection = filter->GetOutputDirection();
std::cout << "filter->GetOutputDirection(): "
<< std::endl
<< outputDirection << std::endl;
bool changeSpacing = filter->GetChangeSpacing();
std::cout << "filter->GetChangeSpacing(): " << changeSpacing << std::endl;
bool changeOrigin = filter->GetChangeOrigin();
std::cout << "filter->GetChangeOrigin(): " << changeOrigin << std::endl;
bool changeDirection = filter->GetChangeDirection();
std::cout << "filter->GetChangeDirection(): " << changeDirection << std::endl;
bool changeRegion = filter->GetChangeRegion();
std::cout << "filter->GetChangeRegion(): " << changeRegion << std::endl;
bool centerImage = filter->GetCenterImage();
std::cout << "filter->GetCenterImage(): " << centerImage << std::endl;
// Test GetVectorMacro
const long * outputOffset = filter->GetOutputOffset();
std::cout << "filter->GetOutputOffset(): " << outputOffset << std::endl;
// Catch any exceptions
try
{
std::cout << "-----------filter: " << filter << std::endl;
filter->Update();
std::cout << "-----------Default behavior: "<< std::endl;;
PrintInformation (inputImage, filter->GetOutput());
filter->ChangeAll();
filter->ChangeRegionOff();
filter->Update();
std::cout << "-----------ChangeAll(), ChangeRegionOff(): " << std::endl;;
PrintInformation (inputImage, filter->GetOutput());
filter->CenterImageOn();
filter->Update();
std::cout << "-----------CenterImageOn(): " << std::endl;;
PrintInformation (inputImage, filter->GetOutput());
filter->CenterImageOn();
filter->ChangeSpacingOff();
filter->Update();
std::cout << "-----------CenterImageOn(), ChangeSpacingOff(): " << std::endl;;
PrintInformation (inputImage, filter->GetOutput());
filter->CenterImageOn();
filter->ChangeSpacingOn();
filter->ChangeOriginOff();
filter->Update();
std::cout << "-----------CenterImageOn(), ChangeOriginOff(): " << std::endl;;
PrintInformation (inputImage, filter->GetOutput());
filter->CenterImageOff();
filter->ChangeNone();
filter->Update();
std::cout << "-----------ChangeNone(): " << std::endl;;
PrintInformation (inputImage, filter->GetOutput());
filter->CenterImageOff();
filter->UseReferenceImageOn();
filter->Update();
std::cout << "-----------ChangeNone(), UseReferenceOn(): " << std::endl;;
PrintInformation3 (inputImage, filter->GetOutput(), referenceImage);
filter->ChangeOriginOn();
filter->Update();
std::cout << "-----------ChangeOriginOn(), UseReferenceOn(): " << std::endl;;
PrintInformation3 (inputImage, filter->GetOutput(), referenceImage);
filter->ChangeOriginOff();
filter->ChangeSpacingOn();
filter->Update();
std::cout << "-----------ChangeSpacingOn(), UseReferenceOn(): " << std::endl;;
PrintInformation3 (inputImage, filter->GetOutput(), referenceImage);
filter->ChangeOriginOff();
filter->ChangeSpacingOff();
filter->ChangeDirectionOn();
filter->Update();
std::cout << "-----------ChangeDirectionOn(), UseReferenceOn(): " << std::endl;;
PrintInformation3 (inputImage, filter->GetOutput(), referenceImage);
filter->ChangeAll();
filter->UpdateLargestPossibleRegion();
std::cout << "-----------ChangeAll(), UseReferenceOn(): " << std::endl;;
PrintInformation3 (inputImage, filter->GetOutput(), referenceImage);
}
catch (itk::ExceptionObject& e)
{
std::cerr << "Exception detected: " << e;
return -1;
}
return EXIT_SUCCESS;
}
|