Are there MATLAB functions or scripts to perform boolean operations on triangulated solids?

23 views (last 30 days)
It is a very specific question. There is no need for more details.

Answers (3)

Matt J
Matt J ungefär 6 timmar ago
Edited: Matt J ungefär 6 timmar ago
If you mean you want to take intersections and unions of meshes, then no, there are no MathWorks-authored commands to do that. However, there are FEX files that let you convert a mesh to a binary volume and back, e.g.,
so you might be able to do it by transfering to the binary image domain if you can tolerate some discretization error.
  1 Comment
Jahir Pabon
Jahir Pabon ungefär 5 timmar ago
Thank you Matt. What you propose, though a bit cumbersome, sounds like a possible alternative. I would like to preserve the simplicity of the original triangulations. But if there is nothing else in the Matlab world I will give that a try.

Sign in to comment.


John D'Errico
John D'Errico ungefär 5 timmar ago
Are there such codes in MATLAB? Yes. I wrote them. intersect, union. In 2-d and in 3-d. (Not sure if I wrote a setdiff variant. Probably not, as the problem I was working on would not need them.) Are they publically available? No. Sorry. The codes belong to my client. I'm not sure they are still in use, but I don't own them.
You want them to retain the original "simplicity" of the triangulations? Sorry, but no. The result tends to get quite complicated near the boundaries.
Could you write something? They were not that terrible to implement. Kind of slow if the tessellations were large, but that always happens. You can start with these operations on a pair of tetrahedra.

DGM
DGM 12 minuter ago
Edited: DGM 6 minuter ago
If your solids can each be expressed as a manifold STL, then I don't see why you can't use external tools to do the job. I normally use OpenSCAD to do CSG, but I'm pretty sure there are command-line tools that can also do basic operations. OpenSCAD can be used from the command line as well, but that's not how I use it. If you want the operation to be part of a MATLAB script, just use system() to deal with the external tools.
For example, this is the difference of two unit cubes, each composed of a minimal 12 triangles.
% a triangulated unit cube
Q.vertices = [0 0 0; 1 0 0; 1 1 0; 0 1 0; ...
0 0 1; 1 0 1; 1 1 1; 0 1 1];
Q.faces = [3 2 1; 1 4 3; 5 6 7; 7 8 5; 1 2 6; 6 5 1; ...
3 4 8; 8 7 3; 4 1 5; 5 8 4; 2 3 6; 3 7 6];
TR = triangulation(Q.faces,Q.vertices);
stlwrite(TR,'cube1.stl')
% the same thing, but translated by [1 -1 1]*0.5
Q.vertices = Q.vertices + [1 -1 1]*0.5;
TR = triangulation(Q.faces,Q.vertices);
stlwrite(TR,'cube2.stl')
% create a simple SCAD model
% this could also just be written externally and reused
% writing scripts via fprintf() is ridiculously cumbersome
% though it demonstrates that names, etc. can be programmatically incorporated
fid = fopen('mymodel.scad','w');
fprintf(fid,'difference(){\n\timport("cube1.stl");\n\timport("cube2.stl");\n}');
fclose(fid);
% compute the composite geometry, output as a new STL
[status,result] = system('openscad -o output.stl mymodel.scad');
% read the new STL as a triangulation object and display it
TRnew = stlread('output.stl');
hs = trisurf(TRnew,'facecolor',[1 1 1]*0.7)
camlight
axis equal
view(-22,35)

Categories

Find more on Delaunay Triangulation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!