Index: DamEngine/trunk/src/Deltares.DamEngine.Data/General/PolyLine.cs =================================================================== diff -u -r4000 -r4052 --- DamEngine/trunk/src/Deltares.DamEngine.Data/General/PolyLine.cs (.../PolyLine.cs) (revision 4000) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/General/PolyLine.cs (.../PolyLine.cs) (revision 4052) @@ -25,273 +25,272 @@ using System.Text; using Deltares.DamEngine.Data.Geometry; -namespace Deltares.DamEngine.Data.General +namespace Deltares.DamEngine.Data.General; + +public class PolyLine where T : GeometryPoint, new() { - public class PolyLine where T : GeometryPoint, new() + protected IList points; + + public PolyLine() { - protected IList points; + points = new List(); + } - public PolyLine() + public virtual int Id { get; private set; } + + public virtual string Name { get; set; } + + public virtual IList Points + { + get { - points = new List(); + return points; } + private set + { + points = value; + } + } - public virtual int Id { get; private set; } + /// + /// Check if lines are equal + /// + /// + /// + public virtual bool Equals(PolyLine other) + { + bool isEqual = (other.Points.Count == Points.Count); + if (isEqual) + { + for (var pointIndex = 0; pointIndex < Points.Count; pointIndex++) + { + isEqual = isEqual && (Points[pointIndex].LocationEquals(other.Points[pointIndex])); + } + } - public virtual string Name { get; set; } + return isEqual; + } - public virtual IList Points + /// + /// Check if line has a minimum of two points + /// + /// + public bool Exists() + { + return (Points.Count > 1); + } + + /// + /// Check if all the points are in strict ascending X order + /// + /// + public bool IsXStrictAscending() + { + var isStrictAscending = true; + for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) { - get + if (Points[pointIndex + 1].X <= Points[pointIndex].X) { - return points; + isStrictAscending = false; + break; } - private set - { - points = value; - } } - /// - /// Check if lines are equal - /// - /// - /// - public virtual bool Equals(PolyLine other) + return isStrictAscending; + } + + /// + /// Check if all the points are in ascending X order + /// + /// + public bool IsXAscending() + { + var isAscending = true; + for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) { - bool isEqual = (other.Points.Count == Points.Count); - if (isEqual) + if (Points[pointIndex + 1].X < Points[pointIndex].X) { - for (var pointIndex = 0; pointIndex < Points.Count; pointIndex++) - { - isEqual = isEqual && (Points[pointIndex].LocationEquals(other.Points[pointIndex])); - } + isAscending = false; + break; } - - return isEqual; } - /// - /// Check if line has a minimum of two points - /// - /// - public bool Exists() + return isAscending; + } + + /// + /// Gets point at X if present within tolerance or null if not present. + /// + /// + public T GetPointAtX(double X, double tolerance) + { + return (from T point in points + where Math.Abs(point.X - X) < tolerance + select point).FirstOrDefault(); + } + + /// + /// Gets point at X if present within tolerance; creates one there if not present. + /// + /// + public T EnsurePointAtX(double X, double tolerance) + { + T point = GetPointAtX(X, tolerance); + if (point == null) { - return (Points.Count > 1); + point = InsertPointAtX(X); } - /// - /// Check if all the points are in strict ascending X order - /// - /// - public bool IsXStrictAscending() - { - var isStrictAscending = true; - for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) - { - if (Points[pointIndex + 1].X <= Points[pointIndex].X) - { - isStrictAscending = false; - break; - } - } + return point; + } - return isStrictAscending; + public double ZFromX(double X) + { + if (!IsXAscending()) + { + throw new PolyLineException("Interpolation only possible with ascending polyline"); } - /// - /// Check if all the points are in ascending X order - /// - /// - public bool IsXAscending() + var valueZ = 0.0; + + // Less then first X + if (X <= Points[0].X) { - var isAscending = true; + valueZ = Points[0].Z; + } + // More then last X + else if (X >= Points[Points.Count - 1].X) + { + valueZ = Points[Points.Count - 1].Z; + } + else + { + // X is inside boundaries for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) { - if (Points[pointIndex + 1].X < Points[pointIndex].X) + if ((X > Points[pointIndex].X) && (X <= Points[pointIndex + 1].X)) { - isAscending = false; + // interpolate in this section + double fractionX = (X - Points[pointIndex].X) / (Points[pointIndex + 1].X - Points[pointIndex].X); + valueZ = Points[pointIndex].Z + fractionX * (Points[pointIndex + 1].Z - Points[pointIndex].Z); break; } } - - return isAscending; } - /// - /// Gets point at X if present within tolerance or null if not present. - /// - /// - public T GetPointAtX(double X, double tolerance) - { - return (from T point in points - where Math.Abs(point.X - X) < tolerance - select point).FirstOrDefault(); - } + return valueZ; + } - /// - /// Gets point at X if present within tolerance; creates one there if not present. - /// - /// - public T EnsurePointAtX(double X, double tolerance) + public virtual void CopyPoints(PolyLine polyLine) + { + foreach (T point in polyLine.Points) { - T point = GetPointAtX(X, tolerance); - if (point == null) - { - point = InsertPointAtX(X); - } - - return point; + points.Add(point); } + } - public double ZFromX(double X) + /// + /// Deletes the coinsiding points. + /// + /// The tolerance. + public virtual void DeleteCoinsidingPoints(double tolerance) + { + // First build a list of indices of points that have to be removed (from end of list to start) + var indicesToDelete = new List(); + for (int pointIndex = Points.Count - 1; pointIndex > 0; pointIndex--) { - if (!IsXAscending()) + for (int i = pointIndex - 1; i >= 0; i--) { - throw new PolyLineException("Interpolation only possible with ascending polyline"); - } - - var valueZ = 0.0; - - // Less then first X - if (X <= Points[0].X) - { - valueZ = Points[0].Z; - } - // More then last X - else if (X >= Points[Points.Count - 1].X) - { - valueZ = Points[Points.Count - 1].Z; - } - else - { - // X is inside boundaries - for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) + if (Points[pointIndex].LocationEquals(Points[i], tolerance)) { - if ((X > Points[pointIndex].X) && (X <= Points[pointIndex + 1].X)) - { - // interpolate in this section - double fractionX = (X - Points[pointIndex].X) / (Points[pointIndex + 1].X - Points[pointIndex].X); - valueZ = Points[pointIndex].Z + fractionX * (Points[pointIndex + 1].Z - Points[pointIndex].Z); - break; - } + indicesToDelete.Add(i); } } - - return valueZ; } - public virtual void CopyPoints(PolyLine polyLine) + // Remove duplicate points beginning from the end + for (var index = 0; index < indicesToDelete.Count; index++) { - foreach (T point in polyLine.Points) - { - points.Add(point); - } + Points.RemoveAt(indicesToDelete[index]); } + } - /// - /// Deletes the coinsiding points. - /// - /// The tolerance. - public virtual void DeleteCoinsidingPoints(double tolerance) + public virtual void DeleteCoinsidingPoints() + { + const double defaultTolerance = 0.001; + DeleteCoinsidingPoints(defaultTolerance); + } + + public virtual double MinZ() + { + if (Points.Count > 1) { - // First build a list of indices of points that have to be removed (from end of list to start) - var indicesToDelete = new List(); - for (int pointIndex = Points.Count - 1; pointIndex > 0; pointIndex--) + double minZ = points.First().Z; + foreach (T point in points) { - for (int i = pointIndex - 1; i >= 0; i--) - { - if (Points[pointIndex].LocationEquals(Points[i], tolerance)) - { - indicesToDelete.Add(i); - } - } + minZ = Math.Min(minZ, point.Z); } - // Remove duplicate points beginning from the end - for (var index = 0; index < indicesToDelete.Count; index++) - { - Points.RemoveAt(indicesToDelete[index]); - } + return minZ; } - public virtual void DeleteCoinsidingPoints() - { - const double defaultTolerance = 0.001; - DeleteCoinsidingPoints(defaultTolerance); - } + return 0.0; + } - public virtual double MinZ() + public virtual double MaxZ() + { + if (Points.Count > 1) { - if (Points.Count > 1) + double maxZ = points.First().Z; + foreach (T point in points) { - double minZ = points.First().Z; - foreach (T point in points) - { - minZ = Math.Min(minZ, point.Z); - } - - return minZ; + maxZ = Math.Max(maxZ, point.Z); } - return 0.0; + return maxZ; } - public virtual double MaxZ() + return 0.0; + } + + public override string ToString() + { + var stringBuilder = new StringBuilder(); + + stringBuilder.Append(Name); + stringBuilder.Append(" ["); + foreach (T point in points) { - if (Points.Count > 1) + if (points.IndexOf(point) > 0) { - double maxZ = points.First().Z; - foreach (T point in points) - { - maxZ = Math.Max(maxZ, point.Z); - } - - return maxZ; + stringBuilder.Append(", "); } - return 0.0; + stringBuilder.Append(point); } - public override string ToString() - { - var stringBuilder = new StringBuilder(); + stringBuilder.Append("]"); - stringBuilder.Append(Name); - stringBuilder.Append(" ["); - foreach (T point in points) - { - if (points.IndexOf(point) > 0) - { - stringBuilder.Append(", "); - } + return stringBuilder.ToString(); + } - stringBuilder.Append(point); - } + private T InsertPointAtX(double X) + { + var newPoint = new T(); + newPoint.X = X; + try + { + T pointAfter = (from T point in points + where point.X > X + select point).First(); - stringBuilder.Append("]"); - - return stringBuilder.ToString(); + points.Insert(points.IndexOf(pointAfter), newPoint); } - - private T InsertPointAtX(double X) + catch { - var newPoint = new T(); - newPoint.X = X; - try - { - T pointAfter = (from T point in points - where point.X > X - select point).First(); - - points.Insert(points.IndexOf(pointAfter), newPoint); - } - catch - { - points.Add(newPoint); - } - - return newPoint; + points.Add(newPoint); } + + return newPoint; } } \ No newline at end of file