Hi, does anyone know how to generate the graph as shown below based on a torus? Thank you so much.

3 visualizzazioni (ultimi 30 giorni)

Risposte (1)

Umar
Umar il 30 Set 2024

Hi @yuchen,

You mentioned,”does anyone know how to generate the graph as shown below based on a torus?”

Please see my response to your comments below.

I have implemented two different Matlab code snippets in order for you to choose and customize it based on your requirements. Both Code 1 and Code 2 serve to generate a 3D torus shape, but they approach the task differently, particularly in terms of how they handle the input image and the visual output.

Code 1 Breakdown

% Load the original image
img = imread('/MATLAB Drive/IMG_7974.PNG');
%Convert the image to grayscale
grayImg = rgb2gray(img);
% Create a binary mask
binaryMask = imbinarize(grayImg);

For more information on imbinarize function, please refer to

https://www.mathworks.com/help/images/ref/imbinarize.html?searchHighlight=imbinarize&s_tid=srchtitle_support_results_1_imbinarize

% Generate the torus shape
theta = linspace(0, 2*pi, 100); % Angle around the torus
phi = linspace(0, 2*pi, 100);   % Angle around the tube
[Theta, Phi] = meshgrid(theta, phi);
% Define the torus parameters
R = 5; % Major radius
r = 2; % Minor radius
% Parametric equations for the torus
X = (R + r * cos(Phi)) .* cos(Theta);
Y = (R + r * cos(Phi)) .* sin(Theta);
Z = r * sin(Phi);
% Plot the torus
figure;
surf(X, Y, Z, 'FaceColor', 'interp', 'EdgeColor', 'none');
axis equal;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Torus Shape Generated from Image');
view(3);
grid on;

Image Loading and Processing: Loads an image and converts it to grayscale, then creates a binary mask. However, the binary mask is not utilized further in generating the torus.

Torus Generation: Defines parameters for the torus (major radius R and minor radius r) and uses parametric equations to compute X,Y and Z coordinates for plotting the torus.

Visualization: Utilizes MATLAB’s surf function to create a surface plot of the torus without any image texture. For more information on surf function, please refer to:

https://www.mathworks.com/help/matlab/ref/surf.html

Please see attached.

Code 2 Breakdown

%Define the path to the image
imagePath = '/MATLAB Drive/IMG_7974.PNG';
% Read the image
img = imread(imagePath);
% Create a figure
figure;
% Define parameters for the torus
R = 3; % Major radius
r = 1; % Minor radius
[theta, phi] = meshgrid(linspace(0, 2*pi, 100), linspace(0, 2*pi, 50));
% Parametric equations for the torus
X = (R + r * cos(phi)) .* cos(theta);
Y = (R + r * cos(phi)) .* sin(theta);
Z = r * sin(phi);
% Create the torus surface
surf(X, Y, Z, 'FaceColor', 'texturemap', 'CData', img, 'EdgeColor', 'none');
% Set axis properties
axis equal;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Torus with Embedded Image');
view(3);
grid on;
% Adjust lighting
light;
lighting gouraud;

Image Loading: Similar to Code 1, it loads an image but does not convert it to grayscale or create a binary mask.

Torus Generation: Defines different radii for the torus, which results in a smaller torus compared to Code 1. Also uses parametric equations for generating coordinates for plotting.

Visualization with Image Texture: The key difference is that this code employs CData to map the loaded image onto the surface of the torus using texturemap creating a visually integrated representation of the image on the toroidal shape.

Please see attached.

Both codes address the requirement to generate a toroidal graph based on an image; however, their execution differs significantly:

Code 1 focuses primarily on creating a basic 3D torus shape without incorporating any visual data from the image itself, which might not meet your expectations if you seek an integrated visual output. However, Code 2, in contrast, effectively meets your requirements by embedding the image as a texture on the surface of the torus. This approach enhances visual appeal and fulfills yours desire for a graphical representation that incorporates elements from the original image. Now, depending on the size of img, performance may vary. Also, larger images can lead to longer rendering times or memory issues. If you notice Code 2 adds lighting effects (light and lighting gouraud), which can enhance depth perception and realism in visualization—an important aspect when presenting graphical data.

Hope this helps.

Please let me know if you have any further questions.

  2 Commenti
yuchen
yuchen il 1 Ott 2024
Sorry, you just provided the code for a torus, what i need is a half torus and been cut along two circles, just like the picture I showed. Thank you.
Umar
Umar il 1 Ott 2024

Hi @yuchen,

Based on your comments, again I created two separate different codes because based on your comments, your interpretation could be different than mine. The first code generates a torus split into left and right halves, while the second code illustrates the right half of the torus further divided into top and bottom sections. Following below shows step-by-step breakdown of each code.

Code 1: Left and Right Halves of a Torus

Figure Creation



figure;



This command initializes a new figure window for plotting.

Parameter Definition:



 R = 3; % Major radius r = 1; % Minor radius offset = 3; % Offset to separate the halves


Here, R is the major radius (distance from the center of the tube to the center of the torus), r is the minor radius (radius of the tube), and offset is used to separate the left and right halves visually.

Meshgrid Creation



 [theta_left, phi] = meshgrid(linspace(-pi/2, pi/2, 100), linspace(0, 2*pi, 50)); % Left half [theta_right, phi] = meshgrid(linspace(pi/2, 3*pi/2, 100), linspace(0, 2*pi, 50)); % Right half


The meshgrid function generates matrices for theta and phi, which are the angles used in the parametric equations. The left half spans from -π/2 to π/2, while the right half spans from π/2 to 3π/2.

Parametric Equations for the Left Half:



 X_left = (R + r * cos(phi)) .* cos(theta_left) - offset; Y_left = (R + r * cos(phi)) .* sin(theta_left); Z_left = r * sin(phi);


These equations calculate the Cartesian coordinates for the left half of the torus. The offset shifts the left half to the left.

Parametric Equations for the Right Half:



X_right = (R + r * cos(phi)) .* cos(theta_right) + offset; 
Y_right = (R + r * cos(phi)) .* sin(theta_right);
Z_right = r * sin(phi);


Similar to the left half, but the right half is shifted to the right by the offset.

Surface Plotting:



 surf(X_left, Y_left, Z_left, 'FaceColor', 'b', 'EdgeColor', 'none'); hold on; surf(X_right, Y_right, Z_right, 'FaceColor', 'r', 'EdgeColor', 'none');


The surf function creates a 3D surface plot for both halves of the torus, with the left half colored blue and the right half red.

Axis Properties and Visualization:



 axis equal; xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis'); title('Torus with Left Half Shifted from Right Half'); view(3); grid on; light; lighting gouraud; legend('Left Half', 'Right Half'); alpha(0.8);


These commands set the axis properties, labels, title, and enable grid and lighting for better visualization. The alpha function adds transparency to the surfaces.

Code 2: Split Right Half of the Torus

New Figure Creation:



figure;


This command opens a new figure window for the right half of the torus.

Parameter Definition:



R = 3; % Major radius
r = 1; % Minor radius
separation = 0.5; % Separation distance in inches (adjust as 
needed)


Similar to Code 1, but here separation is introduced to vertically offset the top half of the torus.

Meshgrid for Bottom Half:



 [theta, phi] = meshgrid(linspace(0, pi, 100), linspace(-pi/2, 0, 50)); 
 This creates a grid for the bottom half of the right torus, covering half the phi range.

Parametric Equations for Bottom Half:



X_bottom = (R + r * cos(phi)) .* cos(theta); 
Y_bottom = (R + r * cos(phi)) .* sin(theta); 
Z_bottom = r * sin(phi); 


These equations calculate the coordinates for the bottom half of the right torus.

Surface Plot for Bottom Half:


surf(X_bottom, Y_bottom, Z_bottom, 'FaceColor', 'g', 'EdgeColor',
'none'); 
hold on; 


The bottom half is plotted in green.

Meshgrid for Top Half



 [theta_top, phi_top] = meshgrid(linspace(0, pi, 100), linspace(0, pi/2, 50));


This creates a grid for the top half of the right torus.

Parametric Equations for Top Half:



X_top = (R + r * cos(phi_top)) .* cos(theta_top); 
Y_top = (R + r * cos(phi_top)) .* sin(theta_top); 
Z_top = r * sin(phi_top) + separation; 


The top half is calculated with an additional separation to shift it upwards.

Surface Plot for Top Half:



surf(X_top, Y_top, Z_top, 'FaceColor', 'y', 'EdgeColor', 'none'); 


The top half is plotted in yellow.

Final Plot Adjustments:


axis equal; 
xlabel('X-axis'); 
ylabel('Y-axis'); 
zlabel('Z-axis'); 
title('Split Right Half Torus'); 
view(3); 
grid on; 
light; 
lighting gouraud; 
legend('Bottom Half', 'Top Half'); 
alpha(0.8); 


Similar to Code 1, these commands finalize the plot settings.

Both codes effectively illustrate different aspects of a torus. Code 1 presents a torus split into left and right halves, addressing your comments about the torus being cut along two circles by visually separating the halves. Code 2 further divides the right half into top and bottom sections, providing a more detailed view of the toroidal structure.

Full Code 1: Left and Right Halves of a Torus

% Create a figure
figure;
% Define parameters for the torus
R = 3; % Major radius
r = 1; % Minor radius
offset = 3; %Offset to separate the halves 
% Adjust the ranges for theta and phi to create both halves of      the torus
[theta_left, phi] = meshgrid(linspace(-pi/2, pi/2, 100),      linspace(0, 2*pi, 50)); % Left half
[theta_right, phi] = meshgrid(linspace(pi/2, 3*pi/2, 100),         linspace(0, 2*pi, 50)); % Right half
% Parametric equations for the left half of the torus
X_left = (R + r * cos(phi)) .* cos(theta_left) - offset; % Shift      left half by 'offset'
Y_left = (R + r * cos(phi)) .* sin(theta_left);
Z_left = r * sin(phi);
% Parametric equations for the right half of the torus
X_right = (R + r * cos(phi)) .* cos(theta_right) + offset; %      Shift right half by 'offset'
Y_right = (R + r * cos(phi)) .* sin(theta_right);
Z_right = r * sin(phi);
% Create the torus surface for the left half
surf(X_left, Y_left, Z_left, 'FaceColor', 'b', 'EdgeColor', 
'none'); % Blue color for left half
hold on; % Retain the current plot
% Create the torus surface for the right half
surf(X_right, Y_right, Z_right, 'FaceColor', 'r', 'EdgeColor',       'none'); % Red color for right half
% Set axis properties
axis equal; % Ensure equal scaling on all axes
xlabel('X-axis'); % Label for X-axis
ylabel('Y-axis'); % Label for Y-axis
zlabel('Z-axis'); % Label for Z-axis
title('Torus with Left Half Shifted from Right Half'); % Title of      the figure
view(3); % Set the view to 3D
grid on; % Enable grid for better visualization
% Adjust lighting
light; % Add a light source
lighting gouraud; % Use Gouraud lighting for smooth shading
% Add a legend for clarity
legend('Left Half', 'Right Half'); % Legend to differentiate the      halves
% Optional: Improve visual appearance by adding transparency
alpha(0.8); % Set transparency level if desired

Please see attached.

Code 2: Split Right Half of the Torus

% Create a new figure for the right half torus
figure;
% Define parameters for the torus
R = 3; % Major radius
r = 1; % Minor radius
separation = 0.5; % Separation distance in inches (adjust as 
needed)
% Adjust the ranges for theta and phi to create the bottom half      of the right half torus
[theta, phi] = meshgrid(linspace(0, pi, 100), linspace(-pi/2, 0,     50)); % Full range for theta, half range for phi (bottom half)
% Parametric equations for the bottom half of the right half 
torus
X_bottom = (R + r * cos(phi)) .* cos(theta); % X-coordinates for 
bottom half
Y_bottom = (R + r * cos(phi)) .* sin(theta); % Y-coordinates for
 bottom half
Z_bottom = r * sin(phi); % Z-coordinates for bottom half
% Create the surface plot for the bottom half of the right half 
   torus
surf(X_bottom, Y_bottom, Z_bottom, 'FaceColor', 'g', 'EdgeColor',
   'none'); % Green color for the bottom half
hold on; % Retain the current plot
% Adjust the ranges for theta and phi to create the top half of  
     the right half torus
[theta_top, phi_top] = meshgrid(linspace(0, pi, 100), linspace(0,
              pi/2, 50)); % Full range for theta, half range for    phi (top 
half)
% Parametric equations for the top half of the right half torus
X_top = (R + r * cos(phi_top)) .* cos(theta_top); % X-coordinates
                for top half
Y_top = (R + r * cos(phi_top)) .* sin(theta_top); % Y-coordinate 
for top half
Z_top = r * sin(phi_top) + separation; % Z-coordinates for top 
half, shifted by separation
% Create the surface plot for the top half of the right half   
 torus
surf(X_top, Y_top, Z_top, 'FaceColor', 'y', 'EdgeColor', 'none');
 % Yellow color for the top half
% Set the properties for the plot
axis equal; % Ensure equal scaling on all axes
xlabel('X-axis'); % Label for X-axis
ylabel('Y-axis'); % Label for Y-axis
zlabel('Z-axis'); % Label for Z-axis
title('Split Right Half Torus'); % Title of the figure
view(3); % Set the view to 3D
grid on; % Enable grid for better visualization
light; % Add a light source
lighting gouraud; % Use Gouraud lighting for smooth shading
legend('Bottom Half', 'Top Half'); % Legend to differentiate the
 halves
alpha(0.8); % Set transparency level if desired

Please see attached.

Please let me know if you have any further questions.

Accedi per commentare.

Categorie

Scopri di più su Lighting, Transparency, and Shading in Help Center e File Exchange

Tag

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by