internal Node(Treemap <T> map, T value, float length) { _map = map; Value = value; SplitVertical = false; IsLeaf = true; Length = length; }
public static Treemap <T> Build(BoundingRectangle space, ITree <T> data) { //Create the output var map = new Treemap <T>(space); //Create root node with unspecified direction and length var root = new Node <T>(map, data.Root.Value, -1); map.Root = root; //Create the map, this will initialize the direction of the root node DivideNode(space, data.Root, root, new ArrayPool <float>(), map); //Initialize the length of the root node root.Length = Size(space, !root.SplitVertical); return(map); }
private static void DivideNode(BoundingRectangle space, INode <T> input, Node <T> output, ArrayPool <float> pool, Treemap <T> map) { //Recursion base case if (input.Count == 0) { return; } var h = pool.Allocate(input.Count); var v = pool.Allocate(input.Count); //Take the split with the least bad aspect ratio if (MeasureSizes(false, space, input, h) < MeasureSizes(true, space, input, v)) { //Free the unused array pool.Free(v); SplitHorizontal(space, input, output, h, pool, map); } else { //Free the unused array pool.Free(h); SplitVertical(space, input, output, v, pool, map); } }
private static void SplitVertical(BoundingRectangle space, INode <T> input, Node <T> output, float[] sizes, ArrayPool <float> pool, Treemap <T> map) { output.SplitVertical = true; ApplySplit(space, input, output, sizes, pool, map); }
private static void ApplySplit(BoundingRectangle space, INode <T> input, Node <T> output, float[] sizes, ArrayPool <float> pool, Treemap <T> map) { //Create child nodes and add them to the parent var index = 0; foreach (var child in input) { output.Add(new Node <T>(map, child.Value, sizes[index++])); } //Now that we're done with the array, free it pool.Free(sizes); //Recursively subdivide each child for (var i = 0; i < output.Count; i++) { var iNode = input[i]; var oNode = output[i]; //Calculate bounds for this node (the bounds are not in the right place, they are the right *size* which is the only thing we care about here) var box = output.SplitVertical ? new BoundingRectangle(new Vector2(0, space.Min.Y), new Vector2(oNode.Length, space.Max.Y)) : new BoundingRectangle(new Vector2(space.Min.X, 0), new Vector2(space.Max.X, oNode.Length)); //Recursion DivideNode(box, iNode, oNode, pool, map); } }