16 #include <vtkPolyData.h> 17 #include <vtkPolyDataMapper2D.h> 18 #include <vtkProperty2D.h> 19 #include <vtkViewport.h> 28 m_PolyData(vtkSmartPointer<vtkPolyData>::New())
30 auto points = vtkSmartPointer<vtkPoints>::New();
31 m_PolyData->SetPoints(points);
33 auto lines = vtkSmartPointer<vtkCellArray>::New();
34 m_PolyData->SetLines(lines);
36 auto mapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
38 auto transformCoordinate = vtkSmartPointer<vtkCoordinate>::New();
39 transformCoordinate->SetCoordinateSystemToDisplay();
40 mapper->SetTransformCoordinate(transformCoordinate);
42 this->CreateRectangle();
44 mapper->SetInputData(m_PolyData);
45 this->SetMapper(mapper);
47 this->GetProperty()->SetLineWidth(2);
56 if (
nullptr == this->Mapper)
58 vtkErrorMacro(<<
"vtkActor2D::Render - No mapper set");
65 if (viewport->GetSize()[0] != m_Width || viewport->GetSize()[1] != m_Height)
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();
74 this->Mapper->RenderOverlay(viewport,
this);
78 void vtkMitkRectangleProp::UpdateRectangle()
80 float offset = this->GetProperty()->GetLineWidth() * 0.5f;
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);
89 void vtkMitkRectangleProp::CreateRectangle()
91 auto points = m_PolyData->GetPoints();
92 auto lines = m_PolyData->GetLines();
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);
99 auto line = vtkSmartPointer<vtkLine>::New();
100 line->GetPointIds()->SetId(0, m_BottomLeft);
101 line->GetPointIds()->SetId(1, m_BottomRight);
102 lines->InsertNextCell(
line);
104 line = vtkSmartPointer<vtkLine>::New();
105 line->GetPointIds()->SetId(0, m_BottomRight);
106 line->GetPointIds()->SetId(1, m_TopRight);
107 lines->InsertNextCell(
line);
109 line = vtkSmartPointer<vtkLine>::New();
110 line->GetPointIds()->SetId(0, m_TopRight);
111 line->GetPointIds()->SetId(1, m_TopLeft);
112 lines->InsertNextCell(
line);
114 line = vtkSmartPointer<vtkLine>::New();
115 line->GetPointIds()->SetId(0, m_TopLeft);
116 line->GetPointIds()->SetId(1, m_BottomLeft);
117 lines->InsertNextCell(
line);
122 this->GetProperty()->SetColor(red, green, blue);
127 this->GetProperty()->SetLineWidth(lineWidth);
void SetColor(float red, float green, float blue)
vtkStandardNewMacro(vtkMitkRectangleProp)
void SetLineWidth(unsigned int lineWidth)
The vtkMitkRectangleProp class renders a rectangle into a renderwindow as a frame.
~vtkMitkRectangleProp() override
int RenderOverlay(vtkViewport *viewport) override