# Ray tracing (show multiple trisurf plots in one figure)

5 views (last 30 days)
Anouk Baeten on 17 Jun 2022
Hello,
I would like to use ray tracing to see if my line/ray wil pass my plane (of which I gave the coordinates in 3D).
For this I use the function 'Triangle/Ray Intersection' (https://nl.mathworks.com/matlabcentral/fileexchange/33073-triangle-ray-intersection), which I use in my code: here I start with taking the coordinates from my STL file, those will be the coordinates of my plane). Then I want to draw a invisible line from the origin (A) to each coordinate (B) and check whether this line/ray will pass my plane. Then the function plots a figure of which the yellow triangles are the parts that the ray is 'touching'.
So this way I have multiple rays and just 1 plane. I made a for-loop for this but when I want to plot all the figures together (so one figure with all the triangles yellow that were 'touched' by all that lines), but it isn't working actually. I thought 'hold on' or 'hold all' would work but it doesn't. For now I only want to plot the yellow pieces en not the rays, therefore this part is commented.
Can somebody help my to plot actually multiple trisurf functions in one figure?
I added my code and also the STL file.
clear variables; close all;
model = createpde(1);
dg = importGeometry(model,'Bovenste GS.stl');
mesh = generateMesh(model, 'Hmax',5);
coordinates = [];
x = [];
y = [];
z = [];
snijpunten = [];
for i = 1:length(mesh.Nodes)
A = [0 0 0];
B = mesh.Nodes(:,i);
coordinates = [coordinates; transpose(B)];
x = [x, (coordinates(i,1))];
y = [y, (coordinates(i,2))];
z = [z, (coordinates(i,3))];
end
xv = linspace(min(x), max(x), 20);
yv = linspace(min(y), max(y), 20);
[X,Y] = meshgrid(xv, yv);
faces = delaunay(X,Y);
Z = griddata(x,y,z,X,Y);
vertices = [X(:) Y(:) Z(:)];
vert1 = vertices(faces(:,1),:);
vert2 = vertices(faces(:,2),:);
vert3 = vertices(faces(:,3),:);
figure;
% clf;
% hold on
orig = A; % ray's origin
% hold on
for j = 1:length(coordinats)
dir = coordinates(j,:); % ray's direction
intersect = TriangleRayIntersection(orig, dir, vert1, vert2, vert3);
% *Display the results: Surface in blue, line in light read and intersected
% triangles in dark red*
trisurf(faces,X_bovengrens,Y_bovengrens,Z_bovengrens, intersect*1.0,'FaceAlpha', 0.9)
hold on
% line('XData',orig(1)+[0 dir(1)],'YData',orig(2)+[0 dir(2)],'ZData',...
% orig(3)+[0 dir(3)],'Color','r','LineWidth',3)
%
% set(gca, 'CameraPosition', [106.2478 -35.9079 136.4875])
% set(gco,'EdgeColor','none');
end
axis equal

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by