diff --git a/Gui/DataView/DirectConditionGenerator.cpp b/Gui/DataView/DirectConditionGenerator.cpp index a6f18b782822029a0e0c6971699377d5ef2db24a..83ef8a150833627965d58d1921ea58f7b22b61a8 100644 --- a/Gui/DataView/DirectConditionGenerator.cpp +++ b/Gui/DataView/DirectConditionGenerator.cpp @@ -20,7 +20,7 @@ #include "DirectConditionGenerator.h" #include "Raster.h" -#include "MshEditor.h" +#include "MeshSurfaceExtraction.h" #include "PointWithID.h" #include "Mesh.h" @@ -45,7 +45,7 @@ const std::vector< std::pair<size_t,double> >& DirectConditionGenerator::directT double const*const img(raster->begin()); const double dir[3] = {0,0,1}; - const std::vector<GeoLib::PointWithID*> surface_nodes(MeshLib::MshEditor::getSurfaceNodes(mesh, dir) ); + const std::vector<GeoLib::PointWithID*> surface_nodes(MeshLib::MeshSurfaceExtraction::getSurfaceNodes(mesh, dir) ); //std::vector<MeshLib::CNode*> nodes = mesh.nod_vector; const size_t nNodes(surface_nodes.size()); _direct_values.reserve(nNodes); @@ -90,9 +90,9 @@ const std::vector< std::pair<size_t,double> >& DirectConditionGenerator::directW //mesh.MarkInterface_mHM_Hydro_3D(); // mark element faces on the surface //---- const double dir[3] = {0,0,1}; - MeshLib::Mesh* sfc_mesh (MeshLib::MshEditor::getMeshSurface(mesh, dir)); + MeshLib::Mesh* sfc_mesh (MeshLib::MeshSurfaceExtraction::getMeshSurface(mesh, dir)); std::vector<double> node_area_vec (sfc_mesh->getNNodes()); - MeshLib::MshEditor::getSurfaceAreaForNodes(sfc_mesh, node_area_vec); + MeshLib::MeshSurfaceExtraction::getSurfaceAreaForNodes(sfc_mesh, node_area_vec); //---- double origin_x(0), origin_y(0), delta(0); size_t imgwidth(0), imgheight(0); diff --git a/Gui/DataView/GeoMapper.cpp b/Gui/DataView/GeoMapper.cpp index 6554734f9fe8b562dd235e92919c05813dd56850..a5b90b9976547479bd3566c16fc67ca71ebc1611 100644 --- a/Gui/DataView/GeoMapper.cpp +++ b/Gui/DataView/GeoMapper.cpp @@ -19,7 +19,7 @@ #include "Mesh.h" #include "Node.h" -#include "MshEditor.h" +#include "MeshSurfaceExtraction.h" #include "PointWithID.h" #include "Raster.h" #include "readMeshFromFile.h" @@ -145,7 +145,7 @@ GeoLib::Grid<GeoLib::PointWithID>* GeoMapper::getFlatGrid(MeshLib::Mesh const*co else { double dir[3] = {0,0,1}; - sfc_pnts = MeshLib::MshEditor::getSurfaceNodes(*mesh, dir); + sfc_pnts = MeshLib::MeshSurfaceExtraction::getSurfaceNodes(*mesh, dir); } size_t nPoints (sfc_pnts.size()); for (unsigned i=0; i<nPoints; ++i) diff --git a/Gui/DataView/MeshValueEditDialog.cpp b/Gui/DataView/MeshValueEditDialog.cpp index 384ef06e30acf91f87f0cad52859cc14f53da99e..2d9c29a35f2e7c3bc6940d36221d5f779b77b618 100644 --- a/Gui/DataView/MeshValueEditDialog.cpp +++ b/Gui/DataView/MeshValueEditDialog.cpp @@ -13,7 +13,7 @@ */ #include "MeshValueEditDialog.h" -#include "MshEditor.h" +#include "MeshEditing/ElementValueModification.h" #include "OGSError.h" @@ -33,7 +33,7 @@ MeshValueEditDialog::~MeshValueEditDialog(void) void MeshValueEditDialog::accept() { if (this->condenseButton->isChecked()) - MeshLib::MshEditor::condenseElementValues(*_mesh); + MeshLib::ElementValueModification::condense(*_mesh); else { if (this->edit_old_value->text().isEmpty()) @@ -49,7 +49,7 @@ void MeshValueEditDialog::accept() } unsigned new_value = static_cast<unsigned>(atoi(this->edit_new_value->text().toStdString().c_str())); bool do_not_replace = this->replaceCheckBox->isChecked(); - bool result = MeshLib::MshEditor::replaceElementValue(*_mesh, old_value, new_value, !do_not_replace); + bool result = MeshLib::ElementValueModification::replace(*_mesh, old_value, new_value, !do_not_replace); if (!result && do_not_replace) { OGSError::box("The new material group already exists."); diff --git a/Gui/DataView/MshLayerMapper.cpp b/Gui/DataView/MshLayerMapper.cpp index 6c22229d5cff82454c7aa9259b25b4e342d2336c..518be36ef3a36ec9804132fa1ef6b213fe402216 100644 --- a/Gui/DataView/MshLayerMapper.cpp +++ b/Gui/DataView/MshLayerMapper.cpp @@ -26,7 +26,7 @@ #include "Elements/Hex.h" #include "Elements/Pyramid.h" #include "Elements/Prism.h" -#include "MshEditor.h" +#include "MeshSurfaceExtraction.h" #include "MathTools.h" #include <QImage> @@ -213,7 +213,7 @@ int MshLayerMapper::LayerMapping(MeshLib::Mesh* new_mesh, const std::string &ras { std::cout << "Warning: Removing " << noData_nodes.size() << " mesh nodes at NoData values ... " << std::endl; - MeshLib::Mesh* red_mesh = MeshLib::MshEditor::removeMeshNodes(new_mesh, noData_nodes); + MeshLib::Mesh* red_mesh = MeshLib::removeMeshNodes(new_mesh, noData_nodes); if (new_mesh->getNElements() == 0) { delete new_mesh; @@ -279,7 +279,7 @@ MeshLib::Mesh* MshLayerMapper::blendLayersWithSurface(MeshLib::Mesh* mesh, const { // construct surface mesh from DEM const double dir[3] = {0,0,1}; - MeshLib::Mesh* dem = MeshLib::MshEditor::getMeshSurface(*mesh, dir); + MeshLib::Mesh* dem = MeshLib::MeshSurfaceExtraction::getMeshSurface(*mesh, dir); MshLayerMapper::LayerMapping(dem, dem_raster, 0, 0); const std::vector<MeshLib::Node*> dem_nodes (dem->getNodes()); diff --git a/Gui/DataView/MshView.cpp b/Gui/DataView/MshView.cpp index 3c23273740016f061c04d2aef56605d6a06d28f8..809b942cabff992aa6b270bb8f85191ef5c4dbff 100644 --- a/Gui/DataView/MshView.cpp +++ b/Gui/DataView/MshView.cpp @@ -19,7 +19,7 @@ #include "MshItem.h" #include "MshModel.h" #include "OGSError.h" -#include "MshEditor.h" +#include "MeshSurfaceExtraction.h" #include "ImportFileTypes.h" #include <QHeaderView> @@ -164,7 +164,7 @@ void MshView::extractSurfaceMesh() const MeshLib::Mesh* mesh = static_cast<MshModel*>(this->model())->getMesh(index); const double dir[3] = {0, 0, 1}; - static_cast<MshModel*>(this->model())->addMesh( MeshLib::MshEditor::getMeshSurface(*mesh, dir) ); + static_cast<MshModel*>(this->model())->addMesh( MeshLib::MeshSurfaceExtraction::getMeshSurface(*mesh, dir) ); } int MshView::writeToFile() const diff --git a/Gui/mainwindow.cpp b/Gui/mainwindow.cpp index f0bf22a475e3ddec7237cd5e6de27ec35c6a5bf6..a30193f80c26dd73dbeb9b951bb278c96b4ddcbf 100644 --- a/Gui/mainwindow.cpp +++ b/Gui/mainwindow.cpp @@ -81,7 +81,7 @@ #include "Mesh.h" #include "Node.h" #include "Elements/Element.h" -#include "MshEditor.h" +#include "MeshSurfaceExtraction.h" #include "readMeshFromFile.h" #include "Mesh2MeshPropertyInterpolation.h" @@ -1153,7 +1153,7 @@ void MainWindow::FEMTestStart() /* const double dir[3] = {0, 0, 1}; const MeshLib::Mesh* mesh = this->_project.getMesh("ketzin_2012_11_11_tets"); - _meshModels->addMesh( MeshLib::MshEditor::getMeshSurface(*mesh, dir) ); + _meshModels->addMesh( MeshLib::MeshSurfaceExtraction::getMeshSurface(*mesh, dir) ); */ /* const std::vector<GeoLib::Polyline*> *lines = this->_geoModels->getPolylineVec("WESS Rivers"); MeshLib::CFEMesh* mesh = const_cast<MeshLib::CFEMesh*>(_project.getMesh("Ammer-Homogen100m-Final")); diff --git a/MeshLib/CMakeLists.txt b/MeshLib/CMakeLists.txt index 442b06c0f5da8709eff1475fd020bb84aec5704a..76004a7a5ff810e4f1b380515b3497dc18602152 100644 --- a/MeshLib/CMakeLists.txt +++ b/MeshLib/CMakeLists.txt @@ -1,8 +1,9 @@ # Source files GET_SOURCE_FILES(SOURCES_MESHLIB) GET_SOURCE_FILES(SOURCES_ELEMENTS Elements) +GET_SOURCE_FILES(SOURCES_EDITING MeshEditing) GET_SOURCE_FILES(SOURCES_QUALITY MeshQuality) -SET ( SOURCES ${SOURCES_MESHLIB} ${SOURCES_ELEMENTS} ${SOURCES_QUALITY}) +SET ( SOURCES ${SOURCES_MESHLIB} ${SOURCES_ELEMENTS} ${SOURCES_EDITING} ${SOURCES_QUALITY}) # Create the library ADD_LIBRARY(MeshLib STATIC ${SOURCES}) diff --git a/MeshLib/MeshEditing/ElementValueModification.cpp b/MeshLib/MeshEditing/ElementValueModification.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f18ba73137e124d9c20e9a3422b0f2fa52768d69 --- /dev/null +++ b/MeshLib/MeshEditing/ElementValueModification.cpp @@ -0,0 +1,92 @@ +/** + * \file + * \author Karsten Rink + * \date 2013-04-04 + * \brief Implementation of the ElementValueModification class. + * + * \copyright + * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#include <algorithm> + +#include "ElementValueModification.h" +#include "Mesh.h" +#include "Elements/Element.h" + +#include "logog/include/logog.hpp" + +namespace MeshLib { + +std::vector<unsigned> ElementValueModification::getMeshValues(const MeshLib::Mesh &mesh) +{ + const std::size_t nElements (mesh.getNElements()); + std::vector<unsigned> value_mapping; + for (unsigned i=0; i<nElements; ++i) + { + bool exists(false); + unsigned value (mesh.getElement(i)->getValue()); + const unsigned nValues (value_mapping.size()); + for (unsigned j=0; j<nValues; ++j) + { + if (value == value_mapping[j]) + { + exists = true; + break; + } + } + if (!exists) + value_mapping.push_back(value); + } + + std::sort(value_mapping.begin(), value_mapping.end()); + return value_mapping; +} + +bool ElementValueModification::replace(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists) +{ + std::vector<unsigned> value_mapping (ElementValueModification::getMeshValues(mesh)); + + if (!replace_if_exists) + { + const unsigned nValues (value_mapping.size()); + for (unsigned j=0; j<nValues; ++j) + { + if (new_value == value_mapping[j]) + { + ERR ("Error in ElementValueModification::replaceElementValue() - Replacement value is already taken."); + return false; + } + } + } + const std::size_t nElements (mesh.getNElements()); + std::vector<MeshLib::Element*> elements (mesh.getElements()); + for (unsigned i=0; i<nElements; ++i) + { + if (elements[i]->getValue() == old_value) + elements[i]->setValue(new_value); + } + return true; +} + +unsigned ElementValueModification::condense(MeshLib::Mesh &mesh) +{ + std::vector<unsigned> value_mapping (ElementValueModification::getMeshValues(mesh)); + std::vector<unsigned> reverse_mapping(value_mapping.back()+1, 0); + const unsigned nValues (value_mapping.size()); + for (unsigned i=0; i<nValues; ++i) + reverse_mapping[value_mapping[i]] = i; + + const std::size_t nElements (mesh.getNElements()); + std::vector<MeshLib::Element*> elements (mesh.getElements()); + for (unsigned i=0; i<nElements; ++i) + elements[i]->setValue(reverse_mapping[elements[i]->getValue()]); + + return nValues; +} + +} // end namespace MeshLib diff --git a/MeshLib/MeshEditing/ElementValueModification.h b/MeshLib/MeshEditing/ElementValueModification.h new file mode 100644 index 0000000000000000000000000000000000000000..55e6604fbdfc6664eb8d578fa7799ccaba68be5e --- /dev/null +++ b/MeshLib/MeshEditing/ElementValueModification.h @@ -0,0 +1,45 @@ +/** + * \file + * \author Karsten Rink + * \date 2013-04-04 + * \brief Definition of the ElementValueModification class + * + * \copyright + * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#ifndef MESHVALUEMODIFICATION_H +#define MESHVALUEMODIFICATION_H + +#include <vector> + +namespace MeshLib { +// forward declarations +class Mesh; + +/** + * \brief A set of methods for manipulating mesh element values + */ +class ElementValueModification +{ +public: + /// Reduces the values assigned the elements of mesh to the smallest possible range. + /// Returns the number of different values. + static unsigned condense(MeshLib::Mesh &mesh); + + /// Replaces for all elements of mesh with the value old_value with new_value if possible. + /// Returns true if successful or false if the value is already taken. + static bool replace(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists = false); + +private: + /// Returns the values of elements within the mesh + static std::vector<unsigned> getMeshValues(const MeshLib::Mesh &mesh); +}; + +} // end namespace MeshLib + +#endif //MESHVALUEMODIFICATION_H diff --git a/MeshLib/MeshEditing/removeMeshNodes.cpp b/MeshLib/MeshEditing/removeMeshNodes.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cb538c21e32bd7f00e489c069ff295ba42c6c77 --- /dev/null +++ b/MeshLib/MeshEditing/removeMeshNodes.cpp @@ -0,0 +1,77 @@ +/** + * \file + * \author Karsten Rink + * \date 2013-04-04 + * \brief Implementation of removeMeshNodes. + * + * \copyright + * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#include "removeMeshNodes.h" +#include "Mesh.h" +#include "Node.h" +#include "Elements/Element.h" + +namespace MeshLib { + +MeshLib::Mesh* removeMeshNodes(MeshLib::Mesh* mesh, const std::vector<size_t> &nodes) +{ + MeshLib::Mesh* new_mesh (new MeshLib::Mesh(*mesh)); + + // delete nodes and their connected elements and replace them with null pointers + const size_t delNodes = nodes.size(); + std::vector<MeshLib::Node*> mesh_nodes = new_mesh->getNodes(); + for (size_t i = 0; i < delNodes; ++i) + { + const MeshLib::Node* node = new_mesh->getNode(i); + std::vector<MeshLib::Element*> conn_elems = node->getElements(); + + for (size_t j = 0; j < conn_elems.size(); ++j) + { + delete conn_elems[j]; + conn_elems[j] = NULL; + } + delete mesh_nodes[i]; + mesh_nodes[i] = NULL; + } + + // create map to adjust node indices in element vector + const size_t nNodes = new_mesh->getNNodes(); + std::vector<int> id_map(nNodes, -1); + size_t count(0); + for (size_t i = 0; i < nNodes; ++i) + { + if (mesh_nodes[i]) + { + mesh_nodes[i]->setID(count); + id_map.push_back(count++); + } + } + + // erase null pointers from node- and element vectors + std::vector<MeshLib::Element*> elements = new_mesh->getElements(); + for (std::vector<MeshLib::Element*>::iterator it = elements.begin(); it != elements.end(); ) + { + if (*it) + ++it; + else + it = elements.erase(it); + } + + for (std::vector<MeshLib::Node*>::iterator it = mesh_nodes.begin(); it != mesh_nodes.end(); ) + { + if (*it) + ++it; + else + it = mesh_nodes.erase(it); + } + + return new_mesh; +} + +} // end namespace MeshLib diff --git a/MeshLib/MeshEditing/removeMeshNodes.h b/MeshLib/MeshEditing/removeMeshNodes.h new file mode 100644 index 0000000000000000000000000000000000000000..fd84afa5e194153e0710ac71de01bac13df59a65 --- /dev/null +++ b/MeshLib/MeshEditing/removeMeshNodes.h @@ -0,0 +1,30 @@ +/** + * \file + * \author Karsten Rink + * \date 2013-04-04 + * \brief Definition of the removeMeshNodes + * + * \copyright + * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#ifndef REMOVEMESHNODES_H +#define REMOVEMESHNODES_H + +#include <vector> + +namespace MeshLib { + +// forward declarations +class Mesh; + + /// Removes the mesh nodes (and connected elements) given in the nodes-list from the mesh. + MeshLib::Mesh* removeMeshNodes(MeshLib::Mesh* mesh, const std::vector<std::size_t> &nodes); + +} // end namespace MeshLib + +#endif //REMOVEMESHNODES_H diff --git a/MeshLib/MshEditor.cpp b/MeshLib/MeshSurfaceExtraction.cpp similarity index 57% rename from MeshLib/MshEditor.cpp rename to MeshLib/MeshSurfaceExtraction.cpp index 6348ed002a6659a3d8568dc040c67aa2ff5daa95..f556f5cb35400d52aa115b1417acaf61b4a889b3 100644 --- a/MeshLib/MshEditor.cpp +++ b/MeshLib/MeshSurfaceExtraction.cpp @@ -1,8 +1,8 @@ /** * \file * \author Karsten Rink - * \date 2011-06-15 - * \brief Implementation of the MshEditor class. + * \date 2013-04-04 + * \brief Implementation of the MeshSurfaceExtraction class. * * \copyright * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) @@ -12,25 +12,20 @@ * */ -#include <algorithm> - -#include "MshEditor.h" +#include "MeshSurfaceExtraction.h" #include "PointWithID.h" #include "Mesh.h" #include "Node.h" -#include "Elements/Element.h" #include "Elements/Face.h" #include "Elements/Cell.h" #include "Elements/Tri.h" #include "Elements/Quad.h" -#include "MathTools.h" - #include "logog/include/logog.hpp" namespace MeshLib { -void MshEditor::getSurfaceAreaForNodes(const MeshLib::Mesh* mesh, std::vector<double> &node_area_vec) +void MeshSurfaceExtraction::getSurfaceAreaForNodes(const MeshLib::Mesh* mesh, std::vector<double> &node_area_vec) { if (mesh->getDimension() == 2) { @@ -61,66 +56,10 @@ void MshEditor::getSurfaceAreaForNodes(const MeshLib::Mesh* mesh, std::vector<do INFO ("Total surface Area: %f", total_area); } else - ERR ("Error in MshEditor::getSurfaceAreaForNodes() - Given mesh is no surface mesh (dimension != 2)."); + ERR ("Error in MeshSurfaceExtraction::getSurfaceAreaForNodes() - Given mesh is no surface mesh (dimension != 2)."); } -MeshLib::Mesh* MshEditor::removeMeshNodes(MeshLib::Mesh* mesh, - const std::vector<size_t> &nodes) -{ - MeshLib::Mesh* new_mesh (new MeshLib::Mesh(*mesh)); - - // delete nodes and their connected elements and replace them with null pointers - const size_t delNodes = nodes.size(); - std::vector<MeshLib::Node*> mesh_nodes = new_mesh->getNodes(); - for (size_t i = 0; i < delNodes; ++i) - { - const MeshLib::Node* node = new_mesh->getNode(i); - std::vector<MeshLib::Element*> conn_elems = node->getElements(); - - for (size_t j = 0; j < conn_elems.size(); ++j) - { - delete conn_elems[j]; - conn_elems[j] = NULL; - } - delete mesh_nodes[i]; - mesh_nodes[i] = NULL; - } - - // create map to adjust node indices in element vector - const size_t nNodes = new_mesh->getNNodes(); - std::vector<int> id_map(nNodes, -1); - size_t count(0); - for (size_t i = 0; i < nNodes; ++i) - { - if (mesh_nodes[i]) - { - mesh_nodes[i]->setID(count); - id_map.push_back(count++); - } - } - - // erase null pointers from node- and element vectors - std::vector<MeshLib::Element*> elements = new_mesh->getElements(); - for (std::vector<MeshLib::Element*>::iterator it = elements.begin(); it != elements.end(); ) - { - if (*it) - ++it; - else - it = elements.erase(it); - } - - for (std::vector<MeshLib::Node*>::iterator it = mesh_nodes.begin(); it != mesh_nodes.end(); ) - { - if (*it) - ++it; - else - it = mesh_nodes.erase(it); - } - - return new_mesh; -} - -MeshLib::Mesh* MshEditor::getMeshSurface(const MeshLib::Mesh &mesh, const double* dir) +MeshLib::Mesh* MeshSurfaceExtraction::getMeshSurface(const MeshLib::Mesh &mesh, const double* dir) { INFO ("Extracting mesh surface..."); const std::vector<MeshLib::Element*> all_elements (mesh.getElements()); @@ -160,7 +99,7 @@ MeshLib::Mesh* MshEditor::getMeshSurface(const MeshLib::Mesh &mesh, const double return NULL; } -void MshEditor::get2DSurfaceElements(const std::vector<MeshLib::Element*> &all_elements, std::vector<MeshLib::Element*> &sfc_elements, const double* dir, unsigned mesh_dimension) +void MeshSurfaceExtraction::get2DSurfaceElements(const std::vector<MeshLib::Element*> &all_elements, std::vector<MeshLib::Element*> &sfc_elements, const double* dir, unsigned mesh_dimension) { bool complete_surface (true); if (dir) @@ -221,7 +160,7 @@ void MshEditor::get2DSurfaceElements(const std::vector<MeshLib::Element*> &all_e ERR("Cannot handle meshes of dimension %i", mesh_dimension); } -void MshEditor::get2DSurfaceNodes(const std::vector<MeshLib::Node*> &all_nodes, std::vector<MeshLib::Node*> &sfc_nodes, const std::vector<MeshLib::Element*> &sfc_elements, std::vector<unsigned> &node_id_map) +void MeshSurfaceExtraction::get2DSurfaceNodes(const std::vector<MeshLib::Node*> &all_nodes, std::vector<MeshLib::Node*> &sfc_nodes, const std::vector<MeshLib::Element*> &sfc_elements, std::vector<unsigned> &node_id_map) { const size_t nNewElements (sfc_elements.size()); std::vector<const MeshLib::Node*> tmp_nodes(all_nodes.size(), NULL); @@ -245,7 +184,7 @@ void MshEditor::get2DSurfaceNodes(const std::vector<MeshLib::Node*> &all_nodes, } } -std::vector<GeoLib::PointWithID*> MshEditor::getSurfaceNodes(const MeshLib::Mesh &mesh, const double *dir) +std::vector<GeoLib::PointWithID*> MeshSurfaceExtraction::getSurfaceNodes(const MeshLib::Mesh &mesh, const double *dir) { INFO ("Extracting surface nodes..."); const std::vector<MeshLib::Element*> all_elements (mesh.getElements()); @@ -272,71 +211,4 @@ std::vector<GeoLib::PointWithID*> MshEditor::getSurfaceNodes(const MeshLib::Mesh return surface_pnts; } -std::vector<unsigned> MshEditor::getMeshValues(const MeshLib::Mesh &mesh) -{ - const std::size_t nElements (mesh.getNElements()); - std::vector<unsigned> value_mapping; - for (unsigned i=0; i<nElements; ++i) - { - bool exists(false); - unsigned value (mesh.getElement(i)->getValue()); - const unsigned nValues (value_mapping.size()); - for (unsigned j=0; j<nValues; ++j) - { - if (value == value_mapping[j]) - { - exists = true; - break; - } - } - if (!exists) - value_mapping.push_back(value); - } - - std::sort(value_mapping.begin(), value_mapping.end()); - return value_mapping; -} - -bool MshEditor::replaceElementValue(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists) -{ - std::vector<unsigned> value_mapping (MshEditor::getMeshValues(mesh)); - - if (!replace_if_exists) - { - const unsigned nValues (value_mapping.size()); - for (unsigned j=0; j<nValues; ++j) - { - if (new_value == value_mapping[j]) - { - ERR ("Error in MshEditor::replaceElementValue() - Replacement value is already taken."); - return false; - } - } - } - const std::size_t nElements (mesh.getNElements()); - std::vector<MeshLib::Element*> elements (mesh.getElements()); - for (unsigned i=0; i<nElements; ++i) - { - if (elements[i]->getValue() == old_value) - elements[i]->setValue(new_value); - } - return true; -} - -unsigned MshEditor::condenseElementValues(MeshLib::Mesh &mesh) -{ - std::vector<unsigned> value_mapping (MshEditor::getMeshValues(mesh)); - std::vector<unsigned> reverse_mapping(value_mapping.back()+1, 0); - const unsigned nValues (value_mapping.size()); - for (unsigned i=0; i<nValues; ++i) - reverse_mapping[value_mapping[i]] = i; - - const std::size_t nElements (mesh.getNElements()); - std::vector<MeshLib::Element*> elements (mesh.getElements()); - for (unsigned i=0; i<nElements; ++i) - elements[i]->setValue(reverse_mapping[elements[i]->getValue()]); - - return nValues; -} - } // end namespace MeshLib diff --git a/MeshLib/MshEditor.h b/MeshLib/MeshSurfaceExtraction.h similarity index 60% rename from MeshLib/MshEditor.h rename to MeshLib/MeshSurfaceExtraction.h index 2a4bf4e666c0b1b8f23a8067bfe8614bb47b37fb..54f51367a990c5b978254baf130e251f8eb7aa44 100644 --- a/MeshLib/MshEditor.h +++ b/MeshLib/MeshSurfaceExtraction.h @@ -1,8 +1,8 @@ /** * \file * \author Karsten Rink - * \date 2011-06-15 - * \brief Definition of the MshEditor class + * \date 2013-04-04 + * \brief Definition of the MeshSurfaceExtraction class * * \copyright * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) @@ -12,14 +12,12 @@ * */ -#ifndef MSHEDITOR_H -#define MSHEDITOR_H +#ifndef MESHSURFACEEXTRACTION_H +#define MESHSURFACEEXTRACTION_H #include <cstddef> #include <vector> -#include "Point.h" - namespace GeoLib { class PointWithID; } @@ -31,42 +29,28 @@ class Element; class Node; /** - * \brief A set of tools for manipulating existing meshes + * \brief A set of tools concerned with extracting nodes and elements from a mesh surface */ -class MshEditor +class MeshSurfaceExtraction { public: /// Returns the area assigned to each node on a surface mesh. static void getSurfaceAreaForNodes(const MeshLib::Mesh* mesh, std::vector<double> &node_area_vec); - /// Removes the mesh nodes (and connected elements) given in the nodes-list from the mesh. - static MeshLib::Mesh* removeMeshNodes(MeshLib::Mesh* mesh, const std::vector<std::size_t> &nodes); - /// Returns the surface nodes of a layered mesh. static std::vector<GeoLib::PointWithID*> getSurfaceNodes(const MeshLib::Mesh &mesh, const double* dir = NULL); /// Returns the 2d-element mesh representing the surface of the given layered mesh. static MeshLib::Mesh* getMeshSurface(const MeshLib::Mesh &mesh, const double* dir = NULL); - /// Reduces the values assigned the elements of mesh to the smallest possible range. - /// Returns the number of different values. - static unsigned condenseElementValues(MeshLib::Mesh &mesh); - - /// Replaces for all elements of mesh with the value old_value with new_value if possible. - /// Returns true if successful or false if the value is already taken. - static bool replaceElementValue(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists = false); - private: /// Functionality needed for getSurfaceNodes() and getMeshSurface() static void get2DSurfaceElements(const std::vector<MeshLib::Element*> &all_elements, std::vector<MeshLib::Element*> &sfc_elements, const double* dir, unsigned mesh_dimension); /// Functionality needed for getSurfaceNodes() and getMeshSurface() static void get2DSurfaceNodes(const std::vector<MeshLib::Node*> &all_nodes, std::vector<MeshLib::Node*> &sfc_nodes, const std::vector<MeshLib::Element*> &sfc_elements, std::vector<unsigned> &node_id_map); - - /// Returns the values of elements within the mesh - static std::vector<unsigned> getMeshValues(const MeshLib::Mesh &mesh); }; } // end namespace MeshLib -#endif //MSHEDITOR_H +#endif //MESHSURFACEEXTRACTION_H diff --git a/MeshLib/Node.h b/MeshLib/Node.h index 215650b9def07e2bf1adb76997ca56e178c47e3e..1df7d968851bc6981b212bada1342357f550cb1a 100644 --- a/MeshLib/Node.h +++ b/MeshLib/Node.h @@ -22,7 +22,8 @@ #include "PointWithID.h" #include "Mesh.h" -#include "MshEditor.h" +#include "MeshEditing/removeMeshNodes.h" +#include "MeshSurfaceExtraction.h" #ifdef OGS_BUILD_GUI #include "../Gui/DataView/MshLayerMapper.h" #endif @@ -37,7 +38,7 @@ class Element; class Node : public GeoLib::PointWithID { /* friend functions: */ - friend MeshLib::Mesh* MshEditor::removeMeshNodes(MeshLib::Mesh* mesh, const std::vector<std::size_t> &nodes); + friend MeshLib::Mesh* removeMeshNodes(MeshLib::Mesh* mesh, const std::vector<std::size_t> &nodes); #ifdef OGS_BUILD_GUI friend int MshLayerMapper::LayerMapping(MeshLib::Mesh* msh, const std::string &rasterfile, const unsigned nLayers, const unsigned layer_id, bool removeNoDataValues); diff --git a/Utils/MeshEdit/moveMeshNodes.cpp b/Utils/MeshEdit/moveMeshNodes.cpp index 91940b92413ab03c9bd0ade21d5a700e26e8e864..2d24f30fb6e6c2b8ef730c00cc82700e08825824 100644 --- a/Utils/MeshEdit/moveMeshNodes.cpp +++ b/Utils/MeshEdit/moveMeshNodes.cpp @@ -11,7 +11,7 @@ #include "AABB.h" #include "Mesh.h" #include "Node.h" -#include "MshEditor.h" +#include "MeshEditing/removeMeshNodes.h" #include "MathTools.h" int find_closest_point(MeshLib::Node const*const point, std::vector<MeshLib::Node*> const& nodes, double const& max_dist) diff --git a/Utils/MeshEdit/removeMeshNodes.cpp b/Utils/MeshEdit/removeMeshNodes.cpp index 0dd86b23f330f8ca8d8e15aef6b74007a156ee5e..8b99e59f97d3a80a25e01c7eb0e98387a9d1a36b 100644 --- a/Utils/MeshEdit/removeMeshNodes.cpp +++ b/Utils/MeshEdit/removeMeshNodes.cpp @@ -8,7 +8,7 @@ #include "Legacy/MeshIO.h" #include "Mesh.h" #include "Node.h" -#include "MshEditor.h" +#include "MeshEditing/removeMeshNodes.h" int main (int argc, char* argv[]) { @@ -82,7 +82,7 @@ int main (int argc, char* argv[]) /**** add other keywords here ****/ // remove nodes and write new file - MeshLib::Mesh* new_mesh = MeshLib::MshEditor::removeMeshNodes(mesh, del_nodes); + MeshLib::Mesh* new_mesh = MeshLib::removeMeshNodes(mesh, del_nodes); FileIO::MeshIO meshIO; meshIO.setMesh(mesh);