How to aligning STL files and find the distance between them ?

16 次查看(过去 30 天)
Hi,
Could anyone suggest the most efficient way to align two imported STL files into MATLAB?
I am trying to align the files at the origin, then use the Hausdorff Distance function to find the distance between two points.
So preferably, one file must be translated to best align/ match up with the other.
Attached is one of the codes I am using to import the stl files, find the vertices and faces. Alternate approaches would be greatly appreciated.
I have also attched 2 images from the stl files.
[F, xyz1] = stlread('MATxct.stl');
[vn] = STLVertexNormals(F,xyz1);
[F1, xyz2] = stlread('MATcad.stl');
[vn1] = STLVertexNormals(F1, xyz2);
norm1 = mean(vn);
norm2 = mean(vn1);
% Centre for (vn)
cx1 = mean(xyz1(:,1));
cy1 = mean(xyz1(:,2));
cz1 = mean(xyz1(:,3));
xyz(:,1)=xyz1(:,1)-cx1;
xyz(:,2)=xyz1(:,2)-cy1;
xyz(:,3)=xyz1(:,3)-cz1;
% Centre for (vn1)
cx2 = mean(xyz2(:,1));
cy2 = mean(xyz2(:,2));
cz2 = mean(xyz2(:,3));
xyz22(:,1)=xyz2(:,1)-cx2;
xyz22(:,2)=xyz2(:,2)-cy2;
xyz22(:,3)=xyz2(:,3)-cz2;
% Rotating the STL files so that thie principle axes align with the
% coordinates
r=vrrotvec(norm1,[0, 0, 1]);
Rot = vrrotvec2mat(r);
xyz=Rot*xyz';
xyz=xyz';
scatter3(xyz(:,1),xyz(:,2),xyz(:,3),'b')
r=vrrotvec(norm2,[0, 0, 1]);
Rot = vrrotvec2mat(r);
xyz22=Rot*xyz22';
xyz22=xyz22';
scatter3(xyz(:,1),xyz(:,2),xyz(:,3),'b')

回答(1 个)

Adit Kirtani
Adit Kirtani 2023-5-15
编辑:Adit Kirtani 2023-5-15
Hi Ronnie,
I see you’ve used the ‘stlread’ function for importing STL files. You can also use the following methods:
  • The ‘importGeometry’ function will create a DiscreteGeometry object of your STL file. You can use the ‘rotate’ function to help align your 3D geometry. Here are some documentation links which you may find handy:
  • ‘import_stl_file’ is also a another option if you want a faster version of stlread. Keep in mind the current version of the program is ASCII only and will not support binary. You can find it on the MATLAB File Exchange here:
I hope this helps,
Adit Kirtani.
  1 个评论
DGM
DGM 2025-7-13
Why does everyone always recommend the same pasted bullet-point list of PDE tools for simple STL import? It's not appropriate, and as far as I know, it's not even an option. The model is not a closed surface. You can't import it with importGeometry().
Error using pde.DiscreteGeometry
Failed to create geometry, the input does not form a closed volume.
This may be due to missing faces or gaps in the input.
Even if you could, converting it to a high-level object doesn't make the object registration any easier. OP is already importing the data, and is already competent at basic geometric transformations as simple as rotation. How does pde.discretegeometry.rotate help with the actual problem, which is distance minimization?
Lastly, FEX #30923 isn't helpful either. We can see that OP can already read the file with (what is almost certainly) FEX #22409, but #30923 isn't any faster than the existing STL reader that is included with MATLAB. OP is using one half-baked old decoder, and so you're recommending a different half-baked old decoder instead of recommending the canonical tools that are already in MATLAB.
You could chalk that up to preference, but there's an actual showstopping problem there. When I called them "half-baked", I'm fairly literal about the "half" part. FEX #22409 is only capable of decoding binary-encoded STL files. Anything else, it will just fail. On the other hand, FEX #30923 is only capable of decoding ASCII-encoded STL files -- specifically only ones with windows-style linebreaks. Both decoders have other problems, but the two decoders simply cannot read the same files. Not only is it not faster than the canonical tool, it wouldn't have even read OP's files at all.
Why is it that so many of these apparently AI-generated junk posts are being posted by staff?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Oceanography and Hydrology 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by