forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_cloud.cc
704 lines (625 loc) · 22.6 KB
/
point_cloud.cc
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
#include "drake/perception/point_cloud.h"
#include <algorithm>
#include <atomic>
#include <functional>
#include <unordered_map>
#include <utility>
#include <vector>
#include <common_robotics_utilities/dynamic_spatial_hashed_voxel_grid.hpp>
#include <common_robotics_utilities/voxel_grid.hpp>
#include <fmt/format.h>
#include <nanoflann.hpp>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_throw.h"
#include "drake/common/unused.h"
using Eigen::Map;
using Eigen::NoChange;
using common_robotics_utilities::voxel_grid::DSHVGSetType;
using common_robotics_utilities::voxel_grid::DynamicSpatialHashedVoxelGrid;
using common_robotics_utilities::voxel_grid::GridIndex;
using common_robotics_utilities::voxel_grid::GridSizes;
namespace drake {
namespace perception {
namespace {
// Convenience aliases.
typedef PointCloud::T T;
typedef PointCloud::C C;
typedef PointCloud::D D;
} // namespace
/*
* Provides encapsulated storage for a `PointCloud`.
*
* This storage is not responsible for initializing default values.
*/
class PointCloud::Storage {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Storage);
Storage(int new_size, pc_flags::Fields fields)
: fields_(fields) {
// Ensure that we incorporate the size of the descriptors.
descriptors_.resize(fields_.descriptor_type().size(), 0);
// Resize as normal.
resize(new_size);
}
// Returns a reference to the fields provided by this storage. Note that the
// outer class PointCloud::fields() returns a copy, but for Storage::fields()
// we need to return a reference for performance and because we need to return
// a nested reference for the descriptor_type().
const pc_flags::Fields& fields() const { return fields_; }
// Returns size of the storage.
int size() const { return size_; }
// Resize to parent cloud's size.
void resize(int new_size) {
size_ = new_size;
if (fields_.contains(pc_flags::kXYZs))
xyzs_.conservativeResize(NoChange, new_size);
if (fields_.contains(pc_flags::kNormals))
normals_.conservativeResize(NoChange, new_size);
if (fields_.contains(pc_flags::kRGBs))
rgbs_.conservativeResize(NoChange, new_size);
if (fields_.has_descriptor())
descriptors_.conservativeResize(NoChange, new_size);
CheckInvariants();
}
// Update fields, allocating (but not initializing) new fields when needed.
void UpdateFields(pc_flags::Fields f) {
xyzs_.conservativeResize(NoChange, f.contains(pc_flags::kXYZs) ? size_ : 0);
normals_.conservativeResize(NoChange,
f.contains(pc_flags::kNormals) ? size_ : 0);
rgbs_.conservativeResize(NoChange, f.contains(pc_flags::kRGBs) ? size_ : 0);
// Note: The row size can change depends on whether 'f' contains a
// descriptor field and the type of the descriptor.
descriptors_.conservativeResize(f.descriptor_type().size(),
f.has_descriptor() ? size_ : 0);
fields_ = f;
CheckInvariants();
}
Eigen::Ref<Matrix3X<T>> xyzs() { return xyzs_; }
Eigen::Ref<Matrix3X<T>> normals() { return normals_; }
Eigen::Ref<Matrix3X<C>> rgbs() { return rgbs_; }
Eigen::Ref<MatrixX<T>> descriptors() { return descriptors_; }
private:
void CheckInvariants() const {
const int xyz_size = xyzs_.cols();
if (fields_.contains(pc_flags::kXYZs)) {
DRAKE_DEMAND(xyz_size == size());
} else {
DRAKE_DEMAND(xyz_size == 0);
}
const int normals_size = normals_.cols();
if (fields_.contains(pc_flags::kNormals)) {
DRAKE_DEMAND(normals_size == size());
} else {
DRAKE_DEMAND(normals_size == 0);
}
const int rgbs_size = rgbs_.cols();
if (fields_.contains(pc_flags::kRGBs)) {
DRAKE_DEMAND(rgbs_size == size());
} else {
DRAKE_DEMAND(rgbs_size == 0);
}
const int descriptor_cols = descriptors_.cols();
const int descriptor_rows = descriptors_.rows();
if (fields_.has_descriptor()) {
DRAKE_DEMAND(descriptor_cols == size());
DRAKE_DEMAND(descriptor_rows == fields_.descriptor_type().size());
} else {
DRAKE_DEMAND(descriptor_cols == 0);
DRAKE_DEMAND(descriptor_rows == 0);
}
}
pc_flags::Fields fields_;
int size_{};
Matrix3X<T> xyzs_;
Matrix3X<T> normals_;
Matrix3X<C> rgbs_;
MatrixX<T> descriptors_;
};
namespace {
pc_flags::Fields ResolveFields(
const PointCloud& other, pc_flags::Fields fields) {
if (fields == pc_flags::kInherit) {
return other.fields();
} else {
return fields;
}
}
// Finds the added fields from `new_fields` w.r.t. `old_fields`.
pc_flags::Fields FindAddedFields(pc_flags::Fields old_fields,
pc_flags::Fields new_fields) {
pc_flags::Fields added_fields(pc_flags::kNone);
for (const auto field :
{pc_flags::kXYZs, pc_flags::kNormals, pc_flags::kRGBs}) {
if (!old_fields.contains(field) && new_fields.contains(field))
added_fields |= field;
}
if (new_fields.has_descriptor() &&
old_fields.descriptor_type() != new_fields.descriptor_type()) {
added_fields |= new_fields.descriptor_type();
}
return added_fields;
}
} // namespace
PointCloud::PointCloud(
int new_size, pc_flags::Fields fields, bool skip_initialize) {
if (fields == pc_flags::kNone)
throw std::runtime_error("Cannot construct a PointCloud without fields");
if (fields.contains(pc_flags::kInherit))
throw std::runtime_error("Cannot construct a PointCloud with kInherit");
storage_.reset(new Storage(new_size, fields));
if (!skip_initialize) {
SetDefault(0, new_size);
}
}
PointCloud::PointCloud(const PointCloud& other,
pc_flags::Fields copy_fields)
: PointCloud(other.size(), ResolveFields(other, copy_fields)) {
SetFrom(other);
}
PointCloud::PointCloud(PointCloud&& other)
: PointCloud(0, other.storage_->fields(), true) {
// This has zero size. Directly swap storages.
storage_.swap(other.storage_);
}
PointCloud& PointCloud::operator=(const PointCloud& other) {
SetFrom(other);
return *this;
}
PointCloud& PointCloud::operator=(PointCloud&& other) {
// Swap storages.
storage_.swap(other.storage_);
// Empty out the other cloud, but let it remain being a valid point cloud
// (with non-null storage).
other.resize(0, false);
return *this;
}
// Define destructor here to use complete definition of `Storage`.
PointCloud::~PointCloud() {}
pc_flags::Fields PointCloud::fields() const {
return storage_->fields();
}
int PointCloud::size() const {
return storage_->size();
}
void PointCloud::resize(int new_size, bool skip_initialization) {
DRAKE_DEMAND(new_size >= 0);
const int old_size = size();
if (old_size == new_size)
return;
storage_->resize(new_size);
DRAKE_DEMAND(storage_->size() == new_size);
if (new_size > old_size && !skip_initialization) {
const int size_diff = new_size - old_size;
SetDefault(old_size, size_diff);
}
}
void PointCloud::SetFields(pc_flags::Fields new_fields, bool skip_initialize) {
const pc_flags::Fields old_fields = storage_->fields();
if (old_fields == new_fields)
return;
storage_->UpdateFields(new_fields);
if (!skip_initialize) {
// Default-initialize containers for newly added fields.
const pc_flags::Fields added_fields =
FindAddedFields(old_fields, new_fields);
if (added_fields.contains(pc_flags::kXYZs))
mutable_xyzs().setConstant(kDefaultValue);
if (added_fields.contains(pc_flags::kNormals))
mutable_normals().setConstant(kDefaultValue);
if (added_fields.contains(pc_flags::kRGBs))
mutable_rgbs().setConstant(kDefaultColor);
if (added_fields.has_descriptor())
mutable_descriptors().setConstant(kDefaultValue);
}
}
void PointCloud::SetDefault(int start, int num) {
auto set = [=](auto ref, auto value) {
ref.middleCols(start, num).setConstant(value);
};
if (has_xyzs()) {
set(mutable_xyzs(), kDefaultValue);
}
if (has_normals()) {
set(mutable_normals(), kDefaultValue);
}
if (has_rgbs()) {
set(mutable_rgbs(), kDefaultColor);
}
if (has_descriptors()) {
set(mutable_descriptors(), kDefaultValue);
}
}
void PointCloud::SetFrom(const PointCloud& other,
pc_flags::Fields fields_in,
bool allow_resize) {
// Update the size of this point cloud if necessary.
int old_size = size();
int new_size = other.size();
if (allow_resize) {
resize(new_size);
} else if (new_size != old_size) {
throw std::runtime_error(
fmt::format("SetFrom: {} != {}", new_size, old_size));
}
// Update or check the fields of the point cloud(s) if necessary.
pc_flags::Fields fields_to_copy = fields_in;
if (fields_in == pc_flags::kInherit) {
fields_to_copy = other.storage_->fields();
SetFields(other.storage_->fields(), true);
} else {
this->RequireFields(fields_to_copy);
other.RequireFields(fields_to_copy);
}
// Populate data from `other` to this point cloud.
if (fields_to_copy.contains(pc_flags::kXYZs)) {
mutable_xyzs() = other.xyzs();
}
if (fields_to_copy.contains(pc_flags::kNormals)) {
mutable_normals() = other.normals();
}
if (fields_to_copy.contains(pc_flags::kRGBs)) {
mutable_rgbs() = other.rgbs();
}
if (fields_to_copy.has_descriptor()) {
mutable_descriptors() = other.descriptors();
}
}
void PointCloud::Expand(
int add_size,
bool skip_initialization) {
DRAKE_DEMAND(add_size >= 0);
const int new_size = size() + add_size;
resize(new_size, skip_initialization);
}
bool PointCloud::has_xyzs() const {
return storage_->fields().contains(pc_flags::kXYZs);
}
Eigen::Ref<const Matrix3X<T>> PointCloud::xyzs() const {
DRAKE_DEMAND(has_xyzs());
return storage_->xyzs();
}
Eigen::Ref<Matrix3X<T>> PointCloud::mutable_xyzs() {
DRAKE_DEMAND(has_xyzs());
return storage_->xyzs();
}
bool PointCloud::has_normals() const {
return storage_->fields().contains(pc_flags::kNormals);
}
Eigen::Ref<const Matrix3X<T>> PointCloud::normals() const {
DRAKE_DEMAND(has_normals());
return storage_->normals();
}
Eigen::Ref<Matrix3X<T>> PointCloud::mutable_normals() {
DRAKE_DEMAND(has_normals());
return storage_->normals();
}
bool PointCloud::has_rgbs() const {
return storage_->fields().contains(pc_flags::kRGBs);
}
Eigen::Ref<const Matrix3X<C>> PointCloud::rgbs() const {
DRAKE_DEMAND(has_rgbs());
return storage_->rgbs();
}
Eigen::Ref<Matrix3X<C>> PointCloud::mutable_rgbs() {
DRAKE_DEMAND(has_rgbs());
return storage_->rgbs();
}
bool PointCloud::has_descriptors() const {
return storage_->fields().has_descriptor();
}
bool PointCloud::has_descriptors(
const pc_flags::DescriptorType& descriptor_type) const {
return storage_->fields().contains(descriptor_type);
}
const pc_flags::DescriptorType& PointCloud::descriptor_type() const {
return storage_->fields().descriptor_type();
}
Eigen::Ref<const MatrixX<D>> PointCloud::descriptors() const {
DRAKE_DEMAND(has_descriptors());
return storage_->descriptors();
}
Eigen::Ref<MatrixX<D>> PointCloud::mutable_descriptors() {
DRAKE_DEMAND(has_descriptors());
return storage_->descriptors();
}
bool PointCloud::HasFields(
pc_flags::Fields fields_in) const {
DRAKE_DEMAND(!fields_in.contains(pc_flags::kInherit));
return storage_->fields().contains(fields_in);
}
void PointCloud::RequireFields(
pc_flags::Fields fields_in) const {
if (!HasFields(fields_in)) {
throw std::runtime_error(
fmt::format("PointCloud does not have expected fields.\n"
"Expected {}, got {}",
fields_in, storage_->fields()));
}
}
bool PointCloud::HasExactFields(
pc_flags::Fields fields_in) const {
return storage_->fields() == fields_in;
}
void PointCloud::RequireExactFields(
pc_flags::Fields fields_in) const {
if (!HasExactFields(fields_in)) {
throw std::runtime_error(
fmt::format("PointCloud does not have the exact expected fields."
"\nExpected {}, got {}",
fields_in, storage_->fields()));
}
}
PointCloud PointCloud::Crop(const Eigen::Ref<const Vector3<T>>& lower_xyz,
const Eigen::Ref<const Vector3<T>>& upper_xyz) {
DRAKE_DEMAND((lower_xyz.array() <= upper_xyz.array()).all());
if (!has_xyzs()) {
throw std::runtime_error("PointCloud must have xyzs in order to Crop");
}
PointCloud crop(size(), storage_->fields(), true);
int index = 0;
for (int i = 0; i < size(); ++i) {
if (((xyzs().col(i).array() >= lower_xyz.array()) &&
(xyzs().col(i).array() <= upper_xyz.array()))
.all()) {
crop.mutable_xyzs().col(index) = xyzs().col(i);
if (has_normals()) {
crop.mutable_normals().col(index) = normals().col(i);
}
if (has_rgbs()) {
crop.mutable_rgbs().col(index) = rgbs().col(i);
}
if (has_descriptors()) {
crop.mutable_descriptors().col(index) = descriptors().col(i);
}
++index;
}
}
crop.resize(index);
return crop;
}
void PointCloud::FlipNormalsTowardPoint(
const Eigen::Ref<const Vector3<T>>& p_CP) {
DRAKE_THROW_UNLESS(has_xyzs());
DRAKE_THROW_UNLESS(has_normals());
for (int i = 0; i < size(); ++i) {
// Note: p_CP - xyz could be arbitrarily close to zero; but this behavior
// is still reasonable.
if ((p_CP - xyz(i)).dot(normal(i)) < 0.0) {
mutable_normal(i) *= T(-1.0);
}
}
}
PointCloud Concatenate(const std::vector<PointCloud>& clouds) {
const int num_clouds = clouds.size();
DRAKE_DEMAND(num_clouds >= 1);
int count = clouds[0].size();
for (int i = 1; i < num_clouds; ++i) {
DRAKE_THROW_UNLESS(clouds[i].fields() == clouds[0].fields());
count += clouds[i].size();
}
PointCloud new_cloud(count, clouds[0].fields(), true);
int index = 0;
for (int i = 0; i < num_clouds; ++i) {
const int s = clouds[i].size();
if (new_cloud.has_xyzs()) {
new_cloud.mutable_xyzs().middleCols(index, s) = clouds[i].xyzs();
}
if (new_cloud.has_normals()) {
new_cloud.mutable_normals().middleCols(index, s) = clouds[i].normals();
}
if (new_cloud.has_rgbs()) {
new_cloud.mutable_rgbs().middleCols(index, s) = clouds[i].rgbs();
}
if (new_cloud.has_descriptors()) {
new_cloud.mutable_descriptors().middleCols(index, s) =
clouds[i].descriptors();
}
index += s;
}
return new_cloud;
}
PointCloud PointCloud::VoxelizedDownSample(
const double voxel_size, const Parallelism parallelize) const {
DRAKE_THROW_UNLESS(has_xyzs());
DRAKE_THROW_UNLESS(voxel_size > 0);
// Create a dynamic-spatial-hashed voxel grid (DSHVG) to bin points. While a
// DSHVG usually has each dynamic "chunk" contain multiple voxels, by setting
// the chunk size to (voxel_size, 1, 1, 1) each chunk contains a single voxel
// and the whole DSHVG behaves as a sparse voxel grid.
const GridSizes chunk_sizes(voxel_size, INT64_C(1), INT64_C(1), INT64_C(1));
const std::vector<int> default_chunk_value;
// By providing an initial estimated number of chunks, we reduce reallocation
// and rehashing in the DSHVG.
const size_t num_expected_chunks = static_cast<size_t>(size() / 16);
DynamicSpatialHashedVoxelGrid<std::vector<int>> dynamic_voxel_grid(
chunk_sizes, default_chunk_value, num_expected_chunks);
const auto& my_xyzs = storage_->xyzs();
// Add points into the voxel grid.
for (int i = 0; i < size(); ++i) {
if (my_xyzs.col(i).array().isFinite().all()) {
auto chunk_query = dynamic_voxel_grid.GetLocationMutable3d(
my_xyzs.col(i).cast<double>());
if (chunk_query) {
// If the containing chunk has already been allocated, add the current
// point index directly.
chunk_query.Value().emplace_back(i);
} else {
// If the containing chunk hasn't already been allocated, create a new
// chunk containing the current point index.
dynamic_voxel_grid.SetLocation3d(
my_xyzs.col(i).cast<double>(), DSHVGSetType::SET_CHUNK, {i});
}
}
}
// Initialize downsampled cloud.
PointCloud down_sampled(
dynamic_voxel_grid.GetImmutableInternalChunks().size(),
storage_->fields());
const bool this_has_normals = has_normals();
const bool this_has_rgbs = has_rgbs();
const bool this_has_descriptors = has_descriptors();
Storage& storage = *storage_;
Storage& down_sampled_storage = *down_sampled.storage_;
// Helper lambda to process a single voxel cell.
const auto process_voxel =
[&storage, &down_sampled_storage, this_has_normals, this_has_rgbs,
this_has_descriptors](
int index_in_down_sampled, const std::vector<int>& indices_in_this) {
// Use doubles instead of floats for accumulators to avoid round-off errors.
Eigen::Vector3d xyz{Eigen::Vector3d::Zero()};
Eigen::Vector3d normal{Eigen::Vector3d::Zero()};
Eigen::Vector3d rgb{Eigen::Vector3d::Zero()};
Eigen::VectorXd descriptor{Eigen::VectorXd::Zero(
this_has_descriptors ? storage.descriptors().rows() : 0)};
int num_normals{0};
int num_descriptors{0};
for (int index_in_this : indices_in_this) {
xyz += storage.xyzs().col(index_in_this).cast<double>();
if (this_has_normals &&
storage.normals().col(index_in_this).array().isFinite().all()) {
normal += storage.normals().col(index_in_this).cast<double>();
++num_normals;
}
if (this_has_rgbs) {
rgb += storage.rgbs().col(index_in_this).cast<double>();
}
if (this_has_descriptors &&
storage.descriptors().col(index_in_this).array().isFinite().all()) {
descriptor += storage.descriptors().col(index_in_this).cast<double>();
++num_descriptors;
}
}
down_sampled_storage.xyzs().col(index_in_down_sampled) =
(xyz / indices_in_this.size()).cast<T>();
if (this_has_normals) {
down_sampled_storage.normals().col(index_in_down_sampled) =
(normal / num_normals).normalized().cast<T>();
}
if (this_has_rgbs) {
down_sampled_storage.rgbs().col(index_in_down_sampled) =
(rgb / indices_in_this.size()).cast<C>();
}
if (this_has_descriptors) {
down_sampled_storage.descriptors().col(index_in_down_sampled) =
(descriptor / num_descriptors).cast<D>();
}
};
// Since the parallel form imposes additional overhead, only use it when
// we are supposed to parallelize.
[[maybe_unused]] const int num_threads = parallelize.num_threads();
#if defined(_OPENMP)
const bool operate_in_parallel = num_threads > 1;
#else
constexpr bool operate_in_parallel = false;
#endif
// Since we specify chunks contain a single voxel, a chunk's lone voxel can be
// retrieved with index (0, 0, 0).
const GridIndex kSingleVoxel(0, 0, 0);
// Populate the elements of the down_sampled cloud.
if (operate_in_parallel) {
// Flatten voxel cells to allow parallel processing.
std::vector<const std::vector<int>*> voxel_indices;
voxel_indices.reserve(
dynamic_voxel_grid.GetImmutableInternalChunks().size());
for (const auto& [chunk_region, chunk] :
dynamic_voxel_grid.GetImmutableInternalChunks()) {
unused(chunk_region);
const std::vector<int>& indices_in_this =
chunk.GetIndexImmutable(kSingleVoxel).Value();
voxel_indices.emplace_back(&indices_in_this);
}
// Process voxel cells in parallel.
#if defined(_OPENMP)
#pragma omp parallel for num_threads(num_threads)
#endif
for (int index_in_down_sampled = 0;
index_in_down_sampled < static_cast<int>(voxel_indices.size());
++index_in_down_sampled) {
process_voxel(
index_in_down_sampled, *voxel_indices[index_in_down_sampled]);
}
} else {
int index_in_down_sampled = 0;
for (const auto& [chunk_region, chunk] :
dynamic_voxel_grid.GetImmutableInternalChunks()) {
unused(chunk_region);
const std::vector<int>& indices_in_this =
chunk.GetIndexImmutable(kSingleVoxel).Value();
process_voxel(index_in_down_sampled, indices_in_this);
++index_in_down_sampled;
}
}
return down_sampled;
}
bool PointCloud::EstimateNormals(
const double radius, const int num_closest,
[[maybe_unused]] const Parallelism parallelize) {
DRAKE_DEMAND(radius > 0);
DRAKE_DEMAND(num_closest >= 3);
DRAKE_THROW_UNLESS(has_xyzs());
constexpr float kNaN = std::numeric_limits<float>::quiet_NaN();
const double squared_radius = radius * radius;
if (!has_normals()) {
storage_->UpdateFields(storage_->fields() | pc_flags::kNormals);
}
const Eigen::MatrixX3f data = xyzs().transpose();
nanoflann::KDTreeEigenMatrixAdaptor<Eigen::MatrixX3f, 3,
nanoflann::metric_L2_Simple>
kd_tree(3, data);
// Iterate through all points and compute their normals.
std::atomic<bool> all_points_have_at_least_three_neighbors(true);
#if defined(_OPENMP)
#pragma omp parallel for num_threads(parallelize.num_threads())
#endif
for (int i = 0; i < size(); ++i) {
VectorX<Eigen::Index> indices(num_closest);
Eigen::VectorXf distances(num_closest);
// nanoflann allows two types of queries:
// 1. search for the num_closest points, and then keep those within radius
// 2. search for points within radius, and then keep the num_closest
// for dense clouds where the number of points within radius would be high,
// approach (1) is considerably faster.
const int num_neighbors = kd_tree.index_->knnSearch(
xyz(i).data(), num_closest, indices.data(), distances.data());
if (num_neighbors < 3) {
all_points_have_at_least_three_neighbors = false;
}
if (num_neighbors < 2) {
mutable_normal(i) = Eigen::Vector3f::Constant(kNaN);
continue;
}
// Compute the covariance matrix.
int count = 0;
Eigen::Vector3d mean = Eigen::Vector3d::Zero();
for (int j = 0; j < num_neighbors; ++j) {
if (distances[j] <= squared_radius) {
++count;
mean += xyz(indices[j]).cast<double>();
}
}
if (count < 3) {
all_points_have_at_least_three_neighbors = false;
}
if (count < 2) {
mutable_normal(i) = Eigen::Vector3f::Constant(kNaN);
continue;
}
mean /= count;
Eigen::Matrix3d covariance = Eigen::Matrix3d::Zero();
for (int j = 0; j < num_neighbors; ++j) {
if (distances[j] <= squared_radius) {
const Eigen::VectorXd x_minus_mean =
xyz(indices[j]).cast<double>() - mean;
covariance += x_minus_mean * x_minus_mean.transpose();
}
}
// TODO(russt): Open3d implements a "FastEigen3x3" for an optimized version
// of this. We probably should, too.
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> solver;
solver.computeDirect(covariance, Eigen::ComputeEigenvectors);
mutable_normal(i) = solver.eigenvectors().col(0).cast<float>();
}
return all_points_have_at_least_three_neighbors.load();
}
} // namespace perception
} // namespace drake