// Copyright (C) Stichting Deltares 2025. 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.
namespace Deltares.DamEngine.Data.Geotechnics;
///
/// Helper class for Soil profile
///
public class SoilProfileHelper
{
///
/// Determines for surface line corrected 1d profile at x.
///
/// The original soil profile1 d.
/// The surface line.
/// The x coordinate.
/// The dike embankment material.
///
public static SoilProfile1D DetermineForSurfaceLineCorrected1DProfileAtX(SoilProfile1D originalSoilProfile1D,
SurfaceLine2 surfaceLine, double xCoordinate, Soil dikeEmbankmentMaterial)
{
var soilProfile = new SoilProfile1D();
soilProfile.Assign(originalSoilProfile1D);
double top = surfaceLine.Geometry.GetZatX(xCoordinate);
if (soilProfile.TopLevel < top)
{
var topLayer = new SoilLayer1D
{
TopLevel = top,
Soil = dikeEmbankmentMaterial,
SoilProfile = soilProfile,
BottomLevel = soilProfile.TopLevel
};
soilProfile.Layers.Insert(0, topLayer);
}
else if (soilProfile.TopLevel > top)
{
int bottom = RemoveLayersAboveTop(originalSoilProfile1D.LayerCount, soilProfile, top);
var topLayer = new SoilLayer1D
{
TopLevel = top,
Soil = originalSoilProfile1D.Layers[bottom].Soil,
SoilProfile = soilProfile,
IsAquifer = originalSoilProfile1D.Layers[bottom].IsAquifer,
BottomLevel = originalSoilProfile1D.Layers[bottom].BottomLevel,
SoilName = originalSoilProfile1D.Layers[bottom].SoilName,
WaterpressureInterpolationModel = originalSoilProfile1D.Layers[bottom].WaterpressureInterpolationModel,
Name = originalSoilProfile1D.Layers[bottom].Name
};
soilProfile.Layers.Insert(0, topLayer);
}
soilProfile.BottomLevel = originalSoilProfile1D.BottomLevel;
return soilProfile;
}
private static int RemoveLayersAboveTop(int layerCount, SoilProfile1D soilProfile, double top)
{
int index = layerCount;
for (int i = layerCount - 1; i >= 0; i--)
{
if (soilProfile.Layers[i].TopLevel > top)
{
soilProfile.Layers.RemoveAt(i);
if (index == layerCount)
{
index = i;
}
}
}
return index; //soilProfile.Layers[0].TopLevel;
}
}