Loading [MathJax]/extensions/tex2jax.js
OGS
MeshToolsLib::MeshRevision Class Reference

Detailed Description

Collapses nodes with a distance smaller min_distance and reduces elements accordingly.

Definition at line 35 of file MeshRevision.h.

#include <MeshRevision.h>

Collaboration diagram for MeshToolsLib::MeshRevision:
[legend]

Public Member Functions

 MeshRevision (MeshLib::Mesh &mesh)
 
unsigned getNumberOfCollapsibleNodes (double eps=std::numeric_limits< double >::epsilon()) const
 Returns the number of potentially collapsible nodes.
 
std::vector< std::size_t > collapseNodeIndices (double eps) const
 
MeshLib::MeshsimplifyMesh (const std::string &new_mesh_name, double eps, unsigned min_elem_dim=1) const
 

Private Member Functions

std::vector< MeshLib::Node * > constructNewNodesArray (const std::vector< std::size_t > &id_map) const
 

Private Attributes

MeshLib::Mesh_mesh
 The original mesh used for constructing the class.
 

Constructor & Destructor Documentation

◆ MeshRevision()

MeshToolsLib::MeshRevision::MeshRevision ( MeshLib::Mesh & mesh)
explicit

Constructor

Parameters
meshThe mesh which is being revised. Note that node IDs in mesh are changed during computation but are reset after the algorithms implemented here are finished

Definition at line 1007 of file MeshRevision.cpp.

1007: _mesh(mesh) {}
MeshLib::Mesh & _mesh
The original mesh used for constructing the class.

Member Function Documentation

◆ collapseNodeIndices()

std::vector< std::size_t > MeshToolsLib::MeshRevision::collapseNodeIndices ( double eps) const

Designates nodes to be collapsed by setting their ID to the index of the node they will get merged with.

Definition at line 1096 of file MeshRevision.cpp.

1098{
1099 const std::vector<MeshLib::Node*>& nodes(_mesh.getNodes());
1100 const std::size_t nNodes(_mesh.getNumberOfNodes());
1101 std::vector<std::size_t> id_map(nNodes);
1102 const double half_eps(eps / 2.0);
1103 const double sqr_eps(eps * eps);
1104 std::iota(id_map.begin(), id_map.end(), 0);
1105
1106 GeoLib::Grid<MeshLib::Node> const grid(nodes.begin(), nodes.end(), 64);
1107
1108 for (std::size_t k = 0; k < nNodes; ++k)
1109 {
1110 MeshLib::Node const* const node(nodes[k]);
1111 if (node->getID() != k)
1112 {
1113 continue;
1114 }
1115 std::vector<std::vector<MeshLib::Node*> const*> const node_vectors(
1116 grid.getPntVecsOfGridCellsIntersectingCube(*node, half_eps));
1117
1118 const std::size_t nVectors(node_vectors.size());
1119 for (std::size_t i = 0; i < nVectors; ++i)
1120 {
1121 const std::vector<MeshLib::Node*>& cell_vector(*node_vectors[i]);
1122 const std::size_t nGridCellNodes(cell_vector.size());
1123 for (std::size_t j = 0; j < nGridCellNodes; ++j)
1124 {
1125 MeshLib::Node const* const test_node(cell_vector[j]);
1126 // are node indices already identical (i.e. nodes will be
1127 // collapsed)
1128 if (id_map[node->getID()] == id_map[test_node->getID()])
1129 {
1130 continue;
1131 }
1132
1133 // if test_node has already been collapsed to another node x,
1134 // ignore it (if the current node would need to be collapsed
1135 // with x it would already have happened when x was tested)
1136 if (test_node->getID() != id_map[test_node->getID()])
1137 {
1138 continue;
1139 }
1140
1141 // calc distance
1142 if (MathLib::sqrDist(*node, *test_node) < sqr_eps)
1143 {
1144 id_map[test_node->getID()] = node->getID();
1145 }
1146 }
1147 }
1148 }
1149 return id_map;
1150}
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:108
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:102
double sqrDist(MathLib::Point3d const &p0, MathLib::Point3d const &p1)
Definition Point3d.cpp:26

References _mesh, MathLib::Point3dWithID::getID(), MeshLib::Mesh::getNodes(), MeshLib::Mesh::getNumberOfNodes(), GeoLib::Grid< POINT >::getPntVecsOfGridCellsIntersectingCube(), and MathLib::sqrDist().

Referenced by getNumberOfCollapsibleNodes(), MeshAnalysisDialog::on_startButton_pressed(), and simplifyMesh().

◆ constructNewNodesArray()

std::vector< MeshLib::Node * > MeshToolsLib::MeshRevision::constructNewNodesArray ( const std::vector< std::size_t > & id_map) const
private

Constructs a new node vector for the resulting mesh by removing all nodes whose ID indicates they need to be merged/removed.

Definition at line 1152 of file MeshRevision.cpp.

1154{
1155 const std::vector<MeshLib::Node*>& nodes(_mesh.getNodes());
1156 const std::size_t nNodes(nodes.size());
1157 std::vector<MeshLib::Node*> new_nodes;
1158 new_nodes.reserve(nNodes);
1159 for (std::size_t k = 0; k < nNodes; ++k)
1160 {
1161 // all nodes that have not been collapsed with other nodes are copied
1162 // into new array
1163 if (nodes[k]->getID() == id_map[k])
1164 {
1165 std::size_t const id(new_nodes.size());
1166 new_nodes.push_back(new MeshLib::Node(
1167 (*nodes[k])[0], (*nodes[k])[1], (*nodes[k])[2], id));
1168 nodes[k]->setID(id); // the node in the old array gets the index of
1169 // the same node in the new array
1170 }
1171 // the other nodes are not copied and get the index of the nodes they
1172 // will have been collapsed with
1173 else
1174 {
1175 nodes[k]->setID(nodes[id_map[k]]->getID());
1176 }
1177 }
1178 return new_nodes;
1179}

References _mesh, and MeshLib::Mesh::getNodes().

Referenced by simplifyMesh().

◆ getNumberOfCollapsibleNodes()

unsigned MeshToolsLib::MeshRevision::getNumberOfCollapsibleNodes ( double eps = std::numeric_limits<double>::epsilon()) const

Returns the number of potentially collapsible nodes.

Definition at line 1009 of file MeshRevision.cpp.

1010{
1011 std::vector<std::size_t> const id_map = collapseNodeIndices(eps);
1012 std::size_t const nNodes = id_map.size();
1013 unsigned count(0);
1014 for (std::size_t i = 0; i < nNodes; ++i)
1015 {
1016 if (i != id_map[i])
1017 {
1018 count++;
1019 }
1020 }
1021 return count;
1022}
std::vector< std::size_t > collapseNodeIndices(double eps) const

References collapseNodeIndices().

Referenced by MeshToolsLib::MeshValidation::existCollapsibleNodes().

◆ simplifyMesh()

MeshLib::Mesh * MeshToolsLib::MeshRevision::simplifyMesh ( const std::string & new_mesh_name,
double eps,
unsigned min_elem_dim = 1 ) const

Create a new mesh where all nodes with a distance < eps from each other are collapsed. Elements are adjusted accordingly and elements with nonplanar faces are subdivided into geometrically correct elements.

Parameters
new_mesh_nameNew name.
epsMinimum distance for nodes not to be collapsed
min_elem_dimMinimum dimension of elements to be inserted into new mesh (i.e. min_elem_dim=3 will prevent the new mesh to contain 2D elements)

Definition at line 1024 of file MeshRevision.cpp.

1027{
1028 if (this->_mesh.getNumberOfElements() == 0)
1029 {
1030 return nullptr;
1031 }
1032
1033 std::vector<MeshLib::Element*> const& elements(this->_mesh.getElements());
1034 auto const node_ids = collapseNodeIndices(eps);
1035 std::vector<MeshLib::Node*> new_nodes =
1036 this->constructNewNodesArray(node_ids);
1037 std::vector<MeshLib::Element*> new_elements;
1038 std::vector<std::size_t> element_ids;
1039
1040 for (std::size_t k(0); k < elements.size(); ++k)
1041 {
1042 MeshLib::Element const* const elem(elements[k]);
1043 unsigned const n_unique_nodes(getNumberOfUniqueNodes(elem));
1044 if (n_unique_nodes == elem->getNumberOfBaseNodes() &&
1045 elem->getDimension() >= min_elem_dim)
1046 {
1047 ElementErrorCode const e = elem->validate();
1049 {
1050 std::size_t const n_new_elements(
1051 subdivideElement(elem, new_nodes, new_elements));
1052 if (n_new_elements == 0)
1053 {
1054 ERR("Element {:d} has unknown element type.", k);
1056 BaseLib::cleanupVectorElements(new_nodes, new_elements);
1057 return nullptr;
1058 }
1059 element_ids.insert(element_ids.end(), n_new_elements, k);
1060 }
1061 else
1062 {
1063 new_elements.push_back(MeshLib::copyElement(elem, new_nodes));
1064 element_ids.push_back(k);
1065 }
1066 }
1067 else if (n_unique_nodes < elem->getNumberOfBaseNodes() &&
1068 n_unique_nodes > 1)
1069 {
1070 std::size_t const n_new_elements(reduceElement(
1071 elem, n_unique_nodes, new_nodes, new_elements, min_elem_dim));
1072 element_ids.insert(element_ids.end(), n_new_elements, k);
1073 }
1074 else
1075 {
1076 ERR("Something is wrong, more unique nodes than actual nodes");
1077 }
1078 }
1079
1080 auto const& props = _mesh.getProperties();
1081 MeshLib::Properties const new_properties =
1082 copyProperties(props, node_ids, element_ids);
1083
1085 if (!new_elements.empty())
1086 {
1087 return new MeshLib::Mesh(new_mesh_name, new_nodes, new_elements,
1088 true /* compute_element_neighbors */,
1089 new_properties);
1090 }
1091
1092 BaseLib::cleanupVectorElements(new_nodes, new_elements);
1093 return nullptr;
1094}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Collects error flags for mesh elements.
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
void resetNodeIDs()
Resets the IDs of all mesh-nodes to their position in the node vector.
Definition Mesh.cpp:160
Properties & getProperties()
Definition Mesh.h:136
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:99
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition Properties.h:33
std::vector< MeshLib::Node * > constructNewNodesArray(const std::vector< std::size_t > &id_map) const
void cleanupVectorElements(std::vector< T * > &items)
Definition Algorithm.h:256
Element * copyElement(Element const *const element, const std::vector< Node * > &nodes, std::vector< std::size_t > const *const id_map)
std::size_t reduceElement(MeshLib::Element const *const element, unsigned const n_unique_nodes, std::vector< MeshLib::Node * > const &nodes, std::vector< MeshLib::Element * > &elements, unsigned const min_elem_dim)
unsigned getNumberOfUniqueNodes(MeshLib::Element const *const element)
std::size_t subdivideElement(MeshLib::Element const *const element, std::vector< MeshLib::Node * > const &nodes, std::vector< MeshLib::Element * > &elements)
MeshLib::Properties copyProperties(MeshLib::Properties const &props, std::vector< std::size_t > const &node_ids, std::vector< std::size_t > const &elem_ids)

References _mesh, BaseLib::cleanupVectorElements(), collapseNodeIndices(), constructNewNodesArray(), MeshLib::copyElement(), ERR(), MeshLib::Element::getDimension(), MeshLib::Mesh::getElements(), MeshLib::Element::getNumberOfBaseNodes(), MeshLib::Mesh::getNumberOfElements(), MeshLib::Mesh::getProperties(), NonCoplanar, MeshLib::Mesh::resetNodeIDs(), and MeshLib::Element::validate().

Referenced by MeshToolsLib::convertSurfaceToMesh(), and main().

Member Data Documentation

◆ _mesh

MeshLib::Mesh& MeshToolsLib::MeshRevision::_mesh
private

The original mesh used for constructing the class.

Definition at line 75 of file MeshRevision.h.

Referenced by collapseNodeIndices(), constructNewNodesArray(), and simplifyMesh().


The documentation for this class was generated from the following files: