-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
distance_to_point_callback.h
279 lines (236 loc) · 11.2 KB
/
distance_to_point_callback.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
#pragma once
#include <algorithm>
#include <limits>
#include <tuple>
#include <unordered_map>
#include <vector>
#include <fcl/fcl.h>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_export.h"
#include "drake/common/eigen_types.h"
#include "drake/geometry/geometry_ids.h"
#include "drake/geometry/proximity/proximity_utilities.h"
#include "drake/geometry/query_results/signed_distance_to_point.h"
#include "drake/math/rigid_transform.h"
namespace drake {
namespace geometry {
namespace internal {
namespace point_distance DRAKE_NO_EXPORT {
/* Supporting data for the distance-to-point callback (see Callback below).
It includes:
- The query point Q, measured and expressed in the world frame, `p_WQ_W`.
- The fcl collision object representing the query point, Q.
- A distance threshold beyond which distances will not be reported.
- The pose of the geometry being queried against.
- The T-valued poses of _all_ geometries in the corresponding SceneGraph
keyed on their GeometryId.
- A vector of distance results -- one instance of SignedDistanceToPoint for
every supported geometry which lies with the threshold.
*/
template <typename T>
struct CallbackData {
/* Constructs the fully-specified callback data. The values are as described
above. _Some_ of the parameters are aliased in the data and require the
aliased parameters to remain valid at least as long as the CallbackData
instance.
@param query_in The object representing the query point. Aliased.
@param threshold_in The query threshold.
@param p_WQ_W_in The T-valued position of the query point.
@param X_WGs_in The T-valued poses. Aliased.
@param distances_in[out] The output results. Aliased.
*/
CallbackData(
fcl::CollisionObjectd* query_in, const double threshold_in,
const Vector3<T>& p_WQ_W_in,
const std::unordered_map<GeometryId, math::RigidTransform<T>>* X_WGs_in,
std::vector<SignedDistanceToPoint<T>>* distances_in)
: query_point(*query_in),
threshold(threshold_in),
p_WQ_W(p_WQ_W_in),
X_WGs(*X_WGs_in),
distances(*distances_in) {
DRAKE_DEMAND(query_in != nullptr);
DRAKE_DEMAND(X_WGs_in != nullptr);
DRAKE_DEMAND(distances_in != nullptr);
}
/* The query fcl object. */
const fcl::CollisionObjectd& query_point;
/* The query threshold. */
const double threshold;
/* The T-valued query point Q. */
const Vector3<T> p_WQ_W;
/* The T-valued pose of every geometry. */
const std::unordered_map<GeometryId, math::RigidTransform<T>>& X_WGs;
/* The accumulator for results. */
std::vector<SignedDistanceToPoint<T>>& distances;
};
/* @name Functions for computing distance from point to primitives
This family of functions compute the distance from a point Q to a primitive
shape. Refer to QueryObject::ComputeSignedDistanceToPoint() for more details.
@param X_WG The pose of the primitive geometry G in the world
frame.
@param p_WQ The position of the point Q in the world frame.
@param p_GN[out] The position of the witness point N on the
primitive, expressed in the primitive geometry
frame G.
@param distance[out] The signed distance from the point Q to the
primitive.
@param grad_W[out] The gradient of the distance function w.r.t p_GQ
(the position of the point Q in the primitive's
frame). This gradient vector has unit-length and
is expressed in the world frame. Where grad_W is
not unique, then an arbitrary value is
assigned (as documented in
QueryObject::ComputeSignedDistanceToPoint(). */
//@{
/* Computes distance from point to sphere with the understanding that all
quantities are measured and expressed in the sphere's frame, S. Otherwise, the
semantics of the parameters are as documented as above. */
template <typename T>
void SphereDistanceInSphereFrame(const fcl::Sphered& sphere,
const Vector3<T>& p_SQ, Vector3<T>* p_SN,
T* distance, Vector3<T>* grad_S);
/* Overload of ComputeDistanceToPrimitive() for sphere primitive. */
template <typename T>
void ComputeDistanceToPrimitive(const fcl::Sphered& sphere,
const math::RigidTransform<T>& X_WG,
const Vector3<T>& p_WQ, Vector3<T>* p_GN,
T* distance, Vector3<T>* grad_W);
/* Overload of ComputeDistanceToPrimitive() for halfspace primitive. */
template <typename T>
void ComputeDistanceToPrimitive(const fcl::Halfspaced& halfspace,
const math::RigidTransform<T>& X_WG,
const Vector3<T>& p_WQ, Vector3<T>* p_GN,
T* distance, Vector3<T>* grad_W);
/* Overload of ComputeDistanceToPrimitive() for capsule primitive. */
template <typename T>
void ComputeDistanceToPrimitive(const fcl::Capsuled& capsule,
const math::RigidTransform<T>& X_WG,
const Vector3<T>& p_WQ, Vector3<T>* p_GN,
T* distance, Vector3<T>* grad_W);
// TODO(DamrongGuoy): Add overloads for all supported geometries.
//@}
/* A functor to compute signed distance between a point and a geometry. By
design, one instance should be created for each unique pairing of query point Q
with geometry G. The functor is constructed with all the parameters _except_
the actual shape. It relies on overloading the () operator and ADL to handle
specific shape types. */
template <typename T>
class DistanceToPoint {
public:
/* Constructs the functor DistanceToPoint.
@param id The id of the geometry G,
@param X_WG The pose of the G in world frame,
@param p_WQ The position of the query point Q in world frame. */
DistanceToPoint(const GeometryId id, const math::RigidTransform<T>& X_WG,
const Vector3<T>& p_WQ)
: geometry_id_(id), X_WG_(X_WG), p_WQ_(p_WQ) {}
// TODO(DamrongGuoy): Revisit computation over operator() overloads as per
// issue: https://github.com/RobotLocomotion/drake/issues/11227
/* Overload to compute distance to a box. */
SignedDistanceToPoint<T> operator()(const fcl::Boxd& box);
/* Overload to compute distance to a capsule. */
SignedDistanceToPoint<T> operator()(const fcl::Capsuled& capsule);
/* Overload to compute distance to a cylinder. */
SignedDistanceToPoint<T> operator()(const fcl::Cylinderd& cylinder);
/* Overload to compute distance to an ellipsoid. */
SignedDistanceToPoint<T> operator()(const fcl::Ellipsoidd& ellipsoid);
/* Overload to compute distance to a halfspace. */
SignedDistanceToPoint<T> operator()(const fcl::Halfspaced& halfspace);
/* Overload to compute distance to a sphere. */
SignedDistanceToPoint<T> operator()(const fcl::Sphered& sphere);
/* Reports the "sign" of x with a small modification; Sign(0) --> 1.
@tparam U Templated to allow DistanceToPoint<AutoDiffXd> to still compute
Sign<double> or Sign<AutoDiffXd> as needed. */
template <typename U = T>
static U Sign(const U& x) {
return (x < U(0.0)) ? U(-1.0) : U(1.0);
}
/* Picks the axis i whose coordinate p(i) is closest to the boundary value
±bounds(i). If there are ties, we prioritize according to an arbitrary
ordering: +x,-x,+y,-y,+z,-z.
Note: the two vector types can be different scalars, they must be extractable
to double.
@tparam dim The number of rows in the column vector.
@tparam U The scalar type of the point `p`; defaults to T. */
template <int dim, typename U = T>
static int ExtremalAxis(const Vector<U, dim>& p,
const Vector<double, dim>& bounds);
/* Calculates the signed distance from a query point Q to a box, G. The box
can be either two-dimensional or three-dimensional. It is defined centered
on its frame G and aligned with G's axes. We use the 3D version directly
for the signed distance from Box and use the 2D version indirectly for the
signed distance from Cylinder.
@param h The vector from the center of the box to the positive
corner of the box measured in frame G (the "half" size of
the box).
Mathematically the box is the Cartesian product
[-h(0),h(0)]x[-h(1),h(1)] in 2D or
[-h(0),h(0)]x[-h(1),h(1)]x[-h(2),h(2)] in 3D.
@param p_GQ_G The position of the query point Q in G's frame.
@retval {p_GN_G, grad_G, is_Q_on_edge_or_vertex} The tuple of the position
of the nearest point N in G's frame and the gradient vector
in the same frame G, together with the type of the nearest
feature.
@tparam dim The dimension, must be 2 or 3. */
template <int dim>
static std::tuple<Vector<T, dim>, Vector<T, dim>, bool> ComputeDistanceToBox(
const Vector<double, dim>& h, const Vector<T, dim>& p_GQ_G);
private:
// The id of the geometry G.
const GeometryId geometry_id_;
// The pose of the geometry G in World frame.
const math::RigidTransform<T> X_WG_;
// The position of the query point Q in World frame.
const Vector3<T> p_WQ_;
};
// TODO(SeanCurtis-TRI): Replace this clunky mechanism with a new mechanism
// which does this implicitly via ADL and templates.
/* @name Mechanism for reporting on which scalars and for which shapes
point-to-shape queries can be made.
By default, nothing is supported. For each supported scalar type, a class
specialization is provided which whitelists the supported shape types.
@tparam T The computational scalar type. */
//@{
template <typename T>
struct ScalarSupport {
static bool is_supported(fcl::NODE_TYPE) { return false; }
};
/* Primitive support for double-valued query. */
template <>
struct ScalarSupport<double> {
static bool is_supported(fcl::NODE_TYPE node_type);
};
/* Primitive support for AutoDiff-valued query. */
template <>
struct ScalarSupport<AutoDiffXd> {
static bool is_supported(fcl::NODE_TYPE node_type) {
switch (node_type) {
case fcl::GEOM_SPHERE:
case fcl::GEOM_BOX:
case fcl::GEOM_HALFSPACE:
case fcl::GEOM_CAPSULE:
return true;
default:
return false;
}
}
};
//@}
/* The callback function for computing the signed distance between a point and
a supported shape. Intended to be invoked as a result of broadphase culling
of candidate geometry pairs for the pair (`object_A_ptr`, `object_B_ptr`).
@pre The `callback_data` is an instance of point_distance::CallbackData.
@pre One of the two fcl objects matches the CallbackData.query_point object.
*/
template <typename T>
bool Callback(fcl::CollisionObjectd* object_A_ptr,
fcl::CollisionObjectd* object_B_ptr,
// NOLINTNEXTLINE
void* callback_data, double& threshold);
// clang-format off
} // namespace point_distance
// clang-format on
} // namespace internal
} // namespace geometry
} // namespace drake