From 2a83b57f005cb50ee6bdd1f9d716bbd040a3d77b Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 28 Jul 2023 21:56:04 +0200 Subject: [PATCH 1/9] [MTL] Simplify return values construction Adding an error in case of invalid input pair. --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 219 +++++++--------------- 1 file changed, 71 insertions(+), 148 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index 315d4fb429f..3529f0fed8a 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -398,177 +398,107 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, /// forming an edge in a Hex. std::array lutHexCuttingQuadNodes(unsigned id1, unsigned id2) { - std::array nodes{}; if (id1 == 0 && id2 == 1) { - nodes[0] = 3; - nodes[1] = 2; - nodes[2] = 5; - nodes[3] = 4; + return {3, 2, 5, 4}; } - else if (id1 == 1 && id2 == 2) + if (id1 == 1 && id2 == 2) { - nodes[0] = 0; - nodes[1] = 3; - nodes[2] = 6; - nodes[3] = 5; + return {0, 3, 6, 5}; } - else if (id1 == 2 && id2 == 3) + if (id1 == 2 && id2 == 3) { - nodes[0] = 1; - nodes[1] = 0; - nodes[2] = 7; - nodes[3] = 6; + return {1, 0, 7, 6}; } - else if (id1 == 3 && id2 == 0) + if (id1 == 3 && id2 == 0) { - nodes[0] = 2; - nodes[1] = 1; - nodes[2] = 4; - nodes[3] = 7; + return {2, 1, 4, 7}; } - else if (id1 == 4 && id2 == 5) + if (id1 == 4 && id2 == 5) { - nodes[0] = 0; - nodes[1] = 1; - nodes[2] = 6; - nodes[3] = 7; + return {0, 1, 6, 7}; } - else if (id1 == 5 && id2 == 6) + if (id1 == 5 && id2 == 6) { - nodes[0] = 1; - nodes[1] = 2; - nodes[2] = 7; - nodes[3] = 4; + return {1, 2, 7, 4}; } - else if (id1 == 6 && id2 == 7) + if (id1 == 6 && id2 == 7) { - nodes[0] = 2; - nodes[1] = 3; - nodes[2] = 4; - nodes[3] = 5; + return {2, 3, 4, 5}; } - else if (id1 == 7 && id2 == 4) + if (id1 == 7 && id2 == 4) { - nodes[0] = 3; - nodes[1] = 0; - nodes[2] = 5; - nodes[3] = 6; + return {3, 0, 5, 6}; } - else if (id1 == 0 && id2 == 4) + if (id1 == 0 && id2 == 4) { - nodes[0] = 3; - nodes[1] = 7; - nodes[2] = 5; - nodes[3] = 1; + return {3, 7, 5, 1}; } - else if (id1 == 1 && id2 == 5) + if (id1 == 1 && id2 == 5) { - nodes[0] = 0; - nodes[1] = 4; - nodes[2] = 6; - nodes[3] = 2; + return {0, 4, 6, 2}; } - else if (id1 == 2 && id2 == 6) + if (id1 == 2 && id2 == 6) { - nodes[0] = 1; - nodes[1] = 5; - nodes[2] = 7; - nodes[3] = 3; + return {1, 5, 7, 3}; } - else if (id1 == 3 && id2 == 7) + if (id1 == 3 && id2 == 7) { - nodes[0] = 2; - nodes[1] = 6; - nodes[2] = 4; - nodes[3] = 0; + return {2, 6, 4, 0}; } - - else if (id1 == 1 && id2 == 0) + if (id1 == 1 && id2 == 0) { - nodes[0] = 2; - nodes[1] = 3; - nodes[2] = 4; - nodes[3] = 5; + return {2, 3, 4, 5}; } - else if (id1 == 2 && id2 == 1) + if (id1 == 2 && id2 == 1) { - nodes[0] = 3; - nodes[1] = 0; - nodes[2] = 5; - nodes[3] = 6; + return {3, 0, 5, 6}; } - else if (id1 == 3 && id2 == 2) + if (id1 == 3 && id2 == 2) { - nodes[0] = 0; - nodes[1] = 1; - nodes[2] = 6; - nodes[3] = 7; + return {0, 1, 6, 7}; } - else if (id1 == 0 && id2 == 3) + if (id1 == 0 && id2 == 3) { - nodes[0] = 1; - nodes[1] = 2; - nodes[2] = 7; - nodes[3] = 4; + return {1, 2, 7, 4}; } - else if (id1 == 5 && id2 == 4) + if (id1 == 5 && id2 == 4) { - nodes[0] = 1; - nodes[1] = 0; - nodes[2] = 7; - nodes[3] = 6; + return {1, 0, 7, 6}; } - else if (id1 == 6 && id2 == 5) + if (id1 == 6 && id2 == 5) { - nodes[0] = 2; - nodes[1] = 1; - nodes[2] = 4; - nodes[3] = 7; + return {2, 1, 4, 7}; } - else if (id1 == 7 && id2 == 6) + if (id1 == 7 && id2 == 6) { - nodes[0] = 3; - nodes[1] = 2; - nodes[2] = 5; - nodes[3] = 4; + return {3, 2, 5, 4}; } - else if (id1 == 4 && id2 == 7) + if (id1 == 4 && id2 == 7) { - nodes[0] = 0; - nodes[1] = 3; - nodes[2] = 6; - nodes[3] = 5; + return {0, 3, 6, 5}; } - else if (id1 == 4 && id2 == 0) + if (id1 == 4 && id2 == 0) { - nodes[0] = 7; - nodes[1] = 3; - nodes[2] = 1; - nodes[3] = 5; + return {7, 3, 1, 5}; } - else if (id1 == 5 && id2 == 1) + if (id1 == 5 && id2 == 1) { - nodes[0] = 4; - nodes[1] = 0; - nodes[2] = 2; - nodes[3] = 6; + return {4, 0, 2, 6}; } - else if (id1 == 6 && id2 == 2) + if (id1 == 6 && id2 == 2) { - nodes[0] = 5; - nodes[1] = 1; - nodes[2] = 3; - nodes[3] = 7; + return {5, 1, 3, 7}; } - else if (id1 == 7 && id2 == 3) + if (id1 == 7 && id2 == 3) { - nodes[0] = 6; - nodes[1] = 2; - nodes[2] = 0; - nodes[3] = 4; + return {6, 2, 0, 4}; } - return nodes; + + OGS_FATAL( + "lutHexCuttingQuadNodes() for nodes {} and {} does not have a valid " + "return value.", + id1, id2); } /// Lookup-table for returning the diametral node id of the given node id in a @@ -589,50 +519,43 @@ std::pair lutHexBackNodes(unsigned const i, unsigned const l) { // collapsed edges are *not* connected - std::pair back(std::numeric_limits::max(), - std::numeric_limits::max()); if (lutHexDiametralNode(i) == k) { - back.first = i; - back.second = lutHexDiametralNode(l); + return {i, lutHexDiametralNode(l)}; } - else if (lutHexDiametralNode(i) == l) + if (lutHexDiametralNode(i) == l) { - back.first = i; - back.second = lutHexDiametralNode(k); + return {i, lutHexDiametralNode(k)}; } - else if (lutHexDiametralNode(j) == k) + if (lutHexDiametralNode(j) == k) { - back.first = j; - back.second = lutHexDiametralNode(l); + return {j, lutHexDiametralNode(l)}; } - else if (lutHexDiametralNode(j) == l) + if (lutHexDiametralNode(j) == l) { - back.first = j; - back.second = lutHexDiametralNode(k); + return {j, lutHexDiametralNode(k)}; } + // collapsed edges *are* connected - else if (i == k) + if (i == k) { - back.first = lutHexDiametralNode(l); - back.second = j; + return {lutHexDiametralNode(l), j}; } - else if (i == l) + if (i == l) { - back.first = lutHexDiametralNode(k); - back.second = j; + return {lutHexDiametralNode(k), j}; } - else if (j == k) + if (j == k) { - back.first = lutHexDiametralNode(l); - back.second = i; + return {lutHexDiametralNode(l), i}; } - else if (j == l) + if (j == l) { - back.first = lutHexDiametralNode(k); - back.second = i; + return {lutHexDiametralNode(k), i}; } - return back; + + return {std::numeric_limits::max(), + std::numeric_limits::max()}; } // In an element with 5 unique nodes, return the node that will be the top of From 080a4e8e2d389f30863d6870ed8f9897324abf56 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Fri, 28 Jul 2023 22:48:50 +0200 Subject: [PATCH 2/9] [MTL] constify everything possible --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 157 +++++++++++----------- 1 file changed, 76 insertions(+), 81 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index 3529f0fed8a..efd8610ca7c 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -29,7 +29,7 @@ namespace MeshToolsLib { /// Lookup-table for returning the third node of bottom or top triangle given /// the other two. -unsigned lutPrismThirdNode(unsigned id1, unsigned id2) +unsigned lutPrismThirdNode(unsigned const id1, unsigned const id2) { if ((id1 == 0 && id2 == 1) || (id1 == 1 && id2 == 0)) { @@ -84,15 +84,15 @@ unsigned subdividePrism(MeshLib::Element const* const prism, std::vector const& nodes, std::vector& new_elements) { - auto addTetrahedron = - [&prism, &nodes, &new_elements](std::size_t id0, std::size_t id1, - std::size_t id2, std::size_t id3) - { - std::array tet_nodes; - tet_nodes[0] = nodes[prism->getNode(id0)->getID()]; - tet_nodes[1] = nodes[prism->getNode(id1)->getID()]; - tet_nodes[2] = nodes[prism->getNode(id2)->getID()]; - tet_nodes[3] = nodes[prism->getNode(id3)->getID()]; + auto addTetrahedron = [&prism, &nodes, &new_elements]( + std::size_t const id0, std::size_t const id1, + std::size_t const id2, std::size_t const id3) + { + std::array const tet_nodes = { + nodes[prism->getNode(id0)->getID()], + nodes[prism->getNode(id1)->getID()], + nodes[prism->getNode(id2)->getID()], + nodes[prism->getNode(id3)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet_nodes)); }; @@ -110,24 +110,20 @@ unsigned subdivideHex(MeshLib::Element const* const hex, std::vector const& nodes, std::vector& new_elements) { - std::array prism1_nodes; - prism1_nodes[0] = nodes[hex->getNode(0)->getID()]; - prism1_nodes[1] = nodes[hex->getNode(2)->getID()]; - prism1_nodes[2] = nodes[hex->getNode(1)->getID()]; - prism1_nodes[3] = nodes[hex->getNode(4)->getID()]; - prism1_nodes[4] = nodes[hex->getNode(6)->getID()]; - prism1_nodes[5] = nodes[hex->getNode(5)->getID()]; + std::array const prism1_nodes = { + nodes[hex->getNode(0)->getID()], nodes[hex->getNode(2)->getID()], + nodes[hex->getNode(1)->getID()], nodes[hex->getNode(4)->getID()], + nodes[hex->getNode(6)->getID()], nodes[hex->getNode(5)->getID()]}; + auto* prism1(new MeshLib::Prism(prism1_nodes)); subdividePrism(prism1, nodes, new_elements); delete prism1; - std::array prism2_nodes; - prism2_nodes[0] = nodes[hex->getNode(4)->getID()]; - prism2_nodes[1] = nodes[hex->getNode(6)->getID()]; - prism2_nodes[2] = nodes[hex->getNode(7)->getID()]; - prism2_nodes[3] = nodes[hex->getNode(0)->getID()]; - prism2_nodes[4] = nodes[hex->getNode(2)->getID()]; - prism2_nodes[5] = nodes[hex->getNode(3)->getID()]; + std::array const prism2_nodes = { + nodes[hex->getNode(4)->getID()], nodes[hex->getNode(6)->getID()], + nodes[hex->getNode(7)->getID()], nodes[hex->getNode(0)->getID()], + nodes[hex->getNode(2)->getID()], nodes[hex->getNode(3)->getID()]}; + auto* prism2(new MeshLib::Prism(prism2_nodes)); subdividePrism(prism2, nodes, new_elements); delete prism2; @@ -140,15 +136,15 @@ unsigned subdividePyramid(MeshLib::Element const* const pyramid, std::vector const& nodes, std::vector& new_elements) { - auto addTetrahedron = - [&pyramid, &nodes, &new_elements](std::size_t id0, std::size_t id1, - std::size_t id2, std::size_t id3) - { - std::array tet_nodes; - tet_nodes[0] = nodes[pyramid->getNode(id0)->getID()]; - tet_nodes[1] = nodes[pyramid->getNode(id1)->getID()]; - tet_nodes[2] = nodes[pyramid->getNode(id2)->getID()]; - tet_nodes[3] = nodes[pyramid->getNode(id3)->getID()]; + auto addTetrahedron = [&pyramid, &nodes, &new_elements]( + std::size_t const id0, std::size_t const id1, + std::size_t const id2, std::size_t const id3) + { + std::array const tet_nodes = { + nodes[pyramid->getNode(id0)->getID()], + nodes[pyramid->getNode(id1)->getID()], + nodes[pyramid->getNode(id2)->getID()], + nodes[pyramid->getNode(id3)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet_nodes)); }; @@ -277,10 +273,10 @@ MeshLib::Element* constructFourNodeElement( /// Reduces a pyramid element by removing collapsed nodes and constructing a new /// elements from the remaining nodes. void reducePyramid(MeshLib::Element const* const org_elem, - unsigned n_unique_nodes, + unsigned const n_unique_nodes, std::vector const& nodes, std::vector& new_elements, - unsigned min_elem_dim) + unsigned const min_elem_dim) { if (n_unique_nodes == 4) { @@ -305,19 +301,19 @@ void reducePyramid(MeshLib::Element const* const org_elem, /// two new elements from the remaining nodes. /// @return The number of newly created elements unsigned reducePrism(MeshLib::Element const* const org_elem, - unsigned n_unique_nodes, + unsigned const n_unique_nodes, std::vector const& nodes, std::vector& new_elements, - unsigned min_elem_dim) + unsigned const min_elem_dim) { - auto addTetrahedron = - [&org_elem, &nodes, &new_elements](std::size_t id0, std::size_t id1, - std::size_t id2, std::size_t id3) - { - std::array tet_nodes{nodes[org_elem->getNode(id0)->getID()], - nodes[org_elem->getNode(id1)->getID()], - nodes[org_elem->getNode(id2)->getID()], - nodes[org_elem->getNode(id3)->getID()]}; + auto addTetrahedron = [&org_elem, &nodes, &new_elements]( + std::size_t const id0, std::size_t const id1, + std::size_t const id2, std::size_t const id3) + { + std::array const tet_nodes = {nodes[org_elem->getNode(id0)->getID()], + nodes[org_elem->getNode(id1)->getID()], + nodes[org_elem->getNode(id2)->getID()], + nodes[org_elem->getNode(id3)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet_nodes)); }; @@ -376,7 +372,7 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, } else if (n_unique_nodes == 4) { - MeshLib::Element* elem( + MeshLib::Element* const elem( constructFourNodeElement(org_elem, nodes, min_elem_dim)); if (elem) { @@ -587,10 +583,10 @@ unsigned findPyramidTopNode(MeshLib::Element const& element, /// one or more new elements from the remaining nodes. /// @return The number of newly created elements unsigned reduceHex(MeshLib::Element const* const org_elem, - unsigned n_unique_nodes, + unsigned const n_unique_nodes, std::vector const& nodes, std::vector& new_elements, - unsigned min_elem_dim) + unsigned const min_elem_dim) { // TODO? // if two diametral nodes collapse, all kinds of bizarre (2D-)element @@ -608,7 +604,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, { const std::array base_nodes( lutHexCuttingQuadNodes(i, j)); - std::array pyr_nodes{ + std::array const pyr_nodes{ nodes[org_elem->getNode(base_nodes[0])->getID()], nodes[org_elem->getNode(base_nodes[1])->getID()], nodes[org_elem->getNode(base_nodes[2])->getID()], @@ -620,7 +616,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, { std::swap(i, j); } - std::array prism_nodes{ + std::array const prism_nodes{ nodes[org_elem->getNode(base_nodes[0])->getID()], nodes[org_elem->getNode(base_nodes[3])->getID()], nodes[org_elem->getNode(lutHexDiametralNode(j)) @@ -644,7 +640,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, if (face->getNode(0)->getID() == face->getNode(1)->getID() && face->getNode(2)->getID() == face->getNode(3)->getID()) { - std::array prism_nodes{ + std::array const prism_nodes{ nodes[org_elem ->getNode(lutHexDiametralNode(getNodeIDinElement( *org_elem, face->getNode(0)))) @@ -676,7 +672,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, if (face->getNode(0)->getID() == face->getNode(3)->getID() && face->getNode(1)->getID() == face->getNode(2)->getID()) { - std::array prism_nodes{ + std::array const prism_nodes{ nodes[org_elem ->getNode(lutHexDiametralNode(getNodeIDinElement( *org_elem, face->getNode(0)))) @@ -737,10 +733,10 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, return 0; } - std::array cutting_plane( + std::array const cutting_plane( lutHexCuttingQuadNodes(back.first, back.second)); - std::array pris1_nodes{ + std::array const pris1_nodes{ const_cast( org_elem->getNode(back.first)), const_cast( @@ -759,7 +755,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, min_elem_dim); delete prism1; - std::array pris2_nodes{ + std::array const pris2_nodes{ const_cast( org_elem->getNode( lutHexDiametralNode(back.first))), @@ -790,21 +786,21 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, else if (n_unique_nodes == 5) { MeshLib::Element* tet1(constructFourNodeElement(org_elem, nodes)); - std::array first_four_nodes = { + std::array const first_four_nodes = { {tet1->getNode(0)->getID(), tet1->getNode(1)->getID(), tet1->getNode(2)->getID(), tet1->getNode(3)->getID()}}; - unsigned fifth_node(findPyramidTopNode(*org_elem, first_four_nodes)); + unsigned const fifth_node = + findPyramidTopNode(*org_elem, first_four_nodes); bool tet_changed(false); if (tet1->getGeomType() == MeshLib::MeshElemType::QUAD) { delete tet1; tet_changed = true; - auto** tet1_nodes = new MeshLib::Node*[4]; - tet1_nodes[0] = nodes[first_four_nodes[0]]; - tet1_nodes[1] = nodes[first_four_nodes[1]]; - tet1_nodes[2] = nodes[first_four_nodes[2]]; - tet1_nodes[3] = nodes[org_elem->getNode(fifth_node)->getID()]; + std::array const tet1_nodes = { + nodes[first_four_nodes[0]], nodes[first_four_nodes[1]], + nodes[first_four_nodes[2]], + nodes[org_elem->getNode(fifth_node)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet1_nodes)); } else @@ -812,11 +808,11 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, new_elements.push_back(tet1); } - std::array tet2_nodes = {(tet_changed) ? nodes[first_four_nodes[0]] - : nodes[first_four_nodes[1]], - nodes[first_four_nodes[2]], - nodes[first_four_nodes[3]], - nodes[org_elem->getNode(fifth_node)->getID()]}; + std::array const tet2_nodes = { + (tet_changed) ? nodes[first_four_nodes[0]] + : nodes[first_four_nodes[1]], + nodes[first_four_nodes[2]], nodes[first_four_nodes[3]], + nodes[org_elem->getNode(fifth_node)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet2_nodes)); return 2; } @@ -876,10 +872,10 @@ std::size_t subdivideElement(MeshLib::Element const* const element, // Revises an element by removing collapsed nodes, using the nodes vector from // the result mesh. std::size_t reduceElement(MeshLib::Element const* const element, - unsigned n_unique_nodes, + unsigned const n_unique_nodes, std::vector const& nodes, std::vector& elements, - unsigned min_elem_dim) + unsigned const min_elem_dim) { /*************** * TODO: modify neighbouring elements if one elements has been subdivided @@ -946,8 +942,7 @@ unsigned getNumberOfUniqueNodes(MeshLib::Element const* const element) template void fillNodeProperty(std::vector& new_prop, std::vector const& old_prop, - std::vector - node_ids) + std::vector const& node_ids) { std::size_t const n_nodes = node_ids.size(); for (std::size_t i = 0; i < n_nodes; ++i) @@ -963,8 +958,7 @@ void fillNodeProperty(std::vector& new_prop, template void fillElemProperty(std::vector& new_prop, std::vector const& old_prop, - std::vector - elem_ids) + std::vector const& elem_ids) { std::transform(elem_ids.cbegin(), elem_ids.cend(), std::back_inserter(new_prop), @@ -1053,10 +1047,10 @@ namespace MeshToolsLib { MeshRevision::MeshRevision(MeshLib::Mesh& mesh) : _mesh(mesh) {} -unsigned MeshRevision::getNumberOfCollapsibleNodes(double eps) const +unsigned MeshRevision::getNumberOfCollapsibleNodes(double const eps) const { - std::vector id_map(this->collapseNodeIndices(eps)); - std::size_t nNodes(id_map.size()); + std::vector const id_map = collapseNodeIndices(eps); + std::size_t const nNodes = id_map.size(); unsigned count(0); for (std::size_t i = 0; i < nNodes; ++i) { @@ -1069,8 +1063,8 @@ unsigned MeshRevision::getNumberOfCollapsibleNodes(double eps) const } MeshLib::Mesh* MeshRevision::simplifyMesh(const std::string& new_mesh_name, - double eps, - unsigned min_elem_dim) const + double const eps, + unsigned const min_elem_dim) const { if (this->_mesh.getNumberOfElements() == 0) { @@ -1091,7 +1085,7 @@ MeshLib::Mesh* MeshRevision::simplifyMesh(const std::string& new_mesh_name, if (n_unique_nodes == elem->getNumberOfBaseNodes() && elem->getDimension() >= min_elem_dim) { - ElementErrorCode e(elem->validate()); + ElementErrorCode const e = elem->validate(); if (e[ElementErrorFlag::NonCoplanar]) { std::size_t const n_new_elements( @@ -1139,7 +1133,8 @@ MeshLib::Mesh* MeshRevision::simplifyMesh(const std::string& new_mesh_name, return nullptr; } -std::vector MeshRevision::collapseNodeIndices(double eps) const +std::vector MeshRevision::collapseNodeIndices( + double const eps) const { const std::vector& nodes(_mesh.getNodes()); const std::size_t nNodes(_mesh.getNumberOfNodes()); @@ -1157,7 +1152,7 @@ std::vector MeshRevision::collapseNodeIndices(double eps) const { continue; } - std::vector const*> node_vectors( + std::vector const*> const node_vectors( grid.getPntVecsOfGridCellsIntersectingCube(*node, half_eps)); const std::size_t nVectors(node_vectors.size()); From e669152f2e236442db25b5b0c94eb53d781d520c Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sun, 30 Jul 2023 13:39:50 +0200 Subject: [PATCH 3/9] [MTL] Extract createTetrahedron() --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 91 ++++++++++++----------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index efd8610ca7c..d2654728621 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -15,6 +15,9 @@ #include "MeshRevision.h" #include +#include +#include +#include #include "BaseLib/Algorithm.h" #include "BaseLib/Logging.h" @@ -79,28 +82,40 @@ unsigned subdivideQuad(MeshLib::Element const* const quad, return 2; } +std::unique_ptr createTetrahedron( + std::span const element_nodes, + std::vector const& nodes, + std::array const local_ids) +{ + using namespace MeshLib::views; + auto lookup_in = [](auto const& values) + { + return ranges::views::transform([&values](std::size_t const n) + { return values[n]; }); + }; + + std::array tet_nodes; + ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), + begin(tet_nodes)); + + return std::make_unique(tet_nodes); +} + /// Subdivides a prism with nonplanar quad faces into two tets. unsigned subdividePrism(MeshLib::Element const* const prism, std::vector const& nodes, std::vector& new_elements) { - auto addTetrahedron = [&prism, &nodes, &new_elements]( - std::size_t const id0, std::size_t const id1, - std::size_t const id2, std::size_t const id3) - { - std::array const tet_nodes = { - nodes[prism->getNode(id0)->getID()], - nodes[prism->getNode(id1)->getID()], - nodes[prism->getNode(id2)->getID()], - nodes[prism->getNode(id3)->getID()]}; - new_elements.push_back(new MeshLib::Tet(tet_nodes)); + auto addTetrahedron = + [&prism, &nodes, &new_elements](std::array const ids) + { + new_elements.push_back( + createTetrahedron(prism->nodes(), nodes, ids).release()); }; - addTetrahedron(0, 1, 2, 3); - - addTetrahedron(3, 2, 4, 5); - - addTetrahedron(2, 1, 3, 4); + addTetrahedron({0, 1, 2, 3}); + addTetrahedron({3, 2, 4, 5}); + addTetrahedron({2, 1, 3, 4}); return 3; } @@ -136,21 +151,15 @@ unsigned subdividePyramid(MeshLib::Element const* const pyramid, std::vector const& nodes, std::vector& new_elements) { - auto addTetrahedron = [&pyramid, &nodes, &new_elements]( - std::size_t const id0, std::size_t const id1, - std::size_t const id2, std::size_t const id3) - { - std::array const tet_nodes = { - nodes[pyramid->getNode(id0)->getID()], - nodes[pyramid->getNode(id1)->getID()], - nodes[pyramid->getNode(id2)->getID()], - nodes[pyramid->getNode(id3)->getID()]}; - new_elements.push_back(new MeshLib::Tet(tet_nodes)); + auto addTetrahedron = + [&pyramid, &nodes, &new_elements](std::array const ids) + { + new_elements.push_back( + createTetrahedron(pyramid->nodes(), nodes, ids).release()); }; - addTetrahedron(0, 1, 2, 4); - - addTetrahedron(0, 2, 3, 4); + addTetrahedron({0, 1, 2, 4}); + addTetrahedron({0, 2, 3, 4}); return 2; } @@ -306,15 +315,11 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, std::vector& new_elements, unsigned const min_elem_dim) { - auto addTetrahedron = [&org_elem, &nodes, &new_elements]( - std::size_t const id0, std::size_t const id1, - std::size_t const id2, std::size_t const id3) - { - std::array const tet_nodes = {nodes[org_elem->getNode(id0)->getID()], - nodes[org_elem->getNode(id1)->getID()], - nodes[org_elem->getNode(id2)->getID()], - nodes[org_elem->getNode(id3)->getID()]}; - new_elements.push_back(new MeshLib::Tet(tet_nodes)); + auto addTetrahedron = + [&org_elem, &nodes, &new_elements](std::array const ids) + { + new_elements.push_back( + createTetrahedron(org_elem->nodes(), nodes, ids).release()); }; // TODO? @@ -336,10 +341,10 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, // non triangle edge collapsed if (i % 3 == j % 3) { - addTetrahedron((i + 1) % 3, (i + 2) % 3, i, - (i + 1) % 3 + 3); - addTetrahedron((i + 1) % 3 + 3, (i + 2) % 3, i, - (i + 2) % 3 + 3); + addTetrahedron( + {(i + 1) % 3, (i + 2) % 3, i, (i + 1) % 3 + 3}); + addTetrahedron( + {(i + 1) % 3 + 3, (i + 2) % 3, i, (i + 2) % 3 + 3}); return 2; } @@ -354,7 +359,7 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, } const unsigned k_offset = (i > 2) ? k - 3 : k + 3; - addTetrahedron(i_offset, j_offset, k_offset, i); + addTetrahedron({i_offset, j_offset, k_offset, i}); const unsigned l = (MathLib::isCoplanar(*org_elem->getNode(i_offset), @@ -364,7 +369,7 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, ? j : i; const unsigned l_offset = (i > 2) ? l - 3 : l + 3; - addTetrahedron(l_offset, k_offset, i, k); + addTetrahedron({l_offset, k_offset, i, k}); return 2; } } From 3e2d854f12f7bd028bdd1e2591b8d135e3c8b192 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sun, 30 Jul 2023 13:57:11 +0200 Subject: [PATCH 4/9] [MTL] Extract createPrism() --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 193 +++++++++------------- 1 file changed, 78 insertions(+), 115 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index d2654728621..8dac4b99726 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -101,6 +101,25 @@ std::unique_ptr createTetrahedron( return std::make_unique(tet_nodes); } +std::unique_ptr createPrism( + std::span const element_nodes, + std::vector const& nodes, + std::array const local_ids) +{ + using namespace MeshLib::views; + auto lookup_in = [](auto const& values) + { + return ranges::views::transform([&values](std::size_t const n) + { return values[n]; }); + }; + + std::array prism_nodes; + ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), + begin(prism_nodes)); + + return std::make_unique(prism_nodes); +} + /// Subdivides a prism with nonplanar quad faces into two tets. unsigned subdividePrism(MeshLib::Element const* const prism, std::vector const& nodes, @@ -125,23 +144,11 @@ unsigned subdivideHex(MeshLib::Element const* const hex, std::vector const& nodes, std::vector& new_elements) { - std::array const prism1_nodes = { - nodes[hex->getNode(0)->getID()], nodes[hex->getNode(2)->getID()], - nodes[hex->getNode(1)->getID()], nodes[hex->getNode(4)->getID()], - nodes[hex->getNode(6)->getID()], nodes[hex->getNode(5)->getID()]}; - - auto* prism1(new MeshLib::Prism(prism1_nodes)); - subdividePrism(prism1, nodes, new_elements); - delete prism1; + auto prism1 = createPrism(hex->nodes(), nodes, {0, 2, 1, 4, 6, 5}); + subdividePrism(prism1.get(), nodes, new_elements); - std::array const prism2_nodes = { - nodes[hex->getNode(4)->getID()], nodes[hex->getNode(6)->getID()], - nodes[hex->getNode(7)->getID()], nodes[hex->getNode(0)->getID()], - nodes[hex->getNode(2)->getID()], nodes[hex->getNode(3)->getID()]}; - - auto* prism2(new MeshLib::Prism(prism2_nodes)); - subdividePrism(prism2, nodes, new_elements); - delete prism2; + auto prism2 = createPrism(hex->nodes(), nodes, {4, 6, 7, 0, 2, 3}); + subdividePrism(prism2.get(), nodes, new_elements); return 6; } @@ -621,16 +628,12 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, { std::swap(i, j); } - std::array const prism_nodes{ - nodes[org_elem->getNode(base_nodes[0])->getID()], - nodes[org_elem->getNode(base_nodes[3])->getID()], - nodes[org_elem->getNode(lutHexDiametralNode(j)) - ->getID()], - nodes[org_elem->getNode(base_nodes[1])->getID()], - nodes[org_elem->getNode(base_nodes[2])->getID()], - nodes[org_elem->getNode(lutHexDiametralNode(i)) - ->getID()]}; - new_elements.push_back(new MeshLib::Prism(prism_nodes)); + std::array const prism_nodes{ + base_nodes[0], base_nodes[3], lutHexDiametralNode(j), + base_nodes[1], base_nodes[2], lutHexDiametralNode(i)}; + new_elements.push_back( + createPrism(org_elem->nodes(), nodes, prism_nodes) + .release()); return 2; } } @@ -645,64 +648,41 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, if (face->getNode(0)->getID() == face->getNode(1)->getID() && face->getNode(2)->getID() == face->getNode(3)->getID()) { - std::array const prism_nodes{ - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(0)))) - ->getID()], - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(1)))) - ->getID()], - nodes[org_elem - ->getNode(getNodeIDinElement(*org_elem, - face->getNode(2))) - ->getID()], - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(2)))) - ->getID()], - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(3)))) - ->getID()], - nodes[org_elem - ->getNode(getNodeIDinElement(*org_elem, - face->getNode(0))) - ->getID()]}; - new_elements.push_back(new MeshLib::Prism(prism_nodes)); + std::array const prism_nodes{ + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(0))), + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(1))), + getNodeIDinElement(*org_elem, face->getNode(2)), + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(2))), + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(3))), + getNodeIDinElement(*org_elem, face->getNode(0))}; + + new_elements.push_back( + createPrism(org_elem->nodes(), nodes, prism_nodes) + .release()); delete face; return 1; } if (face->getNode(0)->getID() == face->getNode(3)->getID() && face->getNode(1)->getID() == face->getNode(2)->getID()) { - std::array const prism_nodes{ - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(0)))) - ->getID()], - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(3)))) - ->getID()], - nodes[org_elem - ->getNode(getNodeIDinElement(*org_elem, - face->getNode(2))) - ->getID()], - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(1)))) - ->getID()], - nodes[org_elem - ->getNode(lutHexDiametralNode(getNodeIDinElement( - *org_elem, face->getNode(2)))) - ->getID()], - nodes[org_elem - ->getNode(getNodeIDinElement(*org_elem, - face->getNode(0))) - ->getID()]}; - new_elements.push_back(new MeshLib::Prism(prism_nodes)); + std::array const prism_nodes{ + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(0))), + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(3))), + getNodeIDinElement(*org_elem, face->getNode(2)), + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(1))), + lutHexDiametralNode( + getNodeIDinElement(*org_elem, face->getNode(2))), + getNodeIDinElement(*org_elem, face->getNode(0))}; + new_elements.push_back( + createPrism(org_elem->nodes(), nodes, prism_nodes) + .release()); delete face; return 1; } @@ -741,45 +721,28 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, std::array const cutting_plane( lutHexCuttingQuadNodes(back.first, back.second)); - std::array const pris1_nodes{ - const_cast( - org_elem->getNode(back.first)), - const_cast( - org_elem->getNode(cutting_plane[0])), - const_cast( - org_elem->getNode(cutting_plane[3])), - const_cast( - org_elem->getNode(back.second)), - const_cast( - org_elem->getNode(cutting_plane[1])), - const_cast( - org_elem->getNode(cutting_plane[2]))}; - auto* prism1(new MeshLib::Prism(pris1_nodes)); + std::array const pris1_nodes{ + back.first, cutting_plane[0], + cutting_plane[3], back.second, + cutting_plane[1], cutting_plane[2]}; + auto prism1 = createPrism(org_elem->nodes(), + nodes, pris1_nodes); unsigned nNewElements = - reducePrism(prism1, 5, nodes, new_elements, - min_elem_dim); - delete prism1; - - std::array const pris2_nodes{ - const_cast( - org_elem->getNode( - lutHexDiametralNode(back.first))), - const_cast( - org_elem->getNode(cutting_plane[0])), - const_cast( - org_elem->getNode(cutting_plane[3])), - const_cast( - org_elem->getNode( - lutHexDiametralNode(back.second))), - const_cast( - org_elem->getNode(cutting_plane[1])), - const_cast( - org_elem->getNode(cutting_plane[2]))}; - auto* prism2(new MeshLib::Prism(pris2_nodes)); + reducePrism(prism1.get(), 5, nodes, + new_elements, min_elem_dim); + + std::array const pris2_nodes{ + lutHexDiametralNode(back.first), + cutting_plane[0], + cutting_plane[3], + lutHexDiametralNode(back.second), + cutting_plane[1], + cutting_plane[2]}; + auto prism2 = createPrism(org_elem->nodes(), + nodes, pris2_nodes); nNewElements += - reducePrism(prism2, 5, nodes, new_elements, - min_elem_dim); - delete prism2; + reducePrism(prism2.get(), 5, nodes, + new_elements, min_elem_dim); return nNewElements; } } From cd902815c32b352708a9a9e67da1d64cbd754fb8 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Sun, 30 Jul 2023 22:33:39 +0200 Subject: [PATCH 5/9] [MTL] Extract createTriangle() --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 35 +++++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index 8dac4b99726..bcdc8bd241c 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -64,20 +64,37 @@ unsigned lutPrismThirdNode(unsigned const id1, unsigned const id2) namespace { +std::unique_ptr createTriangle( + std::span const element_nodes, + std::vector const& nodes, + std::array const local_ids) +{ + using namespace MeshLib::views; + auto lookup_in = [](auto const& values) + { + return ranges::views::transform([&values](std::size_t const n) + { return values[n]; }); + }; + + std::array tri_nodes; + ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), + begin(tri_nodes)); + + return std::make_unique(tri_nodes); +} + /// Subdivides a nonplanar quad into two triangles. unsigned subdivideQuad(MeshLib::Element const* const quad, std::vector const& nodes, std::vector& new_elements) { - std::array tri1_nodes{nodes[quad->getNode(0)->getID()], - nodes[quad->getNode(1)->getID()], - nodes[quad->getNode(2)->getID()]}; - new_elements.push_back(new MeshLib::Tri(tri1_nodes)); - - std::array tri2_nodes{nodes[quad->getNode(0)->getID()], - nodes[quad->getNode(2)->getID()], - nodes[quad->getNode(3)->getID()]}; - new_elements.push_back(new MeshLib::Tri(tri2_nodes)); + std::array const tri1_nodes{0, 1, 2}; + new_elements.push_back( + createTriangle(quad->nodes(), nodes, tri1_nodes).release()); + + std::array const tri2_nodes{0, 2, 3}; + new_elements.push_back( + createTriangle(quad->nodes(), nodes, tri2_nodes).release()); return 2; } From fe61f6266d0470a2338d84b7a15ee39a8186fefc Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Mon, 31 Jul 2023 11:09:55 +0200 Subject: [PATCH 6/9] [MTL] Extract createLine() --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 29 ++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index bcdc8bd241c..23dfe232eda 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -64,6 +64,25 @@ unsigned lutPrismThirdNode(unsigned const id1, unsigned const id2) namespace { +std::unique_ptr createLine( + std::span const element_nodes, + std::vector const& nodes, + std::array const local_ids) +{ + using namespace MeshLib::views; + auto lookup_in = [](auto const& values) + { + return ranges::views::transform([&values](std::size_t const n) + { return values[n]; }); + }; + + std::array prism_nodes; + ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), + begin(prism_nodes)); + + return std::make_unique(prism_nodes); +} + std::unique_ptr createTriangle( std::span const element_nodes, std::vector const& nodes, @@ -193,19 +212,17 @@ unsigned subdividePyramid(MeshLib::Element const* const pyramid, MeshLib::Element* constructLine(MeshLib::Element const* const element, const std::vector& nodes) { - std::array line_nodes; - line_nodes[0] = nodes[element->getNode(0)->getID()]; - line_nodes[1] = nullptr; + std::array line_nodes = {0, 0}; for (unsigned i = 1; i < element->getNumberOfBaseNodes(); ++i) { if (element->getNode(i)->getID() != element->getNode(0)->getID()) { - line_nodes[1] = nodes[element->getNode(i)->getID()]; + line_nodes[1] = i; break; } } - assert(line_nodes[1] != nullptr); - return new MeshLib::Line(line_nodes); + assert(line_nodes[1] != 0); + return createLine(element->nodes(), nodes, line_nodes).release(); } /// Creates a triangle element from the first three unique nodes found in the From 9ecee01b2f444e144a7b7ade6f563a88dda37cc8 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Mon, 31 Jul 2023 11:26:11 +0200 Subject: [PATCH 7/9] [MTL] Extract createPyramid() --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 32 ++++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index 23dfe232eda..c2079ce5e58 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -156,6 +156,25 @@ std::unique_ptr createPrism( return std::make_unique(prism_nodes); } +std::unique_ptr createPyramid( + std::span const element_nodes, + std::vector const& nodes, + std::array const local_ids) +{ + using namespace MeshLib::views; + auto lookup_in = [](auto const& values) + { + return ranges::views::transform([&values](std::size_t const n) + { return values[n]; }); + }; + + std::array prism_nodes; + ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), + begin(prism_nodes)); + + return std::make_unique(prism_nodes); +} + /// Subdivides a prism with nonplanar quad faces into two tets. unsigned subdividePrism(MeshLib::Element const* const prism, std::vector const& nodes, @@ -650,13 +669,12 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, { const std::array base_nodes( lutHexCuttingQuadNodes(i, j)); - std::array const pyr_nodes{ - nodes[org_elem->getNode(base_nodes[0])->getID()], - nodes[org_elem->getNode(base_nodes[1])->getID()], - nodes[org_elem->getNode(base_nodes[2])->getID()], - nodes[org_elem->getNode(base_nodes[3])->getID()], - nodes[org_elem->getNode(i)->getID()]}; - new_elements.push_back(new MeshLib::Pyramid(pyr_nodes)); + std::array const pyr_nodes = { + base_nodes[0], base_nodes[1], base_nodes[2], + base_nodes[3], i}; + new_elements.push_back( + createPyramid(org_elem->nodes(), nodes, pyr_nodes) + .release()); if (i < 4 && j >= 4) { From ef4918ed84734299c9c55672227c7ecbdcbe7e2c Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Mon, 31 Jul 2023 23:27:46 +0200 Subject: [PATCH 8/9] [MTL] Replace createElement function with template --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 130 ++++++---------------- 1 file changed, 33 insertions(+), 97 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index c2079ce5e58..7ec09493d39 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -64,10 +64,11 @@ unsigned lutPrismThirdNode(unsigned const id1, unsigned const id2) namespace { -std::unique_ptr createLine( +template +std::unique_ptr createElement( std::span const element_nodes, std::vector const& nodes, - std::array const local_ids) + std::array const local_ids) { using namespace MeshLib::views; auto lookup_in = [](auto const& values) @@ -76,30 +77,11 @@ std::unique_ptr createLine( { return values[n]; }); }; - std::array prism_nodes; + std::array new_nodes; ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), - begin(prism_nodes)); + begin(new_nodes)); - return std::make_unique(prism_nodes); -} - -std::unique_ptr createTriangle( - std::span const element_nodes, - std::vector const& nodes, - std::array const local_ids) -{ - using namespace MeshLib::views; - auto lookup_in = [](auto const& values) - { - return ranges::views::transform([&values](std::size_t const n) - { return values[n]; }); - }; - - std::array tri_nodes; - ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), - begin(tri_nodes)); - - return std::make_unique(tri_nodes); + return std::make_unique(new_nodes); } /// Subdivides a nonplanar quad into two triangles. @@ -109,72 +91,17 @@ unsigned subdivideQuad(MeshLib::Element const* const quad, { std::array const tri1_nodes{0, 1, 2}; new_elements.push_back( - createTriangle(quad->nodes(), nodes, tri1_nodes).release()); + createElement(quad->nodes(), nodes, tri1_nodes) + .release()); std::array const tri2_nodes{0, 2, 3}; new_elements.push_back( - createTriangle(quad->nodes(), nodes, tri2_nodes).release()); + createElement(quad->nodes(), nodes, tri2_nodes) + .release()); return 2; } -std::unique_ptr createTetrahedron( - std::span const element_nodes, - std::vector const& nodes, - std::array const local_ids) -{ - using namespace MeshLib::views; - auto lookup_in = [](auto const& values) - { - return ranges::views::transform([&values](std::size_t const n) - { return values[n]; }); - }; - - std::array tet_nodes; - ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), - begin(tet_nodes)); - - return std::make_unique(tet_nodes); -} - -std::unique_ptr createPrism( - std::span const element_nodes, - std::vector const& nodes, - std::array const local_ids) -{ - using namespace MeshLib::views; - auto lookup_in = [](auto const& values) - { - return ranges::views::transform([&values](std::size_t const n) - { return values[n]; }); - }; - - std::array prism_nodes; - ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), - begin(prism_nodes)); - - return std::make_unique(prism_nodes); -} - -std::unique_ptr createPyramid( - std::span const element_nodes, - std::vector const& nodes, - std::array const local_ids) -{ - using namespace MeshLib::views; - auto lookup_in = [](auto const& values) - { - return ranges::views::transform([&values](std::size_t const n) - { return values[n]; }); - }; - - std::array prism_nodes; - ranges::copy(local_ids | lookup_in(element_nodes) | ids | lookup_in(nodes), - begin(prism_nodes)); - - return std::make_unique(prism_nodes); -} - /// Subdivides a prism with nonplanar quad faces into two tets. unsigned subdividePrism(MeshLib::Element const* const prism, std::vector const& nodes, @@ -184,7 +111,7 @@ unsigned subdividePrism(MeshLib::Element const* const prism, [&prism, &nodes, &new_elements](std::array const ids) { new_elements.push_back( - createTetrahedron(prism->nodes(), nodes, ids).release()); + createElement(prism->nodes(), nodes, ids).release()); }; addTetrahedron({0, 1, 2, 3}); @@ -199,10 +126,12 @@ unsigned subdivideHex(MeshLib::Element const* const hex, std::vector const& nodes, std::vector& new_elements) { - auto prism1 = createPrism(hex->nodes(), nodes, {0, 2, 1, 4, 6, 5}); + auto prism1 = + createElement(hex->nodes(), nodes, {0, 2, 1, 4, 6, 5}); subdividePrism(prism1.get(), nodes, new_elements); - auto prism2 = createPrism(hex->nodes(), nodes, {4, 6, 7, 0, 2, 3}); + auto prism2 = + createElement(hex->nodes(), nodes, {4, 6, 7, 0, 2, 3}); subdividePrism(prism2.get(), nodes, new_elements); return 6; @@ -217,7 +146,8 @@ unsigned subdividePyramid(MeshLib::Element const* const pyramid, [&pyramid, &nodes, &new_elements](std::array const ids) { new_elements.push_back( - createTetrahedron(pyramid->nodes(), nodes, ids).release()); + createElement(pyramid->nodes(), nodes, ids) + .release()); }; addTetrahedron({0, 1, 2, 4}); @@ -241,7 +171,8 @@ MeshLib::Element* constructLine(MeshLib::Element const* const element, } } assert(line_nodes[1] != 0); - return createLine(element->nodes(), nodes, line_nodes).release(); + return createElement(element->nodes(), nodes, line_nodes) + .release(); } /// Creates a triangle element from the first three unique nodes found in the @@ -379,7 +310,8 @@ unsigned reducePrism(MeshLib::Element const* const org_elem, [&org_elem, &nodes, &new_elements](std::array const ids) { new_elements.push_back( - createTetrahedron(org_elem->nodes(), nodes, ids).release()); + createElement(org_elem->nodes(), nodes, ids) + .release()); }; // TODO? @@ -673,7 +605,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, base_nodes[0], base_nodes[1], base_nodes[2], base_nodes[3], i}; new_elements.push_back( - createPyramid(org_elem->nodes(), nodes, pyr_nodes) + createElement(org_elem->nodes(), + nodes, pyr_nodes) .release()); if (i < 4 && j >= 4) @@ -684,7 +617,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, base_nodes[0], base_nodes[3], lutHexDiametralNode(j), base_nodes[1], base_nodes[2], lutHexDiametralNode(i)}; new_elements.push_back( - createPrism(org_elem->nodes(), nodes, prism_nodes) + createElement(org_elem->nodes(), nodes, + prism_nodes) .release()); return 2; } @@ -713,7 +647,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, getNodeIDinElement(*org_elem, face->getNode(0))}; new_elements.push_back( - createPrism(org_elem->nodes(), nodes, prism_nodes) + createElement(org_elem->nodes(), nodes, + prism_nodes) .release()); delete face; return 1; @@ -733,7 +668,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, getNodeIDinElement(*org_elem, face->getNode(2))), getNodeIDinElement(*org_elem, face->getNode(0))}; new_elements.push_back( - createPrism(org_elem->nodes(), nodes, prism_nodes) + createElement(org_elem->nodes(), nodes, + prism_nodes) .release()); delete face; return 1; @@ -777,8 +713,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, back.first, cutting_plane[0], cutting_plane[3], back.second, cutting_plane[1], cutting_plane[2]}; - auto prism1 = createPrism(org_elem->nodes(), - nodes, pris1_nodes); + auto prism1 = createElement( + org_elem->nodes(), nodes, pris1_nodes); unsigned nNewElements = reducePrism(prism1.get(), 5, nodes, new_elements, min_elem_dim); @@ -790,8 +726,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, lutHexDiametralNode(back.second), cutting_plane[1], cutting_plane[2]}; - auto prism2 = createPrism(org_elem->nodes(), - nodes, pris2_nodes); + auto prism2 = createElement( + org_elem->nodes(), nodes, pris2_nodes); nNewElements += reducePrism(prism2.get(), 5, nodes, new_elements, min_elem_dim); From c3eab8b16c210be9f8f622429bb08c8065ef0013 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 3 Aug 2023 22:50:05 +0200 Subject: [PATCH 9/9] [MTL] Rename variables containing ids --- MeshToolsLib/MeshEditing/MeshRevision.cpp | 69 ++++++++++++----------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/MeshToolsLib/MeshEditing/MeshRevision.cpp b/MeshToolsLib/MeshEditing/MeshRevision.cpp index 7ec09493d39..a72e0d6e60b 100644 --- a/MeshToolsLib/MeshEditing/MeshRevision.cpp +++ b/MeshToolsLib/MeshEditing/MeshRevision.cpp @@ -89,14 +89,14 @@ unsigned subdivideQuad(MeshLib::Element const* const quad, std::vector const& nodes, std::vector& new_elements) { - std::array const tri1_nodes{0, 1, 2}; + std::array const tri1_node_ids{0, 1, 2}; new_elements.push_back( - createElement(quad->nodes(), nodes, tri1_nodes) + createElement(quad->nodes(), nodes, tri1_node_ids) .release()); - std::array const tri2_nodes{0, 2, 3}; + std::array const tri2_node_ids{0, 2, 3}; new_elements.push_back( - createElement(quad->nodes(), nodes, tri2_nodes) + createElement(quad->nodes(), nodes, tri2_node_ids) .release()); return 2; @@ -161,17 +161,17 @@ unsigned subdividePyramid(MeshLib::Element const* const pyramid, MeshLib::Element* constructLine(MeshLib::Element const* const element, const std::vector& nodes) { - std::array line_nodes = {0, 0}; + std::array line_node_ids = {0, 0}; for (unsigned i = 1; i < element->getNumberOfBaseNodes(); ++i) { if (element->getNode(i)->getID() != element->getNode(0)->getID()) { - line_nodes[1] = i; + line_node_ids[1] = i; break; } } - assert(line_nodes[1] != 0); - return createElement(element->nodes(), nodes, line_nodes) + assert(line_node_ids[1] != 0); + return createElement(element->nodes(), nodes, line_node_ids) .release(); } @@ -498,10 +498,10 @@ std::array lutHexCuttingQuadNodes(unsigned id1, unsigned id2) /// Hex. unsigned lutHexDiametralNode(unsigned const id) { - constexpr std::array hex_diametral_nodes = { + constexpr std::array hex_diametral_node_ids = { {6, 7, 4, 5, 2, 3, 0, 1}}; - return hex_diametral_nodes[id]; + return hex_diametral_node_ids[id]; } /// When a hex is subdivided into two prisms, this returns the nodes of the hex @@ -599,26 +599,27 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, if (org_elem->getNode(i)->getID() == org_elem->getNode(j)->getID()) { - const std::array base_nodes( + const std::array base_node_ids( lutHexCuttingQuadNodes(i, j)); - std::array const pyr_nodes = { - base_nodes[0], base_nodes[1], base_nodes[2], - base_nodes[3], i}; + std::array const pyr_node_ids = { + base_node_ids[0], base_node_ids[1], base_node_ids[2], + base_node_ids[3], i}; new_elements.push_back( createElement(org_elem->nodes(), - nodes, pyr_nodes) + nodes, pyr_node_ids) .release()); if (i < 4 && j >= 4) { std::swap(i, j); } - std::array const prism_nodes{ - base_nodes[0], base_nodes[3], lutHexDiametralNode(j), - base_nodes[1], base_nodes[2], lutHexDiametralNode(i)}; + std::array const prism_node_ids{ + base_node_ids[0], base_node_ids[3], + lutHexDiametralNode(j), base_node_ids[1], + base_node_ids[2], lutHexDiametralNode(i)}; new_elements.push_back( createElement(org_elem->nodes(), nodes, - prism_nodes) + prism_node_ids) .release()); return 2; } @@ -634,7 +635,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, if (face->getNode(0)->getID() == face->getNode(1)->getID() && face->getNode(2)->getID() == face->getNode(3)->getID()) { - std::array const prism_nodes{ + std::array const prism_node_ids{ lutHexDiametralNode( getNodeIDinElement(*org_elem, face->getNode(0))), lutHexDiametralNode( @@ -648,7 +649,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, new_elements.push_back( createElement(org_elem->nodes(), nodes, - prism_nodes) + prism_node_ids) .release()); delete face; return 1; @@ -656,7 +657,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, if (face->getNode(0)->getID() == face->getNode(3)->getID() && face->getNode(1)->getID() == face->getNode(2)->getID()) { - std::array const prism_nodes{ + std::array const prism_node_ids{ lutHexDiametralNode( getNodeIDinElement(*org_elem, face->getNode(0))), lutHexDiametralNode( @@ -669,7 +670,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, getNodeIDinElement(*org_elem, face->getNode(0))}; new_elements.push_back( createElement(org_elem->nodes(), nodes, - prism_nodes) + prism_node_ids) .release()); delete face; return 1; @@ -709,17 +710,17 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, std::array const cutting_plane( lutHexCuttingQuadNodes(back.first, back.second)); - std::array const pris1_nodes{ + std::array const pris1_node_ids{ back.first, cutting_plane[0], cutting_plane[3], back.second, cutting_plane[1], cutting_plane[2]}; auto prism1 = createElement( - org_elem->nodes(), nodes, pris1_nodes); + org_elem->nodes(), nodes, pris1_node_ids); unsigned nNewElements = reducePrism(prism1.get(), 5, nodes, new_elements, min_elem_dim); - std::array const pris2_nodes{ + std::array const pris2_node_ids{ lutHexDiametralNode(back.first), cutting_plane[0], cutting_plane[3], @@ -727,7 +728,7 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, cutting_plane[1], cutting_plane[2]}; auto prism2 = createElement( - org_elem->nodes(), nodes, pris2_nodes); + org_elem->nodes(), nodes, pris2_node_ids); nNewElements += reducePrism(prism2.get(), 5, nodes, new_elements, min_elem_dim); @@ -742,11 +743,11 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, else if (n_unique_nodes == 5) { MeshLib::Element* tet1(constructFourNodeElement(org_elem, nodes)); - std::array const first_four_nodes = { + std::array const first_four_node_ids = { {tet1->getNode(0)->getID(), tet1->getNode(1)->getID(), tet1->getNode(2)->getID(), tet1->getNode(3)->getID()}}; unsigned const fifth_node = - findPyramidTopNode(*org_elem, first_four_nodes); + findPyramidTopNode(*org_elem, first_four_node_ids); bool tet_changed(false); if (tet1->getGeomType() == MeshLib::MeshElemType::QUAD) @@ -754,8 +755,8 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, delete tet1; tet_changed = true; std::array const tet1_nodes = { - nodes[first_four_nodes[0]], nodes[first_four_nodes[1]], - nodes[first_four_nodes[2]], + nodes[first_four_node_ids[0]], nodes[first_four_node_ids[1]], + nodes[first_four_node_ids[2]], nodes[org_elem->getNode(fifth_node)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet1_nodes)); } @@ -765,9 +766,9 @@ unsigned reduceHex(MeshLib::Element const* const org_elem, } std::array const tet2_nodes = { - (tet_changed) ? nodes[first_four_nodes[0]] - : nodes[first_four_nodes[1]], - nodes[first_four_nodes[2]], nodes[first_four_nodes[3]], + (tet_changed) ? nodes[first_four_node_ids[0]] + : nodes[first_four_node_ids[1]], + nodes[first_four_node_ids[2]], nodes[first_four_node_ids[3]], nodes[org_elem->getNode(fifth_node)->getID()]}; new_elements.push_back(new MeshLib::Tet(tet2_nodes)); return 2;