Move STL object in matlab

10 ビュー (過去 30 日間)
Daniel Giles
Daniel Giles 2019 年 2 月 25 日
編集済み: DGM 2025 年 7 月 12 日
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 件のコメント
Erin Rezich
Erin Rezich 2020 年 3 月 26 日
I have this same exact question!

サインインしてコメントする。

回答 (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!
  1 件のコメント
DGM
DGM 2025 年 7 月 11 日
編集済み: DGM 2025 年 7 月 12 日
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);
Error using patch
Not enough input arguments.
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
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')

カテゴリ

Help Center および File ExchangeSTL (STereoLithography) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by