OGS
MeshView.cpp
Go to the documentation of this file.
1
15#include "MeshView.h"
16
17#include <QContextMenuEvent>
18#include <QFileDialog>
19#include <QHeaderView>
20#include <QMenu>
21#include <QObject>
22#include <QSettings>
23#include <memory>
24
30#include "Base/OGSError.h"
32#include "MeshItem.h"
33#include "MeshLayerEditDialog.h"
34#include "MeshLib/Mesh.h"
35#include "MeshLib/Node.h"
36#include "MeshMapping2DDialog.h"
37#include "MeshModel.h"
41#include "MeshValueEditDialog.h"
43#include "SaveMeshDialog.h"
45
46MeshView::MeshView(QWidget* parent /*= 0*/) : QTreeView(parent)
47{
48 setUniformRowHeights(true);
49 // resizeColumnsToContents();
50 // resizeRowsToContents();
51}
52
53MeshView::~MeshView() = default;
54
56{
57 setAlternatingRowColors(true);
58 setColumnWidth(0, 150);
59 std::size_t nColumns =
60 (this->model() != nullptr) ? this->model()->columnCount() : 0;
61 for (std::size_t i = 1; i < nColumns; i++)
62 {
63 resizeColumnToContents(i);
64 }
65}
66
67void MeshView::selectionChanged(const QItemSelection& selected,
68 const QItemSelection& deselected)
69{
70 Q_UNUSED(deselected);
71 if (!selected.isEmpty())
72 {
74 const QModelIndex idx = *(selected.indexes().begin());
75 const TreeItem* tree_item =
76 static_cast<TreeModel*>(this->model())->getItem(idx);
77
78 const auto* list_item = dynamic_cast<const MeshItem*>(tree_item);
79 if (list_item)
80 {
81 emit enableSaveButton(true);
82 emit enableRemoveButton(true);
83 emit meshSelected(*list_item->getMesh());
84 }
85 else
86 {
87 emit enableSaveButton(false);
88 emit enableRemoveButton(false);
89 emit elementSelected(
90 dynamic_cast<const MeshItem*>(tree_item->parentItem())
91 ->vtkSource(),
92 static_cast<unsigned>(tree_item->row()), true);
93 }
94 }
95}
96
101
103{
104 QModelIndex index(this->selectionModel()->currentIndex());
105 if (!index.isValid())
106 {
107 OGSError::box("No mesh selected.");
108 }
109 else
110 {
111 emit requestMeshRemoval(index);
112 emit enableSaveButton(false);
113 emit enableRemoveButton(false);
114 }
115}
116
117void MeshView::contextMenuEvent(QContextMenuEvent* event)
118{
119 QModelIndex const& index = this->selectionModel()->currentIndex();
120 MeshItem const* const item = dynamic_cast<MeshItem*>(
121 static_cast<TreeItem*>(index.internalPointer()));
122
123 if (item == nullptr)
124 {
125 return;
126 }
127
128 unsigned const mesh_dim(item->getMesh()->getDimension());
129
130 std::vector<MeshAction> actions;
131 actions.push_back({new QAction("Map mesh...", this), 1, 2});
132 connect(actions.back().action, SIGNAL(triggered()), this,
133 SLOT(openMap2dMeshDialog()));
134 actions.push_back(
135 {new QAction("Assign raster data to mesh...", this), 1, 2});
136 connect(actions.back().action, SIGNAL(triggered()), this,
138 actions.push_back({new QAction("Extend mesh to 3D...", this), 2, 3});
139 connect(actions.back().action, SIGNAL(triggered()), this,
140 SLOT(openMeshEditDialog()));
141 actions.push_back({new QAction("Add layer...", this), 1, 3});
142 connect(actions.back().action, SIGNAL(triggered()), this,
143 SLOT(openAddLayerDialog()));
144 actions.push_back({new QAction("Edit material groups...", this), 1, 3});
145 connect(actions.back().action, SIGNAL(triggered()), this,
146 SLOT(openValuesEditDialog()));
147 actions.push_back({new QAction("Extract surface...", this), 3, 3});
148 connect(actions.back().action, SIGNAL(triggered()), this,
149 SLOT(extractSurfaceMesh()));
150 actions.push_back(
151 {new QAction("Calculate element quality...", this), 2, 3});
152 connect(actions.back().action, SIGNAL(triggered()), this,
153 SLOT(checkMeshQuality()));
154 actions.push_back({new QAction("Convert to geometry", this), 1, 2});
155 connect(actions.back().action, SIGNAL(triggered()), this,
156 SLOT(convertMeshToGeometry()));
157 actions.push_back({new QAction("Export to Shapefile...", this), 2, 2});
158 connect(actions.back().action, SIGNAL(triggered()), this,
159 SLOT(exportToShapefile()));
160 actions.push_back({new QAction("Export to TetGen...", this), 3, 3});
161 connect(actions.back().action, SIGNAL(triggered()), this,
162 SLOT(exportToTetGen()));
163
164 QMenu menu(this);
165 for (MeshAction a : actions)
166 {
167 if (mesh_dim >= a.min_dim && mesh_dim <= a.max_dim)
168 {
169 menu.addAction(a.action);
170 }
171 }
172 menu.exec(event->globalPos());
173}
174
176{
177 MeshModel const* const model = static_cast<MeshModel*>(this->model());
178 QModelIndex const index = this->selectionModel()->currentIndex();
179 MeshLib::Mesh const* const mesh = model->getMesh(index);
180 if (mesh == nullptr)
181 {
182 return;
183 }
184
186 if (dlg.exec() != QDialog::Accepted)
187 {
188 return;
189 }
190
191 auto result = std::make_unique<MeshLib::Mesh>(*mesh);
192 result->setName(dlg.getNewMeshName());
193 if (dlg.useRasterMapping())
194 {
195 std::unique_ptr<GeoLib::Raster> raster{
197 if (!raster)
198 {
199 OGSError::box(QString::fromStdString(
200 "Error mapping mesh. Could not read raster file " +
201 dlg.getRasterPath()));
202 return;
203 }
205 *result, *raster, dlg.getNoDataReplacement(),
206 dlg.getIgnoreNoData()))
207 {
208 OGSError::box("Error mapping mesh.");
209 return;
210 }
211 }
212 else
213 {
215 dlg.getStaticValue());
216 }
217 static_cast<MeshModel*>(this->model())->addMesh(std::move(result));
218}
219
221{
222 MeshModel const* const model = static_cast<MeshModel*>(this->model());
223 QModelIndex const index = this->selectionModel()->currentIndex();
224 MeshLib::Mesh const* mesh = model->getMesh(index);
225 if (mesh == nullptr)
226 {
227 return;
228 }
229
230 RasterDataToMeshDialog dlg(mesh->getName());
231 if (dlg.exec() != QDialog::Accepted)
232 {
233 return;
234 }
235
236 auto result = std::make_unique<MeshLib::Mesh>(*mesh);
237 result->setName(dlg.getMeshName());
238 std::unique_ptr<GeoLib::Raster> raster{
240 if (!raster)
241 {
242 OGSError::box(QString::fromStdString("Could not read raster file " +
243 dlg.getRasterPath()));
244 return;
245 }
246 if (dlg.createNodeArray())
247 {
249 *result, *raster, dlg.getNoDataReplacement(), dlg.getArrayName());
250 }
251 else
252 {
254 *result, *raster, dlg.getNoDataReplacement(), dlg.getArrayName());
255 }
256 static_cast<MeshModel*>(this->model())->addMesh(std::move(result));
257}
258
260{
261 MeshModel const* const model = static_cast<MeshModel*>(this->model());
262 QModelIndex const index = this->selectionModel()->currentIndex();
263 MeshLib::Mesh const* const mesh = model->getMesh(index);
264
265 MeshLayerEditDialog meshLayerEdit(mesh);
266 connect(&meshLayerEdit, SIGNAL(mshEditFinished(MeshLib::Mesh*)), model,
267 SLOT(addMesh(MeshLib::Mesh*)));
268 meshLayerEdit.exec();
269}
270
272{
273 MeshModel const* const model = static_cast<MeshModel*>(this->model());
274 QModelIndex const index = this->selectionModel()->currentIndex();
275 auto* mesh = const_cast<MeshLib::Mesh*>(model->getMesh(index));
276
277 MeshValueEditDialog valueEdit(mesh);
278 connect(&valueEdit, SIGNAL(valueEditFinished(MeshLib::Mesh*)), model,
279 SLOT(updateMesh(MeshLib::Mesh*)));
280 valueEdit.exec();
281}
282
284{
285 QModelIndex const index = this->selectionModel()->currentIndex();
286 if (!index.isValid())
287 {
288 return;
289 }
290
291 MeshLib::Mesh const* const mesh =
292 static_cast<MeshModel*>(this->model())->getMesh(index);
293 if (mesh == nullptr)
294 {
295 return;
296 }
297
299 if (dlg.exec() != QDialog::Accepted)
300 {
301 return;
302 }
303
304 bool const copy_material_ids = false;
305 double const thickness(dlg.getThickness());
306 std::unique_ptr<MeshLib::Mesh> result(MeshToolsLib::addLayerToMesh(
307 *mesh, thickness, dlg.getName(), dlg.isTopLayer(), copy_material_ids,
308 std::nullopt));
309
310 if (result)
311 {
312 static_cast<MeshModel*>(model())->addMesh(std::move(result));
313 }
314 else
315 {
316 OGSError::box("Error adding layer to mesh.");
317 }
318}
319
321{
322 QModelIndex const index = this->selectionModel()->currentIndex();
323 if (!index.isValid())
324 {
325 return;
326 }
327
328 MeshLib::Mesh const* const mesh =
329 static_cast<MeshModel*>(this->model())->getMesh(index);
331 if (dlg.exec() != QDialog::Accepted)
332 {
333 return;
334 }
335
336 Eigen::Vector3d const& dir(dlg.getNormal());
337 int const tolerance(dlg.getTolerance());
338 std::unique_ptr<MeshLib::Mesh> sfc_mesh(
340 *mesh, dir, tolerance, "Bulk Mesh Node IDs",
341 "Bulk Mesh Element IDs", "Bulk Mesh Face IDs"));
342 if (sfc_mesh)
343 {
344 static_cast<MeshModel*>(model())->addMesh(std::move(sfc_mesh));
345 }
346 else
347 {
349 " No surfaces found to extract\n using the specified parameters.");
350 }
351}
352
354{
355 QModelIndex const index = this->selectionModel()->currentIndex();
356 MeshLib::Mesh const* const mesh =
357 static_cast<MeshModel*>(this->model())->getMesh(index);
359}
360
362{
363 QModelIndex const index = this->selectionModel()->currentIndex();
364 if (!index.isValid())
365 {
366 return;
367 }
368
369 QSettings const settings;
370 QFileInfo const fi(
371 settings.value("lastOpenedMeshFileDirectory").toString());
372 MeshLib::Mesh const* const mesh =
373 static_cast<MeshModel*>(this->model())->getMesh(index);
374 QString const fileName = QFileDialog::getSaveFileName(
375 nullptr, "Convert mesh to shapefile...",
377 QString::fromStdString(mesh->getName()),
378 "ESRI Shapefile (*.shp)");
379 if (!fileName.isEmpty())
380 {
382 if (!FileIO::SHPInterface::write2dMeshToSHP(fileName.toStdString(),
383 *mesh))
384 {
385 OGSError::box("Error exporting mesh\n to shapefile");
386 }
387 }
388}
389
391{
392 QModelIndex const index = this->selectionModel()->currentIndex();
393 if (!index.isValid())
394 {
395 return;
396 }
397
398 MeshLib::Mesh const* const mesh =
399 static_cast<MeshModel*>(this->model())->getMesh(index);
400 QSettings const settings;
401 QFileDialog dialog(
402 this, "Write TetGen input file to",
403 settings.value("lastOpenedTetgenFileDirectory").toString(),
404 "TetGen Geometry (*.smesh)");
405 dialog.setDefaultSuffix("smesh");
406 if (dialog.exec())
407 {
408 QString const filename = dialog.getSaveFileName();
409 if (!filename.isEmpty())
410 {
412 std::vector<MeshLib::Node> attr;
413 tg.writeTetGenSmesh(filename.toStdString(), *mesh, attr);
414 }
415 }
416}
417
419{
420 QModelIndex const index = this->selectionModel()->currentIndex();
421 if (!index.isValid())
422 {
423 OGSError::box("No mesh selected.");
424 return;
425 }
426
427 MeshLib::Mesh const* const mesh =
428 static_cast<MeshModel*>(this->model())->getMesh(index);
429 if (mesh == nullptr)
430 {
431 OGSError::box("No mesh selected.");
432 return;
433 }
434
435 SaveMeshDialog dlg(*mesh);
436 dlg.exec();
437}
438
440{
441 // QModelIndex const index = this->selectionModel()->currentIndex();
442 // MeshLib::Mesh const*const grid =
443 // static_cast<MeshModel*>(this->model())->getMesh(index);
444}
445
447{
448 QModelIndex const index = this->selectionModel()->currentIndex();
449 emit loadFEMCondFileRequested(index.data(0).toString().toStdString());
450}
451
453{
454 QModelIndex const index = this->selectionModel()->currentIndex();
455 MeshItem const* const item = static_cast<MeshItem*>(
456 static_cast<MeshModel*>(this->model())->getItem(index));
457 emit qualityCheckRequested(item->vtkSource());
458}
Definition of the AddLayerToMeshDialog class.
Definition of AddLayerToMesh class.
Definition of the AsciiRasterInterface class.
Definition of the ImportFileTypes enumeration.
Manages the last directory used for saving a file.
Definition of the MeshItem class.
Definition of the MeshLayerEditDialog class.
Definition of the MeshModel class.
Definition of the MeshSurfaceExtraction class.
Definition of the MeshValueEditDialog class.
Definition of the MeshView class.
Definition of the Mesh class.
Definition of the Node class.
Definition of the OGSError class.
Implementation of the SHPInterface class.
Definition of the SaveMeshDialog class.
Definition of the SurfaceExtractionDialog class.
Definition of the TetGenInterface class.
A dialog window for adding a layer to the top or bottom of a mesh.
std::string getName() const
Returns the name of the new mesh.
bool isTopLayer() const
Returns if the top layer button is selected (if false, bottom is selected).
double getThickness() const
Returns the thickness of the new layer.
static GeoLib::Raster * readRaster(std::string const &fname)
static bool write2dMeshToSHP(const std::string &file_name, const MeshLib::Mesh &mesh)
static bool writeTetGenSmesh(const std::string &file_name, const GeoLib::GEOObjects &geo_objects, const std::string &geo_name, const std::vector< GeoLib::Point > &attribute_points)
static void setDir(const QString &path)
Sets the directory last used for saving a file.
static const QString getDir()
Returns the directory last used for saving a file.
A TreeItem containing a mesh and the associated vtk object used in the Mesh Model.
Definition MeshItem.h:30
MeshLib::Mesh const * getMesh() const
Returns the mesh.
Definition MeshItem.h:42
MeshLib::VtkMappedMeshSource * vtkSource() const
Returns the VTK object.
Definition MeshItem.h:44
A dialog window for editing meshes in various ways.
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:88
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:103
A dialog window for mapping a 2d mesh based on a raster file.
std::string getNewMeshName() const
std::string getRasterPath() const
double getNoDataReplacement() const
double getStaticValue() const
const MeshLib::Mesh * getMesh(const QModelIndex &idx) const
Returns the mesh with the given index.
Definition MeshModel.cpp:96
static bool layerMapping(MeshLib::Mesh const &mesh, const GeoLib::Raster &raster, double nodata_replacement=0.0, bool const ignore_nodata=false)
static bool mapToStaticValue(MeshLib::Mesh const &mesh, double value)
Maps the elevation of all mesh nodes to the specified static value.
static MeshLib::Mesh * getMeshSurface(const MeshLib::Mesh &subsfc_mesh, Eigen::Vector3d const &dir, double angle, std::string_view subsfc_node_id_prop_name="", std::string_view subsfc_element_id_prop_name="", std::string_view face_id_prop_name="")
A dialog window for changing the MaterialID for mesh elements.
void enableSaveButton(bool)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override
Is called when the selection of this view changes.
Definition MeshView.cpp:67
~MeshView() override
void checkMeshQuality()
Calls the dialog for calculating an element quality metric.
Definition MeshView.cpp:452
void extractSurfaceMesh()
Definition MeshView.cpp:320
void meshSelected(MeshLib::Mesh const &)
void qualityCheckRequested(MeshLib::VtkMappedMeshSource *)
void contextMenuEvent(QContextMenuEvent *event) override
Definition MeshView.cpp:117
void openValuesEditDialog()
Opens a dialog for editing material groups.
Definition MeshView.cpp:271
MeshView(QWidget *parent=nullptr)
Definition MeshView.cpp:46
void openMap2dMeshDialog()
Opens a dialog for mapping 2d meshes.
Definition MeshView.cpp:175
void openMeshEditDialog()
Opens a dialog for editing meshes.
Definition MeshView.cpp:259
void writeToFile() const
Calls the FileDialog to save a mesh to a file.
Definition MeshView.cpp:418
void requestMeshToGeometryConversion(const MeshLib::Mesh *)
void addMesh()
Adds a new mesh.
Definition MeshView.cpp:97
void exportToTetGen()
Definition MeshView.cpp:390
void removeSelectedMeshComponent()
void openRasterDataToMeshDialog()
Opens a dialog for assigning raster data to the mesh.
Definition MeshView.cpp:220
void convertMeshToGeometry()
Definition MeshView.cpp:353
void openMeshFile(int)
void loadDIRECTSourceTerms()
Definition MeshView.cpp:446
void openAddLayerDialog()
Opens a dialog for adding a layer to the mesh.
Definition MeshView.cpp:283
void elementSelected(vtkUnstructuredGridAlgorithm const *const, unsigned, bool)
void requestMeshRemoval(const QModelIndex &)
void loadFEMCondFileRequested(const std::string)
void addDIRECTSourceTerms()
Definition MeshView.cpp:439
void removeMesh()
Remove the currently selected mesh.
Definition MeshView.cpp:102
void updateView()
Definition MeshView.cpp:55
void exportToShapefile() const
Definition MeshView.cpp:361
void enableRemoveButton(bool)
static void box(const QString &e)
Definition OGSError.cpp:23
A dialog window for transferring raster data onto a mesh.
std::string getArrayName() const
std::string getMeshName() const
std::string getRasterPath() const
A dialog window for managing properties for writing meshes to files.
A dialog window for managing properties for writing meshes to files.
Eigen::Vector3d const & getNormal() const
Objects nodes for the TreeModel.
Definition TreeItem.h:28
TreeItem * parentItem() const
Definition TreeItem.cpp:115
int row() const
Definition TreeItem.cpp:73
A hierarchical model for a tree implemented as a double-linked list.
Definition TreeModel.h:30
bool projectToNodes(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
bool projectToElements(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
MeshLib::Mesh * addLayerToMesh(MeshLib::Mesh const &mesh, double const thickness, std::string const &name, bool const on_top, bool const copy_material_ids, std::optional< int > const layer_id)
OGSMesh getMesh(std::string const &name)