poisson

package module
v0.0.0-...-2315038 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2018 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Poisson Disk Sampling

Build Status Go Report Card GoDoc License Donate

Based on article: http://devmag.org.za/2009/05/03/poisson-disk-sampling/

package main

import (
	"os"
	"image"
	"image/draw"
	"image/color"
	"image/png"
	"github.com/plandem/poisson-disk-sampling"
)

func main() {
	width, height, numPoints := 1024, 1024, 1000
	
	//generate poisson disk samplings
	points := poisson.NewPoissonDisk(numPoints)

	//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("output.png", os.O_WRONLY|os.O_CREATE, 0600)
	defer f.Close()
	png.Encode(f, img)
}

Get more examples at example_test.go

basic example
Basic Example
with minimum distance
Minimum Distance Example
with circle area filter
Circle Area Filter Example
with rectangle area filter
Rectangle Area Filter Example
with simplex noise post filter
Simplex Noise Post Filter Example
with PNG map post filter
PNG Map PNG Post Filter Example Full Featured Example

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)

}
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)

}
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)

}
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)

}
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)

}
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)

}
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)

}

Index

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 NewGrid

func NewGrid(cols, rows int, cellSize float64) *Grid

NewGrid returns Grid with cols x rows dimension and size of cell equals to cellSize

func (*Grid) Cols

func (grid *Grid) Cols() int

Cols returns total number of cols in the grid

func (*Grid) GetPoint

func (grid *Grid) GetPoint(col, row int) *Point

GetPoint returns point information from cell with col and row in the grid

func (*Grid) IsNeighbourhood

func (grid *Grid) IsNeighbourhood(point *Point, minDistance float64) bool

IsNeighbourhood checks if point has any neighbourhood point with distance less than minDistance

func (*Grid) PointToCell

func (grid *Grid) PointToCell(point *Point) (int, int)

PointToCell returns col and row of point

func (*Grid) Rows

func (grid *Grid) Rows() int

Rows returns total number of rows in the grid

func (*Grid) SetPoint

func (grid *Grid) SetPoint(point *Point)

SetPoint stores point information in the grid. Point must fit to dimension of grid

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

func WithMinDistance(distance float64) Option

WithMinDistance set a minimum distance between any two points

func WithPoints

func WithPoints(points []*Point) Option

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

func WithStartPoint(x, y float64) Option

WithStartPoint set a start point at x,y to start from

func WithTries

func WithTries(tries int) Option

WithTries set number of tries to generate a new point

type Point

type Point struct {
	X float64
	Y float64
}

Point holds coordinate x,y

func NewPoissonDisk

func NewPoissonDisk(numPoints int, opts ...Option) []*Point

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) Distance

func (point *Point) Distance(other *Point) float64

Distance returns a distance between point and other points

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

type PointFilter interface {
	Filter(point *Point, settings *options) bool
}

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

type RandomGenerator interface {
	Float() float64
	Integer(max int) int
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址