API Documentation
Loading...
Searching...
No Matches
ThreadedRTree.h
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Tree
28File: ThreadedRTree
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "RTree.h>
34#include "Base\Headers\Thread.h>
36#include "Base\Headers\RWLock.h>
37namespace NDEVR
38{
39 template<uint01 t_dims, class t_type>
40 class ThreadedRTree : public RTree<t_dims, t_type>
41 {
42
43 public:
44 explicit ThreadedRTree<t_dims, t_type>(uint04 bucket_size)
45 : RTree<t_dims, t_type>(bucket_size)
47 {
48 m_indices.allocateSpace<false>(bucket_size);
49 }
52 , m_number_of_threads(tree.m_number_of_threads)
53 {}
56 , m_number_of_threads(tree.m_number_of_threads)
57 {}
58
59 template<class t_node_type>
60 bool addAllThreaded(const Buffer<t_node_type>& elements, ProgressInfo* progress = nullptr)
61 {
63
64 m_nodes[root_node].clear(m_nodes[root_node].begin(), _getBoundingBoxThreaded(0, indices, elements, 0, indices.size()));
65
66 if (progress != nullptr) progress->allowCancel(true);//if you change this, be ready to catch the exception that gets thrown when you cancel
67 bool did_balance = _balanceLeafNodeThreaded(root_node, 0, elements, indices, 0, indices.size(), progress);
68 if (progress != nullptr) progress->allowCancel(false);
69
70 m_indices.ensureCapacity(cast<uint04>(1.1 * (m_indices.size())), true, true);
71 m_nodes.ensureCapacity(cast<uint04>(1.1 * (m_nodes.size())), true, true);
72
73 return did_balance;
74
75 }
76
77 template<class t_node_type>
78 bool addAllThreaded(const Buffer<t_node_type>& elements, uint04 start_index, uint04 end_index, ProgressInfo* progress = nullptr)
79 {
80 Buffer<uint04> indices = prepareAddAll(start_index, end_index);
81
82 m_nodes[root_node].clear(m_nodes[root_node].begin(), bounds);
83
84 if (progress != nullptr) progress->allowCancel(true);//if you change this, be ready to catch the exception that gets thrown when you cancel
85 bool did_balance = _balanceLeafNodeThreaded(root_node, 1, elements, indices, 0, indices.size(), progress);
86 if (progress != nullptr) progress->allowCancel(false);
87
88 m_indices.ensureCapacity(cast<uint04>(1.1 * (m_indices.size())), true, true);
89 m_nodes.ensureCapacity(cast<uint04>(1.1 * (m_nodes.size())), true, true);
90
91 return did_balance;
92 }
93
94
95
96 template<class t_node_type>
97 bool _balanceLeafNodeThreaded(uint04 level, const uint04 node_index, const Buffer<t_node_type>& elements, Buffer<uint04>& indices, uint04 start, uint04 end, ProgressInfo* progress = nullptr)
98 {
99 uint04 level_value = pow(2U, level);
100 if (m_number_of_threads <= level_value || (end - start) < 1000)
101 {
102 _balanceLeafNode(node_index, elements, indices, start, end);
103 }
104 else
105 {
106 const uint04 new_left = _calculateAndSplit(node_index, elements, indices, start, end);
107 splitThread(
108 [this, level, start, new_left, node_index, progress, &indices, &elements] {
109 //uint08 start_time = Time::SystemTime();
110 _balanceLeafNodeThreaded(level + 1, m_nodes[node_index].left(), elements, indices, start, new_left, progress);
111 //if (progress != nullptr) progress->addMessage(String("Finished: Level = ") + level + " Time: " + (Time::SystemTime() - start_time));
112 },
113 [this, level, end, new_left, node_index, progress, &indices, &elements] {
114 //uint08 start_time = Time::SystemTime();
115 _balanceLeafNodeThreaded(level + 1, m_nodes[node_index].right(), elements, indices, new_left, end, progress);
116 //if (progress != nullptr) progress->addMessage(String("Finished: Level = ") + level + " Time: " + (Time::SystemTime() - start_time));
117 });
118 }
119 return true;
120 }
121 template<class t_node_type>
123 {
124 if (m_number_of_threads <= pow(2U, top_level) || end - start < 100)
125 {
126 return _getBounds(indices, elements, start, end);
127 }
128 else
129 {
130 const uint04 middle = start + (end - start) / 2;
133 splitThread(
134 [this, &left, &indices, &elements, start, middle, top_level] {
135 left = _getBoundingBoxThreaded(top_level + 1, indices, elements, start, middle);
136 },
137 [this, &right, &indices, &elements, end, middle, top_level] {
138 right = _getBoundingBoxThreaded(top_level + 1, indices, elements, middle, end);
139 });
140 return Bounds<t_dims, t_type>(left, right);
141 }
142 }
143
144 void splitLeafNode(uint04 index) final override
145 {
146 m_split_lock.lock();
148 m_split_lock.unlock();
149
150 }
151 std::mutex m_split_lock;
153 };
154};
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:57
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
constexpr t_index_type size() const
Definition Buffer.hpp:1461
void ensureCapacity(t_index_type new_capacity, bool ensure_not_greater=false, bool ensure_not_less=true)
Definition Buffer.hpp:803
Definition ProgressInfo.hpp:43
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
Definition RTree.hpp:1014
RTree()
Definition RTree.hpp:1020
Bounds< t_dims, t_type > bounds() const
Definition RTree.hpp:1050
Definition ThreadedRTree.h:41
bool _balanceLeafNodeThreaded(uint04 level, const uint04 node_index, const Buffer< t_node_type > &elements, Buffer< uint04 > &indices, uint04 start, uint04 end, ProgressInfo *progress=nullptr)
Definition ThreadedRTree.h:97
bool addAllThreaded(const Buffer< t_node_type > &elements, uint04 start_index, uint04 end_index, ProgressInfo *progress=nullptr)
Definition ThreadedRTree.h:78
void splitLeafNode(uint04 index) final override
Definition ThreadedRTree.h:144
ThreadedRTree(uint04 bucket_size)
Definition ThreadedRTree.h:44
bool addAllThreaded(const Buffer< t_node_type > &elements, ProgressInfo *progress=nullptr)
Definition ThreadedRTree.h:60
std::mutex m_split_lock
Definition ThreadedRTree.h:151
Bounds< t_dims, t_type > _getBoundingBoxThreaded(uint04 top_level, const Buffer< uint04 > &indices, const Buffer< t_node_type > &elements, uint04 start, uint04 end)
Definition ThreadedRTree.h:122
uint04 m_number_of_threads
Definition ThreadedRTree.h:152
Buffer< RNode< t_dims, t_type >, uint04, ObjectAllocator< true > > m_nodes
Definition Tree.hpp:480
const Buffer< uint04 > & indices() const
Definition Tree.hpp:287
Buffer< uint04 > prepareAddAll(const Buffer< bool > &index_values)
Definition Tree.hpp:322
Bounds< t_dims, t_type > _getBounds(const Buffer< uint04 > &indices, const t_buffer_type &elements, uint04 start, uint04 end)
Definition Tree.hpp:257
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
Definition ACIColor.h:37
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514