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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include <iostream>
#include "itkImage.h"
#include "itkImageFunction.h"
#include "itkFloatingPointExceptions.h"
namespace itk {
template< typename TInputImage, typename TCoordRep = SpacePrecisionType >
class TestImageFunction:
public ImageFunction< TInputImage,
typename NumericTraits<
typename TInputImage::PixelType >::RealType,
TCoordRep >
{
public:
/** Standard class typedefs. */
typedef TestImageFunction Self;
typedef ImageFunction< TInputImage,
typename NumericTraits<
typename TInputImage::PixelType >::RealType,
TCoordRep >
Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
/** Run-time type information (and related methods). */
itkTypeMacro(TestImageFunction, ImageFunction);
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** OutputType typedef support. */
typedef typename Superclass::OutputType OutputType;
/** InputImageType typedef support. */
typedef typename Superclass::InputImageType InputImageType;
/** Dimension underlying input image. */
itkStaticConstMacro(ImageDimension, unsigned int,
Superclass::ImageDimension);
/** Point typedef support. */
typedef typename Superclass::PointType PointType;
/** Index typedef support. */
typedef typename Superclass::IndexType IndexType;
typedef typename Superclass::IndexValueType IndexValueType;
/** ContinuousIndex typedef support. */
typedef typename Superclass::ContinuousIndexType ContinuousIndexType;
/** Evaluate the function at specified Point position.*/
virtual OutputType Evaluate(const PointType & itkNotUsed(point) ) const ITK_OVERRIDE
{
OutputType result(0);
return result;
}
/** Evaluate the function at specified Index position. */
virtual OutputType EvaluateAtIndex(const IndexType & itkNotUsed(index) ) const ITK_OVERRIDE
{
OutputType result(0);
return result;
}
/** Evaluate the function at specified ContinuousIndex position. */
virtual OutputType EvaluateAtContinuousIndex( const ContinuousIndexType & itkNotUsed(index) ) const ITK_OVERRIDE
{
OutputType result(0);
return result;
}
protected:
TestImageFunction(){};
~TestImageFunction(){};
private:
TestImageFunction(const Self &) ITK_DELETE_FUNCTION;
void operator=(const Self &) ITK_DELETE_FUNCTION;
};
}//namespace
/****************************************/
int itkImageFunctionTest( int , char*[] )
{
bool result = EXIT_SUCCESS;
const unsigned int Dimension = 3;
typedef float PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef ImageType::RegionType RegionType;
typedef RegionType::SizeType SizeType;
typedef ImageType::IndexType IndexType;
typedef float CoordRepType;
typedef itk::ContinuousIndex<CoordRepType, Dimension> ContinuousIndexType;
typedef itk::Point<CoordRepType, Dimension> PointType;
typedef itk::NumericTraits<IndexType::IndexValueType>
IndexNumericTraits;
typedef itk::NumericTraits<ContinuousIndexType::ValueType>
ContinuousIndexNumericTraits;
typedef itk::NumericTraits<PointType::ValueType>
PointNumericTraits;
typedef itk::TestImageFunction< ImageType, CoordRepType > FunctionType;
ImageType::Pointer image = ImageType::New();
IndexType start;
start.Fill( 1 );
SizeType size;
size[0] = 3;
size[1] = 4;
size[2] = 5;
RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
ImageType::PointType origin;
ImageType::SpacingType spacing;
origin.Fill( 0.0 );
spacing.Fill( 1.0 );
image->SetOrigin( origin );
image->SetSpacing( spacing );
image->Print( std::cout );
FunctionType::Pointer function = FunctionType::New();
/* Test SetInputImage & Accessors */
function->SetInputImage( image );
const IndexType & endIndex = function->GetEndIndex();
const IndexType & startIndex = function->GetStartIndex();
const FunctionType::ContinuousIndexType & endIndexC = function->GetEndContinuousIndex();
const FunctionType::ContinuousIndexType & startIndexC = function->GetStartContinuousIndex();
for ( unsigned int j = 0; j < Dimension; j++ )
{
if( startIndex[j] != start[0] ||
endIndex[j] !=
static_cast<IndexType::IndexValueType>(start[0]+size[j] -1) ||
startIndexC[j] != start[0]-0.5 ||
endIndexC[j] != start[0]+size[j] -0.5 )
{
std::cout << "Error in SetInputImage with index bounds." << std::endl;
return EXIT_FAILURE;
}
}
/* GetInputImage */
ImageType::ConstPointer returnedImage = function->GetInputImage();
if( returnedImage.IsNull() )
{
std::cout << "Error with GetInputImage." << std::endl;
return EXIT_FAILURE;
}
/* Evaluate* methods - skip since they're pure virtual. */
/*
* IsInsideBuffer methods.
*/
/* IsInsideBuffer with Integer index type */
IndexType index;
index = startIndex;
if( !function->IsInsideBuffer( index ) )
{
std::cout << "Error with IsInsideBuffer 1." << std::endl;
result = EXIT_FAILURE;
}
index = endIndex;
if( !function->IsInsideBuffer( index ) )
{
std::cout << "Error with IsInsideBuffer 2." << std::endl;
result = EXIT_FAILURE;
}
index[0] = startIndex[0]-1;
if( function->IsInsideBuffer( index ) )
{
std::cout << "Error with IsInsideBuffer 3. Expected false." << std::endl;
result = EXIT_FAILURE;
}
index[0] = IndexNumericTraits::max();
if( function->IsInsideBuffer( index ) )
{
std::cout << "Error with IsInsideBuffer 4. Expected false." << std::endl;
result = EXIT_FAILURE;
}
if( IndexNumericTraits::is_signed )
{
index[0] = IndexNumericTraits::min();
if( function->IsInsideBuffer( index ) )
{
std::cout << "Error with IsInsideBuffer 5. Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
/* IsInsideBuffer with Continuous index type */
FunctionType::ContinuousIndexType indexC( startIndexC );
if( !function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 1C." << std::endl;
result = EXIT_FAILURE;
}
indexC[0] = endIndexC[0] - 0.1;
indexC[1] = endIndexC[1] - 0.1;
indexC[2] = endIndexC[2] - 0.1;
if( !function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 2C." << std::endl;
result = EXIT_FAILURE;
}
indexC[0] = startIndexC[0]-1;
if( function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 3C. Expected false." << std::endl
<< " indexC: " << indexC << std::endl
<< " start/end continuous indecies: "
<< startIndexC << " " << endIndexC << std::endl;
result = EXIT_FAILURE;
}
indexC[0] = ContinuousIndexNumericTraits::max();
if( function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 4C. Expected false." << std::endl;
result = EXIT_FAILURE;
}
indexC[0] = ContinuousIndexNumericTraits::min();
if( function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 5C. Expected false." << std::endl;
result = EXIT_FAILURE;
}
/* Some tests cause floating point exceptions, so
* only run them when FPE are not enabled. */
if( ! itk::FloatingPointExceptions::GetEnabled() )
{
std::cout << "ContinuousIndex Tests. FPE's disabled." << std::endl;
if( ContinuousIndexNumericTraits::has_quiet_NaN )
{
indexC[0] = ContinuousIndexNumericTraits::quiet_NaN();
if( function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 6C. Expected false."
<< std::endl;
result = EXIT_FAILURE;
}
}
/* Note that signaling_NaN seems to simply wrap quiet_NaN */
if( ContinuousIndexNumericTraits::has_signaling_NaN )
{
indexC[0] = ContinuousIndexNumericTraits::signaling_NaN();
if( function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 7C. Expected false."
<< std::endl;
result = EXIT_FAILURE;
}
}
indexC[0] = ContinuousIndexNumericTraits::infinity();
if( function->IsInsideBuffer( indexC ) )
{
std::cout << "Error with IsInsideBuffer 8C. Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
else
{
std::cout
<< "ContinuousIndex. FPE's enabled. Skipping tests that throw FPE's."
<< std::endl;
}
/* IsInsideBuffer with Point type */
PointType point;
point.Fill(1);
if( !function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 1P." << std::endl;
result = EXIT_FAILURE;
}
point[0] = endIndexC[0] - 0.1;
point[1] = endIndexC[1] - 0.1;
point[2] = endIndexC[2] - 0.1;
if( !function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 2P." << std::endl;
result = EXIT_FAILURE;
}
point[0] = startIndex[0]-1;
if( function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 3P. Expected false." << std::endl;
result = EXIT_FAILURE;
}
if( ! itk::FloatingPointExceptions::GetEnabled() )
{
std::cout << "Tests with Point. FPE's disabled." << std::endl;
point[0] = PointNumericTraits::max();
/* Note that since this calls Image::TransformPhysicalPointToIndex,
* which calls ImageRegion::IsInside, the region range gets added to
* this max() which can cause overflow. But only on one or two machines,
* it seems. */
if( function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 4P. Expected false." << std::endl;
result = EXIT_FAILURE;
}
point[0] = PointNumericTraits::min();
if( function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 5P. Expected false." << std::endl;
result = EXIT_FAILURE;
}
if( PointNumericTraits::has_quiet_NaN )
{
std::cout << "Testing IsInsideBuffer(point) with quiet_NaN." << std::endl;
point[0] = PointNumericTraits::quiet_NaN();
if( function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 6P. Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
if( PointNumericTraits::has_signaling_NaN )
{
std::cout << "Testing IsInsideBuffer(point) with signaling_NaN."
<< std::endl;
point[0] = PointNumericTraits::signaling_NaN();
if( function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 7P. Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
point[0] = PointNumericTraits::infinity();
if( function->IsInsideBuffer( point ) )
{
std::cout << "Error with IsInsideBuffer 8P. Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
else
{
std::cout
<< "Point tests. FPE's enabled. Skipping tests that throw FPE's."
<< std::endl;
}
/* ConvertPointToNearestIndex
* With region origin of 0,0,0 and spacing of 1,1,1,
* this is straight-forward */
point[0]=1.1;
point[1]=1.2;
point[2]=1.3;
function->ConvertPointToNearestIndex( point, index );
if( index[0] != 1 || index[1] != 1 || index[2] != 1 )
{
std::cout << "Error with ConvertPointToNearestIndex." << std::endl
<< "point: " << point << " index: " << index << std::endl;
result = EXIT_FAILURE;
}
/* Test with NaN to see what happens */
if( ! itk::FloatingPointExceptions::GetEnabled() )
{
if( PointNumericTraits::has_quiet_NaN )
{
point[0] = PointNumericTraits::quiet_NaN();
//NOTE: this calls Image::TransformPhysicalPointToContinuousIndex
// but doesn't check return for true/false, and doesn't return
// true or false.
function->ConvertPointToNearestIndex( point, index );
std::cout << "ConvertPointToNearestIndex with quiet_NaN: "
<< index << std::endl;
}
}
/* Test with infinity to see what happens */
if( ! itk::FloatingPointExceptions::GetEnabled() )
{
point[0] = PointNumericTraits::infinity();
function->ConvertPointToNearestIndex( point, index );
std::cout << "ConvertPointToNearestIndex with infinity: "
<< index << std::endl;
}
/* ConvertPointToContinuousIndex */
point[0]=1.1;
point[1]=1.2;
point[2]=1.3;
function->ConvertPointToContinuousIndex( point, indexC );
std::cout << "ConvertPointToContinuousIndex." << std::endl
<< " point: " << point << " indexC: " << indexC << std::endl;
if( indexC[0] != point[0] || indexC[1] != point[1] || indexC[2] != point[2] )
{
std::cout << "Error with ConvertPointToContinuousIndex." << std::endl;
result = EXIT_FAILURE;
}
/* ConvertContinuousIndexToNearestIndex */
indexC[0]=1.1;
indexC[1]=1.2;
indexC[2]=1.3;
function->ConvertContinuousIndexToNearestIndex( indexC, index );
std::cout << "ConvertContinuousIndexToNearestIndex." << std::endl
<< " indexC: " << indexC << " index: " << index << std::endl;
if( index[0] != 1 || index[1] != 1 || index[2] != 1 )
{
std::cout << "Error with ConvertContinuousIndexToNearestIndex." << std::endl;
result = EXIT_FAILURE;
}
/* Test with NaN to see what happens */
if( ! itk::FloatingPointExceptions::GetEnabled() )
{
if( ContinuousIndexNumericTraits::has_quiet_NaN )
{
indexC[0] = ContinuousIndexNumericTraits::quiet_NaN();
function->ConvertContinuousIndexToNearestIndex( indexC, index );
std::cout << "ConvertContinuousIndexToNearestIndex with quiet_NaN:"
<< std::endl
<< " indexC: " << indexC << " index: " << index << std::endl;
}
}
return result;
}
|