% Define the size of the cube using NumberOfGridPoints
NumberOfGridPoints = [32, 32, 16];  % [X, Y, Z] dimensions of the cube
Dimensions         = [30, 30, 15];  % Real-world dimensions for the grid

% Generate a random 3D cube based on the NumberOfGridPoints
cube = rand(NumberOfGridPoints(1), NumberOfGridPoints(2), NumberOfGridPoints(3));

% Define the real-world distance range for each axis (X, Y, Z)
x_range = linspace(-Dimensions(1)/2, Dimensions(1)/2, NumberOfGridPoints(1));  % 32 points along X mapped to [-15, 15]
y_range = linspace(-Dimensions(2)/2, Dimensions(2)/2, NumberOfGridPoints(2));  % 32 points along Y mapped to [-15, 15]
z_range = linspace(-Dimensions(3)/2, Dimensions(3)/2, NumberOfGridPoints(3));  % 16 points along Z mapped to [-7.5, 7.5]

% Create a 3D grid of coordinates for the cube using meshgrid
[x, y, z] = meshgrid(x_range, y_range, z_range);

% Define the cylinder's radius and height (in real-world units)
radius = 10;  % Radius in real-world units (e.g., meters)
height = 4;   % Height in real-world units (e.g., meters)

% Get the center of the cube (in real-world coordinates)
center_x = mean(x_range);  % Center of x_range
center_y = mean(y_range);  % Center of y_range
center_z = mean(z_range);  % Center of z_range

% Define the cylindrical mask
cylinder_mask = ((x - center_x).^2 + (y - center_y).^2 <= radius^2) & ...
                (abs(z - center_z) <= height / 2);

% Set everything inside the cylinder to 1 and outside to 0
mask = double(cylinder_mask);

% Multiply the mask by the random cube to apply the cut-off
masked_cube = cube .* mask;

% Visualize the cube and the cylindrical mask using isosurfaces
figure(1);
clf
figure_width = 1200;
set(gcf, 'Position', [100, 100, figure_width, round(figure_width / 1.618)]);
set(gca, 'FontSize', 16, 'Box', 'On', 'Linewidth', 2);

% Set the axis limits to be the same for both subplots
x_limits = [min(x_range), max(x_range)];
y_limits = [min(y_range), max(y_range)];
z_limits = [min(z_range), max(z_range)];

% Visualize the random cube
subplot(1, 2, 1);
isosurface(x, y, z, cube, 0.5); % Isosurface for the random cube
axis([x_limits, y_limits, z_limits]);
title('Original 3D Numerical Grid', 'FontSize', 16);
xlabel('X (\mum)', 'Interpreter', 'tex', 'FontSize', 16);
ylabel('Y (\mum)', 'Interpreter', 'tex', 'FontSize', 16);
zlabel('Z (\mum)', 'Interpreter', 'tex', 'FontSize', 16);
grid on;
axis equal;
view(3);
camlight;
lighting gouraud;

% Visualize the masked cube
subplot(1, 2, 2);
isosurface(x, y, z, masked_cube, 0.5); % Isosurface for the masked cube
axis([x_limits, y_limits, z_limits]);
title('Grid with Custom Cylindrical Cut-off', 'FontSize', 16);
% Add subtitle with radius and height with adjusted position
text('Units', 'normalized', 'Position', [0.5, -0.2], 'String', ...
    ['Radius = ', num2str(radius), ' \mum, Height = ', num2str(height), ' \mum'], ...
    'Interpreter', 'tex', 'FontSize', 14, 'FontWeight', 'normal', 'HorizontalAlignment', 'center');
xlabel('X (\mum)', 'Interpreter', 'tex', 'FontSize', 16);
ylabel('Y (\mum)', 'Interpreter', 'tex', 'FontSize', 16);
zlabel('Z (\mum)', 'Interpreter', 'tex', 'FontSize', 16);
grid on;
axis equal;
view(3);
camlight;
lighting gouraud;