/****************************************************************************** * * The MIT License (MIT) * * MIConvexHull, Copyright (c) 2015 David Sehnal, Matthew Campbell * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * *****************************************************************************/ using System; namespace NWH.DWP2.MiConvexHull { /// /// A more lightweight alternative to List of T. /// On clear, only resets the count and does not clear the references /// => this works because of the ObjectManager. /// Includes a stack functionality. /// /// internal class SimpleList { /// /// The count /// public int Count; /// /// The capacity /// private int capacity; /// /// The items /// private T[] items; /// /// Get the i-th element. /// /// The i. /// T. public T this[int i] { get { return items[i]; } set { items[i] = value; } } /// /// Size matters. /// private void EnsureCapacity() { if (capacity == 0) { capacity = 32; items = new T[32]; } else { T[] newItems = new T[capacity * 2]; Array.Copy(items, newItems, capacity); capacity = 2 * capacity; items = newItems; } } /// /// Adds a vertex to the buffer. /// /// The item. public void Add(T item) { if (Count + 1 > capacity) { EnsureCapacity(); } items[Count++] = item; } /// /// Pushes the value to the back of the list. /// /// The item. public void Push(T item) { if (Count + 1 > capacity) { EnsureCapacity(); } items[Count++] = item; } /// /// Pops the last value from the list. /// /// T. public T Pop() { return items[--Count]; } /// /// Sets the Count to 0, otherwise does nothing. /// public void Clear() { Count = 0; } } /// /// A fancy name for a list of integers. /// /// internal class IndexBuffer : SimpleList { } /// /// A priority based linked list. /// internal sealed class FaceList { /// /// The last /// private ConvexFaceInternal last; /// /// Get the first element. /// /// The first. public ConvexFaceInternal First { get; private set; } /// /// Adds the element to the beginning. /// /// The face. private void AddFirst(ConvexFaceInternal face) { face.InList = true; First.Previous = face; face.Next = First; First = face; } /// /// Adds a face to the list. /// /// The face. public void Add(ConvexFaceInternal face) { if (face.InList) { if (First.VerticesBeyond.Count < face.VerticesBeyond.Count) { Remove(face); AddFirst(face); } return; } face.InList = true; if (First != null && First.VerticesBeyond.Count < face.VerticesBeyond.Count) { First.Previous = face; face.Next = First; First = face; } else { if (last != null) { last.Next = face; } face.Previous = last; last = face; if (First == null) { First = face; } } } /// /// Removes the element from the list. /// /// The face. public void Remove(ConvexFaceInternal face) { if (!face.InList) { return; } face.InList = false; if (face.Previous != null) { face.Previous.Next = face.Next; } else if ( /*first == face*/ face.Previous == null) { First = face.Next; } if (face.Next != null) { face.Next.Previous = face.Previous; } else if ( /*last == face*/ face.Next == null) { last = face.Previous; } face.Next = null; face.Previous = null; } } /// /// Connector list. /// internal sealed class ConnectorList { /// /// The last /// private FaceConnector last; /// /// Get the first element. /// /// The first. public FaceConnector First { get; private set; } /// /// Adds the element to the beginning. /// /// The connector. private void AddFirst(FaceConnector connector) { First.Previous = connector; connector.Next = First; First = connector; } /// /// Adds a face to the list. /// /// The element. public void Add(FaceConnector element) { if (last != null) { last.Next = element; } element.Previous = last; last = element; if (First == null) { First = element; } } /// /// Removes the element from the list. /// /// The connector. public void Remove(FaceConnector connector) { if (connector.Previous != null) { connector.Previous.Next = connector.Next; } else if ( /*first == face*/ connector.Previous == null) { First = connector.Next; } if (connector.Next != null) { connector.Next.Previous = connector.Previous; } else if ( /*last == face*/ connector.Next == null) { last = connector.Previous; } connector.Next = null; connector.Previous = null; } } }