Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
vtkMitkRectangleProp.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "vtkMitkRectangleProp.h"
14 
15 #include <vtkLine.h>
16 #include <vtkPolyData.h>
17 #include <vtkPolyDataMapper2D.h>
18 #include <vtkProperty2D.h>
19 #include <vtkViewport.h>
20 
22 
24  : m_Height(0),
25  m_Width(0),
26  m_OriginX(0),
27  m_OriginY(0),
28  m_PolyData(vtkSmartPointer<vtkPolyData>::New())
29 {
30  auto points = vtkSmartPointer<vtkPoints>::New();
31  m_PolyData->SetPoints(points);
32 
33  auto lines = vtkSmartPointer<vtkCellArray>::New();
34  m_PolyData->SetLines(lines);
35 
36  auto mapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
37 
38  auto transformCoordinate = vtkSmartPointer<vtkCoordinate>::New();
39  transformCoordinate->SetCoordinateSystemToDisplay();
40  mapper->SetTransformCoordinate(transformCoordinate);
41 
42  this->CreateRectangle();
43 
44  mapper->SetInputData(m_PolyData);
45  this->SetMapper(mapper);
46 
47  this->GetProperty()->SetLineWidth(2);
48 }
49 
51 {
52 }
53 
54 int vtkMitkRectangleProp::RenderOverlay(vtkViewport *viewport)
55 {
56  if (nullptr == this->Mapper)
57  {
58  vtkErrorMacro(<< "vtkActor2D::Render - No mapper set");
59  return 0;
60  }
61 
62  if (!GetVisibility())
63  return 0;
64 
65  if (viewport->GetSize()[0] != m_Width || viewport->GetSize()[1] != m_Height)
66  {
67  m_Width = viewport->GetSize()[0];
68  m_Height = viewport->GetSize()[1];
69  m_OriginX = viewport->GetOrigin()[0];
70  m_OriginY = viewport->GetOrigin()[1];
71  this->UpdateRectangle();
72  }
73 
74  this->Mapper->RenderOverlay(viewport, this);
75  return 1;
76 }
77 
78 void vtkMitkRectangleProp::UpdateRectangle()
79 {
80  float offset = this->GetProperty()->GetLineWidth() * 0.5f;
81 
82  auto points = m_PolyData->GetPoints();
83  points->SetPoint(m_BottomLeft, m_OriginX + offset, m_OriginY + offset, 0.0f);
84  points->SetPoint(m_BottomRight, m_OriginX + m_Width - offset, m_OriginY + offset, 0.0f);
85  points->SetPoint(m_TopRight, m_OriginX + m_Width - offset, m_OriginY + m_Height - offset, 0.0f);
86  points->SetPoint(m_TopLeft, m_OriginX + offset, m_OriginY + m_Height - offset, 0.0f);
87 }
88 
89 void vtkMitkRectangleProp::CreateRectangle()
90 {
91  auto points = m_PolyData->GetPoints();
92  auto lines = m_PolyData->GetLines();
93 
94  m_BottomLeft = points->InsertNextPoint(0.0f, 0.0f, 0.0f);
95  m_BottomRight = points->InsertNextPoint(1.0f, 0.0f, 0.0f);
96  m_TopRight = points->InsertNextPoint(1.0f, 1.0f, 0.0f);
97  m_TopLeft = points->InsertNextPoint(0.0f, 1.0f, 0.0f);
98 
99  auto line = vtkSmartPointer<vtkLine>::New();
100  line->GetPointIds()->SetId(0, m_BottomLeft);
101  line->GetPointIds()->SetId(1, m_BottomRight);
102  lines->InsertNextCell(line);
103 
104  line = vtkSmartPointer<vtkLine>::New();
105  line->GetPointIds()->SetId(0, m_BottomRight);
106  line->GetPointIds()->SetId(1, m_TopRight);
107  lines->InsertNextCell(line);
108 
109  line = vtkSmartPointer<vtkLine>::New();
110  line->GetPointIds()->SetId(0, m_TopRight);
111  line->GetPointIds()->SetId(1, m_TopLeft);
112  lines->InsertNextCell(line);
113 
114  line = vtkSmartPointer<vtkLine>::New();
115  line->GetPointIds()->SetId(0, m_TopLeft);
116  line->GetPointIds()->SetId(1, m_BottomLeft);
117  lines->InsertNextCell(line);
118 }
119 
120 void vtkMitkRectangleProp::SetColor(float red, float green, float blue)
121 {
122  this->GetProperty()->SetColor(red, green, blue);
123 }
124 
125 void vtkMitkRectangleProp::SetLineWidth(unsigned int lineWidth)
126 {
127  this->GetProperty()->SetLineWidth(lineWidth);
128 }
static char * line
Definition: svm.cpp:2870
void SetColor(float red, float green, float blue)
vtkStandardNewMacro(vtkMitkRectangleProp)
static Vector3D offset
void SetLineWidth(unsigned int lineWidth)
The vtkMitkRectangleProp class renders a rectangle into a renderwindow as a frame.
int RenderOverlay(vtkViewport *viewport) override