Code covered by the BSD License  

Highlights from
Plane Fitting and Normal Calculation

5.0

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

Plane Fitting and Normal Calculation

by

 

09 Aug 2012 (Updated )

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.

Sample Call
> data = rand(50,3)*100;
> data(:,3) = data(:,1) * .4 + data(:,2) * .6 + rand(50,1);
> n = fitNormal(data,1)

Acknowledgements

This file inspired Adjust Plane To Given Normal and Plane Fit.

Required Products MATLAB
MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
02 Oct 2013 Arturo Moncada-Torres  
25 Aug 2013 Dan Couture

No Problem. Thanks for the feedback. A new version was just submitted and should be accepted soon. If you need to you can also pull it from git https://github.com/MathYourLife/Matlab-Tools

23 Aug 2013 Amir Masoud

You are right, I used the normal vector and plot the line on my plane. Thanks for your response.

22 Aug 2013 Dan Couture

Thank for the feedback Amir. It's likely that your data doesn't fit in the window set by the code. It's hard coded now for [-1, 1] as you can see on this line.

set(get(L, 'Parent'),'DataAspectRatio',[1 1 1],'XLim',[-1 1],'YLim',[-1 1],'ZLim',[-1 1]);

I'll see if I can get that updated so that it jumps to wherever you data set is. I'll probably keep the range of all the axes equal though or it will make the plot appear as though the solution does not come out to be normal to the plane.

22 Aug 2013 Amir Masoud

Thanks nicely does the job, I checked the normal on my data and it is correct. I just got an empty axis for plot feature which I do not have any idea why! Any thing to take care of, that I missed?

09 Jul 2013 Dan Couture

Hi Sharif,

Thanks for the question. Could you elaborate a little on the angle you are measuring? Is it between the normal and one of the coordinate axes? or the angle between multiple normal vectors? Since the planes being fit have two normal vectors the function will produce only one of them. If possible, could you provide a sample data set that you would expect to give you an angle greater than 135 degrees?

Dan

03 Jul 2013 Sharif

Hi Dan,
thanks for sharing a code.

i am using your code to fit planes to contact patches between particles.

I am finding that the normal of the fitted never exceeds 135 degree's. any ideas as to why this may be?
thanks,
sharif

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.

26 Aug 2013

Graph scaling to the data provided

Contact us