【问题标题】:Random uniform distribution of points with minimum spacing以最小间距随机均匀分布点
【发布时间】:2019-11-07 20:23:43
【问题描述】:

我正在尝试生成二维空间中一组点的坐标,这些点随机均匀分布,但彼此之间不太接近。

我从np.random.uniform 开始,生成 n x 2 值(x 和 y 坐标),然后使用两个嵌套的 for 循环在所有坐标上筛选坐标列表,以删除太近的点并随机放置它们其他地方:

# Generate xy coordinates for the grafting points
rng = np.random.RandomState(seed=self.rng_seed)
    coordinates = rng.uniform(high=(self.box_size[0], self.box_size[1]), size=(n_chains, 2))

for count in range(0, self.max_overlap_iter):
    moved_bead = False
    # Search for overlapping beads by looping over the list doubly
    for id_i, coord_i in enumerate(coordinates):
        for id_j, coord_j in enumerate(coordinates):
            if not id_i == id_j and np.sqrt(sum((coord_i - coord_j)**2)) < self.bead_size:
                # Move the second point
                coordinates[id_j] = rng.uniform(high=(self.box_size[0], self.box_size[1]), size=2)
                moved_bead = True
    if not moved_bead:
        break

一个点被移动到一个新的随机位置后,它必须再次通过外循环,因为它可能仍然重叠。

问题在于,当点的密度足够高时,这会变得非常慢,因为某些点“重叠”的概率会飙升。因此,我必须构建最大数量的迭代,但这显然不是我的问题的解决方案。

有没有更快、更有效的方法来做到这一点?

【问题讨论】:

  • 这些点是“随机的”有多重要 - 即,它会影响您的应用程序在某种网格上生成您的点,并对每个珠子应用高斯位移吗?

标签: python numpy


【解决方案1】:

我最终编写了一个泊松盘点集生成器算法,该算法可以生成非最大点集并在线性时间内运行,使用了我从其他算法中获得的一些想法。

当然感谢@Kamil 给了我术语“泊松磁盘点集”给 Google ;)

可以在这里找到:https://github.com/Compizfox/MDBrushGenerators/blob/master/PoissonDiskGenerator.py

【讨论】:

  • @RahatZaman 抱歉,我重新组织了存储库。我又修复了链接。
【解决方案2】:

您是否尝试过使用 Poisson-Disc 采样算法?

我想这可能就是你要找的东西。

Python implementation

Jason Davies Poisson-Disc Sampling

Mike Bostock implementation in Javascript

以下是删除时的代码

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500;

var sample = poissonDiscSampler(width, height, 10);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.timer(function() {
  for (var i = 0; i < 10; ++i) {
    var s = sample();
    if (!s) return true;
    svg.append("circle")
        .attr("cx", s[0])
        .attr("cy", s[1])
        .attr("r", 0)
      .transition()
        .attr("r", 2);
  }
});

// Based on https://www.jasondavies.com/poisson-disc/
function poissonDiscSampler(width, height, radius) {
  var k = 30, // maximum number of samples before rejection
      radius2 = radius * radius,
      R = 3 * radius2,
      cellSize = radius * Math.SQRT1_2,
      gridWidth = Math.ceil(width / cellSize),
      gridHeight = Math.ceil(height / cellSize),
      grid = new Array(gridWidth * gridHeight),
      queue = [],
      queueSize = 0,
      sampleSize = 0;

  return function() {
    if (!sampleSize) return sample(Math.random() * width, Math.random() * height);

    // Pick a random existing sample and remove it from the queue.
    while (queueSize) {
      var i = Math.random() * queueSize | 0,
          s = queue[i];

      // Make a new candidate between [radius, 2 * radius] from the existing sample.
      for (var j = 0; j < k; ++j) {
        var a = 2 * Math.PI * Math.random(),
            r = Math.sqrt(Math.random() * R + radius2),
            x = s[0] + r * Math.cos(a),
            y = s[1] + r * Math.sin(a);

        // Reject candidates that are outside the allowed extent,
        // or closer than 2 * radius to any existing sample.
        if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) return sample(x, y);
      }

      queue[i] = queue[--queueSize];
      queue.length = queueSize;
    }
  };

  function far(x, y) {
    var i = x / cellSize | 0,
        j = y / cellSize | 0,
        i0 = Math.max(i - 2, 0),
        j0 = Math.max(j - 2, 0),
        i1 = Math.min(i + 3, gridWidth),
        j1 = Math.min(j + 3, gridHeight);

    for (j = j0; j < j1; ++j) {
      var o = j * gridWidth;
      for (i = i0; i < i1; ++i) {
        if (s = grid[o + i]) {
          var s,
              dx = s[0] - x,
              dy = s[1] - y;
          if (dx * dx + dy * dy < radius2) return false;
        }
      }
    }

    return true;
  }

  function sample(x, y) {
    var s = [x, y];
    queue.push(s);
    grid[gridWidth * (y / cellSize | 0) + (x / cellSize | 0)] = s;
    ++sampleSize;
    ++queueSize;
    return s;
  }
}

</script>

【讨论】:

  • 谢谢,起初这看起来很有希望,但我认为我不能使用它,因为我需要能够指定平均密度。 (我忘了在我的问题中提到这一点。)我认为你不能用泊松圆盘采样来做到这一点。您只能指定r,即点之间的最小距离。
  • @Compizfox 没有什么能阻止您定义一个将密度作为输入并计算所需r 的函数。很简单:r = sqrt(PI/density)。该公式给出的半径稍大,但对于更高的密度应该没问题
  • 变化的r 是不一样的,因为这会改变点之间的最小距离。我想独立于r 改变点密度。换句话说,我生成的 Poisson-Disk 点集不应该(总是)是最大的。不过,有了关于泊松磁盘点集的这些信息,我想我可以想出一些比我现在拥有的“蛮力”算法更有效的算法来生成非最大点集。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2021-12-28
  • 2011-08-08
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多