-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquadtree.h
110 lines (98 loc) · 2.64 KB
/
quadtree.h
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
#pragma once
#include <cstdint>
#include <vector>
//
#include <glm/glm.hpp>
#include <SFML/Graphics.hpp>
#include <imgui.h>
#include <fmt/format.h>
//
#include "helper.h"
#include "shape.h"
class quadtree
{
public:
quadtree(const glm::vec4 &boundary_, uint32_t capacity_)
: boundary(boundary_), debug(randInt(1, 255), randInt(1, 255), randInt(1, 255), 128)
{
capacity = capacity_;
data.reserve(4);
}
~quadtree()
{
for(int i = 0; i < 4; i++)
delete child[i];
}
sf::Color debug;
void debugDraw(sf::RenderTarget &target) // ugly
{
sf::RectangleShape rect;
rect.setPosition(boundary.getX(), boundary.getY());
rect.setSize(sf::Vector2f(boundary.getW(), boundary.getH()));
rect.setFillColor(sf::Color::Transparent);
rect.setOutlineThickness(1);
rect.setOutlineColor(sf::Color::White);
target.draw(rect);
for(int i = 0; i < 4; i++)
if(child[i])
child[i]->debugDraw(target);
}
void imguiDebug()
{
if(ImGui::TreeNode("Points"))
{
for (auto &p : data) {
ImGui::PushID(&p);
ImGui::Text("%p %.2f %.2f", p.parent, p.pos.x, p.pos.y);
ImGui::PopID();
}
ImGui::TreePop();
}
for(int i = 0; i < 4; i++)
{
if(!child[i]) continue;
auto ID = fmt::format("{}", i);
ImGui::PushID(ID.c_str());
if(ImGui::TreeNode(fmt::format("Node {}: {}", ID, child[i]->data.size()).c_str()))
{
child[i]->imguiDebug();
ImGui::TreePop();
}
ImGui::PopID();
}
}
/**
* 插入一個點 p 到 quadtree 中
* @param p 點座標
*/
void insert(const Point &p);
/**
* 查詢給定區域 r 可能產生碰撞的點集合
* @param r 集合
*/
std::vector<Point>& query(std::vector<Point> &li, const Rect &r);
/**
* 查詢給定區域 r 可能產生碰撞的 Body 集合
* @param r 集合
*/
std::vector<Body*>& query(std::vector<Body*> &li, const Rect &r);
/**
* 清空整棵樹
*/
void clear();
/**
* 判斷點 p 是否在區域內(boundary)
* @param p 點座標
* @return true/false
*/
bool contains(const Point &p);
/**
* 分割成四個子區域
*/
void subdivide();
Rect boundary; // 當前節點的邊界 (x, y, w, h)
uint32_t capacity; // 容量
// TODO: Use raw array?
std::vector<Point> data; // 點資料
quadtree *child[4] = {nullptr};
};