// Copyright (C) Stichting Deltares 2023. 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.DamEngine.Io.XmlInput;
using Deltares.Geometry;
using Deltares.Geotechnics.Soils;
using Deltares.Standard.Extensions;
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
{
///
/// Converts the to a new instance of .
///
/// The to convert.
/// A .
/// Thrown when
/// is null.
/// Thrown when
/// could not be successfully converted.
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]
};
IList soilLayers = soilProfile.Surfaces;
int nrOfSoilLayers = soilLayers.Count;
convertedSoilProfile.Layers2D = new XMLSoilLayer2D[nrOfSoilLayers];
for (var i = 0; i < nrOfSoilLayers; i++)
{
convertedSoilProfile.Layers2D[i] = ConvertSoilLayer(soilLayers[i]);
}
return convertedSoilProfile;
}
///
/// Converts the input arguments to a new instance of .
///
/// The soil layer to convert.
/// A .
/// Thrown when
/// could not be successfully created from .
private static XMLSoilLayer2D ConvertSoilLayer(SoilLayer2D soilLayer)
{
string soilLayerName = soilLayer.Name;
var convertedLayer = new XMLSoilLayer2D
{
SoilName = soilLayerName,
IsAquifer = soilLayer.IsAquifer,
WaterpressureInterpolationModel = ConversionHelper.ConvertToInputWaterpressureInterpolationModel(soilLayer.WaterpressureInterpolationModel)
};
GeometrySurface surface = soilLayer.GeometrySurface;
convertedLayer.Surface = ConvertGeometrySurface(soilLayerName, surface);
return convertedLayer;
}
///
/// Converts a to a .
///
/// The name of the layer to create a xml soil layer surface for.
/// The surface to create a xml soil layer surface for.
/// A .
/// Thrown when
/// could not be successfully created from .
private static XMLSoilLayer2DSurface ConvertGeometrySurface(string layerName, GeometrySurface surface)
{
var convertedSurface = new XMLSoilLayer2DSurface
{
OuterLoop = ConvertOuterLoop(surface.OuterLoop.Points)
};
List innerLoop = surface.InnerLoops;
int nrOfInnerLoops = innerLoop.Count;
if (nrOfInnerLoops > 1)
{
// Note that the exception message does not match of what is being asserted.
throw new ConversionException($"{layerName} contains more than 1 inner loop.", null);
}
convertedSurface.Innerloop = nrOfInnerLoops == 0
? 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();
}
}