Home > matGeom > geom3d > fitPlane.m

fitPlane

PURPOSE ^

FITPLANE Fit a 3D plane to a set of points.

SYNOPSIS ^

function plane = fitPlane(points)

DESCRIPTION ^

FITPLANE  Fit a 3D plane to a set of points.

   PLANE = fitPlane(POINTS)

   Example
     pts = randn(300, 3);
     pts = transformPoint3d(pts, createScaling3d([6 4 2]));
     pts = transformPoint3d(pts, createRotationOx(pi/6));
     pts = transformPoint3d(pts, createRotationOy(pi/4));
     pts = transformPoint3d(pts, createRotationOz(pi/3));
     pts = transformPoint3d(pts, createTranslation3d([5 4 3]));
     elli = equivalentEllipsoid(pts);
     figure; drawPoint3d(pts); axis equal;
     hold on; drawEllipsoid(elli, ...
         'drawEllipses', true, 'EllipseColor', 'b', 'EllipseWidth', 3);
     plane = fitPlane(pts);
     drawPlane3d(plane, 'm');

   See also
     planes3d, equivalentEllipsoid, fitLine3d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function plane = fitPlane(points)
0002 %FITPLANE  Fit a 3D plane to a set of points.
0003 %
0004 %   PLANE = fitPlane(POINTS)
0005 %
0006 %   Example
0007 %     pts = randn(300, 3);
0008 %     pts = transformPoint3d(pts, createScaling3d([6 4 2]));
0009 %     pts = transformPoint3d(pts, createRotationOx(pi/6));
0010 %     pts = transformPoint3d(pts, createRotationOy(pi/4));
0011 %     pts = transformPoint3d(pts, createRotationOz(pi/3));
0012 %     pts = transformPoint3d(pts, createTranslation3d([5 4 3]));
0013 %     elli = equivalentEllipsoid(pts);
0014 %     figure; drawPoint3d(pts); axis equal;
0015 %     hold on; drawEllipsoid(elli, ...
0016 %         'drawEllipses', true, 'EllipseColor', 'b', 'EllipseWidth', 3);
0017 %     plane = fitPlane(pts);
0018 %     drawPlane3d(plane, 'm');
0019 %
0020 %   See also
0021 %     planes3d, equivalentEllipsoid, fitLine3d
0022 %
0023 
0024 % ------
0025 % Author: David Legland
0026 % E-mail: david.legland@inrae.fr
0027 % Created: 2012-11-11, using Matlab 7.9.0.529 (R2009b)
0028 % Copyright 2012-2024 INRA - Cepia Software Platform
0029 
0030 % number of points
0031 n = size(points, 1);
0032 
0033 % compute centroid
0034 center = mean(points);
0035 
0036 % compute the covariance matrix
0037 covPts = cov(points)/n;
0038 
0039 % perform a principal component analysis with 2 variables,
0040 % to extract inertia axes
0041 [U, S] = svd(covPts);
0042 
0043 % sort axes from greater to lower
0044 [dummy, ind] = sort(diag(S), 'descend'); %#ok<ASGLU>
0045 
0046 % format U to ensure first axis points to positive x direction
0047 U = U(ind, :);
0048 if U(1,1) < 0
0049     U = -U;
0050     % keep matrix determinant positive
0051     U(:,3) = -U(:,3);
0052 end
0053 
0054 plane = [center U(:,1)' U(:,2)'];

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022