Пример #1
0
    private int GetQuads(Bound2D box, out QuadTree <T>[] quads)
    {
        List <QuadTree <T> > listTree = new List <QuadTree <T> >();

        if (bInternal)
        {
            bool bNegX = box.min.x <= treeCenter.x;
            bool bNegY = box.min.y <= treeCenter.y;

            bool bPosX = box.max.x >= treeCenter.x;
            bool bPosY = box.max.y >= treeCenter.y;

            if (bNegX && bNegY)
            {
                listTree.Add(subTrees[BottomLeft]);
            }

            if (bPosX && bNegY)
            {
                listTree.Add(subTrees[BottomRight]);
            }

            if (bNegX && bPosY)
            {
                listTree.Add(subTrees[TopLeft]);
            }

            if (bPosX && bPosY)
            {
                listTree.Add(subTrees[TopRight]);
            }
        }
        quads = listTree.ToArray();
        return(listTree.Count);
    }
Пример #2
0
 public QuadTree(Bound2D box, float minSize)
 {
     treeBox         = box;
     treeCenter      = box.center;
     minimumQuadSize = minSize;
     subTrees        = new QuadTree <T> [NodeCapacity];
 }
Пример #3
0
    static public void DrawBounds(Bound2D box, Color c, Matrix4x4 mat)
    {
        Vector2 Extent  = box.GetExtent();
        Vector2 XExtent = new Vector2(Extent.x, 0.0f);
        Vector2 YExtent = new Vector2(0.0f, Extent.y);
        Vector2 C       = box.center;
        Vector2 TM      = C + YExtent + XExtent;
        Vector2 ML      = C + YExtent - XExtent;
        Vector2 MR      = C - YExtent - XExtent;
        Vector2 BM      = C - YExtent + XExtent;

        GL.PushMatrix();
        GL.MultMatrix(mat);
        GL.Begin(GL.LINES);
        GL.Color(c);
        GL.Vertex3(TM.x, 0, TM.y);
        GL.Vertex3(ML.x, 0, ML.y);
        GL.Vertex3(ML.x, 0, ML.y);
        GL.Vertex3(MR.x, 0, MR.y);
        GL.Vertex3(MR.x, 0, MR.y);
        GL.Vertex3(BM.x, 0, BM.y);
        GL.Vertex3(BM.x, 0, BM.y);
        GL.Vertex3(TM.x, 0, TM.y);
        GL.End();
        GL.PopMatrix();
    }
Пример #4
0
 public void Insert(T element, Bound2D box)
 {
     if (!box.Intersect(treeBox))
     {
         //todo log
     }
     InsertElementRecursive(element, box);
 }
Пример #5
0
    public Tuple <Vector2, Vector2> GetScaleTranlslate(Room currentRoom, Bound2D realSpace) // v is currentRoom
    {
        Room    v         = currentRoom;
        Vector2 Scale     = new Vector2(v.OriginSize.x / v.Size.x, v.OriginSize.y / v.Size.y);
        Vector2 Translate = realSpace.Position - v.Position;

        return(new Tuple <Vector2, Vector2>(Scale, Translate));
    }
Пример #6
0
 private void GetIntersectingElements(Bound2D box, List <T> elementsOut)
 {
     foreach (var node in nodeList)
     {
         if (box.Intersect(node.bound))
         {
             elementsOut.Add(node.element);
         }
     }
 }
Пример #7
0
 public bool IsIntersect(Bound2D other) // this와 point가 교차하는지 판단하는 함수
 {
     if (IsIntersectInXAxis(other) && IsIntersectInYAxis(other))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #8
0
 public bool IsIntersectInXAxis(Bound2D other) // x축 기준으로 this 와 otherbox가 교차하는지 판단하는 함수
 {
     if ((this.Min.x - other.Max.x < 0.01f) && (this.Max.x - other.Min.x > 0.01f))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #9
0
 public bool IsInside(Bound2D other) // this가 other 안에 들어오는지 판단하는 함수
 {
     if (IsInsideInXAxis(other) && IsInsideInYAxis(other))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #10
0
 public bool IsInsideInYAxis(Bound2D other) // y축 기준 this가 other 안에 들어오는지 판단하는 함수
 {
     if ((other.Min.y - this.Min.y <= 0.05f) && (other.Max.y - this.Max.y >= 0.05f))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #11
0
 public bool IsInsideInXAxis(Bound2D other) // x축 기준으로 this가 other 안에 들어오는지 판단하는 함수
 {
     if ((other.Min.x - this.Min.x <= 0.05f) && (other.Max.x - this.Max.x >= 0.05f))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #12
0
 /// <summary>
 /// 是否与当前包围盒相交
 /// </summary>
 /// <param name="bound"></param>
 /// <returns></returns>
 public bool Intersects(Bound2D bound)
 {
     for (int i = 0; i < bound.concer.Length; i++)
     {
         if (Contain(bound.concer[i]))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #13
0
 public bool IsIntersectInYAxis(Bound2D other) // y축 기준
 {
     if ((this.Min.y - other.Max.y < 0.01f) && (this.Max.y - other.Min.y > 0.01f))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #14
0
    public bool Intersect(Bound2D Other)
    {
        if ((min.x > Other.max.x) || (Other.min.x > max.x))
        {
            return(false);
        }

        if ((min.y > Other.max.y) || (Other.min.y > max.y))
        {
            return(false);
        }

        return(true);
    }
Пример #15
0
    public void GetElements(Bound2D box, List <T> elementsOut)
    {
        QuadTree <T>[] Quads;
        int            NumQuads = GetQuads(box, out Quads);

        // Always include any nodes contained in this quad
        GetIntersectingElements(box, elementsOut);

        // As well as all relevant subtrees
        for (int QuadIndex = 0; QuadIndex < NumQuads; QuadIndex++)
        {
            Quads[QuadIndex].GetElements(box, elementsOut);
        }
    }
Пример #16
0
 /// <summary>
 /// 是否与当前包围盒相交
 /// </summary>
 /// <param name="bound"></param>
 /// <returns></returns>
 public bool Intersects(Bound2D bound, out int num)
 {
     num = 0;
     for (int i = 0; i < bound.concer.Length; i++)
     {
         if (Contain(bound.concer[i]))
         {
             num++;
         }
     }
     if (num > 0)
     {
         return(true);
     }
     return(false);
 }
Пример #17
0
    public bool Remove(T element, Bound2D box)
    {
        bool bElementRemoved = false;

        QuadTree <T>[] Quads;
        int            NumQuads = GetQuads(box, out Quads);

        // Remove from nodes referenced by this quad
        bElementRemoved = RemoveNodeForElement(element);

        // Try to remove from subtrees if necessary
        for (int QuadIndex = 0; QuadIndex < NumQuads && !bElementRemoved; QuadIndex++)
        {
            bElementRemoved = Quads[QuadIndex].Remove(element, box);
        }

        return(bElementRemoved);
    }
Пример #18
0
    private void InsertElementRecursive(T element, Bound2D box)
    {
        QuadTree <T>[] Quads;
        int            NumQuads = GetQuads(box, out Quads);

        if (NumQuads == 0)
        {
            // This should only happen for leaves
            //check(!bInternal);

            // It's possible that all elements in the leaf are bigger than the leaf or that more elements than NodeCapacity exist outside the top level quad
            // In either case, we can get into an endless spiral of splitting
            bool bCanSplitTree = treeBox.GetSize().magnitude > minimumQuadSize;
            if (!bCanSplitTree || nodeList.Count < NodeCapacity)
            {
                nodeList.Add(new QuadTreeNode(element, box));

                if (!bCanSplitTree)
                {
                    //UE_LOG(LogQuadTree, Warning, TEXT("Minimum size %f reached for quadtree at %s. Filling beyond capacity %d to %d"), minimumQuadSize, *Position.ToString(), NodeCapacity, Nodes.Num());
                }
            }
            else
            {
                // This quad is at capacity, so split and try again
                Split();
                InsertElementRecursive(element, box);
            }
        }
        else if (NumQuads == 1)
        {
            //check(bInternal);

            // Fully contained in a single subtree, so insert it there
            Quads[0].InsertElementRecursive(element, box);
        }
        else
        {
            // Overlaps multiple subtrees, store here
            //check(bInternal);
            nodeList.Add(new QuadTreeNode(element, box));
        }
    }
Пример #19
0
        public bool Contain(Bound2D bound)
        {
            int num = 0;

            for (int i = 0; i < bound.concer.Length; i++)
            {
                if (Contain(bound.concer[i]))
                {
                    num++;
                }
            }
            if (num == 4)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #20
0
    public void Reduce(VirtualEnvironment virtualEnvironment, Room targetRoom, Room currentRoom, Bound2D realSpace)
    {
        float xMinDist = realSpace.Min.x - targetRoom.Min.x;
        float xMaxDist = realSpace.Max.x - targetRoom.Max.x;
        float yMinDist = realSpace.Min.y - targetRoom.Min.y;
        float yMaxDist = realSpace.Max.y - targetRoom.Max.y;

        if (xMinDist > 0) // 1벽
        {
            virtualEnvironment.MoveWall(targetRoom, 1, xMinDist, currentRoom);
        }
        if (xMaxDist < 0) // 3벽
        {
            virtualEnvironment.MoveWall(targetRoom, 3, xMaxDist, currentRoom);
        }

        if (yMinDist > 0) // 2벽
        {
            virtualEnvironment.MoveWall(targetRoom, 2, yMinDist, currentRoom);
        }
        if (yMaxDist < 0) // 0벽
        {
            virtualEnvironment.MoveWall(targetRoom, 0, yMaxDist, currentRoom);
        }
    }
Пример #21
0
 public bool IsInside(Bound2D Other)
 {
     return(IsInside(Other.min) && IsInside(Other.max));
 }
Пример #22
0
 public QuadTreeNode(T v, Bound2D box)
 {
     bound   = box;
     element = v;
 }