about summary refs log tree commit diff
path: root/quadtree.go
blob: 22cf46c64390f8d0e5962089e653ec1f37c0c142 (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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package structs

import (
	"log"
)

// Quadtree defines a quadtree and it's nodes recursively
type Quadtree struct {
	Boundary     BoundingBox `json:"boundary"`     // Spatial outreach of the quadtree
	CenterOfMass Vec2        `json:"CenterOfMass"` // Center of mass of the cell
	TotalMass    float64     `json:"totalMass"`    // Total mass of the cell
	Depth        int         `json:"depth"`        // Depth of the cell in the quadtree
	Star         Star2D      `json:"star"`         // Star inside the cell
	Leaf         bool        `json:"Leaf"`         // Quadtree is a leaf or not

	// NW, NE, SW, SE
	Quadrants [4]*Quadtree `json:"Quadrants"` // List of quadtrees representing individual Quadrants
}

type object struct {
	position Vec2
	mass     float64
	size     float64
	velocity Vec2
}

type node struct {
	father node
	Nodes  []*object
}

// NewQuadtree generates a new root node.
func NewQuadtree(boundary BoundingBox) *Quadtree {
	newquadtree := &Quadtree{
		Boundary: BoundingBox{
			Center: Vec2{
				X: boundary.Center.X,
				Y: boundary.Center.Y,
			},
			Width: boundary.Width,
		},
		CenterOfMass: Vec2{},
		TotalMass:    0,
		Depth:        0,
		Star:         Star2D{},
		Leaf:         true,
		Quadrants:    [4]*Quadtree{},
	}
	log.Printf("[+++] New Quadtree: %v", newquadtree)
	return newquadtree
}

// SetCenterOfMass is a setter method for quadtrees.
// It sets the CenterOfMass of the quadtree to the given value
func (q *Quadtree) SetCenterOfMass(centerOfMass Vec2) {
	q.CenterOfMass = centerOfMass
}

// CalcCenterOfMass is a calculator method for quadtrees.
// It recursively walks through the quadtree and calculates it's Center of mass.
// The calculated Center of mass is then inserted into the CenterOfMass variable.
func (q *Quadtree) CalcCenterOfMass() (Vec2, float64) {
	var totalMass float64 = 0
	var x float64 = 0
	var y float64 = 0

	q.IsLeaf()
	// If the Node is a leaf
	if q.Leaf == true {

		// update the values needed to calculate the Center of mass
		totalMass += q.Star.M
		x += q.Star.C.X * q.Star.M
		y += q.Star.C.X * q.Star.M

		return Vec2{x, y}, totalMass

	} else {

		// Iterate over all the Quadrants
		for _, element := range q.Quadrants {

			// Calculate the Center of mass for each quadrant
			centerOfMass, totalMass := element.CalcCenterOfMass()

			// Update the overall CenterOfMass for the individual quadtree
			q.CenterOfMass.X += centerOfMass.X
			q.CenterOfMass.Y += centerOfMass.Y
			q.TotalMass += totalMass
		}
	}

	// Return the original CenterOfMass and totalMass
	return q.CenterOfMass, q.TotalMass
}

// IsLeaf is a method for quadtrees returning true if the node is a leaf (has no children)
// or returning false if the node is nor a leaf (has children).
func (q *Quadtree) IsLeaf() {

	// assume that the node is a leaf
	q.Leaf = true

	// iterate over all the elements in the quadtree (all the quadrants)
	for _, element := range q.Quadrants {

		// if one of the quadrants is not nil , the node is not a leaf
		if element != nil {
			q.Leaf = false
		}
	}
}

// Insert inserts the given point into the quadtree the method is called on
// func (q *Quadtree) Insert(point Star2D) {
// 	log.Printf("[   ] Inserting point %v into the tree %v", point, q)
//
// 	// prints the stars inside of the leaves of the current node
// 	log.Printf("[>>>] - Current Star [node]: %v", q.Star)
// 	for i := 0; i < 4; i++ {
// 		if q.Quadrants[i] != nil {
// 			log.Printf("[>>>] - Current Star [%d]: %v", i, q.Quadrants[i].Star)
// 		} else {
// 			log.Printf("[>>>] - Current Star [%d]: ", i)
// 		}
// 	}
//
// 	// create shortcuts for the various bounding box variables
// 	bx := q.Boundary.Center.X
// 	by := q.Boundary.Center.Y
// 	bw := q.Boundary.Width
// 	log.Printf("[ ~ ] \t Bounding Box X: %f", bx)
// 	log.Printf("[ ~ ] \t Bounding Box Y: %f", by)
// 	log.Printf("[ ~ ] \t Bounding Box Width: %f", bw)
//
// 	// Insert the given star into the tree
// 	// Case 1: There is no star inside of the node
// 	if q.Star == (Star2D{Vec2{}, Vec2{}, 0}) {
// 		log.Printf("[   ] There was no star inside of the node -> inserting directly")
// 		q.Star = point
//
// 		// if the star is not a leaf, try to insert the star into the correct leaf
// 		// if there all ready is a star inside of the leaf, subdivide that leaf and insert the two nodes recursively
// 		// into that leaf
// 		// TODO: implement the comment above
//
// 	// Case 2: There is all ready a star inside of the node
// 	} else {
// 		log.Printf("[ ! ] There is allready a star inside of the node -> subdividing")
//
// 		// Test if the star is left or right of the center point
// 		if point.C.X < bx && point.C.X > bx-bw {
// 			log.Println("[<  ] \t\t The point is left of the center point!")
//
// 			// Test if the star is above or below the center point
// 			if point.C.Y > by && point.C.Y < by+bw {
// 				log.Println("[ ^ ] \t\t The point is above of the center point!")
//
// 				// Subdivide if...
// 				// ... the quadrant does not contain a node yet or if there is all ready a node in the quadrant
// 				// ... the quadrant is a leaf
// 				if (q.Quadrants[0] == nil || q.Quadrants[0].Star != (Star2D{Vec2{}, Vec2{}, 0})) && q.Leaf == true {
// 					q.subdivide()
// 				} else {
// 					q.Quadrants[0].Insert(point)
// 				}
//
// 			} else {
// 				log.Println("[ v ] \t\t The point is below of the center point!")
// 				if (q.Quadrants[2] == nil || q.Quadrants[2].Star != (Star2D{Vec2{}, Vec2{}, 0})) && q.Leaf == true {
// 					q.subdivide()
// 				} else {
// 					q.Quadrants[2].Insert(point)
// 				}
// 			}
//
// 		} else {
// 			log.Println("[ > ] \t\t The point is right of the center point!")
//
// 			// Test if the star is above or below the center point
// 			if point.C.Y > by && point.C.Y < by+bw {
// 				log.Println("[ ^ ] \t\t The point is above of the center point!")
// 				if (q.Quadrants[1] == nil || q.Quadrants[1].Star != (Star2D{Vec2{}, Vec2{}, 0})) && q.Leaf == true {
// 					q.subdivide()
// 					q.Quadrants[1].Insert(point)
// 				} else {
// 					q.Quadrants[1].Insert(point)
// 				}
// 			} else {
// 				log.Println("[ v ] \t\t The point is below of the center point!")
// 				if (q.Quadrants[3] == nil || q.Quadrants[3].Star != (Star2D{Vec2{}, Vec2{}, 0})) && q.Leaf == true {
// 					q.subdivide()
// 				} else {
// 					q.Quadrants[3].Insert(point)
// 				}
// 			}
// 		}
// 	}
//
// 	log.Printf("[>>>] Current Star [node]: %v", q.Star)
// 	for i := 0; i < 4; i++ {
// 		if q.Quadrants[i] != nil {
// 			log.Printf("[>>>] Current Star [%d]: %v", i, q.Quadrants[i].Star)
// 		} else {
// 			log.Printf("[>>>] Current Star [%d]: ", i)
// 		}
// 	}
//
// 	// log.Printf("[   ] Tree after insertion: %v", *q)
// 	// q.print()
// }

// is Leaf returns true if the given node is a leaf (has no children) and false if it has children
func (q *Quadtree) isLeaf() bool {
	if q.Quadrants == ([4]*Quadtree{}) {
		return true
	} else {
		return false
	}
}

// noStar returns true if the current node does not contain a star and false if the node contains a star
func (q *Quadtree) noStar() bool {
	if q.Star == (Star2D{}) {
		return true
	} else {
		return false
	}
}

// NewInsert is a method inserting the given point (star) into the tree it is called on.
//
// The Method makes sure that all the points (stars) star are positioned in a leaf or get moved into a leaf
// so if there all ready is a star (B) in the node the star should be inserted into, (B) gets moved into the
// Leaf it belongs and the star that should be inserted gets inserted into it's place and is then shifted
// into the right child. This is repeated, until all the stars are inside of a leaf.
func (q *Quadtree) NewInsert(point Star2D) {
	log.Printf("[   ] Inserting the point %v into the tree", point)

	// so, using the example from the writeup, let's build this. we'll start with one of the more
	// simple cases: we've got an empty tree with an empty node and no children.
	// Inserting now works the following way:
	// The star gets inserted directly into the node, it now is a leaf node, because all of it's
	// children are non existent.
	//
	// Is a leaf (one of our goals \o/) and contains no star (our other goal \o/).
	if q.noStar() == true && q.Leaf == true {
		log.Println("[ + ] There is no other star in the node -> inserting directly")
		q.Star = point
		return
	}

	// Condition: cannot be inserted and into node that is a leaf because of another blocking star
	// Solution: insert the star blocking the node into the nodes subtree and then insert the star
	// into the nodes subtree as well, if the subtree is occupied by an other star, reinsert
	if q.noStar() == false && q.Leaf == true {
		q.insertHasStarIsLeaf(point)
		return
	}

	if q.noStar() == true && q.Leaf == false {
		q.insertHasNoStarIsNotLeaf(point)
		return
	}

	if q.noStar() == false && q.Leaf == false {
		q.insertHasNoStarIsNotLeaf(point)
		return
	}

	// // Let's continue with the case above: we've now got a tree with a star (A) in the space we want
	// // to insert the new star (B), so we need to shift (A) down into the tree and then insert (B)
	// // into the corresponding space so long, until they both are in leaves.
	// if q.noStar() == false {
	//
	// 	// We define A as the star that is all ready inside of the tree and B as the star that should
	// 	// be inserted
	// 	A := q.Star
	// 	B := point
	//
	// 	// The if conditions below evaluate in which of the cells A is.
	// 	// If there is no tree in that quadrant, the tree is subdivided and a 4 new quadrants are generated
	// 	// In the end, the star is inserted into that quadrant
	// 	if A.C.Y > by && A.C.Y < by+bw {
	// 		if A.C.Y > bx && A.C.Y < bx+bw {
	// 			if q.Quadrants[0] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[0].NewInsert(A)
	// 		} else {
	// 			if q.Quadrants[2] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[2].NewInsert(A)
	// 		}
	//
	// 	} else {
	// 		// Test if b is left or right of the x range
	// 		if A.C.Y > bx && A.C.Y < bx+bw {
	// 			if q.Quadrants[1] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[1].NewInsert(A)
	// 		} else {
	// 			if q.Quadrants[3] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[3].NewInsert(A)
	// 		}
	// 	}
	//
	// 	// So after inserting A into the tree, we still need to insert B:
	// 	if B.C.Y > by && B.C.Y < by+bw {
	// 		if B.C.Y > bx && B.C.Y < bx+bw {
	// 			if q.Quadrants[0] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[0].NewInsert(B)
	// 		} else {
	// 			if q.Quadrants[2] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[2].NewInsert(B)
	// 		}
	//
	// 	} else {
	// 		if B.C.Y > bx && B.C.Y < bx+bw {
	// 			if q.Quadrants[1] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[1].NewInsert(B)
	// 		} else {
	// 			if q.Quadrants[3] == nil {
	// 				q.subdivide()
	// 			}
	// 			q.Quadrants[3].NewInsert(B)
	// 		}
	// 	}
	// }

}

// insertHasStarIsLeaf inserts a given point into the quadtree it is called on.
// The condition is, that the node in which the star should be inserted into already contains a star, so the
// star that is all ready inside the node must be moved further into the tree before inserting the new star
func (q *Quadtree) insertHasStarIsLeaf(point Star2D) {
	q.GeneratePrintTree(0)

	bx := q.Boundary.Center.X
	by := q.Boundary.Center.Y
	bw := q.Boundary.Width

	log.Println("[ i ] Star blocking the node, but the node is a leaf")

	log.Println("[   ] Subdividing the node")
	// subdivide the node
	q.subdivide()

	log.Println("[   ] Inserting the star blocking the node into the nodes subtree")

	// insert the node blocking the tree into the newly created subtree
	A := q.Star
	if A.C.Y > by && A.C.Y < by+bw {
		log.Println("[<  ] \t\t The point is left of the center point!")
		if A.C.Y > bx && A.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			if q.Quadrants[0].Star != (Star2D{}) {
				q.Quadrants[0].insertHasStarIsLeaf(point)
			} else {
				q.Quadrants[0].NewInsert(A)
			}
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			if q.Quadrants[2].Star != (Star2D{}) {
				q.Quadrants[2].insertHasStarIsLeaf(point)
			} else {
				q.Quadrants[2].NewInsert(A)
			}
		}

	} else {
		log.Println("[ > ] \t\t The point is right of the center point!")
		// Test if b is left or right of the x range
		if A.C.Y > bx && A.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			if q.Quadrants[1].Star != (Star2D{}) {
				q.Quadrants[1].insertHasStarIsLeaf(point)
			} else {
				q.Quadrants[1].NewInsert(A)
			}
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			if q.Quadrants[3].Star != (Star2D{}) {
				q.Quadrants[3].insertHasStarIsLeaf(point)
			} else {
				q.Quadrants[3].NewInsert(A)
			}
		}
	}

	log.Println("[   ] Inserting the new star into the new subtree")

	// Insert the point into the newly created subtree
	B := point
	if B.C.Y > by && B.C.Y < by+bw {
		log.Println("[<  ] \t\t The point is left of the center point!")
		if B.C.Y > bx && B.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			q.Quadrants[0].NewInsert(B)
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			q.Quadrants[2].NewInsert(B)
		}

	} else {
		log.Println("[ > ] \t\t The point is right of the center point!")
		if B.C.Y > bx && B.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			q.Quadrants[1].NewInsert(B)
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			q.Quadrants[3].NewInsert(B)
		}
	}

	// retry to insert the point
	q.NewInsert(point)

	q.Star = Star2D{}
}

// insertHasNoStarIsNotLeaf inserts a given point into the quadtree it is called on.
// The condition is, that the node in which the star should be inserted into does not contain a star, but
// also isn't a leaf.
func (q *Quadtree) insertHasNoStarIsNotLeaf(point Star2D) {
	log.Println("[ i ] The node does not contain a star and is not a leaf")

	bx := q.Boundary.Center.X
	by := q.Boundary.Center.Y
	bw := q.Boundary.Width

	q.GeneratePrintTree(0)

	// Inserting the star into the subtree
	// If there is all ready a star in the slot, insert that star into it's subtree and then insert the star into
	// that subtree recursively
	A := point
	if A.C.Y > by && A.C.Y < by+bw {
		log.Println("[<  ] \t\t The point is left of the center point!")
		if A.C.Y > bx && A.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			if q.Quadrants[0].Star != (Star2D{}) {
				q.Quadrants[0].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[0].NewInsert(A)
			}
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			if q.Quadrants[2].Star != (Star2D{}) {
				q.Quadrants[2].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[2].NewInsert(A)
			}
		}

	} else {
		log.Println("[ > ] \t\t The point is right of the center point!")
		// Test if b is left or right of the x range
		if A.C.Y > bx && A.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			if q.Quadrants[1].Star != (Star2D{}) {
				q.Quadrants[1].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[1].NewInsert(A)
			}
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			if q.Quadrants[3].Star != (Star2D{}) {
				q.Quadrants[3].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[3].NewInsert(A)
			}
		}
	}

}

// insertHasStarIsNotLeaf inserts a given point into the quadtree it is called on.
// The condition is, that the node in which the star should be inserted into does contain a star, but
// also isn't a leaf.
func (q *Quadtree) insertHasStarIsNotLeaf(point Star2D) {
	log.Println("[ i ] The node contains a star and is not a leaf")

	bx := q.Boundary.Center.X
	by := q.Boundary.Center.Y
	bw := q.Boundary.Width

	q.GeneratePrintTree(0)

	// Inserting the star into the subtree
	// Because of there all ready being a star in the tree, the star in the tree get's inserted into the
	// subtree first, the point to be inserted is then also inserted into the subtree
	A := q.Star
	if A.C.Y > by && A.C.Y < by+bw {
		log.Println("[<  ] \t\t The point is left of the center point!")
		if A.C.Y > bx && A.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			if q.Quadrants[0].Star != (Star2D{}) {
				q.Quadrants[0].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[0].NewInsert(A)
			}
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			if q.Quadrants[2].Star != (Star2D{}) {
				q.Quadrants[2].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[2].NewInsert(A)
			}
		}

	} else {
		log.Println("[ > ] \t\t The point is right of the center point!")
		// Test if b is left or right of the x range
		if A.C.Y > bx && A.C.Y < bx+bw {
			log.Println("[ ^ ] \t\t The point is above of the center point!")
			if q.Quadrants[1].Star != (Star2D{}) {
				q.Quadrants[1].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[1].NewInsert(A)
			}
		} else {
			log.Println("[ v ] \t\t The point is below of the center point!")
			if q.Quadrants[3].Star != (Star2D{}) {
				q.Quadrants[3].NewInsert(q.Star)
				q.Quadrants[0].NewInsert(point)
			} else {
				q.Quadrants[3].NewInsert(A)
			}
		}
	}
}

// subdivide subdivides the quadtree it is called on
func (q *Quadtree) subdivide() {
	// Toggle the leaf state: the node is not a leaf anymore
	q.Leaf = false

	// Get the "old" bounding box values
	oldCenterX := q.Boundary.Center.X
	oldCenterY := q.Boundary.Center.Y
	oldWidth := q.Boundary.Width

	// Calculate the bounding box values for the new quadrants
	newCenterNorthWest := Vec2{oldCenterX - (oldWidth / 4), oldCenterY + (oldWidth / 4)}
	newCenterNorthEast := Vec2{oldCenterX + (oldWidth / 4), oldCenterY + (oldWidth / 4)}
	newCenterSouthWest := Vec2{oldCenterX - (oldWidth / 4), oldCenterY - (oldWidth / 4)}
	newCenterSouthEast := Vec2{oldCenterX + (oldWidth / 4), oldCenterY - (oldWidth / 4)}

	// Calculate the new width
	newWidth := oldWidth / 2

	// Define the new bounding boxes using the values calculated above
	NorthWestBoundingBox := NewBoundingBox(newCenterNorthWest, newWidth)
	NorthEastBoundingBox := NewBoundingBox(newCenterNorthEast, newWidth)
	SouthWestBoundingBox := NewBoundingBox(newCenterSouthWest, newWidth)
	SouthEastBoundingBox := NewBoundingBox(newCenterSouthEast, newWidth)

	// Generate new quadrants using the new bounding boxes and assign them to the quadtree
	q.Quadrants[0] = NewQuadtree(NorthWestBoundingBox)
	q.Quadrants[1] = NewQuadtree(NorthEastBoundingBox)
	q.Quadrants[2] = NewQuadtree(SouthWestBoundingBox)
	q.Quadrants[3] = NewQuadtree(SouthEastBoundingBox)
}