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