TAMSVIZ
Visualization and annotation tool for ROS
All Classes Pages
interactive.h
1 // TAMSVIZ
2 // (c) 2020 Philipp Ruppel
3 
4 #pragma once
5 
6 #include "frame.h"
7 #include "marker.h"
8 #include "text.h"
9 
10 #include "../core/topic.h"
11 #include "../core/watcher.h"
12 
13 #include <geometry_msgs/PointStamped.h>
14 #include <geometry_msgs/PoseStamped.h>
15 #include <tf2_msgs/TFMessage.h>
16 #include <visualization_msgs/InteractiveMarker.h>
17 #include <visualization_msgs/InteractiveMarkerFeedback.h>
18 #include <visualization_msgs/InteractiveMarkerInit.h>
19 #include <visualization_msgs/InteractiveMarkerUpdate.h>
20 
21 class InteractiveMarker;
23 
25  bool show_descriptions = false;
26  double description_size = 1.0;
27  Eigen::Vector2d description_offset = Eigen::Vector2d::Zero();
28  std::shared_ptr<Material> description_material;
29 };
30 
32  std::mutex _mutex;
33  std::vector<std::shared_ptr<VisualizationMarker>> _markers;
34  int _interaction_mode = 0;
35  InteractiveMarker *_parent = nullptr;
36  Eigen::Quaterniond _orientation = Eigen::Quaterniond::Identity();
37  std::string _name;
38  std::shared_ptr<InteractiveMarkerParameters> _params;
39 
40 public:
42  const visualization_msgs::InteractiveMarkerControl &message,
43  const std::shared_ptr<InteractiveMarkerParameters> &params,
44  InteractiveMarker *parent);
45  virtual bool interact(const Interaction &interaction) override;
46  void update(const visualization_msgs::InteractiveMarkerControl &message);
47  InteractiveMarker *parentInteractiveMarker() { return _parent; }
48  const std::string &controlName() const { return _name; }
49 };
50 
51 class InteractiveMarker : public SceneNode {
52  std::shared_ptr<TextRenderer> _text;
53  InteractiveMarkerArray *_parent = nullptr;
54  std::mutex _mutex;
55  std::vector<std::shared_ptr<InteractiveMarkerControl>> _controls;
56  std::string _name;
57  bool _dragged = false;
58  std::shared_ptr<InteractiveMarkerParameters> _params;
59  double _scale = 1.0;
60 
61 public:
62  InteractiveMarker(const visualization_msgs::InteractiveMarker &message,
63  const std::shared_ptr<InteractiveMarkerParameters> &params,
64  InteractiveMarkerArray *parent);
65  void update(const visualization_msgs::InteractiveMarker &message);
66  void update(const visualization_msgs::InteractiveMarkerPose &message);
67  InteractiveMarkerArray *parentInteractiveMarkerArray() { return _parent; }
68  const std::string &markerName() const { return _name; }
69  virtual bool interact(const Interaction &interaction) override;
70  virtual void renderSync(const RenderSyncContext &context) override;
71 };
72 
74  std::mutex _mutex;
75  std::map<std::string, std::shared_ptr<InteractiveMarker>> _markers;
76  std::shared_ptr<InteractiveMarkerParameters> _params;
77 
78 public:
80  const std::shared_ptr<InteractiveMarkerParameters> &params);
81  void init(const visualization_msgs::InteractiveMarkerInit &message);
82  void update(const visualization_msgs::InteractiveMarkerUpdate &message);
83  virtual void renderSync(const RenderSyncContext &context) override;
85  std::shared_ptr<InteractiveMarker> marker(const std::string &name);
86 };
87 
89 protected:
90  std::shared_ptr<InteractiveMarkerParameters> _params;
91  std::shared_ptr<InteractiveMarkerArray> _markers;
93  visualization_msgs::InteractiveMarker makePointMarker();
94  visualization_msgs::InteractiveMarker makePoseMarker();
95  visualization_msgs::InteractiveMarker makeRotationMarker();
96 
97 public:
98  virtual void renderSync(const RenderSyncContext &context) override;
99 };
101 
103  std::shared_ptr<Subscriber<visualization_msgs::InteractiveMarkerUpdate>>
104  _update_subscriber;
105  std::shared_ptr<Subscriber<visualization_msgs::InteractiveMarkerInit>>
106  _init_subscriber;
107  Watcher _watcher;
108  std::shared_ptr<Publisher<visualization_msgs::InteractiveMarkerFeedback>>
109  _publisher;
110  static std::vector<std::string> listTopicNamespaces();
111 
112 public:
113  PROPERTY(std::string, topicNamespace, "",
114  list = [](const Property &property) {
115  return InteractiveMarkerDisplay::listTopicNamespaces();
116  });
117  PROPERTY(bool, showDescriptions, true);
118  PROPERTY(double, descriptionSize, 0.2, min = 0);
119  PROPERTY(Eigen::Vector2d, descriptionOffset, Eigen::Vector2d(0, 0.85));
120  PROPERTY(Color3, descriptionColor, Color3(1, 1, 1));
121  PROPERTY(double, descriptionOpacity, 1.0, min = 0, max = 1);
123  virtual void renderSync(const RenderSyncContext &context) override;
124 };
126 
128  void publish();
129  struct PublishThreadData {
130  std::condition_variable _stop_condition;
131  std::mutex _publish_mutex;
132  bool _stop_flag = false;
133  };
134  std::shared_ptr<PublishThreadData> _publish_thread_data;
135 
136 protected:
139  Watcher _publish_watcher;
140  virtual void publish(const std::string &frame,
141  const Eigen::Isometry3d &pose) = 0;
142  static uint64_t publisherClock() {
143  return std::chrono::duration_cast<std::chrono::seconds>(
144  std::chrono::steady_clock::now().time_since_epoch())
145  .count();
146  };
147 
148 public:
149  PROPERTY(Frame, frame);
150  PROPERTY(Pose, transform);
151  PROPERTY(double, scale, 1.0, min = 0.0);
152  virtual void renderSync(const RenderSyncContext &context) override;
153  virtual bool interact(const Interaction &interaction) override;
154  virtual void refresh() override;
155 };
157 
159  Publisher<tf2_msgs::TFMessage> _publisher{"/tf"};
160  PROPERTY(std::string, childFrame);
162  virtual void publish(const std::string &frame,
163  const Eigen::Isometry3d &pose) override;
164 };
166  Transform);
167 
168 static std::array<double, 16> poseToArray(const Eigen::Isometry3d &pose) {
169  std::array<double, 16> ret;
170  for (size_t row = 0; row < pose.matrix().rows(); row++) {
171  for (size_t col = 0; col < pose.matrix().cols(); col++) {
172  ret.at(row * pose.matrix().cols() + col) = pose.matrix()(row, col);
173  }
174  }
175  return ret;
176 }
Definition: node.h:20
Definition: watcher.h:8
Definition: document.h:83