-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from poncateam/easeKdTreeNodesChange
Simplify KdTree Node type specialization
- Loading branch information
Showing
5 changed files
with
180 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
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/. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <optional> | ||
#include <Ponca/SpatialPartitioning> | ||
#include <Eigen/Core> | ||
|
||
struct DataPoint | ||
{ | ||
enum {Dim = 3}; | ||
using Scalar = float; | ||
using VectorType = Eigen::Vector<Scalar,Dim>; | ||
inline const auto& pos() const {return m_pos;} | ||
VectorType m_pos; | ||
}; | ||
|
||
//! [CustomInnerNodeDefinition] | ||
template <typename NodeIndex, typename Scalar, int DIM, typename _AabbType = Eigen::AlignedBox<Scalar, DIM>> | ||
struct MyKdTreeInnerNode : public Ponca::KdTreeDefaultInnerNode<NodeIndex, Scalar, DIM> { | ||
using AabbType = _AabbType; | ||
AabbType m_aabb{}; | ||
}; | ||
//! [CustomInnerNodeDefinition] | ||
|
||
//! [CustomNodeDefinition] | ||
template <typename Index, typename NodeIndex, typename DataPoint, typename LeafSize = Index> | ||
struct MyKdTreeNode : Ponca::KdTreeCustomizableNode<Index, NodeIndex, DataPoint, LeafSize, | ||
MyKdTreeInnerNode<NodeIndex, typename DataPoint::Scalar, DataPoint::Dim>> { | ||
|
||
using Base = Ponca::KdTreeCustomizableNode<Index, NodeIndex, DataPoint, LeafSize, | ||
MyKdTreeInnerNode<NodeIndex, typename DataPoint::Scalar, DataPoint::Dim>>; | ||
using AabbType = typename Base::AabbType; | ||
|
||
void configure_range(Index start, Index size, const AabbType &aabb) | ||
{ | ||
Base::configure_range(start, size, aabb); | ||
if (! Base::is_leaf() ) | ||
{ | ||
Base::getAsInner().m_aabb = aabb; | ||
} | ||
} | ||
[[nodiscard]] inline std::optional<AabbType> getAabb() const { | ||
if (! Base::is_leaf()) | ||
return Base::getAsInner().m_aabb; | ||
else | ||
return std::optional<AabbType>(); | ||
} | ||
}; | ||
//! [CustomNodeDefinition] | ||
|
||
int main() | ||
{ | ||
// generate N random points | ||
constexpr int N = 1e5; | ||
std::vector<DataPoint> points(N); | ||
std::generate(points.begin(), points.end(), [](){ | ||
return DataPoint{100 * DataPoint::VectorType::Random()};}); | ||
|
||
//! [KdTreeTypeWithCustomNode] | ||
using CustomKdTree = Ponca::KdTreeBase<Ponca::KdTreeDefaultTraits<DataPoint,MyKdTreeNode>>; | ||
//! [KdTreeTypeWithCustomNode] | ||
|
||
// build the k-d tree | ||
const CustomKdTree kdtree(points); | ||
|
||
// neighbor searches are done below from these arbitrary index and point | ||
const int query_idx = 10; | ||
const DataPoint::VectorType query_pt{-10.0, 0.5, 75.0}; | ||
|
||
// | ||
// nearest neighbor search | ||
// | ||
std::cout << "the nearest neighbor of the point at index " << query_idx << " is at index " | ||
<< *kdtree.nearest_neighbor(query_idx).begin() << std::endl; | ||
std::cout << "the nearest neighbor of the point (" << query_pt.transpose() << ") is at index " | ||
<< *kdtree.nearest_neighbor(query_pt).begin() << std::endl; | ||
|
||
//! [ReadCustomProperties] | ||
auto bbox = kdtree.node_data()[0].getAabb(); | ||
if (bbox) { | ||
std::cout << "Root bounding box is as follows: \n" | ||
<< " Center: " << bbox->center() | ||
<< " Diagonal: " << bbox->diagonal() | ||
<< std::endl; | ||
} | ||
//! [ReadCustomProperties] | ||
|
||
return 1; | ||
} |