// 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;
///
/// Helper class for Line objects
///
public static class LineHelper
{
///
/// Calculate intersection between two lines (strict interpolation)
///
///
///
///
///
public static bool GetStrictIntersectionPoint(Line line1, Line line2, ref GeometryPoint 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 GeometryPoint GetIntersectionPointWithExtrapolation(GeometryPoint p1, GeometryPoint p2, GeometryPoint p3, GeometryPoint p4)
{
return IntersectionPointWithExtrapolation(p1, p2, p3, p4);
}
///
/// Determines the intersection point of two lines, allowing the intersection point being extrapolated.
///
///
///
///
///
/// Intersection point or null (parallel lines)
private static GeometryPoint IntersectionPointWithExtrapolation(GeometryPoint p1, GeometryPoint p2, GeometryPoint p3, GeometryPoint p4)
{
GeometryPoint intersectPoint = null;
var point1 = new Point2D(p1.X, p1.Z);
var point2 = new Point2D(p2.X, p2.Z);
var point3 = new Point2D(p3.X, p3.Z);
var point4 = new Point2D(p4.X, p4.Z);
Routines2D.DetermineIf2DLinesIntersectWithExtrapolation(point1, point2, point3, point4, out Point2D ip);
if (ip != null)
{
intersectPoint = new GeometryPoint(ip.X, ip.Z);
}
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);
}
}
}