Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkManufacturerLogo.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 #include "mitkManufacturerLogo.h"
18 
19 #include "mitkVtkLayerController.h"
20 
21 #include <itkMacro.h>
22 #include <itkObject.h>
23 #include <itksys/SystemTools.hxx>
24 #include <mitkConfig.h>
26 
27 #include <vtkCamera.h>
28 #include <vtkConfigure.h>
29 #include <vtkImageActor.h>
30 #include <vtkImageData.h>
31 #include <vtkImageFlip.h>
32 #include <vtkImageImport.h>
33 #include <vtkImageMapper.h>
34 #include <vtkMapper.h>
35 #include <vtkObjectFactory.h>
36 #include <vtkPNGReader.h>
37 #include <vtkPolyData.h>
38 #include <vtkRenderWindow.h>
39 #include <vtkRenderer.h>
40 #include <vtkRendererCollection.h>
41 
42 #include <mbilogo.h>
43 
44 #include <algorithm>
45 
47 {
48  m_RenderWindow = nullptr;
54 
56 
57  m_IsEnabled = false;
59 
60  m_ZoomFactor = 1.15;
61  m_Opacity = 0.5;
62 
63  m_FileName = "";
64  m_PngReader->SetFileName(m_FileName.c_str());
65 }
66 
68 {
69  if (m_RenderWindow != nullptr)
70  if (this->IsEnabled())
71  this->Disable();
72 
73  if (m_Mapper != nullptr)
74  m_Mapper->Delete();
75 
76  if (m_Actor != nullptr)
77  m_Actor->Delete();
78 
79  if (m_Renderer != nullptr)
80  m_Renderer->Delete();
81 
82  if (m_PngReader != nullptr)
83  m_PngReader->Delete();
84 
85  if (m_VtkImageImport != nullptr)
86  m_VtkImageImport->Delete();
87 
88  if (m_ImageData != nullptr)
89  delete[] m_ImageData;
90 }
91 
97 void mitk::ManufacturerLogo::SetRenderWindow(vtkRenderWindow *renderWindow)
98 {
99  m_RenderWindow = renderWindow;
100 }
101 
107 {
108  return m_RenderWindow;
109 }
110 
117 {
118  return m_Renderer;
119 }
120 
125 {
126  return m_Actor;
127 }
128 
134 {
135  return m_Mapper;
136 }
137 
139 {
140  std::string file = filename;
141  if (file.length() != 0)
142  {
143  m_FileName = filename;
144  m_PngReader->SetFileName(m_FileName.c_str());
145  }
146 }
147 
153 {
154  if (m_IsEnabled)
155  return;
156 
157  if (m_RenderWindow != nullptr)
158  {
159  if (itksys::SystemTools::FileExists(m_FileName.c_str()) && !m_ForceShowMBIDepartmentLogo)
160  {
161  m_PngReader->Update();
162  m_Actor->SetInputData(m_PngReader->GetOutput());
163  }
164  else // either logo file not found or logo renderer is forced to show the MBI logo
165  {
166  m_VtkImageImport->SetDataScalarTypeToUnsignedChar();
167  m_VtkImageImport->SetNumberOfScalarComponents(mbiLogo_NumberOfScalars);
168  m_VtkImageImport->SetWholeExtent(0, mbiLogo_Width - 1, 0, mbiLogo_Height - 1, 0, 1 - 1);
169  m_VtkImageImport->SetDataExtentToWholeExtent();
170 
171  // flip mbi logo around y axis and change color order
172  m_ImageData = new char[mbiLogo_Height * mbiLogo_Width * mbiLogo_NumberOfScalars];
173 
174  unsigned int column, row;
175  char *dest = m_ImageData;
176  char *source = (char *)&mbiLogo_Data[0];
177  ;
178  char r, g, b, a;
179  for (column = 0; column < mbiLogo_Height; column++)
180  for (row = 0; row < mbiLogo_Width; row++)
181  { // change r with b
182  b = *source++;
183  g = *source++;
184  r = *source++;
185  a = *source++;
186 
187  *dest++ = r;
188  *dest++ = g;
189  *dest++ = b;
190  *dest++ = a;
191  }
192 
193  m_VtkImageImport->SetImportVoidPointer(m_ImageData);
194  m_VtkImageImport->Modified();
195  m_VtkImageImport->Update();
196 
197  m_Actor->SetInputData(m_VtkImageImport->GetOutput());
198  }
199 
200  m_Actor->SetOpacity(m_Opacity);
201 
202  m_Renderer->AddActor(m_Actor);
203  m_Renderer->InteractiveOff();
204 
205  SetupCamera();
206  SetupPosition();
207 
208  mitk::VtkLayerController::GetInstance(m_RenderWindow)->InsertForegroundRenderer(m_Renderer, false);
209 
210  m_IsEnabled = true;
211  }
212 }
213 
215 {
216  // set the vtk camera in way that stretches the logo all over the renderwindow
217 
218  vtkImageData *image = m_Actor->GetInput();
219  m_Camera = m_Renderer->GetActiveCamera();
220  m_Camera->SetClippingRange(1, 100000);
221 
222  if (!image)
223  return;
224 
225  double spacing[3];
226  double origin[3];
227  int dimensions[3];
228 
229  image->GetSpacing(spacing);
230  image->GetOrigin(origin);
231  image->GetDimensions(dimensions);
232 
233  double focalPoint[3];
234  double position[3];
235 
236  for (unsigned int cc = 0; cc < 3; cc++)
237  {
238  focalPoint[cc] = origin[cc] + (spacing[cc] * dimensions[cc]) / 2.0;
239  position[cc] = focalPoint[cc];
240  }
241 
242  m_Camera->SetViewUp(0, 1, 0);
243  int idx = 2;
244  const double distanceToFocalPoint = 1000;
245  position[idx] = distanceToFocalPoint;
246 
247  m_Camera->ParallelProjectionOn();
248  m_Camera->SetPosition(position);
249  m_Camera->SetFocalPoint(focalPoint);
250 
251  int d1 = (idx + 1) % 3;
252  int d2 = (idx + 2) % 3;
253 
254  double max = std::max(dimensions[d1], dimensions[d2]);
255 
256  m_Camera->SetParallelScale(max / 2);
257 }
258 
260 { // Position and Scale of the logo
261 
262  double newPos[4];
263  int dimensions[3];
264  vtkImageData *image = m_Actor->GetInput();
265  image->GetDimensions(dimensions);
266  // normalize image dimensions
267  double max = std::max(dimensions[0], dimensions[1]);
268  double normX = dimensions[0] / max;
269  double normY = dimensions[1] / max;
270 
271  double buffer = 0; // buffer to the boarder of the renderwindow
272 
273  switch (m_LogoPosition)
274  {
276  {
277  newPos[0] = (0 + buffer);
278  newPos[1] = (0 + buffer);
279  newPos[2] = 0.2 * normX * m_ZoomFactor;
280  newPos[3] = 0.2 * normY * m_ZoomFactor;
281  break;
282  }
284  {
285  newPos[0] = (1 - buffer) - 0.2 * normX * m_ZoomFactor;
286  newPos[1] = 0.0;
287  newPos[2] = (1 - buffer);
288  newPos[3] = 0.2 * normY * m_ZoomFactor;
289  break;
290  }
292  {
293  newPos[0] = (0 + buffer);
294  newPos[1] = (1 - buffer) - 0.2 * normY * m_ZoomFactor;
295  newPos[2] = 0.2 * normX * m_ZoomFactor;
296  newPos[3] = (1 - buffer);
297  break;
298  }
300  {
301  newPos[0] = (1 - buffer) - 0.2 * normX * m_ZoomFactor;
302  newPos[1] = (1 - buffer) - 0.2 * normY * m_ZoomFactor;
303  newPos[2] = (1 - buffer);
304  newPos[3] = (1 - buffer);
305  break;
306  }
308  default:
309  {
310  newPos[0] = 0.5 - 0.1 * normX * m_ZoomFactor;
311  newPos[1] = 0.5 - 0.1 * normY * m_ZoomFactor;
312  newPos[2] = 0.5 + 0.1 * normX * m_ZoomFactor;
313  newPos[3] = 0.5 + 0.1 * normY * m_ZoomFactor;
314  break;
315  }
316  }
317 
318  m_Renderer->SetViewport(newPos);
319 }
320 
322 {
323  m_ForceShowMBIDepartmentLogo = visible;
324 }
325 
327 {
328  m_ZoomFactor = factor;
329 }
331 {
332  m_Opacity = opacity;
333 }
334 
340 {
341  if (this->IsEnabled() && !m_ForceShowMBIDepartmentLogo)
342  {
343  mitk::VtkLayerController::GetInstance(m_RenderWindow)->RemoveRenderer(m_Renderer);
344  m_IsEnabled = false;
345  }
346 }
347 
353 {
354  return m_IsEnabled;
355 }
356 
358 {
359  // nothing to do
360 }
361 
363 {
364  return false;
365 }
366 
368 {
369  return true;
370 }
371 
372 void mitk::ManufacturerLogo::SetRequestedRegion(const itk::DataObject *)
373 {
374  // nothing to do
375 }
virtual bool RequestedRegionIsOutsideOfTheBufferedRegion() override
virtual vtkImageActor * GetActor()
void InsertForegroundRenderer(vtkSmartPointer< vtkRenderer > renderer, bool forceAbsoluteForeground)
static const unsigned int mbiLogo_Data[]
Definition: mbilogo.h:32
virtual vtkRenderWindow * GetRenderWindow()
virtual void SetZoomFactor(double factor)
virtual vtkImageMapper * GetMapper()
virtual void SetRequestedRegion(const itk::DataObject *) override
virtual void SetRequestedRegionToLargestPossibleRegion() override
virtual void ForceMBILogoVisible(bool visible)
static const unsigned int mbiLogo_Height
Definition: mbilogo.h:31
virtual void SetLogoSource(const char *filename)
static VtkLayerController * GetInstance(vtkSmartPointer< vtkRenderWindow > renWin)
static const unsigned int mbiLogo_Width
Definition: mbilogo.h:30
virtual void SetRenderWindow(vtkRenderWindow *renderWindow)
static const std::string filename
static T max(T x, T y)
Definition: svm.cpp:70
vtkImageImport * m_VtkImageImport
void RemoveRenderer(vtkSmartPointer< vtkRenderer > renderer)
virtual void SetOpacity(double opacity)
vtkRenderWindow * m_RenderWindow
virtual bool VerifyRequestedRegion() override
virtual vtkRenderer * GetVtkRenderer()
static const unsigned int mbiLogo_NumberOfScalars
Definition: mbilogo.h:29
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.