Random 3D Array of Cylinders with Random Rotation and Fixed Length

3 vues (au cours des 30 derniers jours)
Sam Bottum
Sam Bottum le 29 Nov 2022
Commenté : Sam Bottum le 29 Nov 2022
Hi all,
I am trying to generate a function that creates a 3D array of some number of cylinders of fixed length and radius within a given volume. I want them to be randomly distributed in xyz and have a random rotation, but no overlapping.
I have gotten the rotation part done by randomizing phi and theta in spherical coordinates and then converting to cartesian, but am unsure of where to go from here. Any thoughts would be appreciated!
Note: this currently uses the function "cylinder2p" that someone else has written, which takes an input of the 2 endpoints of a cylinder.
%setting boundaries for orientation
NW_l = 25;
phi_min = 0;
phi_max = 360;
theta_min = 0;
theta_max = 360;
%setting boundaries for max x,y,z position
max_x = 50;
max_y = 50;
max_z = 50;
%setting cylinder2P parameters
facet = 40; %# of facets around cylinder
rad_single = 1; %radius of cylinder/NW
rad_vector = rad_single*(ones(facet,1));
hold on
for n = [1:5]
NW_phi = phi_max+(phi_min-phi_max)*rand(1,1); %random phi
NW_theta = theta_max+(theta_min-theta_max)*rand(1,1); %random theta
[x,y,z]=sph2cart(NW_phi,NW_theta,NW_l); %convert to cartesian coordinates
%quiver3(25-x+150*n,25,25,x,y,z) %plot NW orientation
%quiver3(-x,-y,-z,x,y,z,2,'LineWidth',3,'color','k','ShowArrowHead','off') %plot NW orientation
%r = 0.2;
%[x,y,z] = cylinder(r);
%h = 50;
%z = z*h;
%surf(x,y,z)
p1 = [x y z];
p2 = [-x y -z];
[X,Y,Z]=cylinder2P(rad_vector,facet,p1,p2);
surf(X, Y, Z)
end
%axis equal
hold off
xlabel('x')
ylabel('y')
zlabel('z')
  2 commentaires
Bruno Luong
Bruno Luong le 29 Nov 2022
NOTE random uniform phi/theta is NOT random uniform orientation. Therefore your code generates cylinders orienations that has biased.

Connectez-vous pour commenter.

Réponses (1)

John D'Errico
John D'Errico le 29 Nov 2022
THERE IS NO SIMPLE SOLUTION. Accept that. For example, even for as simple a problem as random sphere packing, where you want to assign spheres "randomly" in a domain so there are no overlaps, there is no simpler solution than to generate sets of sphere centers, and then discard those that overlap, or try to move them so there is no overlap.
But worse, you want to do that with a non-simple object like a cylinder, which has far less trivial shape than a sphere. Sorry.
  3 commentaires
Bruno Luong
Bruno Luong le 29 Nov 2022
Modifié(e) : Bruno Luong le 29 Nov 2022
This is non trivial task as John pointed out.
You can use rejection by computing the distance between random cylinders, or repulsive force to push new random cylinder away from existing cylinders. If you need any assistance on computing the distance between cylinders and to known if they intersect this FEX can be useful. It has also function to compute distrance of cylindet to shpere/point/plane.
Sam Bottum
Sam Bottum le 29 Nov 2022
I feel confident that I can work out the rejection process after generating the array but am really having trouble getting the random spacing at all.

Connectez-vous pour commenter.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by