forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_cloud.cc
322 lines (284 loc) · 8.7 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
#include "drake/perception/point_cloud.h"
#include <utility>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include "drake/common/drake_assert.h"
using Eigen::Map;
using Eigen::NoChange;
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 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();
}
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 {
if (fields_.contains(pc_flags::kXYZs)) {
const int xyz_size = xyzs_.cols();
DRAKE_DEMAND(xyz_size == size());
}
if (fields_.contains(pc_flags::kNormals)) {
const int normals_size = normals_.cols();
DRAKE_DEMAND(normals_size == size());
}
if (fields_.contains(pc_flags::kRGBs)) {
const int rgbs_size = rgbs_.cols();
DRAKE_DEMAND(rgbs_size == size());
}
if (fields_.has_descriptor()) {
const int descriptor_size = descriptors_.cols();
DRAKE_DEMAND(descriptor_size == size());
}
}
const 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;
}
}
// Resolves the fields from a pair of point clouds and desired fields.
// Implements the resolution rules in `SetFrom`.
// @pre Valid point clouds `a` and `b`.
// @returns Fields that both point clouds have.
pc_flags::Fields ResolvePairFields(
const PointCloud& a,
const PointCloud& b,
pc_flags::Fields fields) {
if (fields == pc_flags::kInherit) {
// If we do not permit a subset, expect the exact same fields.
a.RequireExactFields(b.fields());
return a.fields();
} else {
a.RequireFields(fields);
b.RequireFields(fields);
return fields;
}
}
} // namespace
PointCloud::PointCloud(
int new_size, pc_flags::Fields fields, bool skip_initialize)
: size_(new_size),
fields_(fields) {
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(size_, fields_));
if (!skip_initialize) {
SetDefault(0, 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.fields(), true) {
// This has zero size. Directly swap storages.
storage_.swap(other.storage_);
std::swap(size_, other.size_);
DRAKE_DEMAND(storage_->size() == size());
}
PointCloud& PointCloud::operator=(const PointCloud& other) {
SetFrom(other);
return *this;
}
PointCloud& PointCloud::operator=(PointCloud&& other) {
// We may only take rvalue references if the fields match exactly.
RequireExactFields(other.fields());
// Swap storages.
size_ = other.size_;
storage_.swap(other.storage_);
DRAKE_DEMAND(storage_->size() == size());
// 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() {}
void PointCloud::resize(int new_size, bool skip_initialization) {
DRAKE_DEMAND(new_size >= 0);
int old_size = size();
size_ = new_size;
storage_->resize(new_size);
DRAKE_DEMAND(storage_->size() == new_size);
if (new_size > old_size && !skip_initialization) {
int size_diff = new_size - old_size;
SetDefault(old_size, size_diff);
}
}
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) {
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));
}
pc_flags::Fields fields_resolved =
ResolvePairFields(*this, other, fields_in);
if (fields_resolved.contains(pc_flags::kXYZs)) {
mutable_xyzs() = other.xyzs();
}
if (fields_resolved.contains(pc_flags::kNormals)) {
mutable_normals() = other.normals();
}
if (fields_resolved.contains(pc_flags::kRGBs)) {
mutable_rgbs() = other.rgbs();
}
if (fields_resolved.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 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 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 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 fields_.has_descriptor();
}
bool PointCloud::has_descriptors(
const pc_flags::DescriptorType& descriptor_type) const {
return fields_.contains(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 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, fields()));
}
}
bool PointCloud::HasExactFields(
pc_flags::Fields fields_in) const {
return 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, fields()));
}
}
} // namespace perception
} // namespace drake