Random 3D Array of Cylinders with Random Rotation and Fixed Length
3 views (last 30 days)
Show older comments
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 Comments
Bruno Luong
on 29 Nov 2022
NOTE random uniform phi/theta is NOT random uniform orientation. Therefore your code generates cylinders orienations that has biased.
Answers (1)
John D'Errico
on 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 Comments
Bruno Luong
on 29 Nov 2022
Edited: Bruno Luong
on 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.
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!