-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (68 loc) · 1.92 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/RH12503/Triangula/algorithm"
"github.com/RH12503/Triangula/algorithm/evaluator"
"github.com/RH12503/Triangula/generator"
image2 "github.com/RH12503/Triangula/image"
"github.com/RH12503/Triangula/mutation"
"github.com/RH12503/Triangula/normgeom"
"github.com/RH12503/Triangula/render"
"github.com/RH12503/Triangula/triangulation"
"image"
_ "image/jpeg"
_ "image/png"
"strings"
"syscall/js"
)
var algo algorithm.Algorithm
var img image2.Data
func initAlgorithm(this js.Value, i []js.Value) interface{} {
base64Input := i[0].String()
points := i[1].Int()
base64Data := base64Input[strings.IndexByte(base64Input, ',')+1:]
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64Data))
imageFile, _, err := image.Decode(reader)
if err != nil {
return nil
}
img = image2.ToData(imageFile)
evaluatorFactory := func(n int) evaluator.Evaluator {
return evaluator.NewParallel(img, 20, 5, n)
}
var mutator mutation.Method
mutator = mutation.NewGaussianMethod(2/float64(points), 0.3)
pointFactory := func() normgeom.NormPointGroup {
return generator.RandomGenerator{}.Generate(points)
}
algo = algorithm.NewModifiedGenetic(pointFactory, 400, 5, evaluatorFactory, mutator)
return nil
}
func stepAlgorithm(this js.Value, i []js.Value) interface{} {
algo.Step()
w, h := img.Size()
triangles := triangulation.Triangulate(algo.Best(), w, h)
triangleData := render.TrianglesOnImage(triangles, img)
data := struct {
Data []render.TriangleData
Width, Height int
}{
Data: triangleData,
Width: w,
Height: h,
}
jsonData, _ := json.Marshal(data)
return string(jsonData)
}
func registerCallbacks() {
js.Global().Set("initAlgorithm", js.FuncOf(initAlgorithm))
js.Global().Set("stepAlgorithm", js.FuncOf(stepAlgorithm))
}
func main() {
c := make(chan struct{}, 0)
registerCallbacks()
fmt.Println("WASM Go Initialized")
<-c
}