private static MXCIFQuadTreeNode <object> SetOnNodeWithRect(
            double x,
            double y,
            double width,
            double height,
            XYWHRectangleWValue <TL> value,
            MXCIFQuadTreeNode <object> node,
            MXCIFQuadTree <object> tree)
        {
            if (node is MXCIFQuadTreeNodeLeaf <object> leaf)
            {
                var count = SetOnNodeWithRect(leaf, x, y, width, height, value);
                leaf.IncCount(count);

                if (leaf.Count <= tree.LeafCapacity || node.Level >= tree.MaxTreeHeight)
                {
                    return(leaf);
                }

                node = Subdivide(leaf, tree);
            }

            var branch = (MXCIFQuadTreeNodeBranch <object>)node;

            AddToBranchWithRect(branch, x, y, width, height, value, tree);
            return(node);
        }
        private static int SetOnNodeWithRect(
            MXCIFQuadTreeNode <object> node,
            double x,
            double y,
            double width,
            double height,
            XYWHRectangleWValue <TL> value)
        {
            if (!value.CoordinateEquals(x, y, width, height))
            {
                throw new IllegalStateException();
            }

            return(SetOnNode(node, x, y, width, height, value.Value));
        }
        private static void Subdivide(
            XYWHRectangleWValue <TL> rectangle,
            MXCIFQuadTreeNodeBranch <object> branch,
            MXCIFQuadTree <object> tree)
        {
            var x        = rectangle.X;
            var y        = rectangle.Y;
            var w        = rectangle.W;
            var h        = rectangle.H;
            var quadrant = branch.Bb.GetQuadrantApplies(x, y, w, h);

            if (quadrant == QuadrantAppliesEnum.NW)
            {
                branch.Nw = SetOnNodeWithRect(x, y, w, h, rectangle, branch.Nw, tree);
            }
            else if (quadrant == QuadrantAppliesEnum.NE)
            {
                branch.Ne = SetOnNodeWithRect(x, y, w, h, rectangle, branch.Ne, tree);
            }
            else if (quadrant == QuadrantAppliesEnum.SW)
            {
                branch.Sw = SetOnNodeWithRect(x, y, w, h, rectangle, branch.Sw, tree);
            }
            else if (quadrant == QuadrantAppliesEnum.SE)
            {
                branch.Se = SetOnNodeWithRect(x, y, w, h, rectangle, branch.Se, tree);
            }
            else if (quadrant == QuadrantAppliesEnum.SOME)
            {
                var numAdded = SetOnNodeWithRect(branch, x, y, w, h, rectangle);
                branch.IncCount(numAdded);
            }
            else
            {
                throw new IllegalStateException("No intersection");
            }
        }
        private static void AddToBranchWithRect(
            MXCIFQuadTreeNodeBranch <object> branch,
            double x,
            double y,
            double width,
            double height,
            XYWHRectangleWValue <TL> value,
            MXCIFQuadTree <object> tree)
        {
            switch (branch.Bb.GetQuadrantApplies(x, y, width, height))
            {
            case QuadrantAppliesEnum.NW:
                branch.Nw = SetOnNodeWithRect(x, y, width, height, value, branch.Nw, tree);
                break;

            case QuadrantAppliesEnum.NE:
                branch.Ne = SetOnNodeWithRect(x, y, width, height, value, branch.Ne, tree);
                break;

            case QuadrantAppliesEnum.SW:
                branch.Sw = SetOnNodeWithRect(x, y, width, height, value, branch.Sw, tree);
                break;

            case QuadrantAppliesEnum.SE:
                branch.Se = SetOnNodeWithRect(x, y, width, height, value, branch.Se, tree);
                break;

            case QuadrantAppliesEnum.SOME:
                var count = SetOnNodeWithRect(branch, x, y, width, height, value);
                branch.IncCount(count);
                break;

            default:
                throw new IllegalStateException("Quandrant not applies to any");
            }
        }