~ubuntu-branches/ubuntu/precise/insighttoolkit/precise

« back to all changes in this revision

Viewing changes to Testing/Code/BasicFilters/itkDisplacementFieldJacobianDeterminantFilterTest.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2008-12-19 20:16:49 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081219201649-drt97guwl2ryt0cn

* New upstream version.
  - patches/nifti-versioning.patch: Remove.  Applied upstream.
  - control:
  - rules: Update version numbers, package names.

* control: Build-depend on uuid-dev (gdcm uses it).

* copyright: Update download URL.

* rules: Adhere to parallel=N in DEB_BUILD_OPTIONS by setting MAKEFLAGS.

* compat: Set to 7.
* control: Update build-dep on debhelper to version >= 7.

* CMakeCache.txt.debian: Set CMAKE_BUILD_TYPE to "RELEASE" so that we
  build with -O3 (not -O2), necessary to optimize the templated code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
 
 
3
  Program:   Insight Segmentation & Registration Toolkit
 
4
  Module:    $RCSfile: itkDisplacementFieldJacobianDeterminantFilterTest.cxx,v $
 
5
  Language:  C++
 
6
  Date:      $Date: 2008-12-08 01:10:43 $
 
7
  Version:   $Revision: 1.2.4.1 $
 
8
 
 
9
  Copyright (c) Insight Software Consortium. All rights reserved.
 
10
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
 
11
 
 
12
     This software is distributed WITHOUT ANY WARRANTY; without even
 
13
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
14
     PURPOSE.  See the above copyright notices for more information.
 
15
 
 
16
=========================================================================*/
 
17
#if defined(_MSC_VER)
 
18
#pragma warning ( disable : 4786 )
 
19
#endif
 
20
 
 
21
#include <iostream>
 
22
#include "itkImage.h"
 
23
#include "itkDisplacementFieldJacobianDeterminantFilter.h"
 
24
#include "itkNullImageToImageFilterDriver.txx"
 
25
#include "itkVector.h"
 
26
 
 
27
static bool TestDisplacementJacobianDeterminantValue(void)
 
28
{
 
29
  std::cout.precision(9);
 
30
  bool testPassed = true;
 
31
  const unsigned int ImageDimension = 2;
 
32
 
 
33
  typedef itk::Vector<float,ImageDimension> VectorType;
 
34
  typedef itk::Image<VectorType,ImageDimension> FieldType;
 
35
 
 
36
  // In this case, the image to be warped is also a vector field.
 
37
  typedef FieldType VectorImageType;
 
38
  typedef VectorImageType::PixelType  PixelType;
 
39
  typedef VectorImageType::IndexType  IndexType;
 
40
 
 
41
  //=============================================================
 
42
 
 
43
  std::cout << "Create the dispacementfield image pattern." << std::endl;
 
44
  VectorImageType::RegionType region;
 
45
  //NOTE:  Making the image size much larger than necessary in order to get
 
46
  //       some meaningful time measurements.  Simulate a 256x256x256 image.
 
47
  VectorImageType::SizeType size = {{4096, 4096}};
 
48
  region.SetSize( size );
 
49
 
 
50
  VectorImageType::Pointer dispacementfield = VectorImageType::New();
 
51
  dispacementfield->SetLargestPossibleRegion( region );
 
52
  dispacementfield->SetBufferedRegion( region );
 
53
  dispacementfield->Allocate();
 
54
 
 
55
  VectorType values;
 
56
  values[0]=0;
 
57
  values[1]=0;
 
58
  typedef itk::ImageRegionIteratorWithIndex<VectorImageType> Iterator;
 
59
  Iterator inIter( dispacementfield, region );
 
60
  for( ; !inIter.IsAtEnd(); ++inIter )
 
61
    {
 
62
    const unsigned int i=inIter.GetIndex()[0];
 
63
    const unsigned int j=inIter.GetIndex()[1];
 
64
    values[0]=0.125*i*i+0.125*j;
 
65
    values[1]=0.125*i*j+0.25*j;
 
66
    inIter.Set( values );
 
67
    //std::cout << "Setting: " << values << " at " << inIter.GetIndex() << std::endl;
 
68
    }
 
69
 
 
70
  //displacementfield:
 
71
  //|-------------------------------------------|
 
72
  //| [0.25;0.5]   | [0.375;0.75] | [0.75;1]    |
 
73
  //|-------------------------------------------|
 
74
  //| [0.125;0.25] | [0.25;0.375] | [0.625;0.5] |
 
75
  //|-------------------------------------------|
 
76
  //| [0.0;0.0]    | [0.125;0.0]  | [0.5;0]     |
 
77
  //|-------------------------------------------|
 
78
  //
 
79
  //J(1,1) = [ (.625-.125)/2 (.5-.25)/2; (.375-.125)/2 (.75-0.0)/2] =[ .25  .125; .125 .375]
 
80
  //det((J+I)(1,1))=((.25+1.0)*(.375+1.0))-(.125*.125) = 1.703125;
 
81
  const float KNOWN_ANSWER=(((.25+1.0)*(.375+1.0))-(.125*.125));
 
82
  itk::DisplacementFieldJacobianDeterminantFilter<VectorImageType,float>::Pointer
 
83
    filter =
 
84
    itk::DisplacementFieldJacobianDeterminantFilter<VectorImageType,float>::New();
 
85
 
 
86
  filter->SetInput(dispacementfield);
 
87
  filter->Update();
 
88
  itk::Image<float,2>::Pointer output=filter->GetOutput();
 
89
 
 
90
  VectorImageType::IndexType index;
 
91
  index[0]=1;
 
92
  index[1]=1;
 
93
  //std::cout << "Output "  << output->GetPixel(index) << std::endl;
 
94
  if(vcl_abs(output->GetPixel(index) - KNOWN_ANSWER) > 1e-13)
 
95
    {
 
96
    std::cout << "Test failed." << KNOWN_ANSWER << "!=" << output->GetPixel(index)  << std::endl;
 
97
    testPassed=false;
 
98
    }
 
99
  else
 
100
    {
 
101
    std::cout << "Test passed." << std::endl;
 
102
    }
 
103
  return testPassed;
 
104
}
 
105
 
 
106
int
 
107
itkDisplacementFieldJacobianDeterminantFilterTest(int , char * [] )
 
108
{
 
109
  bool ValueTestPassed=TestDisplacementJacobianDeterminantValue();
 
110
  try
 
111
    {
 
112
    typedef itk::Vector<float, 3> VectorType;
 
113
    typedef itk::Image< VectorType, 3> VectorImageType;
 
114
    typedef itk::Image< float, 3> ScalarVectorImageType;
 
115
 
 
116
    // Set up filter
 
117
 
 
118
    typedef itk::DisplacementFieldJacobianDeterminantFilter<VectorImageType,float> FilterType;
 
119
    FilterType::Pointer filter = FilterType::New();
 
120
    filter->Print(std::cout);
 
121
 
 
122
    // Run Test
 
123
    itk::Size<3> sz;
 
124
    sz[0] = 100 ;
 
125
    sz[1] = 100 ;
 
126
    sz[2] = 100 ;
 
127
    itk::NullImageToImageFilterDriver< VectorImageType, ScalarVectorImageType > test1;
 
128
    test1.SetImageSize(sz);
 
129
    test1.SetFilter(filter.GetPointer());
 
130
    test1.Execute();
 
131
    filter->Print(std::cout);
 
132
 
 
133
    // Run the Test again with ImageSpacingOn
 
134
    filter->SetUseImageSpacingOn();
 
135
    test1.Execute();
 
136
    filter->Print(std::cout);
 
137
 
 
138
    // Run the Test again with specified weights
 
139
    float weights[3] = {1.0,2.0,3.0};
 
140
    filter->SetDerivativeWeights(weights);
 
141
    test1.Execute();
 
142
    filter->Print(std::cout);
 
143
 
 
144
    }
 
145
  catch(itk::ExceptionObject &err)
 
146
    {
 
147
    std::cerr << err << std::endl;
 
148
    return EXIT_FAILURE;
 
149
    }
 
150
  if(ValueTestPassed == false)
 
151
    {
 
152
    return EXIT_FAILURE;
 
153
    }
 
154
  return EXIT_SUCCESS;
 
155
}
 
156