about summary refs log tree commit diff
path: root/backend/update.go
blob: 9e78b84901e7084f1d562bbdd66330798f7593a6 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package backend

import (
	"database/sql"
	"fmt"
	"git.darknebu.la/GalaxySimulator/structs"
	"log"
)

// UpdateTotalMass gets a tree index and returns the nodeID of the trees root node
func UpdateTotalMass(database *sql.DB, index int64) {
	db = database
	rootNodeID := getRootNodeID(index)
	log.Printf("RootID: %d", rootNodeID)
	updateTotalMassNode(rootNodeID)
}

// updateTotalMassNode updates the total mass of the given node
func updateTotalMassNode(nodeID int64) float64 {
	var totalmass float64

	// get the subnode ids
	var subnode [4]int64

	query := fmt.Sprintf("SELECT subnode[1], subnode[2], subnode[3], subnode[4] FROM nodes WHERE node_id=%d", nodeID)
	err := db.QueryRow(query).Scan(&subnode[0], &subnode[1], &subnode[2], &subnode[3])
	if err != nil {
		log.Fatalf("[ E ] updateTotalMassNode query: %v\n\t\t\t query: %s\n", err, query)
	}
	// TODO: implement the getSubtreeIDs(nodeID) []int64 {...} function
	// iterate over all subnodes updating their total masses
	for _, subnodeID := range subnode {
		fmt.Println("----------------------------")
		fmt.Printf("SubdnodeID: %d\n", subnodeID)
		if subnodeID != 0 {
			totalmass += updateTotalMassNode(subnodeID)
		} else {
			// get the starID for getting the star mass
			starID := getStarID(nodeID)
			fmt.Printf("StarID: %d\n", starID)
			if starID != 0 {
				mass := getStarMass(starID)
				log.Printf("starID=%d \t mass: %f", starID, mass)
				totalmass += mass
			}

			// break, this stops a star from being counted multiple (4) times
			break
		}
		fmt.Println("----------------------------")
	}

	query = fmt.Sprintf("UPDATE nodes SET total_mass=%f WHERE node_id=%d", totalmass, nodeID)
	rows, err := db.Query(query)
	defer rows.Close()
	if err != nil {
		log.Fatalf("[ E ] insert total_mass query: %v\n\t\t\t query: %s\n", err, query)
	}

	fmt.Printf("nodeID: %d \t totalMass: %f\n", nodeID, totalmass)

	return totalmass
}

// UpdateCenterOfMass recursively updates the center of mass of all the nodes starting at the node with the given
// root index
func UpdateCenterOfMass(database *sql.DB, index int64) {
	db = database
	rootNodeID := getRootNodeID(index)
	log.Printf("RootID: %d", rootNodeID)
	updateCenterOfMassNode(rootNodeID)
}

// updateCenterOfMassNode updates the center of mass of the node with the given nodeID recursively
// center of mass := ((x_1 * m) + (x_2 * m) + ... + (x_n * m)) / m
func updateCenterOfMassNode(nodeID int64) structs.Vec2 {
	fmt.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")

	var centerOfMass structs.Vec2

	// get the subnode ids
	var subnode [4]int64
	var starID int64

	query := fmt.Sprintf("SELECT subnode[1], subnode[2], subnode[3], subnode[4], star_id FROM nodes WHERE node_id=%d", nodeID)
	err := db.QueryRow(query).Scan(&subnode[0], &subnode[1], &subnode[2], &subnode[3], &starID)
	if err != nil {
		log.Fatalf("[ E ] updateCenterOfMassNode query: %v\n\t\t\t query: %s\n", err, query)
	}

	// if the nodes does not contain a star but has children, update the center of mass
	if subnode != ([4]int64{0, 0, 0, 0}) {
		log.Println("[   ] recursing deeper")

		// define variables storing the values of the subnodes
		var totalMass float64
		var centerOfMassX float64
		var centerOfMassY float64

		// iterate over all the subnodes and calculate the center of mass of each node
		for _, subnodeID := range subnode {
			subnodeCenterOfMass := updateCenterOfMassNode(subnodeID)

			if subnodeCenterOfMass.X != 0 && subnodeCenterOfMass.Y != 0 {
				fmt.Printf("SubnodeCenterOfMass: (%f, %f)\n", subnodeCenterOfMass.X, subnodeCenterOfMass.Y)
				subnodeMass := getNodeTotalMass(subnodeID)
				totalMass += subnodeMass

				centerOfMassX += subnodeCenterOfMass.X * subnodeMass
				centerOfMassY += subnodeCenterOfMass.Y * subnodeMass
			}
		}

		// calculate the overall center of mass of the subtree
		centerOfMass = structs.Vec2{
			X: centerOfMassX / totalMass,
			Y: centerOfMassY / totalMass,
		}

		// else, use the star as the center of mass (this can be done, because of the rule defining that there
		// can only be one star in a cell)
	} else {
		log.Println("[   ] using the star in the node as the center of mass")
		log.Printf("[   ] NodeID: %v", nodeID)
		starID := getStarID(nodeID)

		if starID == 0 {
			log.Println("[   ] StarID == 0...")
			centerOfMass = structs.Vec2{
				X: 0,
				Y: 0,
			}
		} else {
			log.Printf("[   ] NodeID: %v", starID)
			star := GetStar(starID)
			centerOfMassX := star.C.X
			centerOfMassY := star.C.Y
			centerOfMass = structs.Vec2{
				X: centerOfMassX,
				Y: centerOfMassY,
			}
		}
	}

	// build the query
	query = fmt.Sprintf("UPDATE nodes SET center_of_mass='{%f, %f}' WHERE node_id=%d", centerOfMass.X, centerOfMass.Y, nodeID)

	// Execute the query
	rows, err := db.Query(query)
	defer rows.Close()
	if err != nil {
		log.Fatalf("[ E ] update center of mass query: %v\n\t\t\t query: %s\n", err, query)
	}

	fmt.Printf("[   ] CenterOfMass: (%f, %f)\n", centerOfMass.X, centerOfMass.Y)

	return centerOfMass
}

// updateStarForce updates the force acting on the star
func updateStarForce(db *sql.DB, starID int64, force structs.Vec2) structs.Star2D {

	star := GetStar(starID)
	newStar := structs.Star2D{
		structs.Vec2{star.C.X, star.C.Y},
		structs.Vec2{force.X, force.Y},
		star.M,
	}

	// updated the stars Force
	query := fmt.Sprintf("UPDATE stars SET vx=%f, vy=%f WHERE star_id=%d", force.X, force.Y, starID)
	rows, err := db.Query(query)
	defer rows.Close()
	if err != nil {
		log.Fatalf("[ E ] updateStarForce query: %v\n\t\t\t query: %s\n", err, query)
	}

	return newStar
}