Skip to content

Commit

Permalink
GRIDEDIT-778 Reduced the cyclomatic complexity in one function
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior committed May 27, 2024
1 parent f90bb41 commit ddbe30b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 113 deletions.
14 changes: 14 additions & 0 deletions libs/MeshKernel/include/MeshKernel/CasulliDeRefinement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ namespace meshkernel
const std::vector<UInt>& directlyConnected,
const std::vector<std::array<int, 2>>& kne) const;

/// @brief Update the mesh members for the mesh description and connectivity for triangle elements
bool UpdateDirectlyConnectedTriangleElements(Mesh2D& mesh,
const UInt index,
const UInt connectedElementId,
const std::vector<std::array<int, 2>>& kne) const;

/// @brief Update the mesh members for the mesh description and connectivity for non-triangle elements
///
/// That is, element with 4 or more edges.
void UpdateDirectlyConnectedNonTriangleElements(Mesh2D& mesh,
const UInt index,
const UInt elementId,
const UInt connectedElementId) const;

/// @brief Get the most significant node type for all nodes of the element.
int GetNodeCode(const Mesh2D& mesh,
const std::vector<int>& nodeTypes,
Expand Down
245 changes: 132 additions & 113 deletions libs/MeshKernel/src/CasulliDeRefinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,155 +529,174 @@ meshkernel::Point meshkernel::CasulliDeRefinement::ComputeNewNodeCoordinates(con
return newNode;
}

bool meshkernel::CasulliDeRefinement::UpdateDirectlyConnectedElements(Mesh2D& mesh,
const UInt elementId,
const std::vector<UInt>& directlyConnected,
const std::vector<std::array<int, 2>>& kne) const
bool meshkernel::CasulliDeRefinement::UpdateDirectlyConnectedTriangleElements(Mesh2D& mesh,
const UInt index,
const UInt connectedElementId,
const std::vector<std::array<int, 2>>& kne) const
{
for (UInt k = 0; k < directlyConnected.size(); ++k)

for (UInt j = 0; j < mesh.m_numFacesNodes[connectedElementId]; ++j)
{
UInt connectedElementId = directlyConnected[k];
UInt edgeId = mesh.m_facesEdges[connectedElementId][j];

if (mesh.m_numFacesNodes[connectedElementId] < constants::geometric::numNodesInQuadrilateral)
if (mesh.m_edgesNumFaces[edgeId] < 2 && !CleanUpEdge(mesh, edgeId))
{
return false;
}
}

for (UInt j = 0; j < mesh.m_numFacesNodes[connectedElementId]; ++j)
{
UInt edgeId = mesh.m_facesEdges[connectedElementId][j];
// Find adjacent direct neighbours
UInt otherEdgeId = constants::missing::uintValue;

if (mesh.m_edgesNumFaces[edgeId] < 2 && !CleanUpEdge(mesh, edgeId))
{
return false;
}
}
for (UInt i = 0; i < 2; ++i)
{
UInt leftElementId = kne[index][i] == constants::missing::intValue || kne[index][i] < 0 ? constants::missing::uintValue : static_cast<UInt>(kne[index][i]);

if (leftElementId == constants::missing::uintValue)
{
continue;
}

UInt oppositeSide = 1 - i;
UInt rightElementId = kne[index][oppositeSide] == constants::missing::intValue || kne[index][oppositeSide] < 0 ? constants::missing::uintValue : static_cast<UInt>(kne[index][oppositeSide]);

if (leftElementId == constants::missing::uintValue || rightElementId == constants::missing::uintValue)
{
continue;
}

// Find adjacent direct neighbours
UInt otherEdgeId = constants::missing::uintValue;
UInt j;
UInt edgeId = constants::missing::uintValue;

// find the common link
for (j = 0; j < mesh.m_numFacesNodes[leftElementId]; ++j)
{
edgeId = mesh.m_facesEdges[leftElementId][j];

for (UInt i = 0; i < 2; ++i)
if (mesh.m_edgesNumFaces[edgeId] < 2)
{
UInt leftElementId = kne[k][i] == constants::missing::intValue || kne[k][i] < 0 ? constants::missing::uintValue : static_cast<UInt>(kne[k][i]);
continue;
}

if (leftElementId == constants::missing::uintValue)
if (mesh.m_edgesFaces[edgeId][0] == connectedElementId && mesh.m_edgesFaces[edgeId][1] == leftElementId)
{
if (rightElementId != constants::missing::uintValue)
{
continue;
mesh.m_edgesFaces[edgeId][0] = rightElementId;
}

UInt oppositeSide = 1 - i;
UInt rightElementId = kne[k][oppositeSide] == constants::missing::intValue || kne[k][oppositeSide] < 0 ? constants::missing::uintValue : static_cast<UInt>(kne[k][oppositeSide]);

if (leftElementId == constants::missing::uintValue || rightElementId == constants::missing::uintValue)
else
{
continue;
mesh.m_edgesFaces[edgeId][0] = mesh.m_edgesFaces[edgeId][1];
mesh.m_edgesFaces[edgeId][1] = constants::missing::uintValue;
mesh.m_edgesNumFaces[edgeId] = 1;
}

UInt j;
UInt edgeId = constants::missing::uintValue;

// find the common link
for (j = 0; j < mesh.m_numFacesNodes[leftElementId]; ++j)
break;
}
else if (mesh.m_edgesFaces[edgeId][1] == connectedElementId && mesh.m_edgesFaces[edgeId][0] == leftElementId)
{
if (rightElementId != constants::missing::uintValue)
{
edgeId = mesh.m_facesEdges[leftElementId][j];
mesh.m_edgesFaces[edgeId][1] = rightElementId;
}
else
{
mesh.m_edgesFaces[edgeId][1] = constants::missing::uintValue;
mesh.m_edgesNumFaces[edgeId] = 1;
}

if (mesh.m_edgesNumFaces[edgeId] < 2)
{
continue;
}
break;
}
}

if (mesh.m_edgesFaces[edgeId][0] == connectedElementId && mesh.m_edgesFaces[edgeId][1] == leftElementId)
{
if (rightElementId != constants::missing::uintValue)
{
mesh.m_edgesFaces[edgeId][0] = rightElementId;
}
else
{
mesh.m_edgesFaces[edgeId][0] = mesh.m_edgesFaces[edgeId][1];
mesh.m_edgesFaces[edgeId][1] = constants::missing::uintValue;
mesh.m_edgesNumFaces[edgeId] = 1;
}
if (otherEdgeId != constants::missing::uintValue)
{
mesh.m_facesEdges[leftElementId][j] = otherEdgeId;

break;
}
else if (mesh.m_edgesFaces[edgeId][1] == connectedElementId && mesh.m_edgesFaces[edgeId][0] == leftElementId)
{
if (rightElementId != constants::missing::uintValue)
{
mesh.m_edgesFaces[edgeId][1] = rightElementId;
}
else
{
mesh.m_edgesFaces[edgeId][1] = constants::missing::uintValue;
mesh.m_edgesNumFaces[edgeId] = 1;
}
if (!CleanUpEdge(mesh, edgeId))
{
return false;
}
}

break;
}
}
otherEdgeId = edgeId;
}

if (otherEdgeId != constants::missing::uintValue)
{
mesh.m_facesEdges[leftElementId][j] = otherEdgeId;
// deactivate cell
mesh.m_numFacesNodes[connectedElementId] = 0;
return true;
}

if (!CleanUpEdge(mesh, edgeId))
{
return false;
}
}
void meshkernel::CasulliDeRefinement::UpdateDirectlyConnectedNonTriangleElements(Mesh2D& mesh,
const UInt index,
const UInt elementId,
const UInt connectedElementId) const
{
// polygons of degree higher than three: remove node and link

otherEdgeId = edgeId;
}
for (UInt j = 0; j < mesh.m_numFacesNodes[connectedElementId]; ++j)
{
UInt edgeId = mesh.m_facesEdges[connectedElementId][j];

// deactivate cell
mesh.m_numFacesNodes[connectedElementId] = 0;
}
else
if (mesh.m_edgesNumFaces[edgeId] < 2)
{
continue;
}

// polygons of degree higher than three: remove node and link

for (UInt j = 0; j < mesh.m_numFacesNodes[connectedElementId]; ++j)
{
UInt edgeId = mesh.m_facesEdges[connectedElementId][j];
if (mesh.m_edgesFaces[edgeId][0] == elementId || mesh.m_edgesFaces[edgeId][1] == elementId)
{
UInt ndum = mesh.m_numFacesNodes[connectedElementId] - 1;

if (mesh.m_edgesNumFaces[edgeId] < 2)
{
continue;
}
std::shift_left(mesh.m_facesEdges[connectedElementId].begin() + j, mesh.m_facesEdges[connectedElementId].begin() + ndum, 1);

if (mesh.m_edgesFaces[edgeId][0] == elementId || mesh.m_edgesFaces[edgeId][1] == elementId)
{
UInt ndum = mesh.m_numFacesNodes[connectedElementId] - 1;
// remove one node per removed link
// take the first node that has not been removed before, but not the node that is kept,
// which is the first of the center cell

std::shift_left(mesh.m_facesEdges[connectedElementId].begin() + j, mesh.m_facesEdges[connectedElementId].begin() + ndum, 1);
UInt i = 0;

// remove one node per removed link
// take the first node that has not been removed before, but not the node that is kept,
// which is the first of the center cell
while (i < mesh.m_numFacesNodes[connectedElementId] &&
mesh.m_facesNodes[connectedElementId][i] != mesh.GetEdge(edgeId).first &&
mesh.m_facesNodes[connectedElementId][i] != mesh.GetEdge(edgeId).second &&
mesh.m_facesNodes[connectedElementId][i] != mesh.m_facesNodes[elementId][0])
{
++i;
}

UInt i = 0;
if (index < mesh.m_numFacesNodes[connectedElementId])
{
std::shift_left(mesh.m_facesNodes[connectedElementId].begin() + i, mesh.m_facesNodes[connectedElementId].begin() + ndum, 1);
}
else
{
throw AlgorithmError("No node found in Casulli de-refinement");
}

while (i < mesh.m_numFacesNodes[connectedElementId] &&
mesh.m_facesNodes[connectedElementId][i] != mesh.GetEdge(edgeId).first &&
mesh.m_facesNodes[connectedElementId][i] != mesh.GetEdge(edgeId).second &&
mesh.m_facesNodes[connectedElementId][i] != mesh.m_facesNodes[elementId][0])
{
++i;
}
mesh.m_numFacesNodes[connectedElementId] = ndum;
}
}
}

if (k < mesh.m_numFacesNodes[connectedElementId])
{
std::shift_left(mesh.m_facesNodes[connectedElementId].begin() + i, mesh.m_facesNodes[connectedElementId].begin() + ndum, 1);
}
else
{
throw AlgorithmError("No node found in Casulli de-refinement");
}
bool meshkernel::CasulliDeRefinement::UpdateDirectlyConnectedElements(Mesh2D& mesh,
const UInt elementId,
const std::vector<UInt>& directlyConnected,
const std::vector<std::array<int, 2>>& kne) const
{
for (UInt k = 0; k < directlyConnected.size(); ++k)
{
UInt connectedElementId = directlyConnected[k];

mesh.m_numFacesNodes[connectedElementId] = ndum;
}
if (mesh.m_numFacesNodes[connectedElementId] < constants::geometric::numNodesInQuadrilateral)
{
if (!UpdateDirectlyConnectedTriangleElements(mesh, k, connectedElementId, kne))
{
return false;
}
}
else
{
UpdateDirectlyConnectedNonTriangleElements(mesh, k, elementId, connectedElementId);
}
}

return true;
Expand Down

0 comments on commit ddbe30b

Please sign in to comment.