Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Best fit plane

kdee122kdee122 Member, csevp Posts: 2 PRO
Currently, in onshape a plane can be made from 3 points. Is there a method to use more than 3 points of a non-planar surface to create a "best-fit" plane?
Tagged:

Best Answer

Answers

  • Jed_YeiserJed_Yeiser Member Posts: 38 PRO
    @kdee122 this is possible with featurescript, though a better understanding of what you mean by 'best fit' would be helpful. I wrote this featurescript quickly as a guide. 

    https://cad.onshape.com/documents/e07586cc7fffdd615af9086d/w/32107f51e26ed66715308556/e/a89ed3bf1988fac1cb746969?renderMode=0&uiState=6674ee6a607f8261b36ef812

  • kdee122kdee122 Member, csevp Posts: 2 PRO
    Answer ✓
    @kdee122 this is possible with featurescript, though a better understanding of what you mean by 'best fit' would be helpful. I wrote this featurescript quickly as a guide. 

    https://cad.onshape.com/documents/e07586cc7fffdd615af9086d/w/32107f51e26ed66715308556/e/a89ed3bf1988fac1cb746969?renderMode=0&uiState=6674ee6a607f8261b36ef812


    This is exactly what i needed!!!! jed thanks for the feature script and I will take a look at the code, made it sound like it was a easy featurescript to write. I need to learn to make or at least modify feature scripts.
  • Jed_YeiserJed_Yeiser Member Posts: 38 PRO
    @kdee122 - it'd be worth checking out this link which I used as a reference. It's the same process as solving for the 'best fit polynomial' (A plane is just an equation, afterall!) using a least squares fit. My code places the plane origin at the 'center of mass' of the points and applies a uniform weight to all points. Depending on how you'd like to define your 'best fit' I could see adding quite a bit of functionality here (weighting by distance from COM, for instance). 

    All of this said, I can't think of an application where I would WANT to use a plane defined like this. Good luck!
  • kenn_sebesta167kenn_sebesta167 Member Posts: 65 ✭✭
    edited November 28

    Thank you, that is absolutely what I needed.

    I can't think of an application where I would WANT to use a plane defined like this

    I use it as an integral part of building a Part Studio around a 3D scan.

    The cloud points are precise, but still slightly imperfect. Using your script, I was able to choose many points and get the absolute best fit. This was a huge time savings and quality improvement from my prior process: choosing a three point plane and then having to go back and deselect/select individual points until I felt I had achieved best fit.

    The only thing I wish could be different about using BFPfP is to be able to still see the points when I click "Final". (They all disappear from view, as in the below screenshot)

    it'd be worth checking out this link which I used as a reference. It's the same process as solving for the 'best fit polynomial' (A plane is just an equation, afterall!) using a least squares fit. 

    =================================================

    One note of caution is that the LSQ approach to minimizing z distance is perhaps suboptimal for the problem. It works well, but a more perfect solution would be to minimize perpendicular distance. This would be more robust when the user is working with points which have little x-y variance.

    I'm not super familiar with FS, but here's the Matlab code to find the plane which minimizes the perpendicular distance of the points from the plane:

    % Assume we have 'userPoints', a 3xN array of user-selected 3D points
    
    % Find the mean of each of X, Y, and Z.
    meanX = mean(userPoints(1,:));
    meanY = mean(userPoints(2,:));
    meanZ = mean(userPoints(3,:));
    
    % Subtract the mean. Take (unfortunate) advantage of Matlab's implicit 
    % expansion to mix the 3xN matrix with the 3x1 vector
    mesh_points = userPoints - [meanX; meanY; meanZ];
    
    % Take the SVD
    [U,S,Vt] = svd(mesh_points);
    
    % Find the index of the minimum singular value. Since Matlab returns the
    % eigenvalue as a matrix, we look only at that matrix's diagonal.
    [minVal, idx] = min(diag(S));
    
    % Find the left singular value vector which corresponds to this singular
    % value
    U(:,idx);
    
    % This vector is the normal to the plane, so it's elements [A, B, C] are
    % the equations for the plane A(x-xMean) + B(y-yMean) + C(z-zMean) = 0;
    

  • kenn_sebesta167kenn_sebesta167 Member Posts: 65 ✭✭
    edited November 29

    UPDAET: thanks to @Jed_Yeiser's script I was able to modify my code to use SVD. I published it to the Custom FS list, please let me know if there's any feedback or feature requests.

    https://cad.onshape.com/documents/3214428d3d799be304e66fd9/w/83efbb2135152269c6209e10/e/90d14bf609df1d8351b029a5

    (In a v2, it would be interesting to try using RANSAC, as in https://math.stackexchange.com/a/4574602/222880.)

  • MDesignMDesign Member Posts: 248 ✭✭✭

    OMG. Thank you for this…

    @kenn_sebesta167 @Jed_Yeiser

    . Something I've been searching for. This will help so much with ocd of trying to match up with import scans at least for flat areas.. Now if we can get something similar for non planer surfaces…we'll be cookin'!!

  • kenn_sebesta167kenn_sebesta167 Member Posts: 65 ✭✭

Sign In or Register to comment.