forked from fogleman/primitive
/
model.go
174 lines (157 loc) · 4.56 KB
/
model.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package primitive
import (
"fmt"
"image"
"strings"
"github.com/fogleman/gg"
)
type Model struct {
Sw, Sh int
Scale float64
Background Color
Target *image.RGBA
Current *image.RGBA
Context *gg.Context
Score float64
Shapes []Shape
Colors []Color
Scores []float64
Workers []*Worker
}
func NewModel(target image.Image, background Color, size, numWorkers int) *Model {
w := target.Bounds().Size().X
h := target.Bounds().Size().Y
aspect := float64(w) / float64(h)
var sw, sh int
var scale float64
if aspect >= 1 {
sw = size
sh = int(float64(size) / aspect)
scale = float64(size) / float64(w)
} else {
sw = int(float64(size) * aspect)
sh = size
scale = float64(size) / float64(h)
}
model := &Model{}
model.Sw = sw
model.Sh = sh
model.Scale = scale
model.Background = background
model.Target = imageToRGBA(target)
model.Current = uniformRGBA(target.Bounds(), background.NRGBA())
model.Score = differenceFull(model.Target, model.Current)
model.Context = model.newContext()
for i := 0; i < numWorkers; i++ {
worker := NewWorker(model.Target)
model.Workers = append(model.Workers, worker)
}
return model
}
func (model *Model) newContext() *gg.Context {
dc := gg.NewContext(model.Sw, model.Sh)
dc.Scale(model.Scale, model.Scale)
dc.Translate(0.5, 0.5)
dc.SetColor(model.Background.NRGBA())
dc.Clear()
return dc
}
func (model *Model) Frames(scoreDelta float64) []image.Image {
var result []image.Image
dc := model.newContext()
result = append(result, imageToRGBA(dc.Image()))
previous := 10.0
for i, shape := range model.Shapes {
c := model.Colors[i]
dc.SetRGBA255(c.R, c.G, c.B, c.A)
shape.Draw(dc, model.Scale)
dc.Fill()
score := model.Scores[i]
delta := previous - score
if delta >= scoreDelta {
previous = score
result = append(result, imageToRGBA(dc.Image()))
}
}
return result
}
func (model *Model) SVG() string {
bg := model.Background
var lines []string
lines = append(lines, fmt.Sprintf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"%d\" height=\"%d\">", model.Sw, model.Sh))
lines = append(lines, fmt.Sprintf("<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%02x%02x%02x\" />", model.Sw, model.Sh, bg.R, bg.G, bg.B))
lines = append(lines, fmt.Sprintf("<g transform=\"scale(%f) translate(0.5 0.5)\">", model.Scale))
for i, shape := range model.Shapes {
c := model.Colors[i]
attrs := "fill=\"#%02x%02x%02x\" fill-opacity=\"%f\""
attrs = fmt.Sprintf(attrs, c.R, c.G, c.B, float64(c.A)/255)
lines = append(lines, shape.SVG(attrs))
}
lines = append(lines, "</g>")
lines = append(lines, "</svg>")
return strings.Join(lines, "\n")
}
func (model *Model) Add(shape Shape, alpha int) {
before := copyRGBA(model.Current)
lines := shape.Rasterize()
color := computeColor(model.Target, model.Current, lines, alpha)
drawLines(model.Current, color, lines)
score := differencePartial(model.Target, before, model.Current, model.Score, lines)
model.Score = score
model.Shapes = append(model.Shapes, shape)
model.Colors = append(model.Colors, color)
model.Scores = append(model.Scores, score)
model.Context.SetRGBA255(color.R, color.G, color.B, color.A)
shape.Draw(model.Context, model.Scale)
}
func (model *Model) Step(shapeType ShapeType, alpha, repeat int) int {
state := model.runWorkers(shapeType, alpha, 1000, 100, 16)
// state = HillClimb(state, 1000).(*State)
model.Add(state.Shape, state.Alpha)
for i := 0; i < repeat; i++ {
state.Worker.Init(model.Current, model.Score)
a := state.Energy()
state = HillClimb(state, 100).(*State)
b := state.Energy()
if a == b {
break
}
model.Add(state.Shape, state.Alpha)
}
// for _, w := range model.Workers[1:] {
// model.Workers[0].Heatmap.AddHeatmap(w.Heatmap)
// }
// SavePNG("heatmap.png", model.Workers[0].Heatmap.Image(0.5))
counter := 0
for _, worker := range model.Workers {
counter += worker.Counter
}
return counter
}
func (model *Model) runWorkers(t ShapeType, a, n, age, m int) *State {
wn := len(model.Workers)
ch := make(chan *State, wn)
wm := m / wn
if m%wn != 0 {
wm++
}
for i := 0; i < wn; i++ {
worker := model.Workers[i]
worker.Init(model.Current, model.Score)
go model.runWorker(worker, t, a, n, age, wm, ch)
}
var bestEnergy float64
var bestState *State
for i := 0; i < wn; i++ {
state := <-ch
energy := state.Energy()
if i == 0 || energy < bestEnergy {
bestEnergy = energy
bestState = state
}
}
return bestState
}
func (model *Model) runWorker(worker *Worker, t ShapeType, a, n, age, m int, ch chan *State) {
ch <- worker.BestHillClimbState(t, a, n, age, m)
}