diff --git a/Core/Code/Testing/mitkImageVtkMapper2DTest.cpp b/Core/Code/Testing/mitkImageVtkMapper2DTest.cpp index f677e9d2af..d7969e450b 100644 --- a/Core/Code/Testing/mitkImageVtkMapper2DTest.cpp +++ b/Core/Code/Testing/mitkImageVtkMapper2DTest.cpp @@ -1,123 +1,75 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTestingMacros.h" #include "mitkDataNodeFactory.h" #include "mitkStandaloneDataStorage.h" #include #include #include -#include // nice one - -#include -#include +#include #include "mitkRenderingTestHelper.h" #include -class mitkRenderingTestHelperClass -{ - -public: - - static mitk::BaseData::Pointer AddToStorage(const std::string& filename) - { - mitk::DataNodeFactory::Pointer reader = mitk::DataNodeFactory::New(); - try - { - reader->SetFileName( filename ); - reader->Update(); - - if(reader->GetNumberOfOutputs()<1) - { - MITK_TEST_FAILED_MSG(<< "Could not find test data '" << filename << "'"); - } - - mitk::DataNode::Pointer node = reader->GetOutput( 0 ); - mitkRenderingTestHelperClass::s_DataStorage->Add(node); - return node->GetData(); - } - catch ( itk::ExceptionObject & e ) - { - MITK_TEST_FAILED_MSG(<< "Failed loading test data '" << filename << "': " << e.what()); - } - } - - static mitk::DataStorage::Pointer s_DataStorage; - -}; // end test helper class - -mitk::DataStorage::Pointer mitkRenderingTestHelperClass::s_DataStorage; - int mitkImageVtkMapper2DTest(int argc, char* argv[]) { // load all arguments into a datastorage, take last argument as reference rendering // setup a renderwindow of fixed size X*Y // render the datastorage // compare rendering to reference image MITK_TEST_BEGIN("mitkImageVtkMapper2DTest") - // enough parameters? - if ( argc < 2 ) + // enough parameters? + if ( argc < 2 ) { MITK_TEST_OUTPUT( << "Usage: " << std::string(*argv) << " [file1 file2 ...] outputfile" ) MITK_TEST_OUTPUT( << "Will render a central transversal slice of all given files into outputfile" ) exit( EXIT_SUCCESS ); } - // parse parameters - std::vector inputFileNames; - for (int i = 1; i < argc-1; ++i) - { - //add everything to a list but -T and -V - std::string tmp = argv[i]; - if((tmp.compare("-T")) && (tmp.compare("-V"))) - { - inputFileNames.push_back( tmp ); - } - } - // std::string outputFileName( argv[argc-1] ); - - // load all input into a data storage - mitkRenderingTestHelperClass::s_DataStorage = mitk::StandaloneDataStorage::New().GetPointer(); - MITK_TEST_CONDITION_REQUIRED(mitkRenderingTestHelperClass::s_DataStorage.IsNotNull(),"StandaloneDataStorage instantiation"); - - std::for_each( inputFileNames.begin(), inputFileNames.end(), mitkRenderingTestHelperClass::AddToStorage ); - - // create a mitkRenderWindow, let it render the scene and get the vtkRenderWindow - mitkRenderingTestHelper renderingHelper( 640, 480, mitkRenderingTestHelperClass::s_DataStorage ); + mitkRenderingTestHelper renderingHelper(640, 480, argc, argv); + renderingHelper.Render(); //use this to generate a reference screenshot or save the file: bool generateReferenceScreenshot = false; if(generateReferenceScreenshot) { renderingHelper.SaveAsPNG("/home/kilgus/Pictures/RenderingTestData/output.png"); } + + //### Usage of vtkRegressionTestImage: + //vtkRegressionTestImage( vtkRenderWindow ) + //Set a vtkRenderWindow containing the desired scene. + //vtkRegressionTestImage automatically searches in argc and argv[] + //for a path a valid image with -V. 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. int retVal = vtkRegressionTestImage( renderingHelper.GetVtkRenderWindow() ); //retVal meanings: (see VTK/Rendering/vtkTesting.h) //0 = test failed //1 = test passed //2 = test not run //3 = something with vtkInteraction MITK_TEST_CONDITION( retVal == 1, "VTK test result positive" ); MITK_TEST_END(); } diff --git a/Core/Code/Testing/mitkRenderingTestHelper.cpp b/Core/Code/Testing/mitkRenderingTestHelper.cpp index 98fce2fd8d..216c7cdd1b 100644 --- a/Core/Code/Testing/mitkRenderingTestHelper.cpp +++ b/Core/Code/Testing/mitkRenderingTestHelper.cpp @@ -1,87 +1,145 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkRenderingTestHelper.h" -#include "mitkStandaloneDataStorage.h" +#include #include #include #include -#include +#include #include -#include #include #include -#include -mitkRenderingTestHelper::mitkRenderingTestHelper(int width, int height, mitk::DataStorage *ds) +mitkRenderingTestHelper::mitkRenderingTestHelper(int width, int height, int argc, char* argv[]) { - // Global interaction must(!) be initialized - mitk::GlobalInteraction::GetInstance()->Initialize("global"); + // Global interaction must(!) be initialized + mitk::GlobalInteraction::GetInstance()->Initialize("global"); - m_RenderWindow = mitk::RenderWindow::New(); - m_RenderWindow->GetRenderer()->SetDataStorage(ds); - m_RenderWindow->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard2D); - this->GetVtkRenderWindow()->SetSize( width, height ); + m_DataStorage = mitk::StandaloneDataStorage::New(); - mitk::BaseRenderer::GetInstance(m_RenderWindow->GetVtkRenderWindow())->GetSliceNavigationController()->SetDefaultViewDirection(mitk::SliceNavigationController::Transversal); - mitk::TimeSlicedGeometry::Pointer geo = ds->ComputeBoundingGeometry3D(ds->GetAll()); + m_RenderWindow = mitk::RenderWindow::New(); + m_RenderWindow->GetRenderer()->SetDataStorage(m_DataStorage); + m_RenderWindow->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard2D); + this->GetVtkRenderWindow()->SetSize( width, height ); + this->SetInputFileNames(argc, argv); +} - //Level Window Property -// ds->GetNode(mitk::NodePredicateDataType::New("Image"))->SetProperty( "levelwindow", mitk::LevelWindowProperty::New( mitk::LevelWindow(254.0, 1.0) ) ); +mitkRenderingTestHelper::~mitkRenderingTestHelper() +{ +} - mitk::RenderingManager::GetInstance()->InitializeViews( geo ); - mitk::RenderingManager::GetInstance()->RequestUpdate(m_RenderWindow->GetVtkRenderWindow()); +void mitkRenderingTestHelper::Render() +{ + //if the datastorage is initialized and at least 1 image is loaded render it + if(m_DataStorage.IsNotNull() || m_DataStorage->GetAll()->Size() >= 1 ) + { + mitk::TimeSlicedGeometry::Pointer geo = m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll()); + + mitk::RenderingManager::GetInstance()->InitializeViews( geo ); + mitk::RenderingManager::GetInstance()->RequestUpdate(m_RenderWindow->GetVtkRenderWindow()); + } + else + { + MITK_ERROR << "No images loaded in data storage!"; + } + + //use this to actually show the iamge in a renderwindow + // this->GetVtkRenderWindow()->Render(); + // this->GetVtkRenderWindow()->GetInteractor()->Start(); +} -//use this to actually show the iamge in a renderwindow -// this->GetVtkRenderWindow()->Render(); -// this->GetVtkRenderWindow()->GetInteractor()->Start(); +void mitkRenderingTestHelper::SetInputFileNames(int argc, char* argv[]) +{ + // parse parameters + for (int i = 1; i < argc; ++i) + { + //add everything to a list but -T and -V + std::string tmp = argv[i]; + if((tmp.compare("-T")) && (tmp.compare("-V"))) + { + this->AddToStorage(tmp); + } + else + { + break; + } + } } -mitkRenderingTestHelper::~mitkRenderingTestHelper() +void mitkRenderingTestHelper::SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection) { + mitk::BaseRenderer::GetInstance(m_RenderWindow->GetVtkRenderWindow())->GetSliceNavigationController()->SetDefaultViewDirection(viewDirection); } vtkRenderer* mitkRenderingTestHelper::GetVtkRenderer() { - return m_RenderWindow->GetRenderer()->GetVtkRenderer(); + return m_RenderWindow->GetRenderer()->GetVtkRenderer(); +} + +void mitkRenderingTestHelper::SetProperty(const char *propertyKey, mitk::BaseProperty* property ) +{ + this->m_DataStorage->GetNode(mitk::NodePredicateDataType::New("Image"))->SetProperty(propertyKey, property); } vtkRenderWindow* mitkRenderingTestHelper::GetVtkRenderWindow() { - return m_RenderWindow->GetVtkRenderWindow(); + return m_RenderWindow->GetVtkRenderWindow(); } //method to save a screenshot of the renderwindow (e.g. create a reference screenshot) void mitkRenderingTestHelper::SaveAsPNG(std::string fileName) { - vtkSmartPointer renderer = this->GetVtkRenderer(); - bool doubleBuffering( renderer->GetRenderWindow()->GetDoubleBuffer() ); - renderer->GetRenderWindow()->DoubleBufferOff(); + vtkSmartPointer renderer = this->GetVtkRenderer(); + bool doubleBuffering( renderer->GetRenderWindow()->GetDoubleBuffer() ); + renderer->GetRenderWindow()->DoubleBufferOff(); + + vtkSmartPointer magnifier = vtkSmartPointer::New(); + magnifier->SetInput(renderer); + magnifier->SetMagnification(1.0); - vtkSmartPointer magnifier = vtkSmartPointer::New(); - magnifier->SetInput(renderer); - magnifier->SetMagnification(1.0); + vtkSmartPointer fileWriter = vtkSmartPointer::New(); + fileWriter->SetInput(magnifier->GetOutput()); + fileWriter->SetFileName(fileName.c_str()); - vtkSmartPointer fileWriter = vtkSmartPointer::New(); - fileWriter->SetInput(magnifier->GetOutput()); - fileWriter->SetFileName(fileName.c_str()); + fileWriter->Write(); + renderer->GetRenderWindow()->SetDoubleBuffer(doubleBuffering); +} - fileWriter->Write(); - renderer->GetRenderWindow()->SetDoubleBuffer(doubleBuffering); +void mitkRenderingTestHelper::AddToStorage(const std::string &filename) +{ + mitk::DataNodeFactory::Pointer reader = mitk::DataNodeFactory::New(); + try + { + reader->SetFileName( filename ); + reader->Update(); + + if(reader->GetNumberOfOutputs()<1) + { + MITK_ERROR << "Could not find test data '" << filename << "'"; + } + + mitk::DataNode::Pointer node = reader->GetOutput( 0 ); + this->m_DataStorage->Add(node); + } + catch ( itk::ExceptionObject & e ) + { + MITK_ERROR << "Failed loading test data '" << filename << "': " << e.what(); + } } diff --git a/Core/Code/Testing/mitkRenderingTestHelper.h b/Core/Code/Testing/mitkRenderingTestHelper.h index f8790f561e..fdac395dd2 100644 --- a/Core/Code/Testing/mitkRenderingTestHelper.h +++ b/Core/Code/Testing/mitkRenderingTestHelper.h @@ -1,50 +1,84 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkRenderingTestHelper_h #define mitkRenderingTestHelper_h -#include #include -#include #include +#include +#include class vtkRenderWindow; class vtkRenderer; -namespace mitk -{ - class DataStorage; -} - class mitkRenderingTestHelper { - public: - mitkRenderingTestHelper(int width, int height, mitk::DataStorage* ds); + +public: + /** @brief Generate a rendering test helper object including a render window of the size width * height (in pixel). + @param argc Number of parameters. (here: Images) "Usage: [filename1 filenam2 -V referenceScreenshot (optional -T /directory/to/save/differenceImage)] + @param argv Given parameters. + **/ + mitkRenderingTestHelper(int width, int height, int argc, char *argv[]); ~mitkRenderingTestHelper(); + /** @brief Getter for the vtkRenderer. + **/ vtkRenderer* GetVtkRenderer(); + + /** @brief Getter for the vtkRenderWindow which should be used to call vtkRegressionTestImage. + **/ vtkRenderWindow* GetVtkRenderWindow(); + + /** @brief Method can be used to save a screenshot (e.g. reference screenshot as a .png file. + @param fileName The filename of the new screenshot (including path). + **/ void SaveAsPNG(std::string fileName); - protected: - mitk::RenderWindow::Pointer m_RenderWindow; + /** @brief This method set the property of the member datastorage + @param property Set a property for each image in the datastorage m_DataStorage. + **/ + void SetProperty(const char *propertyKey, mitk::BaseProperty *property); + + /** @brief Set the view direction of the renderwindow (e.g. sagittal, coronal, transversal) + **/ + void SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection); + + /** @brief Render everything into an mitkRenderWindow. Call SetViewDirection() and SetProperty() before this method. + **/ + void Render(); +protected: + + /** @brief This method tries to load the given file into a member datastorage, in order to render it. + @param fileName The filename of the file to be loaded (including path). + **/ + void AddToStorage(const std::string& filename); + + /** @brief This method tries to parse the given argv for files (e.g. images) and load them into a member datastorage, in order to render it. + @param argc Number of parameters. + @param argv Given parameters. + **/ + void SetInputFileNames(int argc, char *argv[]); + + mitk::RenderWindow::Pointer m_RenderWindow; //<< Contains the mitkRenderWindow into which the test renders the data + mitk::DataStorage::Pointer m_DataStorage; //<< Contains the mitkDataStorage which contains the data to be rendered }; #endif