diff --git a/Ponca/src/SpatialPartitioning/KnnGraph/KnnGraph.h b/Ponca/src/SpatialPartitioning/KnnGraph/KnnGraph.h index e1687af62..6659cd35c 100644 --- a/Ponca/src/SpatialPartitioning/KnnGraph/KnnGraph.h +++ b/Ponca/src/SpatialPartitioning/KnnGraph/KnnGraph.h @@ -6,6 +6,8 @@ #pragma once +#include "./knnGraphTraits.h" + #include "Query/knnGraphQuery.h" #include "Query/knnGraphRangeQuery.h" @@ -13,25 +15,55 @@ namespace Ponca { -class KdTree; +template 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 +using KnnGraph = KnnGraphBase>; -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 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& indices); - void build(const KdTree& kdtree, int k, const std::vector& indices); + void build(const KdTree& kdtree, const IndexContainer& indices); + void build(const KdTree& kdtree, int k, const IndexContainer& indices); // Query ------------------------------------------------------------------- @@ -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& index_data() const; - std::vector& index_data(); + const IndexContainer& index_data() const; + IndexContainer& index_data(); void set_verbose(bool verbose = true); // Data -------------------------------------------------------------------- protected: int m_k; - std::shared_ptr m_points; - std::shared_ptr> m_indices; + std::shared_ptr m_points; + std::shared_ptr m_indices; bool m_verbose; }; diff --git a/Ponca/src/SpatialPartitioning/KnnGraph/knnGraphTraits.h b/Ponca/src/SpatialPartitioning/KnnGraph/knnGraphTraits.h new file mode 100644 index 000000000..468bd3857 --- /dev/null +++ b/Ponca/src/SpatialPartitioning/KnnGraph/knnGraphTraits.h @@ -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 + +namespace Ponca { + +/*! + * \brief The default traits type used by the kd-tree. + */ +template +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; + + // Containers + using IndexType = int; + using PointContainer = std::vector; + using IndexContainer = std::vector; +}; +} // namespace Ponca