Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/MIConvexHull/ConvexHull/ConvexFaceInternal.cs @ 10718

Last change on this file since 10718 was 9945, checked in by ascheibe, 11 years ago

#1886

  • added new convex hull algorithm based on SMO/SVM
  • added unit test and a visualization
  • updated MIConvexHull
File size: 6.5 KB
Line 
1namespace MIConvexHull
2{
3    using System.Collections.Generic;
4
5    /// <summary>
6    /// Wraps each IVertex to allow marking of nodes.
7    /// </summary>
8    sealed class VertexWrap
9    {
10        /// <summary>
11        /// Ref. to the original vertex.
12        /// </summary>
13        public IVertex Vertex;
14
15        /// <summary>
16        /// Direct reference to PositionData makes IsVertexOverFace faster.
17        /// </summary>
18        public double[] PositionData;
19
20        /// <summary>
21        /// Vertex index.
22        /// </summary>
23        public int Index;
24
25        /// <summary>
26        /// Used mostly to enumerate unique vertices.
27        /// </summary>
28        public bool Marked;
29    }
30
31    /// <summary>
32    /// Compare vertices based on their indices.
33    /// </summary>
34    class VertexWrapComparer : IComparer<VertexWrap>
35    {
36        public static readonly VertexWrapComparer Instance = new VertexWrapComparer();
37
38        public int Compare(VertexWrap x, VertexWrap y)
39        {
40            return x.Index.CompareTo(y.Index);
41        }
42    }
43
44    /// <summary>
45    /// For deferred face addition.
46    /// </summary>
47    sealed class DeferredFace
48    {
49        /// <summary>
50        /// The faces.
51        /// </summary>
52        public ConvexFaceInternal Face, Pivot, OldFace;
53
54        /// <summary>
55        /// The indices.
56        /// </summary>
57        public int FaceIndex, PivotIndex;
58    }
59
60    /// <summary>
61    /// A helper class used to connect faces.
62    /// </summary>
63    sealed class FaceConnector
64    {
65        /// <summary>
66        /// The face.
67        /// </summary>
68        public ConvexFaceInternal Face;
69
70        /// <summary>
71        /// The edge to be connected.
72        /// </summary>
73        public int EdgeIndex;
74
75        /// <summary>
76        /// The vertex indices.
77        /// </summary>
78        public int[] Vertices;
79
80        /// <summary>
81        /// The hash code computed from indices.
82        /// </summary>
83        public uint HashCode;
84
85        /// <summary>
86        /// Prev node in the list.
87        /// </summary>
88        public FaceConnector Previous;
89
90        /// <summary>
91        /// Next node in the list.
92        /// </summary>
93        public FaceConnector Next;
94
95        /// <summary>
96        /// Ctor.
97        /// </summary>
98        /// <param name="dimension"></param>
99        public FaceConnector(int dimension)
100        {
101            Vertices = new int[dimension - 1];
102        }
103
104        /// <summary>
105        /// Updates the connector.
106        /// </summary>
107        /// <param name="face"></param>
108        /// <param name="edgeIndex"></param>
109        /// <param name="dim"></param>
110        public void Update(ConvexFaceInternal face, int edgeIndex, int dim)
111        {
112            this.Face = face;
113            this.EdgeIndex = edgeIndex;
114
115            uint hashCode = 31;
116
117            var vs = face.Vertices;
118            for (int i = 0, c = 0; i < dim; i++)
119            {
120                if (i != edgeIndex)
121                {
122                    var v = vs[i].Index;
123                    this.Vertices[c++] = v;
124                    hashCode += unchecked(23 * hashCode + (uint)v);
125                }
126            }
127
128            this.HashCode = hashCode;
129        }
130
131        /// <summary>
132        /// Can two faces be connected.
133        /// </summary>
134        /// <param name="a"></param>
135        /// <param name="b"></param>
136        /// <param name="dim"></param>
137        /// <returns></returns>
138        public static bool AreConnectable(FaceConnector a, FaceConnector b, int dim)
139        {
140            if (a.HashCode != b.HashCode) return false;
141
142            var n = dim - 1;
143            var av = a.Vertices;
144            var bv = b.Vertices;
145            for (int i = 0; i < n; i++)
146            {
147                if (av[i] != bv[i]) return false;
148            }
149
150            return true;
151        }
152
153        /// <summary>
154        /// Connect two faces.
155        /// </summary>
156        /// <param name="a"></param>
157        /// <param name="b"></param>
158        public static void Connect(FaceConnector a, FaceConnector b)
159        {
160            a.Face.AdjacentFaces[a.EdgeIndex] = b.Face;
161            b.Face.AdjacentFaces[b.EdgeIndex] = a.Face;
162        }
163    }
164
165    /// <summary>
166    /// This internal class manages the faces of the convex hull. It is a
167    /// separate class from the desired user class.
168    /// </summary>
169    sealed class ConvexFaceInternal
170    {
171        /// <summary>
172        /// Initializes a new instance of the <see cref="ConvexFaceInternal"/> class.
173        /// </summary>
174        public ConvexFaceInternal(int dimension, VertexBuffer beyondList)
175        {
176            AdjacentFaces = new ConvexFaceInternal[dimension];
177            VerticesBeyond = beyondList;
178            Normal = new double[dimension];
179            Vertices = new VertexWrap[dimension];
180        }
181
182        /// <summary>
183        /// Gets or sets the adjacent face data.
184        /// </summary>
185        public ConvexFaceInternal[] AdjacentFaces;
186
187        /// <summary>
188        /// Gets or sets the vertices beyond.
189        /// </summary>
190        public VertexBuffer VerticesBeyond;
191
192        /// <summary>
193        /// The furthest vertex.
194        /// </summary>
195        public VertexWrap FurthestVertex;
196
197        /////// <summary>
198        /////// Distance to the furthest vertex.
199        /////// </summary>
200        ////public double FurthestDistance;
201
202        /// <summary>
203        /// Gets or sets the vertices.
204        /// </summary>
205        public VertexWrap[] Vertices;
206       
207        /// <summary>
208        /// Gets or sets the normal vector.
209        /// </summary>
210        public double[] Normal;
211
212        /// <summary>
213        /// Is the normal flipped?
214        /// </summary>
215        public bool IsNormalFlipped;
216
217        /// <summary>
218        /// Face plane constant element.
219        /// </summary>
220        public double Offset;
221
222        /// <summary>
223        /// Used to traverse affected faces and create the Delaunay representation.
224        /// </summary>
225        public int Tag;
226
227        /// <summary>
228        /// Prev node in the list.
229        /// </summary>
230        public ConvexFaceInternal Previous;
231
232        /// <summary>
233        /// Next node in the list.
234        /// </summary>
235        public ConvexFaceInternal Next;
236
237        /// <summary>
238        /// Is it present in the list.
239        /// </summary>
240        public bool InList;
241    }
242}
Note: See TracBrowser for help on using the repository browser.