Atoms Crowd  7.0.0
KdTreePoint.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2018 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <AtomsUtils/Globals.h>
11 #include <AtomsUtils/AtomsMath.h>
12 #include <vector>
13 
14 namespace AtomsUtils
15 {
16  class KdTreePoint;
17 
18  class ATOMSUTILS_EXPORT KdTreePoint
19  {
20  public:
21 
22  typedef int DataType;
23 
24  struct Sample
25  {
26  DataType data;
27  AtomsMath::Vector3 pos;
28  };
29 
31  {
32  public:
33 
34  inline unsigned int numSamples() const { return high - low + 1; }
35 
36  inline bool isLeaf() const { return left == -1 && right == -1; }
37 
38  public:
39 
40  AtomsMath::Box3 bbox;
41  unsigned int low, high;
42  int left, right;
43  };
44 
45  KdTreePoint(unsigned int maxSamplesPerNode = 10, unsigned int maxDepth = 10);
46 
47  ~KdTreePoint();
48 
49  KdTreePoint(const KdTreePoint& rhs);
50 
51  void addSample(const AtomsMath::Vector3 &sample, const int value);
52 
53  void buildTree();
54 
55  void clear();
56 
57  void reserve(size_t maxNumSamples);
58 
59  void findInRange(const AtomsMath::Vector3& position, const double radius, std::vector<DataType>& neighbours) const;
60 
61  void findSampleInRange(const AtomsMath::Vector3& position, const double radius, std::vector<int>& neighbours) const;
62 
63  bool findClosestSample(const AtomsMath::Vector3 &searchPoint, int &closestId, double* maxdist = nullptr) const;
64 
65  void findClosestSamples(const AtomsMath::Vector3 &searchPoint, std::vector<std::pair<int, double>> &closestIds, unsigned int numSamples) const;
66 
67  void findClosestSamples(const AtomsMath::Vector3 &searchPoint, std::vector<std::pair<int, double>> &closestIds, unsigned int numSamples, double maxDistance) const;
68 
69  bool intersect(const AtomsMath::Vector3& start, const AtomsMath::Vector3& end, const bool disableY) const;
70 
71  void findInRayRange(const AtomsMath::Line3& line, double radius, std::vector<DataType>& neighbours) const;
72 
73  void findSampleInRayRange(const AtomsMath::Line3& line, double radius, std::vector<int>& neighbours) const;
74 
75  inline std::vector<Sample>& samples() { return m_samples; }
76 
77  inline const std::vector<Sample>& samples() const { return m_samples; }
78 
79  inline std::vector<int>& comb() { return m_comb; }
80 
81  inline std::vector<KdTreePointNode>& nodes() { return m_nodes; }
82 
83  private:
84 
85  void buildNode(int nodeIndex, const AtomsMath::Box3& box, unsigned int depth, int low, int high);
86 
87  unsigned int allocateNode();
88 
89  void findInRangeRecurse(unsigned int index, const AtomsMath::Vector3& position, const double radius, std::vector<DataType>& neighbours) const;
90 
91  bool intersectRecurse(unsigned int index, const AtomsMath::Line3d& line, const bool disableY) const;
92 
93  void findSampleInRangeRecurse(unsigned int index, const AtomsMath::Vector3& position, const double radius, std::vector<int>& neighbours) const;
94 
95  void findInRayRangeRecurse(unsigned int index, const AtomsMath::Line3& line, const double radius, std::vector<DataType>& neighbours) const;
96 
97  void findSampleInRayRangeRecurse(unsigned int index, const AtomsMath::Line3& line, const double radius, std::vector<int>& neighbours) const;
98 
99  void walkCells(unsigned int cellIndex, const AtomsMath::Vector3 &searchPoint, int &closestId, double& maxDist) const;
100 
101  void walkCells(unsigned int cellIndex, const AtomsMath::Vector3 &searchPoint, std::vector<std::pair<int, double>> & neighbours, unsigned int numSamples, double &maxDist) const;
102 
103  private:
104 
105  std::vector<KdTreePointNode> m_nodes;
106 
107  std::vector<Sample> m_samples;
108 
109  std::vector<int> m_comb;
110 
111  unsigned int m_maxSamplesPerNode;
112 
113  unsigned int m_maxDepth;
114  };
115 }
Definition: KdTreePoint.h:31
Definition: KdTreePoint.h:19
AtomsCore namespace.
Definition: Base64.h:13
Definition: KdTreePoint.h:25