// 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; using Core.Common.Base.Data; using Core.Common.Base.Geometry; using Ringtoets.Common.Data.Properties; namespace Ringtoets.Common.Data.DikeProfiles { /// /// Definition for a foreshore profile for a failure mechanism. /// public class ForeshoreProfile : Observable { /// /// Creates a new instance of the class. /// /// The value for . /// The geometry of the foreshore. /// The break water definition (can be null). /// The property values required to create an instance of . /// Thrown when either , /// or is null. /// Thrown when: /// /// any element of is null /// is null, empty or whitespaces /// /// public ForeshoreProfile(Point2D worldCoordinate, IEnumerable geometry, BreakWater breakWater, ConstructionProperties properties) { if (worldCoordinate == null) { throw new ArgumentNullException(nameof(worldCoordinate)); } if (geometry == null) { throw new ArgumentNullException(nameof(geometry)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } if (string.IsNullOrWhiteSpace(properties.Id)) { throw new ArgumentException(@"Id is null, empty or consists of whitespace.", nameof(properties)); } SetGeometry(geometry); Orientation = new RoundedDouble(2, properties.Orientation); BreakWater = breakWater; Id = properties.Id; Name = string.IsNullOrWhiteSpace(properties.Name) ? properties.Id : properties.Name; WorldReferencePoint = worldCoordinate; X0 = properties.X0; } /// /// Gets the ID of the foreshore profile. /// public string Id { get; private set; } /// /// Gets the name of the foreshore profile. /// public string Name { get; private set; } /// /// Gets the reference point in world coordinates corresponding to the local coordinate . /// public Point2D WorldReferencePoint { get; private set; } /// /// Gets the local x-coordinate corresponding to the world reference point . /// public double X0 { get; private set; } /// /// Gets the orientation of the foreshore profile geometry with respect to North /// in degrees. A positive value equals a clockwise rotation. /// public RoundedDouble Orientation { get; private set; } /// /// Gets a value indicating if there is a break water object available. /// public bool HasBreakWater { get { return BreakWater != null; } } /// /// Gets the break water object of the foreshore profile, if any. /// public BreakWater BreakWater { get; private set; } /// /// Gets the geometry of the foreshore profile. /// public RoundedPoint2DCollection Geometry { get; private set; } /// /// Copies all the properties of /// to the current instance. /// /// The foreshore profile to copy the /// properties from. /// Thrown when /// is null. public void CopyProperties(ForeshoreProfile fromForeshoreProfile) { if (fromForeshoreProfile == null) { throw new ArgumentNullException(nameof(fromForeshoreProfile)); } Id = fromForeshoreProfile.Id; Name = fromForeshoreProfile.Name; X0 = fromForeshoreProfile.X0; Orientation = fromForeshoreProfile.Orientation; BreakWater = fromForeshoreProfile.BreakWater; WorldReferencePoint = fromForeshoreProfile.WorldReferencePoint; SetGeometry(fromForeshoreProfile.Geometry); } public override string ToString() { return Name; } 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((ForeshoreProfile) obj); } public override int GetHashCode() { unchecked { int hashCode = Id.GetHashCode(); hashCode = (hashCode * 397) ^ (Name?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ WorldReferencePoint.GetHashCode(); hashCode = (hashCode * 397) ^ X0.GetHashCode(); hashCode = (hashCode * 397) ^ Orientation.GetHashCode(); hashCode = (hashCode * 397) ^ (BreakWater?.GetHashCode() ?? 0); foreach (Point2D point in Geometry) { hashCode = (hashCode * 397) ^ point.GetHashCode(); } return hashCode; } } private bool Equals(ForeshoreProfile other) { return Id.Equals(other.Id) && string.Equals(Name, other.Name) && WorldReferencePoint.Equals(other.WorldReferencePoint) && X0.Equals(other.X0) && Orientation.Equals(other.Orientation) && Equals(BreakWater, other.BreakWater) && EqualGeometry(other.Geometry.ToArray()); } /// /// Sets the geometry of the foreshore profile. /// /// The points corresponding to the geometry of /// the foreshore profile. /// Thrown when an element in /// is null. private void SetGeometry(IEnumerable points) { Point2D[] foreshorePoints = points.ToArray(); if (foreshorePoints.Any(p => p == null)) { throw new ArgumentException(Resources.ForeshoreProfile_SetGeometry_A_point_in_the_collection_is_null); } Geometry = new RoundedPoint2DCollection(2, foreshorePoints); } private bool EqualGeometry(Point2D[] otherGeometry) { Point2D[] pointsArray = Geometry.ToArray(); int nrOfPoints = pointsArray.Length; if (otherGeometry.Length != nrOfPoints) { return false; } for (var i = 0; i < nrOfPoints; i++) { if (!pointsArray[i].Equals(otherGeometry[i])) { return false; } } return true; } /// /// Class holding the various construction parameters for . /// public class ConstructionProperties { /// /// Gets or sets the value for . /// public string Id { internal get; set; } /// /// Gets or sets the value for . /// public string Name { internal get; set; } /// /// Gets or sets the value for . /// public double X0 { internal get; set; } /// /// Gets or sets the value for . /// /// will be rounded to the /// of . public double Orientation { internal get; set; } } } }