Fit surface to curves
Show older comments
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
William Rose
on 4 Jun 2025
@Daniel Martin, post the data please...
Daniel Martin
on 4 Jun 2025
Mathieu NOE
on 4 Jun 2025
hello
tried also a few things but it failed to get the expected results
think this fex submission is worth to try : Surface Reconstruction from scattered points cloud (open surfaces) - File Exchange - MATLAB Central
(still I need to test it for a complex shape that intersect or fold on itself)
anyway , like other approaches based on Delaunay triangulation, the result will be best if your data is more uniformly distributed like a cloud (as in the demo files).
unfortunately, your data looks like a skeleton with a lot of empty areas . I wished there would be more dots between the lines
do you have control over that generation of data ? maybe you can reduce the point density in each "line" (we can probably get a good result with 10 times less points) and increase the number of lines ...
Daniel Martin
on 5 Jun 2025
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
More Answers (0)
Categories
Find more on Surface and Mesh Plots 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!
