Skip to content

Commit

Permalink
simplify kdtree construction
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Jul 7, 2022
1 parent d33aa90 commit 1383eb9
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 237 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ project(Easy3D)

set(EASY3D_MAJOR_VERSION 2)
set(EASY3D_MINOR_VERSION 4)
set(EASY3D_PATCH_VERSION 8)
set(EASY3D_PATCH_VERSION 9)
set(EASY3D_VERSION "${EASY3D_MAJOR_VERSION}.${EASY3D_MINOR_VERSION}.${EASY3D_PATCH_VERSION}")

################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ void DialogPointCloudSimplification::constructKdTree() {
if (cloud) {
if (kdtree_)
delete kdtree_;
kdtree_ = new KdTreeSearch_ETH;
kdtree_->begin();
kdtree_->add_point_cloud(cloud);
kdtree_->end();
kdtree_ = new KdTreeSearch_ETH(cloud);
}
}

Expand Down
10 changes: 2 additions & 8 deletions easy3d/algo/point_cloud_normals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ namespace easy3d {
w.start();

LOG(INFO) << "building kd_tree...";
KdTreeSearch_NanoFLANN kdtree;
kdtree.begin();
kdtree.add_point_cloud(cloud);
kdtree.end();
KdTreeSearch_NanoFLANN kdtree(cloud);
LOG(INFO) << "done. " << w.time_string();

int num = cloud->n_vertices();
Expand Down Expand Up @@ -388,10 +385,7 @@ namespace easy3d {
w.start();

LOG(INFO) << "building kd_tree...";
KdTreeSearch_NanoFLANN kdtree;
kdtree.begin();
kdtree.add_point_cloud(cloud);
kdtree.end();
KdTreeSearch_NanoFLANN kdtree(cloud);
LOG(INFO) << "done. " << w.time_string();

w.restart();
Expand Down
15 changes: 3 additions & 12 deletions easy3d/algo/point_cloud_simplification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ namespace easy3d {
KdTreeSearch *kdtree = tree;
bool need_delete(false);
if (!kdtree) {
kdtree = new KdTreeSearch_ETH;
kdtree->begin();
kdtree->add_point_cloud(cloud);
kdtree->end();
kdtree = new KdTreeSearch_ETH(cloud);
need_delete = true;
}

Expand Down Expand Up @@ -164,10 +161,7 @@ namespace easy3d {
KdTreeSearch *kdtree = tree;
bool need_delete(false);
if (!kdtree) {
kdtree = new KdTreeSearch_ETH;
kdtree->begin();
kdtree->add_point_cloud(cloud);
kdtree->end();
kdtree = new KdTreeSearch_ETH(cloud);
need_delete = true;
}

Expand Down Expand Up @@ -244,10 +238,7 @@ namespace easy3d {
if (expected_num >= num)
return points_to_delete; // expected num is greater than / equal to given number.

KdTreeSearch_ETH kdtree;
kdtree.begin();
kdtree.add_point_cloud(cloud);
kdtree.end();
KdTreeSearch_ETH kdtree(cloud);

// the average squared distance to its nearest neighbor; smaller value means highter density
std::vector<float> sqr_distance(cloud->n_vertices());
Expand Down
2 changes: 1 addition & 1 deletion easy3d/kdtree/kdtree_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace easy3d {


KdTreeSearch::KdTreeSearch()
KdTreeSearch::KdTreeSearch(const PointCloud *cloud)
{
}

Expand Down
22 changes: 4 additions & 18 deletions easy3d/kdtree/kdtree_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,13 @@ namespace easy3d {

class KdTreeSearch {
public:
KdTreeSearch();

virtual ~KdTreeSearch();

/// \name Tree construction
/// @{
/**
* \brief Begins the construction of a KdTree.
* \brief Constructor.
* \param cloud The point cloud for which a KdTree will be constructed.
*/
virtual void begin() = 0;
KdTreeSearch(const PointCloud *cloud);

/**
* \brief Sets the point cloud for which a KdTree will be constructed.
*/
virtual void add_point_cloud(PointCloud *cloud) = 0;

/**
* \brief Finalizes the construction of a KdTree.
*/
virtual void end() = 0;
/// @}
virtual ~KdTreeSearch();

/// \name Closest point query
/// @{
Expand Down
63 changes: 19 additions & 44 deletions easy3d/kdtree/kdtree_search_ann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,33 @@ using namespace ANN;

namespace easy3d {

KdTreeSearch_ANN::KdTreeSearch_ANN() {
points_ = nullptr;
points_num_ = 0;
tree_ = nullptr;
KdTreeSearch_ANN::KdTreeSearch_ANN(const PointCloud *cloud) : KdTreeSearch(cloud) {
k_for_radius_search_ = 32;
LOG(INFO) << "KdTreeSearch_ANN: k = 32 for radius search";
}


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

delete get_tree(tree_);
annClose();
// create tree
tree_ = new ANNkd_tree(const_cast<float **>(points_), points_num_, 3);
}


void KdTreeSearch_ANN::begin() {
points_num_ = 0;

KdTreeSearch_ANN::~KdTreeSearch_ANN() {
#if COPY_POINT_CLOUD // make a copy of the point cloud when constructing the kd-tree
if (points_)
annDeallocPts(points_);
Expand All @@ -78,33 +79,7 @@ namespace easy3d {
#endif

delete get_tree(tree_);
tree_ = nullptr;
}


void KdTreeSearch_ANN::end() {
tree_ = new ANNkd_tree(points_, points_num_, 3);
}


void KdTreeSearch_ANN::add_point_cloud(PointCloud* cloud) {
points_num_ = int(cloud->n_vertices());

#if COPY_POINT_CLOUD // make a copy of the point cloud when constructing the kd-tree
points_ = annAllocPts(points_num_, 3);
const std::vector<vec3>& pts = cloud->points();
for (int i = 0; i < points_num_; ++i) {
const vec3& p = pts[i];
points_[i][0] = p[0];
points_[i][1] = p[1];
points_[i][2] = p[2];
}
#else
points_ = new float*[points_num_];
std::vector<vec3>& pts = cloud->points();
for (int i = 0; i < points_num_; ++i)
points_[i] = pts[i];
#endif
annClose();
}


Expand Down
23 changes: 4 additions & 19 deletions easy3d/kdtree/kdtree_search_ann.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,13 @@ namespace easy3d {
*/
class KdTreeSearch_ANN : public KdTreeSearch {
public:
KdTreeSearch_ANN();

~KdTreeSearch_ANN() override;


/// \name Tree construction
/// @{
/**
* \brief Begins the construction of a KdTree.
*/
void begin() override;

/**
* \brief Sets the point cloud for which a KdTree will be constructed.
* \brief Constructor.
* \param cloud The point cloud for which a KdTree will be constructed.
*/
void add_point_cloud(PointCloud *cloud) override;
KdTreeSearch_ANN(const PointCloud *cloud);

/**
* \brief Finalizes the construction of a KdTree.
*/
void end() override;
/// @}
~KdTreeSearch_ANN() override;

/// \name Closest point query
/// @{
Expand Down
35 changes: 9 additions & 26 deletions easy3d/kdtree/kdtree_search_eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,24 @@

namespace easy3d {

KdTreeSearch_ETH::KdTreeSearch_ETH() {
points_num_ = 0;
points_ = nullptr;
tree_ = nullptr;
}

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

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


void KdTreeSearch_ETH::begin() {
KdTreeSearch_ETH::~KdTreeSearch_ETH() {
delete get_tree(tree_);
tree_ = nullptr;

points_num_ = 0;
points_ = nullptr;
}


void KdTreeSearch_ETH::end() {
int maxBucketSize = 16 ; // number of points per bucket
tree_ = new kdtree::KdTree(reinterpret_cast<kdtree::Vector3D*>(points_), points_num_, maxBucketSize);
}


void KdTreeSearch_ETH::add_point_cloud(PointCloud* cloud) {
points_num_ = int(cloud->n_vertices());
std::vector<vec3>& points = cloud->points();
points_ = points[0];
}


int KdTreeSearch_ETH::find_closest_point(const vec3& p) const {
kdtree::Vector3D v3d( p.x, p.y, p.z );
get_tree(tree_)->setNOfNeighbours( 1 );
Expand Down
22 changes: 4 additions & 18 deletions easy3d/kdtree/kdtree_search_eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,13 @@ namespace easy3d {
*/
class KdTreeSearch_ETH : public KdTreeSearch {
public:
KdTreeSearch_ETH();

virtual ~KdTreeSearch_ETH();

/// \name Tree construction
/// @{
/**
* \brief Begins the construction of a KdTree.
* \brief Constructor.
* \param cloud The point cloud for which a KdTree will be constructed.
*/
virtual void begin();
KdTreeSearch_ETH(const PointCloud *cloud);

/**
* \brief Sets the point cloud for which a KdTree will be constructed.
*/
virtual void add_point_cloud(PointCloud *cloud);

/**
* \brief Finalizes the construction of a KdTree.
*/
virtual void end();
/// @}
virtual ~KdTreeSearch_ETH();

/// \name Closest point query
/// @{
Expand Down
41 changes: 13 additions & 28 deletions easy3d/kdtree/kdtree_search_flann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,31 @@

namespace easy3d {

KdTreeSearch_FLANN::KdTreeSearch_FLANN() {
points_ = nullptr;
points_num_ = 0;
tree_ = nullptr;

KdTreeSearch_FLANN::KdTreeSearch_FLANN(const PointCloud *cloud) : KdTreeSearch(cloud) {
//checks_ = 32;
checks_ = flann::FLANN_CHECKS_AUTOTUNED;
}


KdTreeSearch_FLANN::~KdTreeSearch_FLANN() {
delete get_tree(tree_);
}


void KdTreeSearch_FLANN::set_checks(int chk) {
checks_ = chk;
}

void KdTreeSearch_FLANN::begin() {
delete get_tree(tree_);
tree_ = nullptr;
}

// prepare data
points_num_ = int(cloud->n_vertices());
const std::vector<vec3>& points = cloud->points();
points_ = const_cast<float *>(points[0].data());

void KdTreeSearch_FLANN::end() {
// create tree
flann::Matrix<float> dataset(points_, points_num_, 3);

// construct a single kd-tree optimized for searching lower dimensionality data
flann::Index< flann::L2<float> >* tree = new flann::Index< flann::L2<float> >(dataset, flann::KDTreeSingleIndexParams());
tree->buildIndex();

tree_ = tree;
}


void KdTreeSearch_FLANN::add_point_cloud(PointCloud* cloud) {
points_num_ = int(cloud->n_vertices());
std::vector<vec3>& points = cloud->points();
points_ = points[0];
KdTreeSearch_FLANN::~KdTreeSearch_FLANN() {
delete get_tree(tree_);
}


void KdTreeSearch_FLANN::set_checks(int chk) {
checks_ = chk;
}


Expand Down
Loading

0 comments on commit 1383eb9

Please sign in to comment.