123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827 |
- /*
- Copyright (c) 2020 Omar Duarte
- Unauthorized copying of this file, via any medium is strictly prohibited.
- Modified by Omar Duarte, 2020.
- This file incorporates work covered by the following copyright and
- permission notice:
- Copyright (c) 2014, Nition, BSD licence. All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- using System.Linq;
- using UnityEngine;
- namespace PluginMaster
- {
- // A node in a PointOctree
- public class PointOctreeNode<T>
- {
- // Centre of this node
- public Vector3 Center { get; private set; }
- // Length of the sides of this node
- public float SideLength { get; private set; }
- // Minimum size for a node in this octree
- float minSize;
- // Bounding box that represents this node
- Bounds bounds = default(Bounds);
- // Objects in this node
- readonly System.Collections.Generic.List<OctreeObject> objects = new System.Collections.Generic.List<OctreeObject>();
- // Child nodes, if any
- PointOctreeNode<T>[] children = null;
- bool HasChildren { get { return children != null; } }
- // bounds of potential children to this node. These are actual size (with looseness taken into account), not base size
- Bounds[] childBounds;
- // If there are already NUM_OBJECTS_ALLOWED in a node, we split it into children
- // A generally good number seems to be something around 8-15
- const int NUM_OBJECTS_ALLOWED = 8;
- // For reverting the bounds size after temporary changes
- Vector3 actualBoundsSize;
- // An object in the octree
- class OctreeObject
- {
- public T Obj;
- public Vector3 Pos;
- }
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="baseLengthVal">Length of this node, not taking looseness into account.</param>
- /// <param name="minSizeVal">Minimum size of nodes in this octree.</param>
- /// <param name="centerVal">Centre position of this node.</param>
- public PointOctreeNode(float baseLengthVal, float minSizeVal, Vector3 centerVal)
- {
- SetValues(baseLengthVal, minSizeVal, centerVal);
- }
- // #### PUBLIC METHODS ####
- /// <summary>
- /// Add an object.
- /// </summary>
- /// <param name="obj">Object to add.</param>
- /// <param name="objPos">Position of the object.</param>
- /// <returns></returns>
- public bool Add(T obj, Vector3 objPos)
- {
- if (!Encapsulates(bounds, objPos))
- {
- return false;
- }
- SubAdd(obj, objPos);
- return true;
- }
- /// <summary>
- /// Remove an object. Makes the assumption that the object only exists once in the tree.
- /// </summary>
- /// <param name="obj">Object to remove.</param>
- /// <returns>True if the object was removed successfully.</returns>
- 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]);
- break;
- }
- }
- if (!removed && children != null)
- {
- for (int i = 0; i < 8; i++)
- {
- removed = children[i].Remove(obj);
- if (removed) break;
- }
- }
- if (removed && children != null)
- {
- // Check if we should merge nodes now that we've removed an item
- if (ShouldMerge())
- {
- Merge();
- }
- }
- return removed;
- }
- /// <summary>
- /// Removes the specified object at the given position.
- /// Makes the assumption that the object only exists once in the tree.
- /// </summary>
- /// <param name="obj">Object to remove.</param>
- /// <param name="objPos">Position of the object.</param>
- /// <returns>True if the object was removed successfully.</returns>
- public bool Remove(T obj, Vector3 objPos)
- {
- if (!Encapsulates(bounds, objPos))
- {
- return false;
- }
- return SubRemove(obj, objPos);
- }
- /// <summary>
- /// Return objects that are within maxDistance of the specified ray.
- /// </summary>
- /// <param name="ray">The ray.</param>
- /// <param name="maxDistance">Maximum distance from the ray to consider.</param>
- /// <param name="result">List result.</param>
- /// <returns>Objects within range.</returns>
- public void GetNearby(ref Ray ray, float maxDistance, System.Collections.Generic.List<T> result)
- {
- // Does the ray hit this node at all?
- // Note: Expanding the bounds is not exactly the same as a real distance check, but it's fast.
- bounds.Expand(new Vector3(maxDistance * 2, maxDistance * 2, maxDistance * 2));
- bool intersected = bounds.IntersectRay(ray);
- bounds.size = actualBoundsSize;
- if (!intersected)
- {
- return;
- }
- // Check against any objects in this node
- for (int i = 0; i < objects.Count; i++)
- {
- if (objects[i].Obj != null && SqrDistanceToRay(ray, objects[i].Pos) <= (maxDistance * maxDistance))
- {
- result.Add(objects[i].Obj);
- }
- }
- // Check children
- if (children != null)
- {
- for (int i = 0; i < 8; i++)
- {
- children[i].GetNearby(ref ray, maxDistance, result);
- }
- }
- }
- /// <summary>
- /// Return objects that are within <paramref name="maxDistance"/> of the specified position.
- /// </summary>
- /// <param name="position">The position.</param>
- /// <param name="maxDistance">Maximum distance from the position to consider.</param>
- /// <param name="result">List result.</param>
- /// <returns>Objects within range.</returns>
- public void GetNearby(ref Vector3 position, float maxDistance, System.Collections.Generic.List<T> result)
- {
- float sqrMaxDistance = maxDistance * maxDistance;
- // Does the node intersect with the sphere of center = position and radius = maxDistance?
- if ((bounds.ClosestPoint(position) - position).sqrMagnitude > sqrMaxDistance)
- {
- return;
- }
- // Check against any objects in this node
- for (int i = 0; i < objects.Count; i++)
- {
- if ((position - objects[i].Pos).sqrMagnitude <= sqrMaxDistance)
- {
- result.Add(objects[i].Obj);
- }
- }
- // Check children
- if (children != null)
- {
- for (int i = 0; i < 8; i++)
- {
- children[i].GetNearby(ref position, maxDistance, result);
- }
- }
- }
- /// <summary>
- /// Return all objects in the tree.
- /// </summary>
- /// <returns>All objects.</returns>
- public void GetAll(System.Collections.Generic.List<T> result)
- {
- // add directly contained objects
- result.AddRange(objects.Select(o => o.Obj));
- // add children objects
- if (children != null)
- {
- for (int i = 0; i < 8; i++)
- {
- children[i].GetAll(result);
- }
- }
- }
- /// <summary>
- /// Set the 8 children of this octree.
- /// </summary>
- /// <param name="childOctrees">The 8 new child nodes.</param>
- public void SetChildren(PointOctreeNode<T>[] childOctrees)
- {
- if (childOctrees.Length != 8)
- {
- Debug.LogError("Child octree array must be length 8. Was length: " + childOctrees.Length);
- return;
- }
- children = childOctrees;
- }
- /// <summary>
- /// We can shrink the octree if:
- /// - This node is >= double minLength in length
- /// - All objects in the root node are within one octant
- /// - This node doesn't have children, or does but 7/8 children are empty
- /// We can also shrink it if there are no objects left at all!
- /// </summary>
- /// <param name="minLength">Minimum dimensions of a node in this octree.</param>
- /// <returns>The new root, or the existing one if we didn't shrink.</returns>
- public PointOctreeNode<T> ShrinkIfPossible(float minLength)
- {
- if (SideLength < (2 * minLength))
- {
- return this;
- }
- if (objects.Count == 0 && (children == null || children.Length == 0))
- {
- return this;
- }
- // Check objects in root
- int bestFit = -1;
- for (int i = 0; i < objects.Count; i++)
- {
- OctreeObject curObj = objects[i];
- int newBestFit = BestFitChild(curObj.Pos);
- if (i == 0 || newBestFit == bestFit)
- {
- if (bestFit < 0)
- {
- bestFit = newBestFit;
- }
- }
- else
- {
- return this; // Can't reduce - objects fit in different octants
- }
- }
- // Check objects in children if there are any
- if (children != null)
- {
- bool childHadContent = false;
- for (int i = 0; i < children.Length; i++)
- {
- if (children[i].HasAnyObjects())
- {
- if (childHadContent)
- {
- return this; // Can't shrink - another child had content already
- }
- if (bestFit >= 0 && bestFit != i)
- {
- return this; // Can't reduce - objects in root are in a different octant to objects in child
- }
- childHadContent = true;
- bestFit = i;
- }
- }
- }
- // Can reduce
- if (children == null)
- {
- // We don't have any children, so just shrink this node to the new size
- // We already know that everything will still fit in it
- SetValues(SideLength / 2, minSize, childBounds[bestFit].center);
- return this;
- }
- // We have children. Use the appropriate child as the new root node
- return children[bestFit];
- }
- /// <summary>
- /// Find which child node this object would be most likely to fit in.
- /// </summary>
- /// <param name="objPos">The object's position.</param>
- /// <returns>One of the eight child octants.</returns>
- public int BestFitChild(Vector3 objPos)
- {
- return (objPos.x <= Center.x ? 0 : 1) + (objPos.y >= Center.y ? 0 : 4) + (objPos.z <= Center.z ? 0 : 2);
- }
- /// <summary>
- /// Checks if this node or anything below it has something in it.
- /// </summary>
- /// <returns>True if this node or any of its children, grandchildren etc have something in them</returns>
- public bool HasAnyObjects()
- {
- if (objects.Count > 0) return true;
- if (children != null)
- {
- for (int i = 0; i < 8; i++)
- {
- if (children[i].HasAnyObjects()) return true;
- }
- }
- return false;
- }
- // #### PRIVATE METHODS ####
- /// <summary>
- /// Set values for this node.
- /// </summary>
- /// <param name="baseLengthVal">Length of this node, not taking looseness into account.</param>
- /// <param name="minSizeVal">Minimum size of nodes in this octree.</param>
- /// <param name="centerVal">Centre position of this node.</param>
- void SetValues(float baseLengthVal, float minSizeVal, Vector3 centerVal)
- {
- SideLength = baseLengthVal;
- minSize = minSizeVal;
- Center = centerVal;
- // Create the bounding box.
- actualBoundsSize = new Vector3(SideLength, SideLength, SideLength);
- bounds = new Bounds(Center, actualBoundsSize);
- float quarter = SideLength / 4f;
- float childActualLength = SideLength / 2;
- Vector3 childActualSize = new Vector3(childActualLength, childActualLength, childActualLength);
- childBounds = new Bounds[8];
- childBounds[0] = new Bounds(Center + new Vector3(-quarter, quarter, -quarter), childActualSize);
- childBounds[1] = new Bounds(Center + new Vector3(quarter, quarter, -quarter), childActualSize);
- childBounds[2] = new Bounds(Center + new Vector3(-quarter, quarter, quarter), childActualSize);
- childBounds[3] = new Bounds(Center + new Vector3(quarter, quarter, quarter), childActualSize);
- childBounds[4] = new Bounds(Center + new Vector3(-quarter, -quarter, -quarter), childActualSize);
- childBounds[5] = new Bounds(Center + new Vector3(quarter, -quarter, -quarter), childActualSize);
- childBounds[6] = new Bounds(Center + new Vector3(-quarter, -quarter, quarter), childActualSize);
- childBounds[7] = new Bounds(Center + new Vector3(quarter, -quarter, quarter), childActualSize);
- }
- /// <summary>
- /// Private counterpart to the public Add method.
- /// </summary>
- /// <param name="obj">Object to add.</param>
- /// <param name="objPos">Position of the object.</param>
- void SubAdd(T obj, Vector3 objPos)
- {
- // We know it fits at this level if we've got this far
- // We always put things in the deepest possible child
- // So we can skip checks and simply move down if there are children aleady
- if (!HasChildren)
- {
- // Just add if few objects are here, or children would be below min size
- if (objects.Count < NUM_OBJECTS_ALLOWED || (SideLength / 2) < minSize)
- {
- OctreeObject newObj = new OctreeObject { Obj = obj, Pos = objPos };
- objects.Add(newObj);
- return; // We're done. No children yet
- }
- // Enough objects in this node already: Create the 8 children
- int bestFitChild;
- if (children == null)
- {
- Split();
- if (children == null)
- {
- Debug.LogError("Child creation failed for an unknown reason. Early exit.");
- return;
- }
- // Now that we have the new children, move this node's existing objects into them
- for (int i = objects.Count - 1; i >= 0; i--)
- {
- OctreeObject existingObj = objects[i];
- // Find which child the object is closest to based on where the
- // object's center is located in relation to the octree's center
- bestFitChild = BestFitChild(existingObj.Pos);
- children[bestFitChild].SubAdd(existingObj.Obj, existingObj.Pos); // Go a level deeper
- objects.Remove(existingObj); // Remove from here
- }
- }
- }
- // Handle the new object we're adding now
- int bestFit = BestFitChild(objPos);
- children[bestFit].SubAdd(obj, objPos);
- }
- /// <summary>
- /// Private counterpart to the public <see cref="Remove(T, Vector3)"/> method.
- /// </summary>
- /// <param name="obj">Object to remove.</param>
- /// <param name="objPos">Position of the object.</param>
- /// <returns>True if the object was removed successfully.</returns>
- bool SubRemove(T obj, Vector3 objPos)
- {
- bool removed = false;
- for (int i = 0; i < objects.Count; i++)
- {
- if (objects[i].Obj.Equals(obj))
- {
- removed = objects.Remove(objects[i]);
- break;
- }
- }
- if (!removed && children != null)
- {
- int bestFitChild = BestFitChild(objPos);
- removed = children[bestFitChild].SubRemove(obj, objPos);
- }
- if (removed && children != null)
- {
- // Check if we should merge nodes now that we've removed an item
- if (ShouldMerge())
- {
- Merge();
- }
- }
- return removed;
- }
- /// <summary>
- /// Splits the octree into eight children.
- /// </summary>
- void Split()
- {
- float quarter = SideLength / 4f;
- float newLength = SideLength / 2;
- children = new PointOctreeNode<T>[8];
- children[0] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(-quarter, quarter, -quarter));
- children[1] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(quarter, quarter, -quarter));
- children[2] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(-quarter, quarter, quarter));
- children[3] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(quarter, quarter, quarter));
- children[4] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(-quarter, -quarter, -quarter));
- children[5] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(quarter, -quarter, -quarter));
- children[6] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(-quarter, -quarter, quarter));
- children[7] = new PointOctreeNode<T>(newLength, minSize, Center + new Vector3(quarter, -quarter, quarter));
- }
- /// <summary>
- /// Merge all children into this node - the opposite of Split.
- /// Note: We only have to check one level down since a merge will never happen if the children already have children,
- /// since THAT won't happen unless there are already too many objects to merge.
- /// </summary>
- void Merge()
- {
- // Note: We know children != null or we wouldn't be merging
- for (int i = 0; i < 8; i++)
- {
- PointOctreeNode<T> curChild = children[i];
- int numObjects = curChild.objects.Count;
- for (int j = numObjects - 1; j >= 0; j--)
- {
- OctreeObject curObj = curChild.objects[j];
- objects.Add(curObj);
- }
- }
- // Remove the child nodes (and the objects in them - they've been added elsewhere now)
- children = null;
- }
- /// <summary>
- /// Checks if outerBounds encapsulates the given point.
- /// </summary>
- /// <param name="outerBounds">Outer bounds.</param>
- /// <param name="point">Point.</param>
- /// <returns>True if innerBounds is fully encapsulated by outerBounds.</returns>
- static bool Encapsulates(Bounds outerBounds, Vector3 point)
- {
- return outerBounds.Contains(point);
- }
- /// <summary>
- /// Checks if there are few enough objects in this node and its children
- /// that the children should all be merged into this.
- /// </summary>
- /// <returns>True there are less or the same abount of objects in this and its children than numObjectsAllowed.
- /// </returns>
- bool ShouldMerge()
- {
- int totalObjects = objects.Count;
- if (children != null)
- {
- foreach (PointOctreeNode<T> child in children)
- {
- if (child.children != null)
- {
- // If any of the *children* have children, there are definitely too many to merge,
- // or the child woudl have been merged already
- return false;
- }
- totalObjects += child.objects.Count;
- }
- }
- return totalObjects <= NUM_OBJECTS_ALLOWED;
- }
- /// <summary>
- /// Returns the closest distance to the given ray from a point.
- /// </summary>
- /// <param name="ray">The ray.</param>
- /// <param name="point">The point to check distance from the ray.</param>
- /// <returns>Squared distance from the point to the closest point of the ray.</returns>
- public static float SqrDistanceToRay(Ray ray, Vector3 point)
- {
- return Vector3.Cross(ray.direction, point - ray.origin).sqrMagnitude;
- }
- }
- // A Dynamic Octree for storing any objects that can be described as a single point
- // See also: BoundsOctree, where objects are described by AABB bounds
- // Octree: An octree is a tree data structure which divides 3D space into smaller partitions (nodes)
- // and places objects into the appropriate nodes. This allows fast access to objects
- // in an area of interest without having to check every object.
- // Dynamic: The octree grows or shrinks as required when objects as added or removed
- // It also splits and merges nodes as appropriate. There is no maximum depth.
- // Nodes have a constant - numObjectsAllowed - which sets the amount of items allowed in a node before it splits.
- // T: The content of the octree can be anything, since the bounds data is supplied separately.
- public class PointOctree<T>
- {
- // The total amount of objects currently in the tree
- public int Count { get; private set; }
- // Root node of the octree
- PointOctreeNode<T> rootNode;
- // Size that the octree was on creation
- readonly float initialSize;
- // Minimum side length that a node can be - essentially an alternative to having a max depth
- readonly float minSize;
- private System.Collections.Generic.HashSet<int> _allObjectIds = new System.Collections.Generic.HashSet<int>();
- public bool Contains(int objId) => _allObjectIds.Contains(objId);
- /// <summary>
- /// Constructor for the point octree.
- /// </summary>
- /// <param name="initialWorldSize">Size of the sides of the initial node.
- /// The octree will never shrink smaller than this.</param>
- /// <param name="initialWorldPos">Position of the centre of the initial node.</param>
- /// <param name="minNodeSize">Nodes will stop splitting if the new nodes would be smaller than this.</param>
- public PointOctree(float initialWorldSize, Vector3 initialWorldPos, float minNodeSize)
- {
- if (minNodeSize > initialWorldSize)
- {
- Debug.LogWarning("Minimum node size must be at least as big as the initial world size. Was: "
- + minNodeSize + " Adjusted to: " + initialWorldSize);
- minNodeSize = initialWorldSize;
- }
- Count = 0;
- initialSize = initialWorldSize;
- minSize = minNodeSize;
- rootNode = new PointOctreeNode<T>(initialSize, minSize, initialWorldPos);
- }
- // #### PUBLIC METHODS ####
- /// <summary>
- /// Add an object.
- /// </summary>
- /// <param name="obj">Object to add.</param>
- /// <param name="objPos">Position of the object.</param>
- public void Add(T obj, Vector3 objPos)
- {
- if (obj is GameObject)
- {
- var gameObj = obj as GameObject;
- var allChildren = gameObj.GetComponentsInChildren<Transform>();
- foreach (var child in allChildren)
- {
- if (!_allObjectIds.Contains(child.gameObject.GetInstanceID()))
- _allObjectIds.Add(child.gameObject.GetInstanceID());
- }
- }
- // Add object or expand the octree until it can be added
- int count = 0; // Safety check against infinite/excessive growth
- while (!rootNode.Add(obj, objPos))
- {
- Grow(objPos - rootNode.Center);
- if (++count > 20)
- {
- Debug.LogError("Aborted Add operation as it seemed to be going on forever ("
- + (count - 1) + ") attempts at growing the octree.");
- return;
- }
- }
- Count++;
- }
- /// <summary>
- /// Remove an object. Makes the assumption that the object only exists once in the tree.
- /// </summary>
- /// <param name="obj">Object to remove.</param>
- /// <returns>True if the object was removed successfully.</returns>
- public bool Remove(T obj)
- {
- bool removed = rootNode.Remove(obj);
- // See if we can shrink the octree down now that we've removed the item
- if (removed)
- {
- Count--;
- Shrink();
- }
- return removed;
- }
- /// <summary>
- /// Removes the specified object at the given position.
- /// Makes the assumption that the object only exists once in the tree.
- /// </summary>
- /// <param name="obj">Object to remove.</param>
- /// <param name="objPos">Position of the object.</param>
- /// <returns>True if the object was removed successfully.</returns>
- public bool Remove(T obj, Vector3 objPos)
- {
- bool removed = rootNode.Remove(obj, objPos);
- // See if we can shrink the octree down now that we've removed the item
- if (removed)
- {
- Count--;
- Shrink();
- }
- return removed;
- }
- /// <summary>
- /// Returns objects that are within <paramref name="maxDistance"/> of the specified ray.
- /// If none, returns false. Uses supplied list for results.
- /// </summary>
- /// <param name="ray">The ray. Passing as ref to improve performance since it won't have to be copied.</param>
- /// <param name="maxDistance">Maximum distance from the ray to consider</param>
- /// <param name="nearBy">Pre-initialized list to populate</param>
- /// <returns>True if items are found, false if not</returns>
- public bool GetNearbyNonAlloc(Ray ray, float maxDistance, System.Collections.Generic.List<T> nearBy)
- {
- nearBy.Clear();
- rootNode.GetNearby(ref ray, maxDistance, nearBy);
- if (nearBy.Count > 0)
- return true;
- return false;
- }
- /// <summary>
- /// Returns objects that are within <paramref name="maxDistance"/> of the specified ray.
- /// If none, returns an empty array (not null).
- /// </summary>
- /// <param name="ray">The ray. Passing as ref to improve performance since it won't have to be copied.</param>
- /// <param name="maxDistance">Maximum distance from the ray to consider.</param>
- /// <returns>Objects within range.</returns>
- public T[] GetNearby(Ray ray, float maxDistance)
- {
- var collidingWith = new System.Collections.Generic.List<T>();
- rootNode.GetNearby(ref ray, maxDistance, collidingWith);
- return collidingWith.Where(o => o != null).ToArray();
- }
- /// <summary>
- /// Returns objects that are within <paramref name="maxDistance"/> of the specified position.
- /// If none, returns an empty array (not null).
- /// </summary>
- /// <param name="position">The position. Passing as ref to improve performance since it won't have to be copied.
- /// </param>
- /// <param name="maxDistance">Maximum distance from the position to consider.</param>
- /// <returns>Objects within range.</returns>
- public T[] GetNearby(Vector3 position, float maxDistance)
- {
- var collidingWith = new System.Collections.Generic.List<T>();
- rootNode.GetNearby(ref position, maxDistance, collidingWith);
- return collidingWith.ToArray();
- }
- /// <summary>
- /// Returns objects that are within <paramref name="maxDistance"/> of the specified position.
- /// If none, returns false. Uses supplied list for results.
- /// </summary>
- /// <param name="maxDistance">Maximum distance from the position to consider</param>
- /// <param name="nearBy">Pre-initialized list to populate</param>
- /// <returns>True if items are found, false if not</returns>
- public bool GetNearbyNonAlloc(Vector3 position, float maxDistance, System.Collections.Generic.List<T> nearBy)
- {
- nearBy.Clear();
- rootNode.GetNearby(ref position, maxDistance, nearBy);
- if (nearBy.Count > 0)
- return true;
- return false;
- }
- /// <summary>
- /// Return all objects in the tree.
- /// If none, returns an empty array (not null).
- /// </summary>
- /// <returns>All objects.</returns>
- public System.Collections.Generic.ICollection<T> GetAll()
- {
- var objects = new System.Collections.Generic.List<T>(Count);
- rootNode.GetAll(objects);
- return objects;
- }
- // #### PRIVATE METHODS ####
- /// <summary>
- /// Grow the octree to fit in all objects.
- /// </summary>
- /// <param name="direction">Direction to grow.</param>
- void Grow(Vector3 direction)
- {
- int xDirection = direction.x >= 0 ? 1 : -1;
- int yDirection = direction.y >= 0 ? 1 : -1;
- int zDirection = direction.z >= 0 ? 1 : -1;
- PointOctreeNode<T> oldRoot = rootNode;
- float half = rootNode.SideLength / 2;
- float newLength = rootNode.SideLength * 2;
- Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half);
- // Create a new, bigger octree root node
- rootNode = new PointOctreeNode<T>(newLength, minSize, newCenter);
- if (oldRoot.HasAnyObjects())
- {
- // Create 7 new octree children to go with the old root as children of the new root
- int rootPos = rootNode.BestFitChild(oldRoot.Center);
- PointOctreeNode<T>[] children = new PointOctreeNode<T>[8];
- for (int i = 0; i < 8; i++)
- {
- if (i == rootPos)
- {
- children[i] = oldRoot;
- }
- else
- {
- xDirection = i % 2 == 0 ? -1 : 1;
- yDirection = i > 3 ? -1 : 1;
- zDirection = (i < 2 || (i > 3 && i < 6)) ? -1 : 1;
- children[i] = new PointOctreeNode<T>(oldRoot.SideLength, minSize,
- newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half));
- }
- }
- // Attach the new children to the new root node
- rootNode.SetChildren(children);
- }
- }
- /// <summary>
- /// Shrink the octree if possible, else leave it the same.
- /// </summary>
- void Shrink()
- {
- rootNode = rootNode.ShrinkIfPossible(initialSize);
- }
- }
- }
|