// Copyright (C) Stichting Deltares 2017. 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 Lesser 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 Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser 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; namespace Core.Common.Base.Geometry { /// /// Extension methods for collections of . /// public static class Point3DCollectionExtensions { /// /// Projects the points in to localized coordinate (LZ-plane) system. /// Z-values are retained, and the first point is put a L=0. /// /// Points to project. /// Collection of 2D points in the LZ-plane. /// Thrown when /// is null. public static IEnumerable ProjectToLZ(this IEnumerable points) { if (points == null) { throw new ArgumentNullException(nameof(points)); } int count = points.Count(); if (count == 0) { return Enumerable.Empty(); } Point3D first = points.First(); if (count == 1) { return new[] { new Point2D(0.0, first.Z) }; } Point3D last = points.Last(); var firstPoint = new Point2D(first.X, first.Y); var lastPoint = new Point2D(last.X, last.Y); return points.Select(p => p.ProjectIntoLocalCoordinates(firstPoint, lastPoint)).ToArray(); } /// /// Checks whether the collection results in /// a line of zero length. /// /// The points forming a line to check. /// true if the line has a length of zero; false /// otherwise. /// Thrown when /// is null. public static bool IsZeroLength(this IEnumerable points) { if (points == null) { throw new ArgumentNullException(nameof(points)); } Point3D lastPoint = null; foreach (Point3D point in points) { if (lastPoint != null) { if (!Equals(lastPoint, point)) { return false; } } lastPoint = point; } return true; } /// /// Checks whether the locally projected would /// return a reclining geometry. That is, given a point from the geometry /// with an L-coordinate, has a point further in the geometry that has an /// L-coordinate smaller than the L-coordinate of the given point. /// /// The points forming a line to check. /// true if the localized (in 2D) line is reclining; /// false otherwise. /// Thrown when /// is null. public static bool IsReclining(this IEnumerable points) { return points.ProjectToLZ().IsReclining(); } } }