Skip to content

Commit

Permalink
Update KDTreeArmadilloAdaptor.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ManosPapadakis95 committed Dec 4, 2023
1 parent b46a7c6 commit 0273aba
Showing 1 changed file with 96 additions and 96 deletions.
192 changes: 96 additions & 96 deletions inst/include/internal/KDTreeArmadilloAdaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
using namespace arma;
using namespace nanoflann;


namespace Rnanoflann {
namespace Rnanoflann
{
// Define an Armadillo KDTreeAdaptor class
template <class MatrixType, class Distance, int DIM = -1>
struct KDTreeArmadilloAdaptor
{
using self_t = KDTreeArmadilloAdaptor<MatrixType, Distance, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;
using metric_t = typename Distance::template traits<num_t, self_t>::distance_t;// You can change the distance metric as needed.
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
index_t* index_;

index_t *index_;

const std::reference_wrapper<const MatrixType> m_data_matrix;
explicit KDTreeArmadilloAdaptor(const uword dimensionality, const std::reference_wrapper<const MatrixType>& mat,const uword leafs = 10)

explicit KDTreeArmadilloAdaptor(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const uword leafs = 10)
: m_data_matrix(mat)
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
Expand All @@ -35,51 +35,51 @@ namespace Rnanoflann {
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor() { delete index_; }
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim,idx);
return m_data_matrix.get()(dim, idx);
}

colvec col(uword idx) const {
colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

template <class BBOX>
bool kdtree_get_bbox(BBOX& bb) const
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
};


// Define an Armadillo KDTreeAdaptor class
template <class MatrixType, class Distance, bool Square, int DIM = -1>
struct KDTreeArmadilloAdaptor2
{
using self_t = KDTreeArmadilloAdaptor2<MatrixType, Distance, Square, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;
using metric_t = typename Distance::template traits<num_t, self_t, Square>::distance_t;// You can change the distance metric as needed.
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t, Square>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
index_t* index_;

index_t *index_;

const std::reference_wrapper<const MatrixType> m_data_matrix;
explicit KDTreeArmadilloAdaptor2(const uword dimensionality, const std::reference_wrapper<const MatrixType>& mat,const uword leafs = 10)

explicit KDTreeArmadilloAdaptor2(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const uword leafs = 10)
: m_data_matrix(mat)
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
Expand All @@ -90,28 +90,29 @@ namespace Rnanoflann {
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor2() { delete index_; }
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

colvec col(uword idx) const {
colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim,idx);
return m_data_matrix.get()(dim, idx);
}

template <class BBOX>
bool kdtree_get_bbox(BBOX& bb) const
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
Expand All @@ -122,21 +123,21 @@ namespace Rnanoflann {
{
using self_t = KDTreeArmadilloAdaptor3<MatrixType, Distance, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;
using metric_t = typename Distance::template traits<num_t, self_t>::distance_t;// You can change the distance metric as needed.
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
index_t* index_;

index_t *index_;

const double p;
const double p_1;

const std::reference_wrapper<const MatrixType> m_data_matrix;
explicit KDTreeArmadilloAdaptor3(const uword dimensionality, const std::reference_wrapper<const MatrixType>& mat, const double p,const uword leafs = 10)
: p(p), p_1(1.0/p), m_data_matrix(mat)

explicit KDTreeArmadilloAdaptor3(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const double p, const uword leafs = 10)
: p(p), p_1(1.0 / p), m_data_matrix(mat)
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
if (static_cast<uword>(dims) != dimensionality)
Expand All @@ -146,61 +147,64 @@ namespace Rnanoflann {
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor3() { delete index_; }
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim,idx);
return m_data_matrix.get()(dim, idx);
}

colvec col(uword idx) const {
colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

const double getP() const {
const double getP() const
{
return p;
}

const double getP_1() const {
const double getP_1() const
{
return p_1;
}

template <class BBOX>
bool kdtree_get_bbox(BBOX& bb) const
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
};

// Define an Armadillo KDTreeAdaptor class
// Define an Armadillo KDTreeAdaptor class
template <class MatrixType, class Distance, int DIM = -1>
struct KDTreeArmadilloAdaptor4
{
using self_t = KDTreeArmadilloAdaptor4<MatrixType, Distance, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;
using metric_t = typename Distance::template traits<num_t, self_t>::distance_t;// You can change the distance metric as needed.
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
index_t* index_;

index_t *index_;

const std::reference_wrapper<const MatrixType> m_data_matrix;
const MatrixType xlogx,ylogy;
const MatrixType xlogx, ylogy;
const num_t *begin_points;
explicit KDTreeArmadilloAdaptor4(const uword dimensionality, const std::reference_wrapper<const MatrixType>& mat, const MatrixType & points,const uword leafs = 10)
: m_data_matrix(mat), xlogx(mat.get()%arma::log(mat.get())), ylogy(points%arma::log(points)), begin_points(points.memptr())

explicit KDTreeArmadilloAdaptor4(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const MatrixType &points, const uword leafs = 10)
: m_data_matrix(mat), xlogx(mat.get() % arma::log(mat.get())), ylogy(points % arma::log(points)), begin_points(points.memptr())
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
if (static_cast<uword>(dims) != dimensionality)
Expand All @@ -210,51 +214,47 @@ namespace Rnanoflann {
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor4() { delete index_; }
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim,idx);
return m_data_matrix.get()(dim, idx);
}

colvec col(uword idx) const {
colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

colvec col_xlogx(uword idx) const {
colvec col_xlogx(uword idx) const
{
return xlogx.col(idx);
}

colvec col_ylogy(const num_t * a) const {
auto index = std::floor((a-begin_points)/ylogy.n_rows);
colvec col_ylogy(const num_t *a) const
{
auto index = std::floor((a - begin_points) / ylogy.n_rows);
return ylogy.col(index);
}

double log0_5() const {
double log0_5() const
{
return std::log(0.5);
}

uword colNumber(uword idx) const {
return ylogy.col(idx);
}

template <class BBOX>
bool kdtree_get_bbox(BBOX& bb) const
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
};




};

0 comments on commit 0273aba

Please sign in to comment.