Matlab代码,用于生成某些形状的某些有符号距离场

时间:2018-10-13 17:58:32

标签: matlab shapes

为了更好地理解我的问题,请先运行下面的matlab代码。如果您取消注释每个形状对应的部分,则此代码可以使用带符号的距离生成矩形,椭圆形和圆形的形状。

请,我需要一个代码,该代码可以使用符号距离在下面的图片1中给出形状。非常感谢。

Picture1

%%% Signed distance code for generating shapes
clear all;
close all;
clc;
DomainWidth=2;
DomainHight=1;
ENPC=40;   
ENPR=80;
EW = DomainWidth / ENPR;       %  The width of each finite element.
EH = DomainHight / ENPC;        %   The hight of each finite element.
M = [ ENPC + 1 , ENPR + 1 ];
[ x,y ] = meshgrid( EW * [ -0.5 : ENPR + 0.5 ] , EH * [ -0.5 : ENPC + 0.5 ]);
[ FENd.x, FENd.y, FirstNdPCol] = MakeNodes(ENPR,ENPC,EW,EH);
LSgrid.x = x(:); LSgrid.y = y(:);       % The coordinates of Level Set grid 1
cx = DomainWidth/2;
cy= DomainHight/2;
a = cx;
b = 0.8*cy;
%% Generate circle
tmpPhi=  sqrt ( ( LSgrid . x - cx  ) .^2 + ( LSgrid . y  - cy  ) .^2 ) - 
DomainHight/2; 
LSgrid.Phi = -((tmpPhi.')).';
%% Generate ellipse
%   tmpPhi=   ( (LSgrid . x - cx)/a  ) .^2 + (( LSgrid . y  - cy)/(0.8*cy)  ) .^2  - 1;
%  LSgrid.Phi = -((tmpPhi.')).';
%% Generate rectangle
% lower = [cx - 0.5 * DomainWidth, cy - 0.25 * DomainHight];
% upper = [cx + 0.5 * DomainWidth, cy + 0.25 * DomainHight];
% Phi11 =max(LSgrid.x  - upper(1), lower(1) - LSgrid . x );
% Phi11 =max(Phi11,LSgrid.y   - upper(2));
% Phi11 =max(Phi11,lower(2) - LSgrid . y );
% LSgrid.Phi = -Phi11;
%%
FENd.Phi = griddata( LSgrid.x, LSgrid.y, LSgrid.Phi, FENd.x, FENd.y, 'cubic');


figure(10)
% Figure of the full contour plot of the signed distance of the shape
     contourf( reshape( FENd.x, M), reshape(FENd.y , M), reshape(FENd.Phi, M));
      axis equal;  grid on;drawnow; colorbar;
 figure(11)
% Figure of the scaled contour plot showing only the shape
      contourf( reshape( FENd.x, M), reshape(FENd.y , M), reshape(FENd.Phi, M), [0 0] );
      axis equal;  grid on;drawnow; colorbar;
figure(12)
%Figure of the signed distance function 
    h3=surface(x, y, reshape(-LSgrid.Phi , M + 1));  view([37.5  30]);  axis equal;  grid on; 
    set(h3,'FaceLighting','phong','FaceColor','interp', 'AmbientStrength',0.6); light('Position',[0 0 1],'Style','infinite'); colorbar;  
   %% Code for creating nodes    
  function [NodesX, NodesY, FirstNdPCol] = 
  MakeNodes(EleNumPerRow,EleNumPerCol,EleWidth,EleHight)
  [ x , y ]= meshgrid( EleWidth * [ 0 : EleNumPerRow ], EleHight * [0 : EleNumPerCol]);
  FirstNdPCol = find( y(:) == max(y(:)));
  NodesX = x(:); NodesY = y(:);
  end

0 个答案:

没有答案