-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
scene_graph_inspector_test.cc
158 lines (133 loc) · 6.23 KB
/
scene_graph_inspector_test.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
#include "drake/geometry/scene_graph_inspector.h"
#include <algorithm>
#include <memory>
#include <vector>
#include <gtest/gtest.h>
#include "drake/geometry/geometry_frame.h"
#include "drake/geometry/geometry_instance.h"
#include "drake/geometry/geometry_state.h"
namespace drake {
namespace geometry {
// Helper class for testing the SceneGraphInspector; specifically to create
// a geometry state and instantiate the inspector.
class SceneGraphInspectorTester {
public:
SceneGraphInspectorTester() { inspector_.set(&state_); }
const SceneGraphInspector<double>& inspector() const { return inspector_; }
GeometryState<double>& mutable_state() { return state_; }
private:
GeometryState<double> state_;
SceneGraphInspector<double> inspector_;
};
namespace {
using math::RigidTransformd;
using std::make_unique;
// Simply exercises all the methods to confirm there's no build or execution
// problems. NOTE: All methods on SceneGraphInspector should be invoked here.
// Because these are thin wrappers of GeometryState methods, the *correctness*
// of these methods is left to be tested directly on GeometryState. NOTE: if
// SceneGraphInspector adds methods that actually have meaningful internal
// logic, they should be tested explicitly for correctness of that logic.
GTEST_TEST(SceneGraphInspector, ExerciseEverything) {
SceneGraphInspectorTester tester;
const SceneGraphInspector<double>& inspector = tester.inspector();
// Scene-graph wide data methods.
inspector.num_sources();
inspector.num_frames();
inspector.num_geometries();
inspector.GetAllGeometryIds();
inspector.GetGeometryIds(GeometrySet{});
inspector.NumGeometriesWithRole(Role::kUnassigned);
inspector.NumDynamicGeometries();
inspector.NumAnchoredGeometries();
inspector.GetCollisionCandidates();
// Source and source-related data methods.
// Register a source to prevent exceptions being thrown.
const SourceId source_id = tester.mutable_state().RegisterNewSource("name");
inspector.SourceIsRegistered(source_id);
inspector.GetName(source_id);
inspector.NumFramesForSource(source_id);
inspector.FramesForSource(source_id);
// Frames and their properties.
// Register a frame to prevent exceptions being thrown.
const FrameId frame_id =
tester.mutable_state().RegisterFrame(source_id, GeometryFrame("frame"));
inspector.BelongsToSource(frame_id, source_id);
inspector.GetOwningSourceName(frame_id);
inspector.GetName(frame_id);
inspector.GetParentFrame(frame_id);
inspector.GetFrameGroup(frame_id);
inspector.NumGeometriesForFrame(frame_id);
inspector.NumGeometriesForFrameWithRole(frame_id, Role::kUnassigned);
inspector.GetGeometries(frame_id, Role::kUnassigned);
// Register a geometry to prevent an exception being thrown.
const GeometryId geometry_id =
tester.mutable_state().RegisterGeometry(
source_id, frame_id,
make_unique<GeometryInstance>(RigidTransformd::Identity(),
make_unique<Sphere>(1.0), "sphere"));
inspector.GetGeometryIdByName(frame_id, Role::kUnassigned, "sphere");
// Geometries and their properties.
inspector.BelongsToSource(geometry_id, source_id);
inspector.GetOwningSourceName(geometry_id);
inspector.GetFrameId(geometry_id);
inspector.GetName(geometry_id);
inspector.GetShape(geometry_id);
inspector.GetPoseInParent(geometry_id);
inspector.GetPoseInFrame(geometry_id);
inspector.maybe_get_hydroelastic_mesh(geometry_id);
inspector.GetProximityProperties(geometry_id);
inspector.GetIllustrationProperties(geometry_id);
inspector.GetPerceptionProperties(geometry_id);
inspector.GetReferenceMesh(geometry_id);
inspector.GetAllDeformableGeometryIds();
inspector.GetReferenceMesh(geometry_id);
// Register an *additional* geometry and assign proximity properties to both
// to prevent an exception being thrown.
const GeometryId geometry_id2 =
tester.mutable_state().RegisterGeometry(
source_id, frame_id,
make_unique<GeometryInstance>(RigidTransformd::Identity(),
make_unique<Sphere>(1.0), "sphere2"));
tester.mutable_state().AssignRole(source_id, geometry_id,
ProximityProperties());
tester.mutable_state().AssignRole(source_id, geometry_id2,
ProximityProperties());
inspector.CollisionFiltered(geometry_id, geometry_id2);
// Tests cloning a geometry.
std::unique_ptr<GeometryInstance> geometry_instance_clone =
inspector.CloneGeometryInstance(geometry_id);
EXPECT_NE(geometry_instance_clone->id(), geometry_id);
EXPECT_EQ(geometry_instance_clone->name(), "sphere");
const auto* shape_clone = dynamic_cast<const Sphere*>(
&geometry_instance_clone->shape());
EXPECT_NE(shape_clone, nullptr);
EXPECT_EQ(shape_clone->radius(), 1.0);
EXPECT_NE(geometry_instance_clone->proximity_properties(), nullptr);
EXPECT_EQ(geometry_instance_clone->perception_properties(), nullptr);
EXPECT_EQ(geometry_instance_clone->illustration_properties(), nullptr);
inspector.geometry_version();
}
// Generally, SceneGraphInspector is a thin wrapper for invoking methods on
// GeometryState. SceneGraphInspector::GetAllFrameIds() is an exception, it does
// transformation work and, as such, needs to be functionally tested.
GTEST_TEST(SceneGraphInspector, AllFrameIds) {
SceneGraphInspectorTester tester;
const SourceId source_id = tester.mutable_state().RegisterNewSource("source");
// Always includes the world frame.
ASSERT_EQ(tester.inspector().GetAllFrameIds().size(), 1);
// Add a number of frames.
const FrameId frame_id_1 =
tester.mutable_state().RegisterFrame(source_id, GeometryFrame("frame1"));
const FrameId frame_id_2 =
tester.mutable_state().RegisterFrame(source_id, GeometryFrame("frame2"));
const FrameId frame_id_3 =
tester.mutable_state().RegisterFrame(source_id, GeometryFrame("frame3"));
const std::vector<FrameId> all_frames = tester.inspector().GetAllFrameIds();
EXPECT_EQ(std::count(all_frames.begin(), all_frames.end(), frame_id_1), 1);
EXPECT_EQ(std::count(all_frames.begin(), all_frames.end(), frame_id_2), 1);
EXPECT_EQ(std::count(all_frames.begin(), all_frames.end(), frame_id_3), 1);
}
} // namespace
} // namespace geometry
} // namespace drake