// 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;
namespace Ringtoets.Piping.Data
{
///
/// Defines extension methods dealing with instances.
///
public static class IPipingCalculationItemExtensions
{
///
/// Recursively enumerates across over the contents of the piping calculation item,
/// yielding the piping calculations found within the calculation item.
///
/// The calculation item to be evaluated.
/// Returns all contained piping calculations as an enumerable result.
public static IEnumerable GetPipingCalculations(this IPipingCalculationItem pipingCalculationItem)
{
var calculation = pipingCalculationItem as PipingCalculationScenario;
if (calculation != null)
{
yield return calculation;
}
var group = pipingCalculationItem as PipingCalculationGroup;
if (group != null)
{
foreach (PipingCalculationScenario calculationInGroup in group.Children.GetPipingCalculations())
{
yield return calculationInGroup;
}
}
}
///
/// Recursively enumerates across over the contents of all the piping calculation
/// items, yielding the piping calculations found within those calculation items.
///
/// The calculation items to be evaluated.
/// Returns all contained piping calculations as an enumerable result.
public static IEnumerable GetPipingCalculations(this IEnumerable pipingCalculationItems)
{
return pipingCalculationItems.SelectMany(GetPipingCalculations);
}
///
/// Determines if the surfaceline of a calculation is instersecting with the section reference line.
///
/// The calculation containing the surface line.
/// The line segments that defines the reference line.
/// true when intersecting. false otherwise.
/// Thrown when contains no elements.
public static bool IsSurfaceLineIntersectionWithReferenceLineInSection(this IPipingCalculationItem pipingCalculationItem, IEnumerable lineSegments)
{
var pipingCalculation = pipingCalculationItem as PipingCalculationScenario;
if (pipingCalculation == null)
{
return false;
}
var surfaceLine = pipingCalculation.InputParameters.SurfaceLine;
if (surfaceLine == null)
{
return false;
}
var minimalDistance = lineSegments.Min(segment => segment.GetEuclideanDistanceToPoint(surfaceLine.ReferenceLineIntersectionWorldPoint));
return minimalDistance < 1.0e-6;
}
}
}