Skip to content

Commit 664a283

Browse files
committed
intersectConvexPolyhedra: add option to merge coplanar faces
1 parent 5b3050e commit 664a283

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

matGeom/meshes3d/intersectConvexPolyhedra.m

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
function res = intersectConvexPolyhedra(mesh1, mesh2)
1+
function varargout = intersectConvexPolyhedra(mesh1, mesh2, varargin)
22
%INTERSECTCONVEXPOLYHEDRA Intersection of two convex polyhedra.
33
%
44
% usage:
5-
% res = intersectConvexPolyhedra(mesh1, mesh2)
5+
% RES = intersectConvexPolyhedra(MESH1, MESH2)
6+
% [VI, FI] = intersectConvexPolyhedra(MESH1, MESH2)
67
% Computes the (convex) mesh resulting from the intersection of the
78
% 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.
917
%
1018
% Example
1119
% mesh1 = createCube;
@@ -18,7 +26,7 @@
1826
% axis equal; view(3);
1927
%
2028
% See also
21-
% convexHull3d, minConvexHull
29+
% meshes3d, convexHull3d, minConvexHull
2230

2331
% ------
2432
% Author: David Legland
@@ -27,10 +35,18 @@
2735
% Created: 2025-12-01, using Matlab 25.1.0.2943329 (R2025a)
2836
% Copyright 2025 INRAE.
2937

38+
39+
% parse optional input arguments
40+
parser = inputParser;
41+
addParameter(parser, 'mergeCoplanarFaces', true);
42+
parse(parser, varargin{:});
43+
mergeCoplanar = parser.Results.mergeCoplanarFaces;
44+
3045
% retrieve data from each mesh
3146
verts1 = mesh1.vertices;
3247
verts2 = mesh2.vertices;
3348

49+
3450
% identify which vertices are within the other mesh
3551
inVertsFlag1 = isPointInMesh(verts1, mesh2);
3652
inVertsFlag2 = isPointInMesh(verts2, mesh1);
@@ -45,7 +61,7 @@
4561
for i1 = 1:ne1
4662
seg = [verts1(edges1(i1,1), :) verts1(edges1(i1,2), :)];
4763
tmp = intersectEdgeMesh3d(seg, mesh2);
48-
inters1 = [inters1 ; tmp];
64+
inters1 = [inters1 ; tmp]; %#ok<AGROW>
4965
end
5066

5167
% iterate over edges of first mesh, and populate array of intersections
@@ -54,11 +70,20 @@
5470
for i1 = 1:ne2
5571
seg = [verts2(edges2(i1,1), :) verts2(edges2(i1,2), :)];
5672
tmp = intersectEdgeMesh3d(seg, mesh1);
57-
inters2 = [inters2 ; tmp];
73+
inters2 = [inters2 ; tmp]; %#ok<AGROW>
5874
end
5975

60-
76+
% concatenates vertices of result mesh
6177
convVerts = [verts1(inVertsFlag1,:) ; verts2(inVertsFlag2,:) ; inters1 ; inters2];
6278

79+
% convert to mesh data structure
6380
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

Comments
 (0)