// 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 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.MacroStabilityInwards.Primitives { /// /// A collection of points which together form a closed line. /// public class Ring { /// /// Creates a new instance of . /// /// The points that form the ring. /// Thrown when is null. /// Thrown when contains less than 2 unique points. /// While a ring is defined to be closed line, it's not required /// that the given ' first point and last point /// are equal. public Ring(IEnumerable points) { ValidateAndTrimPoints(points); Points = points.ToArray(); } public IEnumerable Points { get; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } if (obj.GetType() != GetType()) { return false; } return Equals((Ring) obj); } public override int GetHashCode() { unchecked { var hashCode = 397; foreach (Point2D p in Points) { hashCode = (hashCode * 397) ^ p.GetHashCode(); } return hashCode; } } private bool Equals(Ring other) { return Points.SequenceEqual(other.Points); } /// /// Validates the points collection. /// /// The points to validate. /// Thrown when is null. /// Thrown when contains less than 2 unique points. private void ValidateAndTrimPoints(IEnumerable points) { if (points == null) { throw new ArgumentNullException(nameof(points)); } if (points.Distinct().Count() < 2) { throw new ArgumentException($@"Need at least two distinct points to define a {typeof(Ring).Name}.", nameof(points)); } } } }