Beispiel #1
0
        /// <summary>
        ///   Creates a new space-partitioning tree from the given points.
        /// </summary>
        ///
        /// <param name="points">The points to be added to the tree.</param>
        ///
        /// <returns>A <see cref="SPTree"/> populated with the given data points.</returns>
        ///
        public static SPTree FromData(double[][] points)
        {
            int D    = points.Columns();
            int N    = points.Rows();
            var tree = new SPTree(D);

            // Compute mean, width, and height of current map (boundaries of SPTree)
            var mean_Y = new double[D];
            var min_Y  = new double[D];
            var max_Y  = new double[D];

            for (int d = 0; d < D; d++)
            {
                min_Y[d] = Double.MaxValue;
                max_Y[d] = Double.MinValue;
            }

            for (int n = 0; n < N; n++)
            {
                for (int d = 0; d < D; d++)
                {
                    mean_Y[d] += points[n][d];
                    if (points[n][d] < min_Y[d])
                    {
                        min_Y[d] = points[n][d];
                    }
                    if (points[n][d] > max_Y[d])
                    {
                        max_Y[d] = points[n][d];
                    }
                }
            }

            for (int d = 0; d < D; d++)
            {
                mean_Y[d] /= (double)N;
            }

            // Construct SPTree
            var width = new double[D];

            for (int d = 0; d < D; d++)
            {
                width[d] = System.Math.Max(max_Y[d] - mean_Y[d], mean_Y[d] - min_Y[d]) + 1e-5;
            }

            tree.Root = new SPTreeNode(tree, null, 0, mean_Y, width);
            for (int i = 0; i < points.Length; i++)
            {
                tree.Root.Add(points[i]);
            }

            return(tree);
        }
Beispiel #2
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="SPTreeNode"/> class.
        /// </summary>
        ///
        /// <param name="owner">The tree that this node belongs to.</param>
        /// <param name="parent">The parent node for this node. Can be null if this node is the root.</param>
        /// <param name="corner">The starting point of the spatial cell.</param>
        /// <param name="width">The widths of the spatial cell.</param>
        /// <param name="index">The index of this node in the children collection of its parent node.</param>
        ///
        public SPTreeNode(SPTree owner, SPTreeNode parent, int index, double[] corner, double[] width)
            : base(index)
        {
            int D = owner.Dimension;

            this.owner     = owner;
            Parent         = parent;
            this.Index     = index;
            boundary       = new SPCell(corner, width);
            center_of_mass = new double[D];
            buff           = new double[D];
        }