33#include "TreeIterator.h>
36#include "Base\Headers\BaseValues.h>
37#include "Base\Headers\Buffer.h>
38#include "Base\Headers\Bounds.h>
39#include "Base\Headers\Median.h>
40#include "Base\Headers\ProgressInfo.h>
41#include "Base\Headers\BoolBuffer.h>
42#include "Base\Headers\Vector.h>
43#include "Base\Headers\VectorFunctions.h>
44#include "Base\Headers\intersection.h>
45#include "Base\Headers\BinaryHeap.h>
50 template<u
int01 t_dims,
class t_type>
101 template<u
int01 t_dims,
class t_type>
119 template<
class t_node_type>
127 while (iterator.
valid())
136 if (node.
size() != values.numSet())
143 template<
class t_node_type,
class t_buffer_type>
148 for (
uint01 dim = 0; dim < t_dims; ++dim)
159 const uint01 split_dim = d_size.getDimension<
MAX>();
161 location[split_dim] =
median(elements,
indices, start, end, split_dim);
166 m_nodes[node_index].setSplit(split_dim, elements[
indices[new_left]][split_dim]);
172 template<
class t_node_type,
class t_buffer_type>
178 start_stack[0] = top_start;
179 end_stack[0] = top_end;
185 while (iterator.
valid())
188 uint04 start = start_stack[level];
189 uint04 end = end_stack[level];
194 m_nodes[node_index].setSize(end - start);
201 start_stack[iterator.
depth()] = new_left;
202 end_stack[iterator.
depth()] = end;
203 node_bounds[iterator.
depth()] = bounds;
207 start_stack[iterator.
depth()] = start;
208 end_stack[iterator.
depth()] = new_left;
209 node_bounds[iterator.
depth()] = bounds;
215 size += (end - start);
217 if (progress !=
nullptr && node_index % update_size == 0)
227 if (progress !=
nullptr)
229 if (progress->setProgress(1.0f))
239 template<
class t_node_type,
class t_buffer_type>
243 t_type split_values[256];
244 while (iterator.
valid())
247 if (split_values[iterator.
depth()] >= min_distance)
266 min_index = check_index;
281 split_values[iterator.
depth()] = 0;
289 split_values[iterator.
depth()] = 0;
296 template<
class t_node_type,
class t_buffer_type>
300 t_type split_values[255];
301 while (iterator.
valid())
304 if (split_values[iterator.
depth()] >= min_distance)
325 min_distance = heap.max();
329 if (min_distance <= epsilon)
342 split_values[iterator.
depth()] = 0;
351 split_values[iterator.
depth()] = 0;
357 template<
class t_node_type,
class t_buffer_type>
361 t_type split_values[256];
365 for (
uint01 i = 0; i < t_dims; i++)
367 dim_min[i] = top_line[0][i] < top_line[1][i] ?
MIN :
MAX;
368 dim_max[i] = dim_min[i] !=
MIN ?
MIN :
MAX;
372 t_type sqrt_min_dist =
sqrt(min_index);
373 CachedLineSegment<t_dims, t_type> cached_line(top_line);
375 while (iterator.
valid())
378 if (
abs(split_values[iterator.
depth()]) >= min_distance)
398 min_index = check_index;
399 sqrt_min_dist =
sqrt(min_index);
408 const uint01 min = dim_min[split_dim];
409 const uint01 max = dim_max[split_dim];
410 if (cached_line[max][split_dim] >=
m_nodes[index].split())
412 if (cached_line[min][split_dim] <=
m_nodes[index].split())
415 split_values[iterator.
depth()] = 0;
418 split_values[iterator.
depth()] = 0;
423 split_values[iterator.
depth()] = cached_line[min][split_dim] -
m_nodes[index].split();
424 split_values[iterator.
depth()] *= split_values[iterator.
depth()];
427 split_values[iterator.
depth()] = 0;
430 else if (cached_line[min][split_dim] <=
m_nodes[index].split())
433 split_values[iterator.
depth()] =
m_nodes[index].split() - cached_line[max][split_dim];
434 split_values[iterator.
depth()] *= -split_values[iterator.
depth()];
437 split_values[iterator.
depth()] = 0;
444 template<
class t_node_type,
class t_buffer_type>
449 CachedTriangle<t_dims, t_type> cached_tri(triangle);
451 while (iterator.
valid())
456 if (!node.bounds().expand(
sqrt(min_distance)).doBoundsIntersect<t_dims>(cached_tri))
468 min_index = check_index;
476 if (
m_nodes[node.
left()].bounds().contains(cached_tri.centroid()))
485 template<
class t_node_type,
class t_area_type,
class t_buffer_type>
491 while (iterator.
valid())
526 bounds[iterator.
depth()] = left;
529 bounds[iterator.
depth()] = right;
536 template<
class t_area_type,
class t_node_type,
class t_buffer_type>
541 while (iterator.
valid())
546 switch (
classify(area, node.bounds()))
576 template<
class t_node_type,
class t_buffer_type>
579 bool recalculate_bounds =
false;
581 while (iterator.
valid())
583 const uint04 node_index = iterator.
get();
593 if (index != check_index)
599 if (node.
size() != 0)
602 recalculate_bounds =
true;
603 node.setBounds(bounds);
610 bool needs_rebalance =
false;
614 needs_rebalance =
true;
621 needs_rebalance =
true;
633 recalculate_bounds =
true;
642 if (recalculate_bounds)
644 while (iterator.
valid())
654 template<
class t_node_type,
class t_buffer_type>
658 bool recalculate_bounds =
false;
659 while (iterator.
valid())
661 const uint04 node_index = iterator.
get();
673 if (index != check_index)
679 if (node.
size() != 0)
682 node.setBounds(bounds);
689 bool needs_rebalance =
false;
693 needs_rebalance =
true;
700 needs_rebalance =
true;
712 recalculate_bounds =
true;
721 if (recalculate_bounds)
723 while (iterator.
valid())
734 const uint04 total_size = left_size + right_size;
735 return (left_size < total_size / 4 || right_size < total_size / 4);
740 return (change_size >
size() / 4);
743 template<
class t_node_type,
class t_buffer_type>
758 template<
class t_node_type,
class t_buffer_type>
822 template<
class t_node_type,
class t_buffer_type>
841 node.setBounds(bounds);
852 while (iterator.
valid())
869 template<u
int01 t_dims,
class t_type>
870 class KDTree :
public Tree<t_dims, t_type, KDTreeBase<t_dims, t_type>>
880 :
Tree(std::move(tree))
898 std::swap(
m_nodes, value.m_nodes);
Definition BinaryHeap.hpp:39
void insert(t_comp_type comparison, t_value_type value)
Definition BinaryHeap.hpp:44
uint04 size() const
Definition BinaryHeap.hpp:71
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:57
constexpr void addToBounds(const t_vertex &vector)
Adds to the bounds such that the new bounds fully encompasses the argument.
Definition Bounds.hpp:498
constexpr t_vertex center() const
Returns the center of the bounds.
Definition Bounds.hpp:156
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
void setAll(const t_o_type *src, t_index_type offset, t_index_type size)
Definition Buffer.hpp:1313
constexpr t_index_type size() const
Definition Buffer.hpp:1461
void addAll(const Buffer< t_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > &buffer)
Definition Buffer.hpp:248
void setSize(t_index_type new_size)
Definition Buffer.hpp:1413
void swapIndices(t_index_type index_1, t_index_type index_2)
Definition Buffer.hpp:1550
void ensureCapacity(t_index_type new_capacity, bool ensure_not_greater=false, bool ensure_not_less=true)
Definition Buffer.hpp:803
decltype(auto) begin()
Definition Buffer.hpp:504
bool removeElement(const t_type &element)
Definition Buffer.hpp:1076
void setAllToValue(const t_o_type &fill_element, const t_index_type offset=0, t_index_type fill_size=Constant< t_index_type >::NaN)
Definition Buffer.hpp:1378
t_type m_split
Definition KDTree.hpp:95
uint04 right() const
Definition KDTree.hpp:76
uint01 m_split_dim
Definition KDTree.hpp:96
const uint01 & splitDim() const
Definition KDTree.hpp:85
KDNode(uint04 index_start)
Definition KDTree.hpp:59
uint04 left() const
Definition KDTree.hpp:72
void clear(uint04 index_start, const Bounds< t_dims, t_type > &bounds)
Definition KDTree.hpp:65
void setSplit(uint01 split_dim, t_type split)
Definition KDTree.hpp:89
KDNode(const KDNode &node)
Definition KDTree.hpp:54
const t_type & split() const
Definition KDTree.hpp:81
Definition KDTree.hpp:103
bool _balanceLeafNode(uint04 top_index, const t_buffer_type &elements, Buffer< uint04 > &indices, uint04 top_start, uint04 top_end, bool is_precise, ProgressInfo *progress=nullptr)
Definition KDTree.hpp:173
void _makeWriteable()
Definition KDTree.hpp:845
bool validate(const Buffer< t_node_type > &elements)
Definition KDTree.hpp:120
uint04 _getNumberOfEnclosedElements(uint04 top_node, const t_area_type &area, const t_buffer_type &elements) const
Definition KDTree.hpp:537
void _getClosestElements(uint04 top_index, const Vector< t_dims, t_type > &point, const t_buffer_type &elements, t_type &min_distance, const t_type &epsilon, MinHeap< t_type, uint04 > &heap, uint04 size) const
Definition KDTree.hpp:297
void _addValue(uint04 node_index, uint04 index, const t_buffer_type &elements)
Definition KDTree.hpp:759
void _getEnclosedElements(uint04 top_node, t_area_type area, Buffer< bool > &indices, const t_buffer_type &elements) const
Definition KDTree.hpp:486
bool needsRebalance(uint04 left_size, uint04 right_size)
Definition KDTree.hpp:732
KDTreeBase(uint04 bucket_size)
Definition KDTree.hpp:105
bool useBulkResize(uint04 change_size)
Definition KDTree.hpp:738
bool _getClosestElement(uint04 top_index, const Triangle< t_dims, t_type > &triangle, const t_buffer_type &elements, t_type &min_distance, t_type epsilon, uint04 &min_index) const
Definition KDTree.hpp:445
void _moveValue(uint04 top_node, uint04 index, const t_node_type &new_location, const t_buffer_type &elements)
Definition KDTree.hpp:655
void _addValues(uint04 top_node, Buffer< uint04 > indices, const t_buffer_type &elements, bool is_precise, ProgressInfo *progress=nullptr)
Definition KDTree.hpp:744
bool _getClosestElement(uint04 top_index, const Vector< t_dims, t_type > &point, const t_buffer_type &elements, t_type &min_distance, const t_type epsilon, uint04 &min_index) const
Definition KDTree.hpp:240
void _removeValue(uint04 top_node, uint04 index, const t_buffer_type &elements)
Definition KDTree.hpp:577
bool recalculateBounds(uint04 node_index, const t_buffer_type &elements)
Definition KDTree.hpp:823
uint04 _calculateAndSplit(uint04 node_index, Bounds< t_dims, t_type > node_bounds, const t_buffer_type &elements, Buffer< uint04 > &indices, uint04 start, uint04 end, bool is_precise)
Definition KDTree.hpp:144
bool _getClosestElement(uint04 top_index, const LineSegment< t_dims, t_type > &top_line, const t_buffer_type &elements, t_type &min_distance, t_type epsilon, uint04 &min_index) const
Definition KDTree.hpp:358
Definition KDTree.hpp:871
virtual const char * getTreeType() const override
Definition KDTree.hpp:904
KDTree & operator=(KDTree &&value)
Definition KDTree.hpp:895
KDTree & operator=(const KDTree &value)
Definition KDTree.hpp:887
KDTree(uint04 bucket_size)
Definition KDTree.hpp:873
A line segment represented by two vertices, a start and end.
Definition Line.hpp:55
Definition ProgressInfo.hpp:43
uint04 getNumberOfFreeIndices()
Definition Tree.hpp:343
uint04 m_bucket_size
Definition Tree.hpp:484
Buffer< KDNode< t_dims, t_type >, uint04, ObjectAllocator< true > > m_nodes
Definition Tree.hpp:480
void reclaimChildren(uint04 index)
Definition Tree.hpp:397
const Buffer< uint04 > & indices() const
Definition Tree.hpp:287
Buffer< uint04 > m_available_node_positions
Definition Tree.hpp:481
void _getAllT(const uint04 &node_index, Buffer< bool > &indices) const
Definition Tree.hpp:423
void _getAll(const uint04 &node_index, Buffer< uint04 > &indices) const
Definition Tree.hpp:445
uint04 getNumberOfNodes() const
Definition Tree.hpp:104
Bounds< t_dims, t_type > _getBounds(const Buffer< uint04 > &indices, const t_buffer_type &elements, uint04 start, uint04 end)
Definition Tree.hpp:257
uint04 size() const
Definition Tree.hpp:229
Buffer< uint04 > m_indices
Definition Tree.hpp:479
static const uint04 root_node
Definition Tree.hpp:483
TreeBase(uint04 bucket_size)
Definition Tree.hpp:57
void splitLeafNode(uint04 index)
Definition Tree.hpp:353
void clear()
Definition Tree.hpp:94
Buffer< uint04 > m_available_indexed_positions
Definition Tree.hpp:482
Tree(uint04 bucket_size)
Definition Tree.hpp:491
Definition TreeIterator.hpp:38
void pop()
Definition TreeIterator.hpp:53
bool valid() const
Definition TreeIterator.hpp:49
uint01 depth()
Definition TreeIterator.hpp:57
void addAndGoToIndex(uint04 index)
Definition TreeIterator.hpp:61
uint04 get() const
Definition TreeIterator.hpp:45
uint04 begin() const
Definition Node.hpp:54
bool isLeaf() const
Definition Node.hpp:87
void setSize(uint04 bucket_size)
Definition Node.hpp:82
uint04 end() const
Definition Node.hpp:59
uint04 size() const
Definition Node.hpp:78
uint04 m_element_size
Definition Node.hpp:92
void setBegin(uint04 index_start)
Definition Node.hpp:50
sint04 m_child_start
Definition Node.hpp:93
Definition Triangle.hpp:143
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator.
Definition BaseFunctions.hpp:116
@ MIN
Definition BaseValues.hpp:226
@ MAX
Definition BaseValues.hpp:227
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
uint04 apxMedian(const t_buffer_type &elements, const Buffer< uint04 > &indices, uint04 start, uint04 end, uint01 dimension)
Definition Median.hpp:298
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:42
@ inside
Definition BaseValues.hpp:243
@ mixed
Definition BaseValues.hpp:244
@ outside
Definition BaseValues.hpp:242
IntersectionTypes classify(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition Intersection.hpp:338
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1309
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr t_type distance(const t_vertex &vertex, const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Distance.hpp:250
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Definition AngleFunctions.h:750
uint04 sortAboutValue(uint04 value_index, const Buffer< t_node_type > &points, Buffer< uint04 > &indices, uint04 start, uint04 end, uint01 dimension)
Definition Median.hpp:337
uint04 median(const t_container_type &elements, Buffer< uint04 > &indices, const uint04 start, const uint04 end, uint01 dimension)
Definition Median.hpp:39
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator.
Definition BaseFunctions.hpp:67
Definition BaseValues.hpp:272