Skip to content

Commit

Permalink
[SpatialPartitioning] Add KdTreeTraits and refactor KnnGraph (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmellado committed Jul 27, 2023
1 parent 6f4d7be commit 8eaae82
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 13 deletions.
58 changes: 45 additions & 13 deletions Ponca/src/SpatialPartitioning/KnnGraph/KnnGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,64 @@

#pragma once

#include "./knnGraphTraits.h"

#include "Query/knnGraphQuery.h"
#include "Query/knnGraphRangeQuery.h"

#include <memory>

namespace Ponca {

class KdTree;
template <typename Traits> class KnnGraphBase;

/*!
* \brief Public interface for KnnGraph datastructure.
*
* Provides default implementation of the KnnGraph
*
* \see KnnGraphDefaultTraits for the default trait interface documentation.
* \see KnnGraphBase for complete API
*/
template <typename DataPoint>
using KnnGraph = KnnGraphBase<KnnGraphDefaultTraits<DataPoint>>;

class KnnGraph
class KdTree;
/*!
* \brief Customizable base class for KnnGraph datastructure
*
* \see Ponca::KnGraph
*
* \tparam Traits Traits type providing the types and constants used by the KnnGraph. Must have the
* same interface as the default traits type.
*
* \see KnnGraphDefaultTraits for the trait interface documentation.
*
*/
template <typename Traits> class KnnGraphBase
{
// Types -------------------------------------------------------------------
public:
using DataPoint = typename Traits::DataPoint; ///< DataPoint given by user via Traits
using Scalar = typename DataPoint::Scalar; ///< Scalar given by user via DataPoint
using VectorType = typename DataPoint::VectorType; ///< VectorType given by user via DataPoint

using IndexType = typename Traits::IndexType;
using PointContainer = typename Traits::PointContainer; ///< Container for DataPoint used inside the KdTree
using IndexContainer = typename Traits::IndexContainer; ///< Container for indices used inside the KdTree

using KNearestIndexQuery = KnnGraphQuery;
using RangeIndexQuery = KnnGraphRangeQuery;

// knnGraph ----------------------------------------------------------------
public:
KnnGraph();
KnnGraph(int k);
KnnGraphBase();
KnnGraphBase(int k);

void clear();
void build(const KdTree& kdtree);
void build(const KdTree& kdtree, int k);
void build(const KdTree& kdtree, const std::vector<int>& indices);
void build(const KdTree& kdtree, int k, const std::vector<int>& indices);
void build(const KdTree& kdtree, const IndexContainer& indices);
void build(const KdTree& kdtree, int k, const IndexContainer& indices);


// Query -------------------------------------------------------------------
Expand All @@ -50,19 +82,19 @@ class KnnGraph
int k() const;
int size() const;

const Vector3Array& point_data() const;
Vector3Array& point_data();
const PointContainer& point_data() const;
PointContainer& point_data();

const std::vector<int>& index_data() const;
std::vector<int>& index_data();
const IndexContainer& index_data() const;
IndexContainer& index_data();

void set_verbose(bool verbose = true);

// Data --------------------------------------------------------------------
protected:
int m_k;
std::shared_ptr<Vector3Array> m_points;
std::shared_ptr<std::vector<int>> m_indices;
std::shared_ptr<PointContainer> m_points;
std::shared_ptr<IndexContainer> m_indices;

bool m_verbose;
};
Expand Down
48 changes: 48 additions & 0 deletions Ponca/src/SpatialPartitioning/KnnGraph/knnGraphTraits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/


#pragma once

#include <Eigen/Geometry>

namespace Ponca {

/*!
* \brief The default traits type used by the kd-tree.
*/
template <typename _DataPoint>
struct KnnGraphDefaultTraits
{
/*!
* \brief The type used to store point data.
*
* Must provide `Scalar` and `VectorType` typedefs.
*
* `VectorType` must provide a `squaredNorm()` function returning a `Scalar`, as well as a
* `maxCoeff(int*)` function returning the dimension index of its largest scalar in its output
* parameter (e.g. 0 for *x*, 1 for *y*, etc.).
*/
using DataPoint = _DataPoint;

private:
using Scalar = typename DataPoint::Scalar;
using VectorType = typename DataPoint::VectorType;

public:
/*!
* \brief The type used to calculate node bounding boxes.
*
* Must provide `min()`, `max()`, and `center()` functions, all returning a `VectorType`.
*/
using AabbType = Eigen::AlignedBox<Scalar, DataPoint::Dim>;

// Containers
using IndexType = int;
using PointContainer = std::vector<DataPoint>;
using IndexContainer = std::vector<IndexType>;
};
} // namespace Ponca

0 comments on commit 8eaae82

Please sign in to comment.