-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTrianglePool.cs
305 lines (236 loc) · 7.32 KB
/
TrianglePool.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// -----------------------------------------------------------------------
// <copyright file="TrianglePool.cs" company="">
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
// </copyright>
// -----------------------------------------------------------------------
namespace TriangleNet
{
using System;
using System.Collections.Generic;
using TriangleNet.Geometry;
using TriangleNet.Topology;
public class TrianglePool : ICollection<Triangle>
{
// Determines the size of each block in the pool.
private const int BLOCKSIZE = 1024;
// The total number of currently allocated triangles.
int size;
// The number of triangles currently used.
int count;
// The pool.
Triangle[][] pool;
// A stack of free triangles.
Stack<Triangle> stack;
public TrianglePool()
{
size = 0;
// On startup, the pool should be able to hold 2^16 triangles.
int n = Math.Max(1, 65536 / BLOCKSIZE);
pool = new Triangle[n][];
pool[0] = new Triangle[BLOCKSIZE];
stack = new Stack<Triangle>(BLOCKSIZE);
}
/// <summary>
/// Gets a triangle from the pool.
/// </summary>
/// <returns></returns>
public Triangle Get()
{
Triangle triangle;
if (stack.Count > 0)
{
triangle = stack.Pop();
triangle.hash = -triangle.hash - 1;
Cleanup(triangle);
}
else if (count < size)
{
triangle = pool[count / BLOCKSIZE][count % BLOCKSIZE];
triangle.id = triangle.hash;
Cleanup(triangle);
count++;
}
else
{
triangle = new Triangle();
triangle.hash = size;
triangle.id = triangle.hash;
int block = size / BLOCKSIZE;
if (pool[block] == null)
{
pool[block] = new Triangle[BLOCKSIZE];
// Check if the pool has to be resized.
if (block + 1 == pool.Length)
{
Array.Resize(ref pool, 2 * pool.Length);
}
}
// Add triangle to pool.
pool[block][size % BLOCKSIZE] = triangle;
count = ++size;
}
return triangle;
}
public void Release(Triangle triangle)
{
stack.Push(triangle);
// Mark the triangle as free (used by enumerator).
triangle.hash = -triangle.hash - 1;
}
/// <summary>
/// Restart the triangle pool.
/// </summary>
public TrianglePool Restart()
{
foreach (var triangle in stack)
{
// Reset hash to original value.
triangle.hash = -triangle.hash - 1;
}
stack.Clear();
count = 0;
return this;
}
/// <summary>
/// Samples a number of triangles from the pool.
/// </summary>
/// <param name="k">The number of triangles to sample.</param>
/// <param name="random"></param>
/// <returns></returns>
internal IEnumerable<Triangle> Sample(int k, Random random)
{
int i, count = this.Count;
if (k > count)
{
// TODO: handle Sample special case.
k = count;
}
Triangle t;
// TODO: improve sampling code (to ensure no duplicates).
while (k > 0)
{
i = random.Next(0, count);
t = pool[i / BLOCKSIZE][i % BLOCKSIZE];
if (t.hash >= 0)
{
k--;
yield return t;
}
}
}
private void Cleanup(Triangle triangle)
{
triangle.label = 0;
triangle.area = 0.0;
triangle.infected = false;
for (int i = 0; i < 3; i++)
{
triangle.vertices[i] = null;
triangle.subsegs[i] = default(Osub);
triangle.neighbors[i] = default(Otri);
}
}
public void Add(Triangle item)
{
throw new NotImplementedException();
}
public void Clear()
{
stack.Clear();
int blocks = (size / BLOCKSIZE) + 1;
for (int i = 0; i < blocks; i++)
{
var block = pool[i];
// Number of triangles in current block:
int length = (size - i * BLOCKSIZE) % BLOCKSIZE;
for (int j = 0; j < length; j++)
{
block[j] = null;
}
}
size = count = 0;
}
public bool Contains(Triangle item)
{
int i = item.hash;
if (i < 0 || i > size)
{
return false;
}
return pool[i / BLOCKSIZE][i % BLOCKSIZE].hash >= 0;
}
public void CopyTo(Triangle[] array, int index)
{
var enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
array[index] = enumerator.Current;
index++;
}
}
public int Count
{
get { return count - stack.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(Triangle item)
{
throw new NotImplementedException();
}
public IEnumerator<Triangle> GetEnumerator()
{
return new Enumerator(this);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class Enumerator : IEnumerator<Triangle>
{
// TODO: enumerator should be able to tell if collection changed.
int count;
Triangle[][] pool;
Triangle current;
int index, offset;
public Enumerator(TrianglePool pool)
{
this.count = pool.Count;
this.pool = pool.pool;
index = 0;
offset = 0;
}
public Triangle Current
{
get { return current; }
}
public void Dispose()
{
}
object System.Collections.IEnumerator.Current
{
get { return current; }
}
public bool MoveNext()
{
while (index < count)
{
current = pool[offset / BLOCKSIZE][offset % BLOCKSIZE];
offset++;
if (current.hash >= 0)
{
index++;
return true;
}
}
return false;
}
public void Reset()
{
index = offset = 0;
}
}
}
}