Ray tracing (show multiple trisurf plots in one figure)

9 次查看(过去 30 天)
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.
Thanks in advance!
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

回答(1 个)

Karan Singh
Karan Singh 2023-10-3
Hi Anouk,
From what I understand, the goal is to visualize the intersected triangles on the plane. However, it seems that you are facing issues when trying to plot all the figures together in one figure. The commented code suggests that you have attempted to use the hold on command, but it is not working as expected.
To plot multiple trisurf functions in one figure, you can modify your code as follows:
  • Before the loop, initialize a variable to store the intersected triangles:
intersected_faces = [];
  • Inside the loop, instead of plotting each “trisurf individually, collect the intersected faces:
intersected_faces = [intersected_faces; faces(intersect, :)];
  • After the loop, plot all the intersected triangles together using a single “trisurf function:
trisurf(intersected_faces, X, Y, Z, 'FaceAlpha', 0.9)
Here's the modified code:
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;
intersected_faces = [];
for j = 1:length(coordinates)
dir = coordinates(j, :);
intersect = TriangleRayIntersection(A, dir, vert1, vert2, vert3);
intersected_faces = [intersected_faces; faces(intersect, :)];
end
trisurf(intersected_faces, X, Y, Z, 'FaceAlpha', 0.9)
axis equal
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

类别

Help CenterFile Exchange 中查找有关 Industrial Statistics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by