Skip to content

Commit

Permalink
Reduce cyclomatic complexity (#380 | GRIDEDIT-1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior authored Nov 27, 2024
1 parent b11e79c commit 4a43633
Show file tree
Hide file tree
Showing 38 changed files with 3,547 additions and 2,253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace meshkernel::averaging
/// @brief Construct a new ClosestAveragingStrategy.
/// @param[in] interpolationPoint The point for which the average should be calculated.
/// @param[in] projection The projection used to calculate distances with.
ClosestAveragingStrategy(Projection projection);
explicit ClosestAveragingStrategy(Projection projection);

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace meshkernel::averaging
public:
/// @brief Constructor taking the minimum amount of points parameter
/// @param minNumSamples[in] The minimum amount of samples for a valid interpolation
SimpleAveragingStrategy(size_t minNumSamples);
explicit SimpleAveragingStrategy(size_t minNumSamples);

/// @brief Calculates the average value based on the sample values.
/// @param[in] interpolationPoint The point for which the average should be calculated.
Expand Down
66 changes: 65 additions & 1 deletion libs/MeshKernel/include/MeshKernel/CasulliDeRefinement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

namespace meshkernel
{
/// @brief Compute the Casulli de-refinement for a mesh.
/// @brief Compute the Casulli de-refinement for a mesh (derefine_mesh).
class CasulliDeRefinement
{
public:
Expand Down Expand Up @@ -78,6 +78,14 @@ namespace meshkernel
Unassigned = 0 //< not assigned a value 0
};

/// @brief Indicates what should happen
enum class ResultIndicator
{
ReturnFromFunction, //< Return (false) from the current function
BreakInnerLoop, //< Break the inner loop
ContinueInnerLoop //< Continue in the inner loop
};

/// @brief Indicate if the element can be a seed element or not.
static bool ElementIsSeed(const Mesh2D& mesh,
const std::vector<int>& nodeTypes,
Expand All @@ -98,6 +106,16 @@ namespace meshkernel
const std::vector<UInt>& directlyConnected,
std::vector<UInt>& indirectlyConnected);

/// @brief Assign directly connected element indices
static void AssignDirectlyConnected(const std::vector<UInt>& directlyConnected,
std::array<int, 2>& kne,
UInt& neighbouringElementId);

/// @brief Assign indirectly connected element indices
static void AssignIndirectlyConnected(const std::vector<UInt>& indirectlyConnected,
std::array<int, 2>& kne,
const UInt neighbouringElementId);

/// @brief Find element id's
static void FindAdjacentCells(const Mesh2D& mesh,
const std::vector<UInt>& directlyConnected,
Expand All @@ -111,6 +129,32 @@ namespace meshkernel
std::vector<UInt>& indirectlyConnected,
std::vector<std::array<int, 2>>& kne);

/// @brief Update cell mask for directly connected elements
static void UpdateCellMaskDirectlyConnectedNodeFirst(const std::vector<UInt>& directlyConnected,
const Mesh2D& mesh,
const std::vector<UInt>& frontIndex,
std::vector<UInt>& frontIndexCopy,
std::vector<ElementType>& cellMask);

/// @brief Update cell mask for indirectly connected elements
static void UpdateCellMaskIndirectlyConnectedNodeFirst(const std::vector<UInt>& directlyConnected,
const Mesh2D& mesh,
std::vector<ElementType>& cellMask);

/// @brief Update cell mask for directly connected elements
static void UpdateCellMaskDirectlyConnectedEdgeFirst(const std::vector<UInt>& directlyConnected,
const Mesh2D& mesh,
const std::vector<UInt>& frontIndex,
std::vector<UInt>& frontIndexCopy,
std::vector<ElementType>& cellMask);

/// @brief Update cell mask for indirectly connected elements
static void UpdateCellMaskIndirectlyConnectedEdgeFirst(const std::vector<UInt>& indirectlyConnected,
const Mesh2D& mesh,
const std::vector<UInt>& frontIndex,
std::vector<UInt>& frontIndexCopy,
std::vector<ElementType>& cellMask);

/// @brief Initialise the element mask.
static std::vector<ElementType> InitialiseElementType(const Mesh2D& mesh,
const std::vector<int>& nodeTypes);
Expand All @@ -126,6 +170,16 @@ namespace meshkernel
const std::vector<int>& nodeTypes,
const UInt nodeId);

/// @brief Get the element index
static UInt GetElementIndex(const std::array<int, 2>& kne,
const UInt index);

/// @brief Find a common edge between elements
static std::tuple<UInt, UInt> FindCommonEdge(Mesh2D& mesh,
const UInt leftElementId,
const UInt rightElementId,
const UInt connectedElementId);

/// @brief Update the mesh members for the mesh description and connectivity.
[[nodiscard]] static bool UpdateDirectlyConnectedElements(Mesh2D& mesh,
const UInt elementId,
Expand Down Expand Up @@ -165,6 +219,16 @@ namespace meshkernel
const UInt nodeId,
const std::vector<UInt>& indirectlyConnected);

/// @brief Remove a boundary node or edge
static ResultIndicator RemoveBoundaryNodeAndEdge(Mesh2D& mesh,
const Polygons& polygon,
const std::vector<int>& nodeTypes,
const UInt faceNodeIndex,
const UInt connectedElementId,
const UInt edgeId,
const UInt previousEdgeId,
const UInt nodeId);

/// @brief Removes nodes from the boundary that will not be part of the de-refined mesh.
[[nodiscard]] static bool RemoveUnwantedBoundaryNodes(Mesh2D& mesh,
const std::vector<int>& nodeTypes,
Expand Down
15 changes: 15 additions & 0 deletions libs/MeshKernel/include/MeshKernel/CasulliRefinement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ namespace meshkernel
/// @param [in] numEdges Number of edges in original mesh, before refinement.
static void ConnectNodes(Mesh2D& mesh, const std::vector<EdgeNodes>& newNodes, const UInt numEdges);

/// @brief Get list of node-ids that should be connected to the nodeIndex
static std::vector<UInt> GetNodesToConnect(const Mesh2D& mesh,
const std::vector<NodeMask>& nodeMask,
const std::vector<UInt>& newEdges,
const std::vector<EdgeNodes>& newNodes,
const UInt edgeCount,
const UInt nodeIndex);

/// @brief Connect nodes to nodeIndex.
static void ConnectNodes(Mesh2D& mesh,
const NodeMask nodeMask,
const std::vector<UInt>& nodesToConnect,
const UInt edgeCount,
const UInt nodeIndex);

/// @brief Compute new edges required for refinement
///
/// @param [in, out] mesh The mesh being refined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ namespace meshkernel
/// @return The new displacement
[[nodiscard]] Point TransformDisplacement(Point const& displacement, CurvilinearGridNodeIndices const& node, bool isLocal) const;

UndoActionPtr AddGridLineAtBottom(const CurvilinearGridNodeIndices& firstNode,
const CurvilinearGridNodeIndices& secondNode);

UndoActionPtr AddGridLineAtTop(const CurvilinearGridNodeIndices& firstNode,
const CurvilinearGridNodeIndices& secondNode);

UndoActionPtr AddGridLineAtLeft(const CurvilinearGridNodeIndices& firstNode,
const CurvilinearGridNodeIndices& secondNode);

UndoActionPtr AddGridLineAtRight(const CurvilinearGridNodeIndices& firstNode,
const CurvilinearGridNodeIndices& secondNode);

/// @brief Allocates a new grid line at the boundary of the curvilinear grid if needed.
/// @param firstNode The first node of the boundary grid line.
/// @param secondNode The second node of the boundary grid line.
Expand Down Expand Up @@ -451,6 +463,23 @@ namespace meshkernel
void CommitAction(const ResetCurvilinearNodeAction& undoAction);

private:
/// @brief One dimensional array of NodeType
using NodeTypeArray1D = std::array<meshkernel::NodeType, 2>;

/// @brief Two dimensional array of NodeType
using NodeTypeArray2D = std::array<NodeTypeArray1D, 2>;

/// @brief Three dimensional array of NodeType
using NodeTypeArray3D = std::array<NodeTypeArray2D, 2>;

/// @brief Four dimensional array of NodeType
/// Indices are (n, m), (n-1, m), (n-1, m-1), (n, m-1)
/// top-right, top-left, bottom-left, bottom-right,
using NodeTypeArray4D = std::array<NodeTypeArray3D, 2>;

/// @brief Get interior node type array
static NodeTypeArray4D InitialiseInteriorNodeType();

/// @brief Remove invalid nodes.
/// This function is recursive
/// @param[in] invalidNodesToRemove Whether there are still invalid nodes to remove
Expand Down Expand Up @@ -499,6 +528,21 @@ namespace meshkernel
/// @param[in] value The value of the flag
void SetFacesRTreeRequiresUpdate(bool value) { m_facesRTreeRequiresUpdate = value; }

/// @brief Get the node type of the bottom row of nodes, m = 0
NodeType GetBottomNodeType(const UInt n) const;

/// @brief Get the node type of the top row of nodes, m = NumM - 1
NodeType GetTopNodeType(const UInt n) const;

/// @brief Get the node type of the left row of nodes, n = 0
NodeType GetLeftNodeType(const UInt m) const;

/// @brief Get the node type of the right row of nodes, m = NumN - 1
NodeType GetRightNodeType(const UInt m) const;

/// @brief Get the node type of the interior nodes
NodeType GetInteriorNodeType(const UInt n, const UInt m) const;

Projection m_projection; ///< The curvilinear grid projection
lin_alg::Matrix<Point> m_gridNodes; ///< Member variable storing the grid
lin_alg::Matrix<bool> m_gridFacesMask; ///< The mask of the grid faces (true/false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace meshkernel
public:
/// @brief Class constructor
/// @param[in] grid The input curvilinear grid
CurvilinearGridDeRefinement(CurvilinearGrid& grid);
explicit CurvilinearGridDeRefinement(CurvilinearGrid& grid);

/// @brief Refine the curvilinear grid
[[nodiscard]] UndoActionPtr Compute() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace meshkernel
{
public:
/// @param polygon The input polygon
CurvilinearGridFromPolygon(const Polygon& polygon);
explicit CurvilinearGridFromPolygon(const Polygon& polygon);

/// @brief Compute curvilinear in a polygon (pol2curvi)
/// @returns The computed curvilinear grid
Expand All @@ -52,6 +52,24 @@ namespace meshkernel
std::unique_ptr<CurvilinearGrid> Compute(UInt firstNode, UInt secondNode, UInt thirdNode) const;

private:
/// &brief Fill boundary coordinates
void AssignPolygonPointsToSegment(UInt nodeIndex,
UInt numPointsSide,
int direction,
std::vector<Point>& sideToFill) const;

void ComputeNumberOfMNodes(const UInt firstNode,
const UInt secondNode,
const UInt numPolygonNodes,
int& direction,
UInt& numMNodes) const;

void ComputeNumberOfNNodes(const UInt secondNode,
const UInt thirdNode,
const UInt numPolygonNodes,
const int direction,
UInt& numNNodes) const;

const Polygon& m_polygon; /// Reference to a polygon
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ namespace meshkernel
std::vector<double> ComputeMaximumEdgeGrowTime(const lin_alg::RowVector<Point>& coordinates,
const std::vector<Point>& velocities) const;

/// @brief Check corner nodes
bool CheckCornerNodes(const UInt p,
const lin_alg::RowVector<UInt>& indices,
const lin_alg::Matrix<UInt>& gridPointsIndices,
const bool increaseOffset) const;

/// @brief Copy growth velocities to the advancing front, add points at front corners corners (copy_vel_to_front)
/// @param[in] layerIndex The current grid layerIndex index
/// @param[in] previousFrontVelocities The previous front velocities
Expand Down Expand Up @@ -368,5 +374,33 @@ namespace meshkernel
std::vector<UInt> m_subLayerGridPoints; ///< Sublayer grid points
lin_alg::Matrix<UInt> m_numPerpendicularFacesOnSubintervalAndEdge; ///< Perpendicular faces on subinterval and edge
lin_alg::Matrix<double> m_growFactorOnSubintervalAndEdge; ///< Grow factor on subinterval and edge

/// @brief Move any grid nodes
/// @returns Number of grid nodes moved.
UInt MoveGridNodes(const UInt i, const UInt j, const UInt firstLeftIndex, const UInt firstRightIndex);

/// @brief Compute the end points of the spline crossing the main spline
std::tuple<Point, Point> GetCrossSplinePoints(const UInt s, const UInt i) const;

/// @brief Move active layer points.
///
/// The amount of displacement is determined by the time-step size and the velocity
void TranslateActiveLayerPoints(const std::vector<Point>& velocityVectorAtGridPoints,
const double localTimeStep,
lin_alg::RowVector<Point>& activeLayerPoints) const;

/// @brief Disable front nodes that do not satisfy the time-step requirements
void DisableValidFrontNodes(const std::vector<double>& otherTimeStepMax,
const double otherTimeStep,
const double localTimeStep,
std::vector<UInt>& newValidFrontNodes) const;

/// @brief Set active layer points to invalid is indicated by validFrontNodes
void InvalidateActiveLayerPoints(lin_alg::RowVector<Point>& activeLayerPoints) const;

/// @brief Invalidate grid nodes that exceed edge angle requirements
void InvalidateGridNodes(const UInt layerIndex,
lin_alg::RowVector<Point>& activeLayerPoints,
std::vector<UInt>& newValidFrontNodes);
};
} // namespace meshkernel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ namespace meshkernel
/// finds intersections and orders the splines
void CharacteriseSplines();

/// @brief Compute spline intersections in n-direction
void ComputeNDirectionIntersections();

/// @brief Compute spline intersections in m-direction
void ComputeMDirectionIntersections();

/// @brief Compute spline intersection start and end points
void ComputeSplineStartAndEnd(const UInt outerStart, const UInt outerEnd, const UInt innerStart, const UInt innerEnd);

/// @brief Label each spline and its intersection.
///
/// In which group does it lie (m or n), and spline crossing indices.
Expand Down Expand Up @@ -114,6 +123,21 @@ namespace meshkernel
const std::vector<double>& intersectionDistances,
std::vector<double>& distances) const;

/// @brief Fill side point arrays
void FillInterpolationPlaneBlock(const lin_alg::Matrix<Point>& gridNodes,
const UInt i,
const UInt j,
std::vector<Point>& bottomSide,
std::vector<Point>& upperSide,
std::vector<Point>& leftSide,
std::vector<Point>& rightSide) const;

/// @brief Assign interpolated points to the grid block
void AssignInterpolatedNodes(const UInt i,
const UInt j,
const lin_alg::Matrix<Point>& interpolationResult,
lin_alg::Matrix<Point>& gridNodes) const;

std::vector<int> m_splineType; ///< The spline types (1 horizontal, -1 vertical)
std::vector<std::vector<double>> m_splineIntersectionRatios; ///< For each spline, stores the intersections in terms of total spline length
std::vector<std::vector<UInt>> m_splineGroupIndexAndFromToIntersections; ///< For each spline: position in m or n group, from and to spline crossing indices (MN12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace meshkernel
public:
/// @brief Class constructor
/// @param[in] grid The input curvilinear grid
CurvilinearGridLineShift(CurvilinearGrid& grid);
explicit CurvilinearGridLineShift(CurvilinearGrid& grid);

/// @brief Computes a new curvilinear grid with the line shift
[[nodiscard]] UndoActionPtr Compute() override;
Expand Down
Loading

0 comments on commit 4a43633

Please sign in to comment.