Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SpatialPartitioning] Change the API of kd-tree accessors #116

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class KdTreeKNearestQueryBase : public KdTreeQuery<Traits>, public QueryType

protected:
inline void search(){
KdTreeQuery<Traits>::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->point_data()),
KdTreeQuery<Traits>::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->points()),
[](IndexType, IndexType){},
[this](){return QueryType::descentDistanceThreshold();},
[this](IndexType idx){return QueryType::skipIndexFunctor(idx);},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class KdTreeNearestQueryBase : public KdTreeQuery<Traits>, public QueryType

protected:
inline void search(){
KdTreeQuery<Traits>::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->point_data()),
KdTreeQuery<Traits>::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->points()),
[](IndexType, IndexType){},
[this](){return QueryType::descentDistanceThreshold();},
[this](IndexType idx){return QueryType::skipIndexFunctor(idx);},
Expand Down
6 changes: 3 additions & 3 deletions Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class KdTreeQuery
ProcessNeighborFunctor processNeighborFunctor
)
{
const auto& nodes = m_kdtree->node_data();
const auto& points = m_kdtree->point_data();
const auto& indices = m_kdtree->index_data();
const auto& nodes = m_kdtree->nodes();
const auto& points = m_kdtree->points();
const auto& indices = m_kdtree->sample_indices();

if (nodes.empty() || points.empty() || indices.empty())
throw std::invalid_argument("Empty KdTree");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class KdTreeRangeQueryBase : public KdTreeQuery<Traits>, public QueryType

protected:
inline void advance(Iterator& it){
const auto& points = QueryAccelType::m_kdtree->point_data();
const auto& indices = QueryAccelType::m_kdtree->index_data();
const auto& points = QueryAccelType::m_kdtree->points();
const auto& indices = QueryAccelType::m_kdtree->sample_indices();
const auto& point = QueryType::getInputPosition(points);

auto descentDistanceThreshold = [this](){return QueryType::descentDistanceThreshold();};
Expand Down
33 changes: 16 additions & 17 deletions Ponca/src/SpatialPartitioning/KdTree/kdTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class KdTreeBase

static_assert(std::is_same<typename PointContainer::value_type, DataPoint>::value,
"PointContainer must contain DataPoints");

// Queries use a value of -1 for invalid indices
static_assert(std::is_signed<IndexType>::value, "Index type must be signed");

Expand Down Expand Up @@ -195,52 +195,52 @@ class KdTreeBase

// Accessors ---------------------------------------------------------------
public:
/// \brief The number of nodes in the kd-tree (including leaves).
inline NodeIndexType node_count() const
{
return m_nodes.size();
}

inline IndexType index_count() const
/// \brief The number of points sampled by the kd-tree.
inline IndexType sample_count() const
{
return (IndexType)m_indices.size();
}

/// \brief The number of points in the base point container.
inline IndexType point_count() const
{
return (IndexType)m_points.size();
}

/// \brief The number of leaf nodes in the kd-tree.
inline NodeIndexType leaf_count() const
{
return m_leaf_count;
}

inline PointContainer& point_data()
/// \brief The base points the kd-tree samples into.
/// \warning Changing the order of points will invalidate the kd-tree.
inline PointContainer& points()
{
return m_points;
};

inline const PointContainer& point_data() const
/// \brief The base points the kd-tree samples into.
inline const PointContainer& points() const
{
return m_points;
};

inline const NodeContainer& node_data() const
{
return m_nodes;
}

inline NodeContainer& node_data()
/// \brief The flat container of nodes used internally by the kd-tree.
inline const NodeContainer& nodes() const
{
return m_nodes;
}

inline const IndexContainer& index_data() const
{
return m_indices;
}

inline IndexContainer& index_data()
/// \brief The indices of the points sampled by the kd-tree.
/// \note The order will vary depending on the geometry of the points.
inline const IndexContainer& sample_indices() const
{
return m_indices;
}
Expand Down Expand Up @@ -296,7 +296,6 @@ public :
{
return KdTreeRangeIndexQuery<Traits>(this, r, index);
}


// Data --------------------------------------------------------------------
protected:
Expand Down
24 changes: 12 additions & 12 deletions Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ inline void KdTreeBase<Traits>::buildWithSampling(PointUserContainer&& points,

m_indices = std::move(sampling);

this->build_rec(0, 0, index_count(), 1);
this->build_rec(0, 0, sample_count(), 1);

PONCA_DEBUG_ASSERT(this->valid());
}
Expand All @@ -58,7 +58,7 @@ inline void KdTreeBase<Traits>::rebuild(IndexUserContainer sampling)

m_indices = std::move(sampling);

this->build_rec(0, 0, index_count(), 1);
this->build_rec(0, 0, sample_count(), 1);

PONCA_DEBUG_ASSERT(this->valid());
}
Expand All @@ -71,19 +71,19 @@ bool KdTreeBase<Traits>::valid() const

if (m_points.empty())
return m_nodes.empty() && m_indices.empty();

if(m_nodes.empty() || m_indices.empty())
{
PONCA_DEBUG_ERROR;
return false;
}
if(point_count() < index_count())

if(point_count() < sample_count())
{
PONCA_DEBUG_ERROR;
return false;
}

std::vector<bool> b(point_count(), false);
for(IndexType idx : m_indices)
{
Expand All @@ -100,7 +100,7 @@ bool KdTreeBase<Traits>::valid() const
const NodeType& node = m_nodes.operator[](n);
if(node.is_leaf())
{
if(index_count() <= node.leaf_start() || index_count() < node.leaf_start()+node.leaf_size())
if(sample_count() <= node.leaf_start() || sample_count() < node.leaf_start()+node.leaf_size())
{
PONCA_DEBUG_ERROR;
return false;
Expand Down Expand Up @@ -128,10 +128,10 @@ template<typename Traits>
std::string KdTreeBase<Traits>::to_string() const
{
if (m_indices.empty()) return "";

std::stringstream str;
str << "indices (" << index_count() << ") :\n";
for(IndexType i=0; i<index_count(); ++i)
str << "indices (" << sample_count() << ") :\n";
for(IndexType i=0; i<sample_count(); ++i)
{
str << " " << i << ": " << m_indices.operator[](i) << "\n";
}
Expand Down Expand Up @@ -192,13 +192,13 @@ auto KdTreeBase<Traits>::partition(IndexType start, IndexType end, int dim, Scal
{
const auto& points = m_points;
auto& indices = m_indices;

auto it = std::partition(indices.begin()+start, indices.begin()+end, [&](IndexType i)
{
return points[i].pos()[dim] < value;
});

auto distance = std::distance(m_indices.begin(), it);

return static_cast<IndexType>(distance);
}
6 changes: 3 additions & 3 deletions Ponca/src/SpatialPartitioning/KnnGraph/knnGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ template <typename Traits> class KnnGraphBase
/// \warning KdTreeTraits compatibility is checked with static assertion
template<typename KdTreeTraits>
inline KnnGraphBase(const KdTreeBase<KdTreeTraits>& kdtree, int k = 6)
: m_k(std::min(k,kdtree.index_count()-1)),
m_kdTreePoints(kdtree.point_data())
: m_k(std::min(k,kdtree.sample_count()-1)),
m_kdTreePoints(kdtree.points())
{
static_assert( std::is_same<typename Traits::DataPoint, typename KdTreeTraits::DataPoint>::value,
"KdTreeTraits::DataPoint is not equal to Traits::DataPoint" );
Expand All @@ -85,7 +85,7 @@ template <typename Traits> class KnnGraphBase
// \fixme Update API to properly handle kdtree subsampling
const int cloudSize = kdtree.point_count();
{
const int samplesSize = kdtree.index_count();
const int samplesSize = kdtree.sample_count();
eigen_assert(cloudSize == samplesSize);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/ponca_basic_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void test_fit(Fit& _fit, const KdTree<MyPoint>& tree, const VectorType& _p)
// Iterate over samples and _fit the primitive
for(int i : tree.range_neighbors(_p, tmax) )
{
_fit.addNeighbor( tree.point_data()[i] );
_fit.addNeighbor( tree.points()[i] );
}

//finalize fitting
Expand Down
4 changes: 2 additions & 2 deletions tests/src/basket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void testBasicFunctionalities(const KdTree<typename Fit::DataPoint>& tree, typen
typedef typename DataPoint::VectorType VectorType;
typedef typename Fit::WFunctor WeightFunc;

const auto& vectorPoints = tree.point_data();
const auto& vectorPoints = tree.points();

// Test for each point if the fitted sphere correspond to the theoretical sphere
#ifdef NDEBUG
Expand Down Expand Up @@ -135,7 +135,7 @@ void testIsSame(const KdTree<typename Fit1::DataPoint>& tree,
typedef typename Fit1::Scalar Scalar;
typedef typename Fit1::VectorType VectorType;
typedef typename Fit1::WFunctor WeightFunc;
const auto& vectorPoints = tree.point_data();
const auto& vectorPoints = tree.points();

// Test for each point if the fitted sphere correspond to the theoretical sphere
#ifdef NDEBUG
Expand Down
Loading