OGS
VisualizationWidget.cpp
Go to the documentation of this file.
1 
15 // ** INCLUDES **
16 #include "VisualizationWidget.h"
17 
18 #include <vtkAxesActor.h>
19 #include <vtkCamera.h>
20 #include <vtkCellPicker.h>
21 #include <vtkCommand.h>
22 #include <vtkGenericOpenGLRenderWindow.h>
23 #include <vtkInteractorStyleRubberBandZoom.h>
24 #include <vtkInteractorStyleSwitch.h>
25 #include <vtkMath.h>
26 #include <vtkNew.h>
27 #include <vtkOrientationMarkerWidget.h>
28 #include <vtkPNGWriter.h>
29 #include <vtkRenderWindow.h>
30 #include <vtkRenderWindowInteractor.h>
31 #include <vtkRenderer.h>
32 #include <vtkSmartPointer.h>
33 #include <vtkWindowToImageFilter.h>
34 
35 #include <QCursor>
36 #include <QDir>
37 #include <QFileDialog>
38 #include <QInputDialog>
39 #include <QLineEdit>
40 #include <QSettings>
41 #include <QString>
42 #include <cmath>
43 
44 #include "GeoLib/Point.h"
46 #include "VtkPickCallback.h"
47 
49  : QWidget(parent)
50 {
51  this->setupUi(this);
52 
53  vtkNew<vtkRenderer> ren;
54  _vtkRender = ren;
55 
56  vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
57  vtkWidget->SetRenderWindow(renderWindow);
58  vtkWidget->GetRenderWindow()->AddRenderer(ren);
59 
61  renderWindow->GetInteractor()->SetInteractorStyle(_interactorStyle);
62  _interactorStyle->SetDefaultRenderer(ren);
63 
65  vtkSmartPointer<vtkCellPicker> picker =
66  vtkSmartPointer<vtkCellPicker>::New();
67  picker->AddObserver(vtkCommand::EndPickEvent, _vtkPickCallback);
68  renderWindow->GetInteractor()->SetPicker(picker);
69 
70  QSettings settings;
71 
72  ren->SetBackground(0.0, 0.0, 0.0);
73 
74  // Create an orientation marker using vtkAxesActor
75  vtkSmartPointer<vtkAxesActor> axesActor =
76  vtkSmartPointer<vtkAxesActor>::New();
77  _markerWidget = vtkOrientationMarkerWidget::New();
78  _markerWidget->SetOrientationMarker(axesActor);
79  _markerWidget->SetInteractor(renderWindow->GetInteractor());
80  _markerWidget->EnabledOn();
81  _markerWidget->InteractiveOff();
82 
83  _isShowAllOnLoad = settings.value("resetViewOnLoad", true).toBool();
84 
85  // Set alternate cursor shapes
86  connect(_interactorStyle, SIGNAL(cursorChanged(Qt::CursorShape)), this,
87  SLOT(setCursorShape(Qt::CursorShape)));
88 
89  connect((QObject*)_interactorStyle, SIGNAL(requestViewUpdate()), this,
90  SLOT(updateView()));
91 }
92 
94 {
95  _vtkPickCallback->Delete();
96  _interactorStyle->Delete();
97  _markerWidget->Delete();
98 }
99 
101 {
102  return _interactorStyle;
103 }
104 
106 {
107  return _vtkPickCallback;
108 }
109 
111 {
112  if (vtkWidget->GetRenderWindow()->IsDrawable())
113  {
114  vtkWidget->GetRenderWindow()->Render();
115  }
116 }
117 
118 void VisualizationWidget::showAll(int x, int y, int z)
119 {
120  _vtkRender->ResetCamera();
121  vtkCamera* cam = _vtkRender->GetActiveCamera();
122  double* fp = cam->GetFocalPoint();
123  double* p = cam->GetPosition();
124  double dist = std::sqrt(vtkMath::Distance2BetweenPoints(p, fp));
125  cam->SetPosition(fp[0] + (x * dist), fp[1] + (y * dist),
126  fp[2] + (z * dist));
127 
128  if (x != 0 || y != 0)
129  {
130  cam->SetViewUp(0.0, 0.0, 1.0);
131  }
132  else
133  {
134  cam->SetViewUp(0.0, 1.0, 0.0);
135  }
136  this->updateView();
137 }
138 
140 {
141  if (_isShowAllOnLoad)
142  {
143  this->showAll(0, 0, 1);
144  }
145  else
146  {
147  updateView();
148  }
149 }
150 
152 {
153  if (checked)
154  {
155  vtkSmartPointer<vtkInteractorStyleRubberBandZoom> interactorStyle =
156  vtkSmartPointer<vtkInteractorStyleRubberBandZoom>::New();
157  vtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(
159  QCursor cursor;
160  cursor.setShape(Qt::CrossCursor);
161  vtkWidget->setCursor(cursor);
162  }
163  else
164  {
165  vtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(
167  QCursor cursor;
168  cursor.setShape(Qt::ArrowCursor);
169  vtkWidget->setCursor(cursor);
170  }
171 }
172 
174 {
176 }
177 
179  bool checked)
180 {
181  _vtkRender->GetActiveCamera()->SetParallelProjection(checked);
182  this->updateView();
183 }
184 
186 {
187  QSettings settings;
188  QString filename = QFileDialog::getSaveFileName(
189  this, tr("Save screenshot"),
190  settings.value("lastScreenshotDir").toString(), "PNG file (*.png)");
191  if (filename.count() > 4)
192  {
193  bool ok;
194  int magnification = QInputDialog::getInt(
195  this, tr("Screenshot magnification"),
196  tr("Enter a magnification factor for the resulting image."), 2, 1,
197  10, 1, &ok);
198  if (ok)
199  {
200  QDir dir(filename);
201  settings.setValue("lastScreenshotDir", dir.absolutePath());
202  this->screenshot(filename, magnification);
203  }
204  }
205 }
206 
207 void VisualizationWidget::screenshot(QString filename, int magnification)
208 {
209  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
210  vtkSmartPointer<vtkWindowToImageFilter>::New();
211  windowToImageFilter->SetInput(vtkWidget->GetRenderWindow());
212  // Set the resolution of the output image
213  // magnification times the current resolution of vtk render window
214  windowToImageFilter->SetScale(magnification);
215  // Also record the alpha (transparency) channel
216  windowToImageFilter->SetInputBufferTypeToRGBA();
217  windowToImageFilter->Update();
218 
219  vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
220  writer->SetFileName(filename.toStdString().c_str());
221  writer->SetInputData(windowToImageFilter->GetOutput());
222  writer->Write();
223 
224  this->updateView();
225 }
226 
227 void VisualizationWidget::setCursorShape(Qt::CursorShape shape)
228 {
229  this->setCursor(QCursor(shape));
230 }
Definition of the Point class.
Definition of the VisualizationWidget class.
Definition of the VtkCustomInteractorStyle class.
Definition of the VtkPickCallback class.
void on_highlightToolButton_toggled(bool checked)
Toggles the display of bounding boxes around.
VtkCustomInteractorStyle * _interactorStyle
void updateViewOnLoad()
Updates the view only or additionally shows the entire scene.
~VisualizationWidget() override
Destructor.
vtkOrientationMarkerWidget * _markerWidget
void updateView()
Updates the the 3d view.
void on_screenshotPushButton_pressed()
Saves a screenshot.
void on_zoomToolButton_toggled(bool checked)
Toggles rectangular zooming mode.
VtkCustomInteractorStyle * interactorStyle() const
Returns the VtkCustomInteractorStyle.
VtkPickCallback * _vtkPickCallback
VtkPickCallback * vtkPickCallback() const
Returns the VtkPickCallback.
void showAll(int x, int y, int z)
Shows the entire scene on the views. x,y,z are in {-1, 0, 1} and specify from which direction the sce...
void setCursorShape(Qt::CursorShape shape)
Sets the widgets cursor shape.
VisualizationWidget(QWidget *parent=nullptr)
Constructor.
void screenshot(QString filename, int magnification)
Saves a magnified image of the current render window to a file.
void on_orthogonalProjectionToolButton_toggled(bool checked)
Toggles the orthogonal projection.
static VtkCustomInteractorStyle * New()
static VtkPickCallback * New()
static const double p