110 return *handle == *(m.
handle);
118 return handle->hashCode();
124 template <
class Handle>
struct HashTrait<typename
G3D::_internal::Indirector<Handle> > {
195 #define TreeType KDTree<T, BoundsFunc, HashFunc, EqualsFunc>
219 BoundsFunc::getBounds(v, bounds);
225 return EqualsFunc::equals(value, other.
value);
243 for (
int p = beginIndex; p <= endIndex; ++p) {
248 const Vector3& pLo = point[p]->bounds.low();
249 const Vector3& pHi = point[p]->bounds.high();
250 for (
int a = 0; a < 3; ++a) {
256 return AABox(lo, hi);
376 for (
int i = 0; i < 2; ++i) {
384 Node(
const Node& other) : valueArray(other.valueArray), boundsArray(other.boundsArray) {
388 for (
int i = 0; i < 2; ++i) {
398 for (
int i = 0; i < 2; ++i) {
403 for (
int i = 0; i < valueArray.
size(); ++i) {
404 boundsArray[i] = valueArray[i]->bounds;
410 for (
int i = 0; i < 2; ++i) {
417 return (child[0] ==
NULL) && (child[1] ==
NULL);
426 handleArray.
append(valueArray);
427 for (
int i = 0; i < 2; ++i) {
428 if (child[i] !=
NULL) {
439 format(
"lo = %s, splitBounds.lo = %s",
443 for (
int i = 0; i < valueArray.
length(); ++i) {
444 const AABox& b = valueArray[i]->bounds;
448 for(
int axis = 0; axis < 3; ++axis) {
455 if (child[0] || child[1]) {
465 if (child[0] !=
NULL) {
469 if (child[1] !=
NULL) {
488 for (
int c = 0; c < 2; ++c) {
503 for (
int c = 0; c < 2; ++c) {
517 if (child[0] !=
NULL) {
523 if (child[1] !=
NULL) {
541 bool useSphere)
const {
544 for (
int v = 0; v < boundsArray.
size(); ++v) {
545 const AABox& bounds = boundsArray[v];
567 splitBounds = myBounds;
569 AABox childBounds[2];
570 myBounds.
split(splitAxis, splitLocation, childBounds[0], childBounds[1]);
572 # if defined(G3D_DEBUG) && defined(VERIFY_TREE)
574 for (
int v = 0; v < boundsArray.
size(); ++v) {
575 const AABox& bounds = boundsArray[v];
580 for (
int c = 0; c < 2; ++c) {
591 bool alreadyInsideBounds =
false;
592 bool rayWillHitBounds =
596 bool canHitThisNode = (alreadyInsideBounds ||
597 (rayWillHitBounds && ((location - ray.
origin()).squaredLength() <
square(distance))));
599 return canHitThisNode;
602 template<
typename RayCallback>
605 RayCallback& intersectCallback,
607 bool intersectCallbackIsFast)
const {
615 for (
int v = 0; v < valueArray.
size(); ++v) {
616 bool canHitThisObject =
true;
618 if (! intersectCallbackIsFast) {
621 const AABox& bounds = boundsArray[v];
622 bool alreadyInsideBounds =
false;
623 bool rayWillHitBounds =
627 canHitThisObject = (alreadyInsideBounds ||
628 (rayWillHitBounds && ((location - ray.
origin()).squaredLength() <
square(distance))));
631 if (canHitThisObject) {
634 const T&
value = valueArray[v]->value;
635 intersectCallback(ray, value, distance);
646 int firstChild = NONE;
647 int secondChild = NONE;
679 if ((firstChild != NONE) && child[firstChild]) {
680 child[firstChild]->
intersectRay(ray, intersectCallback, distance, intersectCallbackIsFast);
687 if (distanceToSplittingPlane > distance) {
696 if ((secondChild != NONE) && child[secondChild]) {
697 child[secondChild]->
intersectRay(ray, intersectCallback, distance, intersectCallbackIsFast);
719 if (source.
size() <= valuesPerNode) {
721 node =
new Node(source);
724 for (
int i = 0; i < source.
size(); ++i) {
743 if (numMeanSplits <= 0) {
748 splitLocation = node->
valueArray[0]->center[splitAxis];
752 for (
int i = 0; i < lt.
size(); ++i) {
753 const AABox& bounds = lt[i]->bounds;
754 if ((bounds.
low()[splitAxis] <= splitLocation) && (bounds.
high()[splitAxis] >= splitLocation)) {
762 for (
int i = 0; i < gt.
size(); ++i) {
763 const AABox& bounds = gt[i]->bounds;
764 if ((bounds.
low()[splitAxis] <= splitLocation) && (bounds.
high()[splitAxis] >= splitLocation)) {
773 (source.
size() > 6)) {
785 if (numMeanSplits > 0) {
788 bounds.
high()[splitAxis] * 0.5f +
789 bounds.
low()[splitAxis] * 0.5f;
792 "Internal error: split location must be finite.");
800 # if defined(G3D_DEBUG) && defined(VERIFY_TREE)
804 for (
int i = 0; i < lt.
size(); ++i) {
805 const AABox& bounds = lt[i]->bounds;
809 for (
int i = 0; i < gt.
size(); ++i) {
810 const AABox& bounds = gt[i]->bounds;
814 for (
int i = 0; i < node->
valueArray.size(); ++i) {
829 for (
int i = 0; i < node->
valueArray.size(); ++i) {
836 node->
child[0] =
makeNode(lt, valuesPerNode, numMeanSplits - 1, temp);
840 node->
child[1] =
makeNode(gt, valuesPerNode, numMeanSplits - 1, temp);
857 for (
int i = 0; i < dst->
valueArray.size(); ++i) {
862 for (
int i = 0; i < 2; ++i) {
915 It cur = memberTable.
begin();
916 It
end = memberTable.
end();
918 delete cur->key.handle;
919 cur->key.handle =
NULL;
930 return memberTable.
size();
959 memberTable.
set(m, node);
974 for (
int i = 0; i < valueArray.
size(); ++i) {
979 int j = valueArray.
size() - i - 1;
987 for (
int i = 0; i < valueArray.
size(); ++i) {
1017 "Tried to remove an element from a "
1018 "KDTree that was not present");
1029 for (
int i = list.
length() - 1; i >= 0; --i) {
1039 memberTable[m]->boundsArray.fastRemove(i);
1093 void balance(
int valuesPerNode = 5,
int numMeanSplits = 3) {
1101 for (
int c = 0; c < 2; ++c) {
1106 delete root->
child[c];
1115 root =
makeNode(copy, valuesPerNode, numMeanSplits, temp);
1139 balance(valuesPerNode, numMeanSplits);
1156 if (parentMask == 0) {
1158 for (
int v = node->
valueArray.size() - 1; v >= 0; --v) {
1163 for (
int c = 0; c < 2; ++c) {
1164 if (node->
child[c]) {
1171 for (
int v = node->
boundsArray.size() - 1; v >= 0; --v) {
1172 if (! node->
boundsArray[v].culledBy(plane, dummy, parentMask)) {
1177 uint32 childMask = 0xFFFFFF;
1180 for (
int c = 0; c < 2; ++c) {
1181 if (node->
child[c] &&
1207 for (
int i = 0; i < temp.
size(); ++i) {
1208 members.
append(*temp[i]);
1228 for (
int i = 0; i < frustum.
faceArray.size(); ++i) {
1238 for (
int i = 0; i < temp.
size(); ++i) {
1239 members.
append(*temp[i]);
1281 isEnd(root ==
NULL), box(b),
1282 node(const_cast<
Node*>(root)), nextValueArrayIndex(-1) {
1295 return ! (*
this == other);
1301 }
else if (other.
isEnd) {
1307 if ((box != other.
box) || (node != other.
node) ||
1314 for (
int i = 0; i < stack.
length(); ++i) {
1315 if (stack[i] != other.
stack[i]) {
1331 bool foundIntersection =
false;
1332 while (! isEnd && ! foundIntersection) {
1335 while ((! isEnd) && (nextValueArrayIndex >= node->
valueArray.length())) {
1355 if (stack.
length() > 0) {
1359 nextValueArrayIndex = 0;
1367 while (! isEnd && ! foundIntersection && (nextValueArrayIndex < node->valueArray.length())) {
1369 foundIntersection =
true;
1398 alwaysAssertM(! isEnd,
"Can't dereference the end element of an iterator");
1405 alwaysAssertM(! isEnd,
"Can't dereference the end element of an iterator");
1411 operator T*()
const {
1412 alwaysAssertM(! isEnd,
"Can't dereference the end element of an iterator");
1444 for (
int i = 0; i < temp.
size(); ++i) {
1445 members.
append(*temp[i]);
1508 template<
typename RayCallback>
1511 RayCallback& intersectCallback,
1513 bool intersectCallbackIsFast =
false)
const {
1515 root->
intersectRay(ray, intersectCallback, distance, intersectCallbackIsFast);
1538 for (
int i = 0; i < temp.
size(); ++i) {
1539 members.
append(*temp[i]);
1564 for (
int i = 0; i < temp.
size(); ++i) {
1565 members.
append(*(temp.handle));
1577 if (member ==
NULL) {
1581 return &(member->
handle->value);
1604 return !(*
this == other);
1608 return it == other.
it;
1631 return it->key.handle->value;
1635 return &(it->key.handle->value);
1638 operator T*()
const {
1639 return &(it->key.handle->value);
Iterator & operator++()
Definition: KDTree.h:1614
Array< Handle * > valueArray
Definition: KDTree.h:361
bool operator==(const Handle &other) const
Definition: KDTree.h:224
bool intersects(const AABox &other) const
Definition: AABox.cpp:175
void deserialize(class BinaryInput &b)
Definition: AABox.cpp:99
void getIntersectingMembers(const AABox &box, Array< T * > &members) const
Definition: KDTree.h:1434
void getBounds(class AABox &) const
Definition: Box.cpp:463
size_t hashCode() const
Definition: KDTree.h:228
bool operator==(const Type &m) const
Definition: KDTree.h:113
static void getBounds(const G3D::AABox &v, G3D::AABox &out)
Definition: KDTree.h:51
int nextValueArrayIndex
Definition: KDTree.h:1276
void getIntersectingMembers(const Frustum &frustum, Array< T > &members) const
Definition: KDTree.h:1235
void push(const T &value)
Definition: Array.h:770
~KDTree()
Definition: KDTree.h:904
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
T const * operator->() const
Definition: KDTree.h:1404
static void getBounds(const G3D::Sphere *&s, G3D::AABox &out)
Definition: KDTree.h:79
Node * child[2]
Definition: KDTree.h:351
Indirector()
Definition: KDTree.h:106
static void getBounds(const G3D::Vector3 &v, G3D::AABox &out)
Definition: KDTree.h:43
static void getBounds(const G3D::Triangle *&t, G3D::AABox &out)
Definition: KDTree.h:88
Handle()
Definition: KDTree.h:216
int operator()(Handle *ignore, const Handle *handle) const
Definition: KDTree.h:311
Vector3::Axis sortAxis
Definition: KDTree.h:284
AABox intersect(const AABox &other) const
Definition: AABox.h:282
Node * findDeepestContainingNode(const AABox &bounds)
Definition: KDTree.h:511
void setContents(const Array< T > &array, int valuesPerNode=5, int numMeanSplits=3)
Definition: KDTree.h:1136
void getIntersectingMembers(const Frustum &frustum, Array< T * > &members) const
Definition: KDTree.h:1225
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
size_t hashCode() const
Definition: Vector2unorm16.h:76
static void getBounds(const G3D::Vector4 *&v, G3D::AABox &out)
Definition: KDTree.h:71
Definition: Vector3.h:122
bool contains(const T &value)
Definition: KDTree.h:998
KDTree(const KDTree &src)
Definition: KDTree.h:891
Comparator(Vector3::Axis a, float l)
Definition: KDTree.h:309
bool contains(const AABox &other) const
Definition: AABox.h:238
CenterComparator(Vector3::Axis a)
Definition: KDTree.h:264
void getMembers(Array< T > &members) const
Definition: KDTree.h:1561
void balance(int valuesPerNode=5, int numMeanSplits=3)
Definition: KDTree.h:1093
Node * makeNode(Array< Handle * > &source, int valuesPerNode, int numMeanSplits, Array< Handle * > &temp)
Definition: KDTree.h:711
bool operator==(const Iterator &other) const
Definition: KDTree.h:1607
bool operator==(const Indirector &m) const
Definition: KDTree.h:109
void clear()
Definition: Table.h:578
size_t size() const
Definition: Table.h:589
static void getBounds(const G3D::Vector3 *&v, G3D::AABox &out)
Definition: KDTree.h:67
Definition: HashTrait.h:105
Definition: KDTree.h:1253
Iterator begin() const
Definition: KDTree.h:1650
Dynamic 1D array tuned for performance.
Definition: Array.h:95
Node * root
Definition: KDTree.h:882
Axis primaryAxis() const
Definition: Vector3.cpp:129
Iterator begin() const
Definition: Table.h:562
BoxIntersectionIterator endBoxIntersection() const
Definition: KDTree.h:1425
const T & last() const
Definition: Array.h:923
void getIntersectingMembers(const AABox &box, const Sphere &sphere, Array< T * > &members, bool useSphere) const
Definition: KDTree.h:537
void writeUInt8(uint8 i)
Definition: BinaryOutput.h:283
arena_t NULL
Definition: jemalloc_internal.h:624
void assignSplitBounds(const AABox &myBounds)
Definition: KDTree.h:566
void deserialize(std::string &s, BinaryInput &b)
Definition: serialize.h:16
static void getIntersectingMembers(const Array< Plane > &plane, Array< T * > &members, Node *node, uint32 parentMask)
Definition: KDTree.h:1148
Type * handle
Definition: KDTree.h:102
int length() const
Definition: Array.h:438
static AABox computeBounds(const Array< Handle * > &point, int beginIndex, int endIndex)
Definition: KDTree.h:234
void set(const Key &key, const Value &value)
Definition: Table.h:599
const Point3 & low() const
Definition: AABox.h:136
Array< AABox > boundsArray
Definition: KDTree.h:369
void getBounds(class AABox &out) const
Definition: Sphere.cpp:252
void getIntersectingMembers(const Array< Plane > &plane, Array< T > &members) const
Definition: KDTree.h:1204
Axis
Definition: Vector3.h:122
void verifyNode(const Vector3 &lo, const Vector3 &hi)
Definition: KDTree.h:434
bool operator!=(const Iterator &other) const
Definition: KDTree.h:1603
void getHandles(Array< Handle * > &handleArray) const
Definition: KDTree.h:425
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
KDTree()
Definition: KDTree.h:888
bool isLeaf() const
Definition: KDTree.h:416
~Node()
Definition: KDTree.h:409
An arbitrary (oriented) 3D box, useful as a bounding box.
Definition: Box.h:35
double distance(double x, double y)
Definition: g3dmath.h:731
static size_t hashCode(const G3D::_internal::Indirector< Handle > &key)
Definition: KDTree.h:125
Vector3 xyz() const
Definition: Vector4.cpp:233
T max(const T &x, const T &y)
Definition: g3dmath.h:320
static void getBounds(const G3D::Vector2 &v, G3D::AABox &out)
Definition: KDTree.h:39
bool operator!=(const BoxIntersectionIterator &other) const
Definition: KDTree.h:1294
BoundsComparator(Vector3::Axis a)
Definition: KDTree.h:286
T * operator->() const
Definition: KDTree.h:1634
Vector3::Axis splitAxis
Definition: KDTree.h:337
#define true
Definition: CascPort.h:17
BoxIntersectionIterator beginBoxIntersection(const AABox &box) const
Definition: KDTree.h:1421
bool intersects(const Ray &ray, float distance) const
Definition: KDTree.h:588
Indirector(Type *h)
Definition: KDTree.h:104
void medianPartition(Array< T > <Median, Array< T > &eqMedian, Array< T > >Median, Array< T > &tempArray, const Comparator &comparator) const
Definition: Array.h:1247
void getBounds(class AABox &) const
Definition: Triangle.cpp:124
static const Vector3 & zero()
Definition: Vector3.cpp:119
Vector3::Axis sortAxis
Definition: KDTree.h:262
T min(const T &x, const T &y)
Definition: g3dmath.h:305
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
const T * getPointer(const T &value) const
Definition: KDTree.h:1573
BoxIntersectionIterator()
Definition: KDTree.h:1278
static void getBounds(const G3D::Vector4 &v, G3D::AABox &out)
Definition: KDTree.h:47
static void getBounds(const G3D::AABox *&v, G3D::AABox &out)
Definition: KDTree.h:75
void intersectRay(const Ray &ray, RayCallback &intersectCallback, float &distance, bool intersectCallbackIsFast) const
Definition: KDTree.h:603
Definition: KDTree.h:1591
float splitLocation
Definition: KDTree.h:340
Handle(const T &v)
Definition: KDTree.h:218
friend class TreeType
Definition: KDTree.h:1255
bool culledBy(const Array< Plane > &plane, int32 &cullingPlaneIndex, const uint32 testMask, uint32 &childMask) const
void serialize(const std::string &s, BinaryOutput &b)
Definition: serialize.h:12
Vector3::Axis sortAxis
Definition: KDTree.h:306
BoxIntersectionIterator & operator++()
Definition: KDTree.h:1328
#define debugAssert(exp)
Definition: debugAssert.h:160
static void getBounds(const G3D::Box &b, G3D::AABox &out)
Definition: KDTree.h:59
SmallArray< Face, 6 > faceArray
Definition: Frustum.h:49
static bool collisionLocationForMovingPointFixedAABox(const Vector3 &point, const Vector3 &velocity, const class AABox &box, Vector3 &outLocation, bool &inside=ignoreBool, Vector3 &normal=ignore)
Definition: CollisionDetection.cpp:1226
void clear()
Definition: KDTree.h:911
Node * node
Definition: KDTree.h:1265
int size() const
Definition: KDTree.h:929
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
void getIntersectingMembers(const Sphere &sphere, Array< T * > &members) const
Finds all members whose bounding boxes intersect the sphere. The actual elements may not intersect th...
Definition: KDTree.h:1525
bool operator==(const BoxIntersectionIterator &other) const
Definition: KDTree.h:1298
T value
Definition: KDTree.h:214
double square(double fValue)
Definition: g3dmath.h:698
Point3 center() const
Definition: AABox.h:163
void clear(bool shrink=true)
Definition: Array.h:407
void serialize(class BinaryOutput &b) const
Definition: AABox.cpp:93
AABox bounds
Definition: KDTree.h:206
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
Table< Member, Node * > MemberTable
Definition: KDTree.h:877
friend class TreeType
Definition: KDTree.h:1593
MemberTable memberTable
Definition: KDTree.h:880
int size() const
Definition: Array.h:430
Node(const Node &other)
Definition: KDTree.h:384
void insert(const Array< T > &valueArray)
Definition: KDTree.h:966
Iterator end() const
Definition: KDTree.h:1659
float sortLocation
Definition: KDTree.h:307
Definition: BoundsTrait.h:17
size_t hashCode() const
Definition: KDTree.h:117
void intersectRay(const Ray &ray, RayCallback &intersectCallback, float &distance, bool intersectCallbackIsFast=false) const
Definition: KDTree.h:1509
const Iterator end() const
Definition: Table.h:570
BoxIntersectionIterator(const AABox &b, const Node *root)
Definition: KDTree.h:1280
Array< Node * > stack
Definition: KDTree.h:1272
static const AABox & large()
Definition: AABox.cpp:74
KDTree & operator=(const KDTree &src)
Definition: KDTree.h:896
void serializeStructure(BinaryOutput &bo) const
Definition: KDTree.h:1548
const Point3 & origin() const
Definition: Ray.h:56
Array< Key > getKeys() const
Definition: Table.h:907
const Key * getKeyPointer(const Key &key) const
Definition: Table.h:701
int operator()(Handle *A, const Handle *B) const
Definition: KDTree.h:266
Definition: BinaryOutput.h:52
static Node * deserializeStructure(BinaryInput &bi)
Definition: KDTree.h:495
int operator()(Handle *A, const Handle *B) const
Definition: KDTree.h:288
Node()
Definition: KDTree.h:372
Iterator(const typename Table< Member, Node * >::Iterator &it)
Definition: KDTree.h:1599
AABox splitBounds
Definition: KDTree.h:335
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
bool isEnd
Definition: KDTree.h:1258
void split(const Vector3::Axis &axis, float location, AABox &low, AABox &high) const
Definition: AABox.cpp:112
static void getBounds(const G3D::Vector2 *&v, G3D::AABox &out)
Definition: KDTree.h:63
const FieldDescriptor value
Definition: descriptor.h:1522
uint32_t uint32
Definition: g3dmath.h:168
void getIntersectingMembers(const Array< Plane > &plane, Array< T * > &members) const
Definition: KDTree.h:1196
void getIntersectingMembers(const Sphere &sphere, Array< T > &members) const
Definition: KDTree.h:1535
const Point3 & high() const
Definition: AABox.h:141
Definition: EqualsTrait.h:19
static const Vector3 & inf()
Definition: Vector3.cpp:124
void update(const T &value)
Definition: KDTree.h:1064
void append(const T &value)
Definition: Array.h:583
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
Node * cloneTree(Node *src)
Definition: KDTree.h:853
const T & operator*() const
Definition: KDTree.h:1397
Type
Type of JSON value.
Definition: rapidjson.h:642
Node(const Array< Handle * > &pt)
Definition: KDTree.h:395
Definition: Triangle.h:34
const Vector3 & direction() const
Definition: Ray.h:61
AABox box
Definition: KDTree.h:1261
Vector3 center
Definition: KDTree.h:212
static void serializeStructure(const Node *n, BinaryOutput &bo)
Definition: KDTree.h:480
static void getBounds(const G3D::Box *&b, G3D::AABox &out)
Definition: KDTree.h:83
bool containsKey(const Key &key) const
Definition: Table.h:874
static void getBounds(const G3D::Sphere &s, G3D::AABox &out)
Definition: KDTree.h:55
bool isFinite(double x)
Definition: g3dmath.h:525
std::string toString() const
Definition: Vector3.cpp:386
const T & operator*() const
Definition: KDTree.h:1630
void insert(const T &value)
Definition: KDTree.h:938
_internal::Indirector< Handle > Member
Definition: KDTree.h:875
void deserializeStructure(BinaryInput &bi)
Definition: KDTree.h:1553
Table< Member, Node * >::Iterator it
Definition: KDTree.h:1597
void getIntersectingMembers(const AABox &box, Array< T > &members) const
Definition: KDTree.h:1441