// Copyright (C) Stichting Deltares 2025. 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.Collections.Generic;
namespace Deltares.DamEngine.Data.Geometry;
///
/// Helper class for Line objects
///
public static class LineHelper
{
///
/// Determine intersection between two lines (strict interpolation)
///
///
///
///
///
public static bool DetermineStrictIntersectionPoint(Line line1, Line line2, ref Point2D intersectPoint)
{
var point1 = new Point2D(line1.BeginPoint.X, line1.BeginPoint.Z);
var point2 = new Point2D(line1.EndPoint.X, line1.EndPoint.Z);
var point3 = new Point2D(line2.BeginPoint.X, line2.BeginPoint.Z);
var point4 = new Point2D(line2.EndPoint.X, line2.EndPoint.Z);
Point2D ip;
LineIntersection res = Routines2D.DetermineIf2DLinesIntersectStrickly(point1, point2, point3,
point4, out ip);
if (ip != null)
{
intersectPoint.X = ip.X;
intersectPoint.Z = ip.Z;
}
return res == LineIntersection.Intersects;
}
public static Point2D DetermineIntersectionPointWithExtrapolation(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
{
Point2D intersectPoint = null;
Routines2D.DetermineIf2DLinesIntersectWithExtrapolation(p1, p2, p3, p4, out intersectPoint);
return intersectPoint;
}
///
/// Deletes the duplicated points.
///
/// List of points
/// The tolerance.
public static void RemoveDuplicatedPoints(IList points, double tolerance)
{
// First build a list of indices of points that have to be removed (from end of list to start)
var indicesToDelete = new List();
for (int pointIndex = points.Count - 1; pointIndex > 0; pointIndex--)
{
int nextPointIndex = pointIndex - 1;
if (points[pointIndex].LocationEquals(points[pointIndex - 1], tolerance))
{
indicesToDelete.Add(nextPointIndex);
}
}
// Remove duplicated points beginning from the end
foreach (int index in indicesToDelete)
{
points.RemoveAt(index);
}
}
}