mlpack  master
octree.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_TREE_OCTREE_OCTREE_HPP
13 #define MLPACK_CORE_TREE_OCTREE_OCTREE_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include "../hrectbound.hpp"
17 #include "../statistic.hpp"
18 
19 namespace mlpack {
20 namespace tree {
21 
22 template<typename MetricType = metric::EuclideanDistance,
23  typename StatisticType = EmptyStatistic,
24  typename MatType = arma::mat>
25 class Octree
26 {
27  public:
29  typedef MatType Mat;
31  typedef typename MatType::elem_type ElemType;
32 
34  template<typename RuleType>
36 
38  template<typename RuleType>
40 
41  private:
43  std::vector<Octree*> children;
44 
47  size_t begin;
50  size_t count;
55  MatType* dataset;
59  StatisticType stat;
61  ElemType parentDistance;
65  MetricType metric;
66 
67  public:
76  Octree(const MatType& data, const size_t maxLeafSize = 20);
77 
90  Octree(const MatType& data,
91  std::vector<size_t>& oldFromNew,
92  const size_t maxLeafSize = 20);
93 
109  Octree(const MatType& data,
110  std::vector<size_t>& oldFromNew,
111  std::vector<size_t>& newFromOld,
112  const size_t maxLeafSize = 20);
113 
122  Octree(MatType&& data, const size_t maxLeafSize = 20);
123 
136  Octree(MatType&& data,
137  std::vector<size_t>& oldFromNew,
138  const size_t maxLeafSize = 20);
139 
155  Octree(MatType&& data,
156  std::vector<size_t>& oldFromNew,
157  std::vector<size_t>& newFromOld,
158  const size_t maxLeafSize = 20);
159 
173  Octree(Octree* parent,
174  const size_t begin,
175  const size_t count,
176  const arma::vec& center,
177  const double width,
178  const size_t maxLeafSize = 20);
179 
200  Octree(Octree* parent,
201  const size_t begin,
202  const size_t count,
203  std::vector<size_t>& oldFromNew,
204  const arma::vec& center,
205  const double width,
206  const size_t maxLeafSize = 20);
207 
213  Octree(const Octree& other);
214 
221  Octree(Octree&& other);
222 
228  template<typename Archive>
229  Octree(
230  Archive& ar,
232 
236  ~Octree();
237 
239  const MatType& Dataset() const { return *dataset; }
240 
242  Octree* Parent() const { return parent; }
244  Octree*& Parent() { return parent; }
245 
247  const bound::HRectBound<MetricType>& Bound() const { return bound; }
250 
252  const StatisticType& Stat() const { return stat; }
254  StatisticType& Stat() { return stat; }
255 
257  size_t NumChildren() const;
258 
260  MetricType Metric() const { return MetricType(); }
261 
266  template<typename VecType>
267  size_t GetNearestChild(
268  const VecType& point,
269  typename std::enable_if_t<IsVector<VecType>::value>* = 0) const;
270 
275  template<typename VecType>
276  size_t GetFurthestChild(
277  const VecType& point,
278  typename std::enable_if_t<IsVector<VecType>::value>* = 0) const;
279 
283  bool IsLeaf() const { return NumChildren() == 0; }
284 
289  size_t GetNearestChild(const Octree& queryNode) const;
290 
295  size_t GetFurthestChild(const Octree& queryNode) const;
296 
301  ElemType FurthestPointDistance() const;
302 
310  ElemType FurthestDescendantDistance() const;
311 
313  ElemType MinimumBoundDistance() const;
314 
317  ElemType ParentDistance() const { return parentDistance; }
320  ElemType& ParentDistance() { return parentDistance; }
321 
326  const Octree& Child(const size_t child) const { return *children[child]; }
327 
332  Octree& Child(const size_t child) { return *children[child]; }
333 
338  Octree*& ChildPtr(const size_t child) { return children[child]; }
339 
341  size_t NumPoints() const;
342 
344  size_t NumDescendants() const;
345 
350  size_t Descendant(const size_t index) const;
351 
357  size_t Point(const size_t index) const;
358 
360  ElemType MinDistance(const Octree& other) const;
362  ElemType MaxDistance(const Octree& other) const;
364  math::RangeType<ElemType> RangeDistance(const Octree& other) const;
365 
367  template<typename VecType>
368  ElemType MinDistance(
369  const VecType& point,
370  typename std::enable_if_t<IsVector<VecType>::value>* = 0) const;
372  template<typename VecType>
373  ElemType MaxDistance(
374  const VecType& point,
375  typename std::enable_if_t<IsVector<VecType>::value>* = 0) const;
377  template<typename VecType>
379  const VecType& point,
380  typename std::enable_if_t<IsVector<VecType>::value>* = 0) const;
381 
383  void Center(arma::vec& center) const { bound.Center(center); }
384 
386  template<typename Archive>
387  void Serialize(Archive& ar, const unsigned int /* version */);
388 
389  protected:
396  Octree();
397 
399  friend class boost::serialization::access;
400 
401  private:
410  void SplitNode(const arma::vec& center,
411  const double width,
412  const size_t maxLeafSize);
413 
423  void SplitNode(const arma::vec& center,
424  const double width,
425  std::vector<size_t>& oldFromNew,
426  const size_t maxLeafSize);
427 
431  struct SplitInfo
432  {
434  SplitInfo(const size_t d, const arma::vec& c) : d(d), center(c) {}
435 
437  size_t d;
439  const arma::vec& center;
440 
441  template<typename VecType>
442  static bool AssignToLeftNode(const VecType& point, const SplitInfo& s)
443  {
444  return point[s.d] < s.center[s.d];
445  }
446  };
447 };
448 
449 } // namespace tree
450 } // namespace mlpack
451 
452 // Include implementation.
453 #include "octree_impl.hpp"
454 
455 #endif
StatisticType & Stat()
Modify the statistic object for this node.
Definition: octree.hpp:254
std::vector< Octree * > children
The children held by this node.
Definition: octree.hpp:39
A dual-tree traverser; see dual_tree_traverser.hpp.
bound::HRectBound< MetricType > & Bound()
Modify the bound object for this node.
Definition: octree.hpp:249
SplitInfo(const size_t d, const arma::vec &c)
Create the SplitInfo object.
Definition: octree.hpp:434
ElemType MinimumBoundDistance() const
Return the minimum distance from the center of the node to any bound edge.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
const Octree & Child(const size_t child) const
Return the specified child.
Definition: octree.hpp:326
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
void SplitNode(const arma::vec &center, const double width, const size_t maxLeafSize)
Split the node, using the given center and the given maximum width of this node.
~Octree()
Destroy the tree.
ElemType MaxDistance(const Octree &other) const
Return the maximum distance to another node.
Octree()
A default constructor.
The core includes that mlpack expects; standard C++ includes and Armadillo.
math::RangeType< ElemType > RangeDistance(const Octree &other) const
Return the minimum and maximum distance to another node.
const arma::vec & center
The center of the node.
Definition: octree.hpp:439
bool IsLeaf() const
Return whether or not the node is a leaf.
Definition: octree.hpp:283
MatType Mat
So other classes can use TreeType::Mat.
Definition: octree.hpp:29
static bool AssignToLeftNode(const VecType &point, const SplitInfo &s)
Definition: octree.hpp:442
size_t Point(const size_t index) const
Return the index (with reference to the dataset) of a particular point in this node.
MetricType Metric() const
Return the metric that this tree uses.
Definition: octree.hpp:260
void Center(arma::Col< ElemType > &center) const
Calculates the center of the range, placing it into the given vector.
ElemType & ParentDistance()
Modify the distance from the center of this node to the center of the parent node.
Definition: octree.hpp:320
MetricType metric
An instantiated metric.
Definition: octree.hpp:65
void Serialize(Archive &ar, const unsigned int)
Serialize the tree.
size_t GetNearestChild(const VecType &point, typename std::enable_if_t< IsVector< VecType >::value > *=0) const
Return the index of the nearest child node to the given query point.
Octree *& Parent()
Modify the pointer to the parent (be careful!).
Definition: octree.hpp:244
MatType::elem_type ElemType
The type of element held in MatType.
Definition: octree.hpp:31
ElemType MinDistance(const Octree &other) const
Return the minimum distance to another node.
bound::HRectBound< MetricType > bound
The minimum bounding rectangle of the points held in the node (and its children). ...
Definition: octree.hpp:53
Octree & Child(const size_t child)
Return the specified child.
Definition: octree.hpp:332
size_t NumChildren() const
Return the number of children in this node.
This is used for sorting points while splitting.
Definition: octree.hpp:431
size_t NumPoints() const
Return the number of points in this node (0 if not a leaf).
void Center(arma::vec &center) const
Store the center of the bounding region in the given vector.
Definition: octree.hpp:383
size_t d
The dimension we are splitting on.
Definition: octree.hpp:437
A single-tree traverser; see single_tree_traverser.hpp.
Definition: octree.hpp:35
Octree * Parent() const
Get the pointer to the parent.
Definition: octree.hpp:242
Octree * parent
The parent (NULL if this node is the root).
Definition: octree.hpp:57
const StatisticType & Stat() const
Return the statistic object for this node.
Definition: octree.hpp:252
StatisticType stat
The statistic.
Definition: octree.hpp:59
size_t GetFurthestChild(const VecType &point, typename std::enable_if_t< IsVector< VecType >::value > *=0) const
Return the index of the furthest child node to the given query point.
size_t begin
The index of the first point in the dataset contained in this node (and its children).
Definition: octree.hpp:47
size_t Descendant(const size_t index) const
Return the index (with reference to the dataset) of a particular descendant.
size_t NumDescendants() const
Return the number of descendants of this node.
Octree *& ChildPtr(const size_t child)
Return the pointer to the given child.
Definition: octree.hpp:338
ElemType ParentDistance() const
Return the distance from the center of this node to the center of the parent node.
Definition: octree.hpp:317
ElemType parentDistance
The distance from the center of this node to the center of the parent.
Definition: octree.hpp:61
ElemType FurthestPointDistance() const
Return the furthest distance to a point held in this node.
ElemType FurthestDescendantDistance() const
Return the furthest possible descendant distance.
MatType * dataset
The dataset.
Definition: octree.hpp:55
const bound::HRectBound< MetricType > & Bound() const
Return the bound object for this node.
Definition: octree.hpp:247
If value == true, then VecType is some sort of Armadillo vector or subview.
Definition: arma_traits.hpp:35
ElemType furthestDescendantDistance
The distance to the furthest descendant, cached to speed things up.
Definition: octree.hpp:63
typename enable_if< B, T >::type enable_if_t
Definition: prereqs.hpp:59
size_t count
The number of points of the dataset contained in this node (and its children).
Definition: octree.hpp:50
const MatType & Dataset() const
Return the dataset used by this node.
Definition: octree.hpp:239