Code covered by the BSD License  

Highlights from
Plane Fitting and Normal Calculation

5.0

5.0 | 4 ratings Rate this file 62 Downloads (last 30 days) File Size: 36 KB File ID: #37775
image thumbnail

Plane Fitting and Normal Calculation

by Dan Couture

 

09 Aug 2012 (Updated 13 Aug 2012)

Given a set of x,y,z coordinates, find the best planar fit.

| Watch this File

File Information
Description

Given a set of x,y,z coordinates, find the best planar fit to the points via a least squares regression. Testing script "t_fitNormal" is also included to verify functionality and demonstrate usage.

Acknowledgements

This file inspired Adjust Plane To Given Normal.

Required Products MATLAB
MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
fit(3), modelling(2), normal(2), plane(2), plot, regression
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
12 Apr 2013 Bastian Tietjen

Hi Dan,

what can I say, it's what I was looking for (needless to say it works very well).

Cheers!

08 Mar 2013 Dan Couture

Great question Simon.

This utility uses a least squares regression in which the formula minimizes the sum of the squares of the residuals. The residual is the difference between what the model expects and the actual measured value. So in most cases this would be the difference between the predicted z value and the actual z value.

That is all fine and dandy until your data starts to have a steep slope. In the worse case, for a plane with normal in the x-y plane the residuals of z_meas - z_exp trend to +-infinity.

The way the script handles this issue is to perform the regression three times. Each time using residuals measured in the x, y, and z directions (You'll see those as the for i=1:3 loops in the source). After the residuals are performed, if we run into a case similar to the one just described the z residual value will be very large and therefore not trustworthy.

The decision on which residual to use is made on this line.
best_fit = find(residual_sum == min(residual_sum));

Thanks for the feedback. Hope this helped.

08 Mar 2013 Simon  
08 Mar 2013 Simon

Also works well so far for my current application.

The one thing i'm not quite sure about is, if the function minimizes the normal distance of the points to the plane or the distance in z-direction.

Thanks Couture!

28 Feb 2013 Dan Couture

Thanks for the feedback Algar. Glad to hear it's working well for you. Definitely let me know if you run into an edge case that has trouble.

28 Feb 2013 Daniell Algar

This little function works perfect (so far). It's robust, well implemented, and does exactly what I needed in my current application.

Thank you Couture!

23 Sep 2012 Yg

It can work well

23 Sep 2012 Yg  
Updates
13 Aug 2012

Use residuals to determine which of the fits is best and allow for data that is not perfectly planar.

Contact us