-
Notifications
You must be signed in to change notification settings - Fork 8
/
kdtree.h
192 lines (152 loc) · 3.9 KB
/
kdtree.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
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
#ifndef KDTREE_HDR
#define KDTREE_HDR
#include <stdint.h>
#include "u_string.h"
#include "u_vector.h"
#include "u_set.h"
#include "m_plane.h"
#include "m_quat.h"
struct kdTree;
struct kdEnt {
uint32_t id;
m::vec3 origin;
m::quat rotation;
};
struct kdTriangle {
kdTriangle();
m::vec3 getNormal(const kdTree *const tree);
void generatePlane(const kdTree *const tree);
private:
friend struct kdTree;
friend struct kdNode;
int m_vertices[3];
int m_texCoords[3];
m::plane m_plane;
const u::string *m_textureReference;
};
inline kdTriangle::kdTriangle() {
memset(this, 0, sizeof *this);
}
enum polyPlane : size_t {
kPolyPlaneSplit,
kPolyPlaneBack,
kPolyPlaneFront,
kPolyPlaneCoplanar
};
struct kdNode {
kdNode(kdTree *tree, const u::vector<int> &triangles, size_t recursionDepth);
~kdNode();
// Calculate bounding sphere for node
bool calculateSphere(const kdTree *tree, const u::vector<int> &triangles);
// Is the node a leaf?
bool isLeaf() const;
// Find the best plane to split on
m::plane findSplittingPlane(const kdTree *tree, const u::vector<int> &triangles, m::axis axis) const;
// Split triangles along `axis' axis.
void split(const kdTree *tree, const u::vector<int> &triangles, m::axis axis,
u::vector<int> &front, u::vector<int> &back, u::vector<int> &splitlist, m::plane &splitplane) const;
// Flatten tree representation into a disk-writable medium.
u::vector<unsigned char> serialize();
kdNode *m_front;
kdNode *m_back;
m::plane m_splitPlane; // Not set for leafs
m::vec3 m_sphereOrigin;
float m_sphereRadius;
u::vector<int> m_triangles; // Only filled for leaf node
};
struct kdTree {
kdTree();
~kdTree();
static constexpr float kMaxTraceDistance = 99999.999f;
static constexpr float kEpsilon = 0.01f; // Plane offset for point classification
static constexpr size_t kMaxTrianglesPerLeaf = 5;
static constexpr size_t kMaxRecursionDepth = 35;
bool load(const u::string &file);
polyPlane testTriangle(size_t index, const m::plane &plane) const;
void unload();
u::vector<unsigned char> serialize();
private:
friend struct kdNode;
friend struct kdTriangle;
kdNode *m_root;
u::vector<m::vec3> m_vertices;
u::vector<m::vec2> m_texCoords;
u::vector<kdTriangle> m_triangles;
u::vector<kdEnt> m_entities;
u::set<u::string> m_textures;
size_t m_nodeCount;
size_t m_leafCount;
size_t m_textureCount;
size_t m_depth;
};
// Serialized version for storing on disk
#pragma pack(push, 1)
struct kdBinHeader {
kdBinHeader()
: magic(kMagic)
, version(kVersion)
, padding(0)
{
}
enum : uint32_t {
kMagic = 0x66551133,
kVersion = 1
};
uint32_t magic;
uint32_t version;
uint8_t padding;
void endianSwap();
};
struct kdBinEntry {
uint32_t offset;
uint32_t length;
void endianSwap();
};
struct kdBinPlane {
uint8_t type;
float d;
void endianSwap();
};
struct kdBinTexture {
char name[255];
};
struct kdBinNode {
uint32_t plane;
int32_t children[2]; // Leaf indices are stored with negitive index
float sphereRadius;
m::vec3 sphereOrigin;
void endianSwap();
};
struct kdBinTriangle {
uint32_t texture;
uint32_t v[3];
void endianSwap();
};
struct kdBinVertex {
// GPU layout:
// P.X P.Y P.Z N.X
// N.Y N.Z C.X C.Y
// T.X T.Y T.Z T.W
m::vec3 vertex;
m::vec3 normal;
m::vec2 coordinate;
m::vec4 tangent;
void endianSwap();
};
struct kdBinEnt {
kdBinEnt()
: id(0)
, rotation(0.0f, 0.0f, 0.0f, 1.0f)
{
}
uint32_t id;
m::vec3 origin;
m::quat rotation;
void endianSwap();
};
#pragma pack(pop)
struct kdBinLeaf {
// we treat this as a flexible array member
u::vector<uint32_t> triangles;
};
#endif