예제 #1
0
        public bool containsElement(OctreeElement <T> e)
        {
            float halfSize = mySize / 2.0f;
            float maxX     = myPosition.X + halfSize;
            float maxY     = myPosition.Y + halfSize;
            float maxZ     = myPosition.Z + halfSize;

            float dx = maxX - e.myX;
            float dy = maxY - e.myY;
            float dz = maxZ - e.myZ;

            if (dx < 0 || dx > mySize)
            {
                return(false);
            }
            if (dy < 0 || dy > mySize)
            {
                return(false);
            }
            if (dz < 0 || dz > mySize)
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public void insert(OctreeElement <T> e)
        {
            sbyte child = myOctree.getKey(e.myX, e.myY, e.myZ, e.mySize, myDepth);

            //if it's too big for the child or if this is the deepest we go
            if (child == -1)
            {
                myElements.Add(e);
                e.myNodeId = myId;
                System.Threading.Interlocked.Increment(ref myCount);
                return;
            }

            //insert in the child
            //create a new child node if it doesn't exist yet
            int id = myChildren[child];

            if (id == -1)
            {
                addChild(child);
            }

            myOctree.myNodes[myChildren[child]].insert(e);
            System.Threading.Interlocked.Increment(ref myCount);
        }
예제 #3
0
 public void update(OctreeElement <T> e)
 {
     if (myNodes[e.myNodeId].containsElement(e) == false)
     {
         remove(e);
         insert(e);
     }
 }
예제 #4
0
        public uint distanceBetween(OctreeElement <T> e2)
        {
            //Manhattan distance is cheaper but at a precision loss.
            //It is consistent though, so it should still be ok
            Vector3 p1 = new Vector3(myX, myY, myZ);
            Vector3 p2 = new Vector3(e2.myX, e2.myY, e2.myZ);

            return((uint)(p1 - p2).LengthFast);
        }
예제 #5
0
        public List <OctreeElement <T> > neighborsWithin(OctreeElement <T> e, uint distance)
        {
            Vector3 pos = new Vector3(e.myX, e.myY, e.myZ);
            int     id  = myNodes[0].nodeContainingSphere(pos, distance);

            List <OctreeElement <T> > actualList = new List <OctreeElement <T> >();

            foreach (OctreeElement <T> el in myNodes[id].myElements)
            {
                if (e.distanceBetween(el) < distance)
                {
                    actualList.Add(el);
                }
            }

            return(actualList);
        }
예제 #6
0
        public OctreeElement <T> nearestNeighborWithin(OctreeElement <T> e, ref uint distance)
        {
            OctreeElement <T> nearest = null;

            if (isLeaf() == false)
            {
                for (int i = 0; i < 8; i++)
                {
                    int childId = myChildren[i];
                    if (childId != -1)
                    {
                        uint tempDistance = distance;
                        OctreeElement <T> tempNearest;
                        tempNearest = myOctree.myNodes[childId].nearestNeighborWithin(e, ref tempDistance);
                        if (tempNearest != e && tempDistance < distance)
                        {
                            distance = tempDistance;
                            nearest  = tempNearest;
                        }
                    }
                }
            }
            else
            {
                //check the elements stored in this node (assuming it's a leaf)
                foreach (OctreeElement <T> el in myElements)
                {
                    if (el == e)
                    {
                        continue;
                    }

                    uint tempDistance = e.distanceBetween(el);
                    if (tempDistance < distance)
                    {
                        distance = tempDistance;
                        nearest  = el;
                    }
                }
            }

            return(nearest);
        }
예제 #7
0
        public OctreeElement <T> nearestNeighbor(OctreeElement <T> e)
        {
            //set minimum distance to the maximum possible distance to start with
            uint minDist = 0xffffffff;

            int id = e.myNodeId;

            //while the current node only holds one element (assumed to be the one were finding the nearest neighbor to)
            //loop up and find the parent node
            while (myNodes[id].myCount == 1)
            {
                id = myNodes[id].myParent;
                //damn, hit the root node
                if (id == -1)
                {
                    //couldn't find another node with any other children
                    //this means there's only 1 element in the entire octree
                    return(null);
                }
            }

            //actually just getting the min Distance to the nearest neighbor in the
            //current node branch, but we'll need to refine this by using this as the min distance heuristic
            //and check the entire octree again using this distance
            OctreeElement <T> nn = nearestNeighborInNode(id, e, ref minDist);

            ////determine node that holds a sphere with point e.xyz and radius minDistance in octree
            ////and determine nearest neighbors within that node
            //Vector3 pos=new Vector3(e.myX, e.myY, e.myZ);
            //int secondId = nodeContainingSphere(pos, minDist);

            //if (secondId != -1 && secondId!=id)
            //{
            //   //search that node for the nearest neighbor
            //   nn = nearestNeighborInNode(secondId, e, ref minDist);
            //}

            return(nn);
        }
예제 #8
0
 public OctreeElement <T> nearestNeighborInNode(int id, OctreeElement <T> e, ref uint minDist)
 {
     return(myNodes[id].nearestNeighborWithin(e, ref minDist));
 }
예제 #9
0
 public void remove(OctreeElement <T> e)
 {
     myNodes[e.myNodeId].remove(e);
 }
예제 #10
0
 public void insert(OctreeElement <T> e)
 {
     myNodes[0].insert(e);
 }
예제 #11
0
 public void remove(OctreeElement <T> e)
 {
     myElements.Remove(e);
     decreaseCount();
 }