ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
TrueKdTree.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
12 #include "ReferenceCloud.h"
13 
14 // system
15 #include <cstdint>
16 
17 namespace cloudViewer {
18 
19 class GenericIndexedCloudPersist;
20 class GenericProgressCallback;
21 
24 public:
25  // Warning: never pass a 'constant initializer' by reference
26  static const uint8_t X_DIM = 0;
27  static const uint8_t Y_DIM = 1;
28  static const uint8_t Z_DIM = 2;
29  static const uint8_t NODE_TYPE = 0;
30  static const uint8_t LEAF_TYPE = 1;
31 
33  class BaseNode {
34  public:
35  explicit BaseNode(uint8_t nodeType) : parent(nullptr), type(nodeType) {}
36  virtual ~BaseNode() = default;
37 
38  bool isNode() const { return type == NODE_TYPE; }
39  bool isLeaf() const { return type == LEAF_TYPE; }
40 
41  public:
42  // Warning: put the non aligned members (< 4 bytes) at the end to avoid
43  // too much alignment padding!
44  BaseNode* parent; // 8 bytes
45 
46  protected:
47  const uint8_t type; // 1 byte (+ 3 for alignment)
48 
49  // Total //12 bytes
50  };
51 
53  class Node : public BaseNode {
54  public:
55  // Warning: put the non aligned members (< 4 bytes) at the end to avoid
56  // too much alignment padding!
58  BaseNode* leftChild; // 8 bytes
59  BaseNode* rightChild; // 8 bytes
60  uint8_t splitDim; // 1 byte (+ 3 bytes for alignment)
61 
62  // Total
63  // //24 bytes (+ 12 for father)
64 
65  Node()
66  : BaseNode(NODE_TYPE),
67  splitValue(0),
68  leftChild(nullptr),
69  rightChild(nullptr),
70  splitDim(X_DIM) {}
71 
72  ~Node() override {
73  delete leftChild;
74  delete rightChild;
75  }
76  };
77 
79  class Leaf : public BaseNode {
80  public:
81  // Warning: put the non aligned members (< 4 bytes) at the end to avoid
82  // too much alignment padding!
83  ReferenceCloud* points; // 8 bytes
84  PointCoordinateType planeEq[4]; // 16 bytes
85  ScalarType error; // 4 bytes
86  int userData; // 4 bytes
87 
88  // Total
89  // //32 bytes (+ 12 for father)
90 
92 
95  const PointCoordinateType planeEquation[],
96  ScalarType _error)
97  : BaseNode(LEAF_TYPE), points(set), error(_error), userData(0) {
98  memcpy(planeEq, planeEquation, sizeof(PointCoordinateType) * 4);
99  }
100 
101  ~Leaf() override { delete points; }
102  };
103 
105  using LeafVector = std::vector<Leaf*>;
106 
108  explicit TrueKdTree(GenericIndexedCloudPersist* cloud);
109 
111  ~TrueKdTree();
112 
115  return m_associatedCloud;
116  }
117 
119 
127  bool build(double maxError,
130  unsigned minPointCountPerCell = 3,
131  unsigned maxPointCountPerCell = 0,
132  GenericProgressCallback* progressCb = nullptr);
133 
135  void clear();
136 
138  inline double getMaxError() const { return m_maxError; }
139 
142  return m_errorMeasure;
143  }
144 
146  bool getLeaves(LeafVector& leaves) const;
147 
148 protected:
150  BaseNode* split(ReferenceCloud* subset);
151 
154 
157 
159  double m_maxError;
160 
163 
165 
168 
170 
173 };
174 
175 } // 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
char type
A generic 3D point cloud with index-based and presistent access to points.
A very simple point cloud (no point duplication)
ReferenceCloud * points
Definition: TrueKdTree.h:83
Leaf(ReferenceCloud *set, const PointCoordinateType planeEquation[], ScalarType _error)
Constructor.
Definition: TrueKdTree.h:94
PointCoordinateType splitValue
Definition: TrueKdTree.h:57
Proper KD-tree implementation.
Definition: TrueKdTree.h:23
double m_maxError
Max error for planarity-based split strategy (see m_errorMeasure)
Definition: TrueKdTree.h:159
unsigned m_maxPointCountPerCell
Max number of points per cell (speed-up)
Definition: TrueKdTree.h:172
BaseNode * m_root
Root node.
Definition: TrueKdTree.h:153
DistanceComputationTools::ERROR_MEASURES m_errorMeasure
Error measurement.
Definition: TrueKdTree.h:162
unsigned m_minPointCountPerCell
Min number of points per cell (speed-up)
Definition: TrueKdTree.h:167
std::vector< Leaf * > LeafVector
A vector of leaves.
Definition: TrueKdTree.h:105
GenericIndexedCloudPersist * associatedCloud() const
Returns the associated cloud.
Definition: TrueKdTree.h:114
double getMaxError() const
Returns max error threshold used for planarity-based split strategy.
Definition: TrueKdTree.h:138
DistanceComputationTools::ERROR_MEASURES getMaxErrorType() const
Returns max error estimator used for planarity-based split strategy.
Definition: TrueKdTree.h:141
GenericIndexedCloudPersist * m_associatedCloud
Associated cloud.
Definition: TrueKdTree.h:156
static void error(char *msg)
Definition: lsd.c:159
Generic file read and write utility for python interface.