-
Notifications
You must be signed in to change notification settings - Fork 1
/
pointcloud.h
199 lines (162 loc) · 3.9 KB
/
pointcloud.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Spdx-License-Identifier: MIT
SPDX-FileCopyrightText: 2005-2020 Dominik Haumann <[email protected]>
*/
#ifndef KDTREE_POINTCLOUD_H
#define KDTREE_POINTCLOUD_H
#ifdef WIN32
#pragma warning(disable:4530)
#endif
#include <string>
#include <vector>
#include "point.h"
#include "node.h"
#include <algorithm>
namespace kdtree
{
/**
* The class @p PointCloud represents a cloud of point data.
*/
template <class T>
class PointCloud
{
public:
constexpr PointCloud() = default;
virtual ~PointCloud();
/**
* Find the @p k nearest points to given reference point @p p. The result
* will be stored in the vector @p result.
* @param p reference point
* @param k amount of points to find
* @param result returned vector containing the points
* @return true on success, false if you forgot to call rebuildTree().
*/
bool findKNearest(const float* p, unsigned int k, std::vector<T>& result);
/**
* Find all points in the sphere with center @p m and @p radius. The result
* will be stored in the vector @p result.
* @param m center of sphere
* @param radius2 square radius of sphere
* @param result returned vector containing the points
* @return true on success, false if you forgot to call rebuildTree().
*/
bool findInRadius(const float* m, float radius2, std::vector<T>& result);
/**
* Create the KdTree structure of the current point cloud data.
* @note Call this function once you are done with adding cloud data, i.e.,
* before calling findKNearest() and findInRadius().
*/
void rebuildTree();
/**
* Clear all items, the PointCloud does not contain any data afterwards.
*/
void clear();
/**
* Set all data points. Before the new data is set, the old data is removed.
* @note Call rebuildTree() afterwards.
*/
void setItems(const std::vector <T>& items);
/**
* Append data points to the already existing point cloud.
* @note Call rebuildTree() afterwards.
*/
void addItems(const std::vector <T>& items);
/**
* Append a signel item to the already point cloud.
* @note Call rebuildTree() afterwards.
*/
void addItem(const T& item);
/**
* Get the list of all points as const reference.
*/
const std::vector <T>& points() const;
private:
std::vector <T> m_points;
kdtree::Node<T>* m_kdtree = nullptr;
};
//
//
// TEMPLATE IMPLEMENTATION
//
//
template <class T>
PointCloud<T>::~PointCloud()
{
delete m_kdtree;
m_kdtree = 0;
}
template <class T>
void PointCloud<T>::rebuildTree()
{
if (m_kdtree) {
delete m_kdtree;
}
m_kdtree = new kdtree::Node<T>(m_points, 0, m_points.size());
}
template <class T>
bool PointCloud<T>::findKNearest(const float* p, unsigned int k, std::vector<T>& result)
{
result.clear();
if (!m_kdtree) {
return false;
}
if (k >= m_points.size())
{
result.assign(m_points.begin(), m_points.end());
std::nth_element(result.begin(),
result.end(),
result.end(),
T::smaller_dist);
}
else if (k > 0)
{
kdtree::Node<T>::dist = 100000000.0f;
m_kdtree->findKNearest(p, k, result);
}
return true;
}
template <class T>
bool PointCloud<T>::findInRadius(const float* m, float radius2, std::vector<T>& result)
{
if (!m_kdtree) {
return false;
}
result.clear();
m_kdtree->findInRadius(m, radius2, result);
return true;
}
template <class T>
void PointCloud<T>::clear()
{
delete m_kdtree;
m_kdtree = 0;
m_points.clear();
}
template <class T>
void PointCloud<T>::setItems(const std::vector <T>& items)
{
clear();
m_points = items;
}
template <class T>
void PointCloud<T>::addItems(const std::vector <T>& items)
{
delete m_kdtree;
m_kdtree = 0;
m_points.insert(m_points.end(), items.begin(), items.end());
}
template <class T>
void PointCloud<T>::addItem(const T& item)
{
delete m_kdtree;
m_kdtree = 0;
m_points.push_back(item);
}
template <class T>
const std::vector <T>& PointCloud<T>::points() const
{
return m_points;
}
}
#endif // KDTREE_POINTCLOUD_H
// kate: indent-width 4; tab-width 4; replace-tabs off;