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); }
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); }
public void update(OctreeElement <T> e) { if (myNodes[e.myNodeId].containsElement(e) == false) { remove(e); insert(e); } }
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); }
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); }
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); }
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); }
public OctreeElement <T> nearestNeighborInNode(int id, OctreeElement <T> e, ref uint minDist) { return(myNodes[id].nearestNeighborWithin(e, ref minDist)); }
public void remove(OctreeElement <T> e) { myNodes[e.myNodeId].remove(e); }
public void insert(OctreeElement <T> e) { myNodes[0].insert(e); }
public void remove(OctreeElement <T> e) { myElements.Remove(e); decreaseCount(); }