Visu.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package Navigation::ArmarXObjects::NavigationMemory
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "Visu.h"
24 
25 #include <SimoxUtility/color/Color.h>
26 #include <SimoxUtility/color/GlasbeyLUT.h>
27 #include <SimoxUtility/color/cmaps/colormaps.h>
28 
33 
39 #include <armarx/navigation/core/aron/Graph.aron.generated.h>
40 #include <armarx/navigation/core/aron/Location.aron.generated.h>
42 #include <armarx/navigation/human/aron/Human.aron.generated.h>
44 #include <armarx/navigation/rooms/aron/Room.aron.generated.h>
46 
48 {
49 
51  const armem::server::wm::CoreSegment& locSegment,
52  const armem::server::wm::CoreSegment& graphSegment,
53  const armem::server::wm::CoreSegment& costmapSegment,
54  const armem::server::wm::CoreSegment& humanSegment,
55  const armem::server::wm::CoreSegment& roomsSegment) :
56  arviz(arviz),
57  locSegment(locSegment),
58  graphSegment(graphSegment),
59  costmapSegment(costmapSegment),
60  humanSegment(humanSegment),
61  roomsSegment(roomsSegment),
62  visu(std::make_unique<graph::GraphVisu>())
63  {
64  }
65 
67  {
68  }
69 
70  void
72  const std::vector<ObjectInfo>& info,
73  viz::Layer& layer)
74  {
75  using namespace armem::server;
76 
77  std::map<armem::MemoryID, location::arondto::Location> locations;
79  [&]()
80  {
82  [&](const wm::Entity& entity)
83  {
84  if (const wm::EntityInstance* instance = entity.findLatestInstance())
85  {
86  locations[entity.id()].fromAron(instance->data());
87  }
88  });
89  });
90 
91  for (auto& [id, location] : locations)
92  {
93  FramedPose framedPose;
94  fromAron(location.framedPose, framedPose);
95 
96  const auto res = core::resolveLocation(objects, info, framedPose);
97  if (res.pose.has_value())
98  {
99  visu->vertex->draw(layer, id.str(), res.pose.value());
100  }
101  }
102  }
103 
104  void
106  const std::vector<ObjectInfo>& info,
107  std::vector<viz::Layer>& layers,
108  bool enabled)
109  {
110  using namespace armem::server;
111 
112  std::map<armem::MemoryID, core::Graph> graphs;
114  [&]()
115  {
117  [&](const wm::Entity& entity)
118  {
119  core::Graph& graph = graphs[entity.id()];
120  if (enabled)
121  {
122  if (const wm::EntityInstance* instance = entity.findLatestInstance())
123  {
124  navigation::core::arondto::Graph aron;
125  aron.fromAron(instance->data());
126  fromAron(aron, graph);
127  }
128  }
129  // else: empty layer
130  });
131  });
132 
133  for (auto& [id, graph] : graphs)
134  {
135  viz::Layer& layer = layers.emplace_back(arviz.layer(id.str()));
136  if (enabled)
137  {
139  visu->draw(layer, graph, {objects, info});
140  }
141  // else: clear layer
142  }
143  }
144 
145  void
146  Visu::drawLocations(std::vector<viz::Layer>& layers, bool enabled)
147  {
148  viz::Layer& layer = layers.emplace_back(arviz.layer(locSegment.id().str()));
149  if (enabled)
150  {
151  const objpose::ObjectPoseMap objects;
152  const std::vector<ObjectInfo> info;
153  drawLocations(objects, info, layer);
154  }
155  }
156 
157  void
159  std::vector<viz::Layer>& layers,
160  bool enabled)
161  {
162  viz::Layer& layer = layers.emplace_back(arviz.layer(locSegment.id().str()));
163  if (enabled)
164  {
165  const objpose::ObjectPoseMap objects = objClient.fetchObjectPosesAsMap();
166 
167  auto objectFinder = objClient.getObjectFinder();
168  objectFinder.setLogObjectDiscoveryError(false);
169  const std::vector<ObjectInfo> info = objectFinder.findAllObjects();
170 
171  drawLocations(objects, info, layer);
172  }
173  }
174 
175  void
176  Visu::drawGraphs(std::vector<viz::Layer>& layers, bool enabled)
177  {
178  const objpose::ObjectPoseMap objects;
179  const std::vector<ObjectInfo> info;
180  drawGraphs(objects, info, layers, enabled);
181  }
182 
183  void
185  std::vector<viz::Layer>& layers,
186  bool enabled)
187  {
188  const objpose::ObjectPoseMap objects = objClient.fetchObjectPosesAsMap();
189 
190  auto objectFinder = objClient.getObjectFinder();
191  objectFinder.setLogObjectDiscoveryError(false);
192  const std::vector<ObjectInfo> info = objectFinder.findAllObjects();
193 
194  drawGraphs(objects, info, layers, enabled);
195  }
196 
197  namespace
198  {
199 
200  inline void
201  visualize(const human::Humans& humans, viz::Layer& layer, const bool visuTransparent)
202  {
203 
204  const Eigen::Translation3f human_T_mmm(Eigen::Vector3f{0, 0, 1000});
205 
206  ARMARX_INFO << "Visualizing " << humans.size() << " humans";
207  for (const auto& human : humans)
208  {
209  // viz::Cylinder cylinder(std::to_string(layer.size()));
210  // cylinder.fromTo(conv::to3D(human.pose.translation()),
211  // conv::to3D(human.pose.translation()) + Eigen::Vector3f{0, 0, 10});
212 
213 
214  // cylinder.color(simox::Color::orange());
215  // cylinder.radius(300);
216  // layer.add(cylinder);
217 
218  core::Pose human3d = conv::to3D(human.pose) * human_T_mmm;
219 
220  viz::Robot mmm(std::to_string(layer.size()));
221  mmm.file("RobotAPI", "RobotAPI/robots/MMM/mmm.xml");
222  mmm.pose(human3d);
223  mmm.scale(1.7); // 1.7m
224  mmm.overrideColor(viz::Color::orange(255, visuTransparent ? 100 : 255));
225  layer.add(mmm);
226 
227 
228  if (human.linearVelocity != Eigen::Vector2f::Zero())
229  {
230  core::Pose vel3d = human3d;
231  vel3d.translation().head<2>() += human.linearVelocity * 2;
232  auto arrow = viz::Arrow(std::to_string(layer.size()))
233  .fromTo(human3d.translation(), vel3d.translation())
234  .color(simox::Color::red());
235  layer.add(arrow);
236  }
237  }
238  }
239 
240  } // namespace
241 
242  void
243  Visu::drawCostmaps(std::vector<viz::Layer>& layers, const bool enabled, const float zOffset)
244  {
245  if (not enabled)
246  {
247  return;
248  }
249 
250  std::map<std::string, std::vector<std::pair<std::string, algorithms::Costmap>>>
251  namedProviderCostmaps;
252 
254  [&]()
255  {
256  using namespace armem::server;
257 
259  [&](const wm::Entity& entity)
260  {
261  if (const wm::EntityInstance* instance = entity.findLatestInstance())
262  {
263  navigation::algorithms::Costmap costmap =
264  algorithms::fromAron(*instance);
265 
266  namedProviderCostmaps[instance->id().providerSegmentName].emplace_back(
267  instance->id().entityName, std::move(costmap));
268  }
269  });
270  });
271 
272  for (const auto& [providerName, namedCostmaps] : namedProviderCostmaps)
273  {
274  viz::Layer& layer = layers.emplace_back(arviz.layer("costmaps_" + providerName));
275  for (const auto& [name, costmap] : namedCostmaps)
276  {
277  algorithms::visualize(costmap, layer, name, zOffset);
278  }
279  }
280  }
281 
282  void
283  Visu::drawRooms(std::vector<viz::Layer>& layers, const bool enabled)
284  {
285  using namespace armem::server;
286 
287  std::map<armem::MemoryID, rooms::Room> rooms;
289  [&]()
290  {
292  [&](const wm::Entity& entity)
293  {
294  rooms::Room& room = rooms[entity.id()];
295  if (enabled)
296  {
297  if (const wm::EntityInstance* instance = entity.findLatestInstance())
298  {
299  navigation::rooms::arondto::Room aron;
300  aron.fromAron(instance->data());
301  fromAron(aron, room);
302  }
303  }
304  // else: empty layer
305  });
306  });
307 
308  int i = 0;
309  for (auto& [id, room] : rooms)
310  {
311  viz::Layer& layer = layers.emplace_back(arviz.layer(id.str()));
312  if (enabled)
313  {
314  const auto color = simox::color::GlasbeyLUT::at(i++);
315 
316  drawRoom(layer, room, color);
317  }
318  // else: clear layer
319  }
320  }
321 
322  void
323  Visu::drawRoom(viz::Layer& layer, const rooms::Room& room, const simox::Color& color) const
324  {
325  viz::Polygon polygon(room.name);
326  viz::Path path(room.name + "path");
327 
328  for (const auto& point : room.polygon)
329  {
330  polygon.addPoint(conv::to3D(point));
331  path.addPoint(conv::to3D(point));
332  }
333 
334  // close polygon
335  path.addPoint(conv::to3D(room.polygon.front()));
336 
337  polygon.color(color.with_alpha(50));
338  polygon.lineColor(simox::Color::black());
339  polygon.lineWidth(0);
340 
341  layer.add(polygon);
342 
343  layer.add(path);
344  }
345 
346  void
347  Visu::drawHumans(std::vector<viz::Layer>& layers,
348  const bool enabled,
349  const bool visuTransparent,
350  const Duration maxAge)
351  {
352  if (not enabled)
353  {
354  return;
355  }
356 
357  std::map<std::string, navigation::human::Humans> namedProviderHumans;
358  const DateTime timestamp = Clock::Now();
359 
361  [&]()
362  {
363  using namespace armem::server;
364 
366  [&](const wm::Entity& entity)
367  {
368  namedProviderHumans[entity.id().providerSegmentName];
369  entity.getLatestSnapshot().forEachInstance(
370  [&namedProviderHumans, &timestamp, &maxAge](
371  const armarx::armem::wm::EntityInstance& instance)
372  {
373  const Duration dtToNow =
374  timestamp - instance.metadata().referencedTime;
375 
376  if (dtToNow < maxAge and dtToNow.isPositive())
377  {
378  const auto dto = navigation::human::arondto::Human::FromAron(
379  instance.data());
380 
381  navigation::human::Human human;
382  fromAron(dto, human);
383 
384  namedProviderHumans[instance.id().providerSegmentName]
385  .emplace_back(std::move(human));
386  };
387  });
388  });
389  });
390 
391  for (const auto& [providerName, humans] : namedProviderHumans)
392  {
393  viz::Layer& layer = layers.emplace_back(arviz.layer("humans_" + providerName));
394  visualize(humans, layer, visuTransparent);
395  }
396  }
397 
398 
399 } // namespace armarx::navigation::memory
Visu.h
armarx::navigation::memory::Visu::drawLocations
void drawLocations(std::vector< viz::Layer > &layers, bool enabled)
Definition: Visu.cpp:146
armarx::navigation::core::resolveLocations
void resolveLocations(Graph &graph, const MemoryContainerT &locationContainer)
Definition: Graph.h:140
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:64
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
Path.h
armarx::navigation::core::resolveLocation
void resolveLocation(Graph::Vertex &vertex, const aron::data::DictPtr &locationData)
Definition: Graph.cpp:244
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
framed.h
armarx::navigation::memory::fromAron
void fromAron(const arondto::Circle &dto, Circle &bo)
Definition: aron_conversions.cpp:60
armarx::navigation::memory
Definition: ComponentInterface.ice:37
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:258
aron_conversions.h
armarx::armem::base::detail::GetLatestSnapshotMixin::getLatestSnapshot
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
Definition: lookup_mixins.h:199
armarx::navigation::memory::Visu::graphSegment
const armem::server::wm::CoreSegment & graphSegment
Definition: Visu.h:78
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::navigation::memory::Visu::drawCostmaps
void drawCostmaps(std::vector< viz::Layer > &layers, bool enabled, float zOffset)
Definition: Visu.cpp:243
armarx::rooms
Brief description of class rooms.
Definition: rooms.h:39
armarx::viz::Arrow
Definition: Elements.h:198
armarx::navigation::memory::Visu::arviz
viz::ScopedClient arviz
Definition: Visu.h:75
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::navigation::human::Humans
std::vector< Human > Humans
Definition: types.h:45
armarx::ObjectFinder::setLogObjectDiscoveryError
void setLogObjectDiscoveryError(bool logEnabled)
Definition: ObjectFinder.cpp:377
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::objpose::ObjectPoseClient
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
Definition: ObjectPoseClient.h:18
armarx::armem::base::detail::ForEachEntityMixin::forEachEntity
bool forEachEntity(FunctionT &&func)
Definition: iteration_mixins.h:258
armarx::navigation::memory::Visu::humanSegment
const armem::server::wm::CoreSegment & humanSegment
Definition: Visu.h:80
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:195
armarx::navigation::memory::Visu::drawHumans
void drawHumans(std::vector< viz::Layer > &layers, bool enabled, bool visuTransparent, Duration maxAge)
Definition: Visu.cpp:347
armarx::objpose::ObjectPoseClient::getObjectFinder
const ObjectFinder & getObjectFinder() const
Get the internal object finder.
Definition: ObjectPoseClient.cpp:97
memory_definitions.h
armarx::navigation::memory::Visu::~Visu
~Visu()
Definition: Visu.cpp:66
armarx::navigation::memory::Visu::Visu
Visu(viz::Client arviz, const armem::server::wm::CoreSegment &locSegment, const armem::server::wm::CoreSegment &graphSegment, const armem::server::wm::CoreSegment &costmapSegment, const armem::server::wm::CoreSegment &humanSegment, const armem::server::wm::CoreSegment &roomsSegment)
Definition: Visu.cpp:50
Color
uint32_t Color
RGBA color.
Definition: color.h:8
armarx::objpose::ObjectPoseClient::fetchObjectPosesAsMap
ObjectPoseMap fetchObjectPosesAsMap() const
Fetch all known object poses.
Definition: ObjectPoseClient.cpp:51
armarx::navigation::memory::Visu::locSegment
const armem::server::wm::CoreSegment & locSegment
Definition: Visu.h:77
LocationUtils.h
armarx::navigation::memory::Visu::roomsSegment
const armem::server::wm::CoreSegment & roomsSegment
Definition: Visu.h:81
armarx::navigation::memory::Visu::visu
std::unique_ptr< navigation::graph::GraphVisu > visu
Definition: Visu.h:83
Visu.h
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
aron_conversions.h
armarx::viz::Robot
Definition: Robot.h:9
armarx::Graph
boost::subgraph< CloudGraph > Graph
Definition: Common.h:54
armarx::armem::base::EntityBase::findLatestInstance
auto * findLatestInstance(int instanceIndex=0)
Definition: EntityBase.h:355
armarx::viz::Polygon
Definition: Elements.h:258
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::wm::CoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: memory_definitions.h:112
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::red
QColor red()
Definition: StyleSheets.h:76
armarx::armem::server::wm::CoreSegment
base::CoreSegmentBase
Definition: memory_definitions.h:86
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::navigation::rooms::Room::polygon
std::vector< Eigen::Vector2f > polygon
Definition: types.h:35
Graph.h
armarx::navigation::algorithms::visualize
void visualize(const algorithms::Costmap &costmap, viz::Layer &layer, const std::string &name, const float zOffset)
Definition: visualization.cpp:20
armarx::navigation::memory::Visu::drawRooms
void drawRooms(std::vector< viz::Layer > &layers, bool enabled)
Definition: Visu.cpp:283
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
std
Definition: Application.h:66
armarx::navigation::rooms::Room::name
std::string name
Definition: types.h:33
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::viz::Color::orange
static Color orange(int o=255, int a=255)
2 Red + 1 Green
Definition: Color.h:119
armarx::navigation::conv::to3D
std::vector< Eigen::Vector3f > to3D(const std::vector< Eigen::Vector2f > &v)
Definition: eigen.cpp:11
armarx::navigation::memory::Visu::drawGraphs
void drawGraphs(std::vector< viz::Layer > &layers, bool enabled)
Definition: Visu.cpp:176
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::viz::Layer::size
std::size_t size() const noexcept
Definition: Layer.h:48
armarx::viz::ScopedClient::layer
Layer layer(std::string const &name) const
Definition: ScopedClient.cpp:34
armarx::navigation::memory::Visu::costmapSegment
const armem::server::wm::CoreSegment & costmapSegment
Definition: Visu.h:79
eigen.h
armarx::viz::Path
Definition: Path.h:31
armarx::armem::base::EntityInstanceBase::metadata
MetadataT & metadata()
Definition: EntityInstanceBase.h:117
armarx::viz::Client
Definition: Client.h:109
armarx::viz::Layer
Definition: Layer.h:12
armarx::navigation::rooms::Room
Definition: types.h:31
visualization.h
aron_conversions.h
ObjectFinder.h
armarx::objpose::ObjectPoseMap
std::map< ObjectID, ObjectPose > ObjectPoseMap
Definition: forward_declarations.h:21