Hi, I want to how can we convert the contour geometry to stl file?

조회 수: 7 (최근 30일)
ArxIv
ArxIv 2024년 10월 26일
댓글: ArxIv 2025년 7월 26일
Hello,
I would like to know how can we convert 2D-geometry I obtained from isocontour of the signed distance function to stl.file.
This is because I want to generate triangular mesh for the inside of the isocontour using "generateMesh" function.
For example, I need to mesh the inside of the circular domain shown in contour.png. The isocontour is plottted using the code below.
Plus, I would be happy if you could inform me how to conduct this meshing without stl.file.
clc; clear; close all;
% Generating simple SDF
[x, y] = meshgrid(linspace(-2, 2, 100), linspace(-2, 2, 100));
sdf = sqrt(x.^2 + y.^2) - 1;
% Extracting 0-level isocontour of the SDF.
isoValue = 0;
contour(x(1,:), y(:,1), sdf, [isoValue isoValue]);

채택된 답변

Star Strider
Star Strider 2024년 10월 26일
Try something like this —
% clc; clear; close all;
% Generating simple SDF
[x, y] = meshgrid(linspace(-2, 2, 100), linspace(-2, 2, 100));
sdf = sqrt(x.^2 + y.^2) - 1;
% Extracting 0-level isocontour of the SDF.
isoValue = 0;
c = contour(x(1,:), y(:,1), sdf, [isoValue isoValue]);
axis('equal')
x = c(1,2:end).'; % First Row Of ‘c’ Are The ‘X’ Coordinates, Delete The First Element From Each
y = c(2,2:end).'; % Second Row Of ‘c’ Are The ‘Y’ Coordinates, Delete The First Element From Each
DT = delaunay(x,y); % Delaunay Triangulation
Warning: Duplicate data points have been detected and removed.
Some point indices will not be referenced by the triangulation.
DT = 198×3
74 71 65 26 24 37 15 46 24 65 62 61 196 96 15 115 108 107 78 76 83 96 196 115 165 196 174 15 21 18
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
triplot(DT, x, y) % Plot Result
axis('equal','padded')
TR = triangulation(DT,x,y); % Create ‘triangulation’ Object
Warning: Some input points are not referenced by the triangulation.
TR =
triangulation with properties: Points: [201x2 double] ConnectivityList: [198x3 double]
stlwrite(TR, 'STL_Test.stl', 'text') % Write Triangulation Object To STL File
TRr = stlread('STL_Test.stl') % Check: Read Triangulation Object From STL File
TRr =
triangulation with properties: Points: [200x3 double] ConnectivityList: [198x3 double]
boundaryEdges = freeBoundary(TRr).'; % Get Boundary Edges
figure
triplot(TRr) % Plot ‘triangulation’ Objeect
hold on
plot(TRr.Points(boundaryEdges,1), TRr.Points(boundaryEdges,2), '-r', 'LineWidth',2) % Plot Boundary
hold off
axis('equal','padded')
.
  댓글 수: 6
DGM
DGM 2025년 7월 18일
편집: DGM 2025년 7월 19일
I'm going to use a more complicated example scenario, but the original example with the conical surface is included and does work.
%% choose your data and levels
% Generating simple SDF
%[x, y] = meshgrid(linspace(-2,2,100)); % original annulus example
%sdf = sqrt(x.^2 + y.^2) - 1;
[x y sdf] = peaks(100); % multiple regions, dissimilar lengths
% pick two specific levels
%contourlevels = [-0.5 0]; % for first example
contourlevels = [1.5 2.5]; % for second example
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get contours for two specific levels
figure(1)
[C,hc] = contour(x(1,:),y(:,1),sdf,contourlevels);
axis equal; grid on
% get vertex sequences from contours
% Vc is a cell array of [x y] vertex lists describing individual curves,
% each column in Vc contains the curves for a particular level
for kl = 1:numel(contourlevels) % index over each level
startidx = find(C(1,:) == contourlevels(kl)); % start of each curve
for kc = 1:numel(startidx) % index over each curve in this level
blockstart = startidx(kc); % the starting index for this block
lenv = C(2,blockstart); % the length of this curve + 1
Vc{kc,kl} = C(:,blockstart+(2:lenv)).'; % C is [level, x; length, y]
end
end
% reshape the cell array to a single column
% ideally, we should first flip the vertex lists in each column
% such that the interior curves are CW and exterior curves are ccw
% but we don't necessarily know which is which.
% delaunayTriangulation() seems to figure it out without ordering
% but it might not always work as expected.
Vc = Vc(:);
% construct the edge lists (constraints) in the same direction
% the vertex lists in Vc don't need to be the same length
% i'm only presuming these curves are supposed to be closed
% if they're not closed, they will be.
sz = cellfun(@(x) size(x,1),Vc); % length of each vertex list
szc = [0; cumsum(sz)];
E = zeros(0,2);
for k = 1:numel(Vc)
v = 1+szc(k):szc(k+1);
thisE = [v; circshift(v,-1)].';
E = [E; thisE]; %#ok<*AGROW>
end
% TRIANGULATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% do a constrained triangulation to generate the triangles
T = delaunayTriangulation(cell2mat(Vc),E); % use the constraints
F = T.ConnectivityList(isInterior(T),:); % eliminate extraneous faces
V = T.Points;
% write it
T = triangulation(F,V);
stlwrite(T,'test.stl')
% display it using patch()
figure
patch('faces',F,'vertices',V,'facecolor',[1 1 1]*0.8,'edgecolor','k');
axis equal; grid on
My interpretation of the task might be taking some liberties. I'm not sure whether there was a good reason to be using multiple contour objects, so I just used one. I'm assuming that we're only dealing with two contour levels at a time. I also don't know whether we're supposed to just assume that the contours are always closed, or how open contours should be handled. I simply didn't handle them in any special way. I also don't see how we can assume which curve is interior without prior knowledge of convexity. I just let delaunayTriangulation() try to figure it out, but that might not always work.
I bet there's an even more concise way to do this specifically for contourf plots, but that's a blind guess based on an extremely confused understanding of how some of the undocumented descendant graphics objects work. Might be totally wrong about how they work, and in the best case, it would be a fragile, undocumented solution.
ArxIv
ArxIv 2025년 7월 26일
Very insightful. Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by