3D Least Squares Plane

后端 未结 9 2252
遥遥无期
遥遥无期 2020-11-27 13:56

What\'s the algorithm for computing a least squares plane in (x, y, z) space, given a set of 3D data points? In other words, if I had a bunch of points like (1, 2, 3), (4, 5

9条回答
  •  遥遥无期
    2020-11-27 14:29

    See 'Least Squares Fitting of Data' by David Eberly for how I came up with this one to minimize the geometric fit (orthogonal distance from points to the plane).

    bool Geom_utils::Fit_plane_direct(const arma::mat& pts_in, Plane& plane_out)
    {
        bool success(false);
        int K(pts_in.n_cols);
        if(pts_in.n_rows == 3 && K > 2)  // check for bad sizing and indeterminate case
        {
            plane_out._p_3 = (1.0/static_cast(K))*arma::sum(pts_in,1);
            arma::mat A(pts_in);
            A.each_col() -= plane_out._p_3; //[x1-p, x2-p, ..., xk-p]
            arma::mat33 M(A*A.t());
            arma::vec3 D;
            arma::mat33 V;
            if(arma::eig_sym(D,V,M))
            {
                // diagonalization succeeded
                plane_out._n_3 = V.col(0); // in ascending order by default
                if(plane_out._n_3(2) < 0)
                {
                    plane_out._n_3 = -plane_out._n_3; // upward pointing
                }
                success = true;
            }
        }
        return success;
    }
    

    Timed at 37 micro seconds fitting a plane to 1000 points (Windows 7, i7, 32bit program)

    enter image description here

提交回复
热议问题