Intersection of a line and 3D mesh

조회 수: 38 (최근 30일)
ac_bm_mat
ac_bm_mat 2022년 4월 29일
댓글: Walter Roberson 2023년 10월 18일
I have a line that connects (0,0,0) and (8,8,8). I want to highlight the 3D discretizations (mesh) of cuboid of size (8x8x8) that are intersected by the line. The mesh size is 1x1x1. How to visualize those meshes by a different color in the cuboid.
(Suppose the value of the intersected mesh is 1 and remaining others are zero)
  댓글 수: 5
ac_bm_mat
ac_bm_mat 2022년 4월 29일
Lines are not diagonal. They can have any inclination angle.
Walter Roberson
Walter Roberson 2022년 4월 29일
편집: Walter Roberson 2023년 10월 18일
Then you need to define what it means for the line to intersect a voxel for your purposes. If the line just barely "nicks" a corner of a voxel then should the voxel be included? Should the voxel only be included if the distance between the center of the voxel and the nearest point on the line is at most half of the thickness of the line?

댓글을 달려면 로그인하십시오.

답변 (1개)

Avni Agrawal
Avni Agrawal 2023년 10월 18일
편집: Avni Agrawal 2023년 10월 18일
Hi,
I understand that you are trying to visualize the intersected meshes in the cuboid with a different color :
You can follow the below mentioned steps to achieve the desired output:
1. Define the size of the cuboid and mesh size.
2. Specify the two points that form the line in 3D space.
3. Calculate the direction vector of the line.
4. Determine the number of steps needed to discretize the line based on the mesh size.
5. Normalize the direction vector to obtain the step size for each iteration.
6. Create an empty cuboid.
7. Iterate through each step and mark the intersected meshes as 1 in the cuboid.
8. Create a figure and axis to plot the cuboid.
9. Get the indices of the intersected and remaining meshes.
10. Plot the intersected meshes in red.
11. Plot the remaining meshes in blue.
12. Customize the plot appearance if desired.
To achieve this, you can use the following MATLAB code:
% Define the cuboid size and mesh size
cuboidSize = [8, 8, 8];
meshSize = [1, 1, 1];
% Define the line connecting the two points
lineStart = [0, 0, 0];
lineEnd = [8, 8, 8];
% Calculate the direction vector of the line
direction = lineEnd - lineStart;
% Calculate the number of steps needed to discretize the line based on the mesh size
steps = ceil(norm(direction));
% Normalize the direction vector to obtain the step size for each iteration
stepSize = direction / steps;
% Create an empty cuboid with all zeros
cuboid = zeros(cuboidSize);
% Iterate through each step and mark the intersected meshes as 1
for i = 0:steps
% Calculate the coordinates of the current mesh
meshCoord = lineStart + i * stepSize;
% Calculate the indices of the current mesh
meshIndex = floor(meshCoord ./ meshSize) + 1;
% Mark the intersected mesh as 1
cuboid(meshIndex(1), meshIndex(2), meshIndex(3)) = 1;
end
% Create a figure and axis to plot the cuboid
figure;
axis equal;
hold on;
% Get the indices of the intersected meshes
[x, y, z] = ind2sub(size(cuboid), find(cuboid));
% Plot the intersected meshes in a different color
scatter3(x, y, z, 'filled', 'MarkerFaceColor', 'r');
% Plot the cuboid with a wireframe
box on;
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
This code allows you to visualize the intersected and remaining meshes within the cuboid by representing them with different colors.
You can refer to this MATLAB documentation for more information:
I hope this helps.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by