Skip to content

Commit

Permalink
Fixed bug when inserting multiple faces along a domain boundary (#325
Browse files Browse the repository at this point in the history
…| GRIDEDIT-999)
  • Loading branch information
BillSenior authored Apr 24, 2024
1 parent 510322b commit a166abb
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 67 deletions.
2 changes: 2 additions & 0 deletions libs/MeshKernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ set(
${CURVILINEAR_GRID_SRC_DIR}/CurvilinearGridSmoothing.cpp
${CURVILINEAR_GRID_SRC_DIR}/CurvilinearGridSmoothness.cpp
${CURVILINEAR_GRID_SRC_DIR}/CurvilinearGridSnapping.cpp
${CURVILINEAR_GRID_SRC_DIR}/CurvilinearGridUtilities.cpp
)


Expand Down Expand Up @@ -225,6 +226,7 @@ set(
${CURVILINEAR_GRID_INC_DIR}/CurvilinearGridSmoothing.hpp
${CURVILINEAR_GRID_INC_DIR}/CurvilinearGridSmoothness.hpp
${CURVILINEAR_GRID_INC_DIR}/CurvilinearGridSnapping.hpp
${CURVILINEAR_GRID_INC_DIR}/CurvilinearGridUtilities.hpp
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ namespace meshkernel
/// @brief An enum for boundary grid line types
enum class BoundaryGridLineType
{
Left, ///< Bottom of domain
Right, ///<
Bottom, ///<
Up ///< Right side of domain
Bottom,
Right,
Top,
Left
};

/// @brief Default constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#pragma once

#include <string>

namespace meshkernel
{
/// @brief An enum for curvilinear node types
Expand All @@ -43,4 +45,8 @@ namespace meshkernel
InternalValid, //(10)
Invalid //(0)
};

/// @brief Get string representation of the NodeType
const std::string& toString(const NodeType nodeType);

} // namespace meshkernel
144 changes: 89 additions & 55 deletions libs/MeshKernel/src/CurvilinearGrid/CurvilinearGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,68 +623,83 @@ std::tuple<bool, meshkernel::UndoActionPtr> CurvilinearGrid::AddGridLineAtBounda
if (areNodesValid)
{

if (gridLineType == BoundaryGridLineType::Left)
if (gridLineType == BoundaryGridLineType::Bottom)
{
// n-direction
if (m_startOffset.m_n == 0)
{
lin_alg::InsertRow(m_gridNodes, lin_alg::RowVector<Point>(FullNumM()), 0);
}
else
if (firstNode.m_n == 0 || secondNode.m_n == 0)
{
m_startOffset.m_n -= 1;
}

undoAction = AddGridLineUndoAction::Create(*this, {1, 0}, {0, 0});
gridSizeChanged = true;
// n-direction
if (m_startOffset.m_n == 0)
{
lin_alg::InsertRow(m_gridNodes, lin_alg::RowVector<Point>(FullNumM()), 0);
}
else
{
m_startOffset.m_n -= 1;
}

undoAction = AddGridLineUndoAction::Create(*this, {1, 0}, {0, 0});
gridSizeChanged = true;
}
}

if (gridLineType == BoundaryGridLineType::Right)
if (gridLineType == BoundaryGridLineType::Top)
{
// n-direction
if (m_endOffset.m_n == 0)
{
lin_alg::InsertRow(m_gridNodes, lin_alg::RowVector<Point>(FullNumM()), FullNumN());
}
else
if (firstNode.m_n == NumN() - 1 || secondNode.m_n == NumN() - 1)
{
m_endOffset.m_n -= 1;
// n-direction
if (m_endOffset.m_n == 0)
{
lin_alg::InsertRow(m_gridNodes, lin_alg::RowVector<Point>(FullNumM()), FullNumN());
}
else
{
m_endOffset.m_n -= 1;
}

undoAction = AddGridLineUndoAction::Create(*this, {0, 0}, {1, 0});
gridSizeChanged = true;
}

undoAction = AddGridLineUndoAction::Create(*this, {0, 0}, {1, 0});
gridSizeChanged = true;
}

if (gridLineType == BoundaryGridLineType::Up)
if (gridLineType == BoundaryGridLineType::Right)
{
// m-direction
if (m_endOffset.m_m == 0)
{
lin_alg::InsertCol(m_gridNodes, lin_alg::ColVector<Point>(FullNumN()), FullNumM());
}
else
if (firstNode.m_m == NumM() - 1 || secondNode.m_m == NumM() - 1)
{
m_endOffset.m_m -= 1;
// m-direction
if (m_endOffset.m_m == 0)
{
lin_alg::InsertCol(m_gridNodes, lin_alg::ColVector<Point>(FullNumN()), FullNumM());
}
else
{
m_endOffset.m_m -= 1;
}

undoAction = AddGridLineUndoAction::Create(*this, {0, 0}, {0, 1});
gridSizeChanged = true;
}

undoAction = AddGridLineUndoAction::Create(*this, {0, 0}, {0, 1});
gridSizeChanged = true;
}

if (gridLineType == BoundaryGridLineType::Bottom)
if (gridLineType == BoundaryGridLineType::Left)
{
// m-direction
if (m_startOffset.m_m == 0)
{
lin_alg::InsertCol(m_gridNodes, lin_alg::ColVector<Point>(FullNumN()), 0);
}
else

if (firstNode.m_m == 0 || secondNode.m_m == 0)
{
m_startOffset.m_m -= 1;
}

undoAction = AddGridLineUndoAction::Create(*this, {0, 1}, {0, 0});
gridSizeChanged = true;
// m-direction
if (m_startOffset.m_m == 0)
{
lin_alg::InsertCol(m_gridNodes, lin_alg::ColVector<Point>(FullNumN()), 0);
}
else
{
m_startOffset.m_m -= 1;
}

undoAction = AddGridLineUndoAction::Create(*this, {0, 1}, {0, 0});
gridSizeChanged = true;
}
}
}

Expand All @@ -706,25 +721,25 @@ CurvilinearGrid::BoundaryGridLineType CurvilinearGrid::GetBoundaryGridLineType(C
(firstNodeType == NodeType::BottomLeft && secondNodeType == NodeType::BottomRight) ||
(firstNodeType == NodeType::BottomRight && secondNodeType == NodeType::BottomLeft))
{
return BoundaryGridLineType::Bottom;
return BoundaryGridLineType::Left;
}
if (firstNodeType == NodeType::Up || secondNodeType == NodeType::Up ||
(firstNodeType == NodeType::UpperLeft && secondNodeType == NodeType::UpperRight) ||
(firstNodeType == NodeType::UpperRight && secondNodeType == NodeType::UpperLeft))
{
return BoundaryGridLineType::Up;
return BoundaryGridLineType::Right;
}
if (firstNodeType == NodeType::Left || secondNodeType == NodeType::Left ||
(firstNodeType == NodeType::BottomLeft && secondNodeType == NodeType::UpperLeft) ||
(firstNodeType == NodeType::UpperLeft && secondNodeType == NodeType::BottomLeft))
{
return BoundaryGridLineType::Left;
return BoundaryGridLineType::Bottom;
}
if (firstNodeType == NodeType::Right || secondNodeType == NodeType::Right ||
(firstNodeType == NodeType::BottomRight && secondNodeType == NodeType::UpperRight) ||
(firstNodeType == NodeType::UpperRight && secondNodeType == NodeType::BottomRight))
{
return BoundaryGridLineType::Right;
return BoundaryGridLineType::Top;
}

throw std::invalid_argument("CurvilinearGrid::GetBoundaryGridLineType: Invalid node types");
Expand All @@ -741,7 +756,7 @@ meshkernel::UndoActionPtr CurvilinearGrid::AddEdge(CurvilinearGridNodeIndices co

m_edgesRTreeRequiresUpdate = true;

if (gridLineType == BoundaryGridLineType::Left)
if (gridLineType == BoundaryGridLineType::Bottom)
{
auto const firstNewNodeCoordinates = GetNode(firstNode.m_n, firstNode.m_m) * 2.0 - GetNode(firstNode.m_n + 1, firstNode.m_m);
auto const secondNewNodeCoordinates = GetNode(secondNode.m_n, secondNode.m_m) * 2.0 - GetNode(secondNode.m_n + 1, secondNode.m_m);
Expand All @@ -760,18 +775,22 @@ meshkernel::UndoActionPtr CurvilinearGrid::AddEdge(CurvilinearGridNodeIndices co
}
}

if (gridLineType == BoundaryGridLineType::Right)
if (gridLineType == BoundaryGridLineType::Top)
{
auto const firstNewNodeCoordinates = GetNode(firstNode.m_n, firstNode.m_m) * 2.0 - GetNode(firstNode.m_n - 1, firstNode.m_m);
auto const secondNewNodeCoordinates = GetNode(secondNode.m_n, secondNode.m_m) * 2.0 - GetNode(secondNode.m_n - 1, secondNode.m_m);
auto [isGridLineAdded, addGridLineUndo] = AddGridLineAtBoundary(firstNode, secondNode);

undoAddEdge->Add(std::move(addGridLineUndo));
if (isGridLineAdded)
{
undoAddEdge->Add(std::move(addGridLineUndo));
}

undoAddEdge->Add(MoveNode(CurvilinearGridNodeIndices(firstNode.m_n + 1, firstNode.m_m), firstNewNodeCoordinates));
undoAddEdge->Add(MoveNode(CurvilinearGridNodeIndices(secondNode.m_n + 1, secondNode.m_m), secondNewNodeCoordinates));
}

if (gridLineType == BoundaryGridLineType::Bottom)
if (gridLineType == BoundaryGridLineType::Left)
{
auto const firstNewNodeCoordinates = GetNode(firstNode.m_n, firstNode.m_m) * 2.0 - GetNode(firstNode.m_n, firstNode.m_m + 1);
auto const secondNewNodeCoordinates = GetNode(secondNode.m_n, secondNode.m_m) * 2.0 - GetNode(secondNode.m_n, secondNode.m_m + 1);
Expand All @@ -791,16 +810,31 @@ meshkernel::UndoActionPtr CurvilinearGrid::AddEdge(CurvilinearGridNodeIndices co
}
}

if (gridLineType == BoundaryGridLineType::Up)
if (gridLineType == BoundaryGridLineType::Right)
{
auto const firstNewNodeCoordinates = GetNode(firstNode.m_n, firstNode.m_m) * 2.0 - GetNode(firstNode.m_n, firstNode.m_m - 1);
auto const secondNewNodeCoordinates = GetNode(secondNode.m_n, secondNode.m_m) * 2.0 - GetNode(secondNode.m_n, secondNode.m_m - 1);
[[maybe_unused]] auto [isGridLineAdded, addGridLineUndo] = AddGridLineAtBoundary(firstNode, secondNode);
undoAddEdge->Add(std::move(addGridLineUndo));
auto [isGridLineAdded, addGridLineUndo] = AddGridLineAtBoundary(firstNode, secondNode);

if (isGridLineAdded)
{
undoAddEdge->Add(std::move(addGridLineUndo));
}

undoAddEdge->Add(MoveNode(CurvilinearGridNodeIndices(firstNode.m_n, firstNode.m_m + 1), firstNewNodeCoordinates));
undoAddEdge->Add(MoveNode(CurvilinearGridNodeIndices(secondNode.m_n, secondNode.m_m + 1), secondNewNodeCoordinates));
}

if (gridLineType == BoundaryGridLineType::Bottom ||
gridLineType == BoundaryGridLineType::Right ||
gridLineType == BoundaryGridLineType::Top ||
gridLineType == BoundaryGridLineType::Left)
{
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;
m_facesRTreeRequiresUpdate = true;
}

return undoAddEdge;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ meshkernel::UndoActionPtr CurvilinearGridLineMirror::Compute()

switch (gridLineType)
{
case Left: // Bottom
case Bottom:
lowerLeft = {0, m_lines[0].m_startCoordinate};
upperRight = {1, m_lines[0].m_endCoordinate + 1};
break;
case Right: // Top
case Top:
lowerLeft = {m_grid.NumN() - 1, m_lines[0].m_startCoordinate};
upperRight = {m_grid.NumN() - 0, m_lines[0].m_endCoordinate + 1};
break;
case Up: // Right
case Right:
lowerLeft = {m_lines[0].m_startCoordinate, m_grid.NumM() - 1};
upperRight = {m_lines[0].m_endCoordinate + 1, m_grid.NumM() - 0};
break;
case Bottom: // Left
case Left:
lowerLeft = {0, m_lines[0].m_startCoordinate};
upperRight = {m_lines[0].m_endCoordinate + 1, m_lines[0].m_startCoordinate + 1};
break;
Expand All @@ -102,24 +102,24 @@ meshkernel::UndoActionPtr CurvilinearGridLineMirror::Compute()

for (auto i = m_lines[0].m_startCoordinate; i <= m_lines[0].m_endCoordinate; ++i)
{
if (gridLineType == Left)
if (gridLineType == Bottom)
{
m_grid.GetNode(0, i) = m_grid.GetNode(1, i) * a +
m_grid.GetNode(2, i) * b;
}
if (gridLineType == Right)
if (gridLineType == Top)
{
auto const last_row = (UInt)m_grid.NumN() - 1;
m_grid.GetNode(last_row, i) = m_grid.GetNode(last_row - 1, i) * a +
m_grid.GetNode(last_row - 2, i) * b;
}
if (gridLineType == Up)
if (gridLineType == Right)
{
auto const last_col = (UInt)m_grid.NumM() - 1;
m_grid.GetNode(i, last_col) = m_grid.GetNode(i, last_col - 1) * a +
m_grid.GetNode(i, last_col - 2) * b;
}
if (gridLineType == Bottom)
if (gridLineType == Left)
{
m_grid.GetNode(i, 0) = m_grid.GetNode(i, 1) * a +
m_grid.GetNode(i, 2) * b;
Expand Down
Loading

0 comments on commit a166abb

Please sign in to comment.