Skip to content

Fixed KD_tree for 3D (maybe) #6

Description

@graeioume

I fixed the dimensions on the usage of KDTree to accomodate 2D and 3D.
However, I am still having other issues getting this to work.
Result TBD: I will edit with additional information as I go.

        public static Matrix<double> KD_tree(Matrix<double> M_points, Matrix<double> P_points, bool _3D)
        {
            int Np = P_points.ColumnCount;
            int dimensions = _3D ? 3 : 2;
            //create tree of M_points
            var treeData2 = M_points.ToColumnArrays();
            var treeNodes = treeData2.Select(p => p.ToString()).ToArray();
            var m = Matrix<double>.Build;
            Matrix<double> Y = m.Dense(3, Np, 0);
            var tree = new KDTree<double, string>(dimensions, treeData2, treeNodes, L2Norm);
            var scan_data = P_points.ToColumnArrays();
            //Calculate nearest neighbour for every element of cloud
            for (int i = 0; i < scan_data.Length; i++)
            {
                var num = scan_data[i];
                var test = tree.NearestNeighbors(scan_data[i], 1);
                Y[0, i] = test[0].Item1[0];
                Y[1, i] = test[0].Item1[1];
            }
            return Y;//return result matrix

            // this function is used for calculating the Euclidean norm
            double L2Norm(double[] x, double[] y)
            {
                //square root is not used to speed up
                return x.Select((t, i) => (t - y[i]) * (t - y[i])).Sum();
            }
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions