Visualization.cpp
Go to the documentation of this file.
1 #include "Visualization.h"
2 
3 #include <chrono>
4 #include <thread>
5 
8 
9 
11 {
12 
14  params(params), arviz(arviz)
15  {
16  robotVis = viz::Robot("robot").file(params.robotModelFileName.serialize().package, params.robotModelFileName.serialize().path);
17  robotVis.useCollisionModel();
18  robotVis.hide();
19  robotVis.overrideColor(viz::Color::white(0));
20 
21 
22  arviz.commitLayerContaining("target", robotVis);
23  }
24 
25  void
27  {
28  // TODO(fabian.reister): more finegrained locking
29  std::lock_guard g{paramsMutex};
30 
31  if (!params.enableVisualization)
32  {
33  return;
34  }
35 
36  if (trajectory.poses().size() < 2)
37  {
38  ARMARX_WARNING << "Unable to visualize path with less than two segments";
39  return;
40  }
41 
42  // viz::Layer pathLayer = arviz.layer("path");
43  // for (size_t i = 1; i < path.size(); i++)
44  // {
45  // Eigen::Vector3f prevPose = Eigen::Vector3f::Zero();
46  // prevPose.head<2>() = path.at(i - 1).head<2>();
47  // Eigen::Vector3f currentPose = Eigen::Vector3f::Zero();
48  // currentPose.head<2>() = path.at(i).head<2>();
49  // viz::Arrow a = viz::Arrow("path_segment_" + std::to_string(i));
50  // a.color(viz::Color::orange());
51  // a.width(25);
52  // a.fromTo(prevPose, currentPose);
53  // pathLayer.add(a);
54  // }
55 
56  // arviz.commit({pathLayer});
57 
58  visualizeTrajectory(trajectory);
59  }
60 
61 
62  void
63  Visualization::visualizeTrajectory(const core::GlobalTrajectory& trajectory)
64  {
66  {
67  return;
68  }
69 
70  viz::Layer robotLayer = arviz.layer("trajectory");
71 
72  robotVis.overrideColor(viz::Color::white(params.alphaValue));
73  robotVis.show();
74  robotLayer.add(robotVis);
75 
76  const auto resampledTrajectory = trajectory.resample(10);
77  // const auto duration = trajectory.duration(core::VelocityInterpolation::LastWaypoint);
78 
79  const float speedup = 1.0;
80 
81  ARMARX_INFO << "Visualizing trajectory";
82 
83  const auto points = resampledTrajectory.points();
84  for (size_t i = 0; i < (points.size() - 1); i++)
85  {
86  const auto& wp = points.at(i);
87  robotVis.pose(wp.waypoint.pose.matrix());
88 
89  std::lock_guard<std::mutex> lock(mutex);
90  arviz.commit({robotLayer});
91 
92  const float distance =
93  (wp.waypoint.pose.translation() - points.at(i + 1).waypoint.pose.translation())
94  .norm();
95 
96  ARMARX_CHECK(wp.velocity > 0.F);
97  const float velocity = 200;
98  const int dt = static_cast<int>(speedup * distance / velocity); // [ms]
99 
100  std::this_thread::sleep_for(std::chrono::milliseconds(dt));
101  }
102 
103  ARMARX_DEBUG << "Done visualizing trajectory";
104 
105  robotVis.hide();
106 
107  std::lock_guard<std::mutex> lock(mutex);
108  arviz.commit({robotLayer});
109  }
110 
111 
112  void
113  Visualization::asyncHideDelayed()
114  {
115  std::thread{
116  [this]
117  {
118  std::this_thread::sleep_for(std::chrono::seconds(params.hideDelaySeconds));
119 
120  std::lock_guard<std::mutex> lock(mutex);
121 
122  if (lastUpdate <= (TimeUtil::GetTime().toSeconds() - params.hideDelaySeconds))
123  {
124  robotVis.hide();
125  // arviz.commitLayerContaining("target", robotVis);
126  arviz.commit(arviz.layer("target"));
127  arviz.commit(arviz.layer("path"));
128  }
129  }}
130  .detach();
131  }
132 
133  void
135  {
136  // TODO(fabian.reister): more finegrained locking
137  std::lock_guard g{paramsMutex};
138 
139  if (!params.enableVisualization)
140  {
141  return;
142  }
143 
144  robotVis.overrideColor(viz::Color::green(255, params.alphaValue));
145  {
146  std::lock_guard<std::mutex> lock(mutex);
147  arviz.commitLayerContaining("target", robotVis);
148  }
149  asyncHideDelayed();
150  }
151 
152  void
154  {
155  // TODO(fabian.reister): more finegrained locking
156  std::lock_guard g{paramsMutex};
157 
158  if (!params.enableVisualization)
159  {
160  return;
161  }
162  robotVis.overrideColor(viz::Color::red(255, params.alphaValue));
163  {
164  std::lock_guard<std::mutex> lock(mutex);
165  arviz.commitLayerContaining("target", robotVis);
166  }
167 
168  asyncHideDelayed();
169  }
170 
171  void
173  {
174  // TODO(fabian.reister): more finegrained locking
175  std::lock_guard g{paramsMutex};
176 
177  if (!params.enableVisualization)
178  {
179  return;
180  }
181 
182  robotVis.show();
183  robotVis.overrideColor(viz::Color::azure(128, params.alphaValue));
184  robotVis.pose(m.matrix());
185 
186  std::lock_guard<std::mutex> lock(mutex);
187  arviz.commitLayerContaining("target", robotVis);
188 
189  lastUpdate = TimeUtil::GetTime().toSeconds();
190  }
191 
192 
193 } // namespace armarx::navigation::util
armarx::navigation::core::GlobalTrajectory
Definition: Trajectory.h:68
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::navigation::util::Visualization::failed
void failed()
Definition: Visualization.cpp:153
armarx::navigation::util::Visualization::Params::alphaValue
int alphaValue
Definition: Visualization.h:31
armarx::navigation::util
This file is part of ArmarX.
Definition: geometry.cpp:8
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
Visualization.h
armarx::navigation::util::Visualization::visualize
void visualize(const core::GlobalTrajectory &trajectory)
Definition: Visualization.cpp:26
armarx::navigation::util::Visualization::setTarget
void setTarget(const core::Pose &m)
Definition: Visualization.cpp:172
armarx::viz::Color::white
static Color white(int a=255)
Definition: Color.h:67
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::PackagePath::serialize
data::PackagePath serialize() const
Definition: PackagePath.cpp:76
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::viz::Robot::useCollisionModel
Robot & useCollisionModel()
Definition: Robot.h:31
armarx::viz::Robot::overrideColor
Robot & overrideColor(Color c)
Definition: Robot.h:45
armarx::navigation::util::Visualization::Params::hideDelaySeconds
int hideDelaySeconds
Definition: Visualization.h:29
armarx::navigation::core::GlobalTrajectory::poses
std::vector< Pose > poses() const noexcept
Definition: Trajectory.cpp:344
armarx::viz::Color::red
static Color red(int r=255, int a=255)
Definition: Color.h:79
armarx::navigation::util::Visualization::Visualization
Visualization(armarx::viz::Client &arviz, const Params &params)
Definition: Visualization.cpp:13
armarx::navigation::util::Visualization::Params::enableTrajectoryVisualization
bool enableTrajectoryVisualization
Definition: Visualization.h:27
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:159
armarx::navigation::util::Visualization::Params
Definition: Visualization.h:22
armarx::viz::ElementOps::show
DerivedT & show()
Definition: ElementOps.h:255
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
armarx::viz::ElementOps::hide
DerivedT & hide()
Definition: ElementOps.h:248
armarx::viz::ScopedClient::layer
Layer layer(std::string const &name) const
Definition: ScopedClient.cpp:34
Logging.h
armarx::viz::Color::green
static Color green(int g=255, int a=255)
Definition: Color.h:84
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::viz::Color::azure
static Color azure(int az=255, int a=255)
2 Blue + 1 Green
Definition: Color.h:143
armarx::viz::Client
Definition: Client.h:109
armarx::viz::Layer
Definition: Layer.h:12
armarx::navigation::util::Visualization::Params::robotModelFileName
armarx::PackagePath robotModelFileName
Definition: Visualization.h:24
armarx::navigation::core::GlobalTrajectory::resample
GlobalTrajectory resample(float eps) const
Definition: Trajectory.cpp:581
armarx::navigation::util::Visualization::Params::enableVisualization
bool enableVisualization
Definition: Visualization.h:26
armarx::viz::Client::commitLayerContaining
void commitLayerContaining(std::string const &name)
Definition: Client.h:153
dt
constexpr T dt
Definition: UnscentedKalmanFilterTest.cpp:42
armarx::navigation::util::Visualization::success
void success()
Definition: Visualization.cpp:134
norm
double norm(const Point &a)
Definition: point.hpp:94