Cogs.Core
Quadtree.h
1#pragma once
2
3#include "Foundation/Geometry/Glm.hpp"
4
5#include <array>
6#include <vector>
7#include <memory>
8
9namespace Cogs {
10 namespace Core {
11
12 struct QuadBounds
13 {
14 glm::vec2 min;
15 glm::vec2 max;
16 };
17
18 struct QuadNode
19 {
20 // children quads
21 std::array<std::unique_ptr<QuadNode>, 4> mChildren;
22
23 // 2d points representing the arrows living in node. Policy to assign priority is nearness to node center
24 glm::vec2 mNearestArrow;
25 //TODO: This can be improved (after the baseline works) by looking at a cell and immediate
26 //cell neighbours and choosing the most distant point instead of the point closest to the cell midpoint.
27
28 // bounds of this quad
29 QuadBounds mBounds;
30 };
31
33 {
34 std::unique_ptr<QuadNode> mRoot; //Parent most node
35
36
37 // get the dimensions of sub nodes
38 QuadBounds computeSubQuad(const QuadBounds& bounds, int i) const;
39 /* get and index representing to which subquadrant an arrow belongs*/
40 int getSubQuad(const QuadBounds & bounds, const glm::vec2 & arrow) const;
41 /* attempt to split a node, populating it's mChildren array*/
42 void split(QuadNode* node);
43 // clear tree
44 void clear(QuadNode * node);
45
46
47 public:
48 SeaCurrentQuadtree(glm::vec2 min, glm::vec2 max)
49 {
50 mRoot = std::make_unique<QuadNode>();
51 mRoot->mBounds = { min, max };
52 };
53
54 inline QuadNode* getRoot() const { return mRoot.get(); }
55 inline bool isLeaf(const QuadNode* node) { return !static_cast<bool>(node->mChildren[0]); }
56
57 // add a new node to the tree, split quads if conditions are met
58 void insert(QuadNode * node, size_t depth, const glm::vec2 & arrow);
59
60 // find a node in the tree. Returns priority value
61 int search(QuadNode * node, const glm::vec2 & arrow, int priority) const;
62
63 };
64 }
65}
Contains all Cogs related functionality.
Definition: FieldSetter.h:23