Medusa  1.1
Coordinate Free Mehless Method implementation
KDGrid_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_SPATIAL_SEARCH_KDGRID_FWD_HPP_
2 #define MEDUSA_BITS_SPATIAL_SEARCH_KDGRID_FWD_HPP_
3 
11 #include "Grid_fwd.hpp"
12 #include <iosfwd>
13 #include <vector>
14 
15 namespace mm {
16 
28 template <typename vec_t>
29 class KDGrid {
30  public:
31  typedef vec_t vector_t;
32  typedef typename vector_t::scalar_t scalar_t;
33  enum { dim = vec_t::dim };
35 
36  private:
37  vec_t bot_;
38  vec_t top_;
41  std::vector<vec_t> points_;
43 
44  public:
46  KDGrid(const vec_t& bot, const vec_t& top, scalar_t cell_size) :
47  bot_(bot), top_(top), cell_size_(cell_size),
48  grid_(compute_size(bot, top, cell_size), -1),
49  points_() {}
50 
52  const vec_t& bottom() const { return bot_; }
54  const vec_t& top() const { return top_; }
56  scalar_t cellSize() const { return cell_size_; }
58  const Grid<int, dim>& grid() const { return grid_; }
60  const std::vector<vec_t>& points() const { return points_; }
62  const vec_t& point(int idx) const { return points_[idx]; }
63 
65  int size() const { return points_.size(); }
66 
73  int insert(const vec_t& p) { return insert(p, compute_index(p)); }
74 
76  void insert(const std::vector<vec_t>& pts) {
77  for (const vec_t& p : pts) insert(p);
78  }
79 
81  bool existsPointInSphere(const vec_t& p, scalar_t r) {
82  return existsPointInSphere(p, r, compute_index(p));
83  }
84 
86  template <typename V>
87  friend std::ostream& operator<<(std::ostream& os, const KDGrid<V>& search);
88 
89  private:
91  static IndexArray compute_size(const vec_t& bot, const vec_t& top, scalar_t cell_size);
92 
94  IndexArray compute_index(const vec_t& p);
95 
97  bool existsPointInSphere(const vec_t& p, scalar_t r, const IndexArray& index);
98 
100  int insert(const vec_t& p, const IndexArray& index);
101 
103  bool increment(IndexArray& cur, int span, const IndexArray& ref);
104 };
105 
106 } // namespace mm
107 
108 #endif // MEDUSA_BITS_SPATIAL_SEARCH_KDGRID_FWD_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
scalar_t
Scalar scalar_t
Type of the elements, alias of Scalar.
Definition: MatrixBaseAddons.hpp:16
mm::KDGrid::grid
const Grid< int, dim > & grid() const
Get access to underlying grid of point indices.
Definition: KDGrid_fwd.hpp:58
mm::KDGrid::size
int size() const
Get number of points stored in the structure.
Definition: KDGrid_fwd.hpp:65
mm::KDGrid::bottom
const vec_t & bottom() const
Get bottom bound.
Definition: KDGrid_fwd.hpp:52
mm::KDGrid::existsPointInSphere
bool existsPointInSphere(const vec_t &p, scalar_t r)
Check if any point exists in sphere centered at p with radius r.
Definition: KDGrid_fwd.hpp:81
Grid_fwd.hpp
dim
@ dim
Number of elements of this matrix.
Definition: MatrixBaseAddons.hpp:14
mm::KDGrid::compute_index
IndexArray compute_index(const vec_t &p)
Compute point multi-index.
Definition: KDGrid.hpp:70
mm::KDGrid::top_
vec_t top_
Upper bound of the box.
Definition: KDGrid_fwd.hpp:38
mm::KDGrid::dim
@ dim
Dimensionality of the space.
Definition: KDGrid_fwd.hpp:34
mm::KDGrid::insert
void insert(const std::vector< vec_t > &pts)
Vectorised version of insert.
Definition: KDGrid_fwd.hpp:76
mm::KDGrid::IndexArray
Grid< int, dim >::IndexArray IndexArray
Multi-index type.
Definition: KDGrid_fwd.hpp:42
mm::KDGrid::top
const vec_t & top() const
Get top bound.
Definition: KDGrid_fwd.hpp:54
mm::KDGrid::points
const std::vector< vec_t > & points() const
Get access to array of stored points.
Definition: KDGrid_fwd.hpp:60
mm::Grid< int, dim >
mm::KDGrid::bot_
vec_t bot_
Lower bound of the box.
Definition: KDGrid_fwd.hpp:37
mm::KDGrid::vector_t
vec_t vector_t
Vector type used.
Definition: KDGrid_fwd.hpp:31
mm::KDGrid::point
const vec_t & point(int idx) const
Get point by its sequential index.
Definition: KDGrid_fwd.hpp:62
mm::KDGrid
Search structure over given d-dimensional box with given cell size.
Definition: KDGrid_fwd.hpp:29
mm::KDGrid::cellSize
scalar_t cellSize() const
Get cell size.
Definition: KDGrid_fwd.hpp:56
mm::KDGrid::operator<<
friend std::ostream & operator<<(std::ostream &os, const KDGrid< V > &search)
Output some information about the search grid.
mm::KDGrid::compute_size
static IndexArray compute_size(const vec_t &bot, const vec_t &top, scalar_t cell_size)
Compute the sizes of the rid along each dimension.
Definition: KDGrid.hpp:19
mm::KDGrid::scalar_t
vector_t::scalar_t scalar_t
Scalar type used.
Definition: KDGrid_fwd.hpp:32
mm::KDGrid::cell_size_
scalar_t cell_size_
Size of a single cell.
Definition: KDGrid_fwd.hpp:39
mm::KDGrid::KDGrid
KDGrid(const vec_t &bot, const vec_t &top, scalar_t cell_size)
Construct a search grid from bot to top with given cell_size.
Definition: KDGrid_fwd.hpp:46
mm::KDGrid::insert
int insert(const vec_t &p)
Insert a new point in the structure.
Definition: KDGrid_fwd.hpp:73
mm::KDGrid::points_
std::vector< vec_t > points_
List of inserted points.
Definition: KDGrid_fwd.hpp:41
mm::KDGrid::grid_
Grid< int, dim > grid_
Background grid of cells pointing to sequential point indices.
Definition: KDGrid_fwd.hpp:40
mm::KDGrid::increment
bool increment(IndexArray &cur, int span, const IndexArray &ref)
Increment a dim-ary counter.
Definition: KDGrid.hpp:29