-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKDTree.h
69 lines (50 loc) · 1.62 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
#ifndef RAYTRACING_KDTREE_H
#define RAYTRACING_KDTREE_H
#include "Vector.h"
#include "Primitives.h"
enum Plane {
XY, XZ, YZ, NONE
};
class KDNode {
const static int maxRecursionDepth = 15;
const static int maxObjectsInList = 2;
enum Plane plane;
BoundingBox boundingBox;
KDNode *left;
KDNode *right;
std::vector<Primitive *> nodePrimitives;
#ifdef COUNT_STATS
static int _buildCalls, _findCalls;
#endif
static void dividePrimitives(const Point &middlePoint, const Vector &planeNormal,
const std::vector<Primitive *> &primitives,
std::vector<Primitive *> &leftNodePrimitives,
std::vector<Primitive *> &rightNodePrimitives);
public:
KDNode(const std::vector<Primitive *> &primitives, int iter, const BoundingBox &nodeBox);
~KDNode() {
// doesn't delete primitives
delete left;
delete right;
}
bool findRayIntersection(const Point &rayStart, const Vector &rayDirection, Intersection &nearestIntersection);
#ifdef COUNT_STATS
static int _getBuildCalls() { return _buildCalls; }
static int _getFindCalls() { return _findCalls; }
static void _resetStats() {
_buildCalls = 0;
_findCalls = 0;
}
#endif
};
class KDTree {
KDNode *root;
public:
KDTree() : root(nullptr) { }
~KDTree() {
delete root;
}
void buildTree(const std::vector<Primitive *> &primitives);
bool findRayIntersection(const Point &rayStart, const Vector &rayDirection, Intersection &nearestIntersection);
};
#endif //RAYTRACING_KDTREE_H