Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9860 was 9730, checked in by ascheibe, 11 years ago

#1886 added a library that calculates convex hulls

File size: 6.1 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    /// A helper class used to connect faces.
46    /// </summary>
47    sealed class FaceConnector
48    {
49        /// <summary>
50        /// The face.
51        /// </summary>
52        public ConvexFaceInternal Face;
53
54        /// <summary>
55        /// The edge to be connected.
56        /// </summary>
57        public int EdgeIndex;
58
59        /// <summary>
60        /// The vertex indices.
61        /// </summary>
62        public int[] Vertices;
63
64        /// <summary>
65        /// The hash code computed from indices.
66        /// </summary>
67        public uint HashCode;
68
69        /// <summary>
70        /// Prev node in the list.
71        /// </summary>
72        public FaceConnector Previous;
73
74        /// <summary>
75        /// Next node in the list.
76        /// </summary>
77        public FaceConnector Next;
78
79        /// <summary>
80        /// Ctor.
81        /// </summary>
82        /// <param name="dimension"></param>
83        public FaceConnector(int dimension)
84        {
85            Vertices = new int[dimension - 1];
86        }
87
88        /// <summary>
89        /// Updates the connector.
90        /// </summary>
91        /// <param name="face"></param>
92        /// <param name="edgeIndex"></param>
93        /// <param name="dim"></param>
94        public void Update(ConvexFaceInternal face, int edgeIndex, int dim)
95        {
96            this.Face = face;
97            this.EdgeIndex = edgeIndex;
98
99            uint hashCode = 31;
100
101            var vs = face.Vertices;
102            for (int i = 0, c = 0; i < dim; i++)
103            {
104                if (i != edgeIndex)
105                {
106                    var v = vs[i].Index;
107                    this.Vertices[c++] = v;
108                    hashCode += unchecked(23 * hashCode + (uint)v);
109                }
110            }
111
112            this.HashCode = hashCode;
113        }
114
115        /// <summary>
116        /// Can two faces be connected.
117        /// </summary>
118        /// <param name="a"></param>
119        /// <param name="b"></param>
120        /// <param name="dim"></param>
121        /// <returns></returns>
122        public static bool AreConnectable(FaceConnector a, FaceConnector b, int dim)
123        {
124            if (a.HashCode != b.HashCode) return false;
125
126            var n = dim - 1;
127            var av = a.Vertices;
128            var bv = b.Vertices;
129            for (int i = 0; i < n; i++)
130            {
131                if (av[i] != bv[i]) return false;
132            }
133
134            return true;
135        }
136
137        /// <summary>
138        /// Connect two faces.
139        /// </summary>
140        /// <param name="a"></param>
141        /// <param name="b"></param>
142        public static void Connect(FaceConnector a, FaceConnector b)
143        {
144            a.Face.AdjacentFaces[a.EdgeIndex] = b.Face;
145            b.Face.AdjacentFaces[b.EdgeIndex] = a.Face;
146        }
147    }
148
149    /// <summary>
150    /// This internal class manages the faces of the convex hull. It is a
151    /// separate class from the desired user class.
152    /// </summary>
153    sealed class ConvexFaceInternal
154    {
155        /// <summary>
156        /// Initializes a new instance of the <see cref="ConvexFaceInternal"/> class.
157        /// </summary>
158        public ConvexFaceInternal(int dimension, VertexBuffer beyondList)
159        {
160            AdjacentFaces = new ConvexFaceInternal[dimension];
161            VerticesBeyond = beyondList;
162            Normal = new double[dimension];
163            Vertices = new VertexWrap[dimension];
164        }
165
166        /// <summary>
167        /// Gets or sets the adjacent face data.
168        /// </summary>
169        public ConvexFaceInternal[] AdjacentFaces;
170
171        /// <summary>
172        /// Gets or sets the vertices beyond.
173        /// </summary>
174        public VertexBuffer VerticesBeyond;
175
176        /// <summary>
177        /// The furthest vertex.
178        /// </summary>
179        public VertexWrap FurthestVertex;
180
181        /////// <summary>
182        /////// Distance to the furthest vertex.
183        /////// </summary>
184        ////public double FurthestDistance;
185
186        /// <summary>
187        /// Gets or sets the vertices.
188        /// </summary>
189        public VertexWrap[] Vertices;
190       
191        /// <summary>
192        /// Gets or sets the normal vector.
193        /// </summary>
194        public double[] Normal;
195
196        /// <summary>
197        /// Is the normal flipped?
198        /// </summary>
199        public bool IsNormalFlipped;
200
201        /// <summary>
202        /// Face plane constant element.
203        /// </summary>
204        public double Offset;
205
206        /// <summary>
207        /// Used to traverse affected faces and create the Delaunay representation.
208        /// </summary>
209        public int Tag;
210
211        /// <summary>
212        /// Prev node in the list.
213        /// </summary>
214        public ConvexFaceInternal Previous;
215
216        /// <summary>
217        /// Next node in the list.
218        /// </summary>
219        public ConvexFaceInternal Next;
220
221        /// <summary>
222        /// Is it present in the list.
223        /// </summary>
224        public bool InList;
225    }
226}
Note: See TracBrowser for help on using the repository browser.