From eb31201fb3660c3bdc42a5a494a5cb9a17eff87c Mon Sep 17 00:00:00 2001 From: Matt Schoen Date: Thu, 8 Jun 2017 13:16:13 -0700 Subject: [PATCH 1/3] Use internal modifier for HasAnyObjects --- Scripts/BoundsOctreeNode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Scripts/BoundsOctreeNode.cs b/Scripts/BoundsOctreeNode.cs index 1ffd68d..e840a31 100644 --- a/Scripts/BoundsOctreeNode.cs +++ b/Scripts/BoundsOctreeNode.cs @@ -524,7 +524,7 @@ bool ShouldMerge() { /// Checks if this node or anything below it has something in it. /// /// True if this node or any of its children, grandchildren etc have something in them - public bool HasAnyObjects() { + internal bool HasAnyObjects() { if (objects.Count > 0) return true; if (children != null) { From c1ac913c0583e121e1bef2d26878286861d1caff Mon Sep 17 00:00:00 2001 From: Matt Schoen Date: Tue, 26 Jun 2018 18:09:27 -0700 Subject: [PATCH 2/3] Use a struct and RemoveAt for OctreeObject --- Scripts/BoundsOctreeNode.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Scripts/BoundsOctreeNode.cs b/Scripts/BoundsOctreeNode.cs index e840a31..69a411c 100644 --- a/Scripts/BoundsOctreeNode.cs +++ b/Scripts/BoundsOctreeNode.cs @@ -28,7 +28,7 @@ public class BoundsOctreeNode { const int numObjectsAllowed = 8; // An object in the octree - class OctreeObject { + struct OctreeObject { public T Obj; public Bounds Bounds; } @@ -69,8 +69,10 @@ public bool Remove(T obj) { bool removed = false; for (int i = 0; i < objects.Count; i++) { - if (objects[i].Obj.Equals(obj)) { - removed = objects.Remove(objects[i]); + if (objects[i].Obj.Equals(obj)) + { + removed = true; + objects.RemoveAt(i); break; } } @@ -535,4 +537,4 @@ internal bool HasAnyObjects() { return false; } -} \ No newline at end of file +} From ddc4b113437c7994e0086bd56320f872debac1c0 Mon Sep 17 00:00:00 2001 From: Matt Schoen Date: Tue, 19 Feb 2019 16:59:32 -0800 Subject: [PATCH 3/3] Add an Equals and GetHashCode override for BoundsOctreeNode --- Scripts/BoundsOctreeNode.cs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Scripts/BoundsOctreeNode.cs b/Scripts/BoundsOctreeNode.cs index 69a411c..bb94408 100644 --- a/Scripts/BoundsOctreeNode.cs +++ b/Scripts/BoundsOctreeNode.cs @@ -31,6 +31,34 @@ public class BoundsOctreeNode { struct OctreeObject { public T Obj; public Bounds Bounds; + + public override int GetHashCode() + { + var hashCode = Bounds.GetHashCode(); + + if (Obj != null) + hashCode ^= Obj.GetHashCode(); + + return hashCode; + } + + public override bool Equals(object obj) + { + if (obj == null) + return false; + + if (!(obj is OctreeObject)) + return false; + + var other = (OctreeObject)obj; + if (Bounds != other.Bounds) + return false; + + if (Obj == null && other.Obj == null) + return true; + + return Obj.Equals(other.Obj); + } } /// @@ -365,7 +393,7 @@ public int GetTotalObjects(int startingNum = 0) { // #### PRIVATE METHODS #### /// - /// Set values for this node. + /// Set values for this node. /// /// Length of this node, not taking looseness into account. /// Minimum size of nodes in this octree. @@ -429,7 +457,7 @@ void SubAdd(T obj, Bounds objBounds) { bestFitChild = BestFitChild(existingObj.Bounds); // Does it fit? if (Encapsulates(children[bestFitChild].bounds, existingObj.Bounds)) { - children[bestFitChild].SubAdd(existingObj.Obj, existingObj.Bounds); // Go a level deeper + children[bestFitChild].SubAdd(existingObj.Obj, existingObj.Bounds); // Go a level deeper objects.Remove(existingObj); // Remove from here } }