forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry_instance.h
212 lines (173 loc) · 8 KB
/
geometry_instance.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
#pragma once
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "drake/common/copyable_unique_ptr.h"
#include "drake/common/drake_copyable.h"
#include "drake/common/eigen_types.h"
#include "drake/geometry/geometry_ids.h"
#include "drake/geometry/geometry_roles.h"
#include "drake/geometry/shape_specification.h"
#include "drake/math/rigid_transform.h"
namespace drake {
namespace geometry {
/** A geometry instance combines a geometry definition (i.e., a shape of some
sort), a pose (relative to a parent "frame" P), material information, and an
opaque collection of metadata. The parent frame can be a registered frame or
another registered geometry.
Every %GeometryInstance must be named. The naming convention mirrors that of
valid names in SDF files. Specifically, any user-specified name will have
all leading and trailing space and tab characters trimmed off. The trimmed name
will have to satisfy the following requirements:
- cannot be empty, and
- the name should be unique in the scope of its frame and role. For example,
two GeometryInstances can both be called "ball" as long as they are
affixed to different frames or if one is a collision geometry and the
other is an illustration geometry. This is enforced when a role is assigned
to the geometry.
<!-- TODO(SeanCurtis-TRI): Update this when the perception role is also
added. -->
If valid, the trimmed name will be assigned to the instance.
Names *can* have internal whitespace (e.g., "my geometry name").
@anchor canonicalized_geometry_names
<h3>Canonicalized names</h3>
The silent transformation of a user-defined name to canonical name mirrors that
of specifying geometry names in an SDF file. Consider the following SDF
snippet:
@code{xml}
...
<visual name=" visual">
<geometry>
<sphere>
<radius>1.0</radius>
</sphere>
</geometry>
</visual>
...
@endcode
The name has two leading whitespace characters. The parsing process will
consider this name as equivalent to "visual" and tests for uniqueness and
non-emptiness will be applied to that trimmed result. The following code has
an analogous effect:
```
scene_graph->RegisterGeometry(
source_id, frame_id,
make_unique<GeometryInstance>(pose, make_unique<Sphere>(1.0), " visual"));
```
The specified name includes leading whitespace. That name will be trimmed and
the *result* will be stored in the %GeometryInstance (to be later validated by
SceneGraph as part of geometry registration). Querying the instance of its name
will return this *canonicalized* name.
<!-- Note to developers: The sdf requirements for naming are captured in a
unit test in `scene_graph_parser_detail_test.cc. See test
VisualGeometryNameRequirements and keep those tests and this list in sync. -->
*/
class GeometryInstance {
public:
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(GeometryInstance);
/** Constructs a geometry instance specification.
@param X_PG The pose of this geometry (`G`) in its parent's frame (`P`).
@param shape The underlying shape for this geometry instance.
@param name The name of the geometry (must satisfy the name requirements).
@throws std::exception if the canonicalized version of `name` is empty.
*/
GeometryInstance(const math::RigidTransform<double>& X_PG, const Shape& shape,
const std::string& name);
/** (Advanced) Overload that transfers ownership of `shape` (for performance).
@pre shape is non-null.
@exclude_from_pydrake_mkdoc{Cannot transfer ownership.} */
GeometryInstance(const math::RigidTransform<double>& X_PG,
std::unique_ptr<Shape> shape, const std::string& name);
/** Returns the globally unique id for this geometry specification. Every
instantiation of %GeometryInstance will contain a unique id value. The id
value is preserved across copies. After successfully registering this
%GeometryInstance, this id will serve as the identifier for the registered
representation as well. */
GeometryId id() const { return id_; }
/** Returns the instance geometry's pose in its parent frame. */
const math::RigidTransformd& pose() const { return X_PG_; }
/** Sets the pose of this instance in its parent's frame. */
void set_pose(const math::RigidTransformd& X_PG) { X_PG_ = X_PG; }
/** Returns the underlying shape specification for this geometry instance.
@pre release_shape() has not been called nor has this been moved-from. */
const Shape& shape() const {
DRAKE_THROW_UNLESS(shape_ != nullptr);
return *shape_;
}
/** (Advanced) Transfers ownership of this geometry instance's underlying
shape specification to the caller.
@pre release_shape() has not been called nor has this been moved-from. */
std::unique_ptr<Shape> release_shape() { return std::move(shape_); }
/** Returns the *canonicalized* name for the instance.
@sa @ref canonicalized_geometry_names "Canonicalized names" */
const std::string& name() const { return name_; }
/** Sets the *canonicalized* name for the instance.
@sa @ref canonicalized_geometry_names "Canonicalized names" */
void set_name(const std::string& name);
/** Sets the proximity properties for the given instance. */
void set_proximity_properties(ProximityProperties properties) {
proximity_properties_ = std::move(properties);
}
/** Sets the illustration properties for the given instance. */
void set_illustration_properties(IllustrationProperties properties) {
illustration_properties_ = std::move(properties);
}
/** Sets the perception properties for the given instance. */
void set_perception_properties(PerceptionProperties properties) {
perception_properties_ = std::move(properties);
}
/** Returns a pointer to the geometry's mutable proximity properties (if they
are defined). Nullptr otherwise. */
ProximityProperties* mutable_proximity_properties() {
if (proximity_properties_) return &*proximity_properties_;
return nullptr;
}
/** Returns a pointer to the geometry's const proximity properties (if they
are defined). Nullptr otherwise. */
const ProximityProperties* proximity_properties() const {
if (proximity_properties_) return &*proximity_properties_;
return nullptr;
}
/** Returns a pointer to the geometry's mutable illustration properties (if
they are defined). Nullptr otherwise. */
IllustrationProperties* mutable_illustration_properties() {
if (illustration_properties_) return &*illustration_properties_;
return nullptr;
}
/** Returns a pointer to the geometry's const illustration properties (if
they are defined). Nullptr otherwise. */
const IllustrationProperties* illustration_properties() const {
if (illustration_properties_) return &*illustration_properties_;
return nullptr;
}
/** Returns a pointer to the geometry's mutable perception properties (if
they are defined). Nullptr otherwise. */
PerceptionProperties* mutable_perception_properties() {
if (perception_properties_) return &*perception_properties_;
return nullptr;
}
/** Returns a pointer to the geometry's const perception properties (if
they are defined). Nullptr otherwise. */
const PerceptionProperties* perception_properties() const {
if (perception_properties_) return &*perception_properties_;
return nullptr;
}
private:
// The *globally* unique identifier for this instance. It is functionally
// const (i.e. defined in construction) but not marked const to allow for
// default copying/assigning.
GeometryId id_{};
// The pose of the geometry relative to the parent frame it hangs on.
math::RigidTransform<double> X_PG_;
// The shape associated with this instance.
copyable_unique_ptr<Shape> shape_;
// The name of the geometry instance.
std::string name_;
// Optional properties.
std::optional<ProximityProperties> proximity_properties_{};
std::optional<IllustrationProperties> illustration_properties_{};
std::optional<PerceptionProperties> perception_properties_{};
};
} // namespace geometry
} // namespace drake