Hi, I want to how can we convert the contour geometry to stl file?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ArxIv
el 26 de Oct. de 2024
Comentada: ArxIv
el 26 de Jul. de 2025
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]);
0 comentarios
Respuesta aceptada
Star Strider
el 26 de Oct. de 2024
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
figure
triplot(DT, x, y) % Plot Result
axis('equal','padded')
TR = triangulation(DT,x,y); % Create ‘triangulation’ Object
stlwrite(TR, 'STL_Test.stl', 'text') % Write Triangulation Object To STL File
TRr = stlread('STL_Test.stl') % Check: Read Triangulation Object From STL File
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 comentarios
DGM
el 18 de Jul. de 2025
Editada: DGM
el 19 de Jul. de 2025
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







