26 namespace perception {
28 template <
typename T,
unsigned int d>
83 assert(
data_ !=
nullptr);
84 return (
data_ + i * d);
89 assert(
data_ !=
nullptr);
90 return (
data_ + i * d);
95 return data_[i * d + j];
98 const_reference
operator()(
unsigned int i,
unsigned int j)
const {
100 return data_[i * d + j];
117 template <
typename T,
unsigned int d>
120 template <
typename T,
unsigned int d>
123 data_ = IAllocAligned<T>(n * d, 4);
131 template <
typename T,
unsigned int d>
135 data_ = IAllocAligned<T>(n * d, 4);
145 template <
typename T,
unsigned int d>
151 template <
typename T>
153 T bound_y_min, T bound_y_max, T bound_z_min,
154 T bound_z_max, T voxel_width_x_rec,
155 T voxel_width_y_rec,
int nr_voxel_x,
163 if (x < bound_x_min || x > bound_x_max || y < bound_y_min ||
164 y > bound_y_max || z < bound_z_min || z > bound_z_max) {
169 k =
IMin(nr_voxel_x - 1,
170 static_cast<int>((x - bound_x_min) * voxel_width_x_rec));
171 j =
IMin(nr_voxel_y - 1,
172 static_cast<int>((y - bound_y_min) * voxel_width_y_rec));
173 i = (nr_voxel_x * j) + k;
177 template <
typename T>
194 if (
this != &voxel) {
195 this->dim_x_ = voxel.
dim_x_;
196 this->dim_y_ = voxel.
dim_y_;
197 this->dim_z_ = voxel.
dim_z_;
198 this->ix_ = voxel.
ix_;
199 this->iy_ = voxel.
iy_;
200 this->iz_ = voxel.
iz_;
207 void Init(
const T *v, T dim_x, T dim_y, T dim_z,
int ix,
int iy,
int iz) {
218 void Init(T v_x, T v_y, T v_z, T dim_x, T dim_y, T dim_z,
int ix,
int iy,
234 dim_x_ = dim_y_ = dim_z_ = 0;
239 void Reserve(
unsigned int size) { indices_.reserve(size); }
243 unsigned int Capacity()
const {
return (
unsigned int)indices_.capacity(); }
245 unsigned int NrPoints()
const {
return (
unsigned int)indices_.size(); }
247 bool Empty()
const {
return indices_.empty(); }
249 T v_[3], dim_x_, dim_y_, dim_z_;
259 template <
typename T>
262 static const unsigned int s_nr_max_reserved_points_ = 320000;
266 VoxelGridXY(
unsigned int nr_voxel_x,
unsigned int nr_voxel_y,
267 T spatial_bound_x_min, T spatial_bound_x_max,
268 T spatial_bound_y_min, T spatial_bound_y_max,
269 T spatial_bound_z_min, T spatial_bound_z_max);
281 &this->dim_y_[1], &this->dim_z_[0], &this->dim_z_[1]);
282 this->voxels_.resize(vg.
NrVoxel());
283 for (
unsigned int i = 0; i < vg.
NrVoxel(); ++i) {
284 this->voxels_[i] = vg[i];
287 this->AllocAlignedMemory();
295 IFreeAligned<float>(&mem_aligned16_f32_);
296 IFreeAligned<int>(&mem_aligned16_i32_);
300 bool Alloc(
unsigned int nr_voxel_x,
unsigned int nr_voxel_y,
301 T spatial_bound_x_min, T spatial_bound_x_max,
302 T spatial_bound_y_min, T spatial_bound_y_max,
303 T spatial_bound_z_min, T spatial_bound_z_max);
305 bool Set(
const T *data,
unsigned int nr_points,
306 unsigned int nr_point_element);
309 bool SetS(
const float *data,
unsigned int nr_points,
310 unsigned int nr_point_element);
312 bool Set(
const T *data,
unsigned int nr_points,
unsigned int nr_point_element,
313 unsigned int nr_voxel_x,
unsigned int nr_voxel_y,
314 T spatial_bound_x_min, T spatial_bound_x_max, T spatial_bound_y_min,
315 T spatial_bound_y_max, T spatial_bound_z_min, T spatial_bound_z_max,
316 bool force_bound =
true);
320 unsigned int NrVoxel()
const {
return (
unsigned int)voxels_.size(); }
322 unsigned int NrVoxelX()
const {
return nr_voxel_x_; }
324 unsigned int NrVoxelY()
const {
return nr_voxel_y_; }
332 unsigned int NrIndexedPoints()
const;
339 voxels_.assign(voxels.begin(), voxels.end());
343 T *dim_min_z, T *dim_max_z)
const {
345 dim_min_x = dim_max_x = dim_min_y = dim_max_y = dim_min_z = dim_max_z =
349 *dim_min_x = dim_x_[0];
350 *dim_max_x = dim_x_[1];
351 *dim_min_y = dim_y_[0];
352 *dim_max_y = dim_y_[1];
353 *dim_min_z = dim_z_[0];
354 *dim_max_z = dim_z_[1];
359 T dim_min_z, T dim_max_z) {
360 dim_x_[0] = dim_min_x;
361 dim_x_[1] = dim_max_x;
362 dim_y_[0] = dim_min_y;
363 dim_y_[1] = dim_max_y;
364 dim_z_[0] = dim_min_z;
365 dim_z_[1] = dim_max_z;
369 T *voxel_width_z)
const {
371 *voxel_width_x = *voxel_width_y = *voxel_width_z =
static_cast<T
>(0);
374 *voxel_width_x = voxel_dim_[0];
375 *voxel_width_y = voxel_dim_[1];
376 *voxel_width_z = voxel_dim_[2];
381 voxel_dim_[0] = voxel_width_x;
382 voxel_dim_[1] = voxel_width_y;
383 voxel_dim_[2] = voxel_width_z;
389 nr_point_element_ = nr_point_element;
392 void SetNrVoxel(
unsigned int nr_voxel_x,
unsigned int nr_voxel_y,
393 unsigned int nr_voxel_z) {
394 nr_voxel_x_ = nr_voxel_x;
395 nr_voxel_y_ = nr_voxel_y;
396 nr_voxel_z_ = nr_voxel_z;
405 int WhichVoxel(T x, T y, T z)
const;
408 bool GetVoxelCoordinateXY(T x, T y,
int *row,
int *col)
const;
411 assert(i >= 0 && i < voxels_.size());
416 assert(i >= 0 && i < voxels_.size());
421 assert(i >= 0 && i < static_cast<int>(voxels_.size()));
425 assert(i >= 0 && i < static_cast<int>(voxels_.size()));
430 assert(iy < nr_voxel_y_ && ix < nr_voxel_x_);
431 unsigned int i = nr_voxel_x_ * iy + ix;
432 assert(i < voxels_.size());
437 assert(iy < nr_voxel_y_ && ix < nr_voxel_x_);
438 unsigned int i = nr_voxel_x_ * iy + ix;
439 assert(i < voxels_.size());
449 bool AllocAlignedMemory();
453 unsigned int nr_point_element_ = 0;
454 unsigned int nr_voxel_x_ = 0;
455 unsigned int nr_voxel_y_ = 0;
456 unsigned int nr_voxel_z_ = 0;
457 const T *
data_ =
nullptr;
458 bool initialized_ =
false;
459 T dim_x_[2], dim_y_[2], dim_z_[2], voxel_dim_[3];
460 float *mem_aligned16_f32_ =
nullptr;
461 int *mem_aligned16_i32_ =
nullptr;
462 std::vector<Voxel<T>> voxels_;
465 template <
typename T>
468 nr_point_element_(0),
474 mem_aligned16_f32_(nullptr),
475 mem_aligned16_i32_(nullptr) {
480 AllocAlignedMemory();
483 template <
typename T>
485 initialized_ =
false;
487 nr_points_ = nr_point_element_ = nr_voxel_x_ = nr_voxel_y_ = 0;
496 template <
typename T>
498 T spatial_bound_x_min, T spatial_bound_x_max,
499 T spatial_bound_y_min, T spatial_bound_y_max,
500 T spatial_bound_z_min, T spatial_bound_z_max) {
501 Alloc(nr_voxel_x, nr_voxel_y, spatial_bound_x_min, spatial_bound_x_max,
502 spatial_bound_y_min, spatial_bound_y_max, spatial_bound_z_min,
503 spatial_bound_z_max);
504 AllocAlignedMemory();
505 initialized_ =
false;
508 template <
typename T>
510 int r, c, i = 0, m = 0;
511 unsigned int n = (nr_voxel_x_ * nr_voxel_y_);
513 if (n == 0 || voxels_.size() != n) {
517 double cen_x = (
static_cast<double>(nr_voxel_x_ - 1)) * 0.5;
518 double cen_y = (
static_cast<double>(nr_voxel_y_ - 1)) * 0.5;
522 double sigma =
static_cast<double>(
IAverage(nr_voxel_x_, nr_voxel_y_) / 10.0);
523 double nf =
IDiv(0.5, (sigma * sigma));
524 double dr, drsqr, dc, dcsqr, v, ksum = 0.0;
525 std::vector<double> kernel(n, 0.0);
528 for (r = 0; r < static_cast<int>(nr_voxel_y_); ++r) {
529 dr =
static_cast<double>(r) - cen_y;
531 for (c = 0; c < static_cast<int>(nr_voxel_x_); ++c) {
532 dc =
static_cast<double>(c) - cen_x;
534 v =
IExp(-(drsqr + dcsqr) * nf);
542 for (i = 0; i < static_cast<int>(n); ++i) {
547 for (i = 0; i < static_cast<int>(n); ++i) {
548 m =
IMax(8, static_cast<int>(s_nr_max_reserved_points_ * kernel[i]));
549 voxels_[i].Reserve(m);
553 template <
typename T>
555 if (!mem_aligned16_f32_) {
556 mem_aligned16_f32_ = IAllocAligned<float>(4, 4);
558 if (!mem_aligned16_i32_) {
559 mem_aligned16_i32_ = IAllocAligned<int>(4, 4);
561 return (mem_aligned16_f32_ !=
nullptr && mem_aligned16_i32_ !=
nullptr &&
566 template <
typename T>
573 if (x < dim_x_[0] || x > dim_x_[1] || y < dim_y_[0] || y > dim_y_[1] ||
574 z < dim_z_[0] || z > dim_z_[1]) {
578 k =
static_cast<int>(
IMax(
580 IMin(nr_voxel_x_ - 1, (
unsigned int)((x - dim_x_[0]) / voxel_dim_[0]))));
581 j =
static_cast<int>(
IMax(
583 IMin(nr_voxel_y_ - 1, (
unsigned int)((y - dim_y_[0]) / voxel_dim_[1]))));
584 return (nr_voxel_x_ * j + k);
588 template <
typename T>
590 if (x < dim_x_[0] || x > dim_x_[1] || y < dim_y_[0] || y > dim_y_[1]) {
594 *col =
static_cast<int>((x - dim_x_[0]) / voxel_dim_[0]);
595 *row =
static_cast<int>((y - dim_y_[0]) / voxel_dim_[1]);
599 template <
typename T>
601 T spatial_bound_x_min, T spatial_bound_x_max,
602 T spatial_bound_y_min, T spatial_bound_y_max,
603 T spatial_bound_z_min, T spatial_bound_z_max) {
604 if (!nr_voxel_x || !nr_voxel_y) {
605 initialized_ =
false;
608 nr_voxel_x_ = nr_voxel_x;
609 nr_voxel_y_ = nr_voxel_y;
611 unsigned int i, j, k, n = 0;
612 unsigned int nr_voxel_xy = (nr_voxel_x_ * nr_voxel_y_);
613 unsigned int nr_voxel = nr_voxel_xy;
615 T vx, vy, vz, voxel_width_x, voxel_width_y, voxel_width_z;
618 voxels_.resize(nr_voxel);
621 dim_x_[0] = spatial_bound_x_min;
622 dim_x_[1] = spatial_bound_x_max;
624 dim_y_[0] = spatial_bound_y_min;
625 dim_y_[1] = spatial_bound_y_max;
627 dim_z_[0] = spatial_bound_z_min;
628 dim_z_[1] = spatial_bound_z_max;
630 T span_x = (dim_x_[1] - dim_x_[0]);
631 T span_y = (dim_y_[1] - dim_y_[0]);
632 T span_z = (dim_z_[1] - dim_z_[0]);
634 assert(span_x > 0 && span_y > 0 && span_z > 0);
636 voxel_dim_[0] = voxel_width_x =
IDiv(span_x, static_cast<int>(nr_voxel_x_));
637 voxel_dim_[1] = voxel_width_y =
IDiv(span_y, static_cast<int>(nr_voxel_y_));
638 voxel_dim_[2] = voxel_width_z = span_z;
640 std::vector<T> vxs(nr_voxel_x_);
643 for (i = 1; i < nr_voxel_x_; i++) {
644 vxs[i] = vxs[i - 1] + voxel_width_x;
650 for (j = 0; j < nr_voxel_y_; j++) {
651 for (k = 0; k < nr_voxel_x_; k++) {
653 voxels_[n++].Init(vx, vy, vz, voxel_width_x, voxel_width_y, voxel_width_z,
654 static_cast<int>(k), static_cast<int>(j), 0);
664 template <
typename T>
666 unsigned int nr_point_element) {
667 if (!data || !nr_points || !nr_point_element || !nr_voxel_x_ ||
668 !nr_voxel_y_ || nr_voxel_z_ != 1) {
669 initialized_ =
false;
674 nr_points_ = nr_points;
675 nr_point_element_ = nr_point_element;
677 unsigned int nr_voxel = (nr_voxel_x_ * nr_voxel_y_);
678 unsigned int i, nd = 0;
681 T voxel_width_x_rec =
IRec(voxel_dim_[0]);
682 T voxel_width_y_rec =
IRec(voxel_dim_[1]);
684 T bound_x_min = dim_x_[0];
685 T bound_x_max = dim_x_[1];
686 T bound_y_min = dim_y_[0];
687 T bound_y_max = dim_y_[1];
688 T bound_z_min = dim_z_[0];
689 T bound_z_max = dim_z_[1];
692 for (i = 0; i < nr_voxel; ++i) {
693 voxels_[i].indices_.clear();
697 for (; n < static_cast<int>(nr_points_); n++, nd += nr_point_element_) {
699 data_ + nd, bound_x_min, bound_x_max, bound_y_min, bound_y_max,
700 bound_z_min, bound_z_max, voxel_width_x_rec, voxel_width_y_rec,
701 static_cast<int>(nr_voxel_x_), static_cast<int>(nr_voxel_y_));
704 voxels_[id].indices_.push_back(n);
709 return (initialized_);
712 template <
typename T>
714 unsigned int nr_point_element) {
715 if (!data || !nr_points || !nr_point_element || !nr_voxel_x_ ||
716 !nr_voxel_y_ || nr_voxel_z_ != 1) {
717 initialized_ =
false;
722 nr_points_ = nr_points;
723 nr_point_element_ = nr_point_element;
725 unsigned int nr_voxel = (nr_voxel_x_ * nr_voxel_y_);
726 unsigned int i, n = 0;
729 float voxel_width_x_rec =
IRec(static_cast<float>(voxel_dim_[0]));
730 float voxel_width_y_rec =
IRec(static_cast<float>(voxel_dim_[1]));
732 __m128i iv_nr_voxel_x = _mm_setr_epi32(static_cast<int>(nr_voxel_x_), 0,
733 static_cast<int>(nr_voxel_x_), 0);
734 __m128i iv_nr_voxel_x_m1 = _mm_set1_epi32(static_cast<int>(nr_voxel_x_ - 1));
735 __m128i iv_nr_voxel_y_m1 = _mm_set1_epi32(static_cast<int>(nr_voxel_y_ - 1));
737 __m128 v_width_x_rec = _mm_set_ps1(voxel_width_x_rec);
738 __m128 v_width_y_rec = _mm_set_ps1(voxel_width_y_rec);
740 __m128 v_x_min = _mm_set_ps1(dim_x_[0]);
741 __m128 v_x_max = _mm_set_ps1(dim_x_[1]);
742 __m128 v_y_min = _mm_set_ps1(dim_y_[0]);
743 __m128 v_y_max = _mm_set_ps1(dim_y_[1]);
744 __m128 v_z_min = _mm_set_ps1(dim_z_[0]);
745 __m128 v_z_max = _mm_set_ps1(dim_z_[1]);
747 __m128 v_cmp_x, v_cmp_y, v_cmp_z, v_in_roi;
748 v_in_roi = _mm_setr_ps(1.0, 1.0, 1.0, 1.0);
750 __m128 v_xs, v_ys, v_zs;
751 __m128i iv_indices, iv_x_indices, iv_y_indices, iv_v_indices_02,
754 for (i = 0; i < nr_voxel; ++i) {
755 voxels_[i].indices_.clear();
758 unsigned int nr_loops = (nr_points >> 2);
759 unsigned int nr_fast_processed = (nr_loops << 2);
760 unsigned int remainder = nr_points - nr_fast_processed;
762 unsigned int d1 = nr_point_element;
763 unsigned int d2 = (nr_point_element << 1);
764 unsigned int d3 = d1 + d2;
765 unsigned int d4 = (nr_point_element << 2);
768 const float *cptr_x = data;
769 const float *cptr_y = data + 1;
770 const float *cptr_z = data + 2;
771 const float *cptr_remainder = data + (nr_fast_processed * nr_point_element);
773 for (i = 0; i < nr_loops; ++i, n += 4) {
775 v_xs = _mm_setr_ps(cptr_x[0], cptr_x[d1], cptr_x[d2], cptr_x[d3]);
776 v_ys = _mm_setr_ps(cptr_y[0], cptr_y[d1], cptr_y[d2], cptr_y[d3]);
777 v_zs = _mm_setr_ps(cptr_z[0], cptr_z[d1], cptr_z[d2], cptr_z[d3]);
781 _mm_and_ps(_mm_cmpge_ps(v_xs, v_x_min), _mm_cmple_ps(v_xs, v_x_max));
783 _mm_and_ps(_mm_cmpge_ps(v_ys, v_y_min), _mm_cmple_ps(v_ys, v_y_max));
785 _mm_and_ps(_mm_cmpge_ps(v_zs, v_z_min), _mm_cmple_ps(v_zs, v_z_max));
786 v_in_roi = _mm_and_ps(_mm_and_ps(v_cmp_x, v_cmp_y), v_cmp_z);
789 v_xs = _mm_sub_ps(v_xs, v_x_min);
790 v_xs = _mm_mul_ps(v_xs, v_width_x_rec);
791 iv_x_indices = _mm_cvttps_epi32(v_xs);
792 iv_x_indices = _mm_min_epi32(iv_nr_voxel_x_m1, iv_x_indices);
795 v_ys = _mm_sub_ps(v_ys, v_y_min);
796 v_ys = _mm_mul_ps(v_ys, v_width_y_rec);
797 iv_y_indices = _mm_cvttps_epi32(v_ys);
798 iv_y_indices = _mm_min_epi32(iv_nr_voxel_y_m1, iv_y_indices);
800 iv_v_indices_02 = _mm_mullo_epi32(iv_y_indices, iv_nr_voxel_x);
801 iv_y_indices = _mm_shuffle_epi32(iv_y_indices, _MM_SHUFFLE(2, 3, 0, 1));
803 iv_v_indices_13 = _mm_mullo_epi32(iv_y_indices, iv_nr_voxel_x);
805 _mm_shuffle_epi32(iv_v_indices_13, _MM_SHUFFLE(2, 3, 0, 1));
807 iv_indices = _mm_add_epi32(iv_v_indices_02, iv_v_indices_13);
808 iv_indices = _mm_add_epi32(iv_indices, iv_x_indices);
812 _mm_store_ps(mem_aligned16_f32_, v_in_roi);
814 _mm_store_si128(reinterpret_cast<__m128i *>(mem_aligned16_i32_),
817 if (mem_aligned16_f32_[0] != 0) {
818 voxels_[mem_aligned16_i32_[0]].indices_.push_back(n);
820 if (mem_aligned16_f32_[1] != 0) {
821 voxels_[mem_aligned16_i32_[1]].indices_.push_back(n + 1);
823 if (mem_aligned16_f32_[2] != 0) {
824 voxels_[mem_aligned16_i32_[2]].indices_.push_back(n + 2);
826 if (mem_aligned16_f32_[3] != 0) {
827 voxels_[mem_aligned16_i32_[3]].indices_.push_back(n + 3);
835 for (i = 0; i < remainder; i++, n++) {
837 dim_y_[1], dim_z_[0], dim_z_[1], voxel_width_x_rec,
838 voxel_width_y_rec, static_cast<int>(nr_voxel_x_),
839 static_cast<int>(nr_voxel_y_));
842 voxels_[id].indices_.push_back(n);
845 cptr_remainder += nr_point_element;
849 return (initialized_);
852 template <
typename T>
854 unsigned int nr_point_element,
unsigned int nr_voxel_x,
855 unsigned int nr_voxel_y, T spatial_bound_x_min,
856 T spatial_bound_x_max, T spatial_bound_y_min,
857 T spatial_bound_y_max, T spatial_bound_z_min,
858 T spatial_bound_z_max,
bool force_bound) {
859 if (!data || !nr_points || !nr_point_element || !nr_voxel_x || !nr_voxel_y) {
860 initialized_ =
false;
865 nr_points_ = nr_points;
866 nr_point_element_ = nr_point_element;
867 nr_voxel_x_ = nr_voxel_x;
868 nr_voxel_y_ = nr_voxel_y;
871 unsigned int i, j, k, n = 0, nd = 0;
872 unsigned int nr_voxel_xy = (nr_voxel_x_ * nr_voxel_y_);
873 unsigned int nr_voxel = nr_voxel_xy;
874 unsigned int nr_voxel_xm1 = nr_voxel_x - 1;
875 unsigned int nr_voxel_ym1 = nr_voxel_y - 1;
876 T vx, vy, vz, x, y, z;
877 T voxel_width_x, voxel_width_y, voxel_width_z;
880 voxels_.resize(nr_voxel);
885 dim_x_[0] = spatial_bound_x_min;
886 dim_x_[1] = spatial_bound_x_max;
888 dim_y_[0] = spatial_bound_y_min;
889 dim_y_[1] = spatial_bound_y_max;
891 dim_z_[0] = spatial_bound_z_min;
892 dim_z_[1] = spatial_bound_z_max;
895 data_, nr_points_, 0, nr_point_element_, &dim_x_[0], &dim_x_[1],
896 &dim_y_[0], &dim_y_[1], &dim_z_[0], &dim_z_[1], spatial_bound_x_min,
897 spatial_bound_x_max, spatial_bound_y_min, spatial_bound_y_max,
898 spatial_bound_z_min, spatial_bound_z_max);
901 T dim_x_min = dim_x_[0];
902 T dim_x_max = dim_x_[1];
904 T dim_y_min = dim_y_[0];
905 T dim_y_max = dim_y_[1];
907 T dim_z_min = dim_z_[0];
908 T dim_z_max = dim_z_[1];
910 T span_x = (dim_x_max - dim_x_min);
911 T span_y = (dim_y_max - dim_y_min);
912 T span_z = (dim_z_max - dim_z_min);
914 assert(span_x > 0 && span_y > 0 && span_z > 0);
916 voxel_dim_[0] = voxel_width_x =
IDiv(span_x, static_cast<int>(nr_voxel_x_));
917 voxel_dim_[1] = voxel_width_y =
IDiv(span_y, static_cast<int>(nr_voxel_y_));
918 voxel_dim_[2] = voxel_width_z = span_z;
920 T voxel_width_x_rec =
IRec(voxel_dim_[0]);
921 T voxel_width_y_rec =
IRec(voxel_dim_[1]);
923 std::vector<T> vxs(nr_voxel_x_);
926 for (i = 1; i < nr_voxel_x_; i++) {
927 vxs[i] = vxs[i - 1] + voxel_width_x;
933 for (j = 0; j < nr_voxel_y_; j++) {
934 for (k = 0; k < nr_voxel_x_; k++) {
936 voxels_[n++].init(vx, vy, vz, voxel_width_x, voxel_width_y, voxel_width_z,
937 static_cast<int>(k), static_cast<int>(j), 0);
943 for (; n < nr_points_; n++, nd += nr_point_element_) {
948 if (x < dim_x_min || x > dim_x_max || y < dim_y_min || y > dim_y_max ||
949 z < dim_z_min || z > dim_z_max) {
954 k =
IMax((
unsigned int)0,
956 (
unsigned int)((x - dim_x_min) * voxel_width_x_rec)));
957 j =
IMax((
unsigned int)0,
959 (
unsigned int)((y - dim_y_min) * voxel_width_y_rec)));
960 i = nr_voxel_x * j + k;
962 assert(i < nr_voxel);
963 voxels_[i].indices_.push_back(n);
986 template <
typename T>
991 unsigned int i, n = 0;
992 for (i = 0; i < voxels_.size(); ++i) {
993 n += voxels_[i].indices_.size();
998 template <
typename T>
999 static void IPushBackVectors(
const std::vector<T> &src, std::vector<T> *dst) {
1000 unsigned int i = src.size();
1001 unsigned int j = dst->size();
1005 }
else if (j == 0) {
1006 dst->assign(src.begin(), src.end());
1009 ICopy(src.data(), dst->data() + j,
static_cast<int>(i));
1014 template <
typename T>
1016 unsigned int dsf_dim_x,
unsigned int dsf_dim_y) {
1019 }
else if (dsf_dim_x == 0 && dsf_dim_y == 0) {
1031 unsigned int nr_voxel_x_src = src.
NrVoxelX();
1032 unsigned int nr_voxel_y_src = src.
NrVoxelY();
1033 unsigned int nr_voxel_z_src = src.
NrVoxelZ();
1035 assert(nr_voxel_z_src == 1);
1038 unsigned int sf_x = (
unsigned int)
IPow((
unsigned int)2, dsf_dim_x);
1039 unsigned int sf_y = (
unsigned int)
IPow((
unsigned int)2, dsf_dim_y);
1042 unsigned int nr_voxel_x_dst = nr_voxel_x_src / sf_x;
1043 unsigned int nr_voxel_y_dst = nr_voxel_y_src / sf_y;
1044 unsigned int nr_voxel_z_dst = nr_voxel_z_src;
1046 if (!nr_voxel_x_dst || !nr_voxel_y_dst || !nr_voxel_z_dst) {
1050 unsigned int nr_res_voxel_x_dst = (nr_voxel_x_src % sf_x);
1051 unsigned int nr_res_voxel_y_dst = (nr_voxel_y_src % sf_y);
1053 T voxel_width_x_src = 0;
1054 T voxel_width_y_src = 0;
1055 T voxel_width_z_src = 0;
1056 T dim_min_x_src = 0;
1057 T dim_max_x_src = 0;
1058 T dim_min_y_src = 0;
1059 T dim_max_y_src = 0;
1060 T dim_min_z_src = 0;
1061 T dim_max_z_src = 0;
1065 &voxel_width_z_src);
1068 &dim_max_y_src, &dim_min_z_src, &dim_max_z_src);
1071 T voxel_width_x_dst = (sf_x * voxel_width_x_src);
1072 T voxel_width_y_dst = (sf_y * voxel_width_y_src);
1073 T voxel_width_z_dst = voxel_width_z_src;
1076 T dim_x_dst[2], dim_y_dst[2], dim_z_dst[2];
1078 dim_x_dst[0] = dim_min_x_src;
1079 dim_y_dst[0] = dim_min_y_src;
1080 dim_z_dst[0] = dim_min_z_src;
1082 dim_x_dst[1] = (0 == nr_res_voxel_x_dst)
1084 : (voxel_width_x_dst * nr_voxel_x_dst + dim_x_dst[0]);
1085 dim_y_dst[1] = (0 == nr_res_voxel_y_dst)
1087 : (voxel_width_y_dst * nr_voxel_y_dst + dim_y_dst[0]);
1088 dim_z_dst[1] = dim_max_z_src;
1095 dst->
SetGridDimension(dim_x_dst[0], dim_x_dst[1], dim_y_dst[0], dim_y_dst[1],
1096 dim_z_dst[0], dim_z_dst[1]);
1097 dst->
SetNrVoxel(nr_voxel_x_dst, nr_voxel_y_dst, nr_voxel_z_dst);
1100 unsigned int i, j, r, c, rs, cs, rspi, n = 0;
1102 T vy, vz = dim_z_dst[0];
1103 std::vector<T> vxs(nr_voxel_x_dst);
1105 vxs[0] = dim_x_dst[0];
1107 for (i = 1; i < nr_voxel_x_dst; i++) {
1108 vxs[i] = vxs[i - 1] + voxel_width_x_dst;
1111 std::vector<Voxel<T>> voxels(nr_voxel_y_dst * nr_voxel_x_dst);
1113 for (r = 0; r < nr_voxel_y_dst; r++) {
1115 vy = dim_y_dst[0] + (r * voxel_width_y_dst);
1117 for (c = 0; c < nr_voxel_x_dst; c++) {
1119 voxels[n].init(vxs[c], vy, vz, voxel_width_x_dst, voxel_width_y_dst,
1120 voxel_width_z_dst, static_cast<int>(c),
1121 static_cast<int>(r), static_cast<int>(0));
1123 for (i = 0; i < sf_y; ++i) {
1125 for (j = 0; j < sf_x; ++j) {
1126 IPushBackVectors(src(rspi, cs + j).indices_, &voxels[n].indices_);
1139 template <
typename DATA_TYPE>
1144 unsigned int nr_voxel_y_base,
unsigned int dsf_x,
1145 unsigned int dsf_y, DATA_TYPE spatial_bound_x_min,
1146 DATA_TYPE spatial_bound_x_max,
1147 DATA_TYPE spatial_bound_y_min,
1148 DATA_TYPE spatial_bound_y_max,
1149 DATA_TYPE spatial_bound_z_min,
1150 DATA_TYPE spatial_bound_z_max) {
1151 Alloc(nr_scale, nr_voxel_x_base, nr_voxel_y_base, dsf_x, dsf_y,
1152 spatial_bound_x_min, spatial_bound_x_max, spatial_bound_y_min,
1153 spatial_bound_y_max, spatial_bound_z_min, spatial_bound_z_max);
1160 bool Alloc(
unsigned int nr_scale,
unsigned int nr_voxel_x_base,
1161 unsigned int nr_voxel_y_base,
unsigned int dsf_x,
1162 unsigned int dsf_y, DATA_TYPE spatial_bound_x_min,
1163 DATA_TYPE spatial_bound_x_max, DATA_TYPE spatial_bound_y_min,
1164 DATA_TYPE spatial_bound_y_max, DATA_TYPE spatial_bound_z_min,
1165 DATA_TYPE spatial_bound_z_max);
1168 bool Set(
const DATA_TYPE *pc,
unsigned int nr_points,
1169 unsigned int nr_point_element);
1173 bool SetS(
const float *pc,
unsigned int nr_points,
1174 unsigned int nr_point_element);
1176 bool Set(
const DATA_TYPE *pc,
unsigned int nr_scale,
unsigned int nr_points,
1177 unsigned int nr_point_element,
unsigned int nr_voxel_x_base,
1178 unsigned int nr_voxel_y_base,
unsigned int dsf_x,
unsigned int dsf_y,
1179 DATA_TYPE spatial_bound_x_min, DATA_TYPE spatial_bound_x_max,
1180 DATA_TYPE spatial_bound_y_min, DATA_TYPE spatial_bound_y_max,
1181 DATA_TYPE spatial_bound_z_min, DATA_TYPE spatial_bound_z_max);
1183 unsigned int NrScale()
const {
return (
unsigned int)vgrids_.size(); }
1186 return (i < vgrids_.size()) ? vgrids_[i].nr_voxel() : 0;
1190 return (i < vgrids_.size()) ? vgrids_[i].nr_voxel_x() : 0;
1194 return (i < vgrids_.size()) ? vgrids_[i].nr_voxel_y() : 0;
1198 return (i < vgrids_.size()) ? vgrids_[i].nr_voxel_z() : 0;
1202 return (scale < vgrids_.size()) ? vgrids_[scale].nr_points() : 0;
1209 bool Initialized()
const;
1216 assert(i >= 0 && i < vgrids_.size());
1221 assert(i >= 0 && i < vgrids_.size());
1226 assert(i >= 0 && i < static_cast<int>(vgrids_.size()));
1231 assert(i >= 0 && i < static_cast<int>(vgrids_.size()));
1240 unsigned int nr_points_, nr_point_element_;
1241 unsigned int dsf_x_, dsf_y_, dsf_z_;
1242 const DATA_TYPE *pc_;
1243 std::vector<VoxelGridXY<DATA_TYPE>> vgrids_;
1246 template <
typename DATA_TYPE>
1250 nr_point_element_(0),
1255 template <
typename DATA_TYPE>
1257 unsigned int nr_scale,
unsigned int nr_voxel_x_base,
1258 unsigned int nr_voxel_y_base,
unsigned int dsf_x,
unsigned int dsf_y,
1259 DATA_TYPE spatial_bound_x_min, DATA_TYPE spatial_bound_x_max,
1260 DATA_TYPE spatial_bound_y_min, DATA_TYPE spatial_bound_y_max,
1261 DATA_TYPE spatial_bound_z_min, DATA_TYPE spatial_bound_z_max) {
1262 if (!nr_scale || !nr_voxel_x_base || !nr_voxel_y_base) {
1267 unsigned int nr_voxel_x = nr_voxel_x_base;
1268 unsigned int nr_voxel_y = nr_voxel_y_base;
1269 unsigned int sf_x = (
unsigned int)
IPow((
unsigned int)2, dsf_x);
1270 unsigned int sf_y = (
unsigned int)
IPow((
unsigned int)2, dsf_y);
1277 vgrids_.resize(nr_scale);
1279 for (scale = 0; scale < nr_scale; ++scale) {
1280 if (!vgrids_[scale].alloc(nr_voxel_x, nr_voxel_y, spatial_bound_x_min,
1281 spatial_bound_x_max, spatial_bound_y_min,
1282 spatial_bound_y_max, spatial_bound_z_min,
1283 spatial_bound_z_max)) {
1294 if (scale != nr_scale) {
1295 vgrids_.resize(scale);
1302 template <
typename DATA_TYPE>
1304 unsigned int nr_points,
1305 unsigned int nr_point_element) {
1306 if (!pc || !nr_points || !nr_point_element || vgrids_.empty()) {
1310 unsigned int scale, nr_scale = (
unsigned int)vgrids_.size();
1313 nr_points_ = nr_points;
1314 nr_point_element_ = nr_point_element;
1316 for (scale = 0; scale < nr_scale; ++scale) {
1317 if (!vgrids_[scale].
set(pc, nr_points, nr_point_element)) {
1323 if (scale < nr_scale) {
1324 vgrids_.resize(scale);
1327 return (vgrids_.size() == nr_scale);
1330 template <
typename DATA_TYPE>
1332 unsigned int nr_points,
1333 unsigned int nr_point_element) {
1334 if (!pc || !nr_points || !nr_point_element || vgrids_.empty()) {
1338 unsigned int scale, nr_scale = (
unsigned int)vgrids_.size();
1341 nr_points_ = nr_points;
1342 nr_point_element_ = nr_point_element;
1344 for (scale = 0; scale < nr_scale; ++scale) {
1345 if (!vgrids_[scale].set_s(pc, nr_points, nr_point_element)) {
1351 if (scale < nr_scale) {
1352 vgrids_.resize(scale);
1355 return (vgrids_.size() == nr_scale);
1358 template <
typename DATA_TYPE>
1360 const DATA_TYPE *pc,
unsigned int nr_scale,
unsigned int nr_points,
1361 unsigned int nr_point_element,
unsigned int nr_voxel_x_base,
1362 unsigned int nr_voxel_y_base,
unsigned int dsf_x,
unsigned int dsf_y,
1363 DATA_TYPE spatial_bound_x_min, DATA_TYPE spatial_bound_x_max,
1364 DATA_TYPE spatial_bound_y_min, DATA_TYPE spatial_bound_y_max,
1365 DATA_TYPE spatial_bound_z_min, DATA_TYPE spatial_bound_z_max) {
1366 if (!pc || !nr_scale || !nr_points || !nr_point_element || !nr_voxel_x_base ||
1373 nr_points_ = nr_points;
1374 nr_point_element_ = nr_point_element;
1381 vgrids_.resize(nr_scale);
1384 if (!vgrids_[0].
set(pc_, nr_points, nr_point_element, nr_voxel_x_base,
1385 nr_voxel_y_base, spatial_bound_x_min, spatial_bound_x_max,
1386 spatial_bound_y_min, spatial_bound_y_max,
1387 spatial_bound_z_min, spatial_bound_z_max)) {
1392 for (s = 1; s < nr_scale; ++s) {
1399 for (i = 0; i < (nr_scale - s); ++i) {
1404 return (vgrids_.size() == nr_scale);
1407 template <
typename DATA_TYPE>
1411 nr_points_ = nr_point_element_ = dsf_x_ = dsf_y_ = dsf_z_ = 0;
1414 template <
typename DATA_TYPE>
1417 if (vgrids_.empty()) {
1420 for (i = 0; i < vgrids_.size(); ++i) {
1421 if (!vgrids_[i].initialized()) {
1425 return (i == vgrids_.size());
unsigned int NrVoxelX(unsigned int i=0) const
Definition: i_struct_s.h:1189
void ICopy(const T *src, T *dst, int n)
Definition: i_blas.h:27
Voxel< T > & operator[](int i)
Definition: i_struct_s.h:420
const T * ConstData() const
Definition: i_struct_s.h:48
const T * const_iterator
Definition: i_struct_s.h:32
unsigned int NrPoints(unsigned int scale) const
Definition: i_struct_s.h:1201
void CleanUp()
Definition: i_struct_s.h:1408
unsigned int NrVoxel(unsigned int i=0) const
Definition: i_struct_s.h:1185
void IZero3(T a[3])
Definition: i_blas.h:334
const VoxelGridXY< DATA_TYPE > & operator[](int i) const
Definition: i_struct_s.h:1230
VoxelGridXY()
Definition: i_struct_s.h:466
~VoxelGridXYPyramid()
Definition: i_struct_s.h:1158
void IGetPointcloudsDimWBound(const T *threeds, int n, int start_offset, int element_size, T *dim_min_x, T *dim_max_x, T *dim_min_y, T *dim_max_y, T *dim_min_z, T *dim_max_z, T bound_min_x, T bound_max_x, T bound_min_y, T bound_max_y, T bound_min_z, T bound_max_z)
Definition: i_util.h:27
int IVerifyAlignment(const T *mem, int alignment_power=4)
Definition: i_alloc.h:150
T * Data()
Definition: i_struct_s.h:50
void Init(const T *v, T dim_x, T dim_y, T dim_z, int ix, int iy, int iz)
Definition: i_struct_s.h:207
Voxel()
Definition: i_struct_s.h:180
void SetNrPointElement(unsigned int nr_point_element)
Definition: i_struct_s.h:388
unsigned int NrPointElement() const
Definition: i_struct_s.h:330
VoxelGridXY< DATA_TYPE > & operator[](unsigned int i)
Definition: i_struct_s.h:1215
void SetPointCloudsData(const T *data)
Definition: i_struct_s.h:399
int ix_
Definition: i_struct_s.h:252
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
const_iterator End() const
Definition: i_struct_s.h:109
void ICopy3(const T src[3], T dst[3])
Definition: i_blas.h:40
T * data_
Definition: i_struct_s.h:113
unsigned int NrVoxelX() const
Definition: i_struct_s.h:322
float IRec(float a)
Definition: i_basic.h:69
const VoxelGridXY< DATA_TYPE > & operator[](unsigned int i) const
Definition: i_struct_s.h:1220
bool Empty() const
Definition: i_struct_s.h:247
T * operator[](unsigned int i)
Definition: i_struct_s.h:81
bool GetVoxelDimension(T *voxel_width_x, T *voxel_width_y, T *voxel_width_z) const
Definition: i_struct_s.h:368
VoxelGridXY< DATA_TYPE > & operator[](int i)
Definition: i_struct_s.h:1225
~Voxel()
Definition: i_struct_s.h:181
float IPow(float a, float b)
Definition: i_basic.h:142
T IMax(T a, T b)
Definition: i_basic.h:161
void CleanUp()
Definition: i_struct_s.h:146
Voxel & operator=(const Voxel< T > &voxel)
Definition: i_struct_s.h:193
float ISqr(float a)
Definition: i_basic.h:103
const std::vector< Voxel< T > > & GetConstVoxels() const
Definition: i_struct_s.h:336
unsigned int NrScale() const
Definition: i_struct_s.h:1183
bool Set(const DATA_TYPE *pc, unsigned int nr_points, unsigned int nr_point_element)
Definition: i_struct_s.h:1303
std::vector< Voxel< T > > & GetVoxels()
Definition: i_struct_s.h:334
Definition: i_struct_s.h:260
unsigned int NrPoints() const
Definition: i_struct_s.h:328
float IExp(float x)
Definition: i_basic.h:136
unsigned int GetDsfZ() const
Definition: i_struct_s.h:1213
void PushBack(int id)
Definition: i_struct_s.h:241
unsigned int NrVoxelZ() const
Definition: i_struct_s.h:326
const_reference operator()(unsigned int i, unsigned int j) const
Definition: i_struct_s.h:98
static unsigned int PointsizeInByte()
Definition: i_struct_s.h:38
const Voxel< T > & operator[](unsigned int i) const
Definition: i_struct_s.h:415
DATA_TYPE * Data()
Definition: i_struct_s.h:1237
float IDiv(float a, float b)
Definition: i_basic.h:35
void Init(T v_x, T v_y, T v_z, T dim_x, T dim_y, T dim_z, int ix, int iy, int iz)
Definition: i_struct_s.h:218
Voxel< T > & operator[](unsigned int i)
Definition: i_struct_s.h:410
VoxelGridXY & operator=(const VoxelGridXY< T > &vg)
Definition: i_struct_s.h:271
unsigned int ClustersizeInByte() const
Definition: i_struct_s.h:40
std::vector< int > indices_
Definition: i_struct_s.h:255
Voxel< T > & operator()(unsigned int iy, unsigned int ix)
Definition: i_struct_s.h:429
void SetVoxels(const std::vector< Voxel< T >> &voxels)
Definition: i_struct_s.h:338
bool SetS(const float *pc, unsigned int nr_points, unsigned int nr_point_element)
Definition: i_struct_s.h:1331
unsigned int NrPoints() const
Definition: i_struct_s.h:44
unsigned int GetDsfY() const
Definition: i_struct_s.h:1212
unsigned int NrVoxelY(unsigned int i=0) const
Definition: i_struct_s.h:1193
unsigned int NrVoxelY() const
Definition: i_struct_s.h:324
unsigned int GetDsfX() const
Definition: i_struct_s.h:1211
Definition: i_struct_s.h:178
T * Data()
Definition: i_struct_s.h:443
int iz_
Definition: i_struct_s.h:252
T dim_z_
Definition: i_struct_s.h:249
bool IDownsampleVoxelGridXY(const VoxelGridXY< T > &src, VoxelGridXY< T > *dst, unsigned int dsf_dim_x, unsigned int dsf_dim_y)
Definition: i_struct_s.h:1015
T IAverage(T a, T b)
Definition: i_basic.h:167
const Voxel< T > & operator()(unsigned int iy, unsigned int ix) const
Definition: i_struct_s.h:436
void Reserve(unsigned int size)
Definition: i_struct_s.h:239
PtCluster & operator=(const PtCluster< T, d > &c)
Definition: i_struct_s.h:66
unsigned int NrVoxel() const
Definition: i_struct_s.h:320
unsigned int NrPointElement() const
Definition: i_struct_s.h:1207
const_iterator Begin() const
Definition: i_struct_s.h:107
virtual ~PtCluster()
Definition: i_struct_s.h:79
const Voxel< T > & operator[](int i) const
Definition: i_struct_s.h:424
const DATA_TYPE * ConstData() const
Definition: i_struct_s.h:1235
void SetVoxelDimension(T voxel_width_x, T voxel_width_y, T voxel_width_z)
Definition: i_struct_s.h:380
unsigned int NrVoxelZ(unsigned int i=0) const
Definition: i_struct_s.h:1197
VoxelGridXYPyramid(unsigned int nr_scale, unsigned int nr_voxel_x_base, unsigned int nr_voxel_y_base, unsigned int dsf_x, unsigned int dsf_y, DATA_TYPE spatial_bound_x_min, DATA_TYPE spatial_bound_x_max, DATA_TYPE spatial_bound_y_min, DATA_TYPE spatial_bound_y_max, DATA_TYPE spatial_bound_z_min, DATA_TYPE spatial_bound_z_max)
Definition: i_struct_s.h:1143
T dim_y_
Definition: i_struct_s.h:249
T & reference
Definition: i_struct_s.h:33
void CleanUp()
Definition: i_struct_s.h:484
bool GetGridDimension(T *dim_min_x, T *dim_max_x, T *dim_min_y, T *dim_max_y, T *dim_min_z, T *dim_max_z) const
Definition: i_struct_s.h:342
iterator End()
Definition: i_struct_s.h:105
T IMin(T a, T b)
Definition: i_basic.h:155
iterator Begin()
Definition: i_struct_s.h:103
const T * const_data() const
Definition: i_struct_s.h:445
void SetNrPoints(unsigned int nr_points)
Definition: i_struct_s.h:386
unsigned int nr_points_
Definition: i_struct_s.h:114
unsigned int Capacity() const
Definition: i_struct_s.h:243
Definition: i_struct_s.h:1140
int iy_
Definition: i_struct_s.h:252
T v_[3]
Definition: i_struct_s.h:249
T dim_x_
Definition: i_struct_s.h:249
Voxel(const Voxel< T > &voxel)
Definition: i_struct_s.h:182
bool Initialized() const
Definition: i_struct_s.h:1415
void SetInitialized(bool initialized)
Definition: i_struct_s.h:401
void IZero2(T a[2])
Definition: i_blas.h:330
bool Initialized() const
Definition: i_struct_s.h:318
PtCluster()
Definition: i_struct_s.h:118
const T & const_reference
Definition: i_struct_s.h:34
int IAssignPointToVoxel(const T *data, T bound_x_min, T bound_x_max, T bound_y_min, T bound_y_max, T bound_z_min, T bound_z_max, T voxel_width_x_rec, T voxel_width_y_rec, int nr_voxel_x, int nr_voxel_y)
Definition: i_struct_s.h:152
Definition: i_struct_s.h:29
bool Initialized() const
Definition: i_struct_s.h:46
void IFreeAligned(T **mem)
Definition: i_alloc.h:142
void Reset()
Definition: i_struct_s.h:232
void SetGridDimension(T dim_min_x, T dim_max_x, T dim_min_y, T dim_max_y, T dim_min_z, T dim_max_z)
Definition: i_struct_s.h:358
unsigned int NrPoints() const
Definition: i_struct_s.h:1205
T * iterator
Definition: i_struct_s.h:31
static unsigned int NrPointElement()
Definition: i_struct_s.h:36
~VoxelGridXY()
Definition: i_struct_s.h:294
PtCluster(const PtCluster< T, d > &c)
Definition: i_struct_s.h:57
void SetNrVoxel(unsigned int nr_voxel_x, unsigned int nr_voxel_y, unsigned int nr_voxel_z)
Definition: i_struct_s.h:392
reference operator()(unsigned int i, unsigned int j)
Definition: i_struct_s.h:93
bool Alloc(unsigned int nr_scale, unsigned int nr_voxel_x_base, unsigned int nr_voxel_y_base, unsigned int dsf_x, unsigned int dsf_y, DATA_TYPE spatial_bound_x_min, DATA_TYPE spatial_bound_x_max, DATA_TYPE spatial_bound_y_min, DATA_TYPE spatial_bound_y_max, DATA_TYPE spatial_bound_z_min, DATA_TYPE spatial_bound_z_max)
Definition: i_struct_s.h:1256
const T * operator[](unsigned int i) const
Definition: i_struct_s.h:87
unsigned int NrPoints() const
Definition: i_struct_s.h:245