Skip to content

Commit 5772807

Browse files
committed
configure mouse dragging with a single parameter
1 parent 97ad687 commit 5772807

5 files changed

Lines changed: 39 additions & 40 deletions

File tree

bindings/generated_docstrings/multibody_meshcat.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,11 @@ independent of the body's mass.
417417
418418
When no drag is in progress the output is empty. Any body with
419419
geometry published to Meshcat by a geometry∷MeshcatVisualizer can be
420-
dragged; the world body cannot.)""";
420+
dragged; the world body cannot.
421+
422+
This system is ``double``-only, because Meshcat reports drag state as
423+
plain doubles and mouse interaction is not meaningful for other scalar
424+
types.)""";
421425
// Symbol: drake::multibody::meshcat::MeshcatMouseSpring::AddToBuilder
422426
struct /* AddToBuilder */ {
423427
// Source: drake/multibody/meshcat/meshcat_mouse_spring.h
@@ -434,7 +438,7 @@ newly-added system.
434438
`plant`'s applied-spatial-force input port is not already
435439
connected.)""";
436440
} AddToBuilder;
437-
// Symbol: drake::multibody::meshcat::MeshcatMouseSpring::MeshcatMouseSpring<T>
441+
// Symbol: drake::multibody::meshcat::MeshcatMouseSpring::MeshcatMouseSpring
438442
struct /* ctor */ {
439443
// Source: drake/multibody/meshcat/meshcat_mouse_spring.h
440444
const char* doc =
@@ -464,21 +468,21 @@ Parameter ``stiffness``:
464468
// Source: drake/multibody/meshcat/meshcat_mouse_spring.h
465469
const char* doc =
466470
R"""(Returns the input port for the bodies' poses (a
467-
``std∷vector<math∷RigidTransform<T>>``).)""";
471+
``std∷vector<math∷RigidTransform<double>>``).)""";
468472
} get_body_poses_input_port;
469473
// Symbol: drake::multibody::meshcat::MeshcatMouseSpring::get_body_spatial_velocities_input_port
470474
struct /* get_body_spatial_velocities_input_port */ {
471475
// Source: drake/multibody/meshcat/meshcat_mouse_spring.h
472476
const char* doc =
473477
R"""(Returns the input port for the bodies' spatial velocities (a
474-
``std∷vector<SpatialVelocity<T>>``).)""";
478+
``std∷vector<SpatialVelocity<double>>``).)""";
475479
} get_body_spatial_velocities_input_port;
476480
// Symbol: drake::multibody::meshcat::MeshcatMouseSpring::get_spatial_forces_output_port
477481
struct /* get_spatial_forces_output_port */ {
478482
// Source: drake/multibody/meshcat/meshcat_mouse_spring.h
479483
const char* doc =
480484
R"""(Returns the output port for the applied spatial forces (a
481-
``std∷vector<ExternallyAppliedSpatialForce<T>>``).)""";
485+
``std∷vector<ExternallyAppliedSpatialForce<double>>``).)""";
482486
} get_spatial_forces_output_port;
483487
} MeshcatMouseSpring;
484488
} meshcat;

bindings/generated_docstrings/visualization.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -581,18 +581,6 @@ R"""(Determines whether to enable alpha sliders for geometry display.)""";
581581
const char* doc =
582582
R"""(Whether to create a Meshcat object if needed.)""";
583583
} enable_meshcat_creation;
584-
// Symbol: drake::visualization::VisualizationConfig::enable_mouse_interaction
585-
struct /* enable_mouse_interaction */ {
586-
// Source: drake/visualization/visualization_config.h
587-
const char* doc =
588-
R"""(Whether to enable interactively dragging bodies with the mouse in
589-
Meshcat. In a connected Meshcat browser, holding Ctrl and dragging a
590-
body with the left mouse button applies a virtual spring force that
591-
pulls the body toward the cursor (see
592-
multibody∷meshcat∷MeshcatMouseSpring). Has no effect for Meldis/LCM,
593-
and is skipped if the plant's applied-spatial-force input port is
594-
already connected.)""";
595-
} enable_mouse_interaction;
596584
// Symbol: drake::visualization::VisualizationConfig::initial_proximity_alpha
597585
struct /* initial_proximity_alpha */ {
598586
// Source: drake/visualization/visualization_config.h
@@ -617,8 +605,14 @@ See also:
617605
// Source: drake/visualization/visualization_config.h
618606
const char* doc =
619607
R"""(The mass-normalized stiffness (in 1/s²) of the interactive mouse
620-
spring; see multibody∷meshcat∷MeshcatMouseSpring. Only used when
621-
enable_mouse_interaction is true.)""";
608+
spring that lets a user drag bodies with the mouse in Meshcat. In a
609+
connected Meshcat browser, holding Ctrl and dragging a body with the
610+
left mouse button applies a virtual spring force that pulls the body
611+
toward the cursor (see multibody∷meshcat∷MeshcatMouseSpring).
612+
613+
Setting this to std∷nullopt disables mouse interaction. It also has no
614+
effect for Meldis/LCM, and is skipped if the plant's
615+
applied-spatial-force input port is already connected.)""";
622616
} mouse_interaction_stiffness;
623617
// Symbol: drake::visualization::VisualizationConfig::publish_contacts
624618
struct /* publish_contacts */ {
@@ -656,7 +650,6 @@ point; see drake#15021 for details.))""";
656650
std::make_pair("delete_on_initialization_event", delete_on_initialization_event.doc),
657651
std::make_pair("enable_alpha_sliders", enable_alpha_sliders.doc),
658652
std::make_pair("enable_meshcat_creation", enable_meshcat_creation.doc),
659-
std::make_pair("enable_mouse_interaction", enable_mouse_interaction.doc),
660653
std::make_pair("initial_proximity_alpha", initial_proximity_alpha.doc),
661654
std::make_pair("lcm_bus", lcm_bus.doc),
662655
std::make_pair("mouse_interaction_stiffness", mouse_interaction_stiffness.doc),

visualization/test/visualization_config_functions_test.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "drake/visualization/visualization_config_functions.h"
22

33
#include <memory>
4+
#include <optional>
45
#include <set>
56
#include <string>
67
#include <vector>
@@ -247,7 +248,7 @@ GTEST_TEST(VisualizationConfigFunctionsTest, ApplyNothing) {
247248
config.publish_inertia = false;
248249
config.publish_proximity = false;
249250
config.publish_contacts = false;
250-
config.enable_mouse_interaction = false;
251+
config.mouse_interaction_stiffness = std::nullopt;
251252

252253
// We'll fail in case any message is transmitted.
253254
DrakeLcm drake_lcm;
@@ -311,13 +312,13 @@ GTEST_TEST(VisualizationConfigFunctionsTest, MouseInteraction) {
311312
EXPECT_EQ(count_springs(builder), 1);
312313
}
313314

314-
// Disabled via the config flag.
315+
// Disabled by clearing the stiffness.
315316
{
316317
DiagramBuilder<double> builder;
317318
auto [plant, scene_graph] = AddMultibodyPlantSceneGraph(&builder, 0.0);
318319
plant.Finalize();
319320
VisualizationConfig config;
320-
config.enable_mouse_interaction = false;
321+
config.mouse_interaction_stiffness = std::nullopt;
321322
ApplyVisualizationConfig(config, &builder, &lcm_buses, &plant, &scene_graph,
322323
meshcat);
323324
EXPECT_EQ(count_springs(builder), 0);

visualization/visualization_config.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <optional>
34
#include <string>
45

56
#include <Eigen/Dense>
@@ -29,7 +30,6 @@ struct VisualizationConfig {
2930
a->Visit(DRAKE_NVP(enable_meshcat_creation));
3031
a->Visit(DRAKE_NVP(delete_on_initialization_event));
3132
a->Visit(DRAKE_NVP(enable_alpha_sliders));
32-
a->Visit(DRAKE_NVP(enable_mouse_interaction));
3333
a->Visit(DRAKE_NVP(mouse_interaction_stiffness));
3434
}
3535

@@ -81,18 +81,16 @@ struct VisualizationConfig {
8181
/** Determines whether to enable alpha sliders for geometry display. */
8282
bool enable_alpha_sliders{false};
8383

84-
/** Whether to enable interactively dragging bodies with the mouse in Meshcat.
85-
In a connected Meshcat browser, holding Ctrl and dragging a body with the
86-
left mouse button applies a virtual spring force that pulls the body toward
87-
the cursor (see multibody::meshcat::MeshcatMouseSpring). Has no effect for
88-
Meldis/LCM, and is skipped if the plant's applied-spatial-force input port is
89-
already connected. */
90-
bool enable_mouse_interaction{true};
91-
92-
/** The mass-normalized stiffness (in 1/s²) of the interactive mouse spring;
93-
see multibody::meshcat::MeshcatMouseSpring. Only used when
94-
enable_mouse_interaction is true. */
95-
double mouse_interaction_stiffness{100.0};
84+
/** The mass-normalized stiffness (in 1/s²) of the interactive mouse spring
85+
that lets a user drag bodies with the mouse in Meshcat. In a connected
86+
Meshcat browser, holding Ctrl and dragging a body with the left mouse button
87+
applies a virtual spring force that pulls the body toward the cursor (see
88+
multibody::meshcat::MeshcatMouseSpring).
89+
90+
Setting this to std::nullopt disables mouse interaction. It also has no
91+
effect for Meldis/LCM, and is skipped if the plant's applied-spatial-force
92+
input port is already connected. */
93+
std::optional<double> mouse_interaction_stiffness{100.0};
9694
};
9795

9896
} // namespace visualization

visualization/visualization_config_functions.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,23 @@ void ApplyVisualizationConfigImpl(const VisualizationConfig& config,
7878
builder, plant, meshcat,
7979
internal::ConvertVisualizationConfigToMeshcatContactParams(config));
8080
}
81-
if (config.enable_mouse_interaction) {
81+
if (config.mouse_interaction_stiffness.has_value() &&
82+
config.mouse_interaction_stiffness.value() > 0) {
8283
// MeshcatMouseSpring drives the plant's applied-spatial-force input port.
8384
// Only add it if that port is still available; otherwise (e.g., the user
8485
// has already connected their own external forces) skip it with a warning
8586
// rather than failing the whole visualization setup.
87+
// TODO(vincekurtz): use some sort of spatial force combiner system to
88+
// allow multiple sources of spatial forces to be applied to the plant.
8689
if (builder->IsConnectedOrExported(
8790
plant.get_applied_spatial_force_input_port())) {
8891
log()->warn(
89-
"VisualizationConfig.enable_mouse_interaction is set, but the "
92+
"VisualizationConfig.mouse_interaction_stiffness is set, but the "
9093
"plant's applied-spatial-force input port is already connected, so "
9194
"interactive mouse dragging will not be enabled.");
9295
} else {
93-
MeshcatMouseSpring::AddToBuilder(
94-
builder, &plant, meshcat, config.mouse_interaction_stiffness);
96+
MeshcatMouseSpring::AddToBuilder(builder, &plant, meshcat,
97+
*config.mouse_interaction_stiffness);
9598
}
9699
}
97100
if (config.publish_inertia && config.enable_alpha_sliders) {

0 commit comments

Comments
 (0)