示例#1
0
        public static OctValue CreateTail(Vector3 value)
        {
            var octValue = new OctValue();

            octValue.Position      = value;
            octValue.PreviousValue = IndexToOctValue.Empty();
            return(octValue);
        }
示例#2
0
        public OctNode(AABB aabb)
        {
            AABB = aabb;

            LastValue = IndexToOctValue.Empty();

            ValueCount = 0;

            FirstChildIndex = int.MinValue;
        }
        private void InsertPointInNodeOrChildren(IndexToOctNode nodeIndex, IndexToOctValue valueToInsertIndex)
        {
            var node = nodeIndex.GetElement(Nodes);

            //if node is a leaf
            if (node.ValueCount >= 0)
            {
                #region insert into self

                //if no values currently in node
                if (!node.LastValue.HasValue())
                {
                    node.LastValue = valueToInsertIndex;
                }

                //otherwise find last element and link to new element
                else
                {
                    //connect new last value to previous
                    var newLastValue = valueToInsertIndex.GetElement(Values);
                    newLastValue.PreviousValue = node.LastValue;
                    valueToInsertIndex.SetElement(Values, newLastValue);

                    node.LastValue = valueToInsertIndex; //set last value to new inserted value
                }

                node.ValueCount++;

                #endregion

                float theoreticalChildHalfWidth = node.AABB.HalfWidth / 2f;
                //if exceeded maxium allowed values,
                //and child would not be less than min half width
                if (node.ValueCount > Settings.MaxValuesPerNode &&
                    theoreticalChildHalfWidth > Settings.MinHalfSize)
                {
                    #region redistributie values into children if max values exceeded and can still subdivide
                    IndexToOctValue currentRedistributeValueIndex = node.LastValue;
                    while (currentRedistributeValueIndex.HasValue())
                    {
                        var currentRedistributeValue   = currentRedistributeValueIndex.GetElement(Values);
                        var nextRedistributeValueCache = currentRedistributeValue.PreviousValue;

                        //break up linked list (child will reconstruct it appropriately)
                        currentRedistributeValue.PreviousValue = IndexToOctValue.Empty();
                        currentRedistributeValueIndex.SetElement(Values, currentRedistributeValue);

                        InsertValueInChildren(ref node, currentRedistributeValueIndex);

                        currentRedistributeValueIndex = nextRedistributeValueCache;
                    }

                    //this node is no longer a leaf, revoke ownership of values
                    node.LastValue  = IndexToOctValue.Empty();
                    node.ValueCount = -1; //makes node non-insertable (a leaf)
                    #endregion
                }
            }

            //if node is not a leaf
            else
            {
                InsertValueInChildren(ref node, valueToInsertIndex);
            }

            nodeIndex.SetElement(Nodes, node);
        }