about summary refs log tree commit diff
path: root/main.go
blob: 7ead56c8cf75e2fd7297f6e9c20d5363277e7260 (plain)
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
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strconv"

	"github.com/fogleman/gg"

	"git.darknebu.la/GalaxySimulator/structs"
)

var (
	treeArray []*structs.Node
)

func readfile(filename string) {
	// read the json
	file, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}

	// initialize the rootnode
	var rootnode structs.Node

	// unmarshal the json into the rootnode
	err = json.Unmarshal(file, &rootnode)
	if err != nil {
		panic(err)
	}

	treeArray = append(treeArray, &rootnode)
}

// draw the requested tree
func drawtree(treeindex int64, savepath string) {

	// generate a list of all stars
	var starlist []structs.Star2D
	starlist = treeArray[treeindex].GetAllStars()

	log.Println("[   ] Initializing the Plot")
	dc := initializePlot()
	log.Println("[   ] Done Initializing the Plot")

	log.Println("[   ] Drawing the Starlist")
	drawStarlist(dc, starlist)
	log.Println("[   ] Done Drawing the Starlist")

	log.Println("[   ] Drawing the Boxes")
	drawBoxes(dc, treeindex)
	log.Println("[   ] Done Drawing the Boxes")

	log.Println("[   ] Saving the image")
	saveImage(dc, savepath)
	log.Println("[   ] Done Saving the image")
}

func saveImage(dc *gg.Context, path string) {
	err := dc.SavePNG(path)
	if err != nil {
		panic(err)
	}
}

func drawBox(dc *gg.Context, box structs.BoundingBox) {
	x := (box.Center.X / 5e3 * 2.5) - ((box.Width / 5e3 * 2.5) / 4)
	y := (box.Center.Y / 5e3 * 2.5) - ((box.Width / 5e3 * 2.5) / 4)
	w := box.Width / 5e3

	log.Println("[   ] Drawing the Box")
	dc.DrawRectangle(x, y, w, w)
	log.Println("[   ] 0")
	dc.Stroke()
	log.Println("[   ] Done Drawing the Box")
}

func genBoxes(dc *gg.Context, node structs.Node) {

	// if the BoundingBox is not empty, draw it
	if node.Boundry != (structs.BoundingBox{}) {
		drawBox(dc, node.Boundry)
	}

	for i := 0; i < len(node.Subtrees); i++ {
		if node.Subtrees[i] != nil {
			genBoxes(dc, *node.Subtrees[i])
		}
	}
}

func drawBoxes(dc *gg.Context, treeindex int64) {
	log.Println("[   ] before genBoxes")
	root := treeArray[treeindex]
	genBoxes(dc, *root)
	log.Println("[   ] after genBoxes")
}

func drawStar(dc *gg.Context, star structs.Star2D) {
	// scalingFactor := 50
	defaultStarSize := 2.0

	x := star.C.X / 5e3 * 2.5
	y := star.C.Y / 5e3 * 2.5

	fmt.Printf("(%20.3f, %20.3f)\n", x, y)

	dc.SetRGB(1, 1, 1)
	dc.DrawCircle(x, y, defaultStarSize)
	dc.Fill()
	dc.Stroke()
}

func drawStarlist(dc *gg.Context, starlist []structs.Star2D) {
	for _, star := range starlist {
		drawStar(dc, star)
	}
}

// initializePlot generates a new plot and returns the plot context
func initializePlot() *gg.Context {
	// Define the image size
	const imageWidth = 8192 * 2
	const imageHeight = 8192 * 2

	// Initialize the new context
	dc := gg.NewContext(imageWidth, imageHeight)

	// Set the background black
	dc.SetRGB(0, 0, 0)
	dc.Clear()

	// Invert the Y axis (positive values are on the top and right)
	dc.InvertY()

	// Set the coordinate midpoint to the middle of the image
	dc.Translate(imageWidth/2, imageHeight/2)

	return dc
}

func drawallboxes(amount int64) {
	for i := 0; i < int(amount); i++ {
		index := fmt.Sprintf("%d", i)
		readfile(fmt.Sprintf("%s.json", index))
		drawtree(0, fmt.Sprintf("%s.png", index))
	}
}

func main() {
	amount, parseErr := strconv.ParseInt(os.Args[1], 10, 64)
	if parseErr != nil {
		panic(amount)
	}
	drawallboxes(amount)
}