Code covered by the BSD License

### Highlights fromPlane 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

# Plane Fitting and Normal Calculation

09 Aug 2012 (Updated 13 Aug 2012)

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

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
Tags I've Applied
12 Apr 2013

Hi Dan,

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

Cheers!

08 Mar 2013

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
08 Mar 2013

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

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

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

It can work well

23 Sep 2012
13 Aug 2012

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