polygen: github.com/armhold/polygen Index | Files

package polygen

import "github.com/armhold/polygen"


Package Files

bindata.go candidate.go evolver.go image.go safe_image.go server.go stats.go util.go


const (
    MutationAlpha            = iota
    MutationColor            = iota
    MutationPoint            = iota
    MutationZOrder           = iota
    MutationAddOrDeletePoint = iota
const (
    PopulationCount          = 10
    MaxPolygonPoints         = 6
    MinPolygonPoints         = 3
    PointMutationMaxDistance = 5
    MutationsPerIteration    = 1 // originally had 3, but 1 seems to work best here


var (
    Mutations = []int{MutationColor, MutationPoint, MutationAlpha, MutationZOrder, MutationAddOrDeletePoint}

func Asset Uses

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir Uses

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:


then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo Uses

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames Uses

func AssetNames() []string

AssetNames returns the names of the assets.

func Compare Uses

func Compare(img1, img2 image.Image) (int64, error)

Compare compares images by computing the square root of the total sum of individual squared pixel differences.

func ConvertToRGBA Uses

func ConvertToRGBA(img image.Image) (result *image.RGBA)

from http://blog.golang.org/go-imagedraw-package ("Converting an Image to RGBA"), modified slightly to be a no-op if the src image is already RGBA

func DeriveCheckpointFile Uses

func DeriveCheckpointFile(sourceFile, cpArg string, polyCount int) string

try to make it painless for users to automatically get a checkpoint file. We want the file to be tied both the source file name and the polygon count. If an explicit cpArg is given, we just use that.

func FastCompare Uses

func FastCompare(img1, img2 *image.RGBA) (uint64, error)

FastCompare compares images by diffing the underlying byte arrays directly. This is more than 10x faster than Compare(), but requires a concrete instance of image.RGBA.

func MustAsset Uses

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func MustReadImage Uses

func MustReadImage(file string) image.Image

func RandomBool Uses

func RandomBool() bool

RandomBool uses the default random Source to return either true or false.

func RandomInt Uses

func RandomInt(min, max int) int

RandomInt uses the default random Source to return a random integer that is >= min, but < max

func RestoreAsset Uses

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets Uses

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func Serve Uses

func Serve(hostPort string, refImg image.Image, previews []*SafeImage)

func SplitPath Uses

func SplitPath(path string) []string

type ByFitness Uses

type ByFitness []*Candidate


func (ByFitness) Len Uses

func (cds ByFitness) Len() int

func (ByFitness) Less Uses

func (cds ByFitness) Less(i, j int) bool

func (ByFitness) Swap Uses

func (cds ByFitness) Swap(i, j int)

type Candidate Uses

type Candidate struct {
    W, H     int
    Polygons []*Polygon

    Fitness uint64
    // contains filtered or unexported fields

Candidate is a potential solution (set of polygons) to the problem of how to best represent the reference image.

type Checkpoint Uses

type Checkpoint struct {
    Generation             int
    GenerationsSinceChange int
    MostFit                *Candidate

Checkpoint is used for serializing the current best candidate and corresponding generation count to a checkpoint file.

type Evolver Uses

type Evolver struct {
    // contains filtered or unexported fields

Evolver uses a genetic algorithm to evolve a set of polygons to approximate an image.

func NewEvolver Uses

func NewEvolver(refImg image.Image, dstImageFile string, checkPointFile string) (*Evolver, error)

func (*Evolver) Run Uses

func (e *Evolver) Run(maxGen, polyCount int, previews []*SafeImage)

Run runs the Evolver until maxGen generations have been evaluated. At each generation, the candidate images are rendered & evaluated, and the preview images are updated to reflect the current state.

type Page Uses

type Page struct {

type Point Uses

type Point struct {
    X, Y int

Point defines a vertex in a Polygon.

type Polygon Uses

type Polygon struct {
    Points []Point

Polygon is a set of points with a given fill color.

type SafeImage Uses

type SafeImage struct {
    // contains filtered or unexported fields

SafeImage is an image protected by a RWMutex. All access should be via the Update() and Value() methods. This allows us to update the candidate images from a goroutine in the Evolver, and display them in the Server.

func NewSafeImage Uses

func NewSafeImage(img image.Image) *SafeImage

func (*SafeImage) Update Uses

func (s *SafeImage) Update(img image.Image)

func (*SafeImage) Value Uses

func (s *SafeImage) Value() image.Image

type Stats Uses

type Stats struct {
    // contains filtered or unexported fields

Stats is used by the Evolver for printing runtime statistics.

func NewStats Uses

func NewStats() *Stats

func (*Stats) Increment Uses

func (s *Stats) Increment(count int)

Increments the number of candidates that have been evaluated since last call to Print().

func (*Stats) Print Uses

func (s *Stats) Print(best, worst *Candidate, generation, generationsSinceChange int)

Package polygen imports 26 packages (graph). Updated 2018-09-25. Refresh now. Tools for package owners.