Commit b0e61e70 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

kdtree structures currected

parent d5795006
......@@ -42,11 +42,6 @@ using namespace AMDiS;
namespace experimental {
// =====================================================================================
typedef std::vector<WorldVector<double> > my_vector_of_vectors_t;
// =====================================================================================
/** A simple vector-of-vectors adaptor for nanoflann, without duplicating the storage.
* The i'th vector represents a point in the state space.
*
......@@ -140,7 +135,7 @@ struct KDTreeVectorOfWorldVectorsAdaptor
}; // end of KDTreeVectorOfVectorsAdaptor
typedef KDTreeVectorOfWorldVectorsAdaptor< my_vector_of_vectors_t, double > KD_Tree;
typedef KDTreeVectorOfWorldVectorsAdaptor<std::vector<WorldVector<double> >, double > KD_Tree;
} // end namespace experimental
......
......@@ -42,12 +42,6 @@ using namespace AMDiS;
namespace experimental {
// =====================================================================================
typedef WorldVector<double> PointType;
typedef DOFVector<PointType> VectorOfVectors_Dof;
// =====================================================================================
/** A simple vector-of-vectors adaptor for nanoflann, without duplicating the storage.
* The i'th vector represents a point in the state space.
*
......@@ -168,7 +162,7 @@ typedef DOFVector<PointType> VectorOfVectors_Dof;
* == evalAtPoint_simple
**/
template<typename T>
T evalAtPoint(const DOFVector<T>& vec, const PointType& x, int strategy = 0, int nrOfPoints = -1)
T evalAtPoint(const DOFVector<T>& vec, const WorldVector<double>& x, int strategy = 0, int nrOfPoints = -1)
{
const size_t nnp = 1;
std::vector<DegreeOfFreedom> ret_indexes(nnp);
......@@ -187,7 +181,7 @@ typedef DOFVector<PointType> VectorOfVectors_Dof;
}; // end of KDTreeDOFVectorOfWorldVectorsAdaptor
typedef KDTreeDOFVectorOfWorldVectorsAdaptor< VectorOfVectors_Dof, double > KD_Tree_Dof;
typedef KDTreeDOFVectorOfWorldVectorsAdaptor<DOFVector<WorldVector<double> >, double > KD_Tree_Dof;
static std::map<const FiniteElemSpace*, std::pair<int, KD_Tree_Dof*> > kdtreeMap_Dof;
inline KD_Tree_Dof* provideKDTree(const FiniteElemSpace* feSpace)
......
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2011 Jose Luis Blanco (joseluisblancoc@gmail.com).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
/** \file kdtree_nanoflann_double.h */
#ifndef KDTREE_NANOFLANN_DOUBLE_H
#define KDTREE_NANOFLANN_DOUBLE_H
#include <nanoflann.hpp>
#include <cstdlib>
#include <iostream>
using namespace nanoflann;
using namespace AMDiS;
namespace experimental {
/** A simple vector-of-vectors adaptor for nanoflann, without duplicating the storage.
* The i'th vector represents a point in the state space.
*
* \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality for the points in the data set, allowing more compiler optimizations.
* \tparam value_type The type of the point coordinates (typically, double or float).
* \tparam Distance The distance metric to use: nanoflann::metric_L1, nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc.
* \tparam index_type The type for indices in the KD-tree index (typically, size_t of int)
*/
template <class VectorOfVectorsType,
typename value_type = double,
int DIM = -1,
class Distance = nanoflann::metric_L2_Simple,
typename index_type = size_t>
struct KDTreeVectorOfDoublesAdaptor
{
typedef KDTreeVectorOfDoublesAdaptor<VectorOfVectorsType, value_type, DIM, Distance, index_type> self_t;
typedef typename Distance::template traits<value_type, self_t>::distance_t metric_t;
typedef KDTreeSingleIndexAdaptor< metric_t, self_t, DIM, index_type> index_t;
index_t* index; //! The kd-tree index for the user to call its methods as usual with any other FLANN index.
/// Constructor: takes a const ref to the vector of vectors object with the data points
KDTreeVectorOfDoublesAdaptor(const int dimensionality, const VectorOfVectorsType &mat, const int leaf_max_size = 10) : m_data(mat)
{
assert(mat.size()!=0);
const size_t dims = 1;
if (DIM>0 && static_cast<int>(dims)!=DIM)
throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument");
index = new index_t( dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size, dims ) );
index->buildIndex();
}
~KDTreeVectorOfDoublesAdaptor() {
delete index;
}
const VectorOfVectorsType &m_data;
/** Query for the \a num_closest closest points to a given point (entered as query_point[0:dim-1]).
* Note that this is a short-cut method for index->findNeighbors().
* The user can also call index->... methods as desired.
* \note nChecks_IGNORED is ignored but kept for compatibility with the original FLANN interface.
*/
inline void query(const value_type *query_point, const size_t num_closest, index_type *out_indices, value_type *out_distances_sq, const int nChecks_IGNORED = 10) const
{
nanoflann::KNNResultSet<typename VectorOfVectorsType::Scalar,index_type> resultSet(num_closest);
resultSet.init(out_indices, out_distances_sq);
index->findNeighbors(resultSet, query_point, nanoflann::SearchParams());
}
/** @name Interface expected by KDTreeSingleIndexAdaptor
* @{ */
const self_t & derived() const {
return *this;
}
self_t & derived() {
return *this;
}
// Must return the number of data points
inline size_t kdtree_get_point_count() const {
return m_data.size();
}
// Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored in the class:
inline value_type kdtree_distance(const value_type *p1, const size_t idx_p2,size_t size) const
{
return sqr(p1[0]-m_data[idx_p2]);
}
// Returns the dim'th component of the idx'th point in the class:
inline value_type kdtree_get_pt(const size_t idx, int dim) const {
return m_data[idx];
}
// Optional bounding-box computation: return false to default to a standard bbox computation loop.
// Return true if the BBOX was already computed by the class and returned in "bb" so it can be avoided to redo it again.
// Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)
template <class BBOX>
bool kdtree_get_bbox(BBOX &bb) const {
return false;
}
/** @} */
}; // end of KDTreeVectorOfVectorsAdaptor
typedef KDTreeVectorOfDoublesAdaptor<std::vector<double>, double > KD_Tree_Double;
} // end namespace experimental
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment