47 namespace voxel_grid {
52 #if defined(__CUDACC__) 53 void GetVoxelCoordinatesAndFlattenedIndicesCUDA
68 float* voxel_coords_ptr = voxel_coords.GetDataPtr<
float>();
69 int64_t* flattened_indices_ptr = flattened_indices.GetDataPtr<int64_t>();
71 index_t n = flattened_indices.GetLength();
72 ArrayIndexer voxel_indexer({resolution, resolution, resolution});
73 index_t resolution3 = resolution * resolution * resolution;
76 index_t block_idx = buf_indices_ptr[workload_idx / resolution3];
77 index_t voxel_idx = workload_idx % resolution3;
79 index_t block_key_offset = block_idx * 3;
80 index_t xb = block_key_ptr[block_key_offset + 0];
81 index_t yb = block_key_ptr[block_key_offset + 1];
82 index_t zb = block_key_ptr[block_key_offset + 2];
85 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
87 float x = (xb * resolution + xv) * voxel_size;
88 float y = (yb * resolution + yv) * voxel_size;
89 float z = (zb * resolution + zv) * voxel_size;
91 flattened_indices_ptr[workload_idx] =
92 block_idx * resolution3 + voxel_idx;
94 index_t voxel_coords_offset = workload_idx * 3;
95 voxel_coords_ptr[voxel_coords_offset + 0] = x;
96 voxel_coords_ptr[voxel_coords_offset + 1] = y;
97 voxel_coords_ptr[voxel_coords_offset + 2] = z;
109 index_t xn = (xo + resolution) % resolution;
110 index_t yn = (yo + resolution) % resolution;
111 index_t zn = (zo + resolution) % resolution;
117 index_t nb_idx = (dxb + 1) + (dyb + 1) * 3 + (dzb + 1) * 9;
120 *nb_block_masks_indexer.
GetDataPtr<
bool>(curr_block_idx, nb_idx);
121 if (!block_mask_i)
return -1;
124 curr_block_idx, nb_idx);
126 return (((block_idx_i * resolution) + zn) * resolution + yn) * resolution +
130 template <
typename tsdf_t>
132 const tsdf_t* tsdf_base_ptr,
144 nb_block_masks_indexer,
145 nb_block_indices_indexer);
147 index_t vxp = GetLinearIdx(xo + 1, yo, zo);
148 index_t vxn = GetLinearIdx(xo - 1, yo, zo);
149 index_t vyp = GetLinearIdx(xo, yo + 1, zo);
150 index_t vyn = GetLinearIdx(xo, yo - 1, zo);
151 index_t vzp = GetLinearIdx(xo, yo, zo + 1);
152 index_t vzn = GetLinearIdx(xo, yo, zo - 1);
153 if (vxp >= 0 && vxn >= 0) n[0] = tsdf_base_ptr[vxp] - tsdf_base_ptr[vxn];
154 if (vyp >= 0 && vyn >= 0) n[1] = tsdf_base_ptr[vyp] - tsdf_base_ptr[vyn];
155 if (vzp >= 0 && vzn >= 0) n[2] = tsdf_base_ptr[vzp] - tsdf_base_ptr[vzn];
158 template <
typename input_depth_t,
159 typename input_color_t,
163 #if defined(__CUDACC__) 182 index_t resolution2 = resolution * resolution;
183 index_t resolution3 = resolution2 * resolution;
185 TransformIndexer transform_indexer(depth_intrinsic, extrinsics, voxel_size);
190 ArrayIndexer voxel_indexer({resolution, resolution, resolution});
198 if (!block_value_map.Contains(
"tsdf") ||
199 !block_value_map.Contains(
"weight")) {
201 "TSDF and/or weight not allocated in blocks, please implement " 202 "customized integration.");
204 tsdf_t* tsdf_base_ptr = block_value_map.at(
"tsdf").GetDataPtr<tsdf_t>();
205 weight_t* weight_base_ptr =
206 block_value_map.at(
"weight").GetDataPtr<weight_t>();
208 bool integrate_color =
209 block_value_map.Contains(
"color") && color.NumElements() > 0;
210 color_t* color_base_ptr =
nullptr;
213 float color_multiplier = 1.0;
214 if (integrate_color) {
215 color_base_ptr = block_value_map.at(
"color").
GetDataPtr<color_t>();
220 color_multiplier = 255.0;
224 index_t n = indices.GetLength() * resolution3;
227 index_t block_idx = indices_ptr[workload_idx / resolution3];
228 index_t voxel_idx = workload_idx % resolution3;
233 block_keys_indexer.GetDataPtr<
index_t>(block_idx);
240 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
243 index_t x = xb * resolution + xv;
244 index_t y = yb * resolution + yv;
245 index_t z = zb * resolution + zv;
248 float xc, yc, zc, u, v;
250 static_cast<float>(y),
251 static_cast<float>(z), &xc, &yc, &zc);
254 transform_indexer.
Project(xc, yc, zc, &u, &v);
265 *depth_indexer.
GetDataPtr<input_depth_t>(ui, vi) / depth_scale;
267 float sdf = depth - zc;
268 if (depth <= 0 || depth > depth_max || zc <= 0 || sdf < -sdf_trunc) {
271 sdf = sdf < sdf_trunc ? sdf : sdf_trunc;
274 index_t linear_idx = block_idx * resolution3 + voxel_idx;
276 tsdf_t* tsdf_ptr = tsdf_base_ptr + linear_idx;
277 weight_t* weight_ptr = weight_base_ptr + linear_idx;
279 float inv_wsum = 1.0f / (*weight_ptr + 1);
280 float weight = *weight_ptr;
281 *tsdf_ptr = (weight * (*tsdf_ptr) + sdf) * inv_wsum;
283 if (integrate_color) {
284 color_t* color_ptr = color_base_ptr + 3 * linear_idx;
289 transform_indexer.
Unproject(ui, vi, 1.0, &x, &y, &z);
292 colormap_indexer.
Project(x, y, z, &uf, &vf);
297 input_color_t* input_color_ptr =
298 color_indexer.
GetDataPtr<input_color_t>(ui, vi);
300 for (
index_t i = 0; i < 3; ++i) {
301 color_ptr[i] = (weight * color_ptr[i] +
302 input_color_ptr[i] * color_multiplier) *
307 *weight_ptr = weight + 1;
310 #if defined(__CUDACC__) 315 #if defined(__CUDACC__) 316 void EstimateRangeCUDA
327 int64_t block_resolution,
335 int h_down = h / down_factor;
336 int w_down = w / down_factor;
342 const int fragment_size = 16;
343 const int frag_buffer_size = 65535;
352 #if defined(__CUDACC__) 355 int* count_ptr =
count.GetDataPtr<
int>();
357 std::atomic<int> count_atomic(0);
358 std::atomic<int>* count_ptr = &count_atomic;
370 int* key = block_keys_indexer.
GetDataPtr<
int>(workload_idx);
372 int u_min = w_down - 1, v_min = h_down - 1, u_max = 0,
374 float z_min = depth_max, z_max = depth_min;
376 float xc, yc, zc, u, v;
379 for (
int i = 0; i < 8; ++i) {
380 float xw = (key[0] + ((i & 1) > 0)) * block_resolution *
382 float yw = (key[1] + ((i & 2) > 0)) * block_resolution *
384 float zw = (key[2] + ((i & 4) > 0)) * block_resolution *
389 if (zc <= 0)
continue;
392 w2c_transform_indexer.
Project(xc, yc, zc, &u, &v);
396 v_min = min(static_cast<int>(floorf(v)), v_min);
397 v_max = max(static_cast<int>(ceilf(v)), v_max);
399 u_min = min(static_cast<int>(floorf(u)), u_min);
400 u_max = max(static_cast<int>(ceilf(u)), u_max);
402 z_min = min(z_min, zc);
403 z_max = max(z_max, zc);
406 v_min = max(0, v_min);
407 v_max = min(h_down - 1, v_max);
409 u_min = max(0, u_min);
410 u_max = min(w_down - 1, u_max);
412 if (v_min >= v_max || u_min >= u_max || z_min >= z_max)
return;
416 ceilf(
float(v_max - v_min + 1) /
float(fragment_size));
418 ceilf(
float(u_max - u_min + 1) /
float(fragment_size));
420 int frag_count = frag_v_count * frag_u_count;
422 int frag_count_end = frag_count_start + frag_count;
423 if (frag_count_end >= frag_buffer_size) {
424 printf(
"Fragment count exceeding buffer size, abort!\n");
428 for (
int frag_v = 0; frag_v < frag_v_count; ++frag_v) {
429 for (
int frag_u = 0; frag_u < frag_u_count;
431 float* frag_ptr = frag_buffer_indexer.GetDataPtr<
float>(
432 frag_count_start +
offset);
438 frag_ptr[2] = v_min + frag_v * fragment_size;
439 frag_ptr[3] = u_min + frag_u * fragment_size;
442 frag_ptr[4] = min(frag_ptr[2] + fragment_size - 1,
443 static_cast<float>(v_max));
444 frag_ptr[5] = min(frag_ptr[3] + fragment_size - 1,
445 static_cast<float>(u_max));
449 #if defined(__CUDACC__) 450 int frag_count =
count[0].Item<
int>();
452 int frag_count = (*count_ptr).load();
458 int v = workload_idx / w_down;
459 int u = workload_idx % w_down;
461 range_map_indexer.GetDataPtr<
float>(u, v);
462 range_ptr[0] = depth_max;
463 range_ptr[1] = depth_min;
468 block_keys.
GetDevice(), frag_count * fragment_size * fragment_size,
470 int frag_idx = workload_idx / (fragment_size * fragment_size);
471 int local_idx = workload_idx % (fragment_size * fragment_size);
472 int dv = local_idx / fragment_size;
473 int du = local_idx % fragment_size;
476 frag_buffer_indexer.GetDataPtr<
float>(frag_idx);
477 int v_min =
static_cast<int>(frag_ptr[2]);
478 int u_min =
static_cast<int>(frag_ptr[3]);
479 int v_max =
static_cast<int>(frag_ptr[4]);
480 int u_max =
static_cast<int>(frag_ptr[5]);
484 if (v > v_max || u > u_max)
return;
486 float z_min = frag_ptr[0];
487 float z_max = frag_ptr[1];
488 float* range_ptr = range_map_indexer.GetDataPtr<
float>(u, v);
490 atomicMinf(&(range_ptr[0]), z_min);
491 atomicMaxf(&(range_ptr[1]), z_max);
493 #pragma omp critical(EstimateRangeCPU) 495 range_ptr[0] = min(z_min, range_ptr[0]);
496 range_ptr[1] = max(z_max, range_ptr[1]);
500 #if defined(__CUDACC__) 512 return (xin == x && yin == y && zin == z) ?
block_idx : -1;
522 block_idx = block_idx_in;
526 template <
typename tsdf_t,
typename weight_t,
typename color_t>
527 #if defined(__CUDACC__) 532 (std::shared_ptr<core::HashMap>& hashmap,
545 float weight_threshold,
546 float trunc_voxel_multiplier,
547 int range_map_down_factor) {
552 auto device_hashmap = hashmap->GetDeviceHashBackend();
553 #if defined(__CUDACC__) 557 if (cuda_hashmap ==
nullptr) {
559 "Unsupported backend: CUDA raycasting only supports STDGPU.");
561 auto hashmap_impl = cuda_hashmap->GetImpl();
566 if (cpu_hashmap ==
nullptr) {
568 "Unsupported backend: CPU raycasting only supports TBB.");
570 auto hashmap_impl = *cpu_hashmap->GetImpl();
593 if (!block_value_map.Contains(
"tsdf") ||
594 !block_value_map.Contains(
"weight")) {
596 "TSDF and/or weight not allocated in blocks, please implement " 597 "customized integration.");
599 const tsdf_t* tsdf_base_ptr =
600 block_value_map.at(
"tsdf").GetDataPtr<tsdf_t>();
601 const weight_t* weight_base_ptr =
602 block_value_map.at(
"weight").GetDataPtr<weight_t>();
605 if (renderings_map.Contains(
"depth")) {
606 depth_indexer =
ArrayIndexer(renderings_map.at(
"depth"), 2);
608 if (renderings_map.Contains(
"vertex")) {
609 vertex_indexer =
ArrayIndexer(renderings_map.at(
"vertex"), 2);
611 if (renderings_map.Contains(
"normal")) {
612 normal_indexer =
ArrayIndexer(renderings_map.at(
"normal"), 2);
616 if (renderings_map.Contains(
"index")) {
617 index_indexer =
ArrayIndexer(renderings_map.at(
"index"), 2);
619 if (renderings_map.Contains(
"mask")) {
620 mask_indexer =
ArrayIndexer(renderings_map.at(
"mask"), 2);
622 if (renderings_map.Contains(
"interp_ratio")) {
623 interp_ratio_indexer =
626 if (renderings_map.Contains(
"interp_ratio_dx")) {
627 interp_ratio_dx_indexer =
630 if (renderings_map.Contains(
"interp_ratio_dy")) {
631 interp_ratio_dy_indexer =
634 if (renderings_map.Contains(
"interp_ratio_dz")) {
635 interp_ratio_dz_indexer =
640 bool render_color =
false;
641 if (block_value_map.Contains(
"color") && renderings_map.Contains(
"color")) {
643 color_indexer =
ArrayIndexer(renderings_map.at(
"color"), 2);
645 const color_t* color_base_ptr =
646 render_color ? block_value_map.at(
"color").GetDataPtr<color_t>()
649 bool visit_neighbors = render_color || normal_indexer.
GetDataPtr() ||
665 float block_size = voxel_size * block_resolution;
666 index_t resolution2 = block_resolution * block_resolution;
667 index_t resolution3 = resolution2 * block_resolution;
680 index_t x_vn = (x_v + block_resolution) % block_resolution;
681 index_t y_vn = (y_v + block_resolution) % block_resolution;
682 index_t z_vn = (z_v + block_resolution) % block_resolution;
688 if (dx_b == 0 && dy_b == 0 && dz_b == 0) {
689 return block_buf_idx * resolution3 + z_v * resolution2 +
690 y_v * block_resolution + x_v;
692 Key key(x_b + dx_b, y_b + dy_b, z_b + dz_b);
694 index_t block_buf_idx = cache.
Check(key[0], key[1], key[2]);
695 if (block_buf_idx < 0) {
696 auto iter = hashmap_impl.find(key);
697 if (iter == hashmap_impl.end())
return -1;
698 block_buf_idx = iter->second;
699 cache.
Update(key[0], key[1], key[2], block_buf_idx);
702 return block_buf_idx * resolution3 + z_vn * resolution2 +
703 y_vn * block_resolution + x_vn;
708 float x_o,
float y_o,
float z_o,
709 float x_d,
float y_d,
float z_d,
float t,
711 float x_g = x_o + t * x_d;
712 float y_g = y_o + t * y_d;
713 float z_g = z_o + t * z_d;
720 Key key(x_b, y_b, z_b);
722 if (block_buf_idx < 0) {
723 auto iter = hashmap_impl.find(key);
724 if (iter == hashmap_impl.end())
return -1;
725 block_buf_idx = iter->second;
726 cache.
Update(x_b, y_b, z_b, block_buf_idx);
734 return block_buf_idx * resolution3 + z_v * resolution2 +
735 y_v * block_resolution + x_v;
741 const float* range = range_indexer.
GetDataPtr<
float>(
742 x / range_map_down_factor, y / range_map_down_factor);
744 float* depth_ptr =
nullptr;
745 float* vertex_ptr =
nullptr;
746 float* color_ptr =
nullptr;
747 float* normal_ptr =
nullptr;
749 int64_t* index_ptr =
nullptr;
750 bool* mask_ptr =
nullptr;
751 float* interp_ratio_ptr =
nullptr;
752 float* interp_ratio_dx_ptr =
nullptr;
753 float* interp_ratio_dy_ptr =
nullptr;
754 float* interp_ratio_dz_ptr =
nullptr;
778 for (
int i = 0; i < 8; ++i) {
787 for (
int i = 0; i < 8; ++i) {
792 interp_ratio_ptr = interp_ratio_indexer.
GetDataPtr<
float>(
x,
y);
796 for (
int i = 0; i < 8; ++i) {
797 interp_ratio_ptr[i] = 0;
801 interp_ratio_dx_ptr =
806 for (
int i = 0; i < 8; ++i) {
807 interp_ratio_dx_ptr[i] = 0;
811 interp_ratio_dy_ptr =
816 for (
int i = 0; i < 8; ++i) {
817 interp_ratio_dy_ptr[i] = 0;
821 interp_ratio_dz_ptr =
826 for (
int i = 0; i < 8; ++i) {
827 interp_ratio_dz_ptr[i] = 0;
839 const float t_max = range[1];
840 if (t >= t_max)
return;
843 float x_c = 0, y_c = 0, z_c = 0;
844 float x_g = 0, y_g = 0, z_g = 0;
845 float x_o = 0, y_o = 0, z_o = 0;
850 float tsdf_prev = -1.0f;
852 float sdf_trunc = voxel_size * trunc_voxel_multiplier;
859 c2w_transform_indexer.
Unproject(static_cast<float>(x),
860 static_cast<float>(y), 1.0f, &x_c, &y_c,
862 c2w_transform_indexer.
RigidTransform(x_c, y_c, z_c, &x_g, &y_g, &z_g);
863 float x_d = (x_g - x_o);
864 float y_d = (y_g - y_o);
865 float z_d = (z_g - z_o);
868 bool surface_found =
false;
871 GetLinearIdxAtT(x_o, y_o, z_o, x_d, y_d, z_d, t, cache);
873 if (linear_idx < 0) {
878 tsdf = tsdf_base_ptr[linear_idx];
879 w = weight_base_ptr[linear_idx];
880 if (tsdf_prev > 0 && w >= weight_threshold && tsdf <= 0) {
881 surface_found =
true;
885 float delta = tsdf * sdf_trunc;
886 t += delta < voxel_size ? voxel_size : delta;
892 (t * tsdf_prev - t_prev * tsdf) / (tsdf_prev - tsdf);
893 x_g = x_o + t_intersect * x_d;
894 y_g = y_o + t_intersect * y_d;
895 z_g = z_o + t_intersect * z_d;
899 *depth_ptr = t_intersect * depth_scale;
903 x_g, y_g, z_g, vertex_ptr + 0, vertex_ptr + 1,
906 if (!visit_neighbors)
return;
914 float x_v = (x_g -
float(x_b) * block_size) / voxel_size;
915 float y_v = (y_g -
float(y_b) * block_size) / voxel_size;
916 float z_v = (z_g -
float(z_b) * block_size) / voxel_size;
918 Key key(x_b, y_b, z_b);
921 if (block_buf_idx < 0) {
922 auto iter = hashmap_impl.find(key);
923 if (iter == hashmap_impl.end())
return;
924 block_buf_idx = iter->second;
925 cache.
Update(x_b, y_b, z_b, block_buf_idx);
932 float ratio_x = x_v -
float(x_v_floor);
933 float ratio_y = y_v -
float(y_v_floor);
934 float ratio_z = z_v -
float(z_v_floor);
937 for (
index_t k = 0; k < 8; ++k) {
938 index_t dx_v = (k & 1) > 0 ? 1 : 0;
939 index_t dy_v = (k & 2) > 0 ? 1 : 0;
940 index_t dz_v = (k & 4) > 0 ? 1 : 0;
942 index_t linear_idx_k = GetLinearIdxAtP(
943 x_b, y_b, z_b, x_v_floor + dx_v, y_v_floor + dy_v,
944 z_v_floor + dz_v, block_buf_idx, cache);
946 if (linear_idx_k >= 0 && weight_base_ptr[linear_idx_k] > 0) {
947 float rx = dx_v * (ratio_x) + (1 - dx_v) * (1 - ratio_x);
948 float ry = dy_v * (ratio_y) + (1 - dy_v) * (1 - ratio_y);
949 float rz = dz_v * (ratio_z) + (1 - dz_v) * (1 - ratio_z);
950 float r = rx * ry * rz;
952 if (interp_ratio_ptr) {
953 interp_ratio_ptr[k] = r;
959 index_ptr[k] = linear_idx_k;
962 float tsdf_k = tsdf_base_ptr[linear_idx_k];
963 float interp_ratio_dx = ry * rz * (2 * dx_v - 1);
964 float interp_ratio_dy = rx * rz * (2 * dy_v - 1);
965 float interp_ratio_dz = rx * ry * (2 * dz_v - 1);
967 if (interp_ratio_dx_ptr) {
968 interp_ratio_dx_ptr[k] = interp_ratio_dx;
970 if (interp_ratio_dy_ptr) {
971 interp_ratio_dy_ptr[k] = interp_ratio_dy;
973 if (interp_ratio_dz_ptr) {
974 interp_ratio_dz_ptr[k] = interp_ratio_dz;
978 normal_ptr[0] += interp_ratio_dx * tsdf_k;
979 normal_ptr[1] += interp_ratio_dy * tsdf_k;
980 normal_ptr[2] += interp_ratio_dz * tsdf_k;
984 index_t color_linear_idx = linear_idx_k * 3;
986 r * color_base_ptr[color_linear_idx + 0];
988 r * color_base_ptr[color_linear_idx + 1];
990 r * color_base_ptr[color_linear_idx + 2];
1000 color_ptr[0] /= sum_r;
1001 color_ptr[1] /= sum_r;
1002 color_ptr[2] /= sum_r;
1006 constexpr
float EPSILON = 1e-5f;
1007 float norm = sqrt(normal_ptr[0] * normal_ptr[0] +
1008 normal_ptr[1] * normal_ptr[1] +
1009 normal_ptr[2] * normal_ptr[2]);
1010 norm = std::max(norm, EPSILON);
1011 w2c_transform_indexer.
Rotate(
1012 -normal_ptr[0] / norm, -normal_ptr[1] / norm,
1013 -normal_ptr[2] / norm, normal_ptr + 0,
1014 normal_ptr + 1, normal_ptr + 2);
1020 #if defined(__CUDACC__) 1025 template <
typename tsdf_t,
typename weight_t,
typename color_t>
1026 #if defined(__CUDACC__) 1027 void ExtractPointCloudCUDA
1041 float weight_threshold,
1046 index_t resolution2 = resolution * resolution;
1047 index_t resolution3 = resolution2 * resolution;
1050 ArrayIndexer voxel_indexer({resolution, resolution, resolution});
1060 if (!block_value_map.Contains(
"tsdf") ||
1061 !block_value_map.Contains(
"weight")) {
1063 "TSDF and/or weight not allocated in blocks, please implement " 1064 "customized integration.");
1066 const tsdf_t* tsdf_base_ptr =
1067 block_value_map.at(
"tsdf").GetDataPtr<tsdf_t>();
1068 const weight_t* weight_base_ptr =
1069 block_value_map.at(
"weight").GetDataPtr<weight_t>();
1070 const color_t* color_base_ptr =
nullptr;
1071 if (block_value_map.Contains(
"color")) {
1072 color_base_ptr = block_value_map.at(
"color").GetDataPtr<color_t>();
1076 index_t n = n_blocks * resolution3;
1079 #if defined(__CUDACC__) 1081 block_keys.GetDevice());
1084 std::atomic<index_t> count_atomic(0);
1085 std::atomic<index_t>* count_ptr = &count_atomic;
1088 if (valid_size < 0) {
1090 "No estimated max point cloud size provided, using a 2-pass " 1091 "estimation. Surface extraction could be slow.");
1099 resolution, nb_block_masks_indexer,
1100 nb_block_indices_indexer);
1105 index_t workload_block_idx = workload_idx / resolution3;
1107 index_t voxel_idx = workload_idx % resolution3;
1111 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
1113 index_t linear_idx = block_idx * resolution3 + voxel_idx;
1114 float tsdf_o = tsdf_base_ptr[linear_idx];
1115 float weight_o = weight_base_ptr[linear_idx];
1116 if (weight_o <= weight_threshold)
return;
1119 for (
index_t i = 0; i < 3; ++i) {
1121 GetLinearIdx(xv + (i == 0), yv + (i == 1),
1122 zv + (i == 2), workload_block_idx);
1123 if (linear_idx_i < 0)
continue;
1125 float tsdf_i = tsdf_base_ptr[linear_idx_i];
1126 float weight_i = weight_base_ptr[linear_idx_i];
1127 if (weight_i > weight_threshold && tsdf_i * tsdf_o < 0) {
1133 #if defined(__CUDACC__) 1137 valid_size = (*count_ptr).load();
1142 if (points.GetLength() == 0) {
1156 if (color_base_ptr) {
1166 nb_block_masks_indexer,
1167 nb_block_indices_indexer);
1171 index_t curr_block_idx,
float* n) {
1172 return DeviceGetNormal<tsdf_t>(
1173 tsdf_base_ptr, xo, yo, zo, curr_block_idx, n, resolution,
1174 nb_block_masks_indexer, nb_block_indices_indexer);
1178 index_t workload_block_idx = workload_idx / resolution3;
1180 index_t voxel_idx = workload_idx % resolution3;
1186 index_t xb = block_key_ptr[0];
1187 index_t yb = block_key_ptr[1];
1188 index_t zb = block_key_ptr[2];
1192 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
1194 index_t linear_idx = block_idx * resolution3 + voxel_idx;
1195 float tsdf_o = tsdf_base_ptr[linear_idx];
1196 float weight_o = weight_base_ptr[linear_idx];
1197 if (weight_o <= weight_threshold)
return;
1199 float no[3] = {0}, ne[3] = {0};
1202 GetNormal(xv, yv, zv, workload_block_idx, no);
1209 for (
index_t i = 0; i < 3; ++i) {
1211 GetLinearIdx(xv + (i == 0), yv + (i == 1), zv + (i == 2),
1212 workload_block_idx);
1213 if (linear_idx_i < 0)
continue;
1215 float tsdf_i = tsdf_base_ptr[linear_idx_i];
1216 float weight_i = weight_base_ptr[linear_idx_i];
1217 if (weight_i > weight_threshold && tsdf_i * tsdf_o < 0) {
1218 float ratio = (0 - tsdf_o) / (tsdf_i - tsdf_o);
1221 if (idx >= valid_size) {
1222 printf(
"Point cloud size larger than " 1223 "estimated, please increase the " 1228 float* point_ptr = point_indexer.GetDataPtr<
float>(idx);
1229 point_ptr[0] = voxel_size * (x + ratio *
int(i == 0));
1230 point_ptr[1] = voxel_size * (y + ratio *
int(i == 1));
1231 point_ptr[2] = voxel_size * (z + ratio *
int(i == 2));
1234 float* normal_ptr = normal_indexer.
GetDataPtr<
float>(idx);
1235 GetNormal(xv + (i == 0), yv + (i == 1), zv + (i == 2),
1236 workload_block_idx, ne);
1237 float nx = (1 - ratio) * no[0] + ratio * ne[0];
1238 float ny = (1 - ratio) * no[1] + ratio * ne[1];
1239 float nz = (1 - ratio) * no[2] + ratio * ne[2];
1240 float norm =
static_cast<float>(
1241 sqrt(nx * nx + ny * ny + nz * nz) + 1e-5);
1242 normal_ptr[0] = nx / norm;
1243 normal_ptr[1] = ny / norm;
1244 normal_ptr[2] = nz / norm;
1246 if (color_base_ptr) {
1247 float* color_ptr = color_indexer.
GetDataPtr<
float>(idx);
1248 const color_t* color_o_ptr =
1249 color_base_ptr + 3 * linear_idx;
1250 float r_o = color_o_ptr[0];
1251 float g_o = color_o_ptr[1];
1252 float b_o = color_o_ptr[2];
1254 const color_t* color_i_ptr =
1255 color_base_ptr + 3 * linear_idx_i;
1256 float r_i = color_i_ptr[0];
1257 float g_i = color_i_ptr[1];
1258 float b_i = color_i_ptr[2];
1260 color_ptr[0] = ((1 - ratio) * r_o + ratio * r_i) / 255.0f;
1261 color_ptr[1] = ((1 - ratio) * g_o + ratio * g_i) / 255.0f;
1262 color_ptr[2] = ((1 - ratio) * b_o + ratio * b_i) / 255.0f;
1268 #if defined(__CUDACC__) 1271 index_t total_count = (*count_ptr).load();
1275 valid_size = total_count;
1277 #if defined(BUILD_CUDA_MODULE) && defined(__CUDACC__) 1282 template <
typename tsdf_t,
typename weight_t,
typename color_t>
1283 #if defined(__CUDACC__) 1284 void ExtractTriangleMeshCUDA
1300 float weight_threshold,
1304 index_t resolution = block_resolution;
1305 index_t resolution3 = resolution * resolution * resolution;
1308 ArrayIndexer voxel_indexer({resolution, resolution, resolution});
1317 {n_blocks, resolution, resolution, resolution, 4},
core::Int32,
1319 }
catch (
const std::runtime_error&) {
1321 "[MeshExtractionKernel] Unable to allocate assistance mesh " 1322 "structure for Marching " 1323 "Cubes with {} active voxel blocks. Please consider using a " 1324 "larger voxel size (currently {}) for TSDF " 1325 "integration, or using tsdf_volume.cpu() to perform mesh " 1326 "extraction on CPU.",
1327 n_blocks, voxel_size);
1331 ArrayIndexer mesh_structure_indexer(mesh_structure, 4);
1332 ArrayIndexer nb_block_masks_indexer(nb_block_masks, 2);
1333 ArrayIndexer nb_block_indices_indexer(nb_block_indices, 2);
1337 const index_t* inv_indices_ptr = inv_block_indices.GetDataPtr<
index_t>();
1339 if (!block_value_map.Contains(
"tsdf") ||
1340 !block_value_map.Contains(
"weight")) {
1342 "TSDF and/or weight not allocated in blocks, please implement " 1343 "customized integration.");
1345 const tsdf_t* tsdf_base_ptr =
1346 block_value_map.at(
"tsdf").GetDataPtr<tsdf_t>();
1347 const weight_t* weight_base_ptr =
1348 block_value_map.at(
"weight").GetDataPtr<weight_t>();
1349 const color_t* color_base_ptr =
nullptr;
1350 if (block_value_map.Contains(
"color")) {
1351 color_base_ptr = block_value_map.at(
"color").GetDataPtr<color_t>();
1354 index_t n = n_blocks * resolution3;
1363 static_cast<index_t>(resolution),
1364 nb_block_masks_indexer,
1365 nb_block_indices_indexer);
1369 index_t workload_block_idx = widx / resolution3;
1370 index_t voxel_idx = widx % resolution3;
1374 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
1379 for (
index_t i = 0; i < 8; ++i) {
1381 GetLinearIdx(xv + vtx_shifts[i][0], yv + vtx_shifts[i][1],
1382 zv + vtx_shifts[i][2], workload_block_idx);
1383 if (linear_idx_i < 0)
return;
1385 float tsdf_i = tsdf_base_ptr[linear_idx_i];
1386 float weight_i = weight_base_ptr[linear_idx_i];
1387 if (weight_i <= weight_threshold)
return;
1389 table_idx |= ((tsdf_i < 0) ? (1 << i) : 0);
1393 xv, yv, zv, workload_block_idx);
1394 mesh_struct_ptr[3] = table_idx;
1396 if (table_idx == 0 || table_idx == 255)
return;
1399 index_t edges_with_vertices = edge_table[table_idx];
1400 for (
index_t i = 0; i < 12; ++i) {
1401 if (edges_with_vertices & (1 << i)) {
1402 index_t xv_i = xv + edge_shifts[i][0];
1403 index_t yv_i = yv + edge_shifts[i][1];
1404 index_t zv_i = zv + edge_shifts[i][2];
1405 index_t edge_i = edge_shifts[i][3];
1407 index_t dxb = xv_i / resolution;
1408 index_t dyb = yv_i / resolution;
1409 index_t dzb = zv_i / resolution;
1411 index_t nb_idx = (dxb + 1) + (dyb + 1) * 3 + (dzb + 1) * 9;
1415 workload_block_idx, nb_idx);
1418 xv_i - dxb * resolution,
1419 yv_i - dyb * resolution,
1420 zv_i - dzb * resolution,
1421 inv_indices_ptr[block_idx_i]);
1424 mesh_ptr_i[edge_i] = -1;
1430 #if defined(__CUDACC__) 1435 std::atomic<index_t> count_atomic(0);
1436 std::atomic<index_t>* count_ptr = &count_atomic;
1439 if (vertex_count < 0) {
1442 index_t workload_block_idx = widx / resolution3;
1443 index_t voxel_idx = widx % resolution3;
1447 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
1452 xv, yv, zv, workload_block_idx);
1455 if (mesh_struct_ptr[0] != -1 && mesh_struct_ptr[1] != -1 &&
1456 mesh_struct_ptr[2] != -1) {
1461 for (
index_t e = 0; e < 3; ++e) {
1462 index_t vertex_idx = mesh_struct_ptr[e];
1463 if (vertex_idx != -1)
continue;
1469 #if defined(__CUDACC__) 1472 vertex_count = (*count_ptr).load();
1483 if (color_base_ptr) {
1491 #if defined(__CUDACC__) 1505 nb_block_masks_indexer,
1506 nb_block_indices_indexer);
1510 index_t curr_block_idx,
float* n) {
1511 return DeviceGetNormal<tsdf_t>(
1512 tsdf_base_ptr, xo, yo, zo, curr_block_idx, n, resolution,
1513 nb_block_masks_indexer, nb_block_indices_indexer);
1517 index_t workload_block_idx = widx / resolution3;
1519 index_t voxel_idx = widx % resolution3;
1524 index_t xb = block_key_ptr[0];
1525 index_t yb = block_key_ptr[1];
1526 index_t zb = block_key_ptr[2];
1530 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
1539 xv, yv, zv, workload_block_idx);
1542 if (mesh_struct_ptr[0] != -1 && mesh_struct_ptr[1] != -1 &&
1543 mesh_struct_ptr[2] != -1) {
1548 index_t linear_idx = resolution3 * block_idx + voxel_idx;
1549 float tsdf_o = tsdf_base_ptr[linear_idx];
1551 float no[3] = {0}, ne[3] = {0};
1554 GetNormal(xv, yv, zv, workload_block_idx, no);
1557 for (
index_t e = 0; e < 3; ++e) {
1558 index_t vertex_idx = mesh_struct_ptr[e];
1559 if (vertex_idx != -1)
continue;
1562 GetLinearIdx(xv + (e == 0), yv + (e == 1), zv + (e == 2),
1563 workload_block_idx);
1565 "Internal error: GetVoxelAt returns nullptr.");
1566 float tsdf_e = tsdf_base_ptr[linear_idx_e];
1567 float ratio = (0 - tsdf_o) / (tsdf_e - tsdf_o);
1570 mesh_struct_ptr[e] = idx;
1572 float ratio_x = ratio *
index_t(e == 0);
1573 float ratio_y = ratio *
index_t(e == 1);
1574 float ratio_z = ratio *
index_t(e == 2);
1576 float* vertex_ptr = vertex_indexer.
GetDataPtr<
float>(idx);
1577 vertex_ptr[0] = voxel_size * (x + ratio_x);
1578 vertex_ptr[1] = voxel_size * (y + ratio_y);
1579 vertex_ptr[2] = voxel_size * (z + ratio_z);
1582 float* normal_ptr = normal_indexer.GetDataPtr<
float>(idx);
1583 GetNormal(xv + (e == 0), yv + (e == 1), zv + (e == 2),
1584 workload_block_idx, ne);
1585 float nx = (1 - ratio) * no[0] + ratio * ne[0];
1586 float ny = (1 - ratio) * no[1] + ratio * ne[1];
1587 float nz = (1 - ratio) * no[2] + ratio * ne[2];
1588 float norm =
static_cast<float>(sqrt(nx * nx + ny * ny + nz * nz) +
1590 normal_ptr[0] = nx / norm;
1591 normal_ptr[1] = ny / norm;
1592 normal_ptr[2] = nz / norm;
1594 if (color_base_ptr) {
1595 float* color_ptr = color_indexer.
GetDataPtr<
float>(idx);
1596 float r_o = color_base_ptr[linear_idx * 3 + 0];
1597 float g_o = color_base_ptr[linear_idx * 3 + 1];
1598 float b_o = color_base_ptr[linear_idx * 3 + 2];
1600 float r_e = color_base_ptr[linear_idx_e * 3 + 0];
1601 float g_e = color_base_ptr[linear_idx_e * 3 + 1];
1602 float b_e = color_base_ptr[linear_idx_e * 3 + 2];
1604 color_ptr[0] = ((1 - ratio) * r_o + ratio * r_e) / 255.0f;
1605 color_ptr[1] = ((1 - ratio) * g_o + ratio * g_e) / 255.0f;
1606 color_ptr[2] = ((1 - ratio) * b_o + ratio * b_e) / 255.0f;
1612 index_t triangle_count = vertex_count * 3;
1616 #if defined(__CUDACC__) 1624 index_t workload_block_idx = widx / resolution3;
1625 index_t voxel_idx = widx % resolution3;
1629 voxel_indexer.WorkloadToCoord(voxel_idx, &xv, &yv, &zv);
1633 xv, yv, zv, workload_block_idx);
1635 index_t table_idx = mesh_struct_ptr[3];
1636 if (tri_count[table_idx] == 0)
return;
1638 for (
index_t tri = 0; tri < 16; tri += 3) {
1639 if (tri_table[table_idx][tri] == -1)
return;
1643 for (
index_t vertex = 0; vertex < 3; ++vertex) {
1644 index_t edge = tri_table[table_idx][tri + vertex];
1646 index_t xv_i = xv + edge_shifts[edge][0];
1647 index_t yv_i = yv + edge_shifts[edge][1];
1648 index_t zv_i = zv + edge_shifts[edge][2];
1649 index_t edge_i = edge_shifts[edge][3];
1651 index_t dxb = xv_i / resolution;
1652 index_t dyb = yv_i / resolution;
1653 index_t dzb = zv_i / resolution;
1655 index_t nb_idx = (dxb + 1) + (dyb + 1) * 3 + (dzb + 1) * 9;
1659 workload_block_idx, nb_idx);
1662 xv_i - dxb * resolution,
1663 yv_i - dyb * resolution,
1664 zv_i - dzb * resolution,
1665 inv_indices_ptr[block_idx_i]);
1668 triangle_indexer.GetDataPtr<
index_t>(tri_idx);
1669 triangle_ptr[2 - vertex] = mesh_struct_ptr_i[edge_i];
1674 #if defined(__CUDACC__) 1677 triangle_count = (*count_ptr).load();
1680 triangles = triangles.Slice(0, 0, triangle_count);
Definition: StdGPUHashBackend.h:134
Definition: GeometryIndexer.h:180
void RayCastCPU(std::shared_ptr< core::HashMap > &hashmap, const TensorMap &block_value_map, const core::Tensor &range_map, TensorMap &renderings_map, const core::Tensor &intrinsic, const core::Tensor &extrinsic, index_t h, index_t w, index_t block_resolution, float voxel_size, float depth_scale, float depth_min, float depth_max, float weight_threshold, float trunc_voxel_multiplier, int range_map_down_factor)
Definition: VoxelBlockGridImpl.h:532
TArrayIndexer< index_t > ArrayIndexer
Definition: VoxelBlockGridImpl.h:50
OPEN3D_DEVICE void DeviceGetNormal(const tsdf_t *tsdf_base_ptr, index_t xo, index_t yo, index_t zo, index_t curr_block_idx, float *n, index_t resolution, const ArrayIndexer &nb_block_masks_indexer, const ArrayIndexer &nb_block_indices_indexer)
Definition: VoxelBlockGridImpl.h:131
OPEN3D_HOST_DEVICE void * GetDataPtr() const
Definition: GeometryIndexer.h:335
OPEN3D_HOST_DEVICE int Sign(int x)
Definition: GeometryMacros.h:96
void ExtractTriangleMeshCPU(const core::Tensor &block_indices, const core::Tensor &inv_block_indices, const core::Tensor &nb_block_indices, const core::Tensor &nb_block_masks, const core::Tensor &block_keys, const TensorMap &block_value_map, core::Tensor &vertices, core::Tensor &triangles, core::Tensor &vertex_normals, core::Tensor &vertex_colors, index_t block_resolution, float voxel_size, float weight_threshold, index_t &vertex_count)
Definition: VoxelBlockGridImpl.h:1288
Definition: Dispatch.h:129
void EstimateRangeCPU(const core::Tensor &block_keys, core::Tensor &range_minmax_map, const core::Tensor &intrinsics, const core::Tensor &extrinsics, int h, int w, int down_factor, int64_t block_resolution, float voxel_size, float depth_min, float depth_max)
Definition: VoxelBlockGridImpl.h:320
void OPEN3D_DEVICE Update(index_t xin, index_t yin, index_t zin, index_t block_idx_in)
Definition: VoxelBlockGridImpl.h:515
void ParallelFor(const Device &device, int64_t n, const func_t &func)
Definition: ParallelFor.h:122
const Dtype Float32
Definition: Dtype.cpp:61
Device GetDevice() const
Definition: Tensor.cpp:1365
uint32_t buf_index_t
Definition: HashBackendBuffer.h:63
OPEN3D_DEVICE index_t DeviceGetLinearIdx(index_t xo, index_t yo, index_t zo, index_t curr_block_idx, index_t resolution, const ArrayIndexer &nb_block_masks_indexer, const ArrayIndexer &nb_block_indices_indexer)
Definition: VoxelBlockGridImpl.h:102
#define OPEN3D_DEVICE
Definition: CUDAUtils.h:64
void ExtractPointCloudCPU(const core::Tensor &block_indices, const core::Tensor &nb_block_indices, const core::Tensor &nb_block_masks, const core::Tensor &block_keys, const TensorMap &block_value_map, core::Tensor &points, core::Tensor &normals, core::Tensor &colors, index_t block_resolution, float voxel_size, float weight_threshold, index_t &valid_size)
Definition: VoxelBlockGridImpl.h:1031
#define OPEN3D_ATOMIC_ADD(X, Y)
Definition: GeometryMacros.h:58
void Synchronize()
Definition: CUDAUtils.cpp:78
Definition: Dispatch.h:113
void IntegrateCPU(const core::Tensor &depth, const core::Tensor &color, const core::Tensor &block_indices, const core::Tensor &block_keys, TensorMap &block_value_map, const core::Tensor &depth_intrinsic, const core::Tensor &color_intrinsic, const core::Tensor &extrinsic, index_t resolution, float voxel_size, float sdf_trunc, float depth_scale, float depth_max)
Definition: VoxelBlockGridImpl.h:168
index_t OPEN3D_DEVICE Check(index_t xin, index_t yin, index_t zin)
Definition: VoxelBlockGridImpl.h:511
#define LogDebug(...)
Definition: Logging.h:98
math::float4 color
Definition: LineSetBuffers.cpp:64
core::Tensor InverseTransformation(const core::Tensor &T)
TODO(wei): find a proper place for such functionalities.
Definition: Utility.h:96
const Dtype Int32
Definition: Dtype.cpp:65
void GetVoxelCoordinatesAndFlattenedIndicesCPU(const core::Tensor &buf_indices, const core::Tensor &block_keys, core::Tensor &voxel_coords, core::Tensor &flattened_indices, index_t block_resolution, float voxel_size)
Definition: VoxelBlockGridImpl.h:57
Definition: TBBHashBackend.h:41
int index_t
Definition: VoxelBlockGrid.h:41
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:489
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:380
Definition: VoxelBlockGridImpl.h:505
Definition: PinholeCameraIntrinsic.cpp:35
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle float
Definition: K4aPlugin.cpp:475
T * GetDataPtr()
Definition: Tensor.h:1108
#define OPEN3D_ASSERT(...)
Definition: Macro.h:67
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:392
index_t x
Definition: VoxelBlockGridImpl.h:506
index_t z
Definition: VoxelBlockGridImpl.h:508
index_t y
Definition: VoxelBlockGridImpl.h:507
int64_t GetLength() const
Definition: Tensor.h:1089
OPEN3D_HOST_DEVICE bool InBoundary(float x, float y) const
Definition: GeometryIndexer.h:314
index_t block_idx
Definition: VoxelBlockGridImpl.h:509
static const Dtype Float64
Definition: Dtype.h:43
#define LogError(...)
Definition: Logging.h:67
Definition: TensorMap.h:49