OGS
MeshModel.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#include "MeshModel.h"
5
6#include <vtkUnstructuredGridAlgorithm.h>
7
8#include <QFileInfo>
9#include <QString>
10
12#include "Base/TreeItem.h"
13#include "BaseLib/Logging.h"
14#include "BaseLib/StringTools.h"
15#include "MeshItem.h"
17#include "MeshLib/Node.h"
18
19const QVariant MeshModel::element_str = "Element";
20const std::map<MeshLib::MeshElemType, QVariant> MeshModel::elem_type_map =
22
24 : TreeModel(parent), _project(project)
25{
26 delete _rootItem;
27 QList<QVariant> rootData;
28 rootData << "Mesh Name"
29 << "#"
30 << "Type";
31 _rootItem = new TreeItem(rootData, nullptr);
32}
33
34int MeshModel::columnCount(const QModelIndex& parent /*= QModelIndex()*/) const
35{
36 Q_UNUSED(parent)
37
38 return 3;
39}
40
41void MeshModel::addMesh(std::unique_ptr<MeshLib::Mesh> mesh)
42{
43 _project.addMesh(std::move(mesh));
44 auto const& meshes(_project.getMeshObjects());
45 this->addMeshObject(meshes.back().get());
46}
47
49{
50 _project.addMesh(std::unique_ptr<MeshLib::Mesh>(mesh));
51 auto const& meshes(_project.getMeshObjects());
52 this->addMeshObject(meshes.back().get());
53}
54
56{
57 beginResetModel();
58
59 INFO("name: {:s}", mesh->getName());
60 QVariant const display_name(QString::fromStdString(mesh->getName()));
61 QList<QVariant> meshData;
62 meshData << display_name << ""
63 << "";
64 auto* const newMesh = new MeshItem(meshData, _rootItem, mesh);
65 _rootItem->appendChild(newMesh);
66
67 // display elements
68 std::vector<MeshLib::Element*> const& elems = mesh->getElements();
69 auto const nElems(static_cast<int>(elems.size()));
70
71 for (int i = 0; i < nElems; i++)
72 {
73 QList<QVariant> elemData;
74 elemData.reserve(3);
75 elemData << element_str << i
76 << elem_type_map.at(elems[i]->getGeomType());
77 newMesh->appendChild(new TreeItem(elemData, newMesh));
78 }
79
80 endResetModel();
81 emit meshAdded(this,
82 this->index(_rootItem->childCount() - 1, 0, QModelIndex()));
83}
84
85const MeshLib::Mesh* MeshModel::getMesh(const QModelIndex& idx) const
86{
87 if (idx.isValid())
88 {
89 auto* item = dynamic_cast<MeshItem*>(this->getItem(idx));
90 if (item)
91 {
92 return item->getMesh();
93 }
94
95 return nullptr;
96 }
97 WARN("MeshModel::getMesh(): Specified index does not exist.");
98 return nullptr;
99}
100
101const MeshLib::Mesh* MeshModel::getMesh(const std::string& name) const
102{
103 for (int i = 0; i < _rootItem->childCount(); i++)
104 {
105 auto* item = static_cast<MeshItem*>(_rootItem->child(i));
106 if (item->data(0).toString().toStdString() == name)
107 {
108 return item->getMesh();
109 }
110 }
111
112 INFO("MeshModel::getMesh(): No entry found with name \"{:s}\".", name);
113 return nullptr;
114}
115
116bool MeshModel::removeMesh(const QModelIndex& idx)
117{
118 if (idx.isValid())
119 {
120 auto* item = dynamic_cast<MeshItem*>(this->getItem(idx));
121 if (item)
122 {
123 return this->removeMesh(item->getMesh()->getName());
124 }
125 return false;
126 }
127 return false;
128}
129
130bool MeshModel::removeMesh(const std::string& name)
131{
132 for (int i = 0; i < _rootItem->childCount(); i++)
133 {
134 TreeItem* item = _rootItem->child(i);
135 if (item->data(0).toString().toStdString() == name)
136 {
137 beginResetModel();
138 emit meshRemoved(this, this->index(i, 0, QModelIndex()));
139 _rootItem->removeChildren(i, 1);
140 endResetModel();
141 return _project.removeMesh(name);
142 }
143 }
144
145 INFO("MeshModel::removeMesh(): No entry found with name \"{:s}\".", name);
146 return false;
147}
148
150{
151 for (int i = 0; i < _rootItem->childCount(); i++)
152 {
153 if (dynamic_cast<MeshItem*>(this->_rootItem->child(i))->getMesh() ==
154 mesh)
155 {
156 emit meshRemoved(this, this->index(i, 0, QModelIndex()));
157 _rootItem->removeChildren(i, 1);
158 }
159 }
160 this->addMeshObject(mesh);
161}
162
164{
165 auto const& mesh_vec = _project.getMeshObjects();
166 for (auto const& mesh : mesh_vec)
167 {
168 if (!getMesh(mesh->getName()))
169 { // if Mesh is not yet added to GUI, do it now
170 addMeshObject(mesh.get());
171 }
172 }
173}
174
175std::map<MeshLib::MeshElemType, QVariant> MeshModel::createMeshElemTypeMap()
176{
177 std::vector<MeshLib::MeshElemType> const& elem_types(
179 std::map<MeshLib::MeshElemType, QVariant> elem_map;
180
181 for (MeshLib::MeshElemType t : elem_types)
182 {
183 elem_map[t] =
184 QVariant(QString::fromStdString(MeshLib::MeshElemType2String(t)));
185 }
186
187 return elem_map;
188}
189
190vtkUnstructuredGridAlgorithm* MeshModel::vtkSource(const QModelIndex& idx) const
191{
192 if (idx.isValid())
193 {
194 auto* item = static_cast<MeshItem*>(this->getItem(idx));
195 return item->vtkSource();
196 }
197
198 INFO("MeshModel::vtkSource(): Specified index does not exist.");
199 return nullptr;
200}
201
202vtkUnstructuredGridAlgorithm* MeshModel::vtkSource(
203 const std::string& name) const
204{
205 for (int i = 0; i < _rootItem->childCount(); i++)
206 {
207 auto* item = static_cast<MeshItem*>(_rootItem->child(i));
208 if (item->data(0).toString().toStdString() == name)
209 {
210 return item->vtkSource();
211 }
212 }
213
214 INFO("MeshModel::vtkSource(): No entry found with name \"{:s}\".", name);
215 return nullptr;
216}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
A TreeItem containing a mesh and the associated vtk object used in the Mesh Model.
Definition MeshItem.h:19
MeshLib::Mesh const * getMesh() const
Returns the mesh.
Definition MeshItem.h:31
MeshLib::VtkMappedMeshSource * vtkSource() const
Returns the VTK object.
Definition MeshItem.h:33
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:94
void meshAdded(MeshModel *, const QModelIndex &)
void updateModel()
Updates the model based on the ProjectData-object.
bool removeMesh(const QModelIndex &idx)
Removes the mesh with the given index.
static std::map< MeshLib::MeshElemType, QVariant > createMeshElemTypeMap()
Creates a static map of all element type name-strings in QVariant format.
static const std::map< MeshLib::MeshElemType, QVariant > elem_type_map
Definition MeshModel.h:71
void addMesh(std::unique_ptr< MeshLib::Mesh > mesh)
Adds a new mesh.
Definition MeshModel.cpp:41
static const QVariant element_str
Definition MeshModel.h:72
void meshRemoved(MeshModel *, const QModelIndex &)
vtkUnstructuredGridAlgorithm * vtkSource(const QModelIndex &idx) const
Returns the VTK source item for the mesh with the given index.
DataHolderLib::Project & _project
Checks if the name of the mesh is already exists, if so it generates a unique name.
Definition MeshModel.h:66
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns used for the data list.
Definition MeshModel.cpp:34
void addMeshObject(const MeshLib::Mesh *mesh)
Adds the mesh to the GUI-Mesh-Model und -View.
Definition MeshModel.cpp:55
const MeshLib::Mesh * getMesh(const QModelIndex &idx) const
Returns the mesh with the given index.
Definition MeshModel.cpp:85
MeshModel(DataHolderLib::Project &project, QObject *parent=nullptr)
Definition MeshModel.cpp:23
void updateMesh(MeshLib::Mesh *)
Updates the model/view for a mesh.
Objects nodes for the TreeModel.
Definition TreeItem.h:17
virtual QVariant data(int column) const
Definition TreeItem.cpp:83
TreeItem * getItem(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition TreeModel.cpp:39
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:70
TreeModel(QObject *parent=nullptr)
Definition TreeModel.cpp:15
TreeItem * _rootItem
Definition TreeModel.h:47
std::string MeshElemType2String(const MeshElemType t)
Given a MeshElemType this returns the appropriate string.
Definition MeshEnums.cpp:10
std::vector< MeshElemType > getMeshElemTypes()
Returns a vector of all mesh element types.
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:37