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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTesting.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 "vtkTesting.h"
#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkImageClip.h"
#include "vtkImageData.h"
#include "vtkImageDifference.h"
#include "vtkImageExtractComponents.h"
#include "vtkImageShiftScale.h"
#include "vtkInformation.h"
#include "vtkInteractorEventRecorder.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPNGReader.h"
#include "vtkPNGWriter.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkTimerLog.h"
#include "vtkToolkits.h"
#include "vtkWindowToImageFilter.h"
#include <sstream>
#include <vtksys/SystemTools.hxx>
vtkStandardNewMacro(vtkTesting);
vtkCxxSetObjectMacro(vtkTesting, RenderWindow, vtkRenderWindow);
using std::string;
using std::vector;
//-----------------------------------------------------------------------------
// Find in command tail, failing that find in environment,
// failing that return a default.
// Up to caller to delete the string returned.
static string vtkTestingGetArgOrEnvOrDefault(
const string& argName, // argument idnetifier flag. eg "-D"
vector<string>& argv, // command tail
const string& env, // environment variable name to find
const string& def) // default to use if "env" is not found.
{
string argValue;
// Search command tail.
int argc = static_cast<int>(argv.size());
for (int i = 0; i < argc; i++)
{
if ((i < (argc - 1)) && (argName == argv[i]))
{
argValue = argv[i + 1];
}
}
// If not found search environment.
if (argValue.empty() && !(env.empty() || def.empty()))
{
char* foundenv = getenv(env.c_str());
if (foundenv)
{
argValue = foundenv;
}
else
{
// Not found, fall back to default.
argValue = def;
}
}
return argValue;
}
//-----------------------------------------------------------------------------
// Description:
// Sum the L2 Norm point wise over all tuples. Each term
// is scaled by the magnitude of one of the inputs.
// Return sum and the number of terms.
template <class T>
vtkIdType AccumulateScaledL2Norm(T* pA, // pointer to first data array
T* pB, // pointer to second data array
vtkIdType nTups, // number of tuples
int nComps, // number of comps
double& SumModR) // result
{
//
SumModR = 0.0;
for (vtkIdType i = 0; i < nTups; ++i)
{
double modR = 0.0;
double modA = 0.0;
for (int q = 0; q < nComps; ++q)
{
double a = pA[q];
double b = pB[q];
modA += a * a;
double r = b - a;
modR += r * r;
}
modA = sqrt(modA);
modA = modA < 1.0 ? 1.0 : modA;
SumModR += sqrt(modR) / modA;
pA += nComps;
pB += nComps;
}
return nTups;
}
//=============================================================================
vtkTesting::vtkTesting()
{
this->FrontBuffer = 0;
this->RenderWindow = nullptr;
this->ValidImageFileName = nullptr;
this->ImageDifference = 0;
this->DataRoot = nullptr;
this->TempDirectory = nullptr;
this->BorderOffset = 0;
this->Verbose = 0;
// on construction we start the timer
this->StartCPUTime = vtkTimerLog::GetCPUTime();
this->StartWallTime = vtkTimerLog::GetUniversalTime();
}
//-----------------------------------------------------------------------------
vtkTesting::~vtkTesting()
{
this->SetRenderWindow(nullptr);
this->SetValidImageFileName(nullptr);
this->SetDataRoot(nullptr);
this->SetTempDirectory(nullptr);
}
//-----------------------------------------------------------------------------
void vtkTesting::AddArgument(const char* arg)
{
this->Args.push_back(arg);
}
//-----------------------------------------------------------------------------
void vtkTesting::AddArguments(int argc, const char** argv)
{
for (int i = 0; i < argc; ++i)
{
this->Args.push_back(argv[i]);
}
}
//-----------------------------------------------------------------------------
void vtkTesting::AddArguments(int argc, char** argv)
{
for (int i = 0; i < argc; ++i)
{
this->Args.push_back(argv[i]);
}
}
//-----------------------------------------------------------------------------
char* vtkTesting::GetArgument(const char* argName)
{
string argValue = vtkTestingGetArgOrEnvOrDefault(argName, this->Args, "", "");
char* cArgValue = new char[argValue.size() + 1];
strcpy(cArgValue, argValue.c_str());
return cArgValue;
}
//-----------------------------------------------------------------------------
void vtkTesting::CleanArguments()
{
this->Args.erase(this->Args.begin(), this->Args.end());
}
//-----------------------------------------------------------------------------
const char* vtkTesting::GetDataRoot()
{
#ifdef VTK_DATA_ROOT
string dr = vtkTestingGetArgOrEnvOrDefault("-D", this->Args, "VTK_DATA_ROOT", VTK_DATA_ROOT);
#else
string dr =
vtkTestingGetArgOrEnvOrDefault("-D", this->Args, "VTK_DATA_ROOT", "../../../../VTKData");
#endif
this->SetDataRoot(vtksys::SystemTools::CollapseFullPath(dr).c_str());
return this->DataRoot;
}
//-----------------------------------------------------------------------------
const char* vtkTesting::GetTempDirectory()
{
string td =
vtkTestingGetArgOrEnvOrDefault("-T", this->Args, "VTK_TEMP_DIR", "../../../Testing/Temporary");
this->SetTempDirectory(vtksys::SystemTools::CollapseFullPath(td).c_str());
return this->TempDirectory;
}
//-----------------------------------------------------------------------------
const char* vtkTesting::GetValidImageFileName()
{
this->SetValidImageFileName(nullptr);
if (!this->IsValidImageSpecified())
{
return this->ValidImageFileName;
}
string baseline =
vtkTestingGetArgOrEnvOrDefault("-B", this->Args, "VTK_BASELINE_ROOT", this->GetDataRoot());
for (size_t i = 0; i < (this->Args.size() - 1); ++i)
{
if (this->Args[i] == "-V")
{
const char* ch = this->Args[i + 1].c_str();
if (ch[0] == '/'
#ifdef _WIN32
|| (ch[0] >= 'a' && ch[0] <= 'z' && ch[1] == ':') ||
(ch[0] >= 'A' && ch[0] <= 'Z' && ch[1] == ':')
#endif
)
{
baseline = this->Args[i + 1];
}
else
{
baseline += "/";
baseline += this->Args[i + 1];
}
break;
}
}
this->SetValidImageFileName(baseline.c_str());
return this->ValidImageFileName;
}
//-----------------------------------------------------------------------------
int vtkTesting::IsInteractiveModeSpecified()
{
for (size_t i = 0; i < this->Args.size(); ++i)
{
if (this->Args[i] == "-I")
{
return 1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
int vtkTesting::IsFlagSpecified(const char* flag)
{
for (size_t i = 0; i < this->Args.size(); ++i)
{
if (this->Args[i] == flag)
{
return 1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
int vtkTesting::IsValidImageSpecified()
{
for (size_t i = 1; i < this->Args.size(); ++i)
{
if (this->Args[i - 1] == "-V")
{
return 1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
char* vtkTesting::IncrementFileName(const char* fname, int count)
{
char counts[256];
snprintf(counts, sizeof(counts), "%d", count);
int orgLen = static_cast<int>(strlen(fname));
if (orgLen < 5)
{
return nullptr;
}
int extLen = static_cast<int>(strlen(counts));
char* newFileName = new char[orgLen + extLen + 2];
strcpy(newFileName, fname);
newFileName[orgLen - 4] = '_';
int i, marker;
for (marker = orgLen - 3, i = 0; marker < orgLen - 3 + extLen; marker++, i++)
{
newFileName[marker] = counts[i];
}
strcpy(newFileName + marker, ".png");
return newFileName;
}
//-----------------------------------------------------------------------------
int vtkTesting::LookForFile(const char* newFileName)
{
if (!newFileName)
{
return 0;
}
vtksys::SystemTools::Stat_t fs;
if (vtksys::SystemTools::Stat(newFileName, &fs) != 0)
{
return 0;
}
else
{
return 1;
}
}
//-----------------------------------------------------------------------------
void vtkTesting::SetFrontBuffer(vtkTypeBool frontBuffer)
{
vtkWarningMacro("SetFrontBuffer method is deprecated and has no effet anymore.");
this->FrontBuffer = frontBuffer;
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTest(vtkAlgorithm* imageSource, double thresh)
{
int result = this->RegressionTest(imageSource, thresh, cout);
cout << "<DartMeasurement name=\"WallTime\" type=\"numeric/double\">";
cout << vtkTimerLog::GetUniversalTime() - this->StartWallTime;
cout << "</DartMeasurement>\n";
cout << "<DartMeasurement name=\"CPUTime\" type=\"numeric/double\">";
cout << vtkTimerLog::GetCPUTime() - this->StartCPUTime;
cout << "</DartMeasurement>\n";
return result;
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTestAndCaptureOutput(double thresh, ostream& os)
{
int result = this->RegressionTest(thresh, os);
os << "<DartMeasurement name=\"WallTime\" type=\"numeric/double\">";
os << vtkTimerLog::GetUniversalTime() - this->StartWallTime;
os << "</DartMeasurement>\n";
os << "<DartMeasurement name=\"CPUTime\" type=\"numeric/double\">";
os << vtkTimerLog::GetCPUTime() - this->StartCPUTime;
os << "</DartMeasurement>\n";
return result;
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTest(double thresh)
{
int result = this->RegressionTestAndCaptureOutput(thresh, cout);
return result;
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTest(double thresh, ostream& os)
{
vtkNew<vtkWindowToImageFilter> rtW2if;
rtW2if->SetInput(this->RenderWindow);
for (unsigned int i = 0; i < this->Args.size(); ++i)
{
if ("-FrontBuffer" == this->Args[i])
{
vtkWarningMacro("-FrontBuffer option is deprecated and has no effet anymore.");
this->FrontBufferOn();
}
else if ("-NoRerender" == this->Args[i])
{
rtW2if->ShouldRerenderOff();
}
}
std::ostringstream out1;
// perform and extra render to make sure it is displayed
int swapBuffers = this->RenderWindow->GetSwapBuffers();
// since we're reading from back-buffer, it's essential that we turn off swapping
// otherwise what remains in the back-buffer after the swap is undefined by OpenGL specs.
this->RenderWindow->SwapBuffersOff();
this->RenderWindow->Render();
rtW2if->ReadFrontBufferOff();
rtW2if->Update();
this->RenderWindow->SetSwapBuffers(swapBuffers); // restore swap state.
int res = this->RegressionTest(rtW2if, thresh, out1);
if (res == FAILED)
{
std::ostringstream out2;
// tell it to read front buffer
rtW2if->ReadFrontBufferOn();
rtW2if->Update();
res = this->RegressionTest(rtW2if, thresh, out2);
// If both tests fail, rerun the backbuffer tests to recreate the test
// image. Otherwise an incorrect image will be uploaded to CDash.
if (res == PASSED)
{
os << out2.str();
}
else
{
// we failed both back and front buffers so
// to help us debug, write out renderwindow capabilities
if (this->RenderWindow)
{
os << this->RenderWindow->ReportCapabilities();
}
rtW2if->ReadFrontBufferOff();
rtW2if->Update();
return this->RegressionTest(rtW2if, thresh, os);
}
}
else
{
os << out1.str();
}
return res;
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTest(const string& pngFileName, double thresh)
{
return this->RegressionTest(pngFileName, thresh, cout);
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTest(const string& pngFileName, double thresh, ostream& os)
{
vtkNew<vtkPNGReader> inputReader;
inputReader->SetFileName(pngFileName.c_str());
inputReader->Update();
vtkAlgorithm* src = inputReader;
vtkSmartPointer<vtkImageExtractComponents> extract;
// Convert rgba to rgb if needed
if (inputReader->GetOutput() && inputReader->GetOutput()->GetNumberOfScalarComponents() == 4)
{
extract = vtkSmartPointer<vtkImageExtractComponents>::New();
extract->SetInputConnection(src->GetOutputPort());
extract->SetComponents(0, 1, 2);
extract->Update();
src = extract;
}
return this->RegressionTest(src, thresh, os);
}
//-----------------------------------------------------------------------------
int vtkTesting::RegressionTest(vtkAlgorithm* imageSource, double thresh, ostream& os)
{
// do a get to compute the real value
this->GetValidImageFileName();
string tmpDir = this->GetTempDirectory();
// construct the names for the error images
string validName = this->ValidImageFileName;
string::size_type slashPos = validName.rfind('/');
if (slashPos != string::npos)
{
validName = validName.substr(slashPos + 1);
}
// We want to print the filename of the best matching image for better
// comparisons in CDash:
string bestImageFileName = this->ValidImageFileName;
// check the valid image
FILE* rtFin = vtksys::SystemTools::Fopen(this->ValidImageFileName, "r");
if (rtFin)
{
fclose(rtFin);
}
else // there was no valid image, so write one to the temp dir
{
string vImage = tmpDir + "/" + validName;
rtFin = vtksys::SystemTools::Fopen(vImage, "wb");
if (rtFin)
{
fclose(rtFin);
vtkNew<vtkPNGWriter> rtPngw;
rtPngw->SetFileName(vImage.c_str());
rtPngw->SetInputConnection(imageSource->GetOutputPort());
rtPngw->Write();
os << "<DartMeasurement name=\"ImageNotFound\" type=\"text/string\">"
<< this->ValidImageFileName << "</DartMeasurement>" << endl;
// Write out the image upload tag for the test image.
os << "<DartMeasurementFile name=\"TestImage\" type=\"image/png\">";
os << vImage;
os << "</DartMeasurementFile>";
}
else
{
vtkErrorMacro("Could not open file '" << vImage << "' for writing.");
}
return FAILED;
}
imageSource->Update();
vtkNew<vtkPNGReader> rtPng;
rtPng->SetFileName(this->ValidImageFileName);
rtPng->Update();
vtkNew<vtkImageExtractComponents> rtExtract;
rtExtract->SetInputConnection(rtPng->GetOutputPort());
rtExtract->SetComponents(0, 1, 2);
rtExtract->Update();
vtkNew<vtkImageDifference> rtId;
vtkNew<vtkImageClip> ic1;
ic1->SetClipData(1);
ic1->SetInputConnection(imageSource->GetOutputPort());
vtkNew<vtkImageClip> ic2;
ic2->SetClipData(1);
ic2->SetInputConnection(rtExtract->GetOutputPort());
int* wExt1 = ic1->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
int* wExt2 = ic2->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
ic1->SetOutputWholeExtent(wExt1[0] + this->BorderOffset, wExt1[1] - this->BorderOffset,
wExt1[2] + this->BorderOffset, wExt1[3] - this->BorderOffset, wExt1[4], wExt1[5]);
ic2->SetOutputWholeExtent(wExt2[0] + this->BorderOffset, wExt2[1] - this->BorderOffset,
wExt2[2] + this->BorderOffset, wExt2[3] - this->BorderOffset, wExt2[4], wExt2[5]);
int ext1[6], ext2[6];
rtId->SetInputConnection(ic1->GetOutputPort());
ic1->Update();
ic1->GetOutput()->GetExtent(ext1);
rtId->SetImageConnection(ic2->GetOutputPort());
ic2->Update();
ic2->GetOutput()->GetExtent(ext2);
double minError = VTK_DOUBLE_MAX;
if ((ext2[1] - ext2[0]) == (ext1[1] - ext1[0]) && (ext2[3] - ext2[2]) == (ext1[3] - ext1[2]) &&
(ext2[5] - ext2[4]) == (ext1[5] - ext1[4]))
{
// Cannot compute difference unless image sizes are the same
rtId->Update();
minError = rtId->GetThresholdedError();
}
this->ImageDifference = minError;
int passed = 0;
if (minError <= thresh)
{
// Make sure there was actually a difference image before
// accepting the error measure.
vtkImageData* output = rtId->GetOutput();
if (output)
{
int dims[3];
output->GetDimensions(dims);
if (dims[0] * dims[1] * dims[2] > 0)
{
passed = 1;
}
else
{
vtkErrorMacro("ImageDifference produced output with no data.");
}
}
else
{
vtkErrorMacro("ImageDifference did not produce output.");
}
}
// If the test failed with the first image (foo.png) check if there are
// images of the form foo_N.png (where N=1,2,3...) and compare against
// them.
double error;
int count = 1, errIndex = -1;
char* newFileName;
while (!passed)
{
newFileName = IncrementFileName(this->ValidImageFileName, count);
if (!LookForFile(newFileName))
{
delete[] newFileName;
break;
}
rtPng->SetFileName(newFileName);
// Need to reset the output whole extent cause we may have baselines
// of differing sizes. (Yes, we have such cases !)
ic2->ResetOutputWholeExtent();
ic2->SetOutputWholeExtent(wExt2[0] + this->BorderOffset, wExt2[1] - this->BorderOffset,
wExt2[2] + this->BorderOffset, wExt2[3] - this->BorderOffset, wExt2[4], wExt2[5]);
ic2->UpdateWholeExtent();
rtId->GetImage()->GetExtent(ext2);
if ((ext2[1] - ext2[0]) == (ext1[1] - ext1[0]) && (ext2[3] - ext2[2]) == (ext1[3] - ext1[2]) &&
(ext2[5] - ext2[4]) == (ext1[5] - ext1[4]))
{
// Cannot compute difference unless image sizes are the same
rtId->Update();
error = rtId->GetThresholdedError();
}
else
{
error = VTK_DOUBLE_MAX;
}
if (error <= thresh)
{
// Make sure there was actually a difference image before
// accepting the error measure.
vtkImageData* output = rtId->GetOutput();
if (output)
{
int dims[3];
output->GetDimensions(dims);
if (dims[0] * dims[1] * dims[2] > 0)
{
minError = error;
passed = 1;
}
}
}
else
{
if (error < minError)
{
errIndex = count;
minError = error;
bestImageFileName = newFileName;
}
}
++count;
delete[] newFileName;
}
this->ImageDifference = minError;
// output some information
os << "<DartMeasurement name=\"ImageError\" type=\"numeric/double\">";
os << minError;
os << "</DartMeasurement>";
if (errIndex <= 0)
{
os << "<DartMeasurement name=\"BaselineImage\" type=\"text/string\">Standard</DartMeasurement>";
}
else
{
os << "<DartMeasurement name=\"BaselineImage\" type=\"numeric/integer\">";
os << errIndex;
os << "</DartMeasurement>";
}
if (passed)
{
return PASSED;
}
// write out the image that was generated
string testImageFileName = tmpDir + "/" + validName;
FILE* testImageFile = vtksys::SystemTools::Fopen(testImageFileName, "wb");
if (testImageFile)
{
fclose(testImageFile);
vtkNew<vtkPNGWriter> rtPngw;
rtPngw->SetFileName(testImageFileName.c_str());
rtPngw->SetInputConnection(imageSource->GetOutputPort());
rtPngw->Write();
// Write out the image upload tag for the test image.
os << "<DartMeasurementFile name=\"TestImage\" type=\"image/png\">";
os << testImageFileName;
os << "</DartMeasurementFile>\n";
}
else
{
vtkErrorMacro("Could not open file '" << testImageFileName
<< "' for "
"writing.");
}
os << "Failed Image Test ( " << validName << " ) : " << minError << endl;
if (errIndex >= 0)
{
newFileName = IncrementFileName(this->ValidImageFileName, errIndex);
rtPng->SetFileName(newFileName);
delete[] newFileName;
}
else
{
rtPng->SetFileName(this->ValidImageFileName);
}
rtPng->Update();
rtId->GetImage()->GetExtent(ext2);
// If no image differences produced an image, do not write a
// difference image.
bool hasDiff = minError > 0;
if (!hasDiff)
{
os << "Image differencing failed to produce an image." << endl;
}
if (!((ext2[1] - ext2[0]) == (ext1[1] - ext1[0]) && (ext2[3] - ext2[2]) == (ext1[3] - ext1[2]) &&
(ext2[5] - ext2[4]) == (ext1[5] - ext1[4])))
{
os << "Image differencing failed to produce an image because images are "
"different size:"
<< endl;
os << "Valid image: " << (ext2[1] - ext2[0] + 1) << ", " << (ext2[3] - ext2[2] + 1) << ", "
<< (ext2[5] - ext2[4] + 1) << endl;
os << "Test image: " << (ext1[1] - ext1[0] + 1) << ", " << (ext1[3] - ext1[2] + 1) << ", "
<< (ext1[5] - ext1[4] + 1) << endl;
return FAILED;
}
rtId->Update();
// test the directory for writing
if (hasDiff)
{
string diffFilename = tmpDir + "/" + validName;
string::size_type dotPos = diffFilename.rfind('.');
if (dotPos != string::npos)
{
diffFilename = diffFilename.substr(0, dotPos);
}
diffFilename += ".diff.png";
FILE* rtDout = vtksys::SystemTools::Fopen(diffFilename, "wb");
if (rtDout)
{
fclose(rtDout);
// write out the difference image gamma adjusted for the dashboard
vtkNew<vtkImageShiftScale> rtGamma;
rtGamma->SetInputConnection(rtId->GetOutputPort());
rtGamma->SetShift(0);
rtGamma->SetScale(10);
rtGamma->ClampOverflowOn();
vtkNew<vtkPNGWriter> rtPngw;
rtPngw->SetFileName(diffFilename.c_str());
rtPngw->SetInputConnection(rtGamma->GetOutputPort());
rtPngw->Write();
os << "<DartMeasurementFile name=\"DifferenceImage\" type=\"image/png\">";
os << diffFilename;
os << "</DartMeasurementFile>";
}
else
{
vtkErrorMacro("Could not open file '" << diffFilename << "' for writing.");
}
}
os << "<DartMeasurementFile name=\"ValidImage\" type=\"image/png\">";
os << bestImageFileName;
os << "</DartMeasurementFile>";
return FAILED;
}
//-----------------------------------------------------------------------------
int vtkTesting::Test(int argc, char* argv[], vtkRenderWindow* rw, double thresh)
{
vtkNew<vtkTesting> testing;
for (int i = 0; i < argc; ++i)
{
testing->AddArgument(argv[i]);
}
if (testing->IsInteractiveModeSpecified())
{
return DO_INTERACTOR;
}
if (testing->IsValidImageSpecified())
{
testing->SetRenderWindow(rw);
return testing->RegressionTestAndCaptureOutput(thresh, cout);
}
return NOT_RUN;
}
//-----------------------------------------------------------------------------
int vtkTesting::CompareAverageOfL2Norm(vtkDataArray* daA, vtkDataArray* daB, double tol)
{
int typeA = daA->GetDataType();
int typeB = daB->GetDataType();
if (typeA != typeB)
{
vtkWarningMacro("Incompatible data types: " << typeA << "," << typeB << ".");
return 0;
}
//
vtkIdType nTupsA = daA->GetNumberOfTuples();
vtkIdType nTupsB = daB->GetNumberOfTuples();
int nCompsA = daA->GetNumberOfComponents();
int nCompsB = daB->GetNumberOfComponents();
//
if ((nTupsA != nTupsB) || (nCompsA != nCompsB))
{
vtkWarningMacro("Arrays: " << daA->GetName() << " (nC=" << nCompsA << " nT= " << nTupsA << ")"
<< " and " << daB->GetName() << " (nC=" << nCompsB
<< " nT= " << nTupsB << ")"
<< " do not have the same structure.");
return 0;
}
double L2 = 0.0;
vtkIdType N = 0;
switch (typeA)
{
case VTK_DOUBLE:
{
vtkDoubleArray* A = vtkArrayDownCast<vtkDoubleArray>(daA);
double* pA = A->GetPointer(0);
vtkDoubleArray* B = vtkArrayDownCast<vtkDoubleArray>(daB);
double* pB = B->GetPointer(0);
N = AccumulateScaledL2Norm(pA, pB, nTupsA, nCompsA, L2);
}
break;
case VTK_FLOAT:
{
vtkFloatArray* A = vtkArrayDownCast<vtkFloatArray>(daA);
float* pA = A->GetPointer(0);
vtkFloatArray* B = vtkArrayDownCast<vtkFloatArray>(daB);
float* pB = B->GetPointer(0);
N = AccumulateScaledL2Norm(pA, pB, nTupsA, nCompsA, L2);
}
break;
default:
if (this->Verbose)
{
cout << "Skipping:" << daA->GetName() << endl;
}
return true;
}
//
if (N <= 0)
{
return 0;
}
//
if (this->Verbose)
{
cout << "Sum(L2)/N of " << daA->GetName() << " < " << tol << "? = " << L2 << "/" << N << "."
<< endl;
}
//
double avgL2 = L2 / static_cast<double>(N);
if (avgL2 > tol)
{
return 0;
}
// Test passed
return 1;
}
//-----------------------------------------------------------------------------
int vtkTesting::CompareAverageOfL2Norm(vtkDataSet* dsA, vtkDataSet* dsB, double tol)
{
vtkDataArray* daA = nullptr;
vtkDataArray* daB = nullptr;
int status = 0;
// Compare points if the dataset derives from
// vtkPointSet.
vtkPointSet* ptSetA = vtkPointSet::SafeDownCast(dsA);
vtkPointSet* ptSetB = vtkPointSet::SafeDownCast(dsB);
if (ptSetA != nullptr && ptSetB != nullptr)
{
if (this->Verbose)
{
cout << "Comparing points:" << endl;
}
daA = ptSetA->GetPoints()->GetData();
daB = ptSetB->GetPoints()->GetData();
//
status = CompareAverageOfL2Norm(daA, daB, tol);
if (status == 0)
{
return 0;
}
}
// Compare point data arrays.
if (this->Verbose)
{
cout << "Comparing data arrays:" << endl;
}
int nDaA = dsA->GetPointData()->GetNumberOfArrays();
int nDaB = dsB->GetPointData()->GetNumberOfArrays();
if (nDaA != nDaB)
{
vtkWarningMacro("Point data, " << dsA << " and " << dsB << " differ in number of arrays"
<< " and cannot be compared.");
return 0;
}
//
for (int arrayId = 0; arrayId < nDaA; ++arrayId)
{
daA = dsA->GetPointData()->GetArray(arrayId);
daB = dsB->GetPointData()->GetArray(arrayId);
//
status = CompareAverageOfL2Norm(daA, daB, tol);
if (status == 0)
{
return 0;
}
}
// All tests passed.
return 1;
}
//-----------------------------------------------------------------------------
int vtkTesting::InteractorEventLoop(
int argc, char* argv[], vtkRenderWindowInteractor* iren, const char* playbackStream)
{
bool disableReplay = false, record = false, playbackFile = false;
std::string playbackFileName;
for (int i = 0; i < argc; i++)
{
disableReplay |= (strcmp("--DisableReplay", argv[i]) == 0);
record |= (strcmp("--Record", argv[i]) == 0);
playbackFile |= (strcmp("--PlaybackFile", argv[i]) == 0);
if (playbackFile && playbackFileName.empty())
{
if (i + 1 < argc)
{
playbackFileName = std::string(argv[i + 1]);
++i;
}
}
}
vtkNew<vtkInteractorEventRecorder> recorder;
recorder->SetInteractor(iren);
if (!disableReplay)
{
if (record)
{
recorder->SetFileName("vtkInteractorEventRecorder.log");
recorder->On();
recorder->Record();
}
else
{
if (playbackStream)
{
recorder->ReadFromInputStringOn();
recorder->SetInputString(playbackStream);
recorder->Play();
// Without this, the "-I" option if specified will fail
recorder->Off();
}
else if (playbackFile)
{
recorder->SetFileName(playbackFileName.c_str());
recorder->Play();
// Without this, the "-I" option if specified will fail
recorder->Off();
}
}
}
// iren will be either the object factory instantiation (vtkTestingInteractor)
// or vtkRenderWindowInteractor depending on whether or not "-I" is specified.
iren->Start();
recorder->Off();
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------
void vtkTesting::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "RenderWindow: " << this->RenderWindow << endl;
os << indent
<< "ValidImageFileName: " << (this->ValidImageFileName ? this->ValidImageFileName : "(none)")
<< endl;
os << indent << "FrontBuffer: " << (this->FrontBuffer ? "On" : "Off") << endl;
os << indent << "ImageDifference: " << this->ImageDifference << endl;
os << indent << "DataRoot: " << this->GetDataRoot() << endl;
os << indent << "Temp Directory: " << this->GetTempDirectory() << endl;
os << indent << "BorderOffset: " << this->GetBorderOffset() << endl;
os << indent << "Verbose: " << this->GetVerbose() << endl;
}
|