-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogic.go
163 lines (132 loc) · 5.01 KB
/
logic.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
package main
import (
"errors"
"github.com/RH12503/Triangula-GUI/polygons"
"github.com/RH12503/Triangula-GUI/triangles"
"github.com/RH12503/Triangula/algorithm"
"github.com/RH12503/Triangula/algorithm/evaluator"
"github.com/RH12503/Triangula/color"
"github.com/RH12503/Triangula/fitness"
"github.com/RH12503/Triangula/generator"
"github.com/RH12503/Triangula/image"
"github.com/RH12503/Triangula/mutation"
"github.com/RH12503/Triangula/normgeom"
"github.com/RH12503/Triangula/polygonation"
"github.com/RH12503/Triangula/render"
"github.com/RH12503/Triangula/triangulation"
"strings"
)
const (
none int = iota
gradient int = iota
split int = iota
)
type Logic interface {
NewAlgorithm(img image.Data, mutations int, mutationAmount float64, numPoints, population, cutoff, blockSize, cacheSize int) algorithm.Algorithm
RenderData(normgeom.NormPointGroup, image.Data) RenderData
SaveSVG(file string, points normgeom.NormPointGroup, img image.Data) error
SavePNG(file string, points normgeom.NormPointGroup, img image.Data, scale float64, effect int) error
}
type TriangleLogic struct {
}
func (t TriangleLogic) SaveSVG(file string, points normgeom.NormPointGroup, img image.Data) error {
filename := file
if !strings.HasSuffix(filename, ".svg") {
filename += ".svg"
}
return triangles.WriteSVG(filename, points, img)
}
func (t TriangleLogic) SavePNG(file string, points normgeom.NormPointGroup, img image.Data, scale float64, effect int) error {
filename := file
if !strings.HasSuffix(filename, ".png") {
filename += ".png"
}
if effect == none {
return triangles.WritePNG(filename, points, img, scale)
} else if effect == gradient {
return triangles.WriteEffectPNG(filename, points, img, scale, true)
} else if effect == split {
return triangles.WriteEffectPNG(filename, points, img, scale, false)
}
return errors.New("invalid effect")
}
func (t TriangleLogic) NewAlgorithm(img image.Data, mutations int, mutationAmount float64, numPoints, population, cutoff, blockSize, cacheSize int) algorithm.Algorithm {
evaluatorFactory := func(n int) evaluator.Evaluator {
return evaluator.NewParallel(fitness.TrianglesImageFunctions(img, blockSize, n), cacheSize)
}
var mutator mutation.Method
mutator = mutation.NewGaussianMethod(float64(mutations)/float64(numPoints), mutationAmount)
pointFactory := func() normgeom.NormPointGroup {
return generator.RandomGenerator{}.Generate(numPoints)
}
algo := algorithm.NewModifiedGenetic(pointFactory, population, cutoff, evaluatorFactory, mutator)
return algo
}
func (t TriangleLogic) RenderData(points normgeom.NormPointGroup, img image.Data) RenderData {
w, h := img.Size()
triangles := triangulation.Triangulate(points, w, h)
triangleData := render.TrianglesOnImage(triangles, img)
data := RenderData{
Width: w,
Height: h,
Polygons: make([]normgeom.NormPolygon, len(triangleData)),
Colors: make([]color.RGB, len(triangleData)),
}
for i, d := range triangleData {
data.Colors[i] = d.Color
tri := d.Triangle.Points
data.Polygons[i] = normgeom.NormPolygon{Points: []normgeom.NormPoint{tri[0], tri[1], tri[2]}}
}
return data
}
type PolygonLogic struct {
}
func (p PolygonLogic) SaveSVG(file string, points normgeom.NormPointGroup, img image.Data) error {
filename := file
if !strings.HasSuffix(filename, ".svg") {
filename += ".svg"
}
return polygons.WriteSVG(filename, points, img)
}
func (p PolygonLogic) SavePNG(file string, points normgeom.NormPointGroup, img image.Data, scale float64, effect int) error {
filename := file
if !strings.HasSuffix(filename, ".png") {
filename += ".png"
}
if effect == none {
return polygons.WritePNG(filename, points, img, scale)
} else if effect == gradient {
return polygons.WriteEffectPNG(filename, points, img, scale, true)
} else if effect == split {
return polygons.WriteEffectPNG(filename, points, img, scale, false)
}
return errors.New("invalid effect")
}
func (p PolygonLogic) NewAlgorithm(img image.Data, mutations int, mutationAmount float64, numPoints, population, cutoff, blockSize, cacheSize int) algorithm.Algorithm {
evaluatorFactory := func(n int) evaluator.Evaluator {
return evaluator.NewParallel(fitness.PolygonsImageFunctions(img, blockSize, n), cacheSize)
}
var mutator mutation.Method
mutator = mutation.NewGaussianMethod(float64(mutations)/float64(numPoints), mutationAmount)
pointFactory := func() normgeom.NormPointGroup {
return generator.RandomGenerator{}.Generate(numPoints)
}
algo := algorithm.NewModifiedGenetic(pointFactory, population, cutoff, evaluatorFactory, mutator)
return algo
}
func (p PolygonLogic) RenderData(points normgeom.NormPointGroup, img image.Data) RenderData {
w, h := img.Size()
polygons := polygonation.Polygonate(points, w, h)
triangleData := render.PolygonsOnImage(polygons, img)
data := RenderData{
Width: w,
Height: h,
Polygons: make([]normgeom.NormPolygon, len(triangleData)),
Colors: make([]color.RGB, len(triangleData)),
}
for i, d := range triangleData {
data.Colors[i] = d.Color
data.Polygons[i] = d.Polygon
}
return data
}