-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathPointCloudImpl.h
1294 lines (1118 loc) · 47.2 KB
/
PointCloudImpl.h
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#include <atomic>
#include <vector>
#include "open3d/core/CUDAUtils.h"
#include "open3d/core/Dispatch.h"
#include "open3d/core/Dtype.h"
#include "open3d/core/MemoryManager.h"
#include "open3d/core/ParallelFor.h"
#include "open3d/core/SizeVector.h"
#include "open3d/core/Tensor.h"
#include "open3d/core/linalg/kernel/Matrix.h"
#include "open3d/core/linalg/kernel/SVD3x3.h"
#include "open3d/core/nns/NearestNeighborSearch.h"
#include "open3d/t/geometry/Utility.h"
#include "open3d/t/geometry/kernel/GeometryIndexer.h"
#include "open3d/t/geometry/kernel/GeometryMacros.h"
#include "open3d/t/geometry/kernel/PointCloud.h"
#include "open3d/utility/Logging.h"
namespace open3d {
namespace t {
namespace geometry {
namespace kernel {
namespace pointcloud {
#ifndef __CUDACC__
using std::abs;
using std::max;
using std::min;
using std::sqrt;
#endif
#if defined(__CUDACC__)
void UnprojectCUDA
#else
void UnprojectCPU
#endif
(const core::Tensor& depth,
utility::optional<std::reference_wrapper<const core::Tensor>>
image_colors,
core::Tensor& points,
utility::optional<std::reference_wrapper<core::Tensor>> colors,
const core::Tensor& intrinsics,
const core::Tensor& extrinsics,
float depth_scale,
float depth_max,
int64_t stride) {
const bool have_colors = image_colors.has_value();
NDArrayIndexer depth_indexer(depth, 2);
NDArrayIndexer image_colors_indexer;
core::Tensor pose = t::geometry::InverseTransformation(extrinsics);
TransformIndexer ti(intrinsics, pose, 1.0f);
// Output
int64_t rows_strided = depth_indexer.GetShape(0) / stride;
int64_t cols_strided = depth_indexer.GetShape(1) / stride;
points = core::Tensor({rows_strided * cols_strided, 3}, core::Float32,
depth.GetDevice());
NDArrayIndexer point_indexer(points, 1);
NDArrayIndexer colors_indexer;
if (have_colors) {
const auto& imcol = image_colors.value().get();
image_colors_indexer = NDArrayIndexer{imcol, 2};
colors.value().get() = core::Tensor({rows_strided * cols_strided, 3},
core::Float32, imcol.GetDevice());
colors_indexer = NDArrayIndexer(colors.value().get(), 1);
}
// Counter
#if defined(__CUDACC__)
core::Tensor count(std::vector<int>{0}, {}, core::Int32, depth.GetDevice());
int* count_ptr = count.GetDataPtr<int>();
#else
std::atomic<int> count_atomic(0);
std::atomic<int>* count_ptr = &count_atomic;
#endif
int64_t n = rows_strided * cols_strided;
DISPATCH_DTYPE_TO_TEMPLATE(depth.GetDtype(), [&]() {
core::ParallelFor(
depth.GetDevice(), n, [=] OPEN3D_DEVICE(int64_t workload_idx) {
int64_t y = (workload_idx / cols_strided) * stride;
int64_t x = (workload_idx % cols_strided) * stride;
float d = *depth_indexer.GetDataPtr<scalar_t>(x, y) /
depth_scale;
if (d > 0 && d < depth_max) {
int idx = OPEN3D_ATOMIC_ADD(count_ptr, 1);
float x_c = 0, y_c = 0, z_c = 0;
ti.Unproject(static_cast<float>(x),
static_cast<float>(y), d, &x_c, &y_c,
&z_c);
float* vertex = point_indexer.GetDataPtr<float>(idx);
ti.RigidTransform(x_c, y_c, z_c, vertex + 0, vertex + 1,
vertex + 2);
if (have_colors) {
float* pcd_pixel =
colors_indexer.GetDataPtr<float>(idx);
float* image_pixel =
image_colors_indexer.GetDataPtr<float>(x,
y);
*pcd_pixel = *image_pixel;
*(pcd_pixel + 1) = *(image_pixel + 1);
*(pcd_pixel + 2) = *(image_pixel + 2);
}
}
});
});
#if defined(__CUDACC__)
int total_pts_count = count.Item<int>();
#else
int total_pts_count = (*count_ptr).load();
#endif
#ifdef __CUDACC__
core::cuda::Synchronize();
#endif
points = points.Slice(0, 0, total_pts_count);
if (have_colors) {
colors.value().get() =
colors.value().get().Slice(0, 0, total_pts_count);
}
}
#if defined(__CUDACC__)
void GetPointMaskWithinAABBCUDA
#else
void GetPointMaskWithinAABBCPU
#endif
(const core::Tensor& points,
const core::Tensor& min_bound,
const core::Tensor& max_bound,
core::Tensor& mask) {
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
const scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
const int64_t n = points.GetLength();
const scalar_t* min_bound_ptr = min_bound.GetDataPtr<scalar_t>();
const scalar_t* max_bound_ptr = max_bound.GetDataPtr<scalar_t>();
bool* mask_ptr = mask.GetDataPtr<bool>();
core::ParallelFor(
points.GetDevice(), n, [=] OPEN3D_DEVICE(int64_t workload_idx) {
const scalar_t x = points_ptr[3 * workload_idx + 0];
const scalar_t y = points_ptr[3 * workload_idx + 1];
const scalar_t z = points_ptr[3 * workload_idx + 2];
if (x >= min_bound_ptr[0] && x <= max_bound_ptr[0] &&
y >= min_bound_ptr[1] && y <= max_bound_ptr[1] &&
z >= min_bound_ptr[2] && z <= max_bound_ptr[2]) {
mask_ptr[workload_idx] = true;
} else {
mask_ptr[workload_idx] = false;
}
});
});
}
#if defined(__CUDACC__)
void GetPointMaskWithinOBBCUDA
#else
void GetPointMaskWithinOBBCPU
#endif
(const core::Tensor& points,
const core::Tensor& center,
const core::Tensor& rotation,
const core::Tensor& extent,
core::Tensor& mask) {
const core::Tensor half_extent = extent.Div(2);
// Since we will extract 3 rotation axis from matrix and use it inside
// kernel, the transpose is needed.
const core::Tensor rotation_t = rotation.Transpose(0, 1).Contiguous();
const core::Tensor pd = points - center;
const int64_t n = points.GetLength();
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
const scalar_t* pd_ptr = pd.GetDataPtr<scalar_t>();
// const scalar_t* center_ptr = center.GetDataPtr<scalar_t>();
const scalar_t* rotation_ptr = rotation_t.GetDataPtr<scalar_t>();
const scalar_t* half_extent_ptr = half_extent.GetDataPtr<scalar_t>();
bool* mask_ptr = mask.GetDataPtr<bool>();
core::ParallelFor(points.GetDevice(), n,
[=] OPEN3D_DEVICE(int64_t workload_idx) {
int64_t idx = 3 * workload_idx;
if (abs(core::linalg::kernel::dot_3x1(
pd_ptr + idx, rotation_ptr)) <=
half_extent_ptr[0] &&
abs(core::linalg::kernel::dot_3x1(
pd_ptr + idx, rotation_ptr + 3)) <=
half_extent_ptr[1] &&
abs(core::linalg::kernel::dot_3x1(
pd_ptr + idx, rotation_ptr + 6)) <=
half_extent_ptr[2]) {
mask_ptr[workload_idx] = true;
} else {
mask_ptr[workload_idx] = false;
}
});
});
}
#if defined(__CUDACC__)
void NormalizeNormalsCUDA
#else
void NormalizeNormalsCPU
#endif
(core::Tensor& normals) {
const core::Dtype dtype = normals.GetDtype();
const int64_t n = normals.GetLength();
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
scalar_t* ptr = normals.GetDataPtr<scalar_t>();
core::ParallelFor(normals.GetDevice(), n,
[=] OPEN3D_DEVICE(int64_t workload_idx) {
int64_t idx = 3 * workload_idx;
scalar_t x = ptr[idx];
scalar_t y = ptr[idx + 1];
scalar_t z = ptr[idx + 2];
scalar_t norm = sqrt(x * x + y * y + z * z);
if (norm > 0) {
x /= norm;
y /= norm;
z /= norm;
}
ptr[idx] = x;
ptr[idx + 1] = y;
ptr[idx + 2] = z;
});
});
}
#if defined(__CUDACC__)
void OrientNormalsToAlignWithDirectionCUDA
#else
void OrientNormalsToAlignWithDirectionCPU
#endif
(core::Tensor& normals, const core::Tensor& direction) {
const core::Dtype dtype = normals.GetDtype();
const int64_t n = normals.GetLength();
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
scalar_t* ptr = normals.GetDataPtr<scalar_t>();
const scalar_t* direction_ptr = direction.GetDataPtr<scalar_t>();
core::ParallelFor(normals.GetDevice(), n,
[=] OPEN3D_DEVICE(int64_t workload_idx) {
int64_t idx = 3 * workload_idx;
scalar_t* normal = ptr + idx;
const scalar_t norm = sqrt(normal[0] * normal[0] +
normal[1] * normal[1] +
normal[2] * normal[2]);
if (norm == 0.0) {
normal[0] = direction_ptr[0];
normal[1] = direction_ptr[1];
normal[2] = direction_ptr[2];
} else if (core::linalg::kernel::dot_3x1(
normal, direction_ptr) < 0) {
normal[0] *= -1;
normal[1] *= -1;
normal[2] *= -1;
}
});
});
}
#if defined(__CUDACC__)
void OrientNormalsTowardsCameraLocationCUDA
#else
void OrientNormalsTowardsCameraLocationCPU
#endif
(const core::Tensor& points,
core::Tensor& normals,
const core::Tensor& camera) {
const core::Dtype dtype = points.GetDtype();
const int64_t n = normals.GetLength();
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
const scalar_t* camera_ptr = camera.GetDataPtr<scalar_t>();
const scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
core::ParallelFor(
normals.GetDevice(), n,
[=] OPEN3D_DEVICE(int64_t workload_idx) {
int64_t idx = 3 * workload_idx;
scalar_t* normal = normals_ptr + idx;
const scalar_t* point = points_ptr + idx;
const scalar_t reference[3] = {camera_ptr[0] - point[0],
camera_ptr[1] - point[1],
camera_ptr[2] - point[2]};
const scalar_t norm =
sqrt(normal[0] * normal[0] + normal[1] * normal[1] +
normal[2] * normal[2]);
if (norm == 0.0) {
normal[0] = reference[0];
normal[1] = reference[1];
normal[2] = reference[2];
const scalar_t norm_new = sqrt(normal[0] * normal[0] +
normal[1] * normal[1] +
normal[2] * normal[2]);
if (norm_new == 0.0) {
normal[0] = 0.0;
normal[1] = 0.0;
normal[2] = 1.0;
} else {
normal[0] /= norm_new;
normal[1] /= norm_new;
normal[2] /= norm_new;
}
} else if (core::linalg::kernel::dot_3x1(normal,
reference) < 0) {
normal[0] *= -1;
normal[1] *= -1;
normal[2] *= -1;
}
});
});
}
template <typename scalar_t>
OPEN3D_HOST_DEVICE void GetCoordinateSystemOnPlane(const scalar_t* query,
scalar_t* u,
scalar_t* v) {
// Unless the x and y coords are both close to zero, we can simply take
// ( -y, x, 0 ) and normalize it. If both x and y are close to zero,
// then the vector is close to the z-axis, so it's far from colinear to
// the x-axis for instance. So we take the crossed product with (1,0,0)
// and normalize it.
if (!(abs(query[0] - query[2]) < 1e-6) ||
!(abs(query[1] - query[2]) < 1e-6)) {
const scalar_t norm2_inv =
1.0 / sqrt(query[0] * query[0] + query[1] * query[1]);
v[0] = -1 * query[1] * norm2_inv;
v[1] = query[0] * norm2_inv;
v[2] = 0;
} else {
const scalar_t norm2_inv =
1.0 / sqrt(query[1] * query[1] + query[2] * query[2]);
v[0] = 0;
v[1] = -1 * query[2] * norm2_inv;
v[2] = query[1] * norm2_inv;
}
core::linalg::kernel::cross_3x1(query, v, u);
}
template <typename scalar_t>
inline OPEN3D_HOST_DEVICE void Swap(scalar_t* x, scalar_t* y) {
scalar_t tmp = *x;
*x = *y;
*y = tmp;
}
template <typename scalar_t>
inline OPEN3D_HOST_DEVICE void Heapify(scalar_t* arr, int n, int root) {
int largest = root;
int l = 2 * root + 1;
int r = 2 * root + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != root) {
Swap<scalar_t>(&arr[root], &arr[largest]);
Heapify<scalar_t>(arr, n, largest);
}
}
template <typename scalar_t>
OPEN3D_HOST_DEVICE void HeapSort(scalar_t* arr, int n) {
for (int i = n / 2 - 1; i >= 0; i--) Heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
Swap<scalar_t>(&arr[0], &arr[i]);
Heapify<scalar_t>(arr, i, 0);
}
}
template <typename scalar_t>
OPEN3D_HOST_DEVICE bool IsBoundaryPoints(const scalar_t* angles,
int counts,
double angle_threshold) {
scalar_t diff;
scalar_t max_diff = 0;
// Compute the maximal angle difference between two consecutive angles.
for (int i = 0; i < counts - 1; i++) {
diff = angles[i + 1] - angles[i];
max_diff = max(max_diff, diff);
}
// Get the angle difference between the last and the first.
diff = 2 * M_PI - angles[counts - 1] + angles[0];
max_diff = max(max_diff, diff);
return max_diff > angle_threshold * M_PI / 180.0 ? true : false;
}
#if defined(__CUDACC__)
void ComputeBoundaryPointsCUDA
#else
void ComputeBoundaryPointsCPU
#endif
(const core::Tensor& points,
const core::Tensor& normals,
const core::Tensor& indices,
const core::Tensor& counts,
core::Tensor& mask,
double angle_threshold) {
const int nn_size = indices.GetShape()[1];
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
const scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
const scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
const int64_t n = points.GetLength();
const int32_t* indices_ptr = indices.GetDataPtr<int32_t>();
const int32_t* counts_ptr = counts.GetDataPtr<int32_t>();
bool* mask_ptr = mask.GetDataPtr<bool>();
core::Tensor angles = core::Tensor::Full(
indices.GetShape(), -10, points.GetDtype(), points.GetDevice());
scalar_t* angles_ptr = angles.GetDataPtr<scalar_t>();
core::ParallelFor(
points.GetDevice(), n, [=] OPEN3D_DEVICE(int64_t workload_idx) {
scalar_t u[3], v[3];
GetCoordinateSystemOnPlane(normals_ptr + 3 * workload_idx,
u, v);
// Ignore the point itself.
int indices_size = counts_ptr[workload_idx] - 1;
if (indices_size > 0) {
const scalar_t* query = points_ptr + 3 * workload_idx;
for (int i = 1; i < indices_size + 1; i++) {
const int idx = workload_idx * nn_size + i;
const scalar_t* point_ref =
points_ptr + 3 * indices_ptr[idx];
const scalar_t delta[3] = {point_ref[0] - query[0],
point_ref[1] - query[1],
point_ref[2] - query[2]};
const scalar_t angle = atan2(
core::linalg::kernel::dot_3x1(v, delta),
core::linalg::kernel::dot_3x1(u, delta));
angles_ptr[idx] = angle;
}
// Sort the angles in ascending order.
HeapSort<scalar_t>(
angles_ptr + workload_idx * nn_size + 1,
indices_size);
mask_ptr[workload_idx] = IsBoundaryPoints<scalar_t>(
angles_ptr + workload_idx * nn_size + 1,
indices_size, angle_threshold);
}
});
});
}
// This is a `two-pass` estimate method for covariance which is numerically more
// robust than the `textbook` method generally used for covariance computation.
template <typename scalar_t>
OPEN3D_HOST_DEVICE void EstimatePointWiseRobustNormalizedCovarianceKernel(
const scalar_t* points_ptr,
const int32_t* indices_ptr,
const int32_t& indices_count,
scalar_t* covariance_ptr) {
if (indices_count < 3) {
covariance_ptr[0] = 1.0;
covariance_ptr[1] = 0.0;
covariance_ptr[2] = 0.0;
covariance_ptr[3] = 0.0;
covariance_ptr[4] = 1.0;
covariance_ptr[5] = 0.0;
covariance_ptr[6] = 0.0;
covariance_ptr[7] = 0.0;
covariance_ptr[8] = 1.0;
return;
}
double centroid[3] = {0};
for (int32_t i = 0; i < indices_count; ++i) {
int32_t idx = 3 * indices_ptr[i];
centroid[0] += points_ptr[idx];
centroid[1] += points_ptr[idx + 1];
centroid[2] += points_ptr[idx + 2];
}
centroid[0] /= indices_count;
centroid[1] /= indices_count;
centroid[2] /= indices_count;
// cumulants must always be Float64 to ensure precision.
double cumulants[6] = {0};
for (int32_t i = 0; i < indices_count; ++i) {
int32_t idx = 3 * indices_ptr[i];
const double x = static_cast<double>(points_ptr[idx]) - centroid[0];
const double y = static_cast<double>(points_ptr[idx + 1]) - centroid[1];
const double z = static_cast<double>(points_ptr[idx + 2]) - centroid[2];
cumulants[0] += x * x;
cumulants[1] += y * y;
cumulants[2] += z * z;
cumulants[3] += x * y;
cumulants[4] += x * z;
cumulants[5] += y * z;
}
// Using Bessel's correction (dividing by (n - 1) instead of n).
// Refer:
// https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
const double normalization_factor = static_cast<double>(indices_count - 1);
for (int i = 0; i < 6; ++i) {
cumulants[i] /= normalization_factor;
}
// Covariances(0, 0)
covariance_ptr[0] = static_cast<scalar_t>(cumulants[0]);
// Covariances(1, 1)
covariance_ptr[4] = static_cast<scalar_t>(cumulants[1]);
// Covariances(2, 2)
covariance_ptr[8] = static_cast<scalar_t>(cumulants[2]);
// Covariances(0, 1) = Covariances(1, 0)
covariance_ptr[1] = static_cast<scalar_t>(cumulants[3]);
covariance_ptr[3] = covariance_ptr[1];
// Covariances(0, 2) = Covariances(2, 0)
covariance_ptr[2] = static_cast<scalar_t>(cumulants[4]);
covariance_ptr[6] = covariance_ptr[2];
// Covariances(1, 2) = Covariances(2, 1)
covariance_ptr[5] = static_cast<scalar_t>(cumulants[5]);
covariance_ptr[7] = covariance_ptr[5];
}
#if defined(__CUDACC__)
void EstimateCovariancesUsingHybridSearchCUDA
#else
void EstimateCovariancesUsingHybridSearchCPU
#endif
(const core::Tensor& points,
core::Tensor& covariances,
const double& radius,
const int64_t& max_nn) {
core::Dtype dtype = points.GetDtype();
int64_t n = points.GetLength();
core::nns::NearestNeighborSearch tree(points, core::Int32);
bool check = tree.HybridIndex(radius);
if (!check) {
utility::LogError("Building FixedRadiusIndex failed.");
}
core::Tensor indices, distance, counts;
std::tie(indices, distance, counts) =
tree.HybridSearch(points, radius, max_nn);
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
const scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
int32_t* neighbour_indices_ptr = indices.GetDataPtr<int32_t>();
int32_t* neighbour_counts_ptr = counts.GetDataPtr<int32_t>();
scalar_t* covariances_ptr = covariances.GetDataPtr<scalar_t>();
core::ParallelFor(
points.GetDevice(), n, [=] OPEN3D_DEVICE(int64_t workload_idx) {
// NNS [Hybrid Search].
const int32_t neighbour_offset = max_nn * workload_idx;
// Count of valid correspondences per point.
const int32_t neighbour_count =
neighbour_counts_ptr[workload_idx];
// Covariance is of shape {3, 3}, so it has an
// offset factor of 9 x workload_idx.
const int32_t covariances_offset = 9 * workload_idx;
EstimatePointWiseRobustNormalizedCovarianceKernel(
points_ptr,
neighbour_indices_ptr + neighbour_offset,
neighbour_count,
covariances_ptr + covariances_offset);
});
});
core::cuda::Synchronize(points.GetDevice());
}
#if defined(__CUDACC__)
void EstimateCovariancesUsingRadiusSearchCUDA
#else
void EstimateCovariancesUsingRadiusSearchCPU
#endif
(const core::Tensor& points,
core::Tensor& covariances,
const double& radius) {
core::Dtype dtype = points.GetDtype();
int64_t n = points.GetLength();
core::nns::NearestNeighborSearch tree(points, core::Int32);
bool check = tree.FixedRadiusIndex(radius);
if (!check) {
utility::LogError("Building Radius-Index failed.");
}
core::Tensor indices, distance, counts;
std::tie(indices, distance, counts) =
tree.FixedRadiusSearch(points, radius);
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
const scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
const int32_t* neighbour_indices_ptr = indices.GetDataPtr<int32_t>();
const int32_t* neighbour_counts_ptr = counts.GetDataPtr<int32_t>();
scalar_t* covariances_ptr = covariances.GetDataPtr<scalar_t>();
core::ParallelFor(
points.GetDevice(), n, [=] OPEN3D_DEVICE(int64_t workload_idx) {
const int32_t neighbour_offset =
neighbour_counts_ptr[workload_idx];
const int32_t neighbour_count =
(neighbour_counts_ptr[workload_idx + 1] -
neighbour_counts_ptr[workload_idx]);
// Covariance is of shape {3, 3}, so it has an offset
// factor of 9 x workload_idx.
const int32_t covariances_offset = 9 * workload_idx;
EstimatePointWiseRobustNormalizedCovarianceKernel(
points_ptr,
neighbour_indices_ptr + neighbour_offset,
neighbour_count,
covariances_ptr + covariances_offset);
});
});
core::cuda::Synchronize(points.GetDevice());
}
#if defined(__CUDACC__)
void EstimateCovariancesUsingKNNSearchCUDA
#else
void EstimateCovariancesUsingKNNSearchCPU
#endif
(const core::Tensor& points,
core::Tensor& covariances,
const int64_t& max_nn) {
core::Dtype dtype = points.GetDtype();
int64_t n = points.GetLength();
core::nns::NearestNeighborSearch tree(points, core::Int32);
bool check = tree.KnnIndex();
if (!check) {
utility::LogError("Building KNN-Index failed.");
}
core::Tensor indices, distance;
std::tie(indices, distance) = tree.KnnSearch(points, max_nn);
indices = indices.Contiguous();
int32_t nn_count = static_cast<int32_t>(indices.GetShape()[1]);
if (nn_count < 3) {
utility::LogError(
"Not enough neighbors to compute Covariances / Normals. "
"Try "
"increasing the max_nn parameter.");
}
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
auto points_ptr = points.GetDataPtr<scalar_t>();
auto neighbour_indices_ptr = indices.GetDataPtr<int32_t>();
auto covariances_ptr = covariances.GetDataPtr<scalar_t>();
core::ParallelFor(
points.GetDevice(), n, [=] OPEN3D_DEVICE(int64_t workload_idx) {
// NNS [KNN Search].
const int32_t neighbour_offset = nn_count * workload_idx;
// Covariance is of shape {3, 3}, so it has an offset
// factor of 9 x workload_idx.
const int32_t covariances_offset = 9 * workload_idx;
EstimatePointWiseRobustNormalizedCovarianceKernel(
points_ptr,
neighbour_indices_ptr + neighbour_offset, nn_count,
covariances_ptr + covariances_offset);
});
});
core::cuda::Synchronize(points.GetDevice());
}
template <typename scalar_t>
OPEN3D_HOST_DEVICE void ComputeEigenvector0(const scalar_t* A,
const scalar_t eval0,
scalar_t* eigen_vector0) {
scalar_t row0[3] = {A[0] - eval0, A[1], A[2]};
scalar_t row1[3] = {A[1], A[4] - eval0, A[5]};
scalar_t row2[3] = {A[2], A[5], A[8] - eval0};
scalar_t r0xr1[3], r0xr2[3], r1xr2[3];
core::linalg::kernel::cross_3x1(row0, row1, r0xr1);
core::linalg::kernel::cross_3x1(row0, row2, r0xr2);
core::linalg::kernel::cross_3x1(row1, row2, r1xr2);
scalar_t d0 = core::linalg::kernel::dot_3x1(r0xr1, r0xr1);
scalar_t d1 = core::linalg::kernel::dot_3x1(r0xr2, r0xr2);
scalar_t d2 = core::linalg::kernel::dot_3x1(r1xr2, r1xr2);
scalar_t dmax = d0;
int imax = 0;
if (d1 > dmax) {
dmax = d1;
imax = 1;
}
if (d2 > dmax) {
imax = 2;
}
if (imax == 0) {
scalar_t sqrt_d = sqrt(d0);
eigen_vector0[0] = r0xr1[0] / sqrt_d;
eigen_vector0[1] = r0xr1[1] / sqrt_d;
eigen_vector0[2] = r0xr1[2] / sqrt_d;
return;
} else if (imax == 1) {
scalar_t sqrt_d = sqrt(d1);
eigen_vector0[0] = r0xr2[0] / sqrt_d;
eigen_vector0[1] = r0xr2[1] / sqrt_d;
eigen_vector0[2] = r0xr2[2] / sqrt_d;
return;
} else {
scalar_t sqrt_d = sqrt(d2);
eigen_vector0[0] = r1xr2[0] / sqrt_d;
eigen_vector0[1] = r1xr2[1] / sqrt_d;
eigen_vector0[2] = r1xr2[2] / sqrt_d;
return;
}
}
template <typename scalar_t>
OPEN3D_HOST_DEVICE void ComputeEigenvector1(const scalar_t* A,
const scalar_t* evec0,
const scalar_t eval1,
scalar_t* eigen_vector1) {
scalar_t U[3];
if (abs(evec0[0]) > abs(evec0[1])) {
scalar_t inv_length =
1.0 / sqrt(evec0[0] * evec0[0] + evec0[2] * evec0[2]);
U[0] = -evec0[2] * inv_length;
U[1] = 0.0;
U[2] = evec0[0] * inv_length;
} else {
scalar_t inv_length =
1.0 / sqrt(evec0[1] * evec0[1] + evec0[2] * evec0[2]);
U[0] = 0.0;
U[1] = evec0[2] * inv_length;
U[2] = -evec0[1] * inv_length;
}
scalar_t V[3], AU[3], AV[3];
core::linalg::kernel::cross_3x1(evec0, U, V);
core::linalg::kernel::matmul3x3_3x1(A, U, AU);
core::linalg::kernel::matmul3x3_3x1(A, V, AV);
scalar_t m00 = core::linalg::kernel::dot_3x1(U, AU) - eval1;
scalar_t m01 = core::linalg::kernel::dot_3x1(U, AV);
scalar_t m11 = core::linalg::kernel::dot_3x1(V, AV) - eval1;
scalar_t absM00 = abs(m00);
scalar_t absM01 = abs(m01);
scalar_t absM11 = abs(m11);
scalar_t max_abs_comp;
if (absM00 >= absM11) {
max_abs_comp = max(absM00, absM01);
if (max_abs_comp > 0) {
if (absM00 >= absM01) {
m01 /= m00;
m00 = 1 / sqrt(1 + m01 * m01);
m01 *= m00;
} else {
m00 /= m01;
m01 = 1 / sqrt(1 + m00 * m00);
m00 *= m01;
}
eigen_vector1[0] = m01 * U[0] - m00 * V[0];
eigen_vector1[1] = m01 * U[1] - m00 * V[1];
eigen_vector1[2] = m01 * U[2] - m00 * V[2];
return;
} else {
eigen_vector1[0] = U[0];
eigen_vector1[1] = U[1];
eigen_vector1[2] = U[2];
return;
}
} else {
max_abs_comp = max(absM11, absM01);
if (max_abs_comp > 0) {
if (absM11 >= absM01) {
m01 /= m11;
m11 = 1 / sqrt(1 + m01 * m01);
m01 *= m11;
} else {
m11 /= m01;
m01 = 1 / sqrt(1 + m11 * m11);
m11 *= m01;
}
eigen_vector1[0] = m11 * U[0] - m01 * V[0];
eigen_vector1[1] = m11 * U[1] - m01 * V[1];
eigen_vector1[2] = m11 * U[2] - m01 * V[2];
return;
} else {
eigen_vector1[0] = U[0];
eigen_vector1[1] = U[1];
eigen_vector1[2] = U[2];
return;
}
}
}
template <typename scalar_t>
OPEN3D_HOST_DEVICE void EstimatePointWiseNormalsWithFastEigen3x3(
const scalar_t* covariance_ptr, scalar_t* normals_ptr) {
// Based on:
// https://www.geometrictools.com/Documentation/RobustEigenSymmetric3x3.pdf
// which handles edge cases like points on a plane.
scalar_t max_coeff = covariance_ptr[0];
for (int i = 1; i < 9; ++i) {
if (max_coeff < covariance_ptr[i]) {
max_coeff = covariance_ptr[i];
}
}
if (max_coeff == 0) {
normals_ptr[0] = 0.0;
normals_ptr[1] = 0.0;
normals_ptr[2] = 0.0;
return;
}
scalar_t A[9] = {0};
for (int i = 0; i < 9; ++i) {
A[i] = covariance_ptr[i] / max_coeff;
}
scalar_t norm = A[1] * A[1] + A[2] * A[2] + A[5] * A[5];
if (norm > 0) {
scalar_t eval[3];
scalar_t evec0[3];
scalar_t evec1[3];
scalar_t evec2[3];
scalar_t q = (A[0] + A[4] + A[8]) / 3.0;
scalar_t b00 = A[0] - q;
scalar_t b11 = A[4] - q;
scalar_t b22 = A[8] - q;
scalar_t p =
sqrt((b00 * b00 + b11 * b11 + b22 * b22 + norm * 2.0) / 6.0);
scalar_t c00 = b11 * b22 - A[5] * A[5];
scalar_t c01 = A[1] * b22 - A[5] * A[2];
scalar_t c02 = A[1] * A[5] - b11 * A[2];
scalar_t det = (b00 * c00 - A[1] * c01 + A[2] * c02) / (p * p * p);
scalar_t half_det = det * 0.5;
half_det = min(max(half_det, static_cast<scalar_t>(-1.0)),
static_cast<scalar_t>(1.0));
scalar_t angle = acos(half_det) / 3.0;
const scalar_t two_thrids_pi = 2.09439510239319549;
scalar_t beta2 = cos(angle) * 2.0;
scalar_t beta0 = cos(angle + two_thrids_pi) * 2.0;
scalar_t beta1 = -(beta0 + beta2);
eval[0] = q + p * beta0;
eval[1] = q + p * beta1;
eval[2] = q + p * beta2;
if (half_det >= 0) {
ComputeEigenvector0<scalar_t>(A, eval[2], evec2);
if (eval[2] < eval[0] && eval[2] < eval[1]) {
normals_ptr[0] = evec2[0];
normals_ptr[1] = evec2[1];
normals_ptr[2] = evec2[2];
return;
}
ComputeEigenvector1<scalar_t>(A, evec2, eval[1], evec1);
if (eval[1] < eval[0] && eval[1] < eval[2]) {
normals_ptr[0] = evec1[0];
normals_ptr[1] = evec1[1];
normals_ptr[2] = evec1[2];
return;
}
normals_ptr[0] = evec1[1] * evec2[2] - evec1[2] * evec2[1];
normals_ptr[1] = evec1[2] * evec2[0] - evec1[0] * evec2[2];
normals_ptr[2] = evec1[0] * evec2[1] - evec1[1] * evec2[0];
return;
} else {
ComputeEigenvector0<scalar_t>(A, eval[0], evec0);
if (eval[0] < eval[1] && eval[0] < eval[2]) {
normals_ptr[0] = evec0[0];
normals_ptr[1] = evec0[1];
normals_ptr[2] = evec0[2];
return;
}
ComputeEigenvector1<scalar_t>(A, evec0, eval[1], evec1);
if (eval[1] < eval[0] && eval[1] < eval[2]) {
normals_ptr[0] = evec1[0];
normals_ptr[1] = evec1[1];
normals_ptr[2] = evec1[2];
return;
}
normals_ptr[0] = evec0[1] * evec1[2] - evec0[2] * evec1[1];
normals_ptr[1] = evec0[2] * evec1[0] - evec0[0] * evec1[2];
normals_ptr[2] = evec0[0] * evec1[1] - evec0[1] * evec1[0];
return;
}
} else {
if (covariance_ptr[0] < covariance_ptr[4] &&
covariance_ptr[0] < covariance_ptr[8]) {
normals_ptr[0] = 1.0;
normals_ptr[1] = 0.0;
normals_ptr[2] = 0.0;
return;
} else if (covariance_ptr[4] < covariance_ptr[0] &&
covariance_ptr[4] < covariance_ptr[8]) {
normals_ptr[0] = 0.0;
normals_ptr[1] = 1.0;
normals_ptr[2] = 0.0;
return;
} else {
normals_ptr[0] = 0.0;
normals_ptr[1] = 0.0;
normals_ptr[2] = 1.0;
return;
}
}
}
#if defined(__CUDACC__)
void EstimateNormalsFromCovariancesCUDA
#else
void EstimateNormalsFromCovariancesCPU
#endif
(const core::Tensor& covariances,
core::Tensor& normals,
const bool has_normals) {
core::Dtype dtype = covariances.GetDtype();
int64_t n = covariances.GetLength();
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
const scalar_t* covariances_ptr = covariances.GetDataPtr<scalar_t>();
scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
core::ParallelFor(
covariances.GetDevice(), n,
[=] OPEN3D_DEVICE(int64_t workload_idx) {
int32_t covariances_offset = 9 * workload_idx;
int32_t normals_offset = 3 * workload_idx;
scalar_t normals_output[3] = {0};
EstimatePointWiseNormalsWithFastEigen3x3<scalar_t>(
covariances_ptr + covariances_offset,
normals_output);
if ((normals_output[0] * normals_output[0] +
normals_output[1] * normals_output[1] +