Borrar filtros
Borrar filtros

How to orient multiple STL files the same way?

22 visualizaciones (últimos 30 días)
Elizabeth Kolb
Elizabeth Kolb el 2 de Jul. de 2021
Comentada: Elizabeth Kolb el 6 de Jul. de 2021
Hello! I need to take in multiple STL files and orient the 3D model from them to appear the same way each time (i.e: rotating and flipping 3D models so they all look the same, despite some slight size changes).
For instance, I'd like to make the right image get automatically rotated/flipped around to look like the left image. Dragging the plot to a viewpoint that's the same as the left image doesn't give the same output like one would think. The coordinates are completely different. Say, for instance, the far left point circled in red on the left image would have the coords (-143, 52, 2). The right image would be (18, -89, 2).
I've seen it attempted with pictures in this post, but I'm unsure how to do it with an STL file/3D model: https://www.mathworks.com/matlabcentral/answers/406732-how-it-is-possible-to-change-all-images-orientation-to-same-i-e-to-vertical?s_tid=srchtitle
I've also used this video to help me rotate the file, but it didn't seem to help me in the way that I wanted: https://www.youtube.com/watch?v=7cGqKRL1lC0
I really appreciate any sort of help or anywhere to look to do this sort of thing. :) Thank you!
  4 comentarios
Chunru
Chunru el 5 de Jul. de 2021
There are few possible ways:
  • Modify the code to do rotation around x-axis;
  • Have a double loop to rotate both around x- and z-axis to find the best match;
  • For a more general orientation, you can find a bounding-box of the object and then orientate the bounding box (instead of the object): https://www.mathworks.com/matlabcentral/fileexchange/18264-minimal-bounding-box
Elizabeth Kolb
Elizabeth Kolb el 6 de Jul. de 2021
Thank you!!! I'll be doing that right now. :) I highly appreciate the help!

Iniciar sesión para comentar.

Respuesta aceptada

Chunru
Chunru el 3 de Jul. de 2021
%%
clc
clear
close all
figure(100)
subplot(131)
model = stlread('wristband_1.0.stl'); %Reads STL
trimesh(model); %Plots STL
title('STL')
axis equal %Sets axis as equal on all sides (x,y,z)+stops it from protruding
%view(30,40); %Sets view along certain points
subplot(132)
model2 = stlread('RotatedReduced.stl');
trimesh(model2);
title('STL Rotated Original')
axis equal
x = model.Points(:,1);
y = model.Points(:,2);
z = model.Points(:,3);
xRot = model2.Points(:,1);
yRot = model2.Points(:,2);
zRot = model2.Points(:,3);
%% Align two models
c1 = mean(model.Points);
c2 = mean(model2.Points);
% Translated model (with geometric centre at [0 0 0])
P1 = model.Points - c1;
P2 = model2.Points - c2;
P3 = P2;
% Scaling so that size is similar
s1 = norm(std(P1));
s3 = norm(std(P3));
P3 = P3 * s1/s3;
% Rotate (around z axis) to make the points are close
rotang = (0:360);
d = inf(size(rotang)); % distance between two models
for i=1:length(rotang)
rotm = eye(3);
ca = cosd(rotang(i)); sa = sind(rotang(i));
rotm(1,1) = ca; rotm(2,2)= ca;
rotm(1,2) =-sa; rotm(2,1)= sa;
P3Rot = P3 * rotm';
% d is the difference of the extends of the points
% use 10/90 percentile as distance
d(i) = norm(prctile(P1, [10 90]) - prctile(P3Rot, [10 90]));
end
[~, ii] = min(d); % best rotation angle
rotm = eye(3);
ca = cosd(rotang(ii)); sa = sind(rotang(ii));
rotm(1,1) = ca; rotm(2,2)= ca;
rotm(1,2) =-sa; rotm(2,1)= sa;
P3Rot = P3 * rotm';
subplot(133);
model3 = triangulation(model2.ConnectivityList, P3Rot/(s1/s3)+c1 );
trimesh(model3);
title('Transformed')
axis equal
  2 comentarios
Elizabeth Kolb
Elizabeth Kolb el 3 de Jul. de 2021
Oh this is absolutely wonderful, thank you so much!! This works perfectly and simplifies it down much more than I thought it would. Do you know what variation there would be to the code if it were originally standing upright along the Z-axis to get it to look like the plot of 'STL'?

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by