-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (183 loc) · 4.9 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"image"
_ "image/jpeg"
"os"
"path/filepath"
"strings"
"time"
ebiten "github.com/hajimehoshi/ebiten/v2"
"github.com/urfave/cli/v2"
)
var (
// defaults argument values for the `run` command
defaultNumSeeds = 50
defaultSimulationDuration = 3 * time.Hour
defaultSnapshotsInterval = 1 * time.Minute
defaultImageName = "homer"
)
func main() {
//
// CLI initialization
//
var numSeeds int
var inputImageFilePath string
var simulationDuration time.Duration
var snapshotsInterval time.Duration
app := &cli.App{
Name: "Voronoi Simulated Annealing",
Usage: "Simulated Annealing approximation using Voronoi diagrams",
Description: "This simulation takes an image in input and tries to approximate it with a voronoi diagram, by using the simulate annealing approach",
UsageText: "voronoi-simulated-annealing [command] [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "targetImage",
Aliases: []string{"i"},
Usage: "Path to the input image `FILE` to be used as target image for the annealing. Only JPG images are supported",
Value: "./res/" + defaultImageName + ".jpg",
Destination: &inputImageFilePath,
},
&cli.IntFlag{
Name: "seedsNumber",
Aliases: []string{"n"},
Usage: "Number of seeds (cells) used in the voronoi diagram",
Value: defaultNumSeeds,
Destination: &numSeeds,
},
&cli.DurationFlag{
Name: "simulationDuration",
Aliases: []string{"d"},
Usage: "Duration of the simulation",
Value: defaultSimulationDuration,
Destination: &simulationDuration,
},
&cli.DurationFlag{
Name: "snapshotsInterval",
Aliases: []string{"s"},
Usage: "Time interval between the snapshots taken during the simulation (to track the progresses)",
Value: defaultSnapshotsInterval,
Destination: &snapshotsInterval,
},
},
Commands: []*cli.Command{
{
Name: "run",
Aliases: []string{"r"},
Usage: "Runs the simulated annealing",
Action: func(cCtx *cli.Context) error {
targetImage := getTargetImage(inputImageFilePath)
runSimulatedAnnealing(
targetImage,
numSeeds,
simulationDuration,
snapshotsInterval,
)
return nil
},
},
},
}
// run the cli
if err := app.Run(os.Args); err != nil {
panic(err)
}
}
// getTargetImage reads the target image at the specified path, and extracts the RGB values of each pixel
func getTargetImage(inputImageFilePath string) TargetImage {
// get file name stripped from path and extension
fileNameWithExt := filepath.Base(inputImageFilePath)
fileExtension := filepath.Ext(inputImageFilePath)
fileName := strings.Replace(fileNameWithExt, fileExtension, "", 1)
// open the image file
reader, openErr := os.Open(inputImageFilePath)
if openErr != nil {
panic(openErr)
}
defer reader.Close()
// decode the image using the JPG decoder
image, _, decodeErr := image.Decode(reader)
if decodeErr != nil {
panic(decodeErr)
}
bounds := image.Bounds()
// extract the 8-bit RGBA values of the pixels
imageBytes := []byte{}
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := image.At(x, y).RGBA()
imageBytes = append(
imageBytes,
byte(r/256),
byte(g/256),
byte(b/256),
byte(a/256),
)
}
}
return TargetImage{
Name: fileName,
Bytes: imageBytes,
Width: bounds.Max.X - bounds.Min.X,
Height: bounds.Max.Y - bounds.Min.Y,
}
}
// runSimulatedAnnealing initializes the structs needed to run the simulation, and starts it
func runSimulatedAnnealing(
targetImage TargetImage,
numSeeds int,
simulationDuration time.Duration,
snapshotsInterval time.Duration,
) {
// create a file that logs the temperature progresses in function of time since the start of the simulation, for further analysis
statFile, err := os.Create(
fmt.Sprintf("./res/%s_%d-seeds.csv",
targetImage.Name,
numSeeds,
))
if err != nil {
panic(err)
}
// initialize the Voronoi diagram
voronoi, vErr := NewVoronoi(
targetImage.Width,
targetImage.Height,
numSeeds,
)
if vErr != nil {
panic(vErr)
}
// initialize the simulated annealing
simulatedAnnealing, saErr := NewSimulatedAnnealing(
voronoi,
targetImage,
statFile,
)
if saErr != nil {
panic(saErr)
}
// initialize the canvas for the GUI
c, cErr := NewCanvas(
targetImage.Name,
numSeeds,
targetImage.Width,
targetImage.Height,
simulatedAnnealing,
simulationDuration,
snapshotsInterval,
)
if cErr != nil {
panic(cErr)
}
// initialize the system window size and title
ebiten.SetWindowTitle(
fmt.Sprintf("Voronoi Simulated Annealing (%d seeds)", numSeeds))
ebiten.SetWindowSize(targetImage.Width, targetImage.Height)
// run the simulation
if err := ebiten.RunGame(c); err != nil {
if err == SimulationCompleted {
return
}
panic(err)
}
}