// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets 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.Collections.Generic;
using System.Linq;
using Core.Common.Base.Geometry;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.GrassCoverErosionInwards.Data;
namespace Ringtoets.GrassCoverErosionInwards.Utils
{
///
/// Class holds helper methods to match objects
/// with objects.
///
public static class GrassCoverErosionInwardsHelper
{
///
/// Determine which objects are available for a .
///
/// The objects.
/// The objects.
/// A containing a
/// of objects
/// for each section name which has calculations.
/// When any input parameter is null.
public static Dictionary> CollectCalculationsPerSegment(
IEnumerable sectionResults,
IEnumerable calculations)
{
if (sectionResults == null)
{
throw new ArgumentNullException("sectionResults");
}
if (calculations == null)
{
throw new ArgumentNullException("calculations");
}
SectionSegments[] sectionSegments = MakeSectionSegments(sectionResults);
var calculationsPerSegment = new Dictionary>();
foreach (var calculation in calculations)
{
FailureMechanismSection section = FindSectionForCalculation(sectionSegments, calculation);
if (section == null)
{
continue;
}
UpdateCalculationsOfSegment(calculationsPerSegment, section.Name, calculation);
}
return calculationsPerSegment;
}
///
/// Determine which geometrically contains the .
///
/// The objects
/// whose are considered.
/// The .
/// The containing , or null.
/// When any input parameter is null.
public static FailureMechanismSection FailureMechanismSectionForCalculation(
IEnumerable sectionResults,
GrassCoverErosionInwardsCalculation calculation)
{
if (sectionResults == null)
{
throw new ArgumentNullException("sectionResults");
}
if (calculation == null)
{
throw new ArgumentNullException("calculation");
}
SectionSegments[] sectionSegments = MakeSectionSegments(sectionResults);
return FindSectionForCalculation(sectionSegments, calculation);
}
private static SectionSegments[] MakeSectionSegments(IEnumerable sectionResults)
{
return sectionResults.Select(sr => new SectionSegments(sr.Section)).ToArray();
}
private static FailureMechanismSection FindSectionForCalculation(SectionSegments[] sectionSegmentsCollection, GrassCoverErosionInwardsCalculation calculation)
{
var dikeProfile = calculation.InputParameters.DikeProfile;
if (dikeProfile == null)
{
return null;
}
var minimumDistance = double.PositiveInfinity;
FailureMechanismSection section = null;
foreach (var sectionSegments in sectionSegmentsCollection)
{
var distance = sectionSegments.Distance(dikeProfile.WorldReferencePoint);
if (distance < minimumDistance)
{
minimumDistance = distance;
section = sectionSegments.Section;
}
}
return section;
}
private static void UpdateCalculationsOfSegment(Dictionary> calculationsPerSegment,
string sectionName, GrassCoverErosionInwardsCalculation calculation)
{
if (!calculationsPerSegment.ContainsKey(sectionName))
{
calculationsPerSegment.Add(sectionName, new List());
}
calculationsPerSegment[sectionName].Add(calculation);
}
///
/// This class represents the geometry of a as a collection of objects.
///
private class SectionSegments
{
private readonly IEnumerable segments;
///
/// Creates a new instance of .
///
/// The whose
/// this class represents as a collection of objects.
public SectionSegments(FailureMechanismSection section)
{
Section = section;
segments = Math2D.ConvertLinePointsToLineSegments(section.Points);
}
///
/// Gets the .
///
public FailureMechanismSection Section { get; private set; }
///
/// Calculate the Euclidean distance between the and a .
///
/// The .
/// The Euclidean distance as a .
public double Distance(Point2D point)
{
return segments.Min(segment => segment.GetEuclideanDistanceToPoint(point));
}
}
}
}