//-----------------------------------------------------------------------
//
// Copyright (c) 2009 Deltares. All rights reserved.
//
// B.S.T.I.M. The
// tom.the@deltares.nl
// 03-12-2009
// Constructs a new surface line with an adjusted shoulder
//-----------------------------------------------------------------------
using System.Linq;
using Deltares.Geometry;
using Deltares.Geotechnics;
using Deltares.Geotechnics.SurfaceLines;
using Deltares.Standard.Language;
namespace Deltares.Dam.Data
{
using System;
public class SurfaceLineShoulderAdapter : SurfaceLineAdapter
{
public SurfaceLineShoulderAdapter(SurfaceLine2 surfaceLine, Location location) : base(surfaceLine, location)
{
if (!surfaceLine.HasAnnotation(CharacteristicPointType.SurfaceLevelInside))
throw new SurfaceLineAdapterException();
SlopeOfNewShoulder = 3; // CoTangent i.e. width (dx) : height (dz) = 3 : 1 (= tangent: 1/3)
// It must be possible to define SlopeOfNewShoulder hard in code for certain options (such as Rijnland option) which should not be overriden
// by this use option. So the use option has to be implemented here in the constructor.
if (Location.UseNewShoulderBaseSlope)
{
SlopeOfNewShoulder = 1 / Location.NewShoulderBaseSlope;
}
MaxShoulderLevel = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeTopAtPolder).Z;
}
public double MaxShoulderLevel { get; set; }
public double SlopeOfNewShoulder { get; set; }
///
/// Raise exception if new shoulder is smaller than existing shoulder
///
///
///
///
private void ValidateNewShoulderSize(double shoulderWidth, double shoulderHeight)
{
if (surfaceLine.HasShoulderInside())
{
double currentShoulderWidth = surfaceLine.DetermineShoulderLengthForGivenShoulderTopInside(surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.ShoulderTopInside));
if (currentShoulderWidth > shoulderWidth)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderWidthError"));
}
double currentShoulderHeight = surfaceLine.DetermineShoulderHeight();
if (currentShoulderHeight > shoulderHeight)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderHeightError"));
}
if (shoulderWidth < 0)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderWidthError"));
}
if (shoulderHeight < 0)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderHeightZeroError"));
}
}
}
///
/// Remove all non characteristic points between DikeTopAtPolder and DikeToeAtPolder
///
///
private void RemovePointsBetweenCharacteristicPointsDikeTopAtPolderDikeToeAtPolder(SurfaceLine2 surfaceLine)
{
bool hasShoulderInside = surfaceLine.HasShoulderInside();
GeometryPoint dikeTopAtPolder = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeTopAtPolder);
GeometryPoint dikeToeAtPolder = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeToeAtPolder);
if (hasShoulderInside)
{
GeometryPoint shoulderBaseInside = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.ShoulderBaseInside);
GeometryPoint shoulderTopInside = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.ShoulderTopInside);
surfaceLine.RemoveSegmentBetween(dikeTopAtPolder.X, shoulderBaseInside.X);
surfaceLine.RemoveSegmentBetween(shoulderBaseInside.X, shoulderTopInside.X);
surfaceLine.RemoveSegmentBetween(shoulderTopInside.X, dikeToeAtPolder.X);
}
else
{
surfaceLine.RemoveSegmentBetween(dikeTopAtPolder.X, dikeToeAtPolder.X);
}
}
///
/// ShoulderWidth = horizontal distance between ShoulderTopInside and ShoulderBaseInside
/// ShoulderHeight = vertical distance between ShoulderTopInside and DikeToeAtPolder
/// The default slope of the shoulder will be 1 : 3
/// The height has a maximum of MaxFractionOfDikeHeightForShoulderHeight (default 2/3) * dike height
///
///
///
/// Ensure that shoulder complies with piping design Demands (shoulder from toe instead of intersection old dike, height gives error
///
public SurfaceLine2 ConstructNewSurfaceLine(double shoulderWidth, double shoulderHeight, bool designFromDikeToe)
{
var orgMaxX = surfaceLine.Geometry.GetMaxX();
if (double.IsNaN(orgMaxX))
{
orgMaxX = double.MaxValue;
}
// Make dike and shoulder straight
RemovePointsBetweenCharacteristicPointsDikeTopAtPolderDikeToeAtPolder(surfaceLine);
// See it there is a ditch, if so store its characteristics.
var ditchDefinition = GetDitchDefinition();
// Then delete it from the surfaceline
RemoveExistingDitch(ditchDefinition);
ValidateNewShoulderSize(shoulderWidth, shoulderHeight);
GeometryPoint dikeTopAtPolder = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeTopAtPolder);
GeometryPoint dikeToeAtPolder = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeToeAtPolder);
GeometryPoint surfaceLevelInside = surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside);
if (designFromDikeToe)
{
// for piping design, when the required new height is larger than allowed, it is an error!
if ((MaxShoulderLevel - dikeToeAtPolder.Z) < shoulderHeight)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderHeightTooLargeError"));
}
}
else
{
// Assure new height is less then or equal to the maximum height
shoulderHeight = Math.Min(shoulderHeight, (MaxShoulderLevel - dikeToeAtPolder.Z));
}
// Find the intersection point at new shoulder height with the dike, this is where the new shoulder starts
bool hasShoulderInside = surfaceLine.HasShoulderInside();
double dikeToeZ = dikeToeAtPolder.Z;
GeometryPoint dikeBaseInside = hasShoulderInside ? surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.ShoulderBaseInside) : dikeToeAtPolder;
// Determine intersectionpoint with slope for a horizontal shoulder
var intersectionPointAtDike = LineHelper.GetIntersectionPointWithExtrapolation(dikeTopAtPolder, dikeBaseInside,
GeometryPoint.CreateNewXZPoint(dikeToeAtPolder.X, shoulderHeight + dikeToeZ),
GeometryPoint.CreateNewXZPoint(dikeToeAtPolder.X + 1, shoulderHeight + dikeToeZ));
var newTopShoulder = GeometryPoint.CreateNewXZPoint(intersectionPointAtDike.X + shoulderWidth, shoulderHeight + dikeToeZ);
if (Location.UseNewShoulderTopSlope)
{
// if top slope is not to be horizontal then recalculate the intersection from a point at required slope width
// from the horizontal intersection point. This will result in the actual width of the shoulder being a bit
// larger than the requested size but that can not be helped (classic chicken-egg problem)
var pb = GeometryPoint.CreateNewXZPoint(newTopShoulder.X - 100, newTopShoulder.Z + (100*Location.NewShoulderTopSlope));
intersectionPointAtDike = LineHelper.GetIntersectionPointWithExtrapolation(dikeTopAtPolder, dikeBaseInside, pb, newTopShoulder);
if (intersectionPointAtDike.Z > MaxShoulderLevel)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderHeightTooLargeTopSlopeError"));
}
}
// Find the intersection at the surface line from a line starting at the end of the new shoulder sloping down at 1 : SlopeOfNewShoulder
// to get the new point of the toe. Note that for stability the shoulderwidth is applied directly to the intersection point as for piping
// the shoulderwidth is applied from the original diketoe (so for piping the complete shoulder is longer).
var xStart = intersectionPointAtDike.X;
const double offset = 100.0;
if (designFromDikeToe)
{
xStart = dikeToeAtPolder.X;
}
var line = new Deltares.Geometry.Line
{
BeginPoint = newTopShoulder,
EndPoint =
new GeometryPoint(newTopShoulder.X + offset, 0,
newTopShoulder.Z - offset / SlopeOfNewShoulder)
};
var intersectionpoints = surfaceLine.Geometry.IntersectionPointsXzWithLineXz(line);
GeometryPoint newToeAtPolder = null;
if (intersectionpoints.Count > 0)
{
newToeAtPolder = intersectionpoints.First();
}
if (newToeAtPolder == null)
{
// If the intersection was not found, this could mean that A) the new shoulder intersects the surfaceline at the
// shoulderheight itself or B) that the original surfaceline is not long enough to intersect the new shoulder
// (this happens when the end of the new shoulder falls pratically at the end of the surfaceline in which case
// there is no room to fit the entire shoulder in the original surfaceline). For A) try to find that intersection
// point and use it as new toe. If not, something is very wrong (probably B) and an exception must be raised.
var line1 = new Deltares.Geometry.Line
{
BeginPoint =
new GeometryPoint(xStart + 0.001, 0, intersectionPointAtDike.Z),
EndPoint =
new GeometryPoint(xStart + shoulderWidth, 0, intersectionPointAtDike.Z)
};
var intersectionpoints2 = surfaceLine.Geometry.IntersectionPointsXzWithLineXz(line1);
if (intersectionpoints2.Count > 0)
{
newToeAtPolder = intersectionpoints2.First();
}
if (newToeAtPolder == null)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this,
"SurfaceLineShoulderAdapterNewShoulderNoIntersectionError"));
}
}
if (designFromDikeToe)
{
// for piping design, the new toe must be placed at shoulderWidth from the original toe, not from the new intersection point.
newToeAtPolder.X = dikeToeAtPolder.X + shoulderWidth + shoulderHeight * SlopeOfNewShoulder;
}
// remove old line (point) segment starting from intersection dike inside to new intersection ground
surfaceLine.RemoveSegmentBetween(intersectionPointAtDike.X, newToeAtPolder.X);
// insert the new shoulder points
surfaceLine.EnsurePointOfType(intersectionPointAtDike.X, intersectionPointAtDike.Z, CharacteristicPointType.ShoulderBaseInside);
surfaceLine.EnsurePointOfType(newTopShoulder.X, newTopShoulder.Z, CharacteristicPointType.ShoulderTopInside);
// Place new dike toe
surfaceLine.EnsurePointOfType(newToeAtPolder.X, newToeAtPolder.Z, CharacteristicPointType.DikeToeAtPolder);
// Check whether the surface line is extended. This is not allowed!
if (surfaceLine.Geometry.GetMaxX() > orgMaxX)
{
throw new SurfaceLineAdapterException(LocalizationManager.GetTranslatedText(this, "SurfaceLineShoulderAdapterNewShoulderWidthTooLargeError"));
}
// Restore Ditch (if any)
RestoreDitch(ditchDefinition);
// Restore traffic load
RestoreTrafficLoad();
surfaceLine.SortPoints();
return surfaceLine;
}
}
}