// Copyright (C) Stichting Deltares 2024. 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.IsFalse(polyLine.Exists()); } [Test] public void LineWithOnePointShouldNotExist() { var polyLine = new PolyLine(); polyLine.Points.Add(new GeometryPoint()); Assert.IsFalse(polyLine.Exists()); } [Test] public void LineWithTwoPointsShouldExist() { var polyLine = new PolyLine(); polyLine.Points.Add(new GeometryPoint()); polyLine.Points.Add(new GeometryPoint { X = 1.0 }); Assert.IsTrue(polyLine.Exists()); } [Test] public void CheckIfLineWithAscendingXIsEvaluatedCorrectlyAsAscending() { PolyLine polyLine = CreateStrictAscendingPolyLine(); Assert.IsTrue(polyLine.IsXAscending()); } [Test] public void CheckIfLineWithNotStrictAscendingXIsEvaluatedCorrectlyAsAscending() { PolyLine polyLine = CreateNotStrictAscendingPolyLine(); Assert.IsTrue(polyLine.IsXAscending()); } [Test] public void CheckIfLineWithNotStrictAscendingXIsEvaluatedCorrectlyAsNotStrictAscending() { PolyLine polyLine = CreateNotStrictAscendingPolyLine(); Assert.IsFalse(polyLine.IsXStrictAscending()); } [Test] public void CheckIfLineWithNotAscendingXIsEvaluatedCorrectlyAsNotAscending() { PolyLine polyLine = CreateNotAscendingPolyLine(); Assert.IsFalse(polyLine.IsXAscending()); } [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.AreEqual(5.0, polyLine.YFromX(1.0), cTolerance); Assert.AreEqual(9.0, polyLine.YFromX(4.0), cTolerance); } [Test] public void CheckIfYFromXEvaluatesCorrectlyWithOutOfBoundsXValues() { const double cTolerance = 0.0001; PolyLine polyLine = CreateStrictAscendingPolyLine(); Assert.AreEqual(0.0, polyLine.YFromX(-3.0), cTolerance); Assert.AreEqual(7.0, polyLine.YFromX(10.0), 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.AreEqual(5.0, polyLine.YFromX(1.0), cTolerance); Assert.AreEqual(10.0, polyLine.YFromX(3.0), cTolerance); } /// /// Check if Equals works with unequal lines /// [Test] public void EqualsReturnsFalseWithUnequalLines() { PolyLine polyLine1 = CreateStrictAscendingPolyLine(); PolyLine polyLine2 = CreateNotStrictAscendingPolyLine(); Assert.IsFalse(polyLine1.Equals(polyLine2)); } /// /// 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.AreEqual(currentLength - 1, polyLine1.Points.Count); } /// /// Check if Equals works with equal lines /// [Test] public void EqualsReturnsTrueWithEqualLines() { PolyLine polyLine1 = CreateStrictAscendingPolyLine(); PolyLine polyLine2 = CreateStrictAscendingPolyLine(); Assert.IsTrue(polyLine1.Equals(polyLine2)); } /// /// 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.IsTrue(intersectionPoints.Count == 1); Assert.AreEqual(1.0, intersectionPoints[0].X, GeometryPoint.Precision); Assert.AreEqual(-1.0, intersectionPoints[0].Z, 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.IsTrue(intersectionPoints.Count == 0); } [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.IsTrue(intersectionPoints.Count == 2); Assert.AreEqual(1.0, intersectionPoints[0].X, GeometryPoint.Precision); Assert.AreEqual(-1.0, intersectionPoints[0].Z, GeometryPoint.Precision); Assert.AreEqual(11.0, intersectionPoints[1].X, GeometryPoint.Precision); Assert.AreEqual(-1.0, intersectionPoints[1].Z, 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; } } }