Creating Randomly Placed Cubes

Please login with a confirmed email address before reporting spam

I am trying to create an app through app builder in which I can use assigned values within input fields to randomly generate cubes (lysosomes) inside of my defined rectangular channel with a specific material so that I can continue to run simulations. I want to click a button that is attached to a method to perform this action. I have MatLab code that creates the particles how I wanted to but I am unsure how to convert the code so that it would work in the method. I have all of my parameters linked to an input field within the app builder form as well. If using LiveLink would make more sense to do I would appreciate getting help on how to do that as well.

Here is the working MatLab code:

% Rectangular Prism Placement in Rectangular Area Without Overlap Script

% Clear workspace and command window clc; clear;

% Prompt user for dimensions of the channel length_mm = input('Enter the length of the channel (in mm): '); width_mm = input('Enter the width of the channel (in mm): '); height_mm = input('Enter the height of the channel (in mm): ');

% Prompt user for dimensions of the lysosomes rect_length_um = input('Enter the length of the lysosome (in micrometers): '); rect_width_um = input('Enter the width of the lysosome (in micrometers): '); rect_height_um = input('Enter the height of the lysosome (in micrometers): ');

% Convert dimensions to micrometers for calculations length_mm_um = length_mm * 1000; % mm to um width_mm_um = width_mm * 1000; % mm to um height_mm_um = height_mm * 1000; % mm to um

% Prompt user for the number of prisms num_lysosomes = input('Enter the number of lysosomes to place: ');

% Validate input if length_mm_um <= 0 || width_mm_um <= 0 || height_mm_um <= 0 || num_lysosomes <= 0 || ... rect_length_um <= 0 || rect_width_um <= 0 || rect_height_um <= 0 disp('All dimensions must be positive numbers and the number of lysosomes must be a positive integer.'); else % Initialize arrays for lysosome positions lysosome_positions = zeros(num_lysosomes, 3); % Columns for x, y, z positions placed_lysosomes = 0; % Counter for placed prisms

while placed_lysosomes < num_lysosomes
    % Generate a random position for a new rectangle
    x = rand() * (length_mm_um - rect_length_um);
    y = rand() * (width_mm_um - rect_width_um);
    z = rand() * (height_mm_um - rect_height_um); % Random height placement

    % Check for overlap with existing rectangles
    overlap = false;
    for j = 1:placed_lysosomes
        if (x < lysosome_positions(j, 1) + rect_length_um && ...
            x + rect_length_um > lysosome_positions(j, 1) && ...
            y < lysosome_positions(j, 2) + rect_width_um && ...
            y + rect_width_um > lysosome_positions(j, 2) && ...
            z < lysosome_positions(j, 3) + rect_height_um && ...
            z + rect_height_um > lysosome_positions(j, 3))
            overlap = true;
            break;
        end
    end

    % If no overlap, place the rectangle
    if ~overlap
        placed_lysosomes = placed_lysosomes + 1;
        lysosome_positions(placed_lysosomes, :) = [x, y, z];
    end
end

% Plot the rectangles in 3D
figure;
hold on;
for i = 1:placed_lysosomes
    % Define the corners of the rectangle in mm
    x = lysosome_positions(i, 1) / 1000; % Convert from um to mm
    y = lysosome_positions(i, 2) / 1000; % Convert from um to mm
    z = lysosome_positions(i, 3) / 1000; % Convert from um to mm

    % Create a rectangular prism using patch
    % Correctly define vertices for the lysosomes
    l = rect_length_um / 1000; % Convert length to mm
    w = rect_width_um / 1000;   % Convert width to mm
    h = rect_height_um / 1000;  % Convert height to mm

    vertices = [0 0 0; l 0 0; l w 0; 0 w 0; ...
                0 0 h; l 0 h; l w h; 0 w h]; % Define vertices
    faces = [1 2 3 4; 5 6 7 8; 1 2 6 5; 2 3 7 6; ...
             3 4 8 7; 4 1 5 8];

    % Transform the lysosome to its position
    for j = 1:size(faces, 1)
        patch('Vertices', vertices + [x, y, z], ...
              'Faces', faces, ...
              'FaceColor', rand(1, 3), ...
              'FaceAlpha', 0.5);
    end
end

% Set plot limits
xlim([0, length_mm]); % Limits in mm
ylim([0, width_mm]);  % Limits in mm
zlim([0, height_mm]); % Limits in mm

% Set equal scaling for all axes
daspect([1 1 1]);

% Labels and title
xlabel('X (mm)');
ylabel('Y (mm)');
zlabel('Z (mm)');
title('Random Placement of Lysosomes');
grid on;
view(3);
hold off;

end

I also have attached pictures of what the form in my app look like. Any help on this would be greatly appreciated.



0 Replies Last Post Nov 6, 2024, 6:29 p.m. EST
COMSOL Moderator

Hello Eric Nelson

Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.

If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.

Reply

Please read the discussion forum rules before posting.

Please log in to post a reply.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.