3.1.87 • Published 2 days ago

@thi.ng/poisson v3.1.87

Weekly downloads
166
License
Apache-2.0
Repository
github
Last release
2 days ago

poisson

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

example screenshot

nD Stratified grid and Poisson disc sampling with support for variable spatial density, custom PRNGs (via @thi.ng/random's IRandom interface & implementations) and customizable quality settings.

The Poisson disc sampler requires a spatial index and we recommend using KdTreeSet from the @thi.ng/geom-accel package to speed up the sampling process, but other ISpatialSet-compatible indices are supported as well...

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/geom - Functional, polymorphic API for 2D geometry types & SVG generation
  • @thi.ng/geom-voronoi - Fast, incremental 2D Delaunay & Voronoi mesh implementation
  • @thi.ng/lowdisc - n-dimensional low-discrepancy sequence generators/iterators
  • @thi.ng/random - Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation

Installation

yarn add @thi.ng/poisson

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/poisson"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const poisson = await import("@thi.ng/poisson");

Package sizes (gzipped, pre-treeshake): ESM: 493 bytes

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Poisson-disk shape-aware sampling, Voronoi & Minimum Spanning Tree visualizationDemoSource
2D Poisson-disc sampler with procedural gradient mapDemoSource
2D Stratified grid sampling exampleDemoSource

API

Generated API docs

Poisson disc sampling

The package provides a single function samplePoisson() and the following options to customize the sampling process:

  • points: Point generator function. Responsible for producing a new candidate point within user defined bounds using provided RNG.
  • density: Density field function. Called for each new candidate point created by point generator and should return the poisson disc exclusion radius for the given point location. The related candidate point can only be placed if no other points are already existing within the given radius/distance. If this option is given as number, uses this value to create a uniform distance field.
  • index: Spatial indexing implementation for nearest neighbor searches of candidate points. Currently only @thi.ng/geom-accel types are supported. The data structure is used to store all successful sample points. Furthermore, pre-seeding the data structure allows already indexed points to participate in the sampling process and so can be used to define exclusion zones. It also can be used as mechanism for progressive sampling, i.e. generating a large number of samples and distributing the process over multiple invocations of smaller sample sizes (see max option) to avoid long delays.
  • max: Max number of samples to produce. Must be given, no default.
  • jitter?: Step distance for the random walk each failed candidate point is undergoing. This distance should be adjusted depending on overall sampling area/bounds. Default: 1
  • iter?: Number of random walk steps performed before giving up on a candidate point. Increasing this value improves overall quality. Default: 1
  • quality?: Number of allowed failed consecutive candidate points before stopping entire sampling process (most likely due to not being able to place any further points). As with the iter param, increasing this value improves overall quality, especially in dense regions with small radii. Default: 500
  • rnd?: Random number generator instance. Default: @thi.ng/random SYSTEM (aka Math.random)

example output

import { asSvg, circle, svgDoc } from "@thi.ng/geom";
import { KdTreeSet } from "@thi.ng/geom-accel";
import { fit01 } from "@thi.ng/math";
import { samplePoisson } from "@thi.ng/poisson";
import { dist, randMinMax2 } from "@thi.ng/vectors";

const index = new KdTreeSet(2);

const pts = samplePoisson({
    index,
    points: () => randMinMax2(null, [0, 0], [500, 500]),
    density: (p) => fit01(Math.pow(dist(p, [250, 250]) / 250, 2), 2, 10),
    iter: 5,
    max: 8000,
    quality: 500,
});

// use thi.ng/geom to visualize results
// each circle's radius is set to distance to its nearest neighbor
const circles = pts.map((p) =>
    circle(p, dist(p, index.queryKeys(p, 40, 2)[1]) / 2)
);

document.body.innerHTML = asSvg(
    svgDoc({ fill: "none", stroke: "blue" }, ...circles)
);

Stratified grid sampling

The stratifiedGrid function can produce nD grid samples based on the following config options:

  • dim: nD vector defining grid size (in cells)
  • samples?: Number of samples per grid cell (default: 1)
  • rnd?: Random number generator instance. Default: @thi.ng/random SYSTEM (aka Math.random)

example output

import { asSvg, circle, svgDoc } from "@thi.ng/geom";
import { KdTreeSet } from "@thi.ng/geom-accel";
import { stratifiedGrid } from "@thi.ng/poisson";
import { map } from "@thi.ng/transducers";
import { dist } from "@thi.ng/vectors";

const index = new KdTreeSet(2);
index.into(stratifiedGrid({ dim: [50, 50], samples: 1 }));

document.body.innerHTML = asSvg(
    svgDoc(
        {
            width: 600,
            height: 600,
            fill: "none",
            stroke: "blue",
            "stroke-width": 0.1,
        },
        ...map(
            (p) => circle(p, dist(p, index.queryKeys(p, 2 * Math.SQRT2, 2)[1]) / 2),
            index.keys()
        )
    )
);

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-poisson,
  title = "@thi.ng/poisson",
  author = "Karsten Schmidt",
  note = "https://thi.ng/poisson",
  year = 2016
}

License

© 2016 - 2021 Karsten Schmidt // Apache Software License 2.0

3.1.87

2 days ago

3.1.86

5 days ago

3.1.85

8 days ago

3.1.84

17 days ago

3.1.83

20 days ago

3.1.82

30 days ago

3.1.81

1 month ago

3.1.80

1 month ago

3.1.79

1 month ago

3.1.78

1 month ago

3.1.77

1 month ago

3.1.76

1 month ago

3.1.75

2 months ago

3.1.74

2 months ago

3.1.73

2 months ago

3.1.72

2 months ago

3.1.71

2 months ago

3.1.70

2 months ago

3.1.67

2 months ago

3.1.69

2 months ago

3.1.68

2 months ago

3.1.66

2 months ago

3.1.65

2 months ago

3.1.64

2 months ago

3.1.63

3 months ago

3.1.62

3 months ago

3.1.59

3 months ago

3.1.61

3 months ago

3.1.60

3 months ago

3.1.57

3 months ago

3.1.56

3 months ago

3.1.55

4 months ago

3.1.54

4 months ago

3.1.52

4 months ago

3.1.51

4 months ago

3.1.53

4 months ago

3.1.50

5 months ago

3.1.49

5 months ago

3.1.48

5 months ago

3.1.34

7 months ago

3.1.33

7 months ago

3.1.36

6 months ago

3.1.35

7 months ago

3.1.38

6 months ago

3.1.37

6 months ago

3.1.39

6 months ago

3.1.30

8 months ago

3.1.32

7 months ago

3.1.31

8 months ago

3.1.45

6 months ago

3.1.44

6 months ago

3.1.47

5 months ago

3.1.46

5 months ago

3.1.41

6 months ago

3.1.40

6 months ago

3.1.42

6 months ago

3.1.18

10 months ago

3.1.23

9 months ago

3.1.22

9 months ago

3.1.25

8 months ago

3.1.24

9 months ago

3.1.27

8 months ago

3.1.26

8 months ago

3.1.29

8 months ago

3.1.28

8 months ago

3.1.21

9 months ago

3.1.19

9 months ago

3.1.16

11 months ago

3.1.17

11 months ago

3.1.15

12 months ago

3.1.14

1 year ago

3.1.13

1 year ago

3.1.7

1 year ago

3.1.6

1 year ago

3.1.12

1 year ago

3.1.11

1 year ago

3.1.10

1 year ago

3.1.9

1 year ago

3.1.8

1 year ago

3.1.5

1 year ago

3.1.4

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

3.1.0

1 year ago

2.1.38

1 year ago

2.1.39

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.1.28

2 years ago

2.1.29

2 years ago

2.1.36

1 year ago

2.1.37

1 year ago

2.1.34

1 year ago

2.1.35

1 year ago

2.1.32

1 year ago

2.1.33

1 year ago

2.1.30

2 years ago

2.1.31

2 years ago

2.1.27

2 years ago

2.1.26

2 years ago

2.1.25

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.15

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.20

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.8

2 years ago

2.0.9

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.7

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.53

3 years ago

1.1.52

3 years ago

1.1.51

3 years ago

1.1.50

3 years ago

1.1.49

3 years ago

1.1.48

3 years ago

1.1.47

3 years ago

1.1.46

3 years ago

1.1.45

3 years ago

1.1.44

3 years ago

1.1.43

3 years ago

1.1.42

3 years ago

1.1.41

3 years ago

1.1.40

3 years ago

1.1.39

3 years ago

1.1.38

3 years ago

1.1.37

3 years ago

1.1.36

3 years ago

1.1.35

3 years ago

1.1.34

3 years ago

1.1.33

3 years ago

1.1.29

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.25

3 years ago

1.1.24

3 years ago

1.1.23

3 years ago

1.1.22

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.19

3 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.25

5 years ago

0.2.24

5 years ago

0.2.23

5 years ago

0.2.22

5 years ago

0.2.21

5 years ago

0.2.20

5 years ago

0.2.19

5 years ago

0.2.18

5 years ago

0.2.17

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago