public bool SearchForIntersect(ref VMObstacleSetNode node, BaseMeshTriangle rect) { if (node.Intersects(rect)) { return(true); } //search in child nodes. int dontSearch = 0; switch (node.Dimension) { case IntersectRectDimension.Top: dontSearch = (rect.y2 <= node.y1) ? 2 : 0; break; //if true, do not have to search right (where top greater) case IntersectRectDimension.Left: dontSearch = (rect.x2 <= node.x1) ? 2 : 0; break; //if true, do not have to search right (where left greater) case IntersectRectDimension.Bottom: dontSearch = (rect.y1 >= node.y2) ? 1 : 0; break; //if true, do not have to search left (where bottom less) case IntersectRectDimension.Right: dontSearch = (rect.x1 >= node.x2) ? 1 : 0; break; //if true, do not have to search left (where right less) } //may need to search both :'( won't happen often with our small rectangles over large space though. return((dontSearch != 1 && node.LeftChild != -1 && SearchForIntersect(ref Nodes[node.LeftChild], rect)) || (dontSearch != 2 && node.RightChild != -1 && SearchForIntersect(ref Nodes[node.RightChild], rect))); }
public bool Delete(ref VMObstacleSetNode node, VMEntityObstacle rect, ref VMObstacleSetNode parent) { if (rect.Parent == (node.Rect as VMEntityObstacle).Parent) { if (parent.Index == node.Index) { Root = -1; } else { if (parent.LeftChild == node.Index) { parent.LeftChild = -1; } if (parent.RightChild == node.Index) { parent.RightChild = -1; } } if (node.LeftChild != -1) { RecursiveReAdd(Nodes[node.LeftChild]); } if (node.RightChild != -1) { RecursiveReAdd(Nodes[node.RightChild]); } Reclaim(node.Index); return(true); } //search in child nodes. //binary search to find equal opposing edges. bool rightSide = false; switch (node.Dimension) { case IntersectRectDimension.Top: rightSide = rect.y1 > node.y1; break; case IntersectRectDimension.Left: rightSide = rect.x1 > node.x1; break; case IntersectRectDimension.Bottom: rightSide = rect.y2 > node.y2; break; case IntersectRectDimension.Right: rightSide = rect.x2 > node.x2; break; } return((rightSide && node.RightChild != -1 && Delete(ref Nodes[node.RightChild], rect, ref node)) || (!rightSide && node.LeftChild != -1 && Delete(ref Nodes[node.LeftChild], rect, ref node))); }
public void RecursiveReAdd(VMObstacleSetNode node) { Count--; Reclaim(node.Index); Add(node.Rect); if (node.LeftChild != -1) { RecursiveReAdd(Nodes[node.LeftChild]); } if (node.RightChild != -1) { RecursiveReAdd(Nodes[node.RightChild]); } }
private int GetNode(IntersectRectDimension dir, BaseMeshTriangle rect) { var ind = GetNode(); Nodes[ind] = new VMObstacleSetNode() { Dimension = dir, Rect = rect, LeftChild = -1, RightChild = -1, Index = ind, x1 = rect.x1, x2 = rect.x2, y1 = rect.y1, y2 = rect.y2 }; return(ind); }
private void AddAsChild(ref VMObstacleSetNode node, BaseMeshTriangle rect) { bool rightSide = false; switch (node.Dimension) { case IntersectRectDimension.Top: rightSide = rect.y1 > node.Rect.y1; break; case IntersectRectDimension.Left: rightSide = rect.x1 > node.Rect.x1; break; case IntersectRectDimension.Bottom: rightSide = rect.y2 > node.Rect.y2; break; case IntersectRectDimension.Right: rightSide = rect.x2 > node.Rect.x2; break; } if (rightSide) { if (node.RightChild != -1) { AddAsChild(ref Nodes[node.RightChild], rect); } else { node.RightChild = GetNode((IntersectRectDimension)(((int)node.Dimension + 1) % 4), rect); } } else { if (node.LeftChild != -1) { AddAsChild(ref Nodes[node.LeftChild], rect); } else { node.LeftChild = GetNode((IntersectRectDimension)(((int)node.Dimension + 1) % 4), rect); } } }
public void OnEdge(ref VMObstacleSetNode node, BaseMeshTriangle rect, List <BaseMeshTriangle> result) { if (node.OnEdge(rect)) { result.Add(node.Rect); } //search in child nodes. //binary search to find equal opposing edges. int dontSearch = 0; switch (node.Dimension) { case IntersectRectDimension.Top: dontSearch = (rect.y2 < node.y1) ? 2 : 0; break; //if true, do not have to search right (where top greater) case IntersectRectDimension.Left: dontSearch = (rect.x2 < node.x1) ? 2 : 0; break; //if true, do not have to search right (where left greater) case IntersectRectDimension.Bottom: dontSearch = (rect.y1 > node.y2) ? 1 : 0; break; //if true, do not have to search left (where bottom less) case IntersectRectDimension.Right: dontSearch = (rect.x1 > node.x2) ? 1 : 0; break; //if true, do not have to search left (where right less) } //may need to search both :'( won't happen often with our small rectangles over large space though. //if (node.LeftChild != -1) OnEdge(ref Nodes[node.LeftChild], rect, result); //if (node.RightChild != -1) OnEdge(ref Nodes[node.RightChild], rect, result); if (dontSearch != 1 && node.LeftChild != -1) { OnEdge(ref Nodes[node.LeftChild], rect, result); } if (dontSearch != 2 && node.RightChild != -1) { OnEdge(ref Nodes[node.RightChild], rect, result); } }