10#include <tbb/task_group.h>
13#include <unordered_map>
31 min_sqr_dist_to_center_(
std::numeric_limits<TReal>::max()),
33 static_assert(POS_FN !=
MAX,
"MAX is not allowed for point positions");
34 static_assert(FEAT_FN !=
CENTER,
35 "CENTER is not allowed for feature vectors");
38 template <
class Derived,
41 typename =
typename std::enable_if<
42 std::is_base_of<Eigen::ArrayBase<Derived3>,
44 inline void AddPoint(
const Eigen::MatrixBase<Derived>& pos,
45 const Eigen::MatrixBase<Derived2>& voxel_center,
46 const Derived3& feat) {
47 bool new_nearest_neighbor =
false;
50 sqr_d = (voxel_center - pos).squaredNorm();
51 if (sqr_d < min_sqr_dist_to_center_) {
52 new_nearest_neighbor =
true;
53 min_sqr_dist_to_center_ = sqr_d;
57 position_ += pos.array();
60 }
else if (POS_FN ==
CENTER) {
61 if (count_ == 0) position_ = voxel_center;
65 features_.resizeLike(feat);
72 }
else if (FEAT_FN ==
MAX) {
73 features_ = features_.max(feat);
78 inline Eigen::Array<TReal, 3, 1>
Position()
const {
80 return position_ / count_;
87 inline Eigen::Array<TFeat, Eigen::Dynamic, 1>
Features()
const {
89 return features_ / count_;
96 inline int Count()
const {
return count_; }
100 TReal min_sqr_dist_to_center_;
101 Eigen::Array<TReal, 3, 1> position_;
102 Eigen::Array<TFeat, Eigen::Dynamic, 1> features_;
105template <
class TReal,
113 min_sqr_dist_to_center_(
std::numeric_limits<TReal>::max()),
115 static_assert(POS_FN !=
MAX,
"MAX is not allowed for point positions");
116 static_assert(FEAT_FN !=
CENTER,
117 "CENTER is not allowed for feature vectors");
120 template <
class Derived,
123 typename =
typename std::enable_if<
124 std::is_base_of<Eigen::ArrayBase<Derived3>,
126 inline void AddPoint(
const Eigen::MatrixBase<Derived>& pos,
127 const Eigen::MatrixBase<Derived2>& voxel_center,
128 const Derived3& feat,
130 bool new_nearest_neighbor =
false;
133 sqr_d = (voxel_center - pos).squaredNorm();
134 if (sqr_d < min_sqr_dist_to_center_) {
135 new_nearest_neighbor =
true;
136 min_sqr_dist_to_center_ = sqr_d;
141 position_ += pos.array();
144 }
else if (POS_FN ==
CENTER) {
145 if (count_ == 0) position_ = voxel_center;
149 features_.resizeLike(feat);
157 }
else if (FEAT_FN ==
MAX) {
159 index_.resizeLike(feat);
170 }
else if (FEAT_FN ==
MAX) {
171 for (
int i = 0; i < features_.rows(); ++i) {
172 if (feat(i) > features_(i)) {
173 features_(i) = feat(i);
181 inline Eigen::Array<TReal, 3, 1>
Position()
const {
183 return position_ / count_;
190 inline Eigen::Array<TFeat, Eigen::Dynamic, 1>
Features()
const {
192 return features_ / count_;
199 inline int Count()
const {
return count_; }
201 inline Eigen::Array<size_t, Eigen::Dynamic, 1>
Index()
const {
207 TReal min_sqr_dist_to_center_;
208 Eigen::Array<TReal, 3, 1> position_;
209 Eigen::Array<TFeat, Eigen::Dynamic, 1> features_;
210 Eigen::Array<size_t, Eigen::Dynamic, 1> index_;
216 const size_t num_positions,
217 const T*
const positions,
218 const T voxel_size) {
219 typedef Eigen::Array<double, 3, 1> Vec3_t;
220 if (num_positions == 0) {
224 Vec3_t bb_min, bb_max;
225 bb_min << positions[0], positions[1], positions[2];
228 Vec3_t voxel_size3(voxel_size, voxel_size, voxel_size);
230 for (
size_t i = 1; i < num_positions; ++i) {
231 Vec3_t pos(positions[i * 3 + 0], positions[i * 3 + 1],
232 positions[i * 3 + 2]);
233 bb_min = bb_min.min(pos);
234 bb_max = bb_max.max(pos);
238 bb_min /= voxel_size3;
239 bb_min = bb_min.floor() * voxel_size3;
240 bb_max /= voxel_size3;
241 bb_max = bb_max.ceil() * voxel_size3;
243 if (voxel_size *
double(std::numeric_limits<int>::max()) <
245 voxel_size *
double(std::numeric_limits<int>::min()) >
247 err =
"voxel_size is too small\n";
258template <
class TDerived>
260 const Eigen::ArrayBase<TDerived>& pos,
262 typedef typename TDerived::Scalar Scalar_t;
265 Eigen::Vector3i voxel_index;
266 voxel_index = ref_coord.floor().template cast<int>();
271template <
class TReal,
class TFeat,
class ACCUMULATOR,
class OUTPUT_ALLOCATOR>
273 const TReal*
const inp_positions,
275 const TFeat* inp_features,
277 OUTPUT_ALLOCATOR& output_allocator) {
281 output_allocator.AllocPooledPositions(&out_pos_ptr, 0);
282 output_allocator.AllocPooledFeatures(&out_feat_ptr, 0, in_channels);
286 typedef Eigen::Array<TReal, 3, 1> Vec3_t;
287 typedef Eigen::Array<TFeat, Eigen::Dynamic, 1> FeatureVec_t;
289 std::unordered_map<Eigen::Vector3i, ACCUMULATOR,
291 voxelindex_to_accpoint;
294 Eigen::Vector3i voxel_index;
296 TReal half_voxel_size = 0.5 * voxel_size;
297 for (
size_t i = 0; i < num_inp; ++i) {
298 Eigen::Map<const Vec3_t> pos(inp_positions + i * 3);
302 voxel_center << voxel_index(0) * voxel_size + half_voxel_size,
303 voxel_index(1) * voxel_size + half_voxel_size,
304 voxel_index(2) * voxel_size + half_voxel_size;
306 Eigen::Map<const FeatureVec_t> feat(inp_features + in_channels * i,
308 voxelindex_to_accpoint[voxel_index].AddPoint(
309 pos.matrix(), voxel_center.matrix(), feat);
312 const size_t num_out = voxelindex_to_accpoint.size();
316 output_allocator.AllocPooledPositions(&out_pos_ptr, num_out);
317 output_allocator.AllocPooledFeatures(&out_feat_ptr, num_out, in_channels);
320 for (
const auto point : voxelindex_to_accpoint) {
321 Vec3_t pos =
point.second.Position();
322 Eigen::Map<Vec3_t> out_pos(out_pos_ptr + i * 3);
325 Eigen::Map<FeatureVec_t> out_feat(out_feat_ptr + i * in_channels,
327 out_feat =
point.second.Features();
334template <
class TReal,
class TFeat,
class ACCUMULATOR, AccumulationFn FEAT_FN>
337 const TReal*
const inp_positions,
339 const TFeat*
const inp_features,
341 const TReal*
const pooled_positions,
342 const TFeat*
const pooled_features_gradient,
347 memset(features_backprop, 0,
sizeof(TFeat) * num_inp * in_channels);
349 typedef Eigen::Array<TReal, 3, 1> Vec3_t;
350 typedef Eigen::Array<TFeat, Eigen::Dynamic, 1> FeatureVec_t;
352 Vec3_t voxel_size3(voxel_size, voxel_size, voxel_size);
355 tbb::task_group task_group;
357 std::unordered_map<Eigen::Vector3i, ACCUMULATOR,
359 voxelindex_to_accpoint;
363 Eigen::Vector3i voxel_index;
365 TReal half_voxel_size = 0.5 * voxel_size;
366 for (
size_t i = 0; i < num_inp; ++i) {
367 Eigen::Map<const Vec3_t> pos(inp_positions + i * 3);
371 voxel_center << voxel_index(0) * voxel_size + half_voxel_size,
372 voxel_index(1) * voxel_size + half_voxel_size,
373 voxel_index(2) * voxel_size + half_voxel_size;
375 Eigen::Map<const FeatureVec_t> feat(inp_features + in_channels * i,
377 voxelindex_to_accpoint[voxel_index].AddPoint(
378 pos.matrix(), voxel_center.matrix(), feat, i);
382 std::unordered_map<Eigen::Vector3i, size_t,
384 voxelindex_to_gradindex;
387 Eigen::Vector3i voxel_index;
389 for (
size_t i = 0; i < num_pooled; ++i) {
390 Eigen::Map<const Vec3_t> pos(pooled_positions + i * 3);
394 voxelindex_to_gradindex[voxel_index] = i;
401 Eigen::Vector3i voxel_index;
403 for (
size_t i = 0; i < num_inp; ++i) {
404 Eigen::Map<const Vec3_t> pos(inp_positions + i * 3);
408 Eigen::Map<FeatureVec_t> feat_bp(
409 features_backprop + in_channels * i, in_channels);
411 size_t grad_idx = voxelindex_to_gradindex[voxel_index];
412 int count = voxelindex_to_accpoint[voxel_index].Count();
413 Eigen::Map<const FeatureVec_t> grad(
414 pooled_features_gradient + in_channels * grad_idx,
416 feat_bp = grad /
count;
421 for (
const auto point : voxelindex_to_accpoint) {
422 size_t idx =
point.second.Index()(0);
423 Eigen::Map<FeatureVec_t> feat_bp(
424 features_backprop + in_channels * idx, in_channels);
426 size_t grad_idx = voxelindex_to_gradindex[
point.first];
427 Eigen::Map<const FeatureVec_t> grad(
428 pooled_features_gradient + in_channels * grad_idx,
434 if (FEAT_FN ==
MAX) {
435 for (
const auto point : voxelindex_to_accpoint) {
436 size_t grad_idx = voxelindex_to_gradindex[
point.first];
437 Eigen::Map<const FeatureVec_t> grad(
438 pooled_features_gradient + in_channels * grad_idx,
440 for (
int i = 0; i < in_channels; ++i) {
441 size_t idx =
point.second.Index()(i);
442 Eigen::Map<FeatureVec_t> feat_bp(
443 features_backprop + in_channels * idx, in_channels);
444 feat_bp(i) = grad(i);
494template <
class TReal,
class TFeat,
class OUTPUT_ALLOCATOR>
496 const TReal*
const inp_positions,
498 const TFeat* inp_features,
500 OUTPUT_ALLOCATOR& output_allocator,
503#define CALL_TEMPLATE(POS_FN, FEAT_FN) \
504 if (POS_FN == position_fn && FEAT_FN == feature_fn) { \
505 _VoxelPooling<TReal, TFeat, \
506 Accumulator<TReal, TFeat, POS_FN, FEAT_FN>>( \
507 num_inp, inp_positions, in_channels, inp_features, voxel_size, \
539template <
class TReal,
class TFeat>
542 const TReal*
const inp_positions,
544 const TFeat*
const inp_features,
546 const TReal*
const pooled_positions,
547 const TFeat*
const pooled_features_gradient,
551#define CALL_TEMPLATE(POS_FN, FEAT_FN) \
552 if (POS_FN == position_fn && FEAT_FN == feature_fn) { \
553 _VoxelPoolingBackprop< \
555 AccumulatorBackprop<TReal, TFeat, POS_FN, FEAT_FN>, FEAT_FN>( \
556 features_backprop, num_inp, inp_positions, in_channels, \
557 inp_features, num_pooled, pooled_positions, \
558 pooled_features_gradient, voxel_size); \
#define CALL_TEMPLATE(METRIC, IGNORE_QUERY_POINT, RETURN_DISTANCES)
Point< Real, 3 > point
Definition SurfaceReconstructionPoisson.cpp:166
Definition VoxelPooling.h:109
Eigen::Array< TReal, 3, 1 > Position() const
Definition VoxelPooling.h:181
Eigen::Array< size_t, Eigen::Dynamic, 1 > Index() const
Definition VoxelPooling.h:201
void AddPoint(const Eigen::MatrixBase< Derived > &pos, const Eigen::MatrixBase< Derived2 > &voxel_center, const Derived3 &feat, const size_t idx)
Definition VoxelPooling.h:126
int Count() const
Definition VoxelPooling.h:199
AccumulatorBackprop()
Definition VoxelPooling.h:111
Eigen::Array< TFeat, Eigen::Dynamic, 1 > Features() const
Definition VoxelPooling.h:190
Definition VoxelPooling.h:27
int Count() const
Definition VoxelPooling.h:96
void AddPoint(const Eigen::MatrixBase< Derived > &pos, const Eigen::MatrixBase< Derived2 > &voxel_center, const Derived3 &feat)
Definition VoxelPooling.h:44
Eigen::Array< TReal, 3, 1 > Position() const
Definition VoxelPooling.h:78
Eigen::Array< TFeat, Eigen::Dynamic, 1 > Features() const
Definition VoxelPooling.h:87
Accumulator()
Definition VoxelPooling.h:29
AccumulationFn
Definition VoxelPooling.h:21
@ CENTER
Definition VoxelPooling.h:21
@ NEAREST_NEIGHBOR
Definition VoxelPooling.h:21
@ MAX
Definition VoxelPooling.h:21
@ AVERAGE
Definition VoxelPooling.h:21
void VoxelPoolingBackprop(TFeat *features_backprop, size_t num_inp, const TReal *const inp_positions, int in_channels, const TFeat *const inp_features, size_t num_pooled, const TReal *const pooled_positions, const TFeat *const pooled_features_gradient, TReal voxel_size, AccumulationFn position_fn, AccumulationFn feature_fn)
Definition VoxelPooling.h:540
bool CheckVoxelSize(std::string &err, const size_t num_positions, const T *const positions, const T voxel_size)
Function for debugging. Checks if the voxel size is too small.
Definition VoxelPooling.h:215
void VoxelPooling(size_t num_inp, const TReal *const inp_positions, int in_channels, const TFeat *inp_features, TReal voxel_size, OUTPUT_ALLOCATOR &output_allocator, AccumulationFn position_fn, AccumulationFn feature_fn)
Definition VoxelPooling.h:495
void _VoxelPoolingBackprop(TFeat *features_backprop, size_t num_inp, const TReal *const inp_positions, int in_channels, const TFeat *const inp_features, size_t num_pooled, const TReal *const pooled_positions, const TFeat *const pooled_features_gradient, TReal voxel_size)
Definition VoxelPooling.h:335
void _VoxelPooling(size_t num_inp, const TReal *const inp_positions, int in_channels, const TFeat *inp_features, TReal voxel_size, OUTPUT_ALLOCATOR &output_allocator)
Definition VoxelPooling.h:272
HOST_DEVICE utility::MiniVec< int, 3 > ComputeVoxelIndex(const TVecf &pos, const typename TVecf::Scalar_t &inv_voxel_size)
Definition NeighborSearchCommon.h:42
Definition PinholeCameraIntrinsic.cpp:16