about summary refs log tree commit diff
diff options
context:
space:
mode:
authoremile <hanemile@protonmail.com>2019-01-29 12:28:30 +0000
committerGogs <gogs@fake.local>2019-01-29 12:28:30 +0000
commit463c60ae427f01a0393ee4805bf46347356c6f51 (patch)
tree90612ecf2d696889aced5a19cc74abf59062df2d
parent484c6d61e5b7178f6cc1c59be0c938c800dc2422 (diff)
parent49058131c3ebc8e9c69b93665e6d1081376d0be0 (diff)
Merge branch 'svg' of GalaxySimulator/viewer into master
-rw-r--r--Dockerfile7
-rw-r--r--docker-compose.yml5
-rw-r--r--main.go156
-rw-r--r--src/main.go121
4 files changed, 133 insertions, 156 deletions
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..f1f6c8b
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,7 @@
+FROM golang:latest
+
+WORKDIR /home
+
+COPY /src /home/
+
+ENTRYPOINT ["go", "run", "."]
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..eef2290
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,5 @@
+version: '3'
+
+services:
+  viewer:
+    build: .
diff --git a/main.go b/main.go
index 7ead56c..da29a2c 100644
--- a/main.go
+++ b/main.go
@@ -1,160 +1,4 @@
 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)
 }
diff --git a/src/main.go b/src/main.go
new file mode 100644
index 0000000..dd00ff9
--- /dev/null
+++ b/src/main.go
@@ -0,0 +1,121 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"strconv"
+
+	"github.com/ajstarks/svgo"
+	"github.com/gorilla/mux"
+
+	"git.darknebu.la/GalaxySimulator/structs"
+)
+
+const (
+	width = 1920 * 8
+	height = 1920 * 8
+)
+
+var (
+	treeArray []*structs.Node
+)
+
+func drawTree(w http.ResponseWriter, r *http.Request) {
+	log.Println("The drawtree handler was accessed")
+	w.Header().Set("Content-Type", "image/svg+xml")
+
+	// get the tree index
+	vars := mux.Vars(r)
+	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)
+
+	// define the svg
+	s := svg.New(w)
+	s.Start(width, height)
+	s.Rect(0, 0, width, height, s.RGB(0, 0, 0))
+	s.Gtransform(fmt.Sprintf("translate(%d,%d)", width/2, height/2))
+
+	getGalaxy(treeindex)
+	listOfStars := treeArray[treeindex].GetAllStars()
+
+	// draw the galaxy
+	drawStars(s, listOfStars)
+	drawBoxes(s, treeindex)
+
+	s.Gend()
+	s.End()
+}
+
+func drawStars(s *svg.SVG, listOfStars []structs.Star2D) {
+	log.Println("[   ] Drawing the stars")
+	for _, star := range listOfStars {
+		x := int(star.C.X / 2000)
+		y := int(star.C.Y / 2000)
+		fmt.Printf("(%d, %d)\n", x, y)
+		s.Circle(x, y, 1, s.RGB(255, 255, 255))
+	}
+	log.Println("[   ] Done drawing the stars")
+}
+
+func drawBoxes(s *svg.SVG, treeindex int64) {
+	log.Println("[   ] Drawing the Boxes")
+	drawBox(s, treeArray[treeindex])
+	log.Println("[   ] Done drawing the Boxes")
+}
+
+func drawBox(s *svg.SVG, node *structs.Node) {
+	if node.Boundry != (structs.BoundingBox{}) {
+		x := int(node.Boundry.Center.X / 2000)
+		y := int(node.Boundry.Center.Y / 2000)
+		w := int(node.Boundry.Width / 2000)
+		fmt.Printf("Box {{%d, %d}, %d}\n", x, y, w)
+		s.CenterRect(x, y, w, w, "fill:none;stroke:white")
+	}
+
+	for i := 0; i < len(node.Subtrees); i++ {
+		if node.Subtrees[i] != nil {
+			drawBox(s, node.Subtrees[i])
+		}
+	}
+}
+
+func getGalaxy(index int64) {
+	log.Println("[   ] Getting the Galaxy")
+	// make a http-post request to the databse requesting the tree
+	requesturl := fmt.Sprintf("http://db.nbg1.emile.space/dumptree/%d", index)
+	resp, err := http.Get(requesturl)
+	if err != nil {
+		panic(err)
+	}
+	defer resp.Body.Close()
+
+	body, readerr := ioutil.ReadAll(resp.Body)
+	if readerr != nil {
+		panic(readerr)
+	}
+
+	tree := &structs.Node{}
+	jsonUnmarshalErr := json.Unmarshal(body, tree)
+	if jsonUnmarshalErr != nil {
+		panic(jsonUnmarshalErr)
+	}
+
+	// if the treeArray is not long enough, fill it
+	for int(index) > len(treeArray) {
+		emptyNode := structs.NewNode(structs.NewBoundingBox(structs.NewVec2(0, 0), 10))
+		treeArray = append(treeArray, emptyNode)
+	}
+
+	treeArray = append(treeArray, tree)
+	log.Println("[   ] Done Getting the galaxy")
+}
+
+func main() {
+	router := mux.NewRouter()
+
+	router.HandleFunc("/drawtree/{treeindex}", drawTree).Methods("GET")
+
+	log.Fatal(http.ListenAndServe(":2003", router))
+}