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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestImageDataLIC2D.h
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.
=========================================================================*/
#ifndef __TestImageDataLIC2D_h
#define __TestImageDataLIC2D_h
#include "vtkGenericDataObjectReader.h"
#include "vtkImageDataLIC2D.h"
#include "vtkImageData.h"
#include "vtkImageIterator.h"
#include "vtkImagePermute.h"
#include "vtkImageShiftScale.h"
#include "vtkPNGReader.h"
#include "vtkPNGWriter.h"
#include "vtkProbeFilter.h"
#include "vtkRenderWindow.h"
#include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStructuredData.h"
#include "vtkTimerLog.h"
#include "vtkUnstructuredGrid.h"
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
#include "vtkTesting.h"
#include <vtksys/CommandLineArguments.hxx>
#include "vtkImageMapToColors.h"
#include "vtkLookupTable.h"
#include "vtkTrivialProducer.h"
#define CREATE_NEW(var, class) vtkSmartPointer<class> var = vtkSmartPointer<class>::New();
//-----------------------------------------------------------------------------
void Merge(vtkImageData* dest, vtkImageData* src)
{
if (!src || !dest)
{
return;
}
if (src->GetScalarType() != dest->GetScalarType())
{
cout << src->GetScalarTypeAsString() << ", " << dest->GetScalarTypeAsString() << endl;
abort();
}
vtkImageIterator<unsigned char> inIt(src, src->GetExtent());
int outextent[6];
src->GetExtent(outextent);
vtkImageIterator<unsigned char> outIt(dest, outextent);
while (!outIt.IsAtEnd() && !inIt.IsAtEnd())
{
unsigned char* spanOut = outIt.BeginSpan();
unsigned char* spanIn = inIt.BeginSpan();
unsigned char* outSpanEnd = outIt.EndSpan();
unsigned char* inSpanEnd = inIt.EndSpan();
if (outSpanEnd != spanOut && inSpanEnd != spanIn)
{
size_t minO = outSpanEnd - spanOut;
size_t minI = inSpanEnd - spanIn;
memcpy(spanOut, spanIn, (minO < minI)? minO : minI);
}
inIt.NextSpan();
outIt.NextSpan();
}
}
// Example demonstrating use of vtkImageDataLIC2D filter.
// Typical usage:
// ./bin/ImageDataLIC2D --data=<vtk file> --output=<png file>
int ImageDataLIC2D(int argc, char* argv[])
{
std::string filename;
std::string noise_filename;
int resolution = 10;
int magnification = 1;
std::string outputpath;
int num_partitions = 1;
int num_steps = 40;
vtksys::CommandLineArguments arg;
arg.StoreUnusedArguments(1);
arg.Initialize(argc, argv);
typedef vtksys::CommandLineArguments argT;
arg.AddArgument("--data", argT::EQUAL_ARGUMENT, &filename,
"(required) Enter dataset to load (currently only *.vtk files are supported");
arg.AddArgument("--res", argT::EQUAL_ARGUMENT, &resolution,
"(optional: default 10) Number of sample per unit distance");
arg.AddArgument("--mag", argT::EQUAL_ARGUMENT, &magnification,
"(optional: default 1) Magnification");
arg.AddArgument("--output", argT::EQUAL_ARGUMENT, &outputpath,
"(optional) Output png image");
arg.AddArgument("--partitions", argT::EQUAL_ARGUMENT, &num_partitions,
"(optional: default 1) Number of partitions");
arg.AddArgument("--num-steps", argT::EQUAL_ARGUMENT, &num_steps,
"(optional: default 40) Number of steps in each direction");
arg.AddArgument("--noise", argT::EQUAL_ARGUMENT, &noise_filename,
"(optional) Specify the filename to a png image file to use as the noise texture.");
if (!arg.Parse() || filename=="")
{
cerr << "Problem parsing arguments." << endl;
cerr << arg.GetHelp() << endl;
return 1;
}
if (magnification < 1)
{
cout << "WARNING: Magnification \'" << magnification << "\' is invalid."
" Forcing a magnification of 1.";
magnification = 1;
}
if (num_steps < 1)
{
cout << "WARNING: Number of steps cannot be less than 1. Forcing 10.";
num_steps = 10;
}
CREATE_NEW(reader,vtkGenericDataObjectReader);
reader->SetFileName(filename.c_str());
reader->Update();
double bounds[6];
vtkDataSet::SafeDownCast(reader->GetOutput())->GetBounds(bounds);
// If 3D use XY slice, otherwise use non-trivial slice.
int dataDesc = VTK_XY_PLANE;
if (bounds[0] == bounds[1])
{
dataDesc = VTK_YZ_PLANE;
}
else if (bounds[2] == bounds[3])
{
dataDesc = VTK_XZ_PLANE;
}
else if (bounds[4] == bounds[5])
{
dataDesc = VTK_XY_PLANE;
}
CREATE_NEW(probeData,vtkImageData);
probeData->SetOrigin(bounds[0], bounds[2], bounds[4]);
int width = 0;
int height = 0;
switch (dataDesc)
{
case VTK_XY_PLANE:
width = static_cast<int>(ceil((bounds[1]-bounds[0]) * resolution));
height = static_cast<int>(ceil((bounds[3]-bounds[2]) * resolution));
probeData->SetDimensions(width, height, 1);
probeData->SetSpacing(
(bounds[1]-bounds[0])/double(width), (bounds[3]-bounds[2])/double(height), 1);
break;
case VTK_YZ_PLANE:
width = static_cast<int>(ceil((bounds[3]-bounds[2]) * resolution));
height = static_cast<int>(ceil((bounds[5]-bounds[4]) * resolution));
probeData->SetDimensions(1, width, height);
probeData->SetSpacing(
1, (bounds[3]-bounds[2])/double(width), (bounds[5]-bounds[4])/double(height));
break;
case VTK_XZ_PLANE:
width = static_cast<int>(ceil((bounds[1]-bounds[0]) * resolution));
height = static_cast<int>(ceil((bounds[5]-bounds[4]) * resolution));
probeData->SetDimensions(width, 1, height);
probeData->SetSpacing(
(bounds[1]-bounds[0])/double(width), 1, (bounds[5]-bounds[4])/double(height));
break;
}
CREATE_NEW(probe,vtkProbeFilter);
probe->SetSourceConnection(reader->GetOutputPort());
probe->SetInputData(probeData);
probe->Update();
CREATE_NEW(renWin, vtkRenderWindow);
renWin->Render();
CREATE_NEW(output, vtkImageData);
output->SetDimensions(width * magnification, height * magnification, 1);
output->SetSpacing(probeData->GetSpacing());
output->SetOrigin(probeData->GetOrigin());
output->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
CREATE_NEW( filter, vtkImageDataLIC2D );
if ( filter->SetContext( renWin ) == 0 )
{
cout << "Required OpenGL extensions / GPU not supported." << endl;
return 0;
}
filter->SetInputConnection(0, probe->GetOutputPort(0));
if (noise_filename != "")
{
CREATE_NEW(pngReader,vtkPNGReader);
pngReader->SetFileName(noise_filename.c_str());
filter->SetInputConnection(1, pngReader->GetOutputPort(0));
}
filter->SetSteps(num_steps);
filter->SetStepSize(0.8/magnification);
filter->SetMagnification(magnification);
filter->UpdateInformation();
int original_extents[6];
probeData->GetExtent(original_extents);
for (int kk=0; kk < num_partitions; kk++)
{
filter->SetUpdateExtent(0, kk, num_partitions, 0);
vtkTimerLog* timer = vtkTimerLog::New();
timer->StartTimer();
filter->Update();
if ( filter->GetFBOSuccess() == 0 ||
filter->GetLICSuccess() == 0 )
{
timer->Delete();
timer = NULL;
return 0;
}
timer->StopTimer();
//cout << "Time: " << timer->GetElapsedTime() << endl;
timer->Delete();
CREATE_NEW(clone, vtkImageData);
clone->ShallowCopy(filter->GetOutput());
// input is double between 0.0 and 1.0. Cast it between [0, 255].
CREATE_NEW(caster, vtkImageShiftScale);
caster->SetInputData(clone);
caster->SetShift(0.0);
caster->SetScale(255.0);
caster->SetOutputScalarTypeToUnsignedChar();
CREATE_NEW(permuter, vtkImagePermute);
permuter->SetInputConnection(caster->GetOutputPort());
switch (dataDesc)
{
case VTK_XY_PLANE:
permuter->SetFilteredAxes(0, 1, 2);
break;
case VTK_YZ_PLANE:
permuter->SetFilteredAxes(1, 2, 0);
break;
case VTK_XZ_PLANE:
permuter->SetFilteredAxes(0, 2, 1);
break;
}
permuter->Update();
::Merge(output, permuter->GetOutput());
}
CREATE_NEW(tester, vtkTesting);
for (int cc=0; cc < argc; cc++)
{
tester->AddArgument(argv[cc]);
}
if (outputpath != "")
{
CREATE_NEW(writer, vtkPNGWriter);
writer->SetFileName(outputpath.c_str());
writer->SetInputData(output);
writer->Write();
}
CREATE_NEW(tp, vtkTrivialProducer);
tp->SetOutput(output);
int retVal = (!tester->IsValidImageSpecified() ||
(tester->RegressionTest(tp, 10) == vtkTesting::PASSED))? /*success*/ 0 : /*failure*/ 1;
return retVal;
}
#endif
|