Move STL object in matlab
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am currently working on a project that requires me to create a 3D world that will include apples, trees, mountains, and more. However, although I have had success importing and rendering the .stl files in matlab, I am having trouble figuring out how to actually set the position of these objects in a 3D space, as this will be extremely necessary in creating the world. This is the current code to import an apple .stl file by running renderSTL('Apple.stl') with the function below. Any help here would be greatly appreciated.
function renderSTL(fileName)
% Import an STL mesh, returning a PATCH-compatible face-vertex structure
fv = stlread(fileName);
%All code below is for rendering the 3D object
patch(fv,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);
% Add a camera light, and tone down the specular highlighting
camlight('headlight');
material('dull');
% Fix the axes scaling, and set a nice view angle
axis('image');
view([-135 35]);
end
1 comentario
Respuestas (2)
Jack Stewart
el 27 de Mzo. de 2020
As I understand it, variables of type 'triangulation' cannot directly be manipulated. However, you can work around this by breaking the triangulation down into two separate variables: one for triangulation.Points and one for triangulation.ConnectivityList. In 3D, triangulation.Points will return an nx3 array where the first column is the x value of each vertex, the second is the y, and the third is the z - which can then be manipulated to shift your object. Triangulation.ConnectivityList contains information about which vertices form triangles, and will be left alone to preserve the original object.
For example, if you wanted to shift your 'fv' triangulation by 5 units in the x-direction:
P = fv.Points; %access the vertex data from triangulation
C = fv.ConnectivityList; %access the connectivity data from triangulation
P(:,1) = P(:,1) + 5; %add 5 to each vertex's x value
fv = triangulation(C, P); %Combine both components back into a triangulation variable
There might be a more concise way to do this, but I hope this helps!
1 comentario
DGM
el 11 de Jul. de 2025
Editada: DGM
el 12 de Jul. de 2025
This is correct, but if OP's code was working, they weren't using stlread(); they were using stlread(). How do I know?
% Import an STL mesh, returning a PATCH-compatible face-vertex structure
unzip annularspacer15.stl.zip % for the forum
fv = stlread('annularspacer15.stl');
%All code below is for rendering the 3D object
patch(fv,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);
Since OP's code was working, we know that fv is a struct, not a triangulation() object. Off the top of my head, FEX #22409 is the only reader which supports that output. EDIT: now that I mention it, darova's example is also using #22409.
The same concept would have applied, but it would be a little simpler since the struct isn't read-only like a triangulation object.
darova
el 26 de Mzo. de 2020
Example: the code for selecting pink object
fv = stlread(fileName); % extract data
v = fv.vertices; % vertices [x y z] columns
f = fv.faces; % faces
x = v(:,2); % get X data
ix = sum(x(f) < 2,2)>2; % find faces where 3 points X<2
patch('faces',f(ix,:),'vertices',v(ix,:),'facecolor','r')

0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!