Esempio n. 1
0
    public void AddToNeighbors(BoundsOctree source, int targetXLoc, int targetYLoc, int targetZLoc)
    {
        /* if
        * you have suboctrees lower than the asked for target it will get all the nodes that
        * neighbor it, by comparing the coordinates we create a filter, i wish i could make this a little bit more automated. but it iss friggin fast anyway.*/
        if (source == null)
        {
            return;
        }
        if (subOctrees == null) // (subOctrees == null && Empty) would remove nonEmpty objects from neighbor lists... depending on what you want to do with the octree.
        {
            source.AddNeighbor(this);
        }
        else
        {
            int childBranchBit = 1 << (levelDepth - 1);
            int childIndex;
            if (source.levelDepth >= levelDepth &&subOctrees!=null)
            {
                int binaryCellSize = 1 << levelDepth;
                bool x = (targetXLoc < source.binaryLocation_X || targetXLoc >= source.binaryLocation_X + binaryCellSize);
                bool y = (targetYLoc < source.binaryLocation_Y || targetYLoc >= source.binaryLocation_Y + binaryCellSize);
                bool z = (targetZLoc < source.binaryLocation_Z || targetZLoc >= source.binaryLocation_Z + binaryCellSize);

                if(x&&y&&z)
                {
                    childIndex =
                    (((targetXLoc & childBranchBit) >> levelDepth - 1)) +
                    (((targetYLoc & childBranchBit) >> levelDepth - 1) * 4) +
                    (((targetZLoc & childBranchBit) >> levelDepth - 1) * 2);
                    subOctrees[childIndex].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                }
                else if (!x && !z)
                {

                    int offset = (((targetYLoc & childBranchBit) >> levelDepth - 1)*4);
                    subOctrees[0 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[1 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[2 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[3 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);

                }
                else if(!x && !y)
                {
                    int offset = (((targetZLoc & childBranchBit) >> levelDepth - 1) * 2);
                    subOctrees[0 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[1 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[4 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[5 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                }
                else if (!y && !z)
                {
                    int offset = (((targetXLoc & childBranchBit) >> levelDepth - 1) );
                    subOctrees[0 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[2 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[4 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[6 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                }
                else if(!x)
                {
                    int offset = (((targetYLoc & childBranchBit) >> levelDepth - 1) * 4) +
                         (((targetZLoc & childBranchBit) >> levelDepth - 1) * 2);

                    subOctrees[0 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[1 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);

                }
                else if(!y)
                {
                    int offset = (((targetXLoc & childBranchBit) >> levelDepth - 1)) +
                         (((targetZLoc & childBranchBit) >> levelDepth - 1) * 2);
                    subOctrees[0 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[4 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                }
                else //z
                {
                    int offset = (((targetXLoc & childBranchBit) >> levelDepth - 1)) +
                                 (((targetYLoc & childBranchBit) >> levelDepth - 1)*4);
                    subOctrees[0 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                    subOctrees[2 + offset].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);
                }

            }
            else
            {

                if (subOctrees != null)
                {
                    childIndex =
                    (((targetXLoc & childBranchBit) >> levelDepth - 1)) +
                    (((targetYLoc & childBranchBit) >> levelDepth - 1) * 4) +
                    (((targetZLoc & childBranchBit) >> levelDepth - 1) * 2);
                    if (subOctrees[childIndex] == null)
                    {
                        int subsMissing = 0;
                        foreach (var sub in subOctrees)
                        {
                            if (sub == null)
                            {
                                subsMissing++;
                            }
                        }
                        DebugOutput.Shout("SubOctree has been deleted somewhere : "+subsMissing);
                    }
                    subOctrees[childIndex].AddToNeighbors(source, targetXLoc, targetYLoc, targetZLoc);

                }

            }

        }
    }