Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSurfaceVtkMapper3DTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 // MITK
19 #include "mitkTestingMacros.h"
22 #include <mitkSurface.h>
23 
24 // VTK
25 #include <mitkIOUtil.h>
26 #include <vtkCamera.h>
27 #include <vtkFloatArray.h>
28 #include <vtkPointData.h>
29 #include <vtkPolyData.h>
30 #include <vtkRegressionTestImage.h>
31 #include <vtkSphereSource.h>
32 #include <vtkTextureMapToSphere.h>
33 
34 int mitkSurfaceVtkMapper3DTest(int argc, char *argv[])
35 {
36  // load all arguments into a datastorage, take last argument as reference rendering
37  // setup a renderwindow of fixed size X*Y
38  // render the datastorage
39  // compare rendering to reference image
40  MITK_TEST_BEGIN("mitkSurfaceVtkMapper3DTest")
41 
42  mitk::RenderingTestHelper renderingHelper(640, 480, argc, argv);
43 
44  // 3D rendering test, thus 3D mapper ID.
46 
47  vtkSmartPointer<vtkFloatArray> textureCoordinates = vtkSmartPointer<vtkFloatArray>::New();
48  textureCoordinates->SetNumberOfComponents(2);
49  textureCoordinates->SetName("TextureCoordinates");
50 
51  mitk::Image::Pointer textureImage = static_cast<mitk::Image *>(
52  renderingHelper.GetDataStorage()->GetNode(mitk::NodePredicateDataType::New("Image"))->GetData());
53 
54  // generate texture coordinates assuming that surface and texture can be mapped 1 to 1
55  unsigned int *dims = textureImage->GetDimensions();
56  for (unsigned int j = 0; j < dims[1]; ++j)
57  {
58  for (unsigned int i = 0; i < dims[0]; ++i)
59  {
60  int pixelID = i + j * dims[0];
61  float xNorm = (((float)i) / dims[0]);
62  float yNorm = ((float)j) / dims[1];
63  textureCoordinates->InsertTuple2(pixelID, xNorm, yNorm);
64  }
65  }
66 
67  mitk::Surface::Pointer surfaceToPutTextureOn = static_cast<mitk::Surface *>(
68  renderingHelper.GetDataStorage()->GetNode(mitk::NodePredicateDataType::New("Surface"))->GetData());
69 
70  surfaceToPutTextureOn->GetVtkPolyData()->GetPointData()->SetTCoords(textureCoordinates);
72  renderingHelper.GetDataStorage()
73  ->GetNode(mitk::NodePredicateDataType::New("Surface"))
74  ->SetProperty("Surface.Texture", textureProperty);
75 
76  // remove the image from the data storage in order to not disturb the world geometry
77  //(only the surface geometry should be used for rendering)
78  renderingHelper.GetDataStorage()->Remove(
79  renderingHelper.GetDataStorage()->GetNode(mitk::NodePredicateDataType::New("Image")));
80  // Perform reinit, because we removed data.
82  renderingHelper.GetDataStorage()->ComputeBoundingGeometry3D(renderingHelper.GetDataStorage()->GetAll()));
83 
84  // Find a nice camera position to view the surface from the front.
85  // This has to be done after calling renderingHelper.Render(),
86  // because it would overwrite the camera position with global reinit.
87  // It is not necessary, but else the surface is ugly rendered from the side.
88  mitk::Point3D surfaceCenter = surfaceToPutTextureOn->GetGeometry()->GetCenter();
89  vtkCamera *camera3d = renderingHelper.GetVtkRenderer()->GetActiveCamera();
90  // 1m distance to camera should be a nice default value for most cameras
91  camera3d->SetPosition(0, 0, -1000);
92  camera3d->SetViewUp(0, -1, 0);
93  camera3d->SetFocalPoint(0, 0, surfaceCenter[2]);
94  camera3d->SetViewAngle(40);
95  // camera3d->SetClippingRange(1, 10000);
96  renderingHelper.GetVtkRenderer()->ResetCamera();
97 
98  // use this to generate a reference screenshot or save the file:
99  bool generateReferenceScreenshot = false;
100  if (generateReferenceScreenshot)
101  {
102  renderingHelper.SaveReferenceScreenShot("/home/kilgus/Pictures/RenderingTestData/output.png");
103  }
104 
105  //### Usage of CompareRenderWindowAgainstReference: See docu of mitkRrenderingTestHelper
106  MITK_TEST_CONDITION(renderingHelper.CompareRenderWindowAgainstReference(argc, argv) == true,
107  "CompareRenderWindowAgainstReference test result positive?");
108 
109  MITK_TEST_END();
110 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
void SetMapperID(mitk::BaseRenderer::StandardMapperSlot id)
SetMapperID Change between Standard2D and 3D mappers.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
void SaveReferenceScreenShot(std::string fileName)
SaveReferenceScreenShot Convenience method to save a reference screen shot.
bool CompareRenderWindowAgainstReference(int argc, char *argv[], double threshold=10.0)
CompareRenderWindowAgainstReference Convenience method to compare the image rendered in the internal ...
#define MITK_TEST_CONDITION(COND, MSG)
static RenderingManager * GetInstance()
Image class for storing images.
Definition: mitkImage.h:76
mitk::DataStorage::Pointer GetDataStorage()
Returns the datastorage, in order to modify the data inside a rendering test.
static Pointer New(const char *_arg)
vtkRenderer * GetVtkRenderer()
Getter for the vtkRenderer.
int mitkSurfaceVtkMapper3DTest(int argc, char *argv[])
and MITK_TEST_END()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.