Skip to content

Commit

Permalink
Do admin only when necessary (#374 | gridedit 1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior authored Oct 28, 2024
1 parent a7543c0 commit 13df50d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 15 deletions.
37 changes: 26 additions & 11 deletions libs/MeshKernel/include/MeshKernel/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,22 @@ namespace meshkernel
/// @brief Set the node to a new value, this value may be the in-valid value.
[[nodiscard]] std::unique_ptr<ResetNodeAction> ResetNode(const UInt index, const Point& newValue);

/// @brief Get constant reference to an edge
const Edge& GetEdge(const UInt index) const;

/// @brief Get a non-constant reference to an edge
Edge& GetEdge(const UInt index);

/// @brief Get all edges
// TODO get rid of this function
const std::vector<Edge>& Edges() const;

/// @brief Get constant reference to an edge
const Edge& GetEdge(const UInt index) const;

/// @brief Set all edges to a new set of values.
void SetEdges(const std::vector<Edge>& newValues);

/// @brief Set the edge to a new value, bypassing the undo action
void SetEdge(const UInt index, const Edge& edge);

/// @brief Change the nodes referenced by the edge.
[[nodiscard]] std::unique_ptr<ResetEdgeAction> ResetEdge(UInt edgeId, const Edge& edge);

/// @brief Get the local index of the node belong to a face.
///
/// If the node cannot be found the null value will be returned.
Expand Down Expand Up @@ -244,9 +247,6 @@ namespace meshkernel
/// @return The index of the new edge and the undoAction to connect two nodes
[[nodiscard]] std::tuple<UInt, std::unique_ptr<AddEdgeAction>> ConnectNodes(UInt startNode, UInt endNode);

/// @brief Change the nodes referenced by the edge.
[[nodiscard]] std::unique_ptr<ResetEdgeAction> ResetEdge(UInt edgeId, const Edge& edge);

/// @brief Deletes a node and removes any connected edges
/// @param[in] node The node index
/// @return The undoAction to delete the node and any connecting edges
Expand Down Expand Up @@ -492,6 +492,12 @@ namespace meshkernel
static constexpr UInt m_maximumNumberOfConnectedNodes = m_maximumNumberOfEdgesPerNode * 4; ///< Maximum number of connected nodes

protected:
/// @brief Determine if a administration is required
bool AdministrationRequired() const;

/// @brief Determine if a administration is required
void SetAdministrationRequired(const bool value);

// Make private
std::vector<Point> m_nodes; ///< The mesh nodes (xk, yk)
std::vector<Edge> m_edges; ///< The edges, defined as first and second node(kn)
Expand All @@ -503,6 +509,7 @@ namespace meshkernel
bool m_nodesRTreeRequiresUpdate = true; ///< m_nodesRTree requires an update
bool m_edgesRTreeRequiresUpdate = true; ///< m_edgesRTree requires an update
bool m_facesRTreeRequiresUpdate = true; ///< m_facesRTree requires an update
bool m_administrationRequired = true; ///< Indicates if mesh administration requires an update
std::unordered_map<Location, std::unique_ptr<RTreeBase>> m_RTrees; ///< The RTrees to use
BoundingBox m_boundingBoxCache; ///< Caches the last bounding box used for selecting the locations

Expand Down Expand Up @@ -544,6 +551,7 @@ inline void meshkernel::Mesh::SetNodes(const std::vector<Point>& newValues)
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;
m_facesRTreeRequiresUpdate = true;
m_administrationRequired = true;
}

inline const meshkernel::Edge& meshkernel::Mesh::GetEdge(const UInt index) const
Expand All @@ -556,14 +564,15 @@ inline const meshkernel::Edge& meshkernel::Mesh::GetEdge(const UInt index) const
return m_edges[index];
}

inline meshkernel::Edge& meshkernel::Mesh::GetEdge(const UInt index)
inline void meshkernel::Mesh::SetEdge(const UInt index, const Edge& edge)
{
if (index >= GetNumEdges())
{
throw ConstraintError("The edge index, {}, is not in range.", index);
}

return m_edges[index];
m_administrationRequired = true;
m_edges[index] = edge;
}

inline const std::vector<meshkernel::Edge>& meshkernel::Mesh::Edges() const
Expand All @@ -577,4 +586,10 @@ inline void meshkernel::Mesh::SetEdges(const std::vector<Edge>& newValues)
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;
m_facesRTreeRequiresUpdate = true;
m_administrationRequired = true;
}

inline bool meshkernel::Mesh::AdministrationRequired() const
{
return m_administrationRequired;
}
12 changes: 8 additions & 4 deletions libs/MeshKernel/src/CasulliDeRefinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,18 +785,22 @@ bool meshkernel::CasulliDeRefinement::RemoveUnwantedBoundaryNodes(Mesh2D& mesh,
std::shift_left(mesh.m_facesNodes[connectedElementId].begin() + i, mesh.m_facesNodes[connectedElementId].end(), 1);
std::shift_left(mesh.m_facesEdges[connectedElementId].begin() + i, mesh.m_facesEdges[connectedElementId].end(), 1);

Edge edge = mesh.GetEdge(edgeId);

--mesh.m_numFacesNodes[connectedElementId];

// redirect node of the link that is kept
if (mesh.GetEdge(previousEdgeId).first == nodeId)
{
mesh.GetEdge(previousEdgeId).first = mesh.GetEdge(edgeId).first + mesh.GetEdge(edgeId).second - nodeId;
edge.first = mesh.GetEdge(edgeId).first + mesh.GetEdge(edgeId).second - nodeId;
}
else
{
mesh.GetEdge(previousEdgeId).second = mesh.GetEdge(edgeId).first + mesh.GetEdge(edgeId).second - nodeId;
edge.second = mesh.GetEdge(edgeId).first + mesh.GetEdge(edgeId).second - nodeId;
}

mesh.SetEdge(edgeId, edge);

// delete other link

if (CleanUpEdge(mesh, edgeId))
Expand Down Expand Up @@ -960,8 +964,8 @@ bool meshkernel::CasulliDeRefinement::CleanUpEdge(Mesh2D& mesh, const UInt edgeI
--mesh.m_nodesNumEdges[nodeId];
}

mesh.GetEdge(edgeId).first = constants::missing::uintValue;
mesh.GetEdge(edgeId).second = constants::missing::uintValue;
mesh.SetEdge(edgeId, {constants::missing::uintValue, constants::missing::uintValue});

return true;
}

Expand Down
45 changes: 45 additions & 0 deletions libs/MeshKernel/src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ void Mesh::InvalidateUnConnectedNodes(const std::vector<bool>& connectedNodes, U
{
undoAction->Add(ResetNode(n, {constants::missing::doubleValue, constants::missing::doubleValue}));
}

SetAdministrationRequired(true);
}

if (!m_nodes[n].IsValid())
Expand Down Expand Up @@ -244,6 +246,7 @@ void Mesh::DeleteInvalidNodesAndEdges()
const auto endEdgeVector = std::remove_if(m_edges.begin(), m_edges.end(), [](const Edge& e)
{ return e.first == constants::missing::uintValue || e.second == constants::missing::uintValue; });
m_edges.erase(endEdgeVector, m_edges.end());
SetAdministrationRequired(true);
}

void Mesh::SetUnConnectedNodesAndEdgesToInvalid(CompoundUndoAction* undoAction)
Expand Down Expand Up @@ -288,6 +291,8 @@ void Mesh::SetUnConnectedNodesAndEdgesToInvalid(CompoundUndoAction* undoAction)
{
undoAction->Add(ResetEdge(e, {constants::missing::uintValue, constants::missing::uintValue}));
}

SetAdministrationRequired(true);
}
}
}
Expand Down Expand Up @@ -347,6 +352,7 @@ std::unique_ptr<meshkernel::UndoAction> Mesh::MergeTwoNodes(UInt firstNodeIndex,
if (secondNodeSecondEdge == secondNodeIndex)
{
undoAction->Add(DeleteEdge(secondEdgeIndex));
SetAdministrationRequired(true);
}
}
}
Expand Down Expand Up @@ -379,10 +385,12 @@ std::unique_ptr<meshkernel::UndoAction> Mesh::MergeTwoNodes(UInt firstNodeIndex,
if (m_edges[edgeIndex].first == firstNodeIndex)
{
undoAction->Add(ResetEdge(edgeIndex, {secondNodeIndex, m_edges[edgeIndex].second}));
SetAdministrationRequired(true);
}
else if (m_edges[edgeIndex].second == firstNodeIndex)
{
undoAction->Add(ResetEdge(edgeIndex, {m_edges[edgeIndex].first, secondNodeIndex}));
SetAdministrationRequired(true);
}

numSecondNodeEdges++;
Expand Down Expand Up @@ -456,6 +464,7 @@ std::unique_ptr<meshkernel::UndoAction> Mesh::MergeNodesInPolygon(const Polygons
{
undoAction->Add(MergeTwoNodes(originalNodeIndices[i], originalNodeIndices[nodeIndexInFilteredNodes]));
nodesRtree->DeleteNode(i);
SetAdministrationRequired(true);
}
}
}
Expand All @@ -476,6 +485,7 @@ std::tuple<meshkernel::UInt, std::unique_ptr<meshkernel::AddNodeAction>> Mesh::I
std::unique_ptr<AddNodeAction> undoAction = AddNodeAction::Create(*this, newNodeIndex, newPoint);
CommitAction(*undoAction);

SetAdministrationRequired(true);
return {newNodeIndex, std::move(undoAction)};
}

Expand All @@ -492,6 +502,7 @@ std::tuple<meshkernel::UInt, std::unique_ptr<meshkernel::AddEdgeAction>> Mesh::C

std::unique_ptr<AddEdgeAction> undoAction = AddEdgeAction::Create(*this, newEdgeIndex, startNode, endNode);
CommitAction(*undoAction);
SetAdministrationRequired(true);
return {newEdgeIndex, std::move(undoAction)};
}

Expand All @@ -504,6 +515,7 @@ std::unique_ptr<meshkernel::ResetNodeAction> Mesh::ResetNode(const UInt nodeId,

std::unique_ptr<ResetNodeAction> undoAction = ResetNodeAction::Create(*this, nodeId, m_nodes[nodeId], newValue);
CommitAction(*undoAction);
SetAdministrationRequired(true);
return undoAction;
}

Expand All @@ -514,6 +526,7 @@ void Mesh::SetNode(const UInt nodeId, const Point& newValue)
throw ConstraintError("The node index, {}, is not in range.", nodeId);
}

SetAdministrationRequired(true);
m_nodes[nodeId] = newValue;
}

Expand All @@ -527,6 +540,7 @@ std::unique_ptr<meshkernel::DeleteEdgeAction> Mesh::DeleteEdge(UInt edge)
std::unique_ptr<meshkernel::DeleteEdgeAction> undoAction = DeleteEdgeAction::Create(*this, edge, m_edges[edge].first, m_edges[edge].second);

CommitAction(*undoAction);
SetAdministrationRequired(true);
return undoAction;
}

Expand All @@ -545,6 +559,7 @@ std::unique_ptr<meshkernel::DeleteNodeAction> Mesh::DeleteNode(UInt node)
undoAction->Add(DeleteEdge(edgeIndex));
}

SetAdministrationRequired(true);
CommitAction(*undoAction);
return undoAction;
}
Expand Down Expand Up @@ -759,6 +774,7 @@ std::unique_ptr<meshkernel::ResetEdgeAction> Mesh::ResetEdge(UInt edgeId, const
{
std::unique_ptr<meshkernel::ResetEdgeAction> undoAction = ResetEdgeAction::Create(*this, edgeId, m_edges[edgeId], edge);
CommitAction(*undoAction);
SetAdministrationRequired(true);
return undoAction;
}

Expand Down Expand Up @@ -858,7 +874,14 @@ void Mesh::SortEdgesInCounterClockWiseOrder(UInt startNode, UInt endNode)

void Mesh::Administrate(CompoundUndoAction* undoAction)
{
if (!AdministrationRequired())
{
return;
}

AdministrateNodesEdges(undoAction);

SetAdministrationRequired(false);
}

void Mesh::AdministrateNodesEdges(CompoundUndoAction* undoAction)
Expand Down Expand Up @@ -1003,6 +1026,7 @@ Mesh& Mesh::operator+=(Mesh const& rhs)
m_edges[e].second = rhs.m_edges[index].second + numNodes;
}

SetAdministrationRequired(true);
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;

Expand Down Expand Up @@ -1185,44 +1209,55 @@ void Mesh::BuildTree(Location location, const BoundingBox& boundingBox)
}
}

void Mesh::SetAdministrationRequired(const bool value)
{
m_administrationRequired = value;
}

//--------------------------------

void Mesh::CommitAction(const AddNodeAction& undoAction)
{
m_nodes[undoAction.NodeId()] = undoAction.Node();
m_nodesNumEdges[undoAction.NodeId()] = 0;
m_nodesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::CommitAction(const AddEdgeAction& undoAction)
{
m_edges[undoAction.EdgeId()] = undoAction.GetEdge();
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::CommitAction(const ResetNodeAction& undoAction)
{
m_nodes[undoAction.NodeId()] = undoAction.UpdatedNode();
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::CommitAction(const ResetEdgeAction& undoAction)
{
m_edges[undoAction.EdgeId()] = undoAction.UpdatedEdge();
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::CommitAction(const DeleteEdgeAction& undoAction)
{
m_edges[undoAction.EdgeId()] = {constants::missing::uintValue, constants::missing::uintValue};
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::CommitAction(const DeleteNodeAction& undoAction)
{
m_nodes[undoAction.NodeId()] = {constants::missing::doubleValue, constants::missing::doubleValue};
m_nodesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::CommitAction(NodeTranslationAction& undoAction)
Expand All @@ -1238,6 +1273,7 @@ void Mesh::CommitAction(MeshConversionAction& undoAction)
void Mesh::CommitAction(FullUnstructuredGridUndo& undoAction)
{
undoAction.Swap(m_nodes, m_edges);
SetAdministrationRequired(true);
Administrate();
}

Expand All @@ -1246,37 +1282,43 @@ void Mesh::RestoreAction(const AddNodeAction& undoAction)
m_nodes[undoAction.NodeId()] = Point(constants::missing::doubleValue, constants::missing::doubleValue);
m_nodesNumEdges[undoAction.NodeId()] = 0;
m_nodesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::RestoreAction(const AddEdgeAction& undoAction)
{
m_edges[undoAction.EdgeId()] = {constants::missing::uintValue, constants::missing::uintValue};
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::RestoreAction(const ResetNodeAction& undoAction)
{
m_nodes[undoAction.NodeId()] = undoAction.InitialNode();
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::RestoreAction(const ResetEdgeAction& undoAction)
{
m_edges[undoAction.EdgeId()] = undoAction.InitialEdge();
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::RestoreAction(const DeleteEdgeAction& undoAction)
{
m_edges[undoAction.EdgeId()] = undoAction.GetEdge();
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::RestoreAction(const DeleteNodeAction& undoAction)
{
m_nodes[undoAction.NodeId()] = undoAction.Node();
m_nodesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
}

void Mesh::RestoreAction(NodeTranslationAction& undoAction)
Expand All @@ -1292,5 +1334,8 @@ void Mesh::RestoreAction(MeshConversionAction& undoAction)
void Mesh::RestoreAction(FullUnstructuredGridUndo& undoAction)
{
undoAction.Swap(m_nodes, m_edges);
m_nodesRTreeRequiresUpdate = true;
m_edgesRTreeRequiresUpdate = true;
SetAdministrationRequired(true);
Administrate();
}
Loading

0 comments on commit 13df50d

Please sign in to comment.