Move STL object in matlab

58 次查看(过去 30 天)
Daniel Giles
Daniel Giles 2019-2-25
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

回答(2 个)

Jack Stewart
Jack Stewart 2020-3-27
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!

darova
darova 2020-3-26
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')

标签

Community Treasure Hunt

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

Start Hunting!

Translated by