TreeUtils.cpp
Go to the documentation of this file.
1 
38 
39 #include <limits>
40 #include <vector>
41 using namespace std;
42 using namespace Eigen;
43 
44 namespace lvr2
45 {
46 
47 int splitPoints(Vector3f* points, int n, int axis, double splitValue)
48 {
49  int l = 0, r = n - 1;
50 
51  while (l < r)
52  {
53  while (l < r && points[l](axis) < splitValue)
54  {
55  ++l;
56  }
57  while (r > l && points[r](axis) >= splitValue)
58  {
59  --r;
60  }
61  if (l < r)
62  {
63  std::swap(points[l], points[r]);
64  }
65  }
66 
67  return l;
68 }
69 
70 void createOctree(Vector3f* points,
71  int n,
72  bool* flagged,
73  const Vector3f& min,
74  const Vector3f& max,
75  int level,
76  double voxelSize,
77  int maxLeafSize)
78 {
79  if (n <= maxLeafSize)
80  {
81  return;
82  }
83 
84  int axis = level % 3;
85  Vector3f center = (max + min) / 2.0;
86 
87  if (max[axis] - min[axis] <= voxelSize)
88  {
89  // keep the Point closest to the center
90  int closest = 0;
91  double minDist = (points[closest] - center).squaredNorm();
92  for (int i = 1; i < n; i++)
93  {
94  double dist = (points[i] - center).squaredNorm();
95  if (dist < minDist)
96  {
97  closest = i;
98  minDist = dist;
99  }
100  }
101  // flag all other Points for deletion
102  for (int i = 0; i < n; i++)
103  {
104  flagged[i] = i != closest;
105  }
106  return;
107  }
108 
109  int l = splitPoints(points, n, axis, center[axis]);
110 
111  Vector3f lMin = min, lMax = max;
112  Vector3f rMin = min, rMax = max;
113 
114  lMax[axis] = center[axis];
115  rMin[axis] = center[axis];
116 
117  if (l > maxLeafSize)
118  {
119  #pragma omp task
120  createOctree(points , l , flagged , lMin, lMax, level + 1, voxelSize, maxLeafSize);
121  }
122 
123  if (n - l > maxLeafSize)
124  {
125  #pragma omp task
126  createOctree(points + l, n - l, flagged + l, rMin, rMax, level + 1, voxelSize, maxLeafSize);
127  }
128 }
129 
130 int octreeReduce(Vector3f* points, int n, double voxelSize, int maxLeafSize)
131 {
132  bool* flagged = new bool[n];
133  for (int i = 0; i < n; i++)
134  {
135  flagged[i] = false;
136  }
137 
138  AABB<float> boundingBox(points, (size_t)n);
139 
140  #pragma omp parallel // allows "pragma omp task"
141  #pragma omp single // only execute every task once
142  createOctree(points, n, flagged, boundingBox.min(), boundingBox.max(), 0, voxelSize, maxLeafSize);
143 
144  // remove all flagged elements
145  int i = 0;
146  while (i < n)
147  {
148  if (flagged[i])
149  {
150  n--;
151  if (i == n)
152  {
153  break;
154  }
155  points[i] = points[n];
156  flagged[i] = flagged[n];
157  }
158  else
159  {
160  i++;
161  }
162  }
163 
164  delete[] flagged;
165 
166  return n;
167 }
168 
169 } /* namespace lvr2 */
lvr2::AABB::min
const Vector3< T > & min() const
Returns the "lower left" Corner of the Bounding Box, as in the smallest x, y, z of the Point Cloud.
lvr2::octreeReduce
int octreeReduce(Vector3f *points, int n, double voxelSize, int maxLeafSize)
Reduces a Point Cloud using an Octree with a minimum Voxel size.
Definition: TreeUtils.cpp:130
lvr2::AABB
A struct to calculate the Axis Aligned Bounding Box and Average Point of a Point Cloud.
Definition: AABB.hpp:49
lvr2::Vector3f
Eigen::Vector3f Vector3f
Eigen 3D vector, single precision.
Definition: MatrixTypes.hpp:118
TreeUtils.hpp
lvr2::splitPoints
int splitPoints(Vector3f *points, int n, int axis, double splitValue)
Sorts a Point array so that all Points smaller than splitValue are on the left.
Definition: TreeUtils.cpp:47
lvr2::AABB::max
const Vector3< T > & max() const
Returns the "upper right" Corner of the Bounding Box, as in the largest x, y, z of the Point Cloud.
kfusion::device::swap
__kf_hdevice__ void swap(T &a, T &b)
Definition: temp_utils.hpp:10
std
Definition: HalfEdge.hpp:124
lvr2
Definition: BaseBufferManipulators.hpp:39
AABB.hpp
lvr2::createOctree
void createOctree(Vector3f *points, int n, bool *flagged, const Vector3f &min, const Vector3f &max, int level, double voxelSize, int maxLeafSize)
Definition: TreeUtils.cpp:70


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:25