-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkNeighbor.hpp
More file actions
235 lines (194 loc) · 5.49 KB
/
kNeighbor.hpp
File metadata and controls
235 lines (194 loc) · 5.49 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#pragma once
#ifndef KNEIGHBOR_HPP_
#define KNEIGHBOR_HPP_
// #define CGAL_LINKED_WITH_TBB
#include <CGAL/Simple_cartesian.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/Orthogonal_k_neighbor_search.h>
#include <CGAL/Search_traits_3.h>
#include <CGAL/Search_traits_adapter.h>
#include <CGAL/property_map.h>
#include <boost/iterator/zip_iterator.hpp>
#include <iostream>
#include <utility>
#include <vector>
#ifdef CGAL_LINKED_WITH_TBB
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#endif
using std::vector;
//glog
#include <glog/logging.h>
struct kNeighborData {
Eigen::Vector3f point;
int indices;
float square;
};
class kNeighbor
{
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point_d;
typedef CGAL::Search_traits_3<K> TreeTraits;
typedef boost::tuple<Point_d, int> Point_and_int;
typedef CGAL::Search_traits_adapter<Point_and_int,
CGAL::Nth_of_tuple_property_map<0, Point_and_int>,
TreeTraits> Traits;
typedef CGAL::Orthogonal_k_neighbor_search<Traits> Neighbor_search;
typedef Neighbor_search::Tree Tree;
public:
kNeighbor() {
LOG(INFO) << "Init";
points_.resize(0);
indices_.resize(0);
k_ = 1;
};
kNeighbor(std::vector<Point_d> points)
{
LOG(INFO) << "Init and setInputCloud";
points_ = points;
k_ = 1;
build_tree_();
}
kNeighbor(std::vector<Eigen::Vector3f> points)
{
LOG(INFO) << "Init and setInputCloud";
int i = 0;
for (auto it : points) {
points_.push_back(Point_d(it(0), it(1), it(2)));
indices_.push_back(i);
i++;
}
k_ = 1;
build_tree_();
}
kNeighbor(std::vector<Eigen::Vector4f> points)
{
LOG(INFO) << "Init and setInputCloud";
int i = 0;
for (auto it : points) {
points_.push_back(Point_d(it(0), it(1), it(2)));
indices_.push_back(i);
i++;
}
k_ = 1;
build_tree_();
}
~kNeighbor() { };
template <typename EigenT>
void setInputData(std::vector<EigenT> points) {
LOG(INFO) << "set input Data ";
int i = 0;
for (auto it : points) {
points_.push_back(Point_d(it(0), it(1), it(2)));
indices_.push_back(i);
i++;
}
k_ = 1;
build_tree_();
}
void setInputData(std::vector<Point_d> points) {
points_ = points;
k_ = 1;
build_tree_();
}
// search knn
void searchK(Point_d t, size_t k);
template <typename EigenT>
void searchK(EigenT t, size_t k);
// search rnn
void searchR(Point_d t, float r);
template <typename EigenT>
void searchR(EigenT t, float r);
// get quary
std::vector<kNeighborData> getQuary() {
LOG_IF(INFO, quary_.size() != 0) << "get knn quary successful";
LOG_IF(INFO, quary_.size() == 0) << "get knn quary failed";
return quary_; };
private:
//
size_t k_;
// store the raw point cloud
std::vector<Point_d> points_;
// the indices of points
std::vector<int> indices_;
// the result of knn or rnn
// std::vector<Eigen::Vector4f> quary_;
std::vector<kNeighborData> quary_;
// kdtree
Tree tree_;
bool isIndices_ = true;
// init tree
bool build_tree_();
};
template <typename EigenT>
void kNeighbor::searchK(EigenT t, size_t k)
{
Point_d p(t(0), t(1), t(2));
searchK(p, k);
}
void kNeighbor::searchK(Point_d t, size_t k)
{
LOG(INFO) << "search K(" << k << ")nn P(" << t << ")" ;
quary_.clear();
Neighbor_search search(tree_, t, k);
for (Neighbor_search::iterator it = search.begin(); it != search.end(); ++it) {
int indices = boost::get<1>(it->first);
// Eigen::Vector4f point(boost::get<0>(it->first).x(), boost::get<0>(it->first).y(), boost::get<0>(it->first).z(), indices);
Eigen::Vector3f point(boost::get<0>(it->first).x(), boost::get<0>(it->first).y(), boost::get<0>(it->first).z());
float distance = static_cast<float>(it->second);
float aa = (point(0) - t.x()) * (point(0) - t.x()) * 100000;
aa += (point(1) - t.y()) * (point(1) - t.y()) * 100000;
aa += (point(2) - t.z()) * (point(2) - t.z()) * 100000;
kNeighborData kData;
kData.indices = indices;
kData.square = aa;
kData.point = point;
quary_.push_back(kData);
}
}
void kNeighbor::searchR(Point_d t, float r)
{
LOG(INFO) << "search R(" << r << ")nn P(" << t << ")";
quary_.clear();
Neighbor_search search(tree_, t, 20);
for (Neighbor_search::iterator it = search.begin(); it != search.end(); ++it) {
int indices = boost::get<1>(it->first);
Eigen::Vector3f point(boost::get<0>(it->first).x(), boost::get<0>(it->first).y(), boost::get<0>(it->first).z());
if (it->second < r) {
kNeighborData kData;
kData.indices = indices;
kData.square = it->second;
kData.point = point;
quary_.push_back(kData);
}
}
}
bool kNeighbor::build_tree_()
{
if (points_.size() == 0) {
LOG(WARNING) << "No input points, tree build failed";
return false;
}
if (!tree_.is_built()) {
// if (isIndices_ == false)
// tree_.insert(points_.begin(), points_.end());
if (isIndices_ == true)
tree_.insert(boost::make_zip_iterator(boost::make_tuple(points_.begin(), indices_.begin())),
boost::make_zip_iterator(boost::make_tuple(points_.end(), indices_.end())));
#ifdef CGAL_LINKED_WITH_TBB
tree_.build<CGAL::Parallel_tag>();
#else
tree_.build<CGAL::Sequential_tag>();
#endif
LOG(INFO) << "tree build successful";
}
LOG(INFO) << "tree has built";
return true;
}
template <typename EigenT>
void kNeighbor::searchR(EigenT t, float r)
{
Point_d p(t(0), t(1), t(2));
searchR(p, r);
}
#endif