about summary refs log tree commit diff
path: root/quadtree.go
blob: 24aedb6ca4935cea9210d5ab519e5549b25a33ac (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
package quadtree

import (
	"fmt"
	"git.darknebu.la/GalaxySimulator/Source/structs"

	//"git.darknebu.la/GalaxySimulator/Source/structs"
	"github.com/fogleman/gg"
)

type Coord struct {
	X float64
	Y float64
}

// newCoord returns a new coordinate using the given values
func NewCoord(x float64, y float64) Coord {
	return Coord{x, y}
}

// InsideOf returns true if the point the method is used on in inside of the quadtreecell
func (point Coord) InsideOf(quadtreeCell *Quadtree) bool {
	// define the bounds
	var lowerXBound = quadtreeCell.boundary.center.X - quadtreeCell.boundary.halfDimension
	var upperXBound = quadtreeCell.boundary.center.X + quadtreeCell.boundary.halfDimension
	var lowerYBound = quadtreeCell.boundary.center.Y - quadtreeCell.boundary.halfDimension
	var upperYBound = quadtreeCell.boundary.center.Y + quadtreeCell.boundary.halfDimension

	// if the point is in the right x range
	if point.X < upperXBound && point.X > lowerXBound {

		// test if the point is in the right y range
		if point.Y < upperYBound && point.Y > lowerYBound {

			// the star is within the bounds: return true
			return true
		}
	}

	// the star is outside of the bounds: return false
	return false
}

// BoundingBox defines a single cell in the Quadtree
type BoundingBox struct {
	center        Coord
	halfDimension float64
}

// NewBoundingBox returns a new bounding with the given struct elements
func NewBoundingBox(inCenter Coord, inHalfDimension float64) BoundingBox {
	return BoundingBox{
		center:        inCenter,
		halfDimension: inHalfDimension,
	}
}

// Quadtree defining the whole Quadtree and a node in itself (recursively)
type Quadtree struct {
	// general information (node ca/home/hanemile/go/src/git.darknebu.la/emile/quadtreepacity, spacial outreach)
	nodeCapacity int
	boundary     BoundingBox

	// slice storing the star coordinates
	pointsSlice []Coord

	// the Quadtree leaves
	northWest *Quadtree
	northEast *Quadtree
	southWest *Quadtree
	southEast *Quadtree

	depth int
}

// NewQuadtree returns a new Quadtree
func NewQuadtree(boundary BoundingBox, depth int) *Quadtree {
	return &Quadtree{
		nodeCapacity: 0,
		boundary:     boundary,
		pointsSlice:  nil,
		northWest:    nil,
		northEast:    nil,
		southWest:    nil,
		southEast:    nil,
		depth:        depth,
	}
}

// Subdvide the given quadtree into multiple cells
func (quadtree *Quadtree) subdivide() {

	// Create the new NorthWest boundingbox
	var newCellBoundingNorthWest = BoundingBox{
		center: Coord{
			X: quadtree.boundary.center.X - quadtree.boundary.halfDimension/2,
			Y: quadtree.boundary.center.Y + quadtree.boundary.halfDimension/2,
		},
		halfDimension: quadtree.boundary.halfDimension / 2,
	}
	quadtree.northWest = NewQuadtree(newCellBoundingNorthWest, quadtree.depth+1)

	// Create the new NorthWest boundingbox
	var newCellBoundingNorthEast = BoundingBox{
		center: Coord{
			X: quadtree.boundary.center.X + quadtree.boundary.halfDimension/2,
			Y: quadtree.boundary.center.Y + quadtree.boundary.halfDimension/2,
		},
		halfDimension: quadtree.boundary.halfDimension / 2,
	}
	quadtree.northEast = NewQuadtree(newCellBoundingNorthEast, quadtree.depth+1)

	// Create the new NorthWest boundingbox
	var newCellBoundingSouthWest = BoundingBox{
		center: Coord{
			X: quadtree.boundary.center.X - quadtree.boundary.halfDimension/2,
			Y: quadtree.boundary.center.Y - quadtree.boundary.halfDimension/2,
		},
		halfDimension: quadtree.boundary.halfDimension / 2,
	}
	quadtree.southWest = NewQuadtree(newCellBoundingSouthWest, quadtree.depth+1)

	// Create the new NorthWest boundingbox
	var newCellBoundingSouthEast = BoundingBox{
		center: Coord{
			X: quadtree.boundary.center.X + quadtree.boundary.halfDimension/2,
			Y: quadtree.boundary.center.Y - quadtree.boundary.halfDimension/2,
		},
		halfDimension: quadtree.boundary.halfDimension / 2,
	}
	quadtree.southEast = NewQuadtree(newCellBoundingSouthEast, quadtree.depth+1)
}

// Insert inserts the given point into the Quadtree the method is applied on
// it returns an error incase the point is not in bounds
func (quadtree *Quadtree) Insert(point Coord) {

	// subdivide the Quadtree into four new cells
	quadtree.subdivide()

	if point.InsideOf(quadtree.northWest) {
		quadtree.northWest.Insert(point)
	}
	if point.InsideOf(quadtree.northEast) {
		quadtree.northEast.Insert(point)
	}
	if point.InsideOf(quadtree.southWest) {
		quadtree.southWest.Insert(point)
	}
	if point.InsideOf(quadtree.southEast) {
		quadtree.southEast.Insert(point)
	}

	// if the maximal depth of the tree has been reached:
	// Insert the point into the slice
	quadtree.pointsSlice = append(quadtree.pointsSlice, NewCoord(point.X, point.Y))
	fmt.Printf("Inserted (%f, %f) in layer %d\n", point.X, point.Y, quadtree.depth)
}

func Print(quadtree *Quadtree) {
	// Define an end condition: if the quadtree-leaf is nil, the root does not continue
	if quadtree.northWest != nil {
		Print(quadtree.northWest)
	}
	if quadtree.northEast != nil {
		Print(quadtree.northEast)
	}
	if quadtree.southWest != nil {
		Print(quadtree.southWest)
	}
	if quadtree.southEast != nil {
		Print(quadtree.southEast)
	}
	fmt.Printf("%d\t", quadtree.depth)
	fmt.Printf("%v\t", quadtree.boundary)
	fmt.Printf("%d\n", len(quadtree.pointsSlice))
}

func CreateWithSlice(slice []structs.Star2D) Quadtree {
	// Create a new bounding box at the center of the Galaxy containing the whole galaxy
	var newCenter structs.Vec2 = structs.Vec2{0, 0}
	var newHalfDimension float64 = 5e5

	// Create a Quadtree containing the whole galaxy
	var boundary BoundingBox = NewBoundingBox(Coord(newCenter), newHalfDimension)
	var depth int = 0
	var newQuadtree *Quadtree = NewQuadtree(boundary, depth)

	fmt.Printf("Inserting %d Elements into the Quadtree\n", len(slice))

	// Insert all the stars from the slice into the quadtree step for step
	for i := 0; i < len(slice); i++ {
		newQuadtree.Insert(Coord(slice[i].C))
	}

	return *newQuadtree
}

func InitializeCanvas() *gg.Context {
	const imageWidth = 8192
	const imageHeight = 8192

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

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

	// Invert the Y axis
	dc.InvertY()

	// Translate the Coordinate System to the middle of the image
	dc.TransformPoint(imageWidth/4, imageHeight/4)

	return dc
}

// recursively iterate over a quadtree and draw all the nodes
func drawQuadTree(dc *gg.Context, quadtree Quadtree) {

	// find filled quadtrees
	if quadtree.northWest != nil {
		drawQuadTree(dc, *quadtree.northWest)
	}
	if quadtree.northEast != nil {
		drawQuadTree(dc, *quadtree.northEast)
	}
	if quadtree.southWest != nil {
		drawQuadTree(dc, *quadtree.southWest)
	}
	if quadtree.southEast != nil {
		drawQuadTree(dc, *quadtree.southEast)
	}

	// don't draw the root node
	if quadtree.depth != 0 {

		// calculate the position of the new rectangle
		origx := quadtree.boundary.center.X
		origy := quadtree.boundary.center.X
		x := (origx - quadtree.boundary.halfDimension) / 1000
		y := (origy - quadtree.boundary.halfDimension) / 1000
		w := quadtree.boundary.halfDimension
		h := quadtree.boundary.halfDimension
		//fmt.Printf("(%f, %f), (%f, %f, %f, %f)\n", origx, origy, x, y, w, h)

		// draw the rectangle
		dc.DrawRectangle(x, y, w, h)
		dc.Stroke()
	}
}

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

func DrawQuadtree(quadtree Quadtree) {
	dc := InitializeCanvas()

	dc.SetRGB(1, 1, 1)
	drawQuadTree(dc, quadtree)
	saveImage(dc, "quadtree.png")
}