-
Notifications
You must be signed in to change notification settings - Fork 3
/
PolyhedronR.cs
468 lines (466 loc) · 17.9 KB
/
PolyhedronR.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//todo: fix: There is currently a bug for the case of coplanar planes within one of the polyhedra.
namespace System.Numerics.Rational
{
/// <summary>
/// Intersections and boolean operations on arbitrary polyhedral meshes based on <see cref="BigRational"/>.<br/>
/// <i>This is just a non-optimal example implementation for testing!</i>
/// </summary>
public sealed class PolyhedronR
{
public static PolyhedronR GetInstance()
{
if (wr == null || wr.Target is not PolyhedronR p)
wr = new WeakReference(p = new PolyhedronR());
return p;
}
public void SetMesh(int i, object pp, object ii)
{
var xp = i == 0 ? app : bpp; xp.Clear(); map.Clear();
var xi = i == 0 ? aii : bii; xi.Clear();
if (pp is IList<System.Numerics.Vector3> l2)
{
xp.EnsureCapacity(l2.Count);
for (int k = 0, n = l2.Count; k < n; k++) xp.Add(l2[k]);
}
else if (pp is IList<Vector3R> l1) xp.AddRange(l1);
else throw new ArgumentException(nameof(pp));
if (ii is IList<ushort> u1)
{
xi.EnsureCapacity(u1.Count);
for (int k = 0, n = u1.Count; k < n; k++) xi.Add(u1[k]);
}
else if (ii is IList<int> u2) xi.AddRange(u2);
else throw new ArgumentException(nameof(ii));
}
public void Transform(int i, in Matrix4x3R m)
{
var xp = i == 0 ? app : bpp;
for (i = 0; i < xp.Count; i++) xp[i] = Vector3R.Transform(xp[i], m);
}
public enum Mode { Union, Difference, Intersection }
/// <summary>
/// Constructive Solid Geometry
/// </summary>
/// <remarks>
/// <b>Note</b>: There is currently a bug for the case of coplanar planes within one of the polyhedra.
/// </remarks>
/// <param name="mode">Boolean operation: Union, Difference or Intersection</param>
/// <param name="flags">
/// 1: Interpretation of coplanar planes as intersected.<br/>
/// 2: Generate face map.
/// </param>
public void Csg(Mode mode, int flags = 0)
{
var abox = getbox(app);
var bbox = getbox(bpp);
map.Clear(); if (bmap = (flags & 2) == 0) map.EnsureCapacity((aii.Count + bii.Count) >> 1);
if (mode == Mode.Difference)
{
//if (!touch(abox, bbox))
if (!((flags & 1) == 0 ? intersect(abox, bbox) : touch(abox, bbox)))
{
if (bmap) for (int i = 0; i < aii.Count; i += 3) map.Add(i << 1);
return;
}
}
else if (mode == Mode.Union)
{
//if (!touch(abox, bbox)) { } //todo: simple fast group but with map ...
}
else //Mode.Intersection
{
if (!touch(abox, bbox)) { app.Clear(); aii.Clear(); return; }
}
var aee = getplanes(app, aii);
var bee = getplanes(bpp, bii);
iii.Clear(); iii.EnsureCapacity(aii.Count + bii.Count);
ppp.Clear(); ppp.EnsureCapacity(app.Count + bpp.Count);
if (mode == Mode.Difference)
{
Parallel.For(0, aee.Length, i =>
{
var e = aee[i];
if (!touch(bbox, getbox(app, e.ii))) { fetch(0, e); return; }
var t = Tess.GetInstance();
var f = t.getff(bpp, e.plane, Math.Max(app.Count, bpp.Count));
int q = (f & 2) != 0 ? qplane(bee, e.plane) : -1;
int r = (f & 2) != 0 && (flags & 1) == 0 ? qplane(bee, -e.plane) : -1;
if (f < 5 && q == -1) { fetch(0, e); return; }
if (f >= 5) t.cutmesh(bpp, bii, e.plane);
if (q != -1) t.xor2(bee[q].ii);
if (r != -1) { t.xor2(bee[r].ii); bee[r] = default; }
if (t.empty) { fetch(0, e); return; }
t.tess.Winding = Winding.Positive;
t.tess.SetNormal(e.plane.Normal);
t.tess.BeginPolygon(); t.tessel(bpp); t.xor1(e.ii); t.tessel(app);
t.tess.EndPolygon(); fetch(0, e, t.tess);
});
Parallel.For(0, bee.Length, i =>
{
var e = bee[i]; if (e.ii == null) return;
if (!touch(abox, getbox(bpp, e.ii))) return;
var t = Tess.GetInstance();
var f = t.getff(app, e.plane, Math.Max(app.Count, bpp.Count)); if (f < 5) return;
t.cutmesh(app, aii, e.plane);
t.tess.Winding = Winding.AbsGeqTwo;
t.tess.SetNormal(e.plane.Normal); t.tess.Options ^= TesselatorR.Option.NormNeg;
t.tess.BeginPolygon(); t.tessel(app); t.xor2(e.ii); t.tessel(bpp);
t.tess.EndPolygon(); fetch(1, e, t.tess);
});
}
else if (mode == Mode.Union)
{
Parallel.For(0, aee.Length, i =>
{
var e = aee[i];
if (!touch(bbox, getbox(app, e.ii))) { fetch(0, e); return; }
var t = Tess.GetInstance();
var f = t.getff(bpp, e.plane, Math.Max(app.Count, bpp.Count));
int q = (f & 2) != 0 && (flags & 1) == 0 ? qplane(bee, e.plane) : -1;
if (f < 5 && q == -1) { fetch(0, e); return; }
if (f >= 5) t.cutmesh(bpp, bii, e.plane);
if (q != -1) { t.xor1(bee[q].ii); bee[q] = default; }
t.tess.Winding = Winding.Positive;
t.tess.SetNormal(e.plane.Normal);
t.tess.BeginPolygon(); t.tessel(bpp); t.xor1(e.ii); t.tessel(app);
t.tess.EndPolygon(); fetch(0, e, t.tess);
});
Parallel.For(0, bee.Length, i =>
{
var e = bee[i]; if (e.ii == null) return;
if (!touch(abox, getbox(bpp, e.ii))) { fetch(1, e); return; }
var t = Tess.GetInstance();
var f = t.getff(app, e.plane, Math.Max(app.Count, bpp.Count));
int q = (f & 2) != 0 ? qplane(aee, e.plane) : -1; //if (q != -1 && (flags & 1) != 0) continue;
if (f < 5 && q == -1) { fetch(1, e); return; }
if (f >= 5) t.cutmesh(app, aii, e.plane);
if (q != -1) t.xor2(aee[q].ii); //todo: check this case!!!
t.tess.Winding = Winding.Positive;
t.tess.SetNormal(e.plane.Normal);
t.tess.BeginPolygon(); t.tessel(app); t.xor1(e.ii); t.tessel(bpp);
t.tess.EndPolygon(); fetch(1, e, t.tess);
});
}
else //Mode.Intersection
{
Parallel.For(0, aee.Length, i =>
{
var e = aee[i]; if (!touch(bbox, getbox(app, e.ii))) return;
var t = Tess.GetInstance();
var f = t.getff(bpp, e.plane, Math.Max(app.Count, bpp.Count));
int q = (f & 2) != 0 ? qplane(bee, e.plane) : -1;
if (f < 5 && q == -1) return;
if (f >= 5) t.cutmesh(bpp, bii, e.plane);
if (q != -1) t.xor2(bee[q].ii);
t.tess.Winding = Winding.AbsGeqTwo;
t.tess.SetNormal(e.plane.Normal);
t.tess.BeginPolygon(); t.tessel(bpp); t.xor2(e.ii); t.tessel(app);
t.tess.EndPolygon(); fetch(0, e, t.tess);
});
Parallel.For(0, bee.Length, i =>
{
var e = bee[i]; if (!touch(abox, getbox(bpp, e.ii))) return;
var t = Tess.GetInstance();
var f = t.getff(app, e.plane, Math.Max(app.Count, bpp.Count)); if (f < 5) return;
t.cutmesh(app, aii, e.plane);
t.tess.Winding = Winding.AbsGeqTwo;
t.tess.SetNormal(e.plane.Normal);
t.tess.BeginPolygon(); t.tessel(app); t.xor2(e.ii); t.tessel(bpp);
t.tess.EndPolygon(); fetch(1, e, t.tess);
});
}
app.Clear(); app.AddRange(ppp.Keys);
aii.Clear(); aii.AddRange(iii);
Tess.GetInstance().join(app, aii, bmap ? map : null);
}
public bool Cut(PlaneR plane, int flags = 0)
{
var ff = getff(app.Count); var fa = Tess.getff(ff, app, plane);
if ((fa & 4) == 0) { return false; }
if ((fa & 1) == 0) { app.Clear(); aii.Clear(); return true; }
iii.Clear(); iii.EnsureCapacity(aii.Count);
ppp.Clear(); ppp.EnsureCapacity(app.Count);
map.Clear(); if (bmap = (flags & 2) == 0) map.EnsureCapacity(aii.Count >> 1);
var ee = getplanes(app, aii);
Parallel.For(0, ee.Length, i =>
{
var e = ee[i]; int f = 0, cii = e.ii.Length; Tess? t = null;
for (int k = 0; k < cii; k++) f |= ff[e.ii[k]];
if (f == 2) return; //todo: check, on plain?
if ((f & 4) == 0) { fetch(0, e); return; }
if ((f & 1) == 0) return;
t = Tess.GetInstance();
t.cutface(app, ff, e.ii, plane); //if(t.empty) { }
t.tess.Winding = Winding.NonZero;
t.tess.SetNormal(e.plane.Normal);
t.tess.BeginPolygon(); t.tessel(app);
t.tess.EndPolygon(); fetch(0, e, t.tess);
});
//if (true)
{
var t = Tess.GetInstance();
t.getff(app, plane = -plane, app.Count);
t.cutmesh(app, aii, plane);
t.tess.Winding = Winding.NonZero;
t.tess.SetNormal(plane.Normal);
t.tess.BeginPolygon(); t.tessel(app);
t.tess.EndPolygon(); var n = iii.Count; add(t.tess);
if (bmap) map.AddRange(Enumerable.Repeat(1, (iii.Count - n) / 3));
}
app.Clear(); app.AddRange(ppp.Keys);
aii.Clear(); aii.AddRange(iii);
return true;
}
public List<Vector3R> Vertices => app;
public List<int> Indices => aii;
public List<int> Mapping => map;
[ThreadStatic] static WeakReference? wr; PolyhedronR() { }
readonly List<Vector3R> app = new(), bpp = new();
readonly List<int> aii = new(), bii = new(), map = new(), iii = new();
readonly Dictionary<Vector3R, int> ppp = new();
readonly Dictionary<int, int> abs = new();
int[]? ff; PlaneR[]? ee; bool bmap;
int[] getff(int n)
{
return ff != null && ff.Length >= n ? ff : ff = new int[n];
}
void add(TesselatorR tess)
{
var ii = tess.Indices; var pp = tess.Vertices;
for (int t = 0, n = ii.Length; t < n; t++) iii.Add(add(pp[ii[t]]));
}
void add(List<Vector3R> pp, IList<int> ii)
{
for (int t = 0, n = ii.Count; t < n; t++) iii.Add(add(pp[ii[t]]));
}
int add(in Vector3R p)
{
if (!ppp.TryGetValue(p, out var x)) ppp.Add(p, x = ppp.Count);
return x;
}
void fetch(int ab, in (PlaneR plane, int[] ii, int t) e, TesselatorR? te = null)
{
lock (ppp)
{
var n = iii.Count;
if (te != null) add(te); else add(ab == 0 ? app : bpp, e.ii);
if (bmap) map.AddRange(Enumerable.Repeat(((e.t * 3) << 1) | ab, (iii.Count - n) / 3));
}
}
sealed class Tess
{
internal static Tess GetInstance()
{
if (wr == null || wr.Target is not Tess p)
wr = new WeakReference(p = new Tess());
return p;
}
[ThreadStatic] static WeakReference? wr;
internal readonly TesselatorR tess = TesselatorR.GetInstance();
int[] ff = Array.Empty<int>();
readonly Dictionary<int, int> abs = new();
readonly Dictionary<(int a, int b), int> abi = new();
readonly List<Vector3R> pps = new();
internal bool empty => abs.Count == 0;
internal int getff(IReadOnlyList<Vector3R> pp, in PlaneR plane, int nx)
{
if (ff.Length < nx) ff = new int[nx];
return getff(ff, pp, plane);
}
internal static int getff(int[] ff, IReadOnlyList<Vector3R> pp, in PlaneR plane)
{
int np = pp.Count, fa = 0;
for (int i = 0; i < np; i++)
fa |= ff[i] = 1 << (1 - PlaneR.DotCoordSign(plane, pp[i]));
return fa;
}
internal unsafe void cutface(List<Vector3R> pp, int[] ff, int[] ii, in PlaneR plane)
{
abi.Clear(); pps.Clear(); decimal m; var ss = (int*)&m;
for (int t = 0; t < ii.Length; t += 3)
{
int ns = 0;
for (int j = 0, k = 2; j < 3; k = j++)
{
int i1, i2, f1 = ff[i1 = ii[t + k]], f2 = ff[i2 = ii[t + j]];
if (f1 != 4) ss[ns++] = i1;
if (f1 == f2 || ((f1 | f2) & 2) != 0) continue;
var ab = (Math.Min(i1, i2), Math.Max(i1, i2));
if (abi.TryGetValue(ab, out var x)) { ss[ns++] = x; continue; }
abi.Add(ab, ss[ns++] = pp.Count + pps.Count);
pps.Add(PlaneR.Intersect(plane, pp[i1], pp[i2]));
}
if (ns == 0) continue; Debug.Assert(ns == 3 || ns == 4);
for (int j = ns - 1, k = 0; k < ns; j = k++) xor(ss[j], ss[k]);
}
}
internal void cutmesh(IReadOnlyList<Vector3R> pp, IReadOnlyList<int> ii, in PlaneR plane)
{
abi.Clear(); pps.Clear();
for (int t = 0; t < ii.Count; t += 3)
{
var fa = ff[ii[t]] | ff[ii[t + 1]] | ff[ii[t + 2]];
if (fa < 5) continue;
int x1 = -1, x2 = -1;
for (int j = 0, k = 2; j < 3; k = j++)
{
int i1, i2, f1 = ff[i1 = ii[t + k]], f2 = ff[i2 = ii[t + j]];
if (f1 == 2)
{
if (f2 == 2) { f2 = ff[ii[t + (j + 1) % 3]]; f2 = f2 == 1 ? 4 : 1; }
if (f2 == 1) x2 = i1; else x1 = i1;
continue;
}
if ((f1 | f2) != 5) continue;
var ab = (Math.Min(i1, i2), Math.Max(i1, i2));
if (!abi.TryGetValue(ab, out var x))
{
abi.Add(ab, x = pp.Count + pps.Count);
pps.Add(PlaneR.Intersect(plane, pp[i1], pp[i2]));
}
if (f1 == 1) x1 = x; else x2 = x;
}
if (x1 == -1 || x2 == -1) continue;
add(x1, x2);
}
}
internal void tessel(IReadOnlyList<Vector3R> pp)
{
for (var np = pp.Count; abs.Count != 0;)
{
var e = abs.GetEnumerator(); e.MoveNext();
var t = e.Current;
var s = t.Key & 0x00ffffff;
var a = t.Value; rem(s, a);
tess.BeginContour();
tess.AddVertex(s < np ? pp[s] : pps[s - np]);
for (; ; )
{
tess.AddVertex(a < np ? pp[a] : pps[a - np]);
if (!rem(a, out var b)) { abs.Clear(); throw new Exception(err); }
if (b == s) break; a = b;
}
tess.EndContour();
}
}
internal void xor1(int[] ii)
{
for (int t = 0; t < ii.Length; t++) xor(ii[t], ii[mod(t)]);
}
internal void xor2(int[] ii)
{
for (int t = 0; t < ii.Length; t++) xor(ii[mod(t)], ii[t]);
}
void xor(int a, int b)
{
if (!rem(b, a)) add(a, b);
}
void add(int a, int b)
{
while (abs.ContainsKey(a)) a += 0x01000000;
abs.Add(a, b); Debug.Assert(a >= 0);
}
bool rem(int a, int b)
{
for (int t; abs.TryGetValue(a, out var x); a += 0x01000000)
{
if (x != b) continue; abs.Remove(a);
for (; abs.TryGetValue(t = a + 0x01000000, out x); abs.Remove(t), abs.Add(a, x), a = t) ;
return true;
}
return false;
}
bool rem(int a, out int b)
{
if (!abs.TryGetValue(a, out b)) return false; abs.Remove(a);
for (int t; abs.TryGetValue(t = a + 0x01000000, out var x); abs.Remove(t), abs.Add(a, x), a = t) ;
return true;
}
static int mod(int i) => i % 3 != 2 ? i + 1 : i - 2;
internal void join(List<Vector3R> pp, List<int> ii, List<int>? map)
{
Debug.Assert(abs.Count == 0);
for (int i = 0; i < ii.Count; i++) xor(ii[i], ii[mod(i)]);
if (abs.Count == 0) return;
for (var e = abs.GetEnumerator(); e.MoveNext();)
{
if (!abs.TryGetValue(e.Current.Value, out var c)) continue;
int a = e.Current.Key & 0x00ffffff, b = e.Current.Value;
if (Vector3R.PtInline(pp[a], pp[b], pp[c]) != 3) continue;
xor(b, a); xor(a, c); xor(c, b);
int u = 0, v; for (; ii[u] != a || ii[v = mod(u)] != b; u++) ;
ii[v] = c; ii.Add(ii[mod(v)]); ii.Add(c); ii.Add(b);
if (map != null) map.Add(map[u / 3]);
if (abs.Count == 0) return; e = abs.GetEnumerator();
}
abs.Clear(); throw new Exception(err); //see: There is currently a bug for the case of coplanar planes within one of the polyhedra.
}
}
const string err = "Invalid input polyhedron";
(PlaneR plane, int[] ii, int t)[] getplanes(List<Vector3R> pp, List<int> ii)
{
var nk = ii.Count / 3;
if (ee == null || ee.Length < nk) ee = new PlaneR[Math.Max(aii.Count, bii.Count) >> 1];
Parallel.For(0, nk, k => { int t; ee[k] = PlaneR.FromVertices(pp[ii[t = k * 3]], pp[ii[t + 1]], pp[ii[t + 2]]); });
return Enumerable.Range(0, nk).GroupBy(i => ee[i]).Select(p =>
{
var a = (IList<int>)p; var b = new int[a.Count * 3];
for (int i = 0, j = 0, k; i < b.Length; i += 3)
{
b[i] = ii[k = a[j++] * 3];
b[i + 1] = ii[k + 1];
b[i + 2] = ii[k + 2];
}
return (p.Key, b, a[0]);
}).ToArray();
}
static (Vector3R min, Vector3R max) getbox(List<Vector3R> pp)
{
var box = (min: pp[0], max: pp[0]);
for (int n = pp.Count, i = 1; i < n; i++)
{
var p = pp[i];
box.min = Vector3R.Min(box.min, p);
box.max = Vector3R.Max(box.max, p);
}
return box;
}
static (Vector3R min, Vector3R max) getbox(List<Vector3R> pp, IList<int> ii)
{
var p = pp[ii[0]]; var box = (min: p, max: p);
for (int i = 1; i < ii.Count; i++) //todo: distinct
{
p = pp[ii[i]];
box.min = Vector3R.Min(box.min, p);
box.max = Vector3R.Max(box.max, p);
}
return box;
}
static bool intersect(in (Vector3R min, Vector3R max) a, in (Vector3R min, Vector3R max) b)
{
if (a.max.X <= b.min.X) return false;
if (a.max.Y <= b.min.Y) return false;
if (a.max.Z <= b.min.Z) return false;
if (a.min.X >= b.max.X) return false;
if (a.min.Y >= b.max.Y) return false;
if (a.min.Z >= b.max.Z) return false;
return true;
}
static bool touch(in (Vector3R min, Vector3R max) a, in (Vector3R min, Vector3R max) b)
{
if (a.max.X < b.min.X) return false;
if (a.max.Y < b.min.Y) return false;
if (a.max.Z < b.min.Z) return false;
if (a.min.X > b.max.X) return false;
if (a.min.Y > b.max.Y) return false;
if (a.min.Z > b.max.Z) return false;
return true;
}
static int qplane((PlaneR plane, int[] ii, int t)[] bee, in PlaneR plane)
{
for (int t = 0; t < bee.Length; t++) if (plane.Equals(bee[t].plane)) return t;
return -1;
}
}
}