[英]MATLAB - Intersect between a surface and a plane
I have a 3D mesh like in this picture. 我在这张照片中有一个3D网格。
Now what I want to do is create a plane that will intersect the surface at a certain Z value. 现在我想做的是创建一个与某个Z值相交的平面。 I would then want to get the x and y coordinates of this intersection and have matlab output them. 然后我想得到这个交叉点的x和y坐标并让matlab输出它们。
What I'm planning on doing is that this picture is a model of a lake. 我打算做的是这张照片是一个湖泊的模型。 This lake will have water evaporating that will be removing a certain z value of water. 这个湖将有水蒸发,将消除一定的z值的水。 I would then like to see what the new shoreline would look like by obtaining the x and y coordinates of this intersection. 然后我想通过获得这个交叉点的x和y坐标来看看新海岸线的样子。
This is my code for plotting that picture. 这是我绘制该图片的代码。
function plot(x,y,z)
Contour = xlsread('C:\Users\Joel\Copy\Contour','A2:D4757');
x = Contour(:,1);
y = Contour(:, 2);
z = Contour(:,3);
F = TriScatteredInterp(x,y,z);
[X,Y]=meshgrid(linspace(-600,600,300));
Z=F(X,Y);
surf(X,Y,Z);
end
You can do this by simply thresholding the interpolated Z
values 您可以通过简单地对插值的Z
值进行阈值处理来完成此操作
inside = Z < seaLevel; % binary mask of points inside water
shoreline = bwmorph( inside, 'remove' ); % a mask with only shoreline pixels eq to 1
figure;
surf( X, Y, Z, 'EdgeColor', 'none', 'FaceColor', [210,180,140]/255, 'FaceAlpha', .5 );
hold on;
ii = find( shoreline );
plot3( X(ii), Y(ii), seaLevel*ones(size(ii)), 'b', 'LineWidth', 2 );
The contour3 will give nicer boundaries: contour3将提供更好的边界:
[h,c]=contour3(X,Y,Z,[seaLevel seaLevel]);
seaLevel is given twice: otherwise contour3 thinks seaLevel is the number of levels to automatically calibrate. seaLevel被给出两次:否则contour3认为seaLevel是自动校准的级别数。 And to nicely annotate with the numeric height of your seaLevel: 并使用seaLevel的数字高度很好地注释:
clabel(h,c);
you may modify c to print "sea level" instead of num2str(seaLevel). 您可以修改c以打印“海平面”而不是num2str(seaLevel)。
If you don't have the MATLAB toolbox for image processing you can't use the function "bwmorph". 如果没有用于图像处理的MATLAB工具箱,则无法使用“bwmorph”功能。 As a solution you can replace line 2 of Shai's code with the following code which reimplements the bwmorph function for parameter 'remove'. 作为解决方案,您可以使用以下代码替换Shai代码的第2行,该代码重新实现参数'remove'的bwmorph函数。 (The reimplementation is probably neither very perfomrant nor an exact reimplementatin (the borders of the matrix aren't used) - but the solution should work as a first step. Feel free to improve). (重新实现可能既不是非常实现也不是精确的重新实现(不使用矩阵的边界) - 但解决方案应该是第一步。随意改进)。
shoreline= zeros(size(inside));
for i_row = 2:size(inside,1)-1
for i_col = 2:size(inside,2)-1
if(inside(i_row,i_col))
if (~( inside(i_row+1,i_col) && ...
inside(i_row-1,i_col) && ...
inside(i_row,i_col+1) && ...
inside(i_row,i_col-1)))
inside2(i_row,i_col) = 1;
end
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.