-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfastsamplefilter.hpp
211 lines (164 loc) · 6.95 KB
/
fastsamplefilter.hpp
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
#pragma once
#include <iostream>
#include <map>
#include <tuple>
#include <vector>
#include <omp.h>
#include "ply.hpp"
#include "common.hpp"
namespace FPCFilter {
class FastSampleFilter {
using Voxel = PointXYZ<int>;
using Coord = PointXYZ<double>;
using CoordList = std::vector<Coord>;
std::ostream& log;
std::map<Voxel, CoordList> voxels;
double cell;
double radius;
double radiusSqr;
double originX;
double originY;
double originZ;
bool isVerbose;
public:
FastSampleFilter(double radius, std::ostream& logstream, bool isVerbose) : originX(0), originY(0), originZ(0),
isVerbose(isVerbose), log(logstream), radius(radius), radiusSqr(radius * radius) {
cell = 2.0 * radius / std::sqrt(3.0);
}
void run(PlyFile& file) {
auto points = file.points;
auto extras = file.extras;
const auto cnt = points.size();
if (cnt == 0)
return;
const auto hasNormals = file.hasNormals();
originX = points[0].x;
originY = points[0].y;
originZ = points[0].z;
if (hasNormals) {
std::vector<PlyPoint> newPoints;
newPoints.reserve(cnt);
std::vector<PlyExtra> newExtras;
newExtras.reserve(cnt);
std::vector<PlyPoint> tmpPoints;
tmpPoints.reserve(cnt / omp_get_max_threads());
std::vector<PlyExtra> tmpExtras;
tmpExtras.reserve(cnt / omp_get_max_threads());
#pragma omp parallel private (tmpPoints, tmpExtras)
{
#pragma omp for
for (auto n = 0; n < cnt; n++) {
const auto& point = points[n];
const auto& extra = extras[n];
if (this->safe_voxelize(point)) {
tmpPoints.push_back(point);
tmpExtras.push_back(extra);
}
}
#pragma omp critical
{
if (this->isVerbose)
log << " ?> Sampled " << tmpPoints.size() << " points in thread " << omp_get_thread_num() << std::endl;
newPoints.insert(newPoints.end(), tmpPoints.begin(), tmpPoints.end());
newExtras.insert(newExtras.end(), tmpExtras.begin(), tmpExtras.end());
}
}
newPoints.shrink_to_fit();
newExtras.shrink_to_fit();
file.points = newPoints;
file.extras = newExtras;
} else {
auto res = std::remove_if(points.begin(), points.end(), [this](PlyPoint& point) { return !this->safe_voxelize(point); });
points.erase(res, points.end());
}
}
private:
inline int fast_floor(double x)
{
int i = (int)x; /* truncate */
return i - ( i > x ); /* convert trunc to floor */
}
bool safe_voxelize(const PlyPoint& point) {
double x = point.x;
double y = point.y;
double z = point.z;
// Get voxel indices for current point.
auto v = Voxel(fast_floor((x - originX) / cell),
fast_floor((y - originY) / cell),
fast_floor((z - originZ) / cell));
// Check current voxel before any of the neighbors. We will most often have
// points that are too close in the point's enclosing voxel, thus saving
// cycles.
if (voxels.find(v) != voxels.end())
{
// Get list of coordinates in candidate voxel.
CoordList coords = voxels[v];
for (Coord const& coord : coords)
{
// Compute Euclidean distance between current point and
// candidate voxel.
double xv = coord.x;
double yv = coord.y;
double zv = coord.z;
double distSqr =
(xv - x) * (xv - x) + (yv - y) * (yv - y) + (zv - z) * (zv - z);
// If any point is closer than the minimum radius, we can
// immediately return false, as the minimum distance
// criterion is violated.
if (distSqr < radiusSqr)
return false;
}
}
// Iterate over immediate neighbors of current voxel, computing minimum
// distance between any already added point and the current point.
for (int xi = v.x - 1; xi < v.x + 2; ++xi)
{
for (int yi = v.y - 1; yi < v.y + 2; ++yi)
{
for (int zi = v.z - 1; zi < v.z + 2; ++zi)
{
auto candidate = Voxel(xi, yi, zi);
// We have already visited the center voxel, and can skip it.
if (v == candidate)
continue;
// Check that candidate voxel is occupied.
if (voxels.find(candidate) == voxels.end())
continue;
// Get list of coordinates in candidate voxel.
CoordList coords = voxels[candidate];
for (Coord const& coord : coords)
{
// Compute Euclidean distance between current point and
// candidate voxel.
double xv = coord.x;
double yv = coord.y;
double zv = coord.z;
double distSqr = (xv - x) * (xv - x) + (yv - y) * (yv - y) +
(zv - z) * (zv - z);
// If any point is closer than the minimum radius, we can
// immediately return false, as the minimum distance
// criterion is violated.
if (distSqr < radiusSqr)
return false;
}
}
}
}
auto coord = Coord(x, y, z);
#pragma omp critical
{
if (voxels.find(v) != voxels.end())
{
voxels[v].push_back(coord);
}
else
{
CoordList coords;
coords.push_back(coord);
voxels.emplace(v, coords);
}
}
return true;
}
};
}