forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meshcat_point_cloud_visualizer.cc
79 lines (64 loc) · 2.51 KB
/
meshcat_point_cloud_visualizer.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
#include "drake/geometry/meshcat_point_cloud_visualizer.h"
#include <memory>
#include <string>
#include <utility>
#include <fmt/format.h>
#include "drake/common/extract_double.h"
#include "drake/geometry/utilities.h"
#include "drake/perception/point_cloud.h"
namespace drake {
namespace geometry {
template <typename T>
MeshcatPointCloudVisualizer<T>::MeshcatPointCloudVisualizer(
std::shared_ptr<Meshcat> meshcat, std::string path, double publish_period)
: systems::LeafSystem<T>(
systems::SystemTypeTag<MeshcatPointCloudVisualizer>{}),
meshcat_(std::move(meshcat)),
path_(std::move(path)),
publish_period_(publish_period) {
DRAKE_DEMAND(meshcat_ != nullptr);
DRAKE_DEMAND(publish_period >= 0.0);
this->DeclarePeriodicPublishEvent(
publish_period, 0.0,
&MeshcatPointCloudVisualizer<T>::UpdateMeshcat);
this->DeclareForcedPublishEvent(
&MeshcatPointCloudVisualizer<T>::UpdateMeshcat);
cloud_input_port_ =
this->DeclareAbstractInputPort("cloud", Value<perception::PointCloud>())
.get_index();
pose_input_port_ = this->DeclareAbstractInputPort(
"X_ParentCloud", Value<math::RigidTransform<T>>{})
.get_index();
}
template <typename T>
template <typename U>
MeshcatPointCloudVisualizer<T>::MeshcatPointCloudVisualizer(
const MeshcatPointCloudVisualizer<U>& other)
: MeshcatPointCloudVisualizer(other.meshcat_, other.path_,
other.publish_period_) {
set_point_size(other.point_size_);
set_default_rgba(other.default_rgba_);
}
template <typename T>
void MeshcatPointCloudVisualizer<T>::Delete() const {
meshcat_->Delete(path_);
}
template <typename T>
systems::EventStatus MeshcatPointCloudVisualizer<T>::UpdateMeshcat(
const systems::Context<T>& context) const {
const auto& cloud =
cloud_input_port().template Eval<perception::PointCloud>(context);
meshcat_->SetObject(path_, cloud, point_size_, default_rgba_);
const math::RigidTransformd X_ParentCloud =
pose_input_port().HasValue(context)
? internal::convert_to_double(
pose_input_port().template Eval<math::RigidTransform<T>>(
context))
: math::RigidTransformd::Identity();
meshcat_->SetTransform(path_, X_ParentCloud);
return systems::EventStatus::Succeeded();
}
} // namespace geometry
} // namespace drake
DRAKE_DEFINE_CLASS_TEMPLATE_INSTANTIATIONS_ON_DEFAULT_NONSYMBOLIC_SCALARS(
class ::drake::geometry::MeshcatPointCloudVisualizer)