Search code examples
matlabmodelingintersection

MATLAB - Intersect between a surface and a plane


I have a 3D mesh like in this picture.

Now what I want to do is create a plane that will intersect the surface at a certain Z value. I would then want to get the x and y coordinates of this intersection and have matlab output them.

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. I would then like to see what the new shoreline would look like by obtaining the x and y coordinates of this intersection.

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

Solution

  • You can do this by simply thresholding the interpolated Z values

    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 );