RoomNavigationTargetCreator.cpp
Go to the documentation of this file.
2 
3 #include <SimoxUtility/color/Color.h>
4 
6 
9 
14 #include <range/v3/range/conversion.hpp>
15 #include <range/v3/view/reverse.hpp>
16 
18 {
20  {
21  }
22 
25  const algorithms::Costmap& obstacleDistancesCostmap,
26  const Room& room,
27  const core::Position& global_P_robot,
28  viz::Client* arviz) const
29  {
30  using ShortestPathFasterAlgorithm = algorithms::spfa::ShortestPathFasterAlgorithm;
31 
32  const ShortestPathFasterAlgorithm spfa(obstacleDistancesCostmap,
33  ShortestPathFasterAlgorithm::Parameters());
34 
35  ShortestPathFasterAlgorithm::Result result = spfa.spfa(conv::to2D(global_P_robot));
36 
37  const armarx::navigation::algorithms::Costmap navigationPlanningCostmap(
38  result.distances,
39  obstacleDistancesCostmap.params(),
40  obstacleDistancesCostmap.getLocalSceneBounds(),
41  result.reachable);
42 
43 
44  armarx::navigation::algorithms::Costmap roomCostmap = navigationPlanningCostmap;
45 
46  const std::size_t cX = roomCostmap.getGrid().rows();
47  const std::size_t cY = roomCostmap.getGrid().cols();
48 
49  // invalidate everything that is in collision
50  // roomCostmap.getMutableMask().value() = roomCostmap.getGrid().array() >= 0.5;
51 
52 #pragma omp parallel for schedule(static)
53  for (unsigned int x = 0; x < cX; x++)
54  {
55  for (unsigned int y = 0; y < cY; y++)
56  {
58  const algorithms::Costmap::Position position = roomCostmap.toPositionGlobal(index);
59 
60  auto pos3d = conv::to3D(position);
61  pos3d.z() = room.zFloor + room.height / 2; // just at the center (height)
62 
63  // invalid outside of the room
64  const bool isInside = room.isInside(pos3d);
65 
66  roomCostmap.getMutableMask().value()(x, y) &= isInside;
67  roomCostmap.getMutableGrid()(x, y) *= static_cast<float>(isInside);
68  }
69  }
70 
71  ARMARX_INFO << "Closest position to room: " << roomCostmap.optimum().position;
72 
73  const core::Position closestReachablePositionOnRoomBoundary =
74  conv::to3D(roomCostmap.optimum().position);
75 
76 
77  // We don't stop here. We want to place the robot in front of the room at a
78  const auto plan = spfa.plan(conv::to2D(global_P_robot),
79  conv::to2D(closestReachablePositionOnRoomBoundary));
80 
81  if (arviz != nullptr)
82  {
83  auto layer = arviz->layer("RoomNavigationTargetCreator");
84  algorithms::visualize(roomCostmap, layer, "room_costmap", 3); // FIXME magic number
85 
86  layer.add(viz::Sphere("closestReachablePositionOnRoomBoundary")
87  .position(closestReachablePositionOnRoomBoundary)
88  .radius(50)
89  .color(simox::Color::blue()));
90 
91  arviz->commit(layer);
92  }
93 
94 
95  ARMARX_CHECK(plan.success);
96 
97  const auto posInFrontOfRoom = [&]() -> core::Position
98  {
99  namespace r = ::ranges;
100  namespace rv = ::ranges::views;
101 
102  const auto reversedPlan = rv::reverse(plan.path) | r::to_vector;
103 
104  const auto goalPos = conv::to2D(closestReachablePositionOnRoomBoundary);
105 
106  for (const auto& pos : reversedPlan)
107  {
108  if ((pos - goalPos).norm() > params.distanceToRoomEntry)
109  {
110  return conv::to3D(pos);
111  }
112  }
113 
114  ARMARX_INFO << "There is something wrong with the path!";
115  return conv::to3D(reversedPlan.back()); // the current robot pos
116  }();
117 
118  if (arviz != nullptr)
119  {
120  auto layer = arviz->layer("RoomNavigationTargetCreator_target");
121 
122  layer.add(viz::Sphere("posInFrontOfRoom")
123  .position(posInFrontOfRoom)
124  .radius(50)
126 
127  arviz->commit(layer);
128  }
129 
130  return {.global_P_roomEntry = closestReachablePositionOnRoomBoundary,
131  .global_P_robot = posInFrontOfRoom};
132  }
133 } // namespace armarx::navigation::rooms
Client.h
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::navigation::algorithms::Costmap::Index
Eigen::Array2i Index
Definition: Costmap.h:46
armarx::navigation::algorithms::Costmap::getMutableMask
std::optional< Costmap::Mask > & getMutableMask() noexcept
Definition: Costmap.cpp:398
index
uint8_t index
Definition: EtherCATFrame.h:59
basic_types.h
RoomNavigationTargetCreator.h
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::navigation::algorithms::Costmap::toPositionGlobal
Position toPositionGlobal(const Index &index) const
Definition: Costmap.cpp:71
armarx::viz::Sphere
Definition: Elements.h:134
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Elements.h
armarx::navigation::algorithms::Costmap::getLocalSceneBounds
const SceneBounds & getLocalSceneBounds() const noexcept
Definition: Costmap.cpp:126
ShortestPathFasterAlgorithm.h
armarx::viz::ElementOps::position
DerivedT & position(float x, float y, float z)
Definition: ElementOps.h:127
armarx::reverse
T reverse(const T &o)
Definition: container.h:32
armarx::navigation::rooms::RoomNavigationTargetCreator::Params::distanceToRoomEntry
float distanceToRoomEntry
Definition: RoomNavigationTargetCreator.h:41
armarx::navigation::rooms::RoomNavigationTargetCreator::getClosestPositionOutsideOfRoom
Result getClosestPositionOutsideOfRoom(const algorithms::Costmap &obstacleDistancesCostmap, const Room &room, const core::Position &global_P_robot, viz::Client *arviz=nullptr) const
Definition: RoomNavigationTargetCreator.cpp:24
armarx::navigation::rooms::RoomNavigationTargetCreator::Params
Definition: RoomNavigationTargetCreator.h:39
armarx::navigation::algorithms::Costmap::params
const Parameters & params() const noexcept
Definition: Costmap.cpp:230
armarx::navigation::rooms::Room::height
float height
Definition: types.h:36
armarx::viz::Sphere::radius
Sphere & radius(float r)
Definition: Elements.h:138
armarx::navigation::rooms::RoomNavigationTargetCreator::Result
Definition: RoomNavigationTargetCreator.h:45
armarx::navigation::rooms
This file is part of ArmarX.
Definition: aron_conversions.cpp:5
armarx::navigation::algorithms::Costmap::getGrid
const Grid & getGrid() const
Definition: Costmap.cpp:236
armarx::navigation::algorithms::Costmap::Position
Eigen::Vector2f Position
Definition: Costmap.h:47
armarx::navigation::conv::to2D
std::vector< Eigen::Vector2f > to2D(const std::vector< Eigen::Vector3f > &v)
Definition: eigen.cpp:26
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_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::viz::ElementOps::color
DerivedT & color(Color color)
Definition: ElementOps.h:195
armarx::navigation::conv::to3D
std::vector< Eigen::Vector3f > to3D(const std::vector< Eigen::Vector2f > &v)
Definition: eigen.cpp:11
armarx::navigation::algorithms::Costmap::optimum
Optimum optimum() const
Definition: Costmap.cpp:132
armarx::navigation::rooms::RoomNavigationTargetCreator::RoomNavigationTargetCreator
RoomNavigationTargetCreator(const Params &params)
Definition: RoomNavigationTargetCreator.cpp:19
eigen.h
Logging.h
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
armarx::navigation::algorithms::spfa::ShortestPathFasterAlgorithm
Definition: ShortestPathFasterAlgorithm.h:33
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::viz::Client
Definition: Client.h:109
armarx::navigation::rooms::Room
Definition: types.h:31
armarx::navigation::rooms::Room::isInside
bool isInside(const Eigen::Vector3f &point) const
Definition: types.cpp:14
visualization.h
armarx::navigation::rooms::Room::zFloor
float zFloor
Definition: types.h:38
armarx::navigation::algorithms::Costmap::getMutableGrid
Grid & getMutableGrid()
Definition: Costmap.h:71
armarx::navigation::algorithms::Costmap
Definition: Costmap.h:13
armarx::navigation::algorithms::Costmap::Optimum::position
Position position
Definition: Costmap.h:99
armarx::green
QColor green()
Definition: StyleSheets.h:72
norm
double norm(const Point &a)
Definition: point.hpp:94