/// <summary> /// Recursive partition creation /// </summary> /// <param name="quadTree">Quadtree</param> /// <param name="parent">Parent node</param> /// <param name="bbox">Parent bounding box</param> /// <param name="items">All items</param> /// <param name="maxDepth">Maximum depth</param> /// <param name="treeDepth">Current depth</param> /// <param name="nodeCount">Node count</param> /// <returns>Returns new node</returns> public static QuadTreeNode <T> CreatePartitions( QuadTree <T> quadTree, QuadTreeNode <T> parent, BoundingBox bbox, IEnumerable <T> items, int maxDepth, int treeDepth, ref int nodeCount) { if (treeDepth <= maxDepth) { var nodeItems = items.Where(i => { var tbox = BoundingBox.FromPoints(i.GetVertices()); return(Intersection.BoxContainsBox(ref bbox, ref tbox) != ContainmentType.Disjoint); }); if (nodeItems.Any()) { var node = new QuadTreeNode <T>(quadTree, parent) { Id = -1, Level = treeDepth, BoundingBox = bbox, }; bool haltByDepth = treeDepth == maxDepth; if (haltByDepth) { node.Id = nodeCount++; node.Items.AddRange(nodeItems); } else { // Initialize node partitions IntializeNode(quadTree, node, bbox, nodeItems, maxDepth, treeDepth + 1, ref nodeCount); } return(node); } } return(null); }