diff --git a/Core/Code/TestingHelper/files.cmake b/Core/Code/TestingHelper/files.cmake index 89d9b62a64..bce4101306 100644 --- a/Core/Code/TestingHelper/files.cmake +++ b/Core/Code/TestingHelper/files.cmake @@ -1,10 +1,11 @@ set(H_FILES mitkTestCaller.h mitkTestFixture.h mitkTestingMacros.h ) set(CPP_FILES mitkRenderingTestHelper.cpp + mitkInteractionTestHelper.cpp ) diff --git a/Core/Code/TestingHelper/mitkInteractionTestHelper.cpp b/Core/Code/TestingHelper/mitkInteractionTestHelper.cpp new file mode 100644 index 0000000000..58f6489fe7 --- /dev/null +++ b/Core/Code/TestingHelper/mitkInteractionTestHelper.cpp @@ -0,0 +1,226 @@ +/*=================================================================== + +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. + +===================================================================*/ + +//VTK +#include +#include +#include +#include + +//MITK +#include +#include +#include +#include +#include +#include +#include + +// include gl to read out properties +#include +#include + + +#if defined _MSC_VER +#if _MSC_VER >= 1700 +#define RESIZE_WORKAROUND +#endif +#endif + +#ifdef RESIZE_WORKAROUND +#include "vtkWin32OpenGLRenderWindow.h" +#endif + +//VTK Testing to compare the rendered image pixel-wise against a reference screen shot +#include "vtkTesting.h" + +mitk::InteractionTestHelper::InteractionTestHelper(int width, int height, std::string interactionFilePath) + : m_AutomaticallyCloseRenderWindow(true) +{ + this->Initialize(width, height, interactionFilePath); +} + +void mitk::InteractionTestHelper::Initialize(int width, int height, std::string interactionFilePath) +{ + // Global interaction must(!) be initialized + mitk::GlobalInteraction::GetInstance()->Initialize("global"); + + m_RenderWindow = mitk::RenderWindow::New(); + m_DataStorage = mitk::StandaloneDataStorage::New(); + + m_RenderWindow->GetRenderer()->SetDataStorage(m_DataStorage); + this->SetMapperIDToRender2D(); + this->GetVtkRenderWindow()->SetSize( width, height ); + +#ifdef RESIZE_WORKAROUND + + HWND hWnd = static_cast(this->GetVtkRenderWindow())->GetWindowId(); + + RECT r; + r.left = 10; + r.top = 10; + r.right = r.left + width; + r.bottom = r.top + height; + + LONG style = GetWindowLong(hWnd, GWL_STYLE); + AdjustWindowRect( &r, style, FALSE ); + + MITK_INFO << "WANTED:"; + MITK_INFO << r.right-r.left; + MITK_INFO << r.bottom-r.top; + + RECT rect; +if(GetWindowRect(hWnd, &rect)) +{ + int width = rect.right - rect.left; + int height = rect.bottom - rect.top; + + MITK_INFO << "ACTUAL:"; + MITK_INFO << width; + MITK_INFO <GetRenderer()->Resize( width, height); + + //load interaction pattern + std::ifstream xmlStream(interactionFilePath.c_str()); + mitk::XML2EventParser parser(xmlStream); + m_Events = parser.GetInteractions(); + xmlStream.close(); +} + +mitk::InteractionTestHelper::~InteractionTestHelper() +{ +} + + +void mitk::InteractionTestHelper::SetMapperID( mitk::BaseRenderer::StandardMapperSlot id) +{ + m_RenderWindow->GetRenderer()->SetMapperID(id); +} + + +void mitk::InteractionTestHelper::SetMapperIDToRender3D() +{ + this->SetMapperID(mitk::BaseRenderer::Standard3D); +} + + +void mitk::InteractionTestHelper::SetMapperIDToRender2D() +{ + this->SetMapperID(mitk::BaseRenderer::Standard2D); +} + + +//void mitk::InteractionTestHelper::Render() +//{ +// //if the datastorage is initialized and at least 1 image is loaded render it +// if(m_DataStorage.IsNotNull() || m_DataStorage->GetAll()->Size() >= 1 ) +// { +// //Prepare the VTK camera before rendering. +// m_RenderWindow->GetRenderer()->PrepareRender(); + +// this->GetVtkRenderWindow()->Render(); +// if(m_AutomaticallyCloseRenderWindow == false) +// { +// //Use interaction to stop the test +// this->GetVtkRenderWindow()->GetInteractor()->Start(); +// } +// } +// else +// { +// MITK_ERROR << "No images loaded in data storage!"; +// } +//} + + +mitk::DataStorage::Pointer mitk::InteractionTestHelper::GetDataStorage() +{ + return m_DataStorage; +} + + +void mitk::InteractionTestHelper::SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection) +{ + mitk::BaseRenderer::GetInstance(m_RenderWindow->GetVtkRenderWindow())->GetSliceNavigationController()->SetDefaultViewDirection(viewDirection); + mitk::RenderingManager::GetInstance()->InitializeViews( m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll()) ); +} + + +vtkRenderer* mitk::InteractionTestHelper::GetVtkRenderer() +{ + return m_RenderWindow->GetRenderer()->GetVtkRenderer(); +} + + +vtkRenderWindow* mitk::InteractionTestHelper::GetVtkRenderWindow() +{ + return m_RenderWindow->GetVtkRenderWindow(); +} + + +void mitk::InteractionTestHelper::SetAutomaticallyCloseRenderWindow(bool automaticallyCloseRenderWindow) +{ + m_AutomaticallyCloseRenderWindow = automaticallyCloseRenderWindow; +} + + +void mitk::InteractionTestHelper::AddToStorage(const std::string &filename) +{ + try + { + mitk::DataNode::Pointer node = mitk::IOUtil::LoadDataNode(filename); + this->AddNodeToStorage(node); + } + catch ( itk::ExceptionObject & e ) + { + MITK_ERROR << "Failed loading test data '" << filename << "': " << e.what(); + } +} + +void mitk::InteractionTestHelper::AddNodeToStorage(mitk::DataNode::Pointer node) +{ + this->m_DataStorage->Add(node); + mitk::RenderingManager::GetInstance()->InitializeViews( m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll()) ); +} + + +void mitk::InteractionTestHelper::PlaybackInteraction() +{ + for (int i=0; i < m_Events.size(); ++i) + { + m_Events.at(i)->GetSender()->GetDispatcher()->ProcessEvent(m_Events.at(i)); + } +} diff --git a/Core/Code/TestingHelper/mitkInteractionTestHelper.h b/Core/Code/TestingHelper/mitkInteractionTestHelper.h new file mode 100644 index 0000000000..7234b0d586 --- /dev/null +++ b/Core/Code/TestingHelper/mitkInteractionTestHelper.h @@ -0,0 +1,128 @@ +/*=================================================================== + +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 mitkInteractionTestHelper_h +#define mitkInteractionTestHelper_h + +#include +#include +#include +#include +#include + +#include + +class vtkRenderWindow; +class vtkRenderer; + +namespace mitk +{ + +/** @brief Generate all necessary objects to handle interaction events. + + +*/ +class MITK_TESTINGHELPER_EXPORT InteractionTestHelper +{ + +public: + /** @param interactionFilePath Path to xml file containing interaction events. + **/ + InteractionTestHelper(int width, int height, std::string interactionFilePath); + + /** Default destructor */ + ~InteractionTestHelper(); + + /** @brief Getter for the vtkRenderer. + **/ + vtkRenderer* GetVtkRenderer(); + + /** @brief Getter for the vtkRenderWindow which should be used to call vtkRegressionTestImage. + **/ + vtkRenderWindow* GetVtkRenderWindow(); + + /** + * @brief SetStopRenderWindow Convenience method to make the renderwindow hold after rendering. Usefull for debugging. + * @param flag Flag indicating whether the renderwindow should automatically close (false, default) or stay open (true). Usefull for debugging. + */ + void SetAutomaticallyCloseRenderWindow(bool automaticallyCloseRenderWindow); + + + /** @brief Set the view direction of the renderwindow (e.g. sagittal, coronal, axial) + **/ + void SetViewDirection(mitk::SliceNavigationController::ViewDirection viewDirection); + + +// /** @brief Render everything into an mitkRenderWindow. Call SetViewDirection() and SetProperty() before this method. +// **/ +// void Render(); + + /** @brief Returns the datastorage, in order to modify the data inside a rendering test. + **/ + mitk::DataStorage::Pointer GetDataStorage(); + + /** + * @brief SetMapperID Change between Standard2D and 3D mappers. + * @param id Enum mitk::BaseRenderer::StandardMapperSlot which defines the mapper. + */ + void SetMapperID(mitk::BaseRenderer::StandardMapperSlot id); + + /** + * @brief AddNodeToStorage Add a node to the datastorage and perform a reinit which is necessary for rendering. + * @param node The data you want to add. + */ + void AddNodeToStorage(mitk::DataNode::Pointer node); + + /** + * @brief SetMapperIDToRender3D Convenience method to render in a 3D renderwindow. + * @warning Does not add helper objects like the image planes to render images in 3D. + */ + void SetMapperIDToRender3D(); + + /** + * @brief SetMapperIDToRender2D Convenience method to render in a 2D renderwindow. + */ + void SetMapperIDToRender2D(); + + /** + * @brief PlaybackInteraction plays loaded interaction by passing events to the dispatcher. + */ + void PlaybackInteraction(); + +protected: + /** + * @brief Initialize Internal method to initialize the renderwindow and set the datastorage. + * @param width Height of renderwindow. + * @param height Width of renderwindow. + * @param interactionFilePath path to xml file containing interaction events. + */ + void Initialize(int width, int height, std::string interactionFilePath); + + + /** @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); + + mitk::XML2EventParser::EventContainerType m_Events; //<< List with loaded interaction events + + 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 + bool m_AutomaticallyCloseRenderWindow; //<< Flag indicating whether the renderwindow should automatically close (true, default) or stay open (false). Usefull for debugging. + +}; +}//namespace mitk +#endif