33#include <NDEVR/TreeIterator.h>
34#include <NDEVR/Node.h>
35#include <NDEVR/Tree.h>
36#include <NDEVR/TreeSorter.h>
37#include <NDEVR/BaseValues.h>
38#include <NDEVR/Buffer.h>
39#include <NDEVR/Bounds.h>
40#include <NDEVR/Median.h>
41#include <NDEVR/OpenMP.h>
42#include <NDEVR/ProgressInfo.h>
43#include <NDEVR/Vector.h>
44#include <NDEVR/VectorFunctions.h>
45#include <NDEVR/Intersection.h>
46#include <NDEVR/Distance.h>
47#include <NDEVR/BinaryHeap.h>
48#include <NDEVR/BinaryFile.h>
52 template<u
int01 t_dims,
class t_type>
105 template<u
int01 t_dims,
class t_type>
127 template<
class t_buffer_type>
133 values.
setSize(elements.size());
134 if (elements.size() == 0)
137 while (iterator.
valid())
144 const uint04 node_index = iterator.
get();
157 if (node.
size() != 0 && !node.
bounds().validate())
168 const auto& element = elements[
m_indices[idx]];
204 while (iterator.
valid())
206 const uint04 node_index = iterator.
get();
218 template<
class t_buffer_type>
223 for (
uint01 dim = 0; dim < t_dims; ++dim)
225 if (
m_nodes[node_index].bounds().span()[dim] != 0)
228 d_size[dim] = potential_bounds[0].surfaceArea() + potential_bounds[1].surfaceArea();
232 const uint01 split_dim = d_size.template dimensionalIndex<MIN>();
235 const uint04 median_location = ((end - start) / 2) + start;
238 auto left = sorter.
getBounds(start, median_location, elements);
239 auto right = sorter.
getBounds(median_location, end, elements);
251 return median_location;
253 template<
bool t_has_nans,
bool t_uses_boundary,
class t_buffer_type>
257 if constexpr (t_uses_boundary)
259 sorter.template createBoundarySortList<t_has_nans>(
indices, elements, progress);
263 sorter.template createCenterSortList<t_has_nans>(
indices, elements, progress);
265 m_nodes[top_index].setBounds(sorter.
getBounds(0, (top_end - top_start), elements));
268 start_stack[0] = top_start;
269 end_stack[0] = top_end;
271 while (iterator.
valid())
274 uint04 start = start_stack[level];
275 uint04 end = end_stack[level];
278 m_nodes[node_index].setSize(end - start);
285 start_stack[iterator.
depth()] = new_left;
286 end_stack[iterator.
depth()] = end;
289 start_stack[iterator.
depth()] = start;
290 end_stack[iterator.
depth()] = new_left;
299 template<
class t_location_type>
303 if(left_distance <= t_type(0))
306 if (right_distance <= t_type(0))
308 if (left_distance < right_distance)
310 if (left_distance >= min_distance_squared)
312 if (node_heap.
size() == 0 || node_heap.
extremeComp() >= min_distance_squared)
316 else if(node_heap.
size() > 0 && node_heap.
extremeComp() < left_distance)
320 if (right_distance < min_distance_squared)
326 if (right_distance < min_distance_squared)
332 if (right_distance >= min_distance_squared)
334 if (node_heap.
size() == 0 || node_heap.
extremeComp() >= min_distance_squared)
338 else if (node_heap.
size() > 0 && node_heap.
extremeComp() < right_distance)
342 if (left_distance < min_distance_squared)
347 index = node.
right();
348 if (left_distance < min_distance_squared)
354 template<
bool t_presorted,
class t_location_type,
class t_buffer_type>
355 void _getClosestElement(
uint04 top_index,
const t_location_type& selector,
const t_buffer_type& elements, t_type& min_distance_squared,
const t_type& epsilon,
uint04& min_index)
const
358 lib_assert(min_distance_squared >= 0.0f,
"Cannot search for 0.0 distance. Use epsilon");
359 if (root_distance >= min_distance_squared)
370 for(
uint04 i = start_index; i < end; ++i)
373 if constexpr(t_presorted)
378 if (
distance < min_distance_squared)
381 min_index = check_index;
382 if (min_distance_squared <= epsilon)
386 if (node_heap.
size() == 0 || node_heap.
extremeComp() >= min_distance_squared)
392 if (!
predictBranch(index, node, selector, node_heap, min_distance_squared))
398 template<
bool t_presorted,
class t_buffer_type,
class t_reference_type>
403 if (root_distance >= min_distance_squared)
415 if constexpr (t_presorted)
420 if (
distance < min_distance_squared)
427 if (min_distance_squared <= epsilon)
432 if (node_heap.
size() == 0 || node_heap.
extremeComp() >= min_distance_squared)
438 if (!
predictBranch(index, node, selector, node_heap, min_distance_squared))
443 template<
class t_area_type,
class t_buffer_type>
447 while (iterator.
valid())
480 template<
bool t_is_presorted,
class t_area_type,
class t_buffer_type>
484 while (iterator.
valid())
501 if constexpr (t_is_presorted)
522 template<
class t_area_type,
class t_buffer_type>
527 while (iterator.
valid())
543 for (
int i = node.
begin(); i < end; i++)
561 template<
class t_area_type,
class t_buffer_type>
565 while (iterator.
valid())
571 bounds.
expand(max_distance);
575 #pragma clang diagnostic push
576 #pragma clang diagnostic ignored "-Wunused-value"
582 #pragma clang diagnostic pop
592 if (distanceSqaured(area, elements[
m_indices[i]]) < max_distance)
606 template<
class t_area_type,
class t_buffer_type>
610 while (iterator.
valid())
612 bool is_mixed =
false;
677 template<
class t_buffer_type>
680 bool recalculate_bounds =
false;
682 while (iterator.
valid())
684 const uint04 node_index = iterator.
get();
694 if (index != check_index)
695 bounds.
addToBounds(elements[check_index].
template as<t_dims, t_type>());
700 if (node.
size() != 0)
703 recalculate_bounds =
true;
711 bool needs_rebalance =
false;
715 needs_rebalance =
true;
722 needs_rebalance =
true;
734 recalculate_bounds =
true;
743 if (recalculate_bounds)
745 while (iterator.
valid())
755 template<
class t_node_type,
class t_buffer_type>
756 void _moveValue(
uint04 top_node,
uint04 index,
const t_node_type& old_location,
const t_buffer_type& elements,
bool rebalance)
759 bool recalculate_bounds =
false;
760 while (iterator.
valid())
762 const uint04 node_index = iterator.
get();
776 if (index != check_index)
785 recalculate_bounds =
true;
790 lib_assert(node.
bounds() == bounds,
"Unexpected Node Resize behavior: Removing object should not increase bounds");
791 recalculate_bounds =
false;
797 if (node.
size() != 0)
816 bool added_in =
false;
817 while (iterator.
valid() && (!added_in || recalculate_bounds))
819 const uint04 node_index = iterator.
get();
820 if (recalculate_bounds)
843 const uint04 total_size = left_size + right_size;
844 return (left_size < total_size / 3 || right_size < total_size / 3);
851 template<
bool t_has_nans,
bool t_uses_boundary,
class t_buffer_type>
854 if (progress) progress->addMessage(
"RTree: Getting all");
856 if (progress) progress->addMessage(
"RTree: reclaiming children");
867 max_index_size = 2 * max_index_size;
868 max_node_size =
cast<uint04>(pow(2, level++)) + max_node_size;
871 m_nodes.ensureCapacity(max_node_size);
872 if (progress) progress->addMessage(
"RTree: Balancing Node");
874 if (progress) progress->setProgress(1.0f);
875 if (progress) progress->addMessage(
"RTree: Finished Adding values");
879 template<
bool t_has_nans,
bool t_uses_boundary,
class t_buffer_type>
883 const auto value = elements[index].template as<t_dims, t_type>();
913 bool needs_rebalance =
false;
914 t_type surface_diff =
917 if(surface_diff == 0)
921 node_index = node.
left();
926 node_index = node.
right();
930 else if (surface_diff < 0)
935 needs_rebalance =
true;
937 node_index = node.
left();
945 needs_rebalance =
true;
947 node_index = node.
right();
967 template<
class t_buffer_type>
995 while (iterator.
valid())
1012 template<u
int01 t_dims,
class t_type>
1013 class RTree :
public Tree<t_dims, t_type, RTreeBase<t_dims, t_type>>
1081 bool placeholder =
false;
1082 file.
write(placeholder);
1090 switch (internal_index)
1094 , compressed_data[compression_index]
1099 , compressed_data[compression_index]
1104 , compressed_data[compression_index]
1109 , compressed_data[compression_index]
1118 bool placeholder =
false;
1119 file.
write(placeholder);
#define UNUSED(expr)
Definition BaseValues.hpp:406
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:68
Logic for reading or writing to a binary file including logic for.
Definition BinaryFile.h:59
void write(const t_type &data)
Definition BinaryFile.h:123
void writeCompression(BinaryCompressionObject &compression_object)
t_type read()
Definition BinaryFile.h:233
A heap data structure that takes the form of a binary tree which is a common way of.
Definition BinaryHeap.hpp:42
void insert(t_comp_type comparison, t_value_type value)
Definition BinaryHeap.hpp:47
uint04 size() const
Definition BinaryHeap.hpp:74
t_value_type popExtreme()
Definition BinaryHeap.hpp:115
void deleteExtreme()
Definition BinaryHeap.hpp:121
t_comp_type extremeComp() const
Definition BinaryHeap.hpp:62
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:52
constexpr t_type surfaceArea() const
The surface area of the shape. This is defined as the area between internal space and non-internal sp...
Definition Bounds.hpp:171
constexpr void addToBounds(const t_vertex &vector)
Adds to the bounds such that the new bounds fully encompasses the argument.
Definition Bounds.hpp:461
constexpr void expand(const t_type &expansion_scaler)
Expands the given expansion scaler. such that max and min are both expanded outward from the center b...
Definition Bounds.hpp:200
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:59
void add(t_type &&object)
Definition Buffer.hpp:212
constexpr t_index_type size() const
Definition Buffer.hpp:1395
void addAll(const Buffer< t_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > &buffer)
Definition Buffer.hpp:278
void setSize(t_index_type new_size)
Definition Buffer.hpp:1351
void swapIndices(t_index_type index_1, t_index_type index_2)
Definition Buffer.hpp:1478
void ensureCapacity(t_index_type new_capacity, bool ensure_not_greater=false, bool ensure_not_less=true)
Definition Buffer.hpp:791
decltype(auto) begin()
Definition Buffer.hpp:518
void clear()
Definition Buffer.hpp:578
bool removeElement(const t_type &element)
Definition Buffer.hpp:1040
static std::enable_if<!ObjectInfo< t_type >::Buffer >::type Compress(BinaryCompressionObject &object, Buffer< uint01 > &compression_data, const Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data)
Definition Compressor.h:117
Definition MemoryManager.h:265
A light-weight base class for Log that allows processes to update, without the need for.
Definition ProgressInfo.hpp:48
void clear(uint04 index_start)
Definition RTree.hpp:64
Bounds< t_dims, t_type > & bounds()
Definition RTree.hpp:90
RNode()
Definition RTree.hpp:56
uint04 right() const
Definition RTree.hpp:81
const Bounds< t_dims, t_type > & bounds() const
Definition RTree.hpp:86
void setBounds(const Bounds< t_dims, t_type > &bounds)
Definition RTree.hpp:94
RNode(uint04 index_start)
Definition RTree.hpp:60
uint04 left() const
Definition RTree.hpp:77
void clear(uint04 index_start, const Bounds< t_dims, t_type > &bounds)
Definition RTree.hpp:70
Bounds< t_dims, t_type > m_cache_bounds
Definition RTree.hpp:100
void _getClosestElement(uint04 top_index, const t_location_type &selector, const t_buffer_type &elements, t_type &min_distance_squared, const t_type &epsilon, uint04 &min_index) const
Definition RTree.hpp:355
uint04 _getNumberOfEnclosedElements(uint04 top_node, const t_area_type &area, const t_buffer_type &elements) const
Definition RTree.hpp:523
bool validate(const t_buffer_type &elements)
Definition RTree.hpp:128
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 RTree.hpp:254
uint04 _calculateAndSplit(uint04 node_index, TreeBoundarySorter< t_dims, t_type > &sorter, const t_buffer_type &elements, uint04 start, uint04 end, bool is_precise)
Definition RTree.hpp:219
void _makeWriteable()
Definition RTree.hpp:990
void _getChangedElements(uint04 top_node, const t_area_type &new_area, const t_area_type &old_area, Buffer< bool > &indices, const t_buffer_type &elements, bool allow_enable, bool allow_disable) const
Definition RTree.hpp:607
void _removeValue(uint04 top_node, uint04 index, const t_buffer_type &elements)
Definition RTree.hpp:678
bool recalculateBounds(uint04 node_index, const t_buffer_type &elements)
Definition RTree.hpp:968
void _getEnclosedElements(uint04 top_node, t_area_type area, Buffer< bool > &indices, const t_buffer_type &elements) const
Definition RTree.hpp:444
void _moveValue(uint04 top_node, uint04 index, const t_node_type &old_location, const t_buffer_type &elements, bool rebalance)
Definition RTree.hpp:756
bool needsRebalance(uint04 left_size, uint04 right_size)
Definition RTree.hpp:841
bool useBulkResize(uint04 change_size)
Definition RTree.hpp:847
void _getClosestElements(uint04 top_index, const t_reference_type &selector, const t_buffer_type &elements, t_type &min_distance_squared, const t_type &epsilon, MinHeap< t_type, uint04 > &value_heap, uint04 size) const
Definition RTree.hpp:399
void _addValue(uint04 node_index, uint04 index, const t_buffer_type &elements, bool balance)
Definition RTree.hpp:880
void _addValues(uint04 top_node, Buffer< uint04 > indices, const t_buffer_type &elements, bool is_precise, ProgressInfo *progress=nullptr)
Definition RTree.hpp:852
void _getEnclosedElements(uint04 top_node, t_area_type area, Buffer< uint04 > &indices, const t_buffer_type &elements) const
Definition RTree.hpp:481
void traverse(const std::function< void(uint04 node_index, const Buffer< RNode< t_dims, t_type >, uint04, ObjectAllocator< true > > &node, const Buffer< uint04 > &indices)> &callback) const
Definition RTree.hpp:201
void _getNearElements(uint04 top_node, t_type max_distance, const t_area_type &area, Buffer< bool > &indices, const t_buffer_type &elements) const
Definition RTree.hpp:562
RTreeBase(uint04 bucket_size)
Definition RTree.hpp:113
bool predictBranch(uint04 &index, const RNode< t_dims, t_type > &node, const t_location_type &selector, MaxHeap< t_type, uint04 > &node_heap, t_type min_distance_squared) const
Definition RTree.hpp:300
Definition RTree.hpp:1014
RTree & operator=(const RTree &value)
Definition RTree.hpp:1060
RTree & operator=(RTree &&value)
Definition RTree.hpp:1069
virtual const char * getTreeType() const override
Definition RTree.hpp:1134
void mapToFile(BinaryFile &file, Buffer< BinaryCompressionObject > &objects, uint04 compression_index=0)
Definition RTree.hpp:1078
void mapToFile(BinaryFile &file, CompressionMode mode)
Definition RTree.hpp:1115
void mapFromFile(BinaryFile &file)
Definition RTree.hpp:1125
RTree()
Definition RTree.hpp:1020
void compress(Buffer< BinaryCompressionObject > &objects, Buffer< Buffer< uint01 > > &compressed_data, uint04 compression_index, uint04 internal_index)
Definition RTree.hpp:1088
Bounds< t_dims, t_type > bounds() const
Definition RTree.hpp:1050
uint04 m_bucket_size
Definition Tree.hpp:484
Buffer< RNode< 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 size() const
Definition Tree.hpp:229
Buffer< uint04 > m_indices
Definition Tree.hpp:479
static const uint04 root_node
Definition Tree.hpp:483
void splitLeafNode(uint04 index)
Definition Tree.hpp:353
void clear()
Definition Tree.hpp:94
Buffer< uint04 > m_available_indexed_positions
Definition Tree.hpp:482
Definition TreeSorter.h:94
uint04 getMedian(uint01 dimension, uint04 start, uint04 end)
Definition TreeSorter.h:386
Bounds< t_dims, t_type > getBounds(uint04 start, uint04 finish, const t_list_type &locations) const
Definition TreeSorter.h:244
Vector< 2, Bounds< t_dims, t_type > > getMedianBounds(uint01 dimension, uint04 start, uint04 end, const t_list_type &locations)
Definition TreeSorter.h:266
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
void getList(uint01 dimension, uint04 start, uint04 end, Buffer< uint04 > &values, uint04 index_offset)
Definition TreeSorter.h:60
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:177
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:78
CompressionMode
Logical information about the type of compression implemented or requested.
Definition Compressor.h:15
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:45
@ inside
Definition BaseValues.hpp:207
@ mixed
Definition BaseValues.hpp:208
@ outside
Definition BaseValues.hpp:206
IntersectionTypes classify(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition Intersection.hpp:326
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:94
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1225
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:379
constexpr t_type distance(const t_vertex &vertex, const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Distance.hpp:243
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved.
Definition BaseValues.hpp:230