14 #ifndef G3D_PointKDTree_h
15 #define G3D_PointKDTree_h
104 #define TreeType PointKDTree<T, PositionFunc, HashFunc, EqualsFunc>
119 PositionFunc::getPosition(v, m_position);
136 if (point.
size() == 0) {
140 AABox bounds(point[0].position());
142 for (
int p = 0; p < point.
size(); ++p) {
143 bounds.
merge(point[p].position());
180 for (
int i = 0; i < 2; ++i) {
188 Node(
const Node& other) : valueArray(other.valueArray) {
192 for (
int i = 0; i < 2; ++i) {
202 for (
int i = 0; i < 2; ++i) {
211 for (
int i = 0; i < 2; ++i) {
219 return (child[0] ==
NULL) && (child[1] ==
NULL);
228 handleArray.
append(valueArray);
229 for (
int i = 0; i < 2; ++i) {
230 if (child[i] !=
NULL) {
245 for (
int i = 0; i < valueArray.
length(); ++i) {
246 const Vector3& b = valueArray[i].position();
251 if (child[0] || child[1]) {
261 if (child[0] !=
NULL) {
265 if (child[1] !=
NULL) {
284 for (
int c = 0; c < 2; ++c) {
299 for (
int c = 0; c < 2; ++c) {
310 if (point[splitAxis] < splitLocation) {
313 if (child[0] !=
NULL) {
316 }
else if (point[splitAxis] > splitLocation) {
319 if (child[1] !=
NULL) {
332 const AABox& sphereBounds,
338 const int N = valueArray.
size();
345 for (
int v = 0; v < N; ++v) {
346 if ((center - handleArray[v].position()).squaredLength() <= r2) {
373 bool useSphere)
const {
376 for (
int v = 0; v < valueArray.
size(); ++v) {
377 if ((useSphere && sphere.
contains(valueArray[v].position())) ||
378 (! useSphere && box.
contains(valueArray[v].position()))) {
398 splitBounds = myBounds;
401 if (child[0] || child[1]) {
407 AABox childBounds[2];
408 myBounds.
split(splitAxis, splitLocation, childBounds[0], childBounds[1]);
410 for (
int c = 0; c < 2; ++c) {
452 if (source.
size() <= valuesPerNode) {
454 node =
new Node(source);
457 for (
int i = 0; i < source.
size(); ++i) {
474 if (numMeanSplits <= 0) {
476 splitLocation = node->
valueArray[0].position()[splitAxis];
479 (source.
size() > 10)) {
486 if (numMeanSplits > 0) {
489 splitLocation = (bounds.
high()[splitAxis] +
490 bounds.
low()[splitAxis]) / 2.0f;
494 v[splitAxis] = splitLocation;
500 # if defined(G3D_DEBUG) && defined(VERIFY_TREE)
501 for (
int i = 0; i < lt.
size(); ++i) {
502 const Vector3& v = lt[i].position();
505 for (
int i = 0; i < gt.
size(); ++i) {
506 debugAssert(gt[i].position()[splitAxis] > splitLocation);
508 for (
int i = 0; i < node->
valueArray.size(); ++i) {
520 node->
child[0] =
makeNode(lt, temp, valuesPerNode, numMeanSplits - 1);
524 node->
child[1] =
makeNode(gt, temp, valuesPerNode, numMeanSplits - 1);
528 for(
int i = 0; i < node->
valueArray.size(); ++i) {
546 for (
int i = 0; i < dst->
valueArray.size(); ++i) {
551 for (
int i = 0; i < 2; ++i) {
604 while (stack.
size() > 0) {
608 for (
int i = 0; i < 2; ++i) {
618 return memberTable.
size();
645 memberTable.
set(value, node);
662 for (
int i = 0; i < valueArray.
size(); ++i) {
667 memberTable.
set(valueArray[i], root);
671 for (
int i = 0; i < valueArray.
size(); ++i) {
699 "Tried to remove an element from a "
700 "PointKDTree that was not present");
705 for (
int i = list.
length() - 1; i >= 0; --i) {
754 void balance(
int valuesPerNode = 40,
int numMeanSplits = 3) {
767 root =
makeNode(handleArray, temp, valuesPerNode, numMeanSplits);
794 if (parentMask == 0) {
796 for (
int v = node->
valueArray.size() - 1; v >= 0; --v) {
801 for (
int c = 0; c < 2; ++c) {
802 if (node->
child[c]) {
814 for (
int p = 0; p < plane.
size(); ++p) {
815 if (((parentMask >> p) & 1) != 0) {
817 const Plane& curPlane = plane[p];
818 for (
int v = node->
valueArray.size() - 1; v >= 0; --v) {
827 uint32 childMask = 0xFFFFFF;
830 for (
int c = 0; c < 2; ++c) {
831 if (node->
child[c] &&
871 for (
int i = 0; i < frustum.
faceArray.size(); ++i) {
916 isEnd(root ==
NULL), box(b),
917 node(const_cast<
Node*>(root)), nextValueArrayIndex(-1) {
930 return ! (*
this == other);
936 }
else if (other.
isEnd) {
942 if ((box != other.
box) || (node != other.
node) ||
949 for (
int i = 0; i < stack.
length(); ++i) {
950 if (stack[i] != other.
stack[i]) {
966 bool foundIntersection =
false;
967 while (! isEnd && ! foundIntersection) {
970 while ((! isEnd) && (nextValueArrayIndex >= node->
valueArray.length())) {
994 nextValueArrayIndex = 0;
1002 while (! isEnd && ! foundIntersection && (nextValueArrayIndex < node->valueArray.length())) {
1004 foundIntersection =
true;
1028 alwaysAssertM(! isEnd,
"Can't dereference the end element of an iterator");
1035 alwaysAssertM(! isEnd,
"Can't dereference the end element of an iterator");
1041 operator T*()
const {
1042 alwaysAssertM(! isEnd,
"Can't dereference the end element of an iterator");
1121 typename MemberTable::Iterator
it;
1123 Iterator(
const typename MemberTable::Iterator& it) : it(it) {}
1127 return !(*
this == other);
1131 return it == other.
it;
1159 operator T*()
const {
static void serializeStructure(const Node *n, BinaryOutput &bo)
Definition: PointKDTree.h:276
Node * root
Definition: PointKDTree.h:564
friend class TreeType
Definition: PointKDTree.h:1117
bool intersects(const AABox &other) const
Definition: AABox.cpp:175
void deserialize(class BinaryInput &b)
Definition: AABox.cpp:99
void getIntersectingMembers(const Frustum &frustum, Array< T > &members) const
Definition: PointKDTree.h:868
Vector3 m_position
Definition: PointKDTree.h:112
Node * findDeepestContainingNode(const Vector3 &point)
Definition: PointKDTree.h:307
int nextValueArrayIndex
Definition: PointKDTree.h:911
BoxIntersectionIterator beginBoxIntersection(const AABox &box) const
Definition: PointKDTree.h:1051
void push(const T &value)
Definition: Array.h:770
Node(const Array< Handle > &pt)
Definition: PointKDTree.h:199
void getMembers(Array< T > &members) const
Definition: PointKDTree.h:1105
const T & operator*() const
Definition: PointKDTree.h:1151
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
void merge(const AABox &a)
Definition: AABox.h:106
Handle()
Definition: PointKDTree.h:117
void fastClear()
Definition: Array.h:419
void setSizeHint(size_t n)
Definition: Table.h:318
Definition: PointKDTree.h:110
Definition: Vector3.h:122
BoxIntersectionIterator & operator++()
Definition: PointKDTree.h:963
float splitLocation
Definition: PointKDTree.h:159
static const Vector3 & minFinite()
Definition: Vector3.cpp:126
PointKDTree()
Definition: PointKDTree.h:570
bool contains(const AABox &other) const
Definition: AABox.h:238
T const * operator->() const
Definition: PointKDTree.h:1034
void clear()
Definition: PointKDTree.h:593
float radius
Definition: Sphere.h:31
Handle(const T &v)
Definition: PointKDTree.h:118
void clear()
Definition: Table.h:578
size_t size() const
Definition: Table.h:589
void serializeStructure(BinaryOutput &bo) const
Definition: PointKDTree.h:1092
Definition: HashTrait.h:105
static Node * deserializeStructure(BinaryInput &bi)
Definition: PointKDTree.h:291
Definition: PositionTrait.h:5
T * getCArray()
Definition: Array.h:256
Dynamic 1D array tuned for performance.
Definition: Array.h:95
Axis primaryAxis() const
Definition: Vector3.cpp:129
Iterator begin() const
Definition: Table.h:562
const T & last() const
Definition: Array.h:923
void writeUInt8(uint8 i)
Definition: BinaryOutput.h:283
arena_t NULL
Definition: jemalloc_internal.h:624
BoxIntersectionIterator(const AABox &b, const Node *root)
Definition: PointKDTree.h:915
Definition: PointKDTree.h:102
void deserialize(std::string &s, BinaryInput &b)
Definition: serialize.h:16
static const AABox & maxFinite()
Definition: AABox.cpp:67
void setPosition(const Vector3 &v)
Definition: PointKDTree.h:123
int length() const
Definition: Array.h:438
void set(const Key &key, const Value &value)
Definition: Table.h:599
const Point3 & low() const
Definition: AABox.h:136
bool contains(const T &value)
Definition: PointKDTree.h:682
void getBounds(class AABox &out) const
Definition: Sphere.cpp:252
Axis
Definition: Vector3.h:122
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
bool isLeaf() const
Definition: PointKDTree.h:218
BoxIntersectionIterator operator++(int)
Definition: PointKDTree.h:1019
void balance(int valuesPerNode=40, int numMeanSplits=3)
Definition: PointKDTree.h:754
#define true
Definition: CascPort.h:17
void medianPartition(Array< T > <Median, Array< T > &eqMedian, Array< T > >Median, Array< T > &tempArray, const Comparator &comparator) const
Definition: Array.h:1247
MemberTable memberTable
Definition: PointKDTree.h:562
Iterator operator++(int)
Definition: PointKDTree.h:1145
void getIntersectingMembers(const Array< Plane > &plane, Array< T > &members) const
Definition: PointKDTree.h:847
bool operator==(const Iterator &other) const
Definition: PointKDTree.h:1130
BoxIntersectionIterator endBoxIntersection() const
Definition: PointKDTree.h:1055
static const Vector3 & zero()
Definition: Vector3.cpp:119
void getHandles(Array< Handle > &handleArray) const
Definition: PointKDTree.h:227
Definition: PointKDTree.h:1115
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
void clearData()
Definition: PointKDTree.h:600
Node * node
Definition: PointKDTree.h:900
bool culledBy(const Array< Plane > &plane, int32 &cullingPlaneIndex, const uint32 testMask, uint32 &childMask) const
T value
Definition: PointKDTree.h:115
void serialize(const std::string &s, BinaryOutput &b)
Definition: serialize.h:12
void getIntersectingMembers(const AABox &sphereBounds, const Sphere &sphere, Array< T > &members) const
Definition: PointKDTree.h:331
Node * cloneTree(Node *src)
Definition: PointKDTree.h:542
const T & operator*() const
Definition: PointKDTree.h:1027
#define debugAssert(exp)
Definition: debugAssert.h:160
Iterator & operator++()
Definition: PointKDTree.h:1137
SmallArray< Face, 6 > faceArray
Definition: Frustum.h:49
const Vector3 & position() const
Definition: PointKDTree.h:127
PointKDTree & operator=(const PointKDTree &src)
Definition: PointKDTree.h:578
void insert(const T &value)
Definition: PointKDTree.h:626
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
void getIntersectingMembers(const AABox &box, Array< T > &members) const
Definition: PointKDTree.h:1064
void getIntersectingMembers(const Sphere &sphere, Array< T > &members) const
Definition: PointKDTree.h:1075
static void getIntersectingMembers(const Array< Plane > &plane, Array< T > &members, Node *node, uint32 parentMask)
Definition: PointKDTree.h:786
Definition: PointKDTree.h:888
double square(double fValue)
Definition: g3dmath.h:698
void serialize(class BinaryOutput &b) const
Definition: AABox.cpp:93
int size() const
Definition: Array.h:430
T * operator->() const
Definition: PointKDTree.h:1155
bool operator==(const BoxIntersectionIterator &other) const
Definition: PointKDTree.h:933
size_t size() const
Definition: PointKDTree.h:617
Node * child[2]
Definition: PointKDTree.h:170
bool contains(const Point3 &point) const
Definition: Sphere.cpp:79
int operator()(const Handle &A, const Handle &B) const
Definition: PointKDTree.h:426
const Iterator end() const
Definition: Table.h:570
static const Vector3 & maxFinite()
Definition: Vector3.cpp:127
Node()
Definition: PointKDTree.h:176
void insert(const Array< T > &valueArray)
Definition: PointKDTree.h:652
Array< Node * > stack
Definition: PointKDTree.h:907
Array< Key > getKeys() const
Definition: Table.h:907
Point3 center
Definition: Sphere.h:30
bool operator!=(const Iterator &other) const
Definition: PointKDTree.h:1126
Node * makeNode(Array< Handle > &source, Array< Handle > &temp, int valuesPerNode, int numMeanSplits)
Definition: PointKDTree.h:444
Definition: BinaryOutput.h:52
Node(const Node &other)
Definition: PointKDTree.h:188
Iterator(const typename MemberTable::Iterator &it)
Definition: PointKDTree.h:1123
~PointKDTree()
Definition: PointKDTree.h:586
BoxIntersectionIterator()
Definition: PointKDTree.h:913
Definition: PointKDTree.h:418
Vector3::Axis splitAxis
Definition: PointKDTree.h:156
friend class TreeType
Definition: PointKDTree.h:890
~Node()
Definition: PointKDTree.h:210
static AABox computeBounds(const Array< Handle > &point)
Definition: PointKDTree.h:133
void writeFloat32(float32 f)
Definition: BinaryOutput.h:312
void partition(const T &partitionElement, Array< T > <Array, Array< T > &eqArray, Array< T > >Array, const Comparator &comparator) const
Definition: Array.h:1194
void split(const Vector3::Axis &axis, float location, AABox &low, AABox &high) const
Definition: AABox.cpp:112
const FieldDescriptor value
Definition: descriptor.h:1522
PointKDTree(const PointKDTree &src)
Definition: PointKDTree.h:573
uint32_t uint32
Definition: g3dmath.h:168
AABox box
Definition: PointKDTree.h:896
Vector3::Axis sortAxis
Definition: PointKDTree.h:420
bool operator!=(const BoxIntersectionIterator &other) const
Definition: PointKDTree.h:929
void verifyNode(const Vector3 &lo, const Vector3 &hi)
Definition: PointKDTree.h:237
const Point3 & high() const
Definition: AABox.h:141
Definition: EqualsTrait.h:19
static const Vector3 & inf()
Definition: Vector3.cpp:124
void append(const T &value)
Definition: Array.h:583
Table< T, Node *, HashFunc, EqualsFunc > MemberTable
Definition: PointKDTree.h:561
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
Iterator end() const
Definition: PointKDTree.h:1180
bool isEnd
Definition: PointKDTree.h:893
void assignSplitBounds(const AABox &myBounds)
Definition: PointKDTree.h:397
bool halfSpaceContains(Point3 point) const
Definition: Plane.h:87
void deserializeStructure(BinaryInput &bi)
Definition: PointKDTree.h:1097
bool containsKey(const Key &key) const
Definition: Table.h:874
AxisComparator(Vector3::Axis s)
Definition: PointKDTree.h:424
Array< Handle > valueArray
Definition: PointKDTree.h:173
AABox splitBounds
Definition: PointKDTree.h:154
Definition: PointKDTree.h:149
MemberTable::Iterator it
Definition: PointKDTree.h:1121
void getIntersectingMembers(const AABox &box, const Sphere &sphere, Array< T > &members, bool useSphere) const
Definition: PointKDTree.h:369
Iterator begin() const
Definition: PointKDTree.h:1171
void update(const T &value)
Definition: PointKDTree.h:726