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