Loading...
Searching...
No Matches
point_quadtree.cuh
1/*
2 * Copyright (c) 2022-2024, 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#include <rmm/resource_ref.hpp>
26
27#include <thrust/iterator/zip_iterator.h>
28#include <thrust/tuple.h>
29
30#include <tuple>
31
32namespace cuspatial {
33
40 // uint32_t vector of quad node keys
41 rmm::device_uvector<uint32_t> key;
42 // uint8_t vector of quadtree levels
43 rmm::device_uvector<uint8_t> level;
44 // bool vector indicating whether the node is a parent (true) or leaf (false) node
45 rmm::device_uvector<bool> internal_node_flag;
46 // uint32_t vector for the number of child nodes (if is_internal_node), or number of points
47 rmm::device_uvector<uint32_t> length;
48 // uint32_t vector for the first child position (if is_internal_node), or first point position
49 rmm::device_uvector<uint32_t> offset;
50};
51
53 using key_iterator = decltype(point_quadtree::key)::const_iterator;
54 using level_iterator = decltype(point_quadtree::level)::const_iterator;
55 using internal_node_flag_iterator = decltype(point_quadtree::internal_node_flag)::const_iterator;
56 using length_iterator = decltype(point_quadtree::length)::const_iterator;
57 using offset_iterator = decltype(point_quadtree::offset)::const_iterator;
58
61 : _key_begin(quadtree.key.begin()),
62 _key_end(quadtree.key.end()),
63 _level_begin(quadtree.level.begin()),
64 _level_end(quadtree.level.end()),
65 _internal_node_flag_begin(quadtree.internal_node_flag.begin()),
66 _internal_node_flag_end(quadtree.internal_node_flag.end()),
67 _length_begin(quadtree.length.begin()),
68 _length_end(quadtree.length.end()),
69 _offset_begin(quadtree.offset.begin()),
70 _offset_end(quadtree.offset.end())
71 {
72 }
73
76 key_iterator key_end,
77 level_iterator level_begin,
78 internal_node_flag_iterator internal_node_flag_begin,
79 length_iterator length_begin,
80 offset_iterator offset_begin)
81 : _key_begin(key_begin),
82 _key_end(key_end),
83 _level_begin(level_begin),
84 _level_end(level_begin + std::distance(key_begin, key_end)),
85 _internal_node_flag_begin(internal_node_flag_begin),
86 _internal_node_flag_end(internal_node_flag_begin + std::distance(key_begin, key_end)),
87 _length_begin(length_begin),
88 _length_end(length_begin + std::distance(key_begin, key_end)),
89 _offset_begin(offset_begin),
90 _offset_end(offset_begin + std::distance(key_begin, key_end))
91 {
92 }
93
95 CUSPATIAL_HOST_DEVICE auto num_nodes() const { return std::distance(_key_begin, _key_end); }
96
98 CUSPATIAL_HOST_DEVICE auto key_begin() const { return _key_begin; }
100 CUSPATIAL_HOST_DEVICE auto key_end() const { return _key_end; }
101
103 CUSPATIAL_HOST_DEVICE auto level_begin() const { return _level_begin; }
105 CUSPATIAL_HOST_DEVICE auto level_end() const { return _level_end; }
106
108 CUSPATIAL_HOST_DEVICE auto internal_node_flag_begin() const { return _internal_node_flag_begin; }
110 CUSPATIAL_HOST_DEVICE auto internal_node_flag_end() const { return _internal_node_flag_end; }
111
113 CUSPATIAL_HOST_DEVICE auto length_begin() const { return _length_begin; }
115 CUSPATIAL_HOST_DEVICE auto length_end() const { return _length_end; }
116
118 CUSPATIAL_HOST_DEVICE auto offset_begin() const { return _offset_begin; }
120 CUSPATIAL_HOST_DEVICE auto offset_end() const { return _offset_end; }
121
122 protected:
123 key_iterator _key_begin;
124 key_iterator _key_end;
125 level_iterator _level_begin;
126 level_iterator _level_end;
127 internal_node_flag_iterator _internal_node_flag_begin;
128 internal_node_flag_iterator _internal_node_flag_end;
129 length_iterator _length_begin;
130 length_iterator _length_end;
131 offset_iterator _offset_begin;
132 offset_iterator _offset_end;
133};
134
170template <class PointIterator, class T = typename cuspatial::iterator_value_type<PointIterator>>
171std::pair<rmm::device_uvector<uint32_t>, point_quadtree> quadtree_on_points(
172 PointIterator points_first,
173 PointIterator points_last,
174 vec_2d<T> vertex_1,
175 vec_2d<T> vertex_2,
176 T scale,
177 int8_t max_depth,
178 int32_t max_size,
179 rmm::cuda_stream_view stream = rmm::cuda_stream_default,
180 rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
181
186} // namespace cuspatial
187
188#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::device_async_resource_ref 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.