void Leagalize() { while (stack.Count > 0) { var t = stack.Pop(); if (FindNodes(t, out DN n1, out DN n2)) { var p1 = n1.tetrahedra.RemainingPoint(t); var p2 = n2.tetrahedra.RemainingPoint(t); if (n1.tetrahedra.GetCircumscribedSphere().Contains(p2)) { if (t.Intersects(new SG(p1, p2), out d3 i, out bool onedge)) { var o = DN.Flip23(n1, n2, p1, p2, t); stack.Push(o.t1); stack.Push(o.t2); stack.Push(o.t3); stack.Push(o.t4); stack.Push(o.t5); stack.Push(o.t6); nodes.Remove(n1); nodes.Remove(n2); nodes.Add(o.n1); nodes.Add(o.n2); nodes.Add(o.n3); } else if (onedge) { Debug.LogWarning("point is on edge"); } else { d3 far; if (Utils.IsIntersecting(new SG(i, t.a), new SG(t.b, t.c), 1e-15d)) { far = t.a; } else if (Utils.IsIntersecting(new SG(i, t.b), new SG(t.c, t.a), 1e-15d)) { far = t.b; } else if (Utils.IsIntersecting(new SG(i, t.c), new SG(t.a, t.b), 1e-15d)) { far = t.c; } else { throw new System.Exception(); } var cm = t.Remaining(far); var t3 = new TR(cm, p1); var n3 = n1.GetFacingNode(t3); if (!Equals(n3.tetrahedra.RemainingPoint(t3), p2)) { continue; } var p12 = far; var p23 = p2; var p31 = p1; var o = DN.Flip32(n1, n2, n3, p31, p12, p23, cm.a, cm.b); stack.Push(o.t1); stack.Push(o.t2); stack.Push(o.t3); stack.Push(o.t4); stack.Push(o.t5); stack.Push(o.t6); nodes.Remove(n1); nodes.Remove(n2); nodes.Remove(n3); nodes.Add(o.n1); nodes.Add(o.n2); } } } }