// Copyright (C) Stichting Deltares 2025. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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, 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 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 . // // 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; using Deltares.Dam.Data; using Deltares.Geometry; using NUnit.Framework; namespace Deltares.Dam.Tests { [TestFixture] public class PolyLineTest { [SetUp] public void FixtureSetup() {} [TearDown] public void FixtureTearDown() {} [Test] public void LineWithoutPointsShouldNotExist() { var polyLine = new PolyLine(); Assert.That(polyLine.Exists(), Is.False); } [Test] public void LineWithOnePointShouldNotExist() { var polyLine = new PolyLine(); polyLine.Points.Add(new GeometryPoint()); Assert.That(polyLine.Exists(), Is.False); } [Test] public void LineWithTwoPointsShouldExist() { var polyLine = new PolyLine(); polyLine.Points.Add(new GeometryPoint()); polyLine.Points.Add(new GeometryPoint { X = 1.0 }); Assert.That(polyLine.Exists(), Is.True); } [Test] public void CheckIfLineWithAscendingXIsEvaluatedCorrectlyAsAscending() { PolyLine polyLine = CreateStrictAscendingPolyLine(); Assert.That(polyLine.IsXAscending(), Is.True); } [Test] public void CheckIfLineWithNotStrictAscendingXIsEvaluatedCorrectlyAsAscending() { PolyLine polyLine = CreateNotStrictAscendingPolyLine(); Assert.That(polyLine.IsXAscending(), Is.True); } [Test] public void CheckIfLineWithNotStrictAscendingXIsEvaluatedCorrectlyAsNotStrictAscending() { PolyLine polyLine = CreateNotStrictAscendingPolyLine(); Assert.That(polyLine.IsXStrictAscending(), Is.False); } [Test] public void CheckIfLineWithNotAscendingXIsEvaluatedCorrectlyAsNotAscending() { PolyLine polyLine = CreateNotAscendingPolyLine(); Assert.That(polyLine.IsXAscending(), Is.False); } [Test] public void ThrowsExceptionOnNotStrictAscendingXPolylineYFromXCalculation() { PolyLine polyLine = CreateNotAscendingPolyLine(); Assert.That(() => polyLine.YFromX(1.0), Throws.InstanceOf()); } [Test] public void CheckIfYFromXEvaluatesCorrectly() { const double cTolerance = 0.0001; PolyLine polyLine = CreateStrictAscendingPolyLine(); Assert.That(polyLine.YFromX(1.0), Is.EqualTo(5.0).Within(cTolerance)); Assert.That(polyLine.YFromX(4.0), Is.EqualTo(9.0).Within(cTolerance)); } [Test] public void CheckIfYFromXEvaluatesCorrectlyWithOutOfBoundsXValues() { const double cTolerance = 0.0001; PolyLine polyLine = CreateStrictAscendingPolyLine(); Assert.That(polyLine.YFromX(-3.0), Is.EqualTo(0.0).Within(cTolerance)); Assert.That(polyLine.YFromX(10.0), Is.EqualTo(7.0).Within(cTolerance)); } /// /// If vertical line segment is present (i.e. two points have same X-coord.) then the Y value form the first point will be returned /// [Test] public void CheckIfYFromXEvaluatesCorrectlyWithNotStrictAscendingPolyLine() { const double cTolerance = 0.0001; PolyLine polyLine = CreateNotStrictAscendingPolyLine(); Assert.That(polyLine.YFromX(1.0), Is.EqualTo(5.0).Within(cTolerance)); Assert.That(polyLine.YFromX(3.0), Is.EqualTo(10.0).Within(cTolerance)); } /// /// Check if Equals works with unequal lines /// [Test] public void EqualsReturnsFalseWithUnequalLines() { PolyLine polyLine1 = CreateStrictAscendingPolyLine(); PolyLine polyLine2 = CreateNotStrictAscendingPolyLine(); Assert.That(polyLine1.Equals(polyLine2), Is.False); } /// /// Check if considing points are deleted /// [Test] public void CheckIfDeleteCoinsidingPointsReturnsLessPoints() { PolyLine polyLine1 = CreateStrictAscendingPolyLine(); polyLine1.Points.Add(polyLine1.Points[0]); int currentLength = polyLine1.Points.Count; polyLine1.DeleteCoinsidingPoints(); Assert.That(polyLine1.Points.Count, Is.EqualTo(currentLength - 1)); } /// /// Check if Equals works with equal lines /// [Test] public void EqualsReturnsTrueWithEqualLines() { PolyLine polyLine1 = CreateStrictAscendingPolyLine(); PolyLine polyLine2 = CreateStrictAscendingPolyLine(); Assert.That(polyLine1.Equals(polyLine2), Is.True); } /// /// Check if Equals works with equal lines /// [Test] public void TestOnePointIntersectionPointsXzWithLineXz() { PolyLine polyLine1 = CreateIntersectPolyLine(); var line = new Line { BeginPoint = new GeometryPoint(-5, 0, -1), EndPoint = new GeometryPoint(5, 0, -1) }; IList intersectionPoints = polyLine1.IntersectionPointsXzWithLineXz(line); Assert.That(intersectionPoints, Has.Count.EqualTo(1)); Assert.That(intersectionPoints[0].X, Is.EqualTo(1.0).Within(GeometryPoint.Precision)); Assert.That(intersectionPoints[0].Z, Is.EqualTo(-1.0).Within(GeometryPoint.Precision)); } [Test] public void TestNoPointIntersectionPointsXzWithLineXz() { PolyLine polyLine1 = CreateIntersectPolyLine(); var line = new Line { BeginPoint = new GeometryPoint(-5, 0, -1), EndPoint = new GeometryPoint(-3, 0, -1) }; IList intersectionPoints = polyLine1.IntersectionPointsXzWithLineXz(line); Assert.That(intersectionPoints, Is.Empty); } [Test] public void TestTwoPointIntersectionPointsXzWithLineXz() { PolyLine polyLine1 = CreateIntersectPolyLine(); var line = new Line { BeginPoint = new GeometryPoint(-5, 0, -1), EndPoint = new GeometryPoint(15, 0, -1) }; IList intersectionPoints = polyLine1.IntersectionPointsXzWithLineXz(line); Assert.That(intersectionPoints, Has.Count.EqualTo(2)); Assert.That(intersectionPoints[0].X, Is.EqualTo(1.0).Within(GeometryPoint.Precision)); Assert.That(intersectionPoints[0].Z, Is.EqualTo(-1.0).Within(GeometryPoint.Precision)); Assert.That(intersectionPoints[1].X, Is.EqualTo(11.0).Within(GeometryPoint.Precision)); Assert.That(intersectionPoints[1].Z, Is.EqualTo(-1.0).Within(GeometryPoint.Precision)); } private PolyLine CreateStrictAscendingPolyLine() { var polyLine = new PolyLine(); polyLine.Points.Add(new GeometryPoint { X = -1.0 }); polyLine.Points.Add(new GeometryPoint { X = 3.0, Y = 10.0 }); polyLine.Points.Add(new GeometryPoint { X = 6.0, Y = 7.0 }); return polyLine; } private PolyLine CreateNotAscendingPolyLine() { var polyLine = new PolyLine(); polyLine.Points.Add(GeometryPoint.CreateNewXYPoint(0.0, 0.0)); polyLine.Points.Add(GeometryPoint.CreateNewXYPoint(3.0, 10.0)); polyLine.Points.Add(GeometryPoint.CreateNewXYPoint(1.0, 20.0)); return polyLine; } private PolyLine CreateIntersectPolyLine() { var polyLine = new PolyLine(); polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(-10.0, 0.0)); polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(0.0, 0.0)); polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(2.0, -2.0)); polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(10.0, -2.0)); polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(12.0, 0.0)); return polyLine; } private PolyLine CreateNotStrictAscendingPolyLine() { var polyLine = new PolyLine(); polyLine.Points.Add(new GeometryPoint { X = -1.0 }); polyLine.Points.Add(new GeometryPoint { X = 3.0, Y = 10.0 }); polyLine.Points.Add(new GeometryPoint { X = 3.0, Y = 6.0 }); polyLine.Points.Add(new GeometryPoint { X = 6.0, Y = 7.0 }); return polyLine; } } }