How to obtain a 2D slice of the intersection of a 3D object made of a list of vertices, with a plan?

7 次查看(过去 30 天)
Hello,
My goal is to obtain a cross-section (2D slice) of this 3D crystal shape, itself having various shapes inside (in this example, only one cube), with a plan (here, a bit tilted but almost the a-c plan).
To plot the crystal shape (external surface), I'm using the MTEX toolbox, which defines a crystalShape as such: "Internally, a crystal shape is represented as a list of faces which are bounded by a list of vertices" (see Crystal Shapes (The Class crystalShape) (DocHelp Toolbox) (mtex-toolbox.github.io)).
Then inside is just using the plotcube function. The plan is plotted using the patch function.
I'm having troubles to obtain this 2D slice as previous posts suggesting using the slice or contour function apply it to a meshgrid, and I don't have that here.
Thank you in advance for your help,
Sincerely,
Sarah

回答(1 个)

Karan Singh
Karan Singh 2023-9-5
Hi Sarah,
Based on the code provided, it seems that you have a 3D crystal shape defined using the MTEX toolbox. The crystal shape is represented as a list of faces bounded by a list of vertices. Additionally, you have a cube and a tilted plane that you want to plot alongside the crystal shape.
Now, you're looking to obtain a 2D cross-section or slice of this 3D crystal shape. Specifically, you want to obtain a 2D slice of the crystal shape along a plane (in this case, a slightly tilted plane representing the a-c plane).
To obtain a cross-section (2D slice) of the 3D crystal shape, you can use the slice function in MATLAB. However, since the crystal shape is not defined on a regular meshgrid, you can create a meshgrid manually using the meshgrid function. You can find a demo code for the same below:
% Define the dimensions of the crystal shape
xMin = -1;
xMax = 1;
yMin = -1;
yMax = 1;
zMin = -1;
zMax = 1;
% Create a meshgrid manually
[X, Y, Z] = meshgrid(linspace(xMin, xMax, 100), linspace(yMin, yMax, 100), linspace(zMin, zMax, 100));
% Evaluate the crystal shape at each point in the meshgrid
shape = zeros(size(X));
for i = 1:numel(X)
point = [X(i), Y(i), Z(i)];
if isInside(cS, point)
shape(i) = 1;
end
end
% Create a 2D slice by fixing one of the dimensions
slicePlane = 0.1; % Choose the desired plane position
sliceIndex = find(abs(Z(1, 1, :) - slicePlane) < 1e-6, 1);
% Plot the 2D slice
figure;
slice(X, Y, Z, shape, [], [], Z(1, 1, sliceIndex));
colormap(gray);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('2D Slice of Crystal Shape');
hold on;
% Plot the cube
P = [0 0 0]; % Center point of the cube
L = [0.2 0.2 0.6]; % Dimensions of the cube
O = P - L/2; % Get the origin of the cube so that P is at the center
plotcube(L, O, 1, ol_skelC); % Use function plotcube (you can specify alpha after O, e.g., 0.8)
hold off;
This code manually creates a meshgrid using linspace and evaluates the crystal shape at each point in the meshgrid. The isInside function is used to determine if a point is inside the crystal shape. While the isInside function is not a built-in MATLAB or MTEX fuction, it checks if the point is inside the crystal shape. The rest of the code remains the same, plotting the 2D slice with the crystal shape and the cube.
Attached below are some documentation links that you may find helpful:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Triangulation Representation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by