MATLAB: How to drape a quadrilateral on an stl-surface

MATLAB

Given is  a quadrilateral defined by its four corners in 3D space and it is sought draping this quadrilateral on an stl-surface.
How can I drape a quadrilateral on an stl-surface?

Best Answer

Assumed is that an stl-surface 'stl_surf' is given as a MATLAB struct with fields 'stl_surf.faces' and 'stl_surf.vertices', where 'faces' is an integer matrix standing for the vertex connectivity in the stl representation and 'vertices' stands for a real matrix containing the coordinates of each vertex in the stl-representation.
Moreover, assumed is that a quadrilateral in 3D space is also provided by means of a '3x4' matrix 'quadrilateral' containing the coordinates of each vertex of the quadrilateral.
The suggested draping algorithm is based on a successive orthogonal projection of each vertex of the quadrilateral on the stl-surface, whereas the geometry of the quadrilateral is displaced at every step based on the orthogonal projection of each vertex. The quadrilateral needs to be refined to a denser net of vertices in order to obtain a better draping resolution and for this reason function 'meshgrid' is herein leveraged.
Moreover, the following File Exchange submission is herein employed for the orthogonal projection on a triangulated surface,
but can be replaced by another algorithm as per need and the draping algorithm does not depend conceptually on the aforementioned submission.
The suggested algorithm is demonstrated below,
%% Refine the quadrilateral to contain more nodes than the original four corners(more degrees of freedom for the draping)
delta_u = 0.1; % step size for the refinement in the x-direction
delta_v = 0.1; % step size for the refinement in the y-direction
[X, Y] = meshgrid(min(quadrilateral(1,:)):delta_u:max(quadrilateral(1,:)),min(quadrilateral(2,:)):delta_v:max(quadrilateral(2,:)));
[n, m] = size(X);
Z = ones(n, m)*max(quadrilateral(3,:));
%% Drape the rectangle on the stl surface
X_draped = X;
Y_draped = Y;
Z_draped = Z;
for v = 1:m
for u = 1:n
% get a node on the refined quadrilateral
vertex = [X_draped(u, v) Y_draped(u, v) Z_draped(u, v)];
% project the node on the stl-surface
[~, vertex_proj] = ...
point2trimesh('Faces', stl_surf.faces, 'Vertices', stl_surf.vertices, 'QueryPoints', vertex , ...
'UseSubSurface', false, 'Algorithm', 'parallel');
% computation of the displacement of the projected node
disp_vertex = vertex_proj - vertex;
% assign the new coordinates of the undraped part of
% the surface due to the projection of the the node
X_draped(u:end, v:end) = X_draped(u:end, v:end) + disp_vertex(1, 1);
Y_draped(u:end, v:end) = Y_draped(u:end, v:end) + disp_vertex(1, 2);
Z_draped(u:end, v:end) = Z_draped(u:end, v:end) + disp_vertex(1, 3);
end
end