Skip to content

Commit

Permalink
allows to create a kdtree from an array of 3D points
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Oct 31, 2023
1 parent a434bb9 commit 296b39d
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 0 deletions.
5 changes: 5 additions & 0 deletions easy3d/kdtree/kdtree_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ namespace easy3d {
(void)cloud;
}

KdTreeSearch::KdTreeSearch(const std::vector<vec3>& points)
{
(void)points;
}

} // namespace easy3d
6 changes: 6 additions & 0 deletions easy3d/kdtree/kdtree_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ namespace easy3d {
*/
explicit KdTreeSearch(const PointCloud *cloud);

/**
* \brief Constructor.
* \param points The points for which a KdTree will be constructed.
*/
explicit KdTreeSearch(const std::vector<vec3>& points);

virtual ~KdTreeSearch() = default;

/// \name Closest point query
Expand Down
24 changes: 24 additions & 0 deletions easy3d/kdtree/kdtree_search_ann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ namespace easy3d {
}


KdTreeSearch_ANN::KdTreeSearch_ANN(const std::vector<vec3>& points) : KdTreeSearch(points) {
k_for_radius_search_ = 32;
LOG(INFO) << "KdTreeSearch_ANN: k = 32 for radius search";

// prepare data
points_num_ = int(points.size());
#if COPY_POINT_CLOUD // make a copy of the point cloud when constructing the kd-tree
points_ = annAllocPts(points_num_, 3);
for (int i = 0; i < points_num_; ++i) {
const vec3& p = points[i];
points_[i][0] = p[0];
points_[i][1] = p[1];
points_[i][2] = p[2];
}
#else
points_ = new float* [points_num_];
for (int i = 0; i < points_num_; ++i)
points_[i] = const_cast<float*>(points[i].data());
#endif
// create tree
tree_ = new ANNkd_tree(const_cast<float**>(points_), points_num_, 3);
}


KdTreeSearch_ANN::~KdTreeSearch_ANN() {
#if COPY_POINT_CLOUD // make a copy of the point cloud when constructing the kd-tree
annDeallocPts(points_);
Expand Down
6 changes: 6 additions & 0 deletions easy3d/kdtree/kdtree_search_ann.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace easy3d {
*/
explicit KdTreeSearch_ANN(const PointCloud *cloud);

/**
* \brief Constructor.
* \param points The points for which a KdTree will be constructed.
*/
explicit KdTreeSearch_ANN(const std::vector<vec3>& points);

~KdTreeSearch_ANN() override;

/// \name Closest point query
Expand Down
11 changes: 11 additions & 0 deletions easy3d/kdtree/kdtree_search_eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ namespace easy3d {
}


KdTreeSearch_ETH::KdTreeSearch_ETH(const std::vector<vec3>& points) : KdTreeSearch(points) {
// prepare data
points_num_ = points.size();
points_ = const_cast<float*>(points[0].data());

// create tree
const int maxBucketSize = 16; // number of points per bucket
tree_ = new kdtree::KdTree(reinterpret_cast<kdtree::Vector3D*>(points_), points_num_, maxBucketSize);
}


KdTreeSearch_ETH::~KdTreeSearch_ETH() {
delete get_tree(tree_);
points_ = nullptr;
Expand Down
6 changes: 6 additions & 0 deletions easy3d/kdtree/kdtree_search_eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace easy3d {
*/
explicit KdTreeSearch_ETH(const PointCloud *cloud);

/**
* \brief Constructor.
* \param points The points for which a KdTree will be constructed.
*/
explicit KdTreeSearch_ETH(const std::vector<vec3>& points);

~KdTreeSearch_ETH() override;

/// \name Closest point query
Expand Down
17 changes: 17 additions & 0 deletions easy3d/kdtree/kdtree_search_flann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ namespace easy3d {
}


KdTreeSearch_FLANN::KdTreeSearch_FLANN(const std::vector<vec3>& points) : KdTreeSearch(points) {
//checks_ = 32;
checks_ = flann::FLANN_CHECKS_AUTOTUNED;

// prepare data
points_num_ = int(points.size());
points_ = const_cast<float*>(points[0].data());

// create tree
flann::Matrix<float> dataset(points_, points_num_, 3);
// construct a single kd-tree optimized for searching lower dimensionality data
auto tree = new flann::Index< flann::L2<float> >(dataset, flann::KDTreeSingleIndexParams());
tree->buildIndex();
tree_ = tree;
}


KdTreeSearch_FLANN::~KdTreeSearch_FLANN() {
delete get_tree(tree_);
}
Expand Down
6 changes: 6 additions & 0 deletions easy3d/kdtree/kdtree_search_flann.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace easy3d {
*/
explicit KdTreeSearch_FLANN(const PointCloud *cloud);

/**
* \brief Constructor.
* \param points The points for which a KdTree will be constructed.
*/
explicit KdTreeSearch_FLANN(const std::vector<vec3>& points);

~KdTreeSearch_FLANN() override;

/**
Expand Down
12 changes: 12 additions & 0 deletions easy3d/kdtree/kdtree_search_nanoflann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ namespace easy3d {
}


KdTreeSearch_NanoFLANN::KdTreeSearch_NanoFLANN(const std::vector<vec3>& points) : KdTreeSearch(points) {
// prepare data
points_ = const_cast<std::vector<vec3>*>(&points);

// create tree
auto pset = new PointSet(points_);
auto tree = new KdTree(pset);
tree->buildIndex();
tree_ = tree;
}


KdTreeSearch_NanoFLANN::~KdTreeSearch_NanoFLANN() {
delete get_tree(tree_);
}
Expand Down
6 changes: 6 additions & 0 deletions easy3d/kdtree/kdtree_search_nanoflann.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ namespace easy3d {
*/
explicit KdTreeSearch_NanoFLANN(const PointCloud *cloud);

/**
* \brief Constructor.
* \param points The points for which a KdTree will be constructed.
*/
explicit KdTreeSearch_NanoFLANN(const std::vector<vec3>& points);

~KdTreeSearch_NanoFLANN() override;

/// \name Closest point query
Expand Down

0 comments on commit 296b39d

Please sign in to comment.