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
|
/*=========================================================================
Program: Visualization Toolkit
Module: UnitTestImplicitVolume.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/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 notice for more information.
=========================================================================*/
#include "vtkImageData.h"
#include "vtkImplicitVolume.h"
#include "vtkMathUtilities.h"
#include "vtkSmartPointer.h"
#include "vtkTestErrorObserver.h"
#include <cstdio>
#include <sstream>
static vtkSmartPointer<vtkImageData> MakeVolume(int, int, int);
int UnitTestImplicitVolume(int, char*[])
{
int status = 0;
const int dim = 5;
// Create a volume
vtkSmartPointer<vtkImageData> aVolume = MakeVolume(dim, dim, dim);
// Test empty print
std::cout << "Testing empty Print...";
vtkSmartPointer<vtkImplicitVolume> impVol = vtkSmartPointer<vtkImplicitVolume>::New();
std::ostringstream emptyPrint;
impVol->Print(emptyPrint);
std::cout << "Passed" << std::endl;
//. Test error messages
std::cout << "Testing errors...";
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
impVol->AddObserver(vtkCommand::ErrorEvent, errorObserver);
impVol->EvaluateFunction(0.0, 0.0, 0.0);
int status1 = errorObserver->CheckErrorMessage(
"Can't evaluate function: either volume is missing or volume has no point data");
double zero[3], zg[3];
zero[0] = zero[1] = zero[2] = 0.0;
impVol->EvaluateGradient(zero, zg);
status1 += errorObserver->CheckErrorMessage(
"Can't evaluate gradient: either volume is missing or volume has no point data");
if (status1)
{
std::cout << "Failed" << std::endl;
++status;
}
else
{
std::cout << "Passed" << std::endl;
}
// Test EvaluateFunction
std::cout << "Testing EvaluateFunction...";
int status2 = 0;
impVol->SetVolume(aVolume);
impVol->SetOutValue(-1000.0);
for (int k = 0; k < dim; k++)
{
for (int j = 0; j < dim; j++)
{
for (int i = 0; i < dim; i++)
{
double x = i + .5;
double y = j + .5;
double z = k;
double val = impVol->EvaluateFunction(x + .5, y, z);
if (x > (dim - 1) || y > (dim - 1) || z > (dim - 1))
{
if (val != impVol->GetOutValue())
{
std::cout << "For " << x << ", " << y << ", " << z << " expected "
<< impVol->GetOutValue() << " but got " << val << std::endl;
++status2;
}
}
else if (val != z)
{
std::cout << "For " << x << ", " << y << ", " << z << " expected " << z << " but got "
<< val << std::endl;
++status2;
}
}
}
}
if (status2)
{
std::cout << "Failed" << std::endl;
++status;
}
else
{
std::cout << "Passed" << std::endl;
}
// Test EvaluateGradient
std::cout << "Testing EvaluateGradient...";
int status3 = 0;
double tol = std::numeric_limits<double>::epsilon();
double og[3];
og[0] = og[1] = og[2] = -1000.0;
impVol->SetOutGradient(og);
for (int k = 0; k < dim; k++)
{
for (int j = 0; j < dim; j++)
{
for (int i = 0; i < dim; i++)
{
double xyz[3];
xyz[0] = i + .5;
xyz[1] = j + .5;
xyz[2] = k;
double n[3];
impVol->EvaluateGradient(xyz, n);
if (xyz[0] > (dim - 1) || xyz[1] > (dim - 1) || xyz[2] > (dim - 1))
{
if (n[0] != impVol->GetOutGradient()[0] || n[1] != impVol->GetOutGradient()[1] ||
n[2] != impVol->GetOutGradient()[2])
{
std::cout << "For " << xyz[0] << ", " << xyz[1] << ", " << xyz[2] << " expected "
<< impVol->GetOutGradient()[0] << ", " << impVol->GetOutGradient()[1] << ", "
<< impVol->GetOutGradient()[2] << " but got " << n[0] << ", " << n[1] << ", "
<< n[2] << std::endl;
++status3;
}
}
else if (!vtkMathUtilities::FuzzyCompare(0.0, n[0], tol) ||
!vtkMathUtilities::FuzzyCompare(0.0, n[1], tol) ||
!vtkMathUtilities::FuzzyCompare(-1.0, n[2], tol))
{
std::cout << "For " << xyz[0] << ", " << xyz[1] << ", " << xyz[2] << " expected "
<< "0, 0, -1"
<< " but got " << n[0] << ", " << n[1] << ", " << n[2] << std::endl;
++status3;
}
}
}
}
if (status3)
{
std::cout << "Failed" << std::endl;
++status;
}
else
{
std::cout << "Passed" << std::endl;
}
// Test non-empty print
std::cout << "Testing non-empty Print...";
std::ostringstream nonemptyPrint;
impVol->Print(nonemptyPrint);
std::cout << "Passed" << std::endl;
if (status)
{
return EXIT_FAILURE;
}
else
{
return EXIT_SUCCESS;
}
}
vtkSmartPointer<vtkImageData> MakeVolume(int dimx, int dimy, int dimz)
{
vtkSmartPointer<vtkImageData> aVolume = vtkSmartPointer<vtkImageData>::New();
aVolume->SetDimensions(dimx, dimy, dimz);
aVolume->AllocateScalars(VTK_FLOAT, 1);
float* pixel = static_cast<float*>(aVolume->GetScalarPointer(0, 0, 0));
float value = 0.0;
for (int z = 0; z < dimz; z++)
{
for (int y = 0; y < dimy; y++)
{
for (int x = 0; x < dimx; x++)
{
*pixel++ = value;
}
}
value += 1.0;
}
return aVolume;
}
|