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->renderWindow()->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
99
104
109
111{
112 vtkWidget->renderWindow()->Render();
113}
114
115void VisualizationWidget::showAll(int x, int y, int z)
116{
117 _vtkRender->ResetCamera();
118 vtkCamera* cam = _vtkRender->GetActiveCamera();
119 double* fp = cam->GetFocalPoint();
120 double* p = cam->GetPosition();
121 double dist = std::sqrt(vtkMath::Distance2BetweenPoints(p, fp));
122 cam->SetPosition(fp[0] + (x * dist), fp[1] + (y * dist),
123 fp[2] + (z * dist));
124
125 if (x != 0 || y != 0)
126 {
127 cam->SetViewUp(0.0, 0.0, 1.0);
128 }
129 else
130 {
131 cam->SetViewUp(0.0, 1.0, 0.0);
132 }
133 this->updateView();
134}
135
137{
139 {
140 this->showAll(0, 0, 1);
141 }
142 else
143 {
144 updateView();
145 }
146}
147
149{
150 if (checked)
151 {
152 vtkSmartPointer<vtkInteractorStyleRubberBandZoom> interactorStyle =
153 vtkSmartPointer<vtkInteractorStyleRubberBandZoom>::New();
154 vtkWidget->renderWindow()->GetInteractor()->SetInteractorStyle(
156 QCursor cursor;
157 cursor.setShape(Qt::CrossCursor);
158 vtkWidget->setCursor(cursor);
159 }
160 else
161 {
162 vtkWidget->renderWindow()->GetInteractor()->SetInteractorStyle(
164 QCursor cursor;
165 cursor.setShape(Qt::ArrowCursor);
166 vtkWidget->setCursor(cursor);
167 }
168}
169
174
176 bool checked)
177{
178 _vtkRender->GetActiveCamera()->SetParallelProjection(checked);
179 this->updateView();
180}
181
183{
184 QSettings settings;
185 QString filename = QFileDialog::getSaveFileName(
186 this, tr("Save screenshot"),
187 settings.value("lastScreenshotDir").toString(), "PNG file (*.png)");
188 if (filename.count() > 4)
189 {
190 bool ok;
191 int magnification = QInputDialog::getInt(
192 this, tr("Screenshot magnification"),
193 tr("Enter a magnification factor for the resulting image."), 2, 1,
194 10, 1, &ok);
195 if (ok)
196 {
197 QDir dir(filename);
198 settings.setValue("lastScreenshotDir", dir.absolutePath());
199 this->screenshot(filename, magnification);
200 }
201 }
202}
203
204void VisualizationWidget::screenshot(QString filename, int magnification)
205{
206 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
207 vtkSmartPointer<vtkWindowToImageFilter>::New();
208 windowToImageFilter->SetInput(vtkWidget->renderWindow());
209 // Set the resolution of the output image
210 // magnification times the current resolution of vtk render window
211 windowToImageFilter->SetScale(magnification);
212 // Also record the alpha (transparency) channel
213 windowToImageFilter->SetInputBufferTypeToRGBA();
214 windowToImageFilter->Update();
215
216 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
217 writer->SetFileName(filename.toStdString().c_str());
218 writer->SetInputData(windowToImageFilter->GetOutput());
219 writer->Write();
220
221 this->updateView();
222}
223
224void VisualizationWidget::setCursorShape(Qt::CursorShape shape)
225{
226 this->setCursor(QCursor(shape));
227}
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()