Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geotechnics/CharacteristicPointSet.cs =================================================================== diff -u -r4000 -r4052 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geotechnics/CharacteristicPointSet.cs (.../CharacteristicPointSet.cs) (revision 4000) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geotechnics/CharacteristicPointSet.cs (.../CharacteristicPointSet.cs) (revision 4052) @@ -25,564 +25,563 @@ using System.Linq; using Deltares.DamEngine.Data.Geometry; -namespace Deltares.DamEngine.Data.Geotechnics +namespace Deltares.DamEngine.Data.Geotechnics; + +/// +/// Represents a meta-data set of instances that describe +/// special point-locations of a geometric surfaceline. +/// +public class CharacteristicPointSet : IList, IList { + private readonly List annotations; + private readonly Dictionary typeCache; + private GeometryPointString geometry; + private bool geometryMustContainPoint; + /// - /// Represents a meta-data set of instances that describe - /// special point-locations of a geometric surfaceline. + /// Creates a characteristic point set whose points act as meta data for a geometric + /// description ( false) of a surfaceline. /// - public class CharacteristicPointSet : IList, IList + public CharacteristicPointSet() { - private readonly List annotations; - private readonly Dictionary typeCache; - private GeometryPointString geometry; - private bool geometryMustContainPoint; + typeCache = new Dictionary(); + annotations = new List(); + geometryMustContainPoint = false; + } - /// - /// Creates a characteristic point set whose points act as meta data for a geometric - /// description ( false) of a surfaceline. - /// - public CharacteristicPointSet() + /// + /// The observed localized geometry point string, describing the dike surfaceline + /// shape. + /// + /// References by aggregation. + public GeometryPointString Geometry + { + get { - typeCache = new Dictionary(); - annotations = new List(); - geometryMustContainPoint = false; + return geometry; } - - /// - /// The observed localized geometry point string, describing the dike surfaceline - /// shape. - /// - /// References by aggregation. - public GeometryPointString Geometry + set { - get - { - return geometry; - } - set - { - geometry = value; + geometry = value; - FullSyncWithGeometry(); - } + FullSyncWithGeometry(); } + } - /// - /// When true, requires all instances part of - /// this set to be contained in and cannot have instances - /// not part of . - /// When false, all instances are required NOT - /// to be part of . Their height will then be determined by - /// the height profile defined by . - /// - public bool GeometryMustContainPoint + /// + /// When true, requires all instances part of + /// this set to be contained in and cannot have instances + /// not part of . + /// When false, all instances are required NOT + /// to be part of . Their height will then be determined by + /// the height profile defined by . + /// + public bool GeometryMustContainPoint + { + get { - get - { - return geometryMustContainPoint; - } - set - { - geometryMustContainPoint = value; + return geometryMustContainPoint; + } + set + { + geometryMustContainPoint = value; - FullSyncWithGeometry(); - } + FullSyncWithGeometry(); } + } - /// - /// Retrieves the annotated with the given type. - /// - /// Annotation to look for. - /// The instances annotated with the given type; - /// Null if no definition can be found. - /// This method relies on this class observing changes to its - /// to work correctly. - public GeometryPoint GetGeometryPoint(CharacteristicPointType characteristicPointType) + /// + /// Retrieves the annotated with the given type. + /// + /// Annotation to look for. + /// The instances annotated with the given type; + /// Null if no definition can be found. + /// This method relies on this class observing changes to its + /// to work correctly. + public GeometryPoint GetGeometryPoint(CharacteristicPointType characteristicPointType) + { + return typeCache.ContainsKey(characteristicPointType) + ? typeCache[characteristicPointType] + : null; + } + + /// + /// Sorts this characteristic point set items on + /// ascending. If is true, + /// shall also be sorted. + /// + public void Sort() + { + if (GeometryMustContainPoint && Geometry != null) { - return typeCache.ContainsKey(characteristicPointType) - ? typeCache[characteristicPointType] - : null; + Geometry.SortPointsByXAscending(); } - /// - /// Sorts this characteristic point set items on - /// ascending. If is true, - /// shall also be sorted. - /// - public void Sort() - { - if (GeometryMustContainPoint && Geometry != null) - { - Geometry.SortPointsByXAscending(); - } + annotations.Sort(); + } - annotations.Sort(); - } + /// + /// Annotates the characteristic point at the specified index. + /// + /// The index. + /// The new type of the characteristic point. + /// When is + /// less then 0 or greater or equal to . + /// will be returning the correct points. + public void Annotate(int index, CharacteristicPointType characteristicPointType) + { + annotations[index].CharacteristicPointType = characteristicPointType; + UpdateTypeCache(annotations[index]); + } - /// - /// Annotates the characteristic point at the specified index. - /// - /// The index. - /// The new type of the characteristic point. - /// When is - /// less then 0 or greater or equal to . - /// will be returning the correct points. - public void Annotate(int index, CharacteristicPointType characteristicPointType) + /// + /// Snaps the characteristic point height to if it's not part + /// of it, or set it to in case no + /// is available. + /// + /// The characteristic point whose height is to be updated. + private void UpdateCharacteristicPointHeight(CharacteristicPoint characteristicPoint) + { + if (geometry == null) { - annotations[index].CharacteristicPointType = characteristicPointType; - UpdateTypeCache(annotations[index]); + characteristicPoint.Z = double.NaN; } - - /// - /// Snaps the characteristic point height to if it's not part - /// of it, or set it to in case no - /// is available. - /// - /// The characteristic point whose height is to be updated. - private void UpdateCharacteristicPointHeight(CharacteristicPoint characteristicPoint) + else if (!geometry.Points.Contains(characteristicPoint.GeometryPoint)) { - if (geometry == null) + if (double.IsNaN(characteristicPoint.X)) { characteristicPoint.Z = double.NaN; } - else if (!geometry.Points.Contains(characteristicPoint.GeometryPoint)) + else { - if (double.IsNaN(characteristicPoint.X)) - { - characteristicPoint.Z = double.NaN; - } - else - { - // Note: Cannot use GeometryPointString.GetZAtX due to its requirement - // that Geometry.Points is sorted on X. - characteristicPoint.Z = geometry.GetZAtUnsortedX(characteristicPoint.X); - } + // Note: Cannot use GeometryPointString.GetZAtX due to its requirement + // that Geometry.Points is sorted on X. + characteristicPoint.Z = geometry.GetZAtUnsortedX(characteristicPoint.X); } - // Else: Keep Z the same as in Geometry } + // Else: Keep Z the same as in Geometry + } - private void FullSyncWithGeometry() + private void FullSyncWithGeometry() + { + if (GeometryMustContainPoint) { - if (GeometryMustContainPoint) - { - // Resync with geometry; Do not keep previous data: - ClearCharacteristicPointSet(); + // Resync with geometry; Do not keep previous data: + ClearCharacteristicPointSet(); - if (geometry == null) - { - return; - } - - foreach (GeometryPoint geometryPoint in geometry.Points) - { - Add(new CharacteristicPoint - { - GeometryPoint = geometryPoint, - CharacteristicPointType = CharacteristicPointType.None - }); - } + if (geometry == null) + { + return; } - else + + foreach (GeometryPoint geometryPoint in geometry.Points) { - // Remove all characteristic points whose GeometryPoint are part of Geometry - // as per required by the value of GeometryMustContainPoint. All other - // characteristic points should have their height updated: - foreach (CharacteristicPoint characteristicPoint in annotations.ToArray()) + Add(new CharacteristicPoint { - if (geometry != null && geometry.Points.Contains(characteristicPoint.GeometryPoint)) - { - Remove(characteristicPoint); - } - else - { - UpdateCharacteristicPointHeight(characteristicPoint); - } - } + GeometryPoint = geometryPoint, + CharacteristicPointType = CharacteristicPointType.None + }); } } - - private void ClearCharacteristicPointSet() + else { - ClearAnnotations(); - typeCache.Clear(); - } - - private void ClearAnnotations() - { - annotations.Clear(); - } - - private void UpdateTypeCache(CharacteristicPoint item) - { - if (item.CharacteristicPointType != CharacteristicPointType.None) + // Remove all characteristic points whose GeometryPoint are part of Geometry + // as per required by the value of GeometryMustContainPoint. All other + // characteristic points should have their height updated: + foreach (CharacteristicPoint characteristicPoint in annotations.ToArray()) { - // Prevent duplication: - CharacteristicPoint alreadyDefined = annotations.FirstOrDefault(cp => cp.CharacteristicPointType == item.CharacteristicPointType && - !ReferenceEquals(cp.GeometryPoint, item.GeometryPoint)); - if (alreadyDefined != null) + if (geometry != null && geometry.Points.Contains(characteristicPoint.GeometryPoint)) { - alreadyDefined.CharacteristicPointType = CharacteristicPointType.None; + Remove(characteristicPoint); } - - // Set new annotation definition: - typeCache[item.CharacteristicPointType] = item.GeometryPoint; + else + { + UpdateCharacteristicPointHeight(characteristicPoint); + } } } + } - #region Implementation: IList, IList + private void ClearCharacteristicPointSet() + { + ClearAnnotations(); + typeCache.Clear(); + } - /// - /// Gets an object that can be used to synchronize access to the . - /// - public object SyncRoot { get; } = new object(); + private void ClearAnnotations() + { + annotations.Clear(); + } - /// - /// Gets a value indicating whether access to the is synchronized (thread safe). - /// - public bool IsSynchronized + private void UpdateTypeCache(CharacteristicPoint item) + { + if (item.CharacteristicPointType != CharacteristicPointType.None) { - get + // Prevent duplication: + CharacteristicPoint alreadyDefined = annotations.FirstOrDefault(cp => cp.CharacteristicPointType == item.CharacteristicPointType && + !ReferenceEquals(cp.GeometryPoint, item.GeometryPoint)); + if (alreadyDefined != null) { - return false; + alreadyDefined.CharacteristicPointType = CharacteristicPointType.None; } - } - /// - /// Gets a value indicating whether the has a fixed size. - /// - public bool IsFixedSize - { - get - { - return false; - } + // Set new annotation definition: + typeCache[item.CharacteristicPointType] = item.GeometryPoint; } + } - /// - /// Gets or sets the element at the specified index. - /// - /// The index. - /// - object IList.this[int index] - { - get - { - return this[index]; - } - set - { - this[index] = (CharacteristicPoint) value; - } - } + #region Implementation: IList, IList - /// - /// Gets the number of elements contained in the . - /// - public int Count + /// + /// Gets an object that can be used to synchronize access to the . + /// + public object SyncRoot { get; } = new object(); + + /// + /// Gets a value indicating whether access to the is synchronized (thread safe). + /// + public bool IsSynchronized + { + get { - get - { - return annotations.Count; - } + return false; } + } - /// - /// Gets a value indicating whether the is read-only. - /// - public bool IsReadOnly + /// + /// Gets a value indicating whether the has a fixed size. + /// + public bool IsFixedSize + { + get { - get - { - return false; - } + return false; } + } - /// - /// Gets or sets the element at the specified index. - /// - /// The index. - /// - public CharacteristicPoint this[int index] + /// + /// Gets or sets the element at the specified index. + /// + /// The index. + /// + object IList.this[int index] + { + get { - get - { - return annotations[index]; - } - set - { - RemoveAt(index); - Insert(index, value); - } + return this[index]; } - - /// - /// Adds an item to the . - /// - /// The object to add to the . - /// - /// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection, - /// - public int Add(object value) + set { - Add((CharacteristicPoint) value); - return Count - 1; + this[index] = (CharacteristicPoint) value; } + } - /// - /// Determines whether the contains a specific value. - /// - /// The object to locate in the . - /// - /// true if the is found in the ; otherwise, false. - /// - public bool Contains(object value) + /// + /// Gets the number of elements contained in the . + /// + public int Count + { + get { - var cp = value as CharacteristicPoint; - if (cp == null) - { - return false; - } - - return Contains(cp); + return annotations.Count; } + } - /// - /// Copies the elements of the to an , starting at a particular index. - /// - /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. - /// The zero-based index in at which copying begins. - public void CopyTo(Array array, int index) + /// + /// Gets a value indicating whether the is read-only. + /// + public bool IsReadOnly + { + get { - CopyTo((CharacteristicPoint[]) array, index); + return false; } + } - /// - /// Determines the index of a specific item in the . - /// - /// The object to locate in the . - /// - /// The index of if found in the list; otherwise, -1. - /// - public int IndexOf(object value) + /// + /// Gets or sets the element at the specified index. + /// + /// The index. + /// + public CharacteristicPoint this[int index] + { + get { - return IndexOf((CharacteristicPoint) value); + return annotations[index]; } - - /// - /// Inserts an item to the at the specified index. - /// - /// The zero-based index at which should be inserted. - /// The object to insert into the . - public void Insert(int index, object value) + set { - Insert(index, (CharacteristicPoint) value); + RemoveAt(index); + Insert(index, value); } + } - /// - /// Removes the first occurrence of a specific object from the . - /// - /// The object to remove from the . - public void Remove(object value) + /// + /// Adds an item to the . + /// + /// The object to add to the . + /// + /// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection, + /// + public int Add(object value) + { + Add((CharacteristicPoint) value); + return Count - 1; + } + + /// + /// Determines whether the contains a specific value. + /// + /// The object to locate in the . + /// + /// true if the is found in the ; otherwise, false. + /// + public bool Contains(object value) + { + var cp = value as CharacteristicPoint; + if (cp == null) { - Remove((CharacteristicPoint) value); + return false; } - /// - /// Removes the first occurrence of a specific object from the . - /// - /// The object to remove from the . - /// - /// true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . - /// - public bool Remove(CharacteristicPoint item) - { - bool removed = PerformCollectionRemoveWithEvents(annotations, item); + return Contains(cp); + } - if (removed) - { - typeCache.Remove(item.CharacteristicPointType); + /// + /// Copies the elements of the to an , starting at a particular index. + /// + /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. + /// The zero-based index in at which copying begins. + public void CopyTo(Array array, int index) + { + CopyTo((CharacteristicPoint[]) array, index); + } - bool removeGeometryPoint = GeometryMustContainPoint && Geometry != null && - Geometry.Points.Contains(item.GeometryPoint) && - !annotations.Any(cp => ReferenceEquals(cp.GeometryPoint, item.GeometryPoint)); + /// + /// Determines the index of a specific item in the . + /// + /// The object to locate in the . + /// + /// The index of if found in the list; otherwise, -1. + /// + public int IndexOf(object value) + { + return IndexOf((CharacteristicPoint) value); + } - if (removeGeometryPoint) - { - PerformCollectionRemoveWithEvents(Geometry.Points, item.GeometryPoint); - } - } + /// + /// Inserts an item to the at the specified index. + /// + /// The zero-based index at which should be inserted. + /// The object to insert into the . + public void Insert(int index, object value) + { + Insert(index, (CharacteristicPoint) value); + } - return removed; - } + /// + /// Removes the first occurrence of a specific object from the . + /// + /// The object to remove from the . + public void Remove(object value) + { + Remove((CharacteristicPoint) value); + } - /// - /// Adds an item to the . - /// - /// The object to add to the . - public void Add(CharacteristicPoint item) - { - Insert(Count, item); - } + /// + /// Removes the first occurrence of a specific object from the . + /// + /// The object to remove from the . + /// + /// true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . + /// + public bool Remove(CharacteristicPoint item) + { + bool removed = PerformCollectionRemoveWithEvents(annotations, item); - /// - /// Removes all items from the . - /// - public void Clear() + if (removed) { - ClearCharacteristicPointSet(); + typeCache.Remove(item.CharacteristicPointType); - if (GeometryMustContainPoint && Geometry != null) + bool removeGeometryPoint = GeometryMustContainPoint && Geometry != null && + Geometry.Points.Contains(item.GeometryPoint) && + !annotations.Any(cp => ReferenceEquals(cp.GeometryPoint, item.GeometryPoint)); + + if (removeGeometryPoint) { - Geometry.Points.Clear(); - Geometry.CalcPoints.Clear(); + PerformCollectionRemoveWithEvents(Geometry.Points, item.GeometryPoint); } } - /// - /// Determines whether the contains a specific value. - /// - /// The object to locate in the . - /// - /// true if is found in the ; otherwise, false. - /// - public bool Contains(CharacteristicPoint item) - { - return annotations.Contains(item); - } + return removed; + } - /// - /// Copies to. - /// - /// The array. - /// Index of the array. - public void CopyTo(CharacteristicPoint[] array, int arrayIndex) + /// + /// Adds an item to the . + /// + /// The object to add to the . + public void Add(CharacteristicPoint item) + { + Insert(Count, item); + } + + /// + /// Removes all items from the . + /// + public void Clear() + { + ClearCharacteristicPointSet(); + + if (GeometryMustContainPoint && Geometry != null) { - annotations.CopyTo(array, arrayIndex); + Geometry.Points.Clear(); + Geometry.CalcPoints.Clear(); } + } - /// - /// Determines the index of a specific item in the . - /// - /// The object to locate in the . - /// - /// The index of if found in the list; otherwise, -1. - /// - public int IndexOf(CharacteristicPoint item) + /// + /// Determines whether the contains a specific value. + /// + /// The object to locate in the . + /// + /// true if is found in the ; otherwise, false. + /// + public bool Contains(CharacteristicPoint item) + { + return annotations.Contains(item); + } + + /// + /// Copies to. + /// + /// The array. + /// Index of the array. + public void CopyTo(CharacteristicPoint[] array, int arrayIndex) + { + annotations.CopyTo(array, arrayIndex); + } + + /// + /// Determines the index of a specific item in the . + /// + /// The object to locate in the . + /// + /// The index of if found in the list; otherwise, -1. + /// + public int IndexOf(CharacteristicPoint item) + { + return annotations.IndexOf(item); + } + + /// + /// Inserts an item to the at the specified index. + /// + /// The zero-based index at which should be inserted. + /// The object to insert into the . + public void Insert(int index, CharacteristicPoint item) + { + if (!ReferenceEquals(item.PointSet, this)) { - return annotations.IndexOf(item); + item.PointSet = this; } - /// - /// Inserts an item to the at the specified index. - /// - /// The zero-based index at which should be inserted. - /// The object to insert into the . - public void Insert(int index, CharacteristicPoint item) + GeometryPoint itemAtIndex = index < annotations.Count ? annotations[index].GeometryPoint : null; + PerformListInsertWithEvents(annotations, item, index); + if (GeometryMustContainPoint && Geometry != null && !Geometry.Points.Contains(item.GeometryPoint)) { - if (!ReferenceEquals(item.PointSet, this)) + int geometryIndex = Geometry.Points.Count; + if (null != itemAtIndex) { - item.PointSet = this; - } - - GeometryPoint itemAtIndex = index < annotations.Count ? annotations[index].GeometryPoint : null; - PerformListInsertWithEvents(annotations, item, index); - if (GeometryMustContainPoint && Geometry != null && !Geometry.Points.Contains(item.GeometryPoint)) - { - int geometryIndex = Geometry.Points.Count; - if (null != itemAtIndex) - { - for (var i = 0; i < Geometry.Points.Count; i++) - { - if (ReferenceEquals(Geometry.Points[i], itemAtIndex)) - { - geometryIndex = i; - break; - } - } - } - - // Check if point at same position already exists and set that point to the existing point - // Do this to avoid points in Surfaceline.Geometry with the same location - var IsPointExist = false; for (var i = 0; i < Geometry.Points.Count; i++) { - if (Geometry.Points[i].LocationEquals(item.GeometryPoint)) + if (ReferenceEquals(Geometry.Points[i], itemAtIndex)) { - item.GeometryPoint = Geometry.Points[i]; - IsPointExist = true; + geometryIndex = i; break; } } + } - if (!IsPointExist) + // Check if point at same position already exists and set that point to the existing point + // Do this to avoid points in Surfaceline.Geometry with the same location + var IsPointExist = false; + for (var i = 0; i < Geometry.Points.Count; i++) + { + if (Geometry.Points[i].LocationEquals(item.GeometryPoint)) { - // Only add new point if no point on same location was found - PerformListInsertWithEvents(Geometry.Points, item.GeometryPoint, geometryIndex); + item.GeometryPoint = Geometry.Points[i]; + IsPointExist = true; + break; } } - if (geometry != null) + if (!IsPointExist) { - UpdateCharacteristicPointHeight(item); + // Only add new point if no point on same location was found + PerformListInsertWithEvents(Geometry.Points, item.GeometryPoint, geometryIndex); } - - UpdateTypeCache(item); } - /// - /// Removes the item at the specified index. - /// - /// The zero-based index of the item to remove. - public void RemoveAt(int index) + if (geometry != null) { - Remove(annotations[index]); + UpdateCharacteristicPointHeight(item); } - /// - /// Returns an enumerator that iterates through a collection. - /// - /// - /// An object that can be used to iterate through the collection. - /// - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + UpdateTypeCache(item); + } - /// - /// Returns an enumerator that iterates through the collection. - /// - /// - /// A that can be used to iterate through the collection. - /// - public IEnumerator GetEnumerator() - { - return annotations.GetEnumerator(); - } + /// + /// Removes the item at the specified index. + /// + /// The zero-based index of the item to remove. + public void RemoveAt(int index) + { + Remove(annotations[index]); + } - #endregion + /// + /// Returns an enumerator that iterates through a collection. + /// + /// + /// An object that can be used to iterate through the collection. + /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } - #region Events + /// + /// Returns an enumerator that iterates through the collection. + /// + /// + /// A that can be used to iterate through the collection. + /// + public IEnumerator GetEnumerator() + { + return annotations.GetEnumerator(); + } - /// - /// - /// Type of the list elements. - /// The list where the item is inserted into. - /// The item inserted into the list. - /// Index where the item is inserted. - private static void PerformListInsertWithEvents(IList list, T item, int index) - { - list.Insert(index, item); - } + #endregion - private static bool PerformCollectionRemoveWithEvents(ICollection list, T item) - { - bool removed = list.Remove(item); + #region Events - return removed; - } + /// + /// + /// Type of the list elements. + /// The list where the item is inserted into. + /// The item inserted into the list. + /// Index where the item is inserted. + private static void PerformListInsertWithEvents(IList list, T item, int index) + { + list.Insert(index, item); + } - #endregion + private static bool PerformCollectionRemoveWithEvents(ICollection list, T item) + { + bool removed = list.Remove(item); + + return removed; } + + #endregion } \ No newline at end of file