// Copyright (C) Stichting Deltares 2024. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Deltares.DamEngine.Data.Geometry;
public static class GeometryHelper
{
///
/// Extends the geometry left to the given value of x.
///
/// The geometry to extend
/// The x position to move the boundary to.
public static void ExtendGeometryLeft(GeometryData geometry, double toX)
{
if (toX > geometry.Left)
{
throw new ArgumentException("The new left boundary should be smaller than the current left boundary.");
}
Point2D[] leftPoints = geometry.GetLeftPoints().OrderBy(x => x.Z).ToArray();
List leftCurves = geometry.GetLeftCurves();
Point2D prevPoint = null;
for (var i = 0; i < leftPoints.Count(); i++)
{
var newPoint = new Point2D(toX, leftPoints[i].Z);
geometry.Points.Add(newPoint);
var newhorizontalCurve = new GeometryCurve(newPoint, leftPoints[i]);
geometry.Curves.Add(newhorizontalCurve);
if (i > 0)
{
var newVerticalCurve = new GeometryCurve(newPoint, prevPoint);
geometry.Curves.Add(newVerticalCurve);
}
prevPoint = newPoint;
}
foreach (GeometryCurve geometryCurve in leftCurves)
{
// check if curve is vertical, only then it is on the "old" boundary and needs to be deleted
if (Math.Abs(geometryCurve.HeadPoint.X - geometryCurve.EndPoint.X) < GeometryConstants.Accuracy)
{
geometry.DeleteCurve(geometryCurve, false);
}
}
geometry.Rebox();
geometry.Left = toX;
}
///
/// Extends the geometry right to the given value of x.
///
/// The geometry to extend
/// The x position to move the boundary to.
public static void ExtendGeometryRight(GeometryData geometry, double toX)
{
if (toX < geometry.Right)
{
throw new ArgumentException("The new right boundary should be larger than the current right boundary.");
}
Point2D[] rightPoints = geometry.GetRightPoints().OrderBy(x => x.Z).ToArray();
List rightCurves = geometry.GetRightCurves();
Point2D prevPoint = null;
for (var i = 0; i < rightPoints.Length; i++)
{
var newPoint = new Point2D(toX, rightPoints[i].Z);
geometry.Points.Add(newPoint);
var newhorizontalCurve = new GeometryCurve(rightPoints[i], newPoint);
geometry.Curves.Add(newhorizontalCurve);
if (i > 0)
{
var newVerticalCurve = new GeometryCurve(newPoint, prevPoint);
geometry.Curves.Add(newVerticalCurve);
}
prevPoint = newPoint;
}
foreach (GeometryCurve geometryCurve in rightCurves)
{
// check if curve is vertical, only then it is on the "old" boundary and needs to be deleted
if (Math.Abs(geometryCurve.HeadPoint.X - geometryCurve.EndPoint.X) < GeometryConstants.Accuracy)
{
geometry.DeleteCurve(geometryCurve, false);
}
}
geometry.Rebox();
geometry.Right = toX;
}
}