Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/LineHelper.cs =================================================================== diff -u -r4897 -r4905 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/LineHelper.cs (.../LineHelper.cs) (revision 4897) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/LineHelper.cs (.../LineHelper.cs) (revision 4905) @@ -174,4 +174,29 @@ 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); + } + } } \ No newline at end of file Index: DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/LineHelperTests.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/LineHelperTests.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/LineHelperTests.cs (revision 4905) @@ -0,0 +1,61 @@ +// 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.Collections.Generic; +using Deltares.DamEngine.Data.Geometry; +using NUnit.Framework; + +namespace Deltares.DamEngine.Data.Tests.Geometry; + +[TestFixture] +public class LineHelperTests +{ + [Test] + public void Given2DPointsListWithDuplicatedPoints_WhenRemovingDuplicatedPoints_ThenCorrectListReturned() + { + var pointList = new List(); + + // The list contains only 2 times coinciding points => expected 3 points + pointList.Add(new Point2D(2, 3)); + pointList.Add(new Point2D(2, 3)); + pointList.Add(new Point2D(4, 3)); + pointList.Add(new Point2D(2, 3)); + pointList.Add(new Point2D(2, 3)); + pointList.Add(new Point2D(2, 3)); + pointList.Add(new Point2D(1, 2)); + pointList.Add(new Point2D(1, 2)); + + LineHelper.RemoveDuplicatedPoints(pointList, 1e-12); + + Assert.That(pointList, Has.Count.EqualTo(4)); + Assert.Multiple(() => + { + Assert.That(pointList[0].X, Is.EqualTo(2)); + Assert.That(pointList[0].Z, Is.EqualTo(3)); + Assert.That(pointList[1].X, Is.EqualTo(4)); + Assert.That(pointList[1].Z, Is.EqualTo(3)); + Assert.That(pointList[2].X, Is.EqualTo(2)); + Assert.That(pointList[2].Z, Is.EqualTo(3)); + Assert.That(pointList[3].X, Is.EqualTo(1)); + Assert.That(pointList[3].Z, Is.EqualTo(2)); + }); + } +} \ No newline at end of file