Index: DamEngine/trunk/src/Deltares.DamEngine.Data/General/PolyLine.cs =================================================================== diff -u -r3893 -r4000 --- DamEngine/trunk/src/Deltares.DamEngine.Data/General/PolyLine.cs (.../PolyLine.cs) (revision 3893) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/General/PolyLine.cs (.../PolyLine.cs) (revision 4000) @@ -33,14 +33,24 @@ public PolyLine() { - this.points = new List(); + points = new List(); } public virtual int Id { get; private set; } public virtual string Name { get; set; } - public virtual IList Points { get { return this.points; } private set { this.points = value; } } + public virtual IList Points + { + get + { + return points; + } + private set + { + points = value; + } + } /// /// Check if lines are equal @@ -49,14 +59,15 @@ /// public virtual bool Equals(PolyLine other) { - bool isEqual = (other.Points.Count == this.Points.Count); + bool isEqual = (other.Points.Count == Points.Count); if (isEqual) { - for (int pointIndex = 0; pointIndex < this.Points.Count; pointIndex++) + for (var pointIndex = 0; pointIndex < Points.Count; pointIndex++) { - isEqual = isEqual && (this.Points[pointIndex].LocationEquals(other.Points[pointIndex])); + isEqual = isEqual && (Points[pointIndex].LocationEquals(other.Points[pointIndex])); } } + return isEqual; } @@ -66,7 +77,7 @@ /// public bool Exists() { - return (this.Points.Count > 1); + return (Points.Count > 1); } /// @@ -75,15 +86,16 @@ /// public bool IsXStrictAscending() { - bool isStrictAscending = true; - for (int pointIndex = 0; pointIndex < this.Points.Count - 1; pointIndex++) + var isStrictAscending = true; + for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) { - if (this.Points[pointIndex + 1].X <= this.Points[pointIndex].X) + if (Points[pointIndex + 1].X <= Points[pointIndex].X) { isStrictAscending = false; break; } } + return isStrictAscending; } @@ -93,15 +105,16 @@ /// public bool IsXAscending() { - bool isAscending = true; - for (int pointIndex = 0; pointIndex < this.Points.Count - 1; pointIndex++) + var isAscending = true; + for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) { - if (this.Points[pointIndex + 1].X < this.Points[pointIndex].X) + if (Points[pointIndex + 1].X < Points[pointIndex].X) { isAscending = false; break; } } + return isAscending; } @@ -111,30 +124,11 @@ /// public T GetPointAtX(double X, double tolerance) { - return (from T point in this.points + return (from T point in points where Math.Abs(point.X - X) < tolerance select point).FirstOrDefault(); } - private T InsertPointAtX(double X) - { - T newPoint = new T(); - newPoint.X = X; - try - { - T pointAfter = (from T point in this.points - where point.X > X - select point).First(); - - this.points.Insert(this.points.IndexOf(pointAfter), newPoint); - } - catch - { - this.points.Add(newPoint); - } - return newPoint; - } - /// /// Gets point at X if present within tolerance; creates one there if not present. /// @@ -146,62 +140,47 @@ { point = InsertPointAtX(X); } + return point; } public double ZFromX(double X) { - if (!this.IsXAscending()) + if (!IsXAscending()) { throw new PolyLineException("Interpolation only possible with ascending polyline"); } - double valueZ = 0.0; + var valueZ = 0.0; // Less then first X - if (X <= this.Points[0].X) + if (X <= Points[0].X) { - valueZ = this.Points[0].Z; + valueZ = Points[0].Z; } // More then last X - else if (X >= this.Points[this.Points.Count - 1].X) + else if (X >= Points[Points.Count - 1].X) { - valueZ = this.Points[this.Points.Count - 1].Z; + valueZ = Points[Points.Count - 1].Z; } else { // X is inside boundaries - for (int pointIndex = 0; pointIndex < this.Points.Count - 1; pointIndex++) + for (var pointIndex = 0; pointIndex < Points.Count - 1; pointIndex++) { - if ((X > this.Points[pointIndex].X) && (X <= this.Points[pointIndex + 1].X)) + if ((X > Points[pointIndex].X) && (X <= Points[pointIndex + 1].X)) { // interpolate in this section - double fractionX = (X - this.Points[pointIndex].X) / (this.Points[pointIndex + 1].X - this.Points[pointIndex].X); - valueZ = this.Points[pointIndex].Z + fractionX * (this.Points[pointIndex + 1].Z - this.Points[pointIndex].Z); + 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 valueZ; } - public override string ToString() - { - StringBuilder stringBuilder = new StringBuilder(); - - stringBuilder.Append(this.Name); - stringBuilder.Append(" ["); - foreach (T point in this.points) - { - if (this.points.IndexOf(point) > 0) - stringBuilder.Append(", "); - stringBuilder.Append(point.ToString()); - } - stringBuilder.Append("]"); - - return stringBuilder.ToString(); - } - public virtual void CopyPoints(PolyLine polyLine) { foreach (T point in polyLine.Points) @@ -230,11 +209,10 @@ } // Remove duplicate points beginning from the end - for (int index = 0; index < indicesToDelete.Count; index++) + for (var index = 0; index < indicesToDelete.Count; index++) { Points.RemoveAt(indicesToDelete[index]); } - } public virtual void DeleteCoinsidingPoints() @@ -245,36 +223,75 @@ public virtual double MinZ() { - if (this.Points.Count > 1) + if (Points.Count > 1) { - double minZ = this.points.First().Z; - foreach (T point in this.points) + double minZ = points.First().Z; + foreach (T point in points) { minZ = Math.Min(minZ, point.Z); } + return minZ; } - else - { - return 0.0; - } + + return 0.0; } public virtual double MaxZ() { - if (this.Points.Count > 1) + if (Points.Count > 1) { - double maxZ = this.points.First().Z; - foreach (T point in this.points) + double maxZ = points.First().Z; + foreach (T point in points) { maxZ = Math.Max(maxZ, point.Z); } + return maxZ; } - else + + return 0.0; + } + + public override string ToString() + { + var stringBuilder = new StringBuilder(); + + stringBuilder.Append(Name); + stringBuilder.Append(" ["); + foreach (T point in points) { - return 0.0; + if (points.IndexOf(point) > 0) + { + stringBuilder.Append(", "); + } + + stringBuilder.Append(point); } + + stringBuilder.Append("]"); + + return stringBuilder.ToString(); } + + 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(); + + points.Insert(points.IndexOf(pointAfter), newPoint); + } + catch + { + points.Add(newPoint); + } + + return newPoint; + } } -} +} \ No newline at end of file