How to trim a surface intersecting with another

36 ビュー (過去 30 日間)
KostasK
KostasK 2021 年 5 月 1 日
コメント済み: Star Strider 2021 年 5 月 2 日
Hi all, I have two surfaces Z1 and Z2 (a surface and an extruded line) that intersect with each other, in point format with two different grids. What I want to accomplish is to trim the left part of the Z1 surface after its point of intrersection with Z2 as shown in the example below, so that I can obtain the trimmed surface (lets call it Z1t ) in its new point format, as a series of three matrices.
Below is the example code the creates the above (without the trimming part obviously). The reason that I am struggling to accomplish that, is because the surfaces that I am given do not share the same grid, as I also demonstrate in my code below. So I do not have the exact points where they intersect. Moreover, when I attempt to place those surfaces on the same grid using griddata , I am getting a matrix full of NaN.
clear ; clc
n = 20 ;
x1 = linspace(1,10,n) ;
y1 = linspace(-25,25,n) ;
x2 = linspace(1,10,n) ;
y2 = linspace(-10,10,n) ;
[X1, Y1] = meshgrid(x1, y1) ; % Grid 1
[X2, Y2] = meshgrid(x2, y2) ; % Grid 2
Z1 = sin(X1) + cos(Y1) ; % Surface
Z2 = X2.*sin(X2) ; % Line (extruded for plotting)
z2 = x2.*sin(x2) ; % Line (not extruded)
% Match grid surface to line
[~, ~, Z2new] = griddata(x2, y2, z2, x1, y1') ;
figure
surf(X1,Y1,Z1)
hold on
surf(X2,Z2,Y2)
P.S. I have seen other posts with similar subjects, however they don't address this problem exactly. There is this post which seems to offer a valuable hint, however I don't get how to implement the method. Also, there is this post that straight-up doesn't work for me when dealing with different grid sizes. Finally, this post seems to get the closes, however I can't obtain the new plane in point format.
Thanks for your help in advance

採用された回答

Star Strider
Star Strider 2021 年 5 月 1 日
Try this—
n = 50 ;
x1 = linspace(1,10,n) ;
y1 = linspace(-25,25,n) ;
x2 = linspace(1,10,n) ;
y2 = linspace(-10,10,n) ;
[X1, Y1] = meshgrid(x1, y1) ; % Grid 1
[X2, Y2] = meshgrid(x2, y2) ; % Grid 2
Z1 = sin(X1) + cos(Y1) ; % Surface
Z2 = X2.*sin(X2) ; % Line (extruded for plotting)
z2 = x2.*sin(x2) ; % Line (not extruded)
% Match grid surface to line
[~, ~, Z2new] = griddata(x2, y2, z2, x1, y1') ;
Lv = nan(size(X1)); % Create 'NaN' Matrix
Lv(Y1<=Z2) = 1; % Set Selected Elements TO 'true'
figure
surf(X1.*Lv,Y1.*Lv,Z1.*Lv) % Multiply Matrices By 'Lv' To Approximate Desired Result
hold on
surf(X2,Z2,Y2)
hold off
xlabel('X')
ylabel('Y')
view(-37.5,30)
grid on
figure
surf(X1.*Lv,Y1.*Lv,Z1.*Lv)
hold on
surf(X2,Z2,Y2)
hold off
xlabel('X')
ylabel('Y')
view(150.5,60)
grid on
title('Rotated To See Result')
.
  4 件のコメント
KostasK
KostasK 2021 年 5 月 2 日
Yep, that seems to work. So essentially you have re-gridded the data from the beginning down to a common grid. Thanks for taking the time, greatly appreciated.
Star Strider
Star Strider 2021 年 5 月 2 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by