// 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.
using System;
using Deltares.DamEngine.Data.Geometry;
using Deltares.DamEngine.Data.Geotechnics;
using Deltares.DamEngine.TestHelpers.Factories;
using NUnit.Framework;
namespace Deltares.DamEngine.Data.Tests.Geotechnics;
[TestFixture]
public class GeometryHelperTests
{
const double cTolerance = 1e-6;
[Test]
public void GivenTwoLayerGeometryWhenExtendingLeftThenLeftBoundaryIsChanged()
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayers();
// When
GeometryHelper.ExtendGeometryLeft(soilProfile2D.Geometry, -2);
// Then
GeometryBounds geometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(geometryBounds.Left, Is.EqualTo(-2).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Left, Is.EqualTo(-2).Within(cTolerance));
// At first there are 3 surfaces, but after extending the right boundary, there are 6 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(6));
// the number of points should now be 12 + 4 = 16
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(16));
// the number of curves should now be 14 + 7 = 21
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(21));
});
}
[Test]
public void GivenTwoLayerGeometryWhenExtendingLeftToTheRightThenExceptionIsThrown()
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayers();
// When & Then
Assert.Throws(() => { GeometryHelper.ExtendGeometryLeft(soilProfile2D.Geometry, 2); });
}
[Test]
public void GivenTwoLayerGeometryWhenExtendingRightThenRightBoundaryIsChanged()
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayers();
// When
GeometryHelper.ExtendGeometryRight(soilProfile2D.Geometry, 12);
// Then
Assert.Multiple(() =>
{
Assert.That(soilProfile2D.Geometry.Right, Is.EqualTo(12).Within(cTolerance));
// At first there are 3 surfaces, but after extending the right boundary, there are 6 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(6));
// the number of points should now be 12 + 4 = 16
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(16));
// the number of curves should now be 14 + 7 = 21
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(21));
});
}
[Test]
public void GivenTwoLayerGeometryWhenExtendingRightToTheLeftThenExceptionIsThrown()
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayers();
// When & Then
Assert.Throws(() => { GeometryHelper.ExtendGeometryRight(soilProfile2D.Geometry, 8); });
}
[Test]
public void GivenTwoLayerGeometryWhenCuttingLeftThenLeftBoundaryIsChanged()
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayers();
// When
GeometryHelper.CutGeometryLeft(soilProfile2D.Geometry, 2);
// Then
GeometryBounds geometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(geometryBounds.Left, Is.EqualTo(2).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Left, Is.EqualTo(2).Within(cTolerance));
// At first there are 3 surfaces, after cutting the boundary, there still must be 3 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(3));
// At first there are 12 points, after cutting the boundary, there still must be 12 points
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(12));
// At first there are 14 curves, after cutting the boundary, there still must be 14 curves
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(14));
});
}
[Test]
public void GivenTwoLayerGeometryWhenCuttingRightThenRightBoundaryIsChanged()
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayers();
// When
GeometryHelper.CutGeometryRight(soilProfile2D.Geometry, 8);
// Then
GeometryBounds geometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(geometryBounds.Right, Is.EqualTo(8).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Right, Is.EqualTo(8).Within(cTolerance));
// At first there are 3 surfaces, after cutting the boundary, there still must be 3 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(3));
// At first there are 12 points, after cutting the boundary, there still must be 12 points
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(12));
// At first there are 14 curves, after cutting the boundary, there still must be 14 curves
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(14));
});
}
[Test]
[TestCase(-40, 3, 12, 12, 2)]
[TestCase(-30, 3, 12, 13, 1)]
[TestCase(-20, 3, 12, 13, 1)]
[TestCase(-10, 2, 10, 10, 1)]
[TestCase(0, 2, 8, 8, 1)]
[TestCase(10, 2, 8, 9, 0)]
[TestCase(20, 2, 8, 9, 0)]
[TestCase(45, 1, 4, 4, 0)]
public void GivenInnerLoopLayerGeometryWhenCuttingLeftThenLeftBoundaryIsChanged(double atX, int surfacesCount, int pointsCount, int curvesCount, int innerLoopsCount)
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayersOfWhichTwoAreInnerLoops();
GeometryBounds originalGeometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(originalGeometryBounds.Left, Is.EqualTo(-50).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Left, Is.EqualTo(-50).Within(cTolerance));
// At first there are 3 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(3));
// At first there are 12 points
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(12));
// At first there are 12 curves
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(12));
// At first there are 2 inner loops
Assert.That(soilProfile2D.Geometry.InnerLoopsCount, Is.EqualTo(2));
});
// When
GeometryHelper.CutGeometryLeft(soilProfile2D.Geometry, atX);
// Then
GeometryBounds geometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(geometryBounds.Left, Is.EqualTo(atX).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Left, Is.EqualTo(atX).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(surfacesCount));
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(pointsCount));
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(curvesCount));
Assert.That(soilProfile2D.Geometry.InnerLoopsCount, Is.EqualTo(innerLoopsCount));
});
}
[Test]
[TestCase(-45, 3, 10, 11, 2)]
[TestCase(-40, 3, 10, 12, 0)]
[TestCase(-30, 3, 10, 12, 0)]
[TestCase(-20, 3, 10, 12, 0)]
[TestCase(-10, 3, 10, 12, 0)]
[TestCase(0, 2, 8, 9, 0)]
[TestCase(10, 2, 8, 9, 0)]
[TestCase(20, 2, 8, 9, 0)]
[TestCase(40, 1, 5, 5, 0)]
[TestCase(45, 1, 4, 4, 0)]
public void GivenInnerLoopLayerGeometryWithConnectedLoopsWhenCuttingLeftThenLeftBoundaryIsChanged(double atX, int surfacesCount, int pointsCount, int curvesCount, int innerLoopsCount)
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayersOfWhichTwoAreConnectedInnerLoops();
GeometryBounds originalGeometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(originalGeometryBounds.Left, Is.EqualTo(-50).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Left, Is.EqualTo(-50).Within(cTolerance));
// At first there are 3 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(3));
// At first there are 12 points
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(10));
// At first there are 12 curves
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(11));
// At first there are 2 inner loops
Assert.That(soilProfile2D.Geometry.InnerLoopsCount, Is.EqualTo(2));
});
// When
GeometryHelper.CutGeometryLeft(soilProfile2D.Geometry, atX);
// Then
GeometryBounds geometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(geometryBounds.Left, Is.EqualTo(atX).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Left, Is.EqualTo(atX).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(surfacesCount));
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(pointsCount));
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(curvesCount));
Assert.That(soilProfile2D.Geometry.InnerLoopsCount, Is.EqualTo(innerLoopsCount));
});
}
[Test]
[TestCase(-45, 1, 4, 4, 0)]
[TestCase(-40, 1, 5, 5, 0)]
[TestCase(-30, 2, 7, 8, 0)]
[TestCase(-20, 2, 8, 9, 0)]
[TestCase(-10, 2, 8, 9, 0)]
[TestCase(0, 2, 8, 9, 0)]
[TestCase(10, 3, 10, 12, 0)]
[TestCase(20, 3, 10, 12, 0)]
[TestCase(40, 3, 10, 12, 0)]
[TestCase(45, 3, 10, 11, 2)]
public void GivenInnerLoopLayerGeometryWithConnectedLoopsWhenCuttingRightThenRightBoundaryIsChanged(double atX, int surfacesCount, int pointsCount, int curvesCount, int innerLoopsCount)
{
// Given
SoilProfile2D soilProfile2D = FactoryForSoilProfiles.CreateSoilProfile2DWithThreeLayersOfWhichTwoAreConnectedInnerLoops();
GeometryBounds originalGeometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(originalGeometryBounds.Right, Is.EqualTo(50).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Right, Is.EqualTo(50).Within(cTolerance));
// At first there are 3 surfaces
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(3));
// At first there are 12 points
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(10));
// At first there are 12 curves
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(11));
// At first there are 2 inner loops
Assert.That(soilProfile2D.Geometry.InnerLoopsCount, Is.EqualTo(2));
});
// When
GeometryHelper.CutGeometryRight(soilProfile2D.Geometry, atX);
// Then
GeometryBounds geometryBounds = soilProfile2D.Geometry.GetGeometryBounds();
Assert.Multiple(() =>
{
Assert.That(geometryBounds.Right, Is.EqualTo(atX).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Right, Is.EqualTo(atX).Within(cTolerance));
Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(surfacesCount));
Assert.That(soilProfile2D.Geometry.Points, Has.Count.EqualTo(pointsCount));
Assert.That(soilProfile2D.Geometry.Curves, Has.Count.EqualTo(curvesCount));
Assert.That(soilProfile2D.Geometry.InnerLoopsCount, Is.EqualTo(innerLoopsCount));
});
}
}