Documentation
¶
Overview ¶
Example (AreaCircleFilter) ¶
package main
import (
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate poisson disk samplings
points := poisson.NewPoissonDisk(
NUM_POINTS,
poisson.WithAreaFilter(poisson.NewCircleFilter(0.5, 0.5, 0.25)),
)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ := os.OpenFile("./example-screenshots/output-area-circle-filter.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Example (AreaRectangleFilter) ¶
package main
import (
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate poisson disk samplings
points := poisson.NewPoissonDisk(
NUM_POINTS,
poisson.WithAreaFilter(poisson.NewRectangleFilter(0.2, 0.8)),
)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ := os.OpenFile("./example-screenshots/output-area-rectangle-filter.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Example (Basic) ¶
package main
import (
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate poisson disk samplings
points := poisson.NewPoissonDisk(NUM_POINTS)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ := os.OpenFile("./example-screenshots/output-basic.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Example (FullFeatured) ¶
package main
import (
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate poisson disk samplings
points := poisson.NewPoissonDisk(
NUM_POINTS,
poisson.WithTries(10000),
poisson.WithMinDistance(0.01),
poisson.WithGenerator(poisson.NewBasicGenerator(100)),
poisson.WithAreaFilter(poisson.NewCircleFilter(0.5, 0.5, 0.25)),
poisson.WithPostFilter(poisson.NewSimplexNoiseFilter(WIDTH/16, 0)),
poisson.WithStartPoint(0.5, 0.5),
poisson.WithPoints([]*poisson.Point{
{X: 0.45, Y: 0.5},
{X: 0.55, Y: 0.5},
{X: 0.5, Y: 0.45},
{X: 0.5, Y: 0.55},
}),
)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ := os.OpenFile("./example-screenshots/output-full-featured.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Example (MinDistance) ¶
package main
import (
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate poisson disk samplings
points := poisson.NewPoissonDisk(
NUM_POINTS,
poisson.WithMinDistance(0.01),
)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ := os.OpenFile("./example-screenshots/output-min-distance.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Example (WithPngNoisePostFilter) ¶
package main
import (
"github.com/ojrac/opensimplex-go"
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate png file with simple noise
noise := opensimplex.NewWithSeed(0)
noisePng := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
featureSize := WIDTH / 4
for y := 0; y < HEIGHT; y++ {
for x := 0; x < WIDTH; x++ {
value := noise.Eval2(float64(x)/float64(featureSize), float64(y)/float64(featureSize))
c := 0x01 * uint8((value+1)*127)
noisePng.Set(x, y, color.RGBA{c, c, c, 255})
}
}
f, _ := os.OpenFile("./example-screenshots/noise.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, noisePng)
//generate poisson disk samplings
points := poisson.NewPoissonDisk(
NUM_POINTS*2,
poisson.WithPostFilter(poisson.NewGrayScalePngFilter("./example-screenshots/noise.png")),
)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ = os.OpenFile("./example-screenshots/output-png-post-filter.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Example (WithSimplexNoisePostFilter) ¶
package main
import (
"github.com/plandem/poisson-disk-sampling"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const WIDTH = 1024
const HEIGHT = 1024
const NUM_POINTS = 50000
func main() {
//generate poisson disk samplings
points := poisson.NewPoissonDisk(
NUM_POINTS,
poisson.WithPostFilter(poisson.NewSimplexNoiseFilter(WIDTH/32, 0)),
)
//draw result
img := image.NewRGBA(image.Rect(0, 0, WIDTH, HEIGHT))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
for _, point := range points {
x := int(point.X * float64(WIDTH))
y := int(point.Y * float64(HEIGHT))
img.Set(x, y, color.White)
}
//save result png
f, _ := os.OpenFile("./example-screenshots/output-simplex-post-filter.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}
Output:
Index ¶
- type Grid
- type Option
- func WithAreaFilter(filter PointFilter) Option
- func WithGenerator(generator RandomGenerator) Option
- func WithMinDistance(distance float64) Option
- func WithPoints(points []*Point) Option
- func WithPostFilter(filter PointFilter) Option
- func WithStartPoint(x, y float64) Option
- func WithTries(tries int) Option
- type Point
- type PointFilter
- type RandomGenerator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Grid ¶
type Grid struct {
// contains filtered or unexported fields
}
Grid is a structure that holds information about points inside of grid
func (*Grid) IsNeighbourhood ¶
IsNeighbourhood checks if point has any neighbourhood point with distance less than minDistance
func (*Grid) PointToCell ¶
PointToCell returns col and row of point
type Option ¶
type Option func(*options)
Option is function to update required setting
func WithAreaFilter ¶
func WithAreaFilter(filter PointFilter) Option
WithAreaFilter set an area filter for candidate points
func WithGenerator ¶
func WithGenerator(generator RandomGenerator) Option
WithGenerator set a random number genetator
func WithMinDistance ¶
WithMinDistance set a minimum distance between any two points
func WithPoints ¶
WithPoints set points to will the grid before processing. N.B.: only one point per cell - only last one will be stored at grid
func WithPostFilter ¶
func WithPostFilter(filter PointFilter) Option
WithPostFilter set a post filter to drop points that do not meet required condition
func WithStartPoint ¶
WithStartPoint set a start point at x,y to start from
type Point ¶
Point holds coordinate x,y
func NewPoissonDisk ¶
NewPoissonDisk returns sampling points with provided options
func NewRandomPoint ¶
func NewRandomPoint(generator RandomGenerator) *Point
NewRandomPoint generates random point using generator to get random position
func (*Point) RandomPointAround ¶
func (point *Point) RandomPointAround(minDist float64, generator RandomGenerator) *Point
RandomPointAround generates random point in annulus [minDist, 2 * minDist] using generator to get random radius and angle
type PointFilter ¶
PointFilter interface is for filters to return valid points. In general, any point must be inside of [0.0, 1.0] box
func NewCircleFilter ¶
func NewCircleFilter(x, y, r float64) PointFilter
NewCircleFilter returns a PointFilter that returns only points inside of circle
func NewGrayScalePngFilter ¶
func NewGrayScalePngFilter(pngFileName string) PointFilter
NewGrayScalePngFilter returns a PointFilter that uses a gray scale image for filtering
func NewRectangleFilter ¶
func NewRectangleFilter(min, max float64) PointFilter
NewRectangleFilter returns a PointFilter that returns only points inside of rectangle
func NewSimplexNoiseFilter ¶
func NewSimplexNoiseFilter(featureSize int, noiseSeed int64) PointFilter
NewSimplexNoiseFilter returns a PointFilter that uses the simplex noise algorithm
type RandomGenerator ¶
RandomGenerator is interface for any random generator that can be used with poisson disk sampling
func NewBasicGenerator ¶
func NewBasicGenerator(seed int) RandomGenerator
NewBasicGenerator returns a basic random generator