-
Notifications
You must be signed in to change notification settings - Fork 2
/
EdgeList.cs
138 lines (122 loc) · 3.81 KB
/
EdgeList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisualVoronoi
{
class EdgeList
{
private double deltaX;
private double xMin;
private int hashSize;
private List<HalfEdge> hash;
public HalfEdge leftEnd;
public HalfEdge rightEnd;
public EdgeList(double xMin, double deltaX, int sqrtNSites)
{
this.xMin = xMin;
this.deltaX = deltaX;
this.hashSize = (2 * sqrtNSites);
hash = new List<HalfEdge>(hashSize);
leftEnd = HalfEdge.CreateDummy();
rightEnd = HalfEdge.CreateDummy();
leftEnd.edgeListLeftNeighbor = null;
leftEnd.edgeListRightNeighbor = rightEnd;
rightEnd.edgeListLeftNeighbor = leftEnd;
rightEnd.edgeListRightNeighbor = null;
hash.Add(leftEnd);
for (int i = 1; i < hashSize - 1; i++)
hash.Add(null);
hash.Add(rightEnd);
}
public void Insert(HalfEdge lB, HalfEdge nHalfEdge)
{
nHalfEdge.edgeListLeftNeighbor = lB;
nHalfEdge.edgeListRightNeighbor = lB.edgeListRightNeighbor;
if (lB.edgeListRightNeighbor != null)
lB.edgeListRightNeighbor.edgeListLeftNeighbor = nHalfEdge;
lB.edgeListRightNeighbor = nHalfEdge;
}
public void Remove(HalfEdge hE)
{
hE.edgeListLeftNeighbor.edgeListRightNeighbor = hE.edgeListRightNeighbor;
hE.edgeListRightNeighbor.edgeListLeftNeighbor = hE.edgeListLeftNeighbor;
hE.e = Edge.DELETED;
hE.edgeListLeftNeighbor = null;
hE.edgeListRightNeighbor = null;
}
public HalfEdge EdgeListLeftNeighbor(Point p)
{
int i;
int bucket;
HalfEdge hE;
bucket = (int)((p.x - xMin) / deltaX * hashSize);
if (bucket < 0)
bucket = 0;
if (bucket >= hashSize)
bucket = hashSize - 1;
hE = GetHash(bucket);
if (hE == null)
{
for (i = 1; true; ++i)
{
if ((hE = GetHash(bucket - i)) != null)
break;
if ((hE = GetHash(bucket + i)) != null)
break;
}
}
if ((hE == leftEnd) || ((hE != rightEnd) && hE.IsLeftOf(p)))
{
do
{
hE = hE.edgeListRightNeighbor;
} while (hE != rightEnd && hE.IsLeftOf(p));
hE = hE.edgeListLeftNeighbor;
}
else
{
do
{
hE = hE.edgeListLeftNeighbor;
} while (hE != leftEnd && !hE.IsLeftOf(p));
}
if (bucket > 0 && bucket < hashSize - 1)
hash[bucket] = hE;
return hE;
}
private HalfEdge GetHash(int b)
{
HalfEdge hE;
if ((b < 0) || (b >= hashSize))
return null;
hE = hash[b];
if (hE != null && hE.e == Edge.DELETED)
{
hash[b] = null;
return null;
}
else
{
return hE;
}
}
public void Dispose()
{
HalfEdge hE = leftEnd;
HalfEdge prevHE;
while (hE != rightEnd)
{
prevHE = hE;
hE = hE.edgeListRightNeighbor;
prevHE.Dispose();
}
leftEnd = null;
rightEnd.Dispose();
rightEnd = null;
hash.Clear();
hash = null;
}
}
}