繁体   English   中英

Matlab:“X-Ray”情节线通过补丁

[英]Matlab: “X-Ray” plot line through patch

问题

我试图想象一个三维路径,以及围绕它的“云”,它代表数据的标准偏差。 我希望能够看到一条粗黑线作为路径,周围有一个偶然的灰色区域,线条没有任何浑浊,就像看到穿过云层的线条像X射线一样。

尝试 尝试1

我使用plot3创建一条粗线和patch来创建一系列以线的每个点为中心的框(在图上有一些额外的东西来表示开始/停止和方向,我也希望它们很容易被看到) 。 我试着玩patchalpha ,但是这会给线条带来浑浊,这样灰色的亮度会随着许多灰色框的变化而变化。 我希望alpha为1,因此每个灰色框都是完全相同的颜色,但我希望找到一些方法让云线均匀地看到。

最小的例子

根据要求,这是一个最小的例子,它产生下面的图。 例2

% Create a path as an example (a circle in the x-y plane, with sinusoidal deviations in the z-axis)
t = 0:1/100:2*pi;
x = sin(t);y = cos(t);
z = cos(t).*sin(5*t);
figure;
plot3(x,y,z,'k','linewidth',7);

% Draw patches
cloud = .1*rand(size(t)); % The size of each box (make them random, "like" real data)
grayIntensity = .9; % Color of patch
faceAlpha = .15; % Alpha of patch

for i = 1:length(x)
patch([x(i) - cloud(i); x(i) + cloud(i); x(i) - cloud(i); x(i) + cloud(i); x(i) - cloud(i); x(i) + cloud(i); x(i) - cloud(i); x(i) + cloud(i)],... % X values
[y(i) - cloud(i); y(i) - cloud(i); y(i) + cloud(i); y(i) + cloud(i); y(i) - cloud(i); y(i) - cloud(i); y(i) + cloud(i); y(i) + cloud(i)],... % Y values
[z(i) + cloud(i); z(i) + cloud(i); z(i) + cloud(i); z(i) + cloud(i); z(i) - cloud(i); z(i) - cloud(i); z(i) - cloud(i); z(i) - cloud(i)],... % Z values
grayIntensity*ones(1,3),... % Color of patch
'faces', [1 2 4 3;5 6 8 7;1 2 6 5; 8 7 3 4;1 5 7 3;2 6 8 4],... % Connect vertices to form faces (a box)
'edgealpha',0,... % Make edges invisible (to get continuous cloud effect)
'facealpha',faceAlpha); % Set alpha of faces
end

对于for循环中非常长的代码段的抱歉, patch命令有相当多的参数。 前三行简单地定义了定义立方体的8个顶点的x,y和z坐标,方法是指定中心点加上或减去立方体的一半半宽度, cloud(i) 其余的应该由他们各自的评论来解释。

感谢您的任何帮助!

我的Matlab版本很旧,但是这样的东西也有希望对你有用:

  1. 首先绘制阴影贴片(如上所述)(不是实心路径)。
  2. 存储轴手柄:

     patch_axis = gca; 
  3. 创建一个新的重叠轴

     path_axis = axes('position',get(patch_axis,'position'),'visible','off'); 
  4. 绘制实心路径线(在此新轴中)。

  5. 将patch_axis(后面)的旋转和限制链接到path_axis(在前面):

     set(rotate3d(path_axis),'ActionPostCallback', ... @(src,evt)set(patch_axis,{'view','xlim','ylim','zlim'}, ... get(path_axis,{'view','xlim','ylim','zlim'}))); 
  6. 如果您手动旋转视图,则应在第一次调整后将两个轴对齐并保持它们对齐。 但是如果您使用命令设置旋转和限制,则可以在set命令中包含两个轴控制柄(patch_axis和path_axis),或者在以下情况下复制设置:

     set(patch_axis,{'view','xlim','ylim','zlim'}, ... get(path_axis,{'view','xlim','ylim','zlim'}) 

请注意,要调整轴属性(勾选标签等),您需要对可见的patch_axis而不是不可见的path_axis执行此操作。 如果你希望它通过手动旋转实时交互,我不知道如何在每次重绘时执行对齐功能。

这是我在评论中提到的解决方案的实现(只绘制一个单一的surface云)。

它没有经过优化,有一些for循环可以通过巧妙地使用bsxfun或这些辅助函数系列来避免,但它运行正常。 在每个点找到曲线的切线并定向(旋转)每个横截面的数学运算也可以简化,但这不是我的强项,所以如果他们觉得这样,我就把它留给专家。

基本上,它定义了一个圆(在代码中通常称为“横截面”),其半径与某些东西成比例(应用程序中的标准偏差,示例中的随机值)。 然后将每个圆以3D旋转,使其在平移点处垂直于曲线。 然后将所有这些圆用作单个surface图形对象的包络。

当表面多次重叠时(取决于视角),主中心线仍有一些阴影,但主线始终可见。 另外,您只需要管理一个图形对象。

结果如下: cloudenv

当然,您可以根据自己的喜好改变曲面的AlphaValue 我定义了一个与颜色信息数据大小相同的完整矩阵。 此时它全部设置为0 (所以它指向默认色图中的绿色),但这样也很容易,如果你想要制作另一个参数的颜色函数,只需相应调整颜色矩阵(和随之而来的色彩图)。

代码末尾有一个选项可将每个横截面显示为补丁对象。 它不打算用在最终结果中,但如果您想进行自己的修改,它可以帮助您了解整个事物是如何构建的。

这里是代码:

%% // Create a path as an example (a circle in the x-y plane, with sinusoidal deviations in the z-axis)
nPts = 180 ;
t = linspace(0,359,nPts)*pi/180;
x = sin(t); y = cos(t);
z = cos(t).*sin(2*t);

figure;
h.line = plot3(x,y,z,'k','linewidth',2,'Marker','none');
hold on
xlabel('X')
ylabel('Y')
zlabel('Z')

%% // Define options
%// cloud = .1*rand(size(t)) ; % The size of each box (make them random, "like" real data)
%// I used another randomization process, make that function of your stdev
r.min = 0.1 ; r.max = 0.2 ;
radius = r.min + (r.max-r.min).* rand(size(t)) ;

%// define surface and patch display options (FaceAlpha etc ...), for later
surfoptions  = {'FaceAlpha',0.2 , 'EdgeColor','none' , 'EdgeAlpha',0.1 , 'DiffuseStrength',1 , 'AmbientStrength',1 } ;
patchoptions = {'FaceAlpha',0.2 , 'EdgeColor','k'    , 'EdgeAlpha',0.2 , 'DiffuseStrength',1 , 'AmbientStrength',1 } ;
patchcol     = [1 0 0] ;  % Color of patch

%% // get the gradient at each point of the curve
Gx = diff([x,x(1)]).' ;                       %'//damn StackOverflow prettifier 
Gy = diff([y,y(1)]).' ;                       %'//damn StackOverflow prettifier 
Gz = diff([z,z(1)]).' ;                       %'//damn StackOverflow prettifier 
%// get the middle gradient between 2 segments (optional, just for better rendering if low number of points)
G = [ (Gx+circshift(Gx,1))./2 (Gy+circshift(Gy,1))./2 (Gz+circshift(Gz,1))./2] ;

%% // get the angles (azimuth, elevation) of each plane normal to the curve

ux = [1 0 0] ;
uy = [0 1 0] ;
uz = [0 0 1] ;

for k = nPts:-1:1 %// running the loop in reverse does automatic preallocation
    a = G(k,:) ./ norm(G(k,:)) ;
    angx(k) =  atan2( norm(cross(a,ux)) , dot(a,ux))  ;
    angy(k) =  atan2( norm(cross(a,uy)) , dot(a,uy))  ;
    angz(k) =  atan2( norm(cross(a,uz)) , dot(a,uz))  ;

    [az(k),el(k)] = cart2sph( a(1) , a(2) , a(3) ) ;
end
%// adjustment to be normal to cross section plane the way the rotation are defined later
az = az + pi/2 ; 
el = pi/2 - el ;

%% // define basic disc
discResolution = 20 ;
tt = linspace( 0 , 2*pi , discResolution ) ;
xd = cos(tt) ;
yd = sin(tt) ;
zd = zeros( size(xd) ) ;

%% // Generate coordinates for each cross section

ccylX = zeros( nPts , discResolution ) ;
ccylY = zeros( nPts , discResolution ) ;
ccylZ = zeros( nPts , discResolution ) ;
ccylC = zeros( nPts , discResolution ) ;

for ip = 1:nPts
    %// cross section coordinates, with radius function of [rand] in this
    %// example. Make it function of the stdev in your application.
    csTemp = [ ( radius(ip) .* xd )  ; ... %// X coordinates
               ( radius(ip) .* yd )  ; ... %// Y coordinates
                               zd    ] ;   %// Z coordinates

    %// rotate the cross section (around X axis, around origin)
    elev = el(ip) ;
    Rmat = [ 1     0           0     ; ...
             0 cos(elev)  -sin(elev) ; ...
             0 sin(elev)   cos(elev) ] ;
    csTemp = Rmat * csTemp ;

    %// do the same again to orient the azimuth (around Z axis)
    azi = az(ip) ;
    Rmat = [ cos(azi)  -sin(azi) 0 ; ...
             sin(azi)   cos(azi) 0 ; ...
               0            0    1 ] ;
    csTemp = Rmat * csTemp ;

    %// translate each cross section where it should be and store in global coordinate vector
    ccylX(ip,:) = csTemp(1,:) + x(ip) ;
    ccylY(ip,:) = csTemp(2,:) + y(ip) ;
    ccylZ(ip,:) = csTemp(3,:) + z(ip) ;
end

%% // Display the full cylinder
hd.cyl = surf( ccylX , ccylY , ccylZ , ccylC ) ;

%// use that if the graphic object already exist but you just want to update your data:
%// set( hd.cyl , 'XData',ccylX , 'YData',ccylY ,'ZData',ccylZ ) 

set( hd.cyl , surfoptions{:} )


%% // this is just to avoid displaying the patches in the next block
%// comment the "return" instruction or just execute next block if you want
%// to see the building cross sections as patches
return 

%% // display patches
hp = zeros(nPts,1) ;
for ip = 1:nPts
   hp(ip) = patch( ccylX(ip,:) , ccylY(ip,:) , ccylZ(ip,:) , patchcol ) ;
   set( hp(ip) , patchoptions{:} )
end

这只是一个快速缩放的视图,其中包含补丁(代码重新运行时点数较少,否则很快会使整个数字混乱):

cloudenvzoom

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM