-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPointCloud.cpp
274 lines (246 loc) · 7 KB
/
PointCloud.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "PointCloud.h"
#include <MiscLib/Vector.h>
#include <iostream>
#include <iterator>
#include <fstream>
#include <limits>
#include <algorithm>
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <cmath>
#include "Plane.h"
#include <GfxTL/KdTree.h>
#include <GfxTL/CellRangeDataTreeStrategy.h>
#include <GfxTL/IndexedTreeDataKernels.h>
#include <GfxTL/VectorKernel.h>
#include <GfxTL/NullTreeStrategy.h>
#include <GfxTL/BBoxDistanceKdTreeStrategy.h>
#include <GfxTL/IncrementalDistanceKdTreeStrategy.h>
#include <GfxTL/MaxIntervalSplittingKdTreeStrategy.h>
#include <GfxTL/CellBBoxBuildInformationKdTreeStrategy.h>
#include <GfxTL/BBoxBuildInformationTreeStrategy.h>
#include <GfxTL/BucketSizeMaxLevelSubdivisionTreeStrategy.h>
#include <GfxTL/CellLevelTreeStrategy.h>
#include <GfxTL/L2Norm.h>
#include <GfxTL/Plane.h>
#include <GfxTL/VectorXD.h>
#include <GfxTL/IndexedIterator.h>
#include <MiscLib/AlignedAllocator.h>
#include <MiscLib/NoShrinkVector.h>
typedef GfxTL::KdTree
<
GfxTL::IncrementalDistanceKdTreeStrategy
<
GfxTL::MaxIntervalSplittingKdTreeStrategy
<
GfxTL::CellBBoxBuildInformationKdTreeStrategy
<
GfxTL::BBoxBuildInformationTreeStrategy
<
GfxTL::BucketSizeMaxLevelSubdivisionTreeStrategy
<
GfxTL::CellLevelTreeStrategy
<
GfxTL::BaseKdTreeStrategy
<
GfxTL::CellRangeDataTreeStrategy
<
GfxTL::NullTreeStrategy,
GfxTL::IndexedIteratorTreeDataKernel
<
PointCloud::const_iterator,
MiscLib::Vector< size_t >
>
>
>
>
>
>
>
>
>,
GfxTL::L2Norm,
GfxTL::VectorKernelD< 3 >::VectorKernelType
> KdTree3Df;
//#define LMS_NORMALS
#define PCA_NORMALS
using namespace std;
/*
* * This Quickselect routine is based on the algorithm described in
* * "Numerical recipes in C", Second Edition,
* * Cambridge University Press, 1992, Section 8.5, ISBN 0-521-43108-5
* * This code by Nicolas Devillard - 1998. Public domain.
* */
#define ELEM_SWAP(a,b) { register float t=(a);(a)=(b);(b)=t; }
float quick_select(float arr[], int n)
{
int low, high ;
int median;
int middle, ll, hh;
low = 0 ; high = n-1 ; median = (low + high) / 2;
for (;;) {
if (high <= low) /* One element only */
return arr[median] ;
if (high == low + 1) { /* Two elements only */
if (arr[low] > arr[high])
ELEM_SWAP(arr[low], arr[high]) ;
return arr[median] ;
}
/* Find median of low, middle and high items; swap into position low */
middle = (low + high) / 2;
if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]) ;
if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]) ;
if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
/* Swap low item (now in position middle) into position (low+1) */
ELEM_SWAP(arr[middle], arr[low+1]) ;
/* Nibble from each end towards middle, swapping items when stuck */
ll = low + 1;
hh = high;
for (;;) {
do ll++; while (arr[low] > arr[ll]) ;
do hh--; while (arr[hh] > arr[low]) ;
if (hh < ll)
break;
ELEM_SWAP(arr[ll], arr[hh]) ;
}
/* Swap middle item (in position low) back into correct position */
ELEM_SWAP(arr[low], arr[hh]) ;
/* Re-set active partition */
if (hh <= median)
low = ll;
if (hh >= median)
high = hh - 1;
}
}
#undef ELEM_SWAP
PointCloud::PointCloud()
{
float fmax = numeric_limits<float>::max();
float fmin = -fmax;
m_min = Vec3f(fmax, fmax, fmax);
m_max = Vec3f(fmin, fmin, fmin);
}
PointCloud::PointCloud(Point *points, unsigned int s)
{
float fmax = numeric_limits<float>::max();
float fmin = -fmax;
m_min = Vec3f(fmax, fmax, fmax);
m_max = Vec3f(fmin, fmin, fmin);
std::copy(points, points + s, std::back_inserter(*this));
}
void PointCloud::reset(size_t s)
{
resize(s);
float fmax = numeric_limits<float>::max();
float fmin = -fmax;
m_min = Vec3f(fmax, fmax, fmax);
m_max = Vec3f(fmin, fmin, fmin);
}
float *PointCloud::getBbox () const
{
float *bbox = new float[6];
m_min.getValue(bbox[0], bbox[2], bbox[4]);
m_max.getValue(bbox[1], bbox[3], bbox[5]);
return bbox;
}
void PointCloud::GetCurrentBBox(Vec3f *min, Vec3f *max) const
{
*min = m_min;
*max = m_max;
}
void PointCloud::Translate(const Vec3f &trans)
{
for(size_t i = 0; i < size(); ++i)
at(i).pos += trans;
m_min += trans;
m_max += trans;
}
void PointCloud::calcNormals ( float radius, unsigned int kNN, unsigned int maxTries )
{
// cerr << "Begin calcNormals " << endl << flush;
KdTree3Df kd;
kd.IndexedData(begin(), end());
kd.Build();
GfxTL::LimitedHeap< GfxTL::NN< float > > nn;
KdTree3Df::NearestNeighborsAuxData< value_type > nnAux;
//GfxTL::AssumeUniqueLimitedHeap< GfxTL::NN< float > > nn;
MiscLib::NoShrinkVector< float > weights;
vector<int> stats(91, 0);
#ifdef PCA_NORMALS
GfxTL::Plane< GfxTL::Vector3Df > plane;
#endif
for ( unsigned int i = 0; i < size(); i ++ )
{
//kd.PointsInBall(at(i), radius, &nn);
//if(nn.size() > kNN)
//{
// std::sort(nn.begin(), nn.end());
// nn.resize(kNN);
//}
kd.NearestNeighbors(at(i), kNN, &nn, &nnAux);
unsigned int num = (unsigned int)nn.size();
//if(i%1000 == 0)
// cerr << num << " ";
if ( num > kNN )
num = kNN;
at(i).normal = Vec3f(0,0,0);
if ( num < 8 ) {
continue;
}
#ifdef PCA_NORMALS
weights.resize(nn.size());
if(nn.front().sqrDist > 0)
{
float h = nn.front().sqrDist / 2;
for(unsigned int i = 0; i < nn.size(); ++i)
weights[i] = std::exp(-nn[i].sqrDist / h);
}
else
std::fill(weights.begin(), weights.end(), 1.f);
plane.Fit(GfxTL::IndexIterate(nn.begin(), begin()),
GfxTL::IndexIterate(nn.end(), begin()), weights.begin());
at(i).normal = Vec3f(plane.Normal().Data());
#endif
#ifdef LMS_NORMALS
float score, bestScore = -1.f;
for (unsigned int tries = 0; tries < maxTries; tries++)
{
//choose candidate
int i0, i1, i2;
i0 = rand() % num;
do
i1 = rand() % num;
while (i1 == i0);
do
i2 = rand() % num;
while (i2 == i0 || i2 == i1);
Plane plane;
if(!plane.Init(at(nn[i0]), at(nn[i1]), at(nn[i2])))
continue;
//evaluate metric
float *dist = new float[num];
for (unsigned int j = 0; j < num; j++)
{
dist[j] = plane.getDistance(at(nn[j]));
}
// sort(dist, dist+num);
// score = dist[num/2]; // evaluate median
score = quick_select(dist, num); // evaluate median
delete[] dist;
if (score < bestScore || bestScore < 0.f)
{
if ( tries > maxTries/2 ) {
// let us see how good the first half of candidates are...
int index = std::floor(180/M_PI*std::acos(std::min(1.f, abs(plane.getNormal().dot(at(i).normal)))));
stats[index]++;
}
at(i).normal = plane.getNormal();
bestScore = score;
}
}
#endif
}
//cerr << "End calcNormals " << endl << flush;
//copy(stats.begin(), stats.end(), ostream_iterator<int>(cerr, " "));
}