From f2b6c07e2d91860560fd35bd055b50239fd30b04 Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Mon, 30 Aug 2021 12:03:42 +0800 Subject: [PATCH] Release references to solids once merged. --- Csg/Solid.cs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Csg/Solid.cs b/Csg/Solid.cs index 3125a4f..3d30297 100644 --- a/Csg/Solid.cs +++ b/Csg/Solid.cs @@ -36,16 +36,22 @@ public static Solid FromPolygons(List polygons) public Solid Union(params Solid[] others) { - var csgs = new List(); - csgs.Add(this); - csgs.AddRange(others); - var i = 1; - for (; i < csgs.Count; i += 2) + if (others.Length == 0) { + return this; + } + + var csgs = new Queue(others); + csgs.Enqueue (this); + while (csgs.Count > 1) { - var n = csgs[i - 1].UnionSub(csgs[i], false, false); - csgs.Add(n); + var a = csgs.Dequeue(); + var b = csgs.Dequeue(); + + var n = a.UnionSub(b, false, false); + csgs.Enqueue(n); } - return csgs[i - 1].Retesselated().Canonicalized(); + + return csgs.Dequeue().Retesselated().Canonicalized(); } Solid UnionSub(Solid csg, bool retesselate, bool canonicalize)