Index: DamClients/DamUI/trunk/src/Dam/Tests/DamEngineIo/XMLSoilProfile2DConverterTest.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/Dam/Tests/DamEngineIo/XMLSoilProfile2DConverterTest.cs (revision 0) +++ DamClients/DamUI/trunk/src/Dam/Tests/DamEngineIo/XMLSoilProfile2DConverterTest.cs (revision 3034) @@ -0,0 +1,223 @@ +// Copyright (C) Stichting Deltares 2020. 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; +using System.Collections.Generic; +using System.Linq; +using Deltares.Dam.Data.DamEngineIo; +using Deltares.Geometry; +using Deltares.Geotechnics.Soils; +using NUnit.Framework; +using SoilProfile2D = Deltares.Geotechnics.Soils.SoilProfile2D; +using XMLSoilProfile2D = Deltares.DamEngine.Io.XmlInput.SoilProfile2D; +using XMLSoilLayer2D = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2D; +using XMLSoilLayer2DSurface = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2DSurface; +using XMLSurfaceOuterPoint = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2DSurfaceOuterPoint; +using XMLSurfaceInnerPoint = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2DSurfaceInnerPoint; + +namespace Deltares.Dam.Tests.DamEngineIo +{ + [TestFixture] + public class XMLSoilProfile2DConverterTest + { + + [Test] + public void Convert_SoilProfileNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => XMLSoilProfile2DConverter.Convert(null); + + // Assert + Assert.That(call, Throws.TypeOf() + .With.Property(nameof(ArgumentNullException.ParamName)) + .EqualTo("soilProfile")); + } + + /// + /// ------------------------------------------------------------- + /// Layer three + /// |----------------------------------------------------| + /// | | + /// | | + /// | | + /// |----------------------------------------------------| + /// + /// ------------------------------------------------------------- + /// Layer two + /// |------------------------| |---------------------| + /// | | | | + /// | | | | + /// |------------------------| |---------------------| + /// + /// ------------------------------------------------------------- + /// Layer one + /// + /// + /// + /// ------------------------------------------------------------- + /// + [Test] + public void Convert_WithValidArgument_ReturnsExpectedSoilProfile2D() + { + // Setup + var layerOne = new SoilLayer2D + { + Name = "LayerOne", + IsAquifer = true, + GeometrySurface = new GeometrySurface() + }; + layerOne.GeometrySurface.OuterLoop = CreateLoop(new GeometryPoint(0, 0, -15), + new GeometryPoint(10, 0, -15), + new GeometryPoint(0, 0, -10), + new GeometryPoint(10, 0, -10)); + + var layerTwo = new SoilLayer2D + { + Name = "LayerTwo", + IsAquifer = true, + GeometrySurface = new GeometrySurface() + }; +// layerTwo.GeometrySurface.InnerLoops.AddRange(new [] +// { +// CreateLoop(new GeometryPoint(6, 0, -9), +// new GeometryPoint(9, 0, -9), +// new GeometryPoint(6, 0, -6), +// new GeometryPoint(9, 0, -6)), +// CreateLoop(new GeometryPoint(1, 0, -9), +// new GeometryPoint(4, 0, -9), +// new GeometryPoint(1, 0, -6), +// new GeometryPoint(4, 0, -6)) +// }); + layerTwo.GeometrySurface.OuterLoop = CreateLoop(new GeometryPoint(0, 0, -10), + new GeometryPoint(10, 0, -10), + new GeometryPoint(0, 0, -5), + new GeometryPoint(10, 0, -5)); + + var layerThree = new SoilLayer2D + { + Name = "LayerThree", + IsAquifer = false, + GeometrySurface = new GeometrySurface() + }; + layerThree.GeometrySurface.InnerLoops.AddRange(new[] + { + CreateLoop(new GeometryPoint(6, 0, -4), + new GeometryPoint(9, 0, -4), + new GeometryPoint(6, 0, -1), + new GeometryPoint(9, 0, -1)) + }); + layerThree.GeometrySurface.OuterLoop = CreateLoop(new GeometryPoint(0, 0, -5), + new GeometryPoint(10, 0, -5), + new GeometryPoint(0, 0, 0), + new GeometryPoint(10, 0, 0)); + + var soilProfile = new SoilProfile2D + { + Name = "SoilProfile2D" + }; + soilProfile.Surfaces.Add(layerTwo); + soilProfile.Surfaces.Add(layerThree); + + // Call + XMLSoilProfile2D convertedProfile = XMLSoilProfile2DConverter.Convert(soilProfile); + + // Assert + Assert.That(convertedProfile.Name, Is.SameAs(soilProfile.Name)); + Assert.That(convertedProfile.PreconsolidationStresses, Is.Empty); // Don't do anything with preconsolidation stresses yet + + int nrOfExpectedSoilLayers = soilProfile.Surfaces.Count; + Assert.That(convertedProfile.Layers2D, Has.Length.EqualTo(nrOfExpectedSoilLayers)); + for (int i = 0; i < nrOfExpectedSoilLayers; i++) + { + AssertLayer(soilProfile.Surfaces[i], convertedProfile.Layers2D[i]); + } + } + + private static void AssertLayer(SoilLayer2D soilLayer, XMLSoilLayer2D convertedSoilLayer) + { + Assert.That(convertedSoilLayer.SoilName, Is.EqualTo(soilLayer.Name)); + Assert.That(convertedSoilLayer.IsAquifer, Is.EqualTo(soilLayer.IsAquifer)); + Assert.That(convertedSoilLayer.WaterpressureInterpolationModel, Is.EqualTo(0)); // Water pressure interpolation model is not instantiated. + + AssertSurface(soilLayer.GeometrySurface, convertedSoilLayer.Surface); + } + + private static void AssertSurface(GeometrySurface surface, XMLSoilLayer2DSurface convertedSurface) + { + IList outerLoopPoints = surface.OuterLoop.Points; + int nrOfExpectedOuterLoopPoints = outerLoopPoints.Count; + + var convertedOuterLoop = convertedSurface.OuterLoop; + Assert.That(convertedOuterLoop, Has.Length.EqualTo(nrOfExpectedOuterLoopPoints)); + for (int i = 0; i < nrOfExpectedOuterLoopPoints; i++) + { + AssertPoint(outerLoopPoints[i], convertedOuterLoop[i]); + } + + // Current XSD supports only ONE innerloop + GeometryLoop innerLoop = surface.InnerLoops.FirstOrDefault(); + var convertedInnerLoop = convertedSurface.Innerloop; + if (innerLoop == null) + { + Assert.That(convertedInnerLoop, Is.Empty); + return; + } + + IList innerLoopPoints = innerLoop.Points; + int nrOfExpectedInnerLoopPoints = innerLoopPoints.Count; + + Assert.That(convertedInnerLoop, Has.Length.EqualTo(nrOfExpectedInnerLoopPoints)); + for (int i = 0; i < nrOfExpectedInnerLoopPoints; i++) + { + AssertPoint(innerLoopPoints[i], convertedInnerLoop[i]); + } + } + + private static void AssertPoint(GeometryPoint point, XMLSurfaceOuterPoint convertedPoint) + { + Assert.That(convertedPoint.X, Is.EqualTo(point.X)); + Assert.That(convertedPoint.Z, Is.EqualTo(point.Z)); + } + + private static void AssertPoint(GeometryPoint point, XMLSurfaceInnerPoint convertedPoint) + { + Assert.That(convertedPoint.X, Is.EqualTo(point.X)); + Assert.That(convertedPoint.Z, Is.EqualTo(point.Z)); + } + + private static GeometryLoop CreateLoop(GeometryPoint bottomLeftPoint, + GeometryPoint bottomRightPoint, + GeometryPoint topLeftPoint, + GeometryPoint topRightPoint) + { + var loop = new GeometryLoop(); + loop.CurveList.AddRange(new [] + { + new GeometryCurve(bottomLeftPoint, bottomRightPoint), + new GeometryCurve(bottomLeftPoint, topLeftPoint), + new GeometryCurve(topLeftPoint, topRightPoint), + new GeometryCurve(bottomRightPoint, topRightPoint) + }); + + return loop; + } + } +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Deltares.Dam.Data.csproj =================================================================== diff -u -r3015 -r3034 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Deltares.Dam.Data.csproj (.../Deltares.Dam.Data.csproj) (revision 3015) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Deltares.Dam.Data.csproj (.../Deltares.Dam.Data.csproj) (revision 3034) @@ -188,6 +188,7 @@ + Index: DamClients/DamUI/trunk/src/Dam/Tests/Deltares.Dam.Tests.csproj =================================================================== diff -u -r3015 -r3034 --- DamClients/DamUI/trunk/src/Dam/Tests/Deltares.Dam.Tests.csproj (.../Deltares.Dam.Tests.csproj) (revision 3015) +++ DamClients/DamUI/trunk/src/Dam/Tests/Deltares.Dam.Tests.csproj (.../Deltares.Dam.Tests.csproj) (revision 3034) @@ -135,6 +135,7 @@ + Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/DamEngineIo/XMLSoilProfile2DConverter.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/DamEngineIo/XMLSoilProfile2DConverter.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/DamEngineIo/XMLSoilProfile2DConverter.cs (revision 3034) @@ -0,0 +1,122 @@ +// Copyright (C) Stichting Deltares 2020. All rights reserved. +// +// This file is part of the application DAM - Clients Library. +// +// 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; +using System.Collections.Generic; +using System.Linq; +using Deltares.DamEngine.Io.XmlInput; +using Deltares.Geometry; +using Deltares.Geotechnics.Soils; +using SoilProfile2D = Deltares.Geotechnics.Soils.SoilProfile2D; +using XMLSoilProfile2D = Deltares.DamEngine.Io.XmlInput.SoilProfile2D; +using XMLSoilLayer2D = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2D; +using XMLSoilLayer2DSurface = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2DSurface; +using XMLSurfaceOuterPoint = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2DSurfaceOuterPoint; +using XMLSurfaceInnerPoint = Deltares.DamEngine.Io.XmlInput.SoilProfile2DLayer2DSurfaceInnerPoint; + +namespace Deltares.Dam.Data.DamEngineIo +{ + /// + /// Converter to create instance of . + /// + public static class XMLSoilProfile2DConverter + { + /// + /// Creates a new instance based on . + /// + /// The to create a + /// for. + /// A . + /// Thrown when + /// is null. + public static XMLSoilProfile2D Convert(SoilProfile2D soilProfile) + { + if (soilProfile == null) + { + throw new ArgumentNullException(nameof(soilProfile)); + } + + var convertedSoilProfile = new XMLSoilProfile2D + { + Name = soilProfile.Name, + PreconsolidationStresses = new SoilProfile2DPreconsolidationStress[0] + }; + + var soilLayers = soilProfile.Surfaces; + int nrOfSoilLayers = soilLayers.Count; + convertedSoilProfile.Layers2D = new XMLSoilLayer2D[nrOfSoilLayers]; + for (int i = 0; i < nrOfSoilLayers; i++) + { + convertedSoilProfile.Layers2D[i] = ConvertSoilLayer(soilLayers[i]); + } + + return convertedSoilProfile; + } + + private static XMLSoilLayer2D ConvertSoilLayer(SoilLayer2D soilLayer) + { + var convertedLayer = new XMLSoilLayer2D + { + SoilName = soilLayer.Name, + IsAquifer = soilLayer.IsAquifer + }; + + GeometrySurface surface = soilLayer.GeometrySurface; + convertedLayer.Surface = ConvertSGeometrySurface(surface); + + return convertedLayer; + } + + private static XMLSoilLayer2DSurface ConvertSGeometrySurface(GeometrySurface surface) + { + var convertedSurface = new XMLSoilLayer2DSurface + { + OuterLoop = ConvertOuterLoop(surface.OuterLoop.Points), + }; + + // TODO: throw exception when there are more inner loops + var innerLoop = surface.InnerLoops; + convertedSurface.Innerloop = !surface.InnerLoops.Any() + ? new XMLSurfaceInnerPoint[0] + : ConvertInnerLoop(innerLoop.Single().Points); + + return convertedSurface; + } + + private static XMLSurfaceOuterPoint[] ConvertOuterLoop(IEnumerable points) + { + return points.Select(point => new XMLSurfaceOuterPoint + { + X = point.X, + Z = point.Z + }).ToArray(); + } + + private static XMLSurfaceInnerPoint[] ConvertInnerLoop(IEnumerable points) + { + return points.Select(point => new XMLSurfaceInnerPoint + { + X = point.X, + Z = point.Z + }).ToArray(); + } + } +} \ No newline at end of file