Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "vtkMitkRectangleProp.h"
18 
19 #include <vtkActor2D.h>
20 #include <vtkCellArray.h>
21 #include <vtkLine.h>
22 #include <vtkObjectFactory.h>
23 #include <vtkPoints.h>
24 #include <vtkPolyData.h>
25 #include <vtkPolyDataMapper2D.h>
26 #include <vtkProperty2D.h>
27 #include <vtkSmartPointer.h>
28 #include <vtkViewport.h>
29 
31 
32 vtkMitkRectangleProp::vtkMitkRectangleProp() : m_Height(0), m_Width(0), m_OriginX(0), m_OriginY(0)
33 {
34  vtkSmartPointer<vtkPolyDataMapper2D> mapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
36 
37  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
38  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
39 
40  m_PolyData->SetPoints(points);
41  m_PolyData->SetLines(lines);
42 
43  vtkCoordinate *tcoord = vtkCoordinate::New();
44  tcoord->SetCoordinateSystemToDisplay();
45  mapper->SetTransformCoordinate(tcoord);
46  tcoord->Delete();
47 
49 
50  mapper->SetInputData(m_PolyData);
51  SetMapper(mapper);
52  GetProperty()->SetLineWidth(2);
53 }
54 
56 {
57 }
58 
59 int vtkMitkRectangleProp::RenderOverlay(vtkViewport *viewport)
60 {
61  if (!this->Mapper)
62  {
63  vtkErrorMacro(<< "vtkActor2D::Render - No mapper set");
64  return 0;
65  }
66 
67  if (!GetVisibility())
68  return 0;
69  if (viewport->GetSize()[0] != m_Width || viewport->GetSize()[1] != m_Height)
70  {
71  m_Width = viewport->GetSize()[0];
72  m_Height = viewport->GetSize()[1];
73  m_OriginX = viewport->GetOrigin()[0];
74  m_OriginY = viewport->GetOrigin()[1];
76  }
77  this->Mapper->RenderOverlay(viewport, this);
78 
79  return 1;
80 }
81 
83 {
84  vtkSmartPointer<vtkPoints> points = m_PolyData->GetPoints();
85  float offset = (GetProperty()->GetLineWidth() - 0.5);
86  float wLine = m_OriginX + m_Width - 1;
87  float hLine = m_OriginY + m_Height - 1;
88  float offX = m_OriginX + offset;
89  float offY = m_OriginY + offset;
90 
91  points->SetPoint(m_BottomLeftR, m_OriginX, offY, 0.0);
92  points->SetPoint(m_BottomRightL, m_Width + m_OriginX, offY, 0.0);
93 
94  points->SetPoint(m_BottomLeftU, offX, m_OriginY, 0.0);
95  points->SetPoint(m_TopLeftD, offX, m_OriginY + m_Height, 0.0);
96 
97  points->SetPoint(m_TopLeftR, m_OriginX, hLine, 0.0);
98  points->SetPoint(m_TopRightL, m_Width + m_OriginX, hLine, 0.0);
99 
100  points->SetPoint(m_TopRightD, wLine, m_OriginY + m_Height, 0.0);
101  points->SetPoint(m_BottomRightU, wLine, m_OriginY, 0.0);
102 }
103 
105 {
106  vtkSmartPointer<vtkPoints> points = m_PolyData->GetPoints();
107  vtkSmartPointer<vtkCellArray> lines = m_PolyData->GetLines();
108 
109  // 4 corner points
110  m_BottomLeftR = points->InsertNextPoint(0.0, 0.0, 0.0);
111  m_BottomRightL = points->InsertNextPoint(1.0, 0.0, 0.0);
112 
113  m_BottomLeftU = points->InsertNextPoint(0.0, 0.0, 0.0);
114  m_TopLeftD = points->InsertNextPoint(0.0, 1.0, 0.0);
115 
116  m_TopLeftR = points->InsertNextPoint(0.0, 1.0, 0.0);
117  m_TopRightL = points->InsertNextPoint(1.0, 1.0, 0.0);
118 
119  m_TopRightD = points->InsertNextPoint(1.0, 1.0, 0.0);
120  m_BottomRightU = points->InsertNextPoint(1.0, 0.0, 0.0);
121 
122  vtkSmartPointer<vtkLine> lineVtk;
123  lineVtk = vtkSmartPointer<vtkLine>::New();
124  lineVtk->GetPointIds()->SetId(0, m_BottomLeftR);
125  lineVtk->GetPointIds()->SetId(1, m_BottomRightL);
126  lines->InsertNextCell(lineVtk);
127 
128  lineVtk = vtkSmartPointer<vtkLine>::New();
129  lineVtk->GetPointIds()->SetId(0, m_BottomLeftU);
130  lineVtk->GetPointIds()->SetId(1, m_TopLeftD);
131  lines->InsertNextCell(lineVtk);
132 
133  lineVtk = vtkSmartPointer<vtkLine>::New();
134  lineVtk->GetPointIds()->SetId(0, m_TopLeftR);
135  lineVtk->GetPointIds()->SetId(1, m_TopRightL);
136  lines->InsertNextCell(lineVtk);
137 
138  lineVtk = vtkSmartPointer<vtkLine>::New();
139  lineVtk->GetPointIds()->SetId(0, m_TopRightD);
140  lineVtk->GetPointIds()->SetId(1, m_BottomRightU);
141  lines->InsertNextCell(lineVtk);
142 }
143 
144 void vtkMitkRectangleProp::SetColor(float col1, float col2, float col3)
145 {
146  GetProperty()->SetColor(col1, col2, col3);
147 }
148 
149 void vtkMitkRectangleProp::SetLineWidth(unsigned int lineWidth)
150 {
151  GetProperty()->SetLineWidth(lineWidth);
152 }
void SetColor(float col1, float col2, float col3)
SetColor Set the color of the rectangle.
vtkSmartPointer< vtkPolyData > m_PolyData
m_PolyData holds the rectangle.
vtkStandardNewMacro(vtkMitkRectangleProp)
static Vector3D offset
void SetLineWidth(unsigned int lineWidth)
The vtkMitkRectangleProp2 class Renders a rectangle into a renderwindow as a frame.
int RenderOverlay(vtkViewport *viewport)
void CreateRectangle()
CreateRectangle internal helper to fill a vtkPolydata with a rectangle.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.