NDEVR
API Documentation
PCLTreeWrapper.h
1#pragma once
2#include "../Headers/DLLInfo.h"
3#include "../Headers/PCLFunctions.h"
4#include <pcl/point_cloud.h>
5#include <pcl/point_types.h>
6#include <pcl/search/search.h>
7#include <NDEVR/RTree.h>
8#include <NDEVR/Matrix.h>
9#include <NDEVR/Buffer.h>
10#include <NDEVR/Translator.h>
11namespace NDEVR
12{
16 template<typename PointT>
17 class PCLRTreeWrapper : public pcl::search::Search<PointT>
18 {
19 public:
20 using PointCloud = pcl::PointCloud<PointT>;
21 using PointCloudPtr = typename PointCloud::Ptr;
22 using PointCloudConstPtr = typename PointCloud::ConstPtr;
23
24 typedef std::shared_ptr<std::vector<int>> IndicesPtr;
25 typedef std::shared_ptr<const std::vector<int>> IndicesConstPtr;
26
27 typedef std::shared_ptr<PCLRTreeWrapper<PointT>> Ptr;
28 typedef std::shared_ptr<const PCLRTreeWrapper<PointT> > ConstPtr;
29
37 PCLRTreeWrapper(const Buffer<Vertex<3, fltp04>>& vertices, const RTree<3, fltp04>& tree, uint04& total_search_ptr, uint04 search_count, LogPtr log = LogPtr())
38 : pcl::search::Search<PointT>()
39 , m_tree(tree)
40 , m_values(vertices)
41 , m_search_count(search_count)
42 , m_current_search_count(total_search_ptr)
43 , m_log(_t("Tree Operation"), log)
44 {}
45
46 PCLRTreeWrapper(const Buffer<Vertex<3, fltp04>>& vertices, const RTree<3, fltp04>& tree, uint04& search_count, LogPtr log = LogPtr())
47 : pcl::search::Search<PointT>()
48 , m_tree(tree)
49 , m_values(vertices)
50 , m_search_count(m_values.size())
51 , m_current_search_count(search_count)
52 , m_log(_t("Tree Operation"), log)
53 {}
54
57 {
58 }
59
60
61 bool setInputCloud(const PointCloudConstPtr& cloud, const IndicesConstPtr& indices = IndicesConstPtr()) final override
62 {
63 return pcl::search::Search<PointT>::setInputCloud(cloud, indices);
64 }
65
74
75 int nearestKSearch(const PointT& point, int k,
76 std::vector<int>& k_indices,
77 std::vector<float>& k_sqr_distances) const final override
78 {
79 return radiusSearch(point, m_max_tree_span,
80 k_indices, k_sqr_distances,
81 cast<uint04>(k));
82 }
83
94 int radiusSearch(const PointT& point, double radius,
95 std::vector<int>& k_indices,
96 std::vector<float>& k_sqr_distances,
97 unsigned int k = 0) const final override
98 {
99 Vertex<3, fltp04> vertex(point.x, point.y, point.z);
100 fltp04 max_distance_squared = cast<fltp04>(radius * radius);
101 uint04 ku;
102 if (k == 0)
103 ku = getMin(m_max_k, m_values.size());
104 else
105 ku = getMin(m_max_k, cast<uint04>(k));
106 if (ku == 1)
107 {
108 uint04 index = m_tree.closestElementPresorted(vertex, m_values, max_distance_squared, 0.0f);
109 k_indices.resize(1);
110 k_sqr_distances.resize(1);
111 if (IsInvalid(index))
112 {
113 k_indices[0] = 0;
114 k_sqr_distances[0] = Constant<fltp04>::Max;
115 }
116 else
117 {
118 k_indices[0] = m_tree.indices()[index];
119 k_sqr_distances[0] = max_distance_squared;
120 }
121 }
122 else
123 {
124
125 //vertex = m_input_transform * vertex;
126 MaxHeap<fltp04, uint04> heap(cast<uint04>(ku + 1));
127 m_tree.presortedClosestElements(vertex, ku, heap, m_values, max_distance_squared);
128 k_indices.resize(heap.size());
129 k_sqr_distances.resize(heap.size());
130 const uint04 size = heap.size();
131 auto sorted_values = heap.sortedValues();
132 for (uint04 i = 0; i < sorted_values.size(); i++)
133 {
134 k_indices[i] = cast<int>(m_tree.indices()[sorted_values[i].first]);
135 k_sqr_distances[i] = sorted_values[i].second;
136 }
137 }
138 if (m_current_search_count % 5000 == 0)
139 m_log.setProgress(cast<fltp04>(m_current_search_count) / cast<fltp04>(m_search_count));
140 m_current_search_count++;
141 return cast<int>(k_indices.size());
142 }
143 void setMaxSearchDistance(fltp04 max)
144 {
145 m_max_tree_span = max;
146 }
147 void setMaxK(uint04 max)
148 {
149 m_max_k = max;
150 }
151 protected:
153 const RTree<3, fltp04>& m_tree;
154 const Buffer<Vertex<3, fltp04>>& m_values;
155 uint04 m_search_count;
156 uint04& m_current_search_count;
157 fltp04 m_max_tree_span = Constant<fltp04>::Max;
158 uint04 m_max_k = Constant<uint04>::Max;
159 //Matrix<fltp08> m_input_transform = Matrix<fltp08>(1.0);
160 mutable ProgressInfo m_log;
161 };
162}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
PCLRTreeWrapper(const Buffer< Vertex< 3, fltp04 > > &vertices, const RTree< 3, fltp04 > &tree, uint04 &total_search_ptr, uint04 search_count, LogPtr log=LogPtr())
Constructor for KdTree.
int radiusSearch(const PointT &point, double radius, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances, unsigned int k=0) const final override
Search for all the nearest neighbors of the query point in a given radius.
virtual ~PCLRTreeWrapper()
Destructor for KdTree.
int nearestKSearch(const PointT &point, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const final override
Search for the k-nearest neighbors for the given query point.
const RTree< 3, fltp04 > & m_tree
A pointer to the internal KdTree object.
Used with InfoPipe to signal that the system will be using progress.
A point in N-dimensional space, used primarily for spatial location information.
Definition Vertex.hpp:44
The primary namespace for the NDEVR SDK.
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408