使用Matlab在立方体内随机封闭非重叠球体

时间:2014-08-27 04:28:38

标签: matlab random geometry packing

我正在尝试使用Matlab对立方体中非均匀尺寸的随机封闭填料球进行建模。问题有三个限制因素。首先,生成的随机球体应该在立方体内。其次,球体不应重叠。最后,球体应该封闭。我已经在下面构建的代码中实现了前两个条件。代码如下:

 function [ c r ] = randomSphere( dims )
 % creating one sphere at random inside [0..dims(1)]x[0..dims(2)]x...
 % In general cube dimension would be 10mm*10mm*10mm
 % radius and center coordinates are sampled from a uniform distribution 
 % over the relevant domain.
 %
 % output: c - center of sphere (vector cx, cy,... )
 %         r - radius of sphere (scalar)
 r = 0.15 + ( 0.55 - 0.15) .* rand(1);% radius varies between 0.15mm - 0.55mm
 c = bsxfun(@times,(dims - 2*r) , rand(1,3)) + r; % to make sure sphere is placed inside the cube

 function not_ovlp = nonOver( centers, rads ) % to make sure spheres do not overlap

 if numel( rads ) == 1
     not_ovlp = true;
     return; % nothing to check for a single or the first sphere
 end

 center_dist = sqrt(sum(bsxfun(@minus,centers(1:end-1,:),centers(end,:)).^2,2));
 radsum = rads(end) + rads(1:end-1);
 not_ovlp = all(center_dist >= radsum);

 return;

 function [centers, rads] = sampleSpheres( dims, n ) 
 % main function which is to be called for adding multiple spheres in a cube
 % dims is assumed to be a row vector of size 1-by-ndim
 % For demo take dims = [ 2 2 2 ] and n = 50 

 % preallocate
 ndim = numel(dims);
 centers = zeros( n, ndim );
 rads = zeros( n, 1 );
 ii = 1;
 while ii <= n
      [centers(ii,:), rads(ii) ] = randomSphere( dims );     
      if nonOver( centers(1:ii,:), rads(1:ii) )
           ii = ii + 1; % accept and move on
      end
 end
 disp (centers);
 disp (rads);

从这段代码中,我可以在一个多维数据集中获得随机分布的球体,但它们并没有完全封闭。如果我增加球体的数量(n),那么它就会变成无限循环并且不会产生任何答案。

为了使其封闭包装我的想法是在生成第一个球体后,在为下一个球体选择随机中心(x,y,z)时应该有一个约束。它应该接近第一个球体的位置,依此类推。

只是想知道目标是什么:This is the sort of closed packing am trying to generate

请告诉我,如何实施最后一个约束。

0 个答案:

没有答案