// Copyright (C) Stichting Deltares 2025. 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 Deltares.Dam.Data;
using Deltares.Geometry;
using Deltares.Geotechnics.GeotechnicalGeometry;
using Deltares.Geotechnics.SurfaceLines;
using NUnit.Framework;
namespace Deltares.Dam.Tests
{
[TestFixture]
public class CoordinateSystemConverterTest
{
[Test]
public void ThrowsExceptionIfCoordinateSystemConverterNotInitialized()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
Assert.That(() => coordinateSystemConverter.CosAlpha, Throws.InstanceOf());
}
[Test]
public void CanConvertPoint()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlphaBetween0and90Degrees())
using (SurfaceLine2 referenceSurfaceline = CreateGlobalSurfaceLineWithAlphaBetween0and90Degrees())
{
coordinateSystemConverter.DefineGlobalXYZBasedOnLine(surfaceLine.Geometry);
// Convert to global
GeometryPoint firstPoint = surfaceLine.Geometry.Points[0];
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(firstPoint);
Assert.That(firstPoint.LocationEquals(new GeometryPoint(0.0000, 0.0000, 0.0000)), Is.True, "First point not converted correctly to local");
GeometryPoint lastPoint = surfaceLine.Geometry.Points[surfaceLine.Geometry.Count - 1];
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(lastPoint);
Assert.That(lastPoint.LocationEquals(new GeometryPoint(111.8034, 0.0000, 10.0000)), Is.True, "Last point not converted correctly to local");
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(firstPoint);
Assert.That(firstPoint.LocationEquals(referenceSurfaceline.Geometry.Points[0]), Is.True, "First point not converted correctly to global");
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(lastPoint);
Assert.That(lastPoint.LocationEquals(referenceSurfaceline.Geometry.Points[referenceSurfaceline.Geometry.Count - 1]), Is.True, "Last point not converted correctly to global");
}
}
[Test]
public void IsGlobalXYZCorrectlyDefined()
{
const double cTolerance = 0.0000001;
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceline = CreateGlobalSurfaceLineWithAlphaBetween0and90Degrees())
{
coordinateSystemConverter.DefineGlobalXYZBasedOnLine(surfaceline.Geometry);
Assert.That(coordinateSystemConverter.SinAlpha / coordinateSystemConverter.CosAlpha, Is.EqualTo(2.0).Within(cTolerance));
}
}
[Test]
public void ArePropertiesUnchangedWhenSurfaceLineIsConverted()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceline = CreateGlobalSurfaceLineWithAlpha0Degrees())
using (SurfaceLine2 localSurfaceLine = CreateGlobalSurfaceLineWithAlpha0Degrees())
{
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(localSurfaceLine.Geometry);
Assert.That(localSurfaceLine.Name, Is.EqualTo(surfaceline.Name));
}
}
[Test]
public void CanConvertSurfaceLineWithAlpha0Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlpha0Degrees())
using (SurfaceLine2 expectedGlobalSurfaceLine = CreateGlobalSurfaceLineWithAlpha0Degrees())
{
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(surfaceLine.Geometry);
CheckResultingLocalSurfaceLine(surfaceLine);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(surfaceLine.Geometry);
Assert.That(expectedGlobalSurfaceLine.Geometry.Equals(surfaceLine.Geometry), Is.True);
}
}
[Test]
public void CanConvertSurfaceLineWithAlpha90Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlpha90Degrees())
using (SurfaceLine2 expectedGlobalSurfaceLine = CreateGlobalSurfaceLineWithAlpha90Degrees())
{
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(surfaceLine.Geometry);
CheckResultingLocalSurfaceLine(surfaceLine);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(surfaceLine.Geometry);
Assert.That(expectedGlobalSurfaceLine.Geometry.Equals(surfaceLine.Geometry), Is.True);
}
}
[Test]
public void CanConvertSurfaceLineWithAlphaBetween0and90Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlphaBetween0and90Degrees())
using (SurfaceLine2 expectedGlobalSurfaceLine = CreateGlobalSurfaceLineWithAlphaBetween0and90Degrees())
{
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(surfaceLine.Geometry);
CheckResultingLocalSurfaceLine(surfaceLine);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(surfaceLine.Geometry);
Assert.That(expectedGlobalSurfaceLine.Geometry.Equals(surfaceLine.Geometry), Is.True);
}
}
[Test]
public void CanConvertSurfaceLineWithAlphaBetween90and180Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlphaBetween90and180Degrees())
using (SurfaceLine2 expectedGlobalSurfaceLine = CreateGlobalSurfaceLineWithAlphaBetween90and180Degrees())
{
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(surfaceLine.Geometry);
CheckResultingLocalSurfaceLine(surfaceLine);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(surfaceLine.Geometry);
Assert.That(expectedGlobalSurfaceLine.Geometry.Equals(surfaceLine.Geometry), Is.True);
}
}
[Test]
public void CanConvertSurfaceLineWithAlphaBetween180and270Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlphaBetween180and270Degrees())
using (SurfaceLine2 expectedGlobalSurfaceLine = CreateGlobalSurfaceLineWithAlphaBetween180and270Degrees())
{
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(surfaceLine.Geometry);
CheckResultingLocalSurfaceLine(surfaceLine);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(surfaceLine.Geometry);
Assert.That(expectedGlobalSurfaceLine.Geometry.Equals(surfaceLine.Geometry), Is.True);
}
}
[Test]
public void CanConvertSurfaceLineWithAlphaBetween270and360Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
using (SurfaceLine2 surfaceLine = CreateGlobalSurfaceLineWithAlphaBetween270and360Degrees())
using (SurfaceLine2 expectedGlobalSurfaceLine = CreateGlobalSurfaceLineWithAlphaBetween270and360Degrees())
{
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(surfaceLine.Geometry);
CheckResultingLocalSurfaceLine(surfaceLine);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(surfaceLine.Geometry);
Assert.That(expectedGlobalSurfaceLine.Geometry.Equals(surfaceLine.Geometry), Is.True);
}
}
[Test]
public void CanConvertPL1LineWithAlphaBetween0and90Degrees()
{
var coordinateSystemConverter = new CoordinateSystemConverter();
PL1Line pl1Line = CreateGlobalPL1LineWithAlphaBetween0and90Degrees();
PL1Line expectedGlobalPL1Line = CreateGlobalPL1LineWithAlphaBetween0and90Degrees();
coordinateSystemConverter.DefineGlobalXYZBasedOnLine(pl1Line);
// Convert to global
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(pl1Line);
CheckResultingLocalPL1Line(pl1Line);
// Convert to local
coordinateSystemConverter.ConvertLocalXZToGlobalXYZ(pl1Line);
Assert.That(expectedGlobalPL1Line.Equals(pl1Line), Is.True);
}
///
/// Create result Surface line
///
///
private SurfaceLine2 CreateResultingLocalSurfaceLine()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(0.0000, 0.0000, 0.0000));
surfaceline.Geometry.Points.Add(new GeometryPoint(27.9508, 0.0000, 2.50000));
surfaceline.Geometry.Points.Add(new GeometryPoint(55.9017, 0.0000, 5.00000));
surfaceline.Geometry.Points.Add(new GeometryPoint(83.8525, 0.0000, 7.50000));
surfaceline.Geometry.Points.Add(new GeometryPoint(111.8034, 0.0000, 10.0000));
return surfaceline;
}
private SurfaceLine2 DefaultTestSurfaceLine()
{
return new SurfaceLine2
{
Name = "SL1",
Geometry = new LocalizedGeometryPointString()
};
}
///
/// Create Surface line
///
///
private SurfaceLine2 CreateGlobalSurfaceLineWithAlpha0Degrees()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(10000.0000, 2000.0000, 0.0000));
surfaceline.Geometry.Points.Add(new GeometryPoint(10027.9508, 2000.0000, 2.50000));
surfaceline.Geometry.Points.Add(new GeometryPoint(10055.9017, 2000.0000, 5.00000));
surfaceline.Geometry.Points.Add(new GeometryPoint(10083.8525, 2000.0000, 7.50000));
surfaceline.Geometry.Points.Add(new GeometryPoint(10111.8034, 2000.0000, 10.0000));
return surfaceline;
}
///
/// Create Surface line
///
///
private SurfaceLine2 CreateGlobalSurfaceLineWithAlpha90Degrees()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(2000.0000, 10000.0000, 0.0000));
surfaceline.Geometry.Points.Add(new GeometryPoint(2000.0000, 10027.9508, 2.50000));
surfaceline.Geometry.Points.Add(new GeometryPoint(2000.0000, 10055.9017, 5.00000));
surfaceline.Geometry.Points.Add(new GeometryPoint(2000.0000, 10083.8525, 7.50000));
surfaceline.Geometry.Points.Add(new GeometryPoint(2000.0000, 10111.8034, 10.0000));
return surfaceline;
}
///
/// Create Surface line
///
///
private SurfaceLine2 CreateGlobalSurfaceLineWithAlphaBetween0and90Degrees()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(10000.0, 20000.0, 0.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(10012.5, 20025.0, 2.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(10025.0, 20050.0, 5.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(10037.5, 20075.0, 7.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(10050.0, 20100.0, 10.0));
return surfaceline;
}
///
/// Create Surface line
///
///
private SurfaceLine2 CreateGlobalSurfaceLineWithAlphaBetween90and180Degrees()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(-10000.0, 20000.0, 0.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10012.5, 20025.0, 2.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10025.0, 20050.0, 5.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10037.5, 20075.0, 7.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10050.0, 20100.0, 10.0));
return surfaceline;
}
///
/// Create Surface line
///
///
private SurfaceLine2 CreateGlobalSurfaceLineWithAlphaBetween180and270Degrees()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(-10000.0, -20000.0, 0.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10012.5, -20025.0, 2.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10025.0, -20050.0, 5.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10037.5, -20075.0, 7.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(-10050.0, -20100.0, 10.0));
return surfaceline;
}
///
/// Create Surface line
///
///
private SurfaceLine2 CreateGlobalSurfaceLineWithAlphaBetween270and360Degrees()
{
SurfaceLine2 surfaceline = DefaultTestSurfaceLine();
surfaceline.Geometry.Points.Add(new GeometryPoint(10000.0, -20000.0, 0.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(10012.5, -20025.0, 2.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(10025.0, -20050.0, 5.0));
surfaceline.Geometry.Points.Add(new GeometryPoint(10037.5, -20075.0, 7.5));
surfaceline.Geometry.Points.Add(new GeometryPoint(10050.0, -20100.0, 10.0));
return surfaceline;
}
///
/// Create result PL1-line
///
///
private PL1Line CreateResultingLocalPL1Line()
{
var pl1line = new PL1Line();
pl1line.Points.Add(new GeometryPoint(0.0000, 0.0000, -1.0000));
pl1line.Points.Add(new GeometryPoint(27.9508, 0.0000, 1.50000));
pl1line.Points.Add(new GeometryPoint(55.9017, 0.0000, 4.00000));
pl1line.Points.Add(new GeometryPoint(83.8525, 0.0000, 6.50000));
pl1line.Points.Add(new GeometryPoint(111.8034, 0.0000, 9.0000));
return pl1line;
}
///
/// Create test PL1-line
///
/// a Pl1-line with angle in XY plane between 0 and 90 degrees
private PL1Line CreateGlobalPL1LineWithAlphaBetween0and90Degrees()
{
var pl1line = new PL1Line();
pl1line.Points.Add(new GeometryPoint(10000.0, 20000.0, -1.0));
pl1line.Points.Add(new GeometryPoint(10012.5, 20025.0, 1.5));
pl1line.Points.Add(new GeometryPoint(10025.0, 20050.0, 4.0));
pl1line.Points.Add(new GeometryPoint(10037.5, 20075.0, 6.5));
pl1line.Points.Add(new GeometryPoint(10050.0, 20100.0, 9.0));
return pl1line;
}
private void CheckResultingLocalSurfaceLine(SurfaceLine2 localSurfaceLine)
{
using (SurfaceLine2 expectedLocalSurfaceLine = CreateResultingLocalSurfaceLine())
{
Assert.That(expectedLocalSurfaceLine.Geometry.Equals(localSurfaceLine.Geometry), Is.True);
}
}
private void CheckResultingLocalPL1Line(PL1Line localPL1Line)
{
PL1Line expectedLocalPL1Line = CreateResultingLocalPL1Line();
Assert.That(expectedLocalPL1Line.Equals(localPL1Line), Is.True);
}
}
}