libcuspatial  24.04.00
Loading...
Searching...
No Matches
point_quadtree.cuh
1/*
2 * Copyright (c) 2022-2023, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
20#include <cuspatial/traits.hpp>
21
22#include <rmm/cuda_stream_view.hpp>
23#include <rmm/device_uvector.hpp>
24#include <rmm/mr/device/per_device_resource.hpp>
25
26#include <thrust/iterator/zip_iterator.h>
27#include <thrust/tuple.h>
28
29#include <tuple>
30
31namespace cuspatial {
32
39 // uint32_t vector of quad node keys
40 rmm::device_uvector<uint32_t> key;
41 // uint8_t vector of quadtree levels
42 rmm::device_uvector<uint8_t> level;
43 // bool vector indicating whether the node is a parent (true) or leaf (false) node
44 rmm::device_uvector<bool> internal_node_flag;
45 // uint32_t vector for the number of child nodes (if is_internal_node), or number of points
46 rmm::device_uvector<uint32_t> length;
47 // uint32_t vector for the first child position (if is_internal_node), or first point position
48 rmm::device_uvector<uint32_t> offset;
49};
50
52 using key_iterator = decltype(point_quadtree::key)::const_iterator;
53 using level_iterator = decltype(point_quadtree::level)::const_iterator;
54 using internal_node_flag_iterator = decltype(point_quadtree::internal_node_flag)::const_iterator;
55 using length_iterator = decltype(point_quadtree::length)::const_iterator;
56 using offset_iterator = decltype(point_quadtree::offset)::const_iterator;
57
60 : _key_begin(quadtree.key.begin()),
61 _key_end(quadtree.key.end()),
62 _level_begin(quadtree.level.begin()),
63 _level_end(quadtree.level.end()),
64 _internal_node_flag_begin(quadtree.internal_node_flag.begin()),
65 _internal_node_flag_end(quadtree.internal_node_flag.end()),
66 _length_begin(quadtree.length.begin()),
67 _length_end(quadtree.length.end()),
68 _offset_begin(quadtree.offset.begin()),
69 _offset_end(quadtree.offset.end())
70 {
71 }
72
75 key_iterator key_end,
76 level_iterator level_begin,
77 internal_node_flag_iterator internal_node_flag_begin,
78 length_iterator length_begin,
79 offset_iterator offset_begin)
80 : _key_begin(key_begin),
81 _key_end(key_end),
82 _level_begin(level_begin),
83 _level_end(level_begin + std::distance(key_begin, key_end)),
84 _internal_node_flag_begin(internal_node_flag_begin),
85 _internal_node_flag_end(internal_node_flag_begin + std::distance(key_begin, key_end)),
86 _length_begin(length_begin),
87 _length_end(length_begin + std::distance(key_begin, key_end)),
88 _offset_begin(offset_begin),
89 _offset_end(offset_begin + std::distance(key_begin, key_end))
90 {
91 }
92
94 CUSPATIAL_HOST_DEVICE auto num_nodes() const { return std::distance(_key_begin, _key_end); }
95
97 CUSPATIAL_HOST_DEVICE auto key_begin() const { return _key_begin; }
99 CUSPATIAL_HOST_DEVICE auto key_end() const { return _key_end; }
100
102 CUSPATIAL_HOST_DEVICE auto level_begin() const { return _level_begin; }
104 CUSPATIAL_HOST_DEVICE auto level_end() const { return _level_end; }
105
107 CUSPATIAL_HOST_DEVICE auto internal_node_flag_begin() const { return _internal_node_flag_begin; }
109 CUSPATIAL_HOST_DEVICE auto internal_node_flag_end() const { return _internal_node_flag_end; }
110
112 CUSPATIAL_HOST_DEVICE auto length_begin() const { return _length_begin; }
114 CUSPATIAL_HOST_DEVICE auto length_end() const { return _length_end; }
115
117 CUSPATIAL_HOST_DEVICE auto offset_begin() const { return _offset_begin; }
119 CUSPATIAL_HOST_DEVICE auto offset_end() const { return _offset_end; }
120
121 protected:
122 key_iterator _key_begin;
123 key_iterator _key_end;
124 level_iterator _level_begin;
125 level_iterator _level_end;
126 internal_node_flag_iterator _internal_node_flag_begin;
127 internal_node_flag_iterator _internal_node_flag_end;
128 length_iterator _length_begin;
129 length_iterator _length_end;
130 offset_iterator _offset_begin;
131 offset_iterator _offset_end;
132};
133
169template <class PointIterator, class T = typename cuspatial::iterator_value_type<PointIterator>>
170std::pair<rmm::device_uvector<uint32_t>, point_quadtree> quadtree_on_points(
171 PointIterator points_first,
172 PointIterator points_last,
173 vec_2d<T> vertex_1,
174 vec_2d<T> vertex_2,
175 T scale,
176 int8_t max_depth,
177 int32_t max_size,
178 rmm::cuda_stream_view stream = rmm::cuda_stream_default,
179 rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
180
185} // namespace cuspatial
186
187#include <cuspatial/detail/point_quadtree.cuh>
std::pair< rmm::device_uvector< uint32_t >, point_quadtree > quadtree_on_points(PointIterator points_first, PointIterator points_last, vec_2d< T > vertex_1, vec_2d< T > vertex_2, T scale, int8_t max_depth, int32_t max_size, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a quadtree structure from points.
CUSPATIAL_HOST_DEVICE auto length_begin() const
Return iterator to the first length of the quadtree.
CUSPATIAL_HOST_DEVICE auto offset_end() const
Return iterator to the last child / point offset of the quadtree.
CUSPATIAL_HOST_DEVICE auto length_end() const
Return iterator to the last length of the quadtree.
CUSPATIAL_HOST_DEVICE auto offset_begin() const
Return iterator to the first child / point offset of the quadtree.
CUSPATIAL_HOST_DEVICE auto internal_node_flag_begin() const
Return iterator to the first internal node flag of the quadtree.
CUSPATIAL_HOST_DEVICE auto key_begin() const
Return iterator to the first ring of the quadtree.
CUSPATIAL_HOST_DEVICE auto num_nodes() const
Return the number of keys in the quadtree.
CUSPATIAL_HOST_DEVICE auto key_end() const
Return iterator to the last ring of the quadtree.
CUSPATIAL_HOST_DEVICE auto internal_node_flag_end() const
Return iterator to the last internal node flag of the quadtree.
point_quadtree_ref(key_iterator key_begin, key_iterator key_end, level_iterator level_begin, internal_node_flag_iterator internal_node_flag_begin, length_iterator length_begin, offset_iterator offset_begin)
Construct from iterators and size.
point_quadtree_ref(point_quadtree const &quadtree)
Construct from a point_quadtree struct.
CUSPATIAL_HOST_DEVICE auto level_end() const
Return iterator to the last level of the quadtree.
CUSPATIAL_HOST_DEVICE auto level_begin() const
Return iterator to the first level of the quadtree.