How do I make a 3D model using Matlab Patch function?

51 views (last 30 days)
Karina Coman
Karina Coman on 9 Apr 2024 at 16:04
Answered: Jaynik on 17 Apr 2024 at 5:24
Hello,
So I have a problem using patch function. So bascally, I am trying to code for a truss pratt bridge, whereby I have all my nodes. Yet, I want each member to look 3D, as in to have some thickness and dimension rather than being just a straight line which is what I have right now. Any help? Also, I need to determine the loading using DSM, if there is any link you know helpful please let me know, i need to set each member to be coloured regarding to the force it experiences when a truck is driving through.
Here is my code !
Height=8.66;
Nodes_f= [ 0 6 0; % Node 1
10 6 0; % Node 2
20 6 0; % Node 3
30 6 0; % Node 4
40 6 0; % Node 5
50 6 0; % Node 6
5 6 Height; % Node 7
15 6 Height; % Node 8
25 6 Height; % Node 9
35 6 Height; % Node 10
45 6 Height; % Node 11
5 6 0; % Node 12
10 6 Height; % Node 13
15 6 0; % Node 14
20 6 Height; % Node 15
25 6 0; % Node 16
30 6 Height; % Node 17
35 6 0; % Node 18
40 6 Height; % Node 19
45 6 0; % Node 20
];
faces=[ 1 2; 1 7; 7 2; 7 8; 2 8; 2 3; 8 3; 8 9; 3 4;
3 9; 9 4; 4 5; 9 10; 4 10; 10 5; 5 11; 10 11;
5 6; 11 6; 12 7; 2 13; 8 14; 3 15; 9 16; 4 17;
10 18; 5 19; 11 20];
c = [0.8500 0.3250 0.0980]
patch('Faces',faces,'Vertices',Nodes_f,'Edgecolor',c,'LineWidth',2);
view(3)
colorbar
hold on;
Thank you !

Answers (1)

Jaynik
Jaynik on 17 Apr 2024 at 5:24
Hi Karina,
According to me, there is no direct function to plot the bridge in 3D with some thickness and dimension. You will have to custom code for plotting in 3D. Here is a sample code which uses rectangular beams to construct the bridge:
beamWidth = 2; % Width of the beam in the same units as your nodes
beamThickness = 0.2; % Thickness of the beam
figure;
hold on;
for i = 1:size(faces, 1)
startNode = Nodes_f(faces(i, 1), :);
endNode = Nodes_f(faces(i, 2), :);
draw3DBeam(startNode, endNode, beamWidth, beamThickness, c);
end
view(3);
axis equal;
grid on;
function draw3DBeam(startNode, endNode, width, thickness, color)
% Direction vectors
dirVector = endNode - startNode;
dirVector = dirVector / norm(dirVector); % Normalize
upVector = [0, 0, 1]; % Up vector
% Perpendicular vector to the direction and up vector
perpVector = cross(dirVector, upVector);
perpVector = perpVector / norm(perpVector); % Normalize
% Four corners of the beam
corner1 = startNode + width/2 * perpVector + thickness/2 * upVector;
corner2 = startNode - width/2 * perpVector + thickness/2 * upVector;
corner3 = startNode - width/2 * perpVector - thickness/2 * upVector;
corner4 = startNode + width/2 * perpVector - thickness/2 * upVector;
% End nodes of the beam
corner5 = endNode + width/2 * perpVector + thickness/2 * upVector;
corner6 = endNode - width/2 * perpVector + thickness/2 * upVector;
corner7 = endNode - width/2 * perpVector - thickness/2 * upVector;
corner8 = endNode + width/2 * perpVector - thickness/2 * upVector;
% Vertices and faces for the patch
vertices = [corner1; corner2; corner3; corner4; corner5; corner6; corner7; corner8];
faces = [1 2 3 4; 5 6 7 8; 1 5 8 4; 2 6 7 3; 1 5 6 2; 4 8 7 3];
patch('Faces',faces,'Vertices',vertices,'FaceColor',color, 'EdgeColor', 'none');
end
You will need to change the implementation of draw3DBeam based on your specific requirements.
To perform further analysis for the truss bridge, you can try exploring these from File Exchange:
Hope this helps!

Tags

Community Treasure Hunt

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

Start Hunting!