Converting an STL file to a 3D Surface/Equation
73 views (last 30 days)
Show older comments
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:
- Nearest neighbor
- Linear
- Cubic spline
- Biharmonic
- 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
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.
Answers (1)
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
on 27 Oct 2024 at 3:23
Edited: Walter Roberson
on 27 Oct 2024 at 4:14
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!