Intersection of a line and 3D mesh

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 Comments

See your other question.
Voxel representation is okay but can you please help me in determining if a line intersects a voxel or not?
Is the task always going to be for perfectly diagonal lines in 3 space? Or could it involve slightly different angles?
Perfectly diagonal lines in 3-space only pass through (K,K,K), K=0:8 .
Perfectly diagonal lines in flat axis-aligned 2-space seconds only pass through (K,K,Z) or (K,Y,K) or (X,K,K)
Any other case, you start intersecting "part" of a voxel, and then you have to start asking questions about how thick the line is and how much of a voxel needs to be occupied before you want to turn on the voxel.
Lines are not diagonal. They can have any inclination angle.
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?

Sign in to comment.

Answers (1)

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.

Products

Release

R2020b

Asked:

on 29 Apr 2022

Commented:

on 18 Oct 2023

Community Treasure Hunt

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

Start Hunting!