Index: DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryPointStringTests.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryPointStringTests.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryPointStringTests.cs (revision 4904) @@ -0,0 +1,95 @@ +// 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 Deltares.DamEngine.Data.Geometry; +using NUnit.Framework; + +namespace Deltares.DamEngine.Data.Tests.Geometry; + +[TestFixture] +public class GeometryPointStringTests +{ + [Test] + public void GivenGeometryPointStringWithRedundantPoints_WhenRemovingUnnecessaryPoints_ThenCorrectGeometryPointStringReturned() + { + var geometryPointString = new GeometryPointString(); + + // The polyline contains only 3 identical points => expected 1 point + geometryPointString.CalcPoints.Add(new Point2D(2, 3)); + geometryPointString.CalcPoints.Add(new Point2D(2, 3)); + geometryPointString.CalcPoints.Add(new Point2D(2, 3)); + geometryPointString.RemoveUnnecessaryPoints(); + Assert.That(geometryPointString.CalcPoints, Has.Count.EqualTo(1)); + Assert.That(geometryPointString.CalcPoints[0].X, Is.EqualTo(2)); + Assert.That(geometryPointString.CalcPoints[0].Z, Is.EqualTo(3)); + + // The polyline contains 1 straight line with 3 points => expected 2 points + geometryPointString.CalcPoints.Clear(); + geometryPointString.CalcPoints.Add(new Point2D(2, 3)); + geometryPointString.CalcPoints.Add(new Point2D(3, 3)); + geometryPointString.CalcPoints.Add(new Point2D(4, 3)); + geometryPointString.RemoveUnnecessaryPoints(); + Assert.That(geometryPointString.CalcPoints, Has.Count.EqualTo(2)); + Assert.Multiple(() => + { + Assert.That(geometryPointString.CalcPoints[0].X, Is.EqualTo(2)); + Assert.That(geometryPointString.CalcPoints[0].Z, Is.EqualTo(3)); + Assert.That(geometryPointString.CalcPoints[1].X, Is.EqualTo(4)); + Assert.That(geometryPointString.CalcPoints[1].Z, Is.EqualTo(3)); + }); + + // The polyline contains 1 straight line with 5 points => expected 2 points + geometryPointString.CalcPoints.Clear(); + geometryPointString.CalcPoints.Add(new Point2D(2, 3)); + geometryPointString.CalcPoints.Add(new Point2D(3, 3)); + geometryPointString.CalcPoints.Add(new Point2D(4, 3)); + geometryPointString.CalcPoints.Add(new Point2D(5, 3)); + geometryPointString.CalcPoints.Add(new Point2D(6, 3)); + geometryPointString.RemoveUnnecessaryPoints(); + Assert.That(geometryPointString.CalcPoints, Has.Count.EqualTo(2)); + Assert.Multiple(() => + { + Assert.That(geometryPointString.CalcPoints[0].X, Is.EqualTo(2)); + Assert.That(geometryPointString.CalcPoints[0].Z, Is.EqualTo(3)); + Assert.That(geometryPointString.CalcPoints[1].X, Is.EqualTo(6)); + Assert.That(geometryPointString.CalcPoints[1].Z, Is.EqualTo(3)); + }); + + // The polyline contains 2 straight lines with 5 points => expected 3 points + geometryPointString.CalcPoints.Clear(); + geometryPointString.CalcPoints.Add(new Point2D(2, 3)); + geometryPointString.CalcPoints.Add(new Point2D(3, 4)); + geometryPointString.CalcPoints.Add(new Point2D(4, 5)); + geometryPointString.CalcPoints.Add(new Point2D(5, 4)); + geometryPointString.CalcPoints.Add(new Point2D(6, 3)); + geometryPointString.RemoveUnnecessaryPoints(); + Assert.That(geometryPointString.CalcPoints, Has.Count.EqualTo(3)); + Assert.Multiple(() => + { + Assert.That(geometryPointString.CalcPoints[0].X, Is.EqualTo(2)); + Assert.That(geometryPointString.CalcPoints[0].Z, Is.EqualTo(3)); + Assert.That(geometryPointString.CalcPoints[1].X, Is.EqualTo(4)); + Assert.That(geometryPointString.CalcPoints[1].Z, Is.EqualTo(5)); + Assert.That(geometryPointString.CalcPoints[2].X, Is.EqualTo(6)); + Assert.That(geometryPointString.CalcPoints[2].Z, Is.EqualTo(3)); + }); + } +} \ No newline at end of file Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryPointString.cs =================================================================== diff -u -r4897 -r4904 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryPointString.cs (.../GeometryPointString.cs) (revision 4897) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryPointString.cs (.../GeometryPointString.cs) (revision 4904) @@ -695,7 +695,7 @@ CondensePoints(); - for (int i = calcPoints.Count - 2; i > 1; i--) + for (int i = calcPoints.Count - 2; i > 0; i--) { // if the slope of the line before the point is equal to the slope after the point, the point can be removed double slopeBefore = (calcPoints[i].Z - calcPoints[i - 1].Z) / (calcPoints[i].X - calcPoints[i - 1].X);