Skip to content

Commit

Permalink
Introduce mkernel_curvilinear_delete_interior
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato committed Oct 31, 2023
1 parent 735e0f6 commit d6a85f9
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2021.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#pragma once

#include "CurvilinearGridAlgorithm.hpp"

namespace meshkernel
{

/// @brief A class implementing the curvilinear grid delete interior algorithm.
class CurvilinearGridDeleteInterior : public CurvilinearGridAlgorithm
{
public:
/// @brief Class constructor
/// @param[in] grid The input curvilinear grid
CurvilinearGridDeleteInterior(CurvilinearGrid& grid);

/// @brief Computes the deletion
void Compute() override;
};
} // namespace meshkernel
11 changes: 9 additions & 2 deletions libs/MeshKernel/src/CurvilinearGrid/CurvilinearGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,15 @@ std::tuple<CurvilinearGridNodeIndices, CurvilinearGridNodeIndices> CurvilinearGr
std::tuple<CurvilinearGridNodeIndices, CurvilinearGridNodeIndices>
CurvilinearGrid::ComputeBlockFromCornerPoints(const CurvilinearGridNodeIndices& firstNode, const CurvilinearGridNodeIndices& secondNode) const
{
return {{std::min(firstNode.m_m, secondNode.m_m), std::min(firstNode.m_n, secondNode.m_n)},
{std::max(firstNode.m_m, secondNode.m_m), std::max(firstNode.m_n, secondNode.m_n)}};
CurvilinearGridNodeIndices lowerLeft(std::min(firstNode.m_m, secondNode.m_m), std::min(firstNode.m_n, secondNode.m_n));
CurvilinearGridNodeIndices upperRight(std::max(firstNode.m_m, secondNode.m_m), std::max(firstNode.m_n, secondNode.m_n));

if (!lowerLeft.IsValid() || !upperRight.IsValid())
{
throw ConstraintError("Invalid index: first index - {{{}, {}}}, second index - {{{}, {}}}", lowerLeft.m_m, lowerLeft.m_n, upperRight.m_m, upperRight.m_n);
}

return {lowerLeft, upperRight};
}

void CurvilinearGrid::ComputeGridFacesMask()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ void CurvilinearGridAlgorithm::SetLine(Point const& firstPoint, Point const& sec
// Coinciding nodes, no valid line, nothing to do
if (newLineLowerLeft == newLineUpperRight)
{
throw std::invalid_argument("CurvilinearGridAlgorithm::SetLine the start and the end points of the line are coinciding");
throw AlgorithmError("The start and the end points of the line are coinciding");
}

// The points of the frozen line, must be on the same grid-line
if (!newLineLowerLeft.IsOnTheSameGridLine(newLineUpperRight))
{
throw std::invalid_argument("CurvilinearGridAlgorithm::SetLine the nodes do not define a grid line");
throw AlgorithmError("The nodes do not define a grid line");
}

CurvilinearGridLine const newGridline{newLineLowerLeft, newLineUpperRight};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//---- GPL ---------------------------------------------------------------------
//
// Copyright (C) Stichting Deltares, 2011-2021.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact: [email protected]
// Stichting Deltares
// P.O. Box 177
// 2600 MH Delft, The Netherlands
//
// All indications and logos of, and references to, "Delft3D" and "Deltares"
// are registered trademarks of Stichting Deltares, and remain the property of
// Stichting Deltares. All rights reserved.
//
//------------------------------------------------------------------------------

#include "MeshKernel/CurvilinearGrid/CurvilinearGridDeleteInterior.hpp"
#include <MeshKernel/CurvilinearGrid/CurvilinearGrid.hpp>
#include <MeshKernel/CurvilinearGrid/CurvilinearGridLine.hpp>

using meshkernel::CurvilinearGrid;
using meshkernel::CurvilinearGridLine;
using meshkernel::CurvilinearGridNodeIndices;

meshkernel::CurvilinearGridDeleteInterior::CurvilinearGridDeleteInterior(CurvilinearGrid& grid)
: CurvilinearGridAlgorithm(grid)
{
}

void meshkernel::CurvilinearGridDeleteInterior::Compute()
{

if (m_lowerLeft.m_m >= m_grid.m_numM || m_lowerLeft.m_n >= m_grid.m_numN)
{
throw ConstraintError("Invalid index: first index {{{}, {}}} not in mesh limits {{{}, {}}}", m_lowerLeft.m_m, m_lowerLeft.m_n, m_grid.m_numM, m_grid.m_numN);
}

if (m_upperRight.m_m >= m_grid.m_numM || m_upperRight.m_n >= m_grid.m_numN)
{
throw ConstraintError("Invalid index: second index {{{}, {}}} not in mesh limits {{{}, {}}}", m_upperRight.m_m, m_upperRight.m_n, m_grid.m_numM, m_grid.m_numN);
}

UInt lowerLimitI = m_lowerLeft.m_n;
UInt upperLimitI = m_upperRight.m_n;

UInt lowerLimitJ = m_lowerLeft.m_m;
UInt upperLimitJ = m_upperRight.m_m;

for (UInt n = lowerLimitI + 1; n < upperLimitI; ++n)
{
for (UInt m = lowerLimitJ + 1; m < upperLimitJ; ++m)
{
m_grid.m_gridNodes(n, m).SetInvalid();
}
}
}
12 changes: 6 additions & 6 deletions libs/MeshKernelApi/include/MeshKernelApi/MeshKernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ namespace meshkernelapi
/// @brief Converts a curvilinear grid to an unstructured mesh
MKERNEL_API int mkernel_curvilinear_convert_to_mesh2d(int meshKernelId);

/// @brief Delete the interior part of a curvilinear gris
/// @brief Delete the interior part of a curvilinear gris
/// @param meshKernelId The id of the mesh state
/// @param[in] xFirstPointCoordinate The x coordinate of the first point
/// @param[in] yFirstPointCoordinate The y coordinate of the first point
/// @param[in] xSecondPointCoordinate The x coordinate of the second point
/// @param[in] ySecondPointCoordinate The y coordinate of the second point
/// @return Error code
//MKERNEL_API int mkernel_curvilinear_delete_interior(int meshKernelId,
// double xFirstPointCoordinate,
// double yFirstPointCoordinate,
// double xSecondPointCoordinate,
// double ySecondPointCoordinate);
MKERNEL_API int mkernel_curvilinear_delete_interior(int meshKernelId,
double xFirstPointCoordinate,
double yFirstPointCoordinate,
double xSecondPointCoordinate,
double ySecondPointCoordinate);

/// @brief Delete the node closest to a point
/// @param meshKernelId The id of the mesh state
Expand Down
76 changes: 38 additions & 38 deletions libs/MeshKernelApi/src/MeshKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <MeshKernel/Contacts.hpp>
#include <MeshKernel/CurvilinearGrid/CurvilinearGrid.hpp>
#include <MeshKernel/CurvilinearGrid/CurvilinearGridAlgorithm.hpp>
#include "MeshKernel/CurvilinearGrid/CurvilinearGridDeleteInterior.hpp"
#include <MeshKernel/CurvilinearGrid/CurvilinearGridDeRefinement.hpp>
#include <MeshKernel/CurvilinearGrid/CurvilinearGridFromPolygon.hpp>
#include <MeshKernel/CurvilinearGrid/CurvilinearGridFromSplines.hpp>
Expand Down Expand Up @@ -3226,6 +3227,43 @@ namespace meshkernelapi
return lastExitCode;
}

MKERNEL_API int mkernel_curvilinear_delete_interior(int meshKernelId,
double xFirstPointCoordinate,
double yFirstPointCoordinate,
double xSecondPointCoordinate,
double ySecondPointCoordinate)
{
lastExitCode = meshkernel::ExitCode::Success;
try
{
if (!meshKernelState.contains(meshKernelId))
{
throw meshkernel::MeshKernelError("The selected mesh kernel id does not exist.");
}

if (meshKernelState[meshKernelId].m_curvilinearGrid == nullptr)
{
throw meshkernel::MeshKernelError("Not a valid curvilinear grid instance.");
}

if (!meshKernelState[meshKernelId].m_curvilinearGrid->IsValid())
{
throw meshkernel::MeshKernelError("Not valid curvilinear grid.");
}
meshkernel::CurvilinearGridDeleteInterior curvilinearDeleteInterior(*meshKernelState[meshKernelId].m_curvilinearGrid);

curvilinearDeleteInterior.SetBlock({xFirstPointCoordinate, yFirstPointCoordinate},
{xSecondPointCoordinate, ySecondPointCoordinate});

curvilinearDeleteInterior.Compute();
}
catch (...)
{
lastExitCode = HandleException();
}
return lastExitCode;
}

MKERNEL_API int mkernel_curvilinear_line_attraction_repulsion(int meshKernelId,
double repulsionParameter,
double xFirstNodeOnTheLine,
Expand Down Expand Up @@ -3302,44 +3340,6 @@ namespace meshkernelapi
return lastExitCode;
}

/*
MKERNEL_API int mkernel_curvilinear_delete_interior(int meshKernelId,
double xFirstPointCoordinate,
double yFirstPointCoordinate,
double xSecondPointCoordinate,
double ySecondPointCoordinate)
{
lastExitCode = meshkernel::ExitCode::Success;
try
{
if (!meshKernelState.contains(meshKernelId))
{
throw meshkernel::MeshKernelError("The selected mesh kernel id does not exist.");
}
if (meshKernelState[meshKernelId].m_curvilinearGrid == nullptr)
{
throw meshkernel::MeshKernelError("Not a valid curvilinear grid instance.");
}
if (!meshKernelState[meshKernelId].m_curvilinearGrid->IsValid())
{
throw meshkernel::MeshKernelError("Not valid curvilinear grid.");
}
meshKernelState[meshKernelId].m_curvilinearGrid.SetBlock(firstPoint, secondPoint);
meshKernelState[meshKernelId].m_curvilinearGrid->DeleteInterior({xFirstPointCoordinate, yFirstPointCoordinate},
{xSecondPointCoordinate, ySecondPointCoordinate});
}
catch (...)
{
lastExitCode = HandleException();
}
return lastExitCode;
}
*/

MKERNEL_API int mkernel_curvilinear_delete_node(int meshKernelId,
double xPointCoordinate,
double yPointCoordinate)
Expand Down

0 comments on commit d6a85f9

Please sign in to comment.