// 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 System;
using System.Linq;
using Deltares.Geometry;
using Deltares.Geotechnics.SurfaceLines;
namespace Deltares.Dam.Data;
///
/// Exception class for DikeCoordinateSystemConverter
///
public class DikeCoordinateSystemConverterException : Exception
{
public DikeCoordinateSystemConverterException(string message)
: base(message) {}
}
///
/// class for converting dike object coordinates from global to local system
///
public class DikeCoordinateSystemConverter
{
public void CreateLocalXZObjects(Dike dike)
{
ThrowIfInvalidDike(dike);
foreach (Location location in dike.Locations.Where(x => null != x.SurfaceLine2))
{
// This can obviously only be done when SurfaceLine is available
// In unit test situations sometimes the surfaceline is not defined
ThrowIfAlreadyConvertedToLocalCoordinates(location.AreLocalXZObjectsCreated, $"Location {location.Name}");
var coordinateSystemConverter = new CoordinateSystemConverter();
coordinateSystemConverter.DefineGlobalXYZBasedOnLine(location.SurfaceLine2.Geometry);
SurfaceLine2 localSurfaceLine = location.SurfaceLine2.FullDeepClone();
coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(localSurfaceLine.Geometry);
if (location.LocalXZSurfaceLine2 != null)
{
location.LocalXZSurfaceLine2.Dispose();
}
location.LocalXZSurfaceLine2 = localSurfaceLine;
location.AreLocalXZObjectsCreated = true;
}
}
///
/// Validate dike and throw exception if not valid
///
///
private static void ThrowIfInvalidDike(Dike dike)
{
try
{
dike.Validate();
}
catch (Exception exception)
{
throw new DikeCoordinateSystemConverterException(exception.Message);
}
}
///
/// Throws exception if surfaceline already has been converted
///
///
private static void ThrowIfAlreadyConvertedToLocalCoordinates(bool isConvertedToLocalCoordinates, string objectDescription)
{
if (isConvertedToLocalCoordinates)
{
throw new DikeCoordinateSystemConverterException($"{objectDescription} has already been converted from global to local coordinates");
}
}
}