Converting an STL file to a 3D Surface/Equation

78 views (last 30 days)
Arafat Asghar
Arafat Asghar on 26 Oct 2024 at 9:50
Commented: Skye on 28 Oct 2024 at 11:56
Given an arbitrary 3D CAD model (in STL, STEP format), is it possible to convert the model into a function , where x and y are points on the 3D model/surface? In other words, how do we fit an equation on an arbitrary 3D CAD model/surface?
I convert the STL file into a point cloud using CloudCompare software and then export the pointcloud to a .txt. From the txt file, I obtain the coordinates, and finally, I obtain surface fit using the cftool (curve fitting toolbox of MATLAB). The result is nowhere same as the original STL file. I have tried using all available interpolation options such as:
  1. Nearest neighbor
  2. Linear
  3. Cubic spline
  4. Biharmonic
  5. Thin-plate spline
The curve fitting code is enclosed herewith:
function fitresult = createFit(Xvec, Yvec, Zvec)
%CREATEFIT(XVEC,YVEC,ZVEC)
% Create a fit.
%
% Data for 'curve1' fit:
% X Input: Xvec
% Y Input: Yvec
% Z Output: Zvec
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 02-Aug-2024 11:41:46
%% Fit: 'curve1'.
[xData, yData, zData] = prepareSurfaceData( Xvec, Yvec, Zvec );
% Set up fittype and options.
%ft = 'nearestinterp';
%opts = fitoptions( 'Method', 'NearestInterpolant' );
%opts.ExtrapolationMethod = 'nearest';
ft = 'linearinterp';
opts = fitoptions( 'Method', 'LinearInterpolant' );
opts.ExtrapolationMethod = 'none';
%ft = 'thinplateinterp';
%opts = fitoptions( 'Method', 'ThinPlateInterpolant' );
%opts.ExtrapolationMethod = 'thinplate';
%opts.Normalize = 'on';
% Fit model to data.
[fitresult, ~] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
%figure( 'Name', 'curve1' );
%h = plot( fitresult, [xData, yData], zData );
%legend( h, 'curve1', 'Zvec vs. Xvec, Yvec', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
%xlabel( 'Xvec', 'Interpreter', 'none' );
%ylabel( 'Yvec', 'Interpreter', 'none' );
%zlabel( 'Zvec', 'Interpreter', 'none' );
Please also find enclosed the cartesian point cloud and stl file in the following links:
  1 Comment
Skye
Skye on 28 Oct 2024 at 11:56
I faced a similar issue trying to convert a 3D CAD model into a function using MATLAB. I tried various interpolation methods in cftool, but the results weren’t close to the original STL surface. I eventually reached out to www.matlabassignmentexperts.com for help, and they guided me through a customized approach that worked far better than the standard options. Their experts helped me with advanced surface-fitting techniques and tailored MATLAB solutions, which were exactly what I needed!
If anyone else is facing challenges with 3D modeling in MATLAB, I’d recommend contacting them – they’re really responsive. You can reach them via WhatsApp at [+1 (315) 557-6473] or email at info@matlabassignmentexperts.com.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 26 Oct 2024 at 12:41
Um, I think you misunderstand a few things.
It seems the STL model actually has thickness. So you have exported the entire thing, as a set of points, both to and bottom. We see that in the point cloud as what looks like almost noise. I'm not sure how to otherwise explain what is clearly a fuzzy cloud of points in the point cloud you plotted. Or, maybe the point cloud tool you used just generated some random set of points. We are not told. And that means the curve, at best, wants to pass through the middle of that cloud.
And of course, we don't see your data, or what you passed in. So we cannot even inpect it carefully to learn anything.
Next, can you just create an "equation" for any general point cloud? Well, no. Not such a trivial thing to do. Especially since your data has regions where that fitted model will have sharp derivative discontinuities, creases in the surface. And that means using something like a thin plate spline is going to generate complete crap. Such a curve tries to be smooth everythere.
At the other end of the fitting spectrum is a nearest neighbor interpolant. Now you have something that just grabs the nearest point from the point cloud, so effectively no interpolation at all.
The other methods will also have problems. I'm not sure why you are trying to do this, but you are not going to succeed.
  1 Comment
Arafat Asghar
Arafat Asghar on 27 Oct 2024 at 3:23
Edited: Walter Roberson on 27 Oct 2024 at 4:14
Thank you @John D'Errico for the detailed and insightful response. I have already shared link to the STL file as well as extracted point cloud data (as a .mat file) on my Google Drive. Please let me know if there is some problem with accessing or opening the file.
I am basically investigating sheet metal deformation in which the STL file represents the target or desired shape/deformation. I want to compare the target shape with the obtained geometry/shape (in terms of nodal coordinates) from an Abaqus Finite Element simulation or an experimental STL model obtained using a laser/optical scanner. The idea is basically to transform the STL model into an equation for a point-wise comparison on the grid/mesh. The CloudCompare software converts the STL model into a point cloud by sampling points on the mesh.
I tried an alternative method using the importGeometry command as follows:
clearvars
clc
close all
%%
geometry_shape = importGeometry('Target_Geom_Modified.STL');
%geometry_shape =
%DiscreteGeometry with properties:
% NumCells: 1
% NumFaces: 24
% NumEdges: 47
% NumVertices: 31
% Vertices: [31×3 double]
geometry_shape_coord = geometry_shape.Vertices;
Xcoord = geometry_shape_coord(:,1);
Ycoord = geometry_shape_coord(:,2);
Zcoord = geometry_shape_coord(:,3);
cftool
However, the obtained shape still does not match the original shape as evident in the following figure.

Sign in to comment.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!