|
1 | | -function res = intersectConvexPolyhedra(mesh1, mesh2) |
| 1 | +function varargout = intersectConvexPolyhedra(mesh1, mesh2, varargin) |
2 | 2 | %INTERSECTCONVEXPOLYHEDRA Intersection of two convex polyhedra. |
3 | 3 | % |
4 | 4 | % usage: |
5 | | -% res = intersectConvexPolyhedra(mesh1, mesh2) |
| 5 | +% RES = intersectConvexPolyhedra(MESH1, MESH2) |
| 6 | +% [VI, FI] = intersectConvexPolyhedra(MESH1, MESH2) |
6 | 7 | % Computes the (convex) mesh resulting from the intersection of the |
7 | 8 | % volumes defined by the two input convex meshes. The result is also a |
8 | | -% mesh corresponding to the boundary of the convex volume. |
| 9 | +% mesh corresponding to the boundary of the convex volume. If two output |
| 10 | +% arguments are requested, they correspond to the vertices and the faces |
| 11 | +% of the resulting mesh. |
| 12 | +% |
| 13 | +% ... = intersectConvexPolyhedra(..., 'mergeCoplanarFaces', TF) |
| 14 | +% Specifies whether the resulting mesh should be given as a triangulation |
| 15 | +% (TF is false) or as a polygonal mesh with potentially a variable number |
| 16 | +% of vertex per face (TF is true). Default value is TRUE. |
9 | 17 | % |
10 | 18 | % Example |
11 | 19 | % mesh1 = createCube; |
|
18 | 26 | % axis equal; view(3); |
19 | 27 | % |
20 | 28 | % See also |
21 | | -% convexHull3d, minConvexHull |
| 29 | +% meshes3d, convexHull3d, minConvexHull |
22 | 30 |
|
23 | 31 | % ------ |
24 | 32 | % Author: David Legland |
|
27 | 35 | % Created: 2025-12-01, using Matlab 25.1.0.2943329 (R2025a) |
28 | 36 | % Copyright 2025 INRAE. |
29 | 37 |
|
| 38 | + |
| 39 | +% parse optional input arguments |
| 40 | +parser = inputParser; |
| 41 | +addParameter(parser, 'mergeCoplanarFaces', true); |
| 42 | +parse(parser, varargin{:}); |
| 43 | +mergeCoplanar = parser.Results.mergeCoplanarFaces; |
| 44 | + |
30 | 45 | % retrieve data from each mesh |
31 | 46 | verts1 = mesh1.vertices; |
32 | 47 | verts2 = mesh2.vertices; |
33 | 48 |
|
| 49 | + |
34 | 50 | % identify which vertices are within the other mesh |
35 | 51 | inVertsFlag1 = isPointInMesh(verts1, mesh2); |
36 | 52 | inVertsFlag2 = isPointInMesh(verts2, mesh1); |
|
45 | 61 | for i1 = 1:ne1 |
46 | 62 | seg = [verts1(edges1(i1,1), :) verts1(edges1(i1,2), :)]; |
47 | 63 | tmp = intersectEdgeMesh3d(seg, mesh2); |
48 | | - inters1 = [inters1 ; tmp]; |
| 64 | + inters1 = [inters1 ; tmp]; %#ok<AGROW> |
49 | 65 | end |
50 | 66 |
|
51 | 67 | % iterate over edges of first mesh, and populate array of intersections |
|
54 | 70 | for i1 = 1:ne2 |
55 | 71 | seg = [verts2(edges2(i1,1), :) verts2(edges2(i1,2), :)]; |
56 | 72 | tmp = intersectEdgeMesh3d(seg, mesh1); |
57 | | - inters2 = [inters2 ; tmp]; |
| 73 | + inters2 = [inters2 ; tmp]; %#ok<AGROW> |
58 | 74 | end |
59 | 75 |
|
60 | | - |
| 76 | +% concatenates vertices of result mesh |
61 | 77 | convVerts = [verts1(inVertsFlag1,:) ; verts2(inVertsFlag2,:) ; inters1 ; inters2]; |
62 | 78 |
|
| 79 | +% convert to mesh data structure |
63 | 80 | tri = convhulln(convVerts); |
64 | | -res = trimMesh(convVerts, tri); |
| 81 | +[vertices2, faces2] = trimMesh(convVerts, tri); |
| 82 | + |
| 83 | +% optional cleanup of faces |
| 84 | +if mergeCoplanar |
| 85 | + [vertices2, faces2] = mergeCoplanarFaces(vertices2, faces2); |
| 86 | +end |
| 87 | + |
| 88 | +% format output arguments |
| 89 | +varargout = formatMeshOutput(nargout, vertices2, faces2); |
0 commit comments