OGS
VisualizationWidget.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ** INCLUDES **
6
7#include <vtkAxesActor.h>
8#include <vtkCamera.h>
9#include <vtkCellPicker.h>
10#include <vtkCommand.h>
11#include <vtkGenericOpenGLRenderWindow.h>
12#include <vtkInteractorStyleRubberBandZoom.h>
13#include <vtkInteractorStyleSwitch.h>
14#include <vtkMath.h>
15#include <vtkNew.h>
16#include <vtkOrientationMarkerWidget.h>
17#include <vtkPNGWriter.h>
18#include <vtkRenderWindow.h>
19#include <vtkRenderWindowInteractor.h>
20#include <vtkRenderer.h>
21#include <vtkSmartPointer.h>
22#include <vtkWindowToImageFilter.h>
23
24#include <QCursor>
25#include <QDir>
26#include <QFileDialog>
27#include <QInputDialog>
28#include <QLineEdit>
29#include <QSettings>
30#include <QString>
31#include <cmath>
32
33#include "GeoLib/Point.h"
35#include "VtkPickCallback.h"
36
38 : QWidget(parent)
39{
40 this->setupUi(this);
41
42 vtkNew<vtkRenderer> ren;
43 _vtkRender = ren;
44
45 vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
46 vtkWidget->setRenderWindow(renderWindow);
47 vtkWidget->renderWindow()->AddRenderer(ren);
48
50 renderWindow->GetInteractor()->SetInteractorStyle(_interactorStyle);
51 _interactorStyle->SetDefaultRenderer(ren);
52
54 vtkSmartPointer<vtkCellPicker> picker =
55 vtkSmartPointer<vtkCellPicker>::New();
56 picker->AddObserver(vtkCommand::EndPickEvent, _vtkPickCallback);
57 renderWindow->GetInteractor()->SetPicker(picker);
58
59 QSettings settings;
60
61 ren->SetBackground(0.0, 0.0, 0.0);
62
63 // Create an orientation marker using vtkAxesActor
64 vtkSmartPointer<vtkAxesActor> axesActor =
65 vtkSmartPointer<vtkAxesActor>::New();
66 _markerWidget = vtkOrientationMarkerWidget::New();
67 _markerWidget->SetOrientationMarker(axesActor);
68 _markerWidget->SetInteractor(renderWindow->GetInteractor());
69 _markerWidget->EnabledOn();
70 _markerWidget->InteractiveOff();
71
72 _isShowAllOnLoad = settings.value("resetViewOnLoad", true).toBool();
73
74 // Set alternate cursor shapes
75 connect(_interactorStyle, SIGNAL(cursorChanged(Qt::CursorShape)), this,
76 SLOT(setCursorShape(Qt::CursorShape)));
77
78 connect((QObject*)_interactorStyle, SIGNAL(requestViewUpdate()), this,
79 SLOT(updateView()));
80}
81
88
93
98
100{
101 vtkWidget->renderWindow()->Render();
102}
103
104void VisualizationWidget::showAll(int x, int y, int z)
105{
106 _vtkRender->ResetCamera();
107 vtkCamera* cam = _vtkRender->GetActiveCamera();
108 double* fp = cam->GetFocalPoint();
109 double* p = cam->GetPosition();
110 double dist = std::sqrt(vtkMath::Distance2BetweenPoints(p, fp));
111 cam->SetPosition(fp[0] + (x * dist), fp[1] + (y * dist),
112 fp[2] + (z * dist));
113
114 if (x != 0 || y != 0)
115 {
116 cam->SetViewUp(0.0, 0.0, 1.0);
117 }
118 else
119 {
120 cam->SetViewUp(0.0, 1.0, 0.0);
121 }
122 this->updateView();
123}
124
126{
128 {
129 this->showAll(0, 0, 1);
130 }
131 else
132 {
133 updateView();
134 }
135}
136
138{
139 if (checked)
140 {
141 vtkSmartPointer<vtkInteractorStyleRubberBandZoom> interactorStyle =
142 vtkSmartPointer<vtkInteractorStyleRubberBandZoom>::New();
143 vtkWidget->renderWindow()->GetInteractor()->SetInteractorStyle(
145 QCursor cursor;
146 cursor.setShape(Qt::CrossCursor);
147 vtkWidget->setCursor(cursor);
148 }
149 else
150 {
151 vtkWidget->renderWindow()->GetInteractor()->SetInteractorStyle(
153 QCursor cursor;
154 cursor.setShape(Qt::ArrowCursor);
155 vtkWidget->setCursor(cursor);
156 }
157}
158
160{
161 _interactorStyle->setHighlightActor(checked);
162}
163
165 bool checked)
166{
167 _vtkRender->GetActiveCamera()->SetParallelProjection(checked);
168 this->updateView();
169}
170
172{
173 QSettings settings;
174 QString filename = QFileDialog::getSaveFileName(
175 this, tr("Save screenshot"),
176 settings.value("lastScreenshotDir").toString(), "PNG file (*.png)");
177 if (filename.count() > 4)
178 {
179 bool ok;
180 int magnification = QInputDialog::getInt(
181 this, tr("Screenshot magnification"),
182 tr("Enter a magnification factor for the resulting image."), 2, 1,
183 10, 1, &ok);
184 if (ok)
185 {
186 QDir dir(filename);
187 settings.setValue("lastScreenshotDir", dir.absolutePath());
188 this->screenshot(filename, magnification);
189 }
190 }
191}
192
193void VisualizationWidget::screenshot(QString filename, int magnification)
194{
195 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
196 vtkSmartPointer<vtkWindowToImageFilter>::New();
197 windowToImageFilter->SetInput(vtkWidget->renderWindow());
198 // Set the resolution of the output image
199 // magnification times the current resolution of vtk render window
200 windowToImageFilter->SetScale(magnification);
201 // Also record the alpha (transparency) channel
202 windowToImageFilter->SetInputBufferTypeToRGBA();
203 windowToImageFilter->Update();
204
205 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
206 writer->SetFileName(filename.toStdString().c_str());
207 writer->SetInputData(windowToImageFilter->GetOutput());
208 writer->Write();
209
210 this->updateView();
211}
212
213void VisualizationWidget::setCursorShape(Qt::CursorShape shape)
214{
215 this->setCursor(QCursor(shape));
216}
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()