Fit surface to curves
18 views (last 30 days)
Show older comments
Daniel Martin
on 4 Jun 2025
Commented: Mathieu NOE
on 10 Jun 2025 at 7:30
Hey everybody :)
I'm slowly losing my mind trying to create a surface from curves plotted in 3D. I've already tried:
- 2D Delaunay
- Alpha‐Shape masking
- Lofting between constant curves
- 2D grid interpolation + “fillmissing”
- Thin‐plate spline / RBF interpolation
- Edge‐length filtering on 2D Delaunay
- 3D Delaunay + FreeBoundary
- Constant‐x / constant‐y triangle removal
but I can't quite get it working. The last two points combined result in a decent surface (see code and picture below).
DT3 = delaunayTriangulation(X_all, Y_all, Z_all);
% Only outer triangles (FreeBoundary)
[FV, Pts] = freeBoundary(DT3);
% Coordinates of edges
I1 = FV(:,1); I2 = FV(:,2); I3 = FV(:,3);
x1 = Pts(I1,1); y1 = Pts(I1,2);
x2 = Pts(I2,1); y2 = Pts(I2,2);
x3 = Pts(I3,1); y3 = Pts(I3,2);
tol = 1e-6;
% Mask if triangle is in x-z or y-z-plane
allSameX = (abs(x1 - x2) < tol) & (abs(x2 - x3) < tol);
allSameY = (abs(y1 - y2) < tol) & (abs(y2 - y3) < tol);
keepTri = ~(allSameX | allSameY);
FV = FV(keepTri, :);
% Plot
hold on;
trisurf(FV, Pts(:,1), Pts(:,2), Pts(:,3), Pts(:,3), ...
'EdgeColor','none', ...
'FaceColor','interp', ...
'FaceAlpha',0.6);

The problems are the loops and leaning of the curves. No matter what I do, I either get surfaces in all of the loops and overhangs as well or like in this case a surface, which isn't fitting the curves. Below you can see the 2D delaunay, which is really good, but somehow the part of the code, that is supposed to delete triangles in the x-z or y-z-plane isn't working.
% 2D Delaunay triangulation
tri = delaunay(X_all, Y_all);
% 2) Compute corner indices and coordinates for each triangle
I1 = tri(:,1); I2 = tri(:,2); I3 = tri(:,3);
x1 = X_all(I1); y1 = Y_all(I1);
x2 = X_all(I2); y2 = Y_all(I2);
x3 = X_all(I3); y3 = Y_all(I3);
% 3) Mark any triangle whose three values are (almost) identical
tol = 1e-6;
allSameX = (abs(x1 - x2) < tol) & (abs(x2 - x3) < tol);
allSameY = (abs(y1 - y2) < tol) & (abs(y2 - y3) < tol);
% Keep only those triangles that are neither “allSameX” nor “allSameY”
keepTri = ~(allSameX | allSameY);
tri = tri(keepTri, :);
% 5) Plot the filtered surface (trisurf)
trisurf(tri, X_all, Y_all, Z_all, Z_all, ...
'EdgeColor', 'none', ...
'FaceColor', 'interp', ...
'FaceAlpha', 0.6);

I would really appreciate your tips and help on how to get a good surface fitting to those curves. If you need any further info, please just let me know. Thank you so much in advance!
Edit: Added data files.
5 Comments
Mathieu NOE
on 5 Jun 2025
if that is too complicated , we can also manage the density from within matlab (we can downsample your data by picking only every k samples)
more lines would be beneficial for sure !
Accepted Answer
Mathieu NOE
on 4 Jun 2025
well , maybe with boundary (shrink factor = 1) and trisurf we have a simple solution - maybe not perfect but visually quite close to what we want (or am I wrong ? )
my result :

load('X_all.mat')
load('Y_all.mat')
load('Z_all.mat')
figure
plot3(X_all,Y_all,Z_all,'b.')
hold on
k = boundary(X_all,Y_all,Z_all,1);
trisurf(k,X_all,Y_all,Z_all,'FaceAlpha',0.2,'EdgeColor','none');
view([45 25])
hold off
4 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!