/
factory.go
54 lines (46 loc) · 1.27 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"math/rand"
"github.com/aurelien-rainone/evolve/factory"
"github.com/aurelien-rainone/evolve/framework"
)
type imageDNAfactory struct {
factory.AbstractCandidateFactory
}
func newImageDNAfactory(imgW, imgH int) (*imageDNAfactory, error) {
if imgW == 0 || imgH == 0 {
return nil, fmt.Errorf("invalid dimensions %v x %v", imgW, imgH)
}
sf := &imageDNAfactory{
factory.AbstractCandidateFactory{
RandomCandidateGenerator: &imageDNAGenerator{
imgW: imgW,
imgH: imgH,
},
},
}
return sf, nil
}
type imageDNAGenerator struct {
imgW, imgH int // width/height of the reference image
}
func (g *imageDNAGenerator) GenerateRandomCandidate(rng *rand.Rand) framework.Candidate {
var numPolys int
if appConfig.Image.MinPolys == appConfig.Image.MaxPolys {
numPolys = appConfig.Image.MaxPolys
} else {
numPolys = appConfig.Image.MinPolys + rng.Intn(appConfig.Image.MaxPolys-appConfig.Image.MinPolys)
}
// create image dna with same dimensions than reference image
var img = &imageDNA{
w: g.imgW,
h: g.imgH,
polys: make([]poly, numPolys),
}
// add N `numPolys` random polygons
for i := 0; i < numPolys; i++ {
img.polys[i] = randomPoly(img, appConfig.Polygon.MinPoints, appConfig.Polygon.MaxPoints, rng)
}
return img
}