/// <summary> /// Inserts an object into the neartree. /// </summary> public void InsertObject(NearTreeObject nearTreeObject) { double tempRight = 0; double tempLeft = 0; if (m_pRight != null) { tempRight = Dist(nearTreeObject.Location, m_pRight.Location); tempLeft = Dist(nearTreeObject.Location, m_pLeft.Location); } if (m_pLeft == null) { m_pLeft = nearTreeObject; } else if (m_pRight == null) { m_pRight = nearTreeObject; } else if (tempLeft > tempRight) { if (m_pRightBranch == null) { m_pRightBranch = new NearTree(this.Metric); } // Note: that the next line assumes that m_maxRight // is negative for a new node. if (m_maxRight < tempRight) { m_maxRight = tempRight; } m_pRightBranch.InsertObject(nearTreeObject); } else { if (m_pLeftBranch == null) { m_pLeftBranch = new NearTree(this.Metric); } // Note: that the next line assumes that m_maxLeft // is negative for a new node. if (m_maxLeft < tempLeft) { m_maxLeft = tempLeft; } m_pLeftBranch.InsertObject(nearTreeObject); } }
private bool FindNearestNeighborRecursive(ref NearTreeObject closest, Vector3D location, ref double searchRadius) { double tempRadius = 0; bool bRet = false; // First test each of the left and right positions to see // if one holds a point nearer than the nearest so far. if (m_pLeft != null) { tempRadius = Dist(location, m_pLeft.Location); if (tempRadius <= searchRadius) { searchRadius = tempRadius; closest = m_pLeft; bRet = true; } } if (m_pRight != null) { tempRadius = Dist(location, m_pRight.Location); if (tempRadius <= searchRadius) { searchRadius = tempRadius; closest = m_pRight; bRet = true; } } // Now we test to see if the branches below might hold an // object nearer than the best so far found. The triangle // rule is used to test whether it's even necessary to descend. if (m_pLeftBranch != null) { if ((searchRadius + m_maxLeft) >= Dist(location, m_pLeft.Location)) { bRet |= m_pLeftBranch.FindNearestNeighborRecursive(ref closest, location, ref searchRadius); } } if (m_pRightBranch != null) { if ((searchRadius + m_maxRight) >= Dist(location, m_pRight.Location)) { bRet |= m_pRightBranch.FindNearestNeighborRecursive(ref closest, location, ref searchRadius); } } return(bRet); }
/// <summary> /// Finds the nearest neighbor to a location, and /// withing a specified search radius (returns false if none found). /// </summary> public bool FindNearestNeighbor(out NearTreeObject closest, Vector3D location, double searchRadius) { closest = null; return(FindNearestNeighborRecursive(ref closest, location, ref searchRadius)); }