Home > matGeom > meshes3d > meshSilhouette.m

meshSilhouette

PURPOSE ^

MESHSILHOUETTE Compute the 2D outline of a 3D mesh on an arbitrary plane.

SYNOPSIS ^

function silhouette = meshSilhouette(v, f, varargin)

DESCRIPTION ^

MESHSILHOUETTE Compute the 2D outline of a 3D mesh on an arbitrary plane.

   ATTENTION: Very slow brute force approach! Keep the number of faces as
   low as possible.

   SILHOUETTE = meshSilhouette(MESH, PLANE)
   Calculates the silhouette (2D outline) of the MESH projected on the
   PLANE.

   SILHOUETTE = meshSilhouette(MESH) uses the x-y plane.

   SILHOUETTE = meshSilhouette(V, F, ...)

   SILHOUETTE = meshSilhouette(..., 'visu', 1) visualizes the results.
   By default the results are not visualized.

   Example:
     v = [5, 2, 6, 0, 3;  0, 2, 4, 2, 1;  -5, -6, -6, -7, -9]';
     f = [1, 2, 4; 1, 5, 4; 1, 2, 5; 2, 3, 5; 2, 4, 3; 3, 4, 5];
     sil = meshSilhouette(v, f, rand(1,9),'visu',1);
   
   See also
     projPointOnPlane

   Source:
     Sean de Wolski - https://www.mathworks.com/matlabcentral/answers/68004

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function silhouette = meshSilhouette(v, f, varargin)
0002 %MESHSILHOUETTE Compute the 2D outline of a 3D mesh on an arbitrary plane.
0003 %
0004 %   ATTENTION: Very slow brute force approach! Keep the number of faces as
0005 %   low as possible.
0006 %
0007 %   SILHOUETTE = meshSilhouette(MESH, PLANE)
0008 %   Calculates the silhouette (2D outline) of the MESH projected on the
0009 %   PLANE.
0010 %
0011 %   SILHOUETTE = meshSilhouette(MESH) uses the x-y plane.
0012 %
0013 %   SILHOUETTE = meshSilhouette(V, F, ...)
0014 %
0015 %   SILHOUETTE = meshSilhouette(..., 'visu', 1) visualizes the results.
0016 %   By default the results are not visualized.
0017 %
0018 %   Example:
0019 %     v = [5, 2, 6, 0, 3;  0, 2, 4, 2, 1;  -5, -6, -6, -7, -9]';
0020 %     f = [1, 2, 4; 1, 5, 4; 1, 2, 5; 2, 3, 5; 2, 4, 3; 3, 4, 5];
0021 %     sil = meshSilhouette(v, f, rand(1,9),'visu',1);
0022 %
0023 %   See also
0024 %     projPointOnPlane
0025 %
0026 %   Source:
0027 %     Sean de Wolski - https://www.mathworks.com/matlabcentral/answers/68004
0028 
0029 % ------
0030 % Author: oqilipo
0031 % E-mail: N/A
0032 % Created: 2020-07-29
0033 % Copyright 2020-2024
0034 
0035 narginchk(1,5)
0036 nargoutchk(0,1)
0037 
0038 %% Parse inputs
0039 % If first argument is a struct
0040 if isstruct(v)
0041     if nargin > 1
0042         varargin=[{f} varargin{:}];
0043     end
0044     mesh = v;
0045     [v, f] = parseMeshData(v);
0046 else
0047     mesh.vertices = v;
0048     mesh.faces = f;
0049 end
0050 
0051 p = inputParser;
0052 logParValidFunc = @(x) (islogical(x) || isequal(x,1) || isequal(x,0));
0053 addOptional(p,'plane',[0 0 0 1 0 0 0 1 0],@isPlane)
0054 addParameter(p,'visualization',false,logParValidFunc);
0055 parse(p, varargin{:});
0056 plane = p.Results.plane;
0057 
0058 % Transform into the x-y plane
0059 TFM = createBasisTransform3d('g', plane);
0060 v = transformPoint3d(v,TFM);
0061 
0062 % Initialize final polygon vectors
0063 [px, py] = boundary(polyshape(v(f(1,:),1) ,v(f(1,:),2), 'Simplify',false));
0064 for i = 2:size(f,1)
0065     A = polyshape(v(f(i,:),1), v(f(i,:),2), 'Simplify',false);
0066     B = polyshape(px, py, 'Simplify',false);
0067     [px, py] = boundary(union(A,B));
0068 end
0069 
0070 % Transform back into the plane
0071 silhouette = transformPoint3d([px,py,zeros(size(px))], inv(TFM));
0072 
0073 if p.Results.visualization
0074     figure('Color','w'); axH = axes(); axis(axH, 'equal', 'tight')
0075     drawPolyline3d(axH, silhouette,'Color','r','LineWidth',3)
0076     drawPlane3d(axH, plane,'FaceAlpha',0.5)
0077     drawMesh(mesh,'FaceAlpha',0.5,'FaceColor','none')
0078     axis(axH, 'equal')
0079     camTar = mean(silhouette, "omitnan");
0080     axH.CameraTarget = camTar;
0081     axH.CameraPosition = camTar + ...
0082         planeNormal(plane)*vectorNorm3d(axH.CameraPosition-axH.CameraTarget);
0083     axH.CameraUpVector = plane(4:6);
0084     xlabel(axH, 'x'); ylabel(axH, 'y'); zlabel(axH, 'z');
0085 end
0086 
0087 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022