ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CVKdTree.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Local
11 #include "PointProjectionTools.h"
12 
13 namespace cloudViewer {
14 
15 class GenericIndexedCloud;
16 class GenericProgressCallback;
17 
21 public:
23  KDTree();
24 
26  virtual ~KDTree();
27 
29 
34  bool buildFromCloud(GenericIndexedCloud *cloud,
35  GenericProgressCallback *progressCb = nullptr);
36 
38 
41  return m_associatedCloud;
42  }
43 
45 
53  bool findNearestNeighbour(const PointCoordinateType *queryPoint,
54  unsigned &nearestPointIndex,
55  ScalarType maxDist);
56 
58 
61  bool findNearestNeighbourWithMaxDist(const PointCoordinateType *queryPoint,
62  ScalarType maxDist);
63 
66 
72  unsigned radiusSearch(const PointCoordinateType *queryPoint,
73  ScalarType distance,
74  ScalarType tolerance,
75  std::vector<unsigned> &pointndexes);
76 
78 
81  bool findPointBelowDistance(const PointCoordinateType *queryPoint,
82  ScalarType maxDist);
83 
86 
94  unsigned findPointsLyingToDistance(const PointCoordinateType *queryPoint,
95  ScalarType distance,
96  ScalarType tolerance,
97  std::vector<unsigned> &points);
98 
99 protected:
101  struct KdCell {
103  : cuttingDim(0),
104  cuttingCoordinate(0),
105  leSon(nullptr),
106  gSon(nullptr),
107  father(nullptr),
108  startingPointIndex(0),
109  nbPoints(0),
110  boundsMask(0) {}
111 
112  // Warning: put the non aligned members (< 4 bytes) at the end to avoid
113  // too much alignment padding!
114 
116 
119  CCVector3 inbbmax; // 12 bytes
121 
124  CCVector3 inbbmin; // 12 bytes
126 
129  CCVector3 outbbmin; // 12 bytes
131 
134  CCVector3 outbbmax; // 12 bytes
135 
138  unsigned cuttingDim; // 4 bytes
143  struct KdCell *leSon; // 8 bytes
146  struct KdCell *gSon; // 8 bytes
148  struct KdCell *father; // 8 bytes
150  unsigned startingPointIndex; // 4 bytes
152  unsigned nbPoints; // 4 bytes
153 
155 
160  unsigned char boundsMask; // 1 byte (+ 3 for alignment)
161 
162  // Total
163  // //92 bytes
164  };
165 
166  /*** Protected attributes ***/
167 
171  std::vector<unsigned> m_indexes;
175  unsigned m_cellCount;
176 
177  /*** Protected methods ***/
178 
180 
188  KdCell *buildSubTree(unsigned first,
189  unsigned last,
190  KdCell *father,
191  unsigned &nbBuildCell,
192  GenericProgressCallback *progressCb = nullptr);
193 
195  void deleteSubTree(KdCell *cell);
196 
200  void updateInsideBoundingBox(KdCell *cell);
201 
204  void updateOutsideBoundingBox(KdCell *cell);
205 
207 
212  ScalarType pointToCellSquareDistance(const PointCoordinateType *queryPoint,
213  KdCell *cell);
214 
217 
222  ScalarType insidePointToCellDistance(const PointCoordinateType *queryPoint,
223  KdCell *cell);
224 
227 
233  void pointToCellDistances(const PointCoordinateType *queryPoint,
234  KdCell *cell,
235  ScalarType &min,
236  ScalarType &max);
237 
240 
247  int checkNearerPointInSubTree(const PointCoordinateType *queryPoint,
248  ScalarType &maxSqrDist,
249  KdCell *cell);
250 
253 
260  bool checkDistantPointInSubTree(const PointCoordinateType *queryPoint,
261  ScalarType &maxSqrDist,
262  KdCell *cell);
263 
266 
273  void distanceScanTree(const PointCoordinateType *queryPoint,
274  ScalarType distance,
275  ScalarType tolerance,
276  KdCell *cell,
277  std::vector<unsigned> &localArray);
278 };
279 
280 } // namespace cloudViewer
#define CV_CORE_LIB_API
Definition: CVCoreLibWin.h:15
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
int points
A generic 3D point cloud with index-based point access.
std::vector< unsigned > m_indexes
Point indexes.
Definition: CVKdTree.h:171
GenericIndexedCloud * m_associatedCloud
Associated cloud.
Definition: CVKdTree.h:173
unsigned m_cellCount
Number of cells.
Definition: CVKdTree.h:175
KdCell * m_root
Tree root.
Definition: CVKdTree.h:169
GenericIndexedCloud * getAssociatedCloud() const
Gets the point cloud from which the tree has been build.
Definition: CVKdTree.h:40
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
Generic file read and write utility for python interface.
flann::Index< flann::L2< float > > KDTree
A KDTre cell struct.
Definition: CVKdTree.h:101
CCVector3 outbbmin
Outside bounding box min point.
Definition: CVKdTree.h:129
PointCoordinateType cuttingCoordinate
Place where the space is cut into two sub-spaces (children)
Definition: CVKdTree.h:140
unsigned char boundsMask
Mask to know if the outside box is bounded for a given dimmension.
Definition: CVKdTree.h:160
struct KdCell * father
To go up in the tree.
Definition: CVKdTree.h:148
CCVector3 inbbmin
Inside bounding box min point.
Definition: CVKdTree.h:124
struct KdCell * leSon
Definition: CVKdTree.h:143
CCVector3 outbbmax
Outside bounding box max point.
Definition: CVKdTree.h:134
CCVector3 inbbmax
Inside bounding box max point.
Definition: CVKdTree.h:119
struct KdCell * gSon
Definition: CVKdTree.h:146
unsigned nbPoints
Number of elements in this cell.
Definition: CVKdTree.h:152
unsigned startingPointIndex
Index of the first element that belongs to this cell.
Definition: CVKdTree.h:150