Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/Common/SoilProfile2DHelper.cs =================================================================== diff -u -r5928 -r5937 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/Common/SoilProfile2DHelper.cs (.../SoilProfile2DHelper.cs) (revision 5928) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/Common/SoilProfile2DHelper.cs (.../SoilProfile2DHelper.cs) (revision 5937) @@ -24,6 +24,7 @@ using System.Linq; using Deltares.DamEngine.Data.Geometry; using Deltares.DamEngine.Data.Geotechnics; +using Deltares.DamEngine.Data.Standard; namespace Deltares.DamEngine.Calculators.KernelWrappers.Common; @@ -43,6 +44,12 @@ Bottom } + private enum PolyLineConnectionType + { + SamePoint, + SameX + } + private const double deviationX = 1e-06; private const double toleranceAlmostEqual = 1e-09; @@ -282,13 +289,23 @@ return xCoordinatesAll.ToArray(); } + internal static List ConnectPolyLines(List polyLines) + { + List connectedPolyLinesSamePoint = ConnectPolyLines(PolyLineConnectionType.SamePoint, polyLines); + List connectedPolyLinesSameX = ConnectPolyLines(PolyLineConnectionType.SameX, connectedPolyLinesSamePoint); + return connectedPolyLinesSameX; + } + /// - /// Connect the poly-lines in the list when possible (i.e. when the end point of a poly-line coincides with the start point - /// of another poly-line. + /// Connect the poly-lines in the list when possible. Two types of connection are available: + /// - SamePoint = the end point of a poly-line coincides with the start point of another poly-line + /// - SameX = the X_end point of a poly-line coincides with the X_start point of another poly-line (connection is done only is + /// this other poly-line is unique) /// - /// + /// The type of connection for the poly-lines (same point or same X). + /// The list of poly-lines that must be connected when possible. /// A list of connected poly-lines. - internal static List ConnectPolyLines(List polyLines) + private static List ConnectPolyLines(PolyLineConnectionType connectionType, List polyLines) { var connectedPolyLines = new List(); var usedPolyLines = new HashSet(); @@ -311,20 +328,34 @@ if (usedPolyLines.Contains(nextPolyLine)) continue; - if (currentPolyLine.CalcPoints.Last().LocationEquals(nextPolyLine.CalcPoints.First())) + if (connectionType == PolyLineConnectionType.SamePoint) { - currentPolyLine.CalcPoints.AddRange(nextPolyLine.CalcPoints.Skip(1)); - usedPolyLines.Add(nextPolyLine); - isConnectionFound = true; - break; + if (currentPolyLine.CalcPoints.Last().LocationEquals(nextPolyLine.CalcPoints.First())) + { + currentPolyLine.CalcPoints.AddRange(nextPolyLine.CalcPoints.Skip(1)); + usedPolyLines.Add(nextPolyLine); + isConnectionFound = true; + break; + } } + else + { + bool isPolyLineUnique = polyLines.FindAll(p => p.CalcPoints.First().X.IsNearEqual(nextPolyLine.CalcPoints.First().X, toleranceAlmostEqual)).Count == 1; + if (currentPolyLine.CalcPoints.Last().X.IsNearEqual(nextPolyLine.CalcPoints.First().X, toleranceAlmostEqual) && isPolyLineUnique) + { + currentPolyLine.CalcPoints.AddRange(nextPolyLine.CalcPoints); + usedPolyLines.Add(nextPolyLine); + isConnectionFound = true; + break; + } + } } } while (isConnectionFound); currentPolyLine.SyncPoints(); connectedPolyLines.Add(currentPolyLine); } - + return connectedPolyLines; } Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators.Tests/KernelWrappers/Common/SoilProfile2DHelperTests.cs =================================================================== diff -u -r5928 -r5937 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators.Tests/KernelWrappers/Common/SoilProfile2DHelperTests.cs (.../SoilProfile2DHelperTests.cs) (revision 5928) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators.Tests/KernelWrappers/Common/SoilProfile2DHelperTests.cs (.../SoilProfile2DHelperTests.cs) (revision 5937) @@ -358,33 +358,25 @@ // Assert Assert.Multiple(() => { - Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(2)); - Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(2)); + Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(1)); + Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(1)); }); - GeometryPoint[] expectedTop1 = + GeometryPoint[] expectedTop = [ new(leftCoordinate, -10), - new(middleXCoordinate, -10) - ]; - GeometryPoint[] expectedTop2 = - [ + new(middleXCoordinate, -10), new(middleXCoordinate, -15), new(rightCoordinate, -15) ]; - GeometryPoint[] expectedBottom1 = + GeometryPoint[] expectedBottom = [ new(leftCoordinate, -20), - new(middleXCoordinate, -20) - ]; - GeometryPoint[] expectedBottom2 = - [ + new(middleXCoordinate, -20), new(middleXCoordinate, -21), new(rightCoordinate, -21) ]; - AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop1); - AssertGeometry(topLevelInBetweenAquifer[1].Points, expectedTop2); - AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom1); - AssertGeometry(bottomLevelInBetweenAquifer[1].Points, expectedBottom2); + AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop); + AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom); } /// @@ -461,16 +453,13 @@ // Assert Assert.Multiple(() => { - Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(2)); + Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(1)); Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(1)); }); - GeometryPoint[] expectedTop1 = + GeometryPoint[] expectedTop = [ new(leftCoordinate, -10), - new(middleXCoordinate, -10) - ]; - GeometryPoint[] expectedTop2 = - [ + new(middleXCoordinate, -10), new(middleXCoordinate, -5), new(rightCoordinate, -5) ]; @@ -480,8 +469,7 @@ new(middleXCoordinate, -20), new(rightCoordinate, -20) ]; - AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop1); - AssertGeometry(topLevelInBetweenAquifer[1].Points, expectedTop2); + AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop); AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom); } @@ -585,33 +573,25 @@ // Assert Assert.Multiple(() => { - Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(2)); - Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(2)); + Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(1)); + Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(1)); }); - GeometryPoint[] expectedTop1 = + GeometryPoint[] expectedTop = [ new(leftCoordinate, -10), - new(middleXCoordinate, -10) - ]; - GeometryPoint[] expectedTop2 = - [ + new(middleXCoordinate, -10), new(middleXCoordinate, -15), new(rightCoordinate, -15) ]; - GeometryPoint[] expectedBottom1 = + GeometryPoint[] expectedBottom = [ new(leftCoordinate, -20), - new(middleXCoordinate, -20) - ]; - GeometryPoint[] expectedBottom2 = - [ + new(middleXCoordinate, -20), new(middleXCoordinate, -17), new(rightCoordinate, -17) ]; - AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop1); - AssertGeometry(topLevelInBetweenAquifer[1].Points, expectedTop2); - AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom1); - AssertGeometry(bottomLevelInBetweenAquifer[1].Points, expectedBottom2); + AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop); + AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom); } /// @@ -714,33 +694,25 @@ // Assert Assert.Multiple(() => { - Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(2)); - Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(2)); + Assert.That(topLevelInBetweenAquifer, Has.Count.EqualTo(1)); + Assert.That(bottomLevelInBetweenAquifer, Has.Count.EqualTo(1)); }); - GeometryPoint[] expectedTop1 = + GeometryPoint[] expectedTop = [ new(leftCoordinate, -10), - new(middleXCoordinate, -10) - ]; - GeometryPoint[] expectedTop2 = - [ + new(middleXCoordinate, -10), new(middleXCoordinate, -5), new(rightCoordinate, -5) ]; - GeometryPoint[] expectedBottom1 = + GeometryPoint[] expectedBottom = [ new(leftCoordinate, -20), - new(middleXCoordinate, -20) - ]; - GeometryPoint[] expectedBottom2 = - [ + new(middleXCoordinate, -20), new(middleXCoordinate, -21), new(rightCoordinate, -21) ]; - AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop1); - AssertGeometry(topLevelInBetweenAquifer[1].Points, expectedTop2); - AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom1); - AssertGeometry(bottomLevelInBetweenAquifer[1].Points, expectedBottom2); + AssertGeometry(topLevelInBetweenAquifer[0].Points, expectedTop); + AssertGeometry(bottomLevelInBetweenAquifer[0].Points, expectedBottom); } /// @@ -1032,21 +1004,26 @@ polyLine3.CalcPoints.Add(new Point2D(9, 5)); polyLine3.CalcPoints.Add(new Point2D(6, 8)); + var polyLine4 = new GeometryPointString(); + polyLine4.CalcPoints.Add(new Point2D(15, 14)); + polyLine4.CalcPoints.Add(new Point2D(16, 14)); + var polyLines = new List { polyLine1, polyLine2, - polyLine3 + polyLine3, + polyLine4 }; // Call List connectedPolyLines = SoilProfile2DHelper.ConnectPolyLines(polyLines); - // Assert - Expected is that poly-lines 1 and 2 are connected but poly-line 3 not + // Assert - Expected is that poly-lines 1, 2 and 4 are connected but poly-line 3 not Assert.Multiple(() => { Assert.That(connectedPolyLines, Has.Count.EqualTo(2)); - Assert.That(connectedPolyLines[0].CalcPoints, Has.Count.EqualTo(5)); + Assert.That(connectedPolyLines[0].CalcPoints, Has.Count.EqualTo(7)); Assert.That(connectedPolyLines[1].CalcPoints, Has.Count.EqualTo(3)); Assert.That(connectedPolyLines[0].CalcPoints[0].X, Is.EqualTo(-1)); Assert.That(connectedPolyLines[0].CalcPoints[0].Z, Is.EqualTo(2)); @@ -1058,6 +1035,10 @@ Assert.That(connectedPolyLines[0].CalcPoints[3].Z, Is.EqualTo(1)); Assert.That(connectedPolyLines[0].CalcPoints[4].X, Is.EqualTo(15)); Assert.That(connectedPolyLines[0].CalcPoints[4].Z, Is.EqualTo(26)); + Assert.That(connectedPolyLines[0].CalcPoints[5].X, Is.EqualTo(15)); + Assert.That(connectedPolyLines[0].CalcPoints[5].Z, Is.EqualTo(14)); + Assert.That(connectedPolyLines[0].CalcPoints[6].X, Is.EqualTo(16)); + Assert.That(connectedPolyLines[0].CalcPoints[6].Z, Is.EqualTo(14)); }); }