30 #include <opencv2/core/eigen.hpp>
31 #include <opencv2/core/mat.hpp>
32 #include <opencv2/core/types.hpp>
33 #include <opencv2/imgproc.hpp>
35 #include <VirtualRobot/BoundingBox.h>
36 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
37 #include <VirtualRobot/Random.h>
38 #include <VirtualRobot/Robot.h>
39 #include <VirtualRobot/SceneObjectSet.h>
40 #include <VirtualRobot/VirtualRobot.h>
41 #include <VirtualRobot/Workspace/WorkspaceGrid.h>
54 #include <range/v3/algorithm/for_each.hpp>
55 #include <range/v3/view/zip.hpp>
65 const std::vector<Room>&
rooms,
67 const bool restrictToRooms)
77 for (
const auto& room :
rooms)
79 for (
const auto& point : room.polygon)
96 ARMARX_CHECK((not obstacles->getCollisionModels().empty()) or
99 const auto expandBounds = [&bounds](
const VirtualRobot::BoundingBox&& bb)
108 for (
const auto& colModel : obstacles->getCollisionModels())
110 expandBounds(colModel->getBoundingBox());
113 ARMARX_VERBOSE <<
"non-articulated objects: scene bounds: " << bounds.
min <<
" and "
119 if (articulatedObject->getCollisionModels().empty())
121 ARMARX_WARNING <<
"The articulated object `" << articulatedObject->getType()
122 <<
"/" << articulatedObject->getName()
123 <<
"` does not provide any collision model!";
126 ARMARX_VERBOSE <<
"Articulated object `" << articulatedObject->getType() <<
"/"
127 << articulatedObject->getName();
129 for (
const auto& colModel : articulatedObject->getCollisionModels())
131 expandBounds(colModel->getBoundingBox());
133 <<
VAROUT(colModel->getBoundingBox().getMax());
143 bounds.
min.x() -= margin;
144 bounds.
min.y() -= margin;
145 bounds.
max.x() += margin;
146 bounds.
max.y() += margin;
154 return {.
min = Eigen::Vector2f{extends.minX, extends.minY},
155 .max = Eigen::Vector2f{extends.maxX, extends.maxY}};
159 merge(
const std::vector<SceneBounds>& sceneBounds)
163 const auto expandBounds = [&bounds](
const SceneBounds& sb)
174 ranges::for_each(sceneBounds, expandBounds);
180 toCostmap(
const VirtualRobot::WorkspaceGrid& workspaceGrid)
182 const Costmap::Grid grid(workspaceGrid.getCells().x, workspaceGrid.getCells().y);
186 .cellSize = workspaceGrid.getDiscretizeSize()};
188 return {grid, parameters, sceneBounds};
269 const auto assertSameSize = [&costmaps](
const Costmap& costmap)
275 ranges::for_each(costmaps, assertSameSize);
288 const std::vector<float> weights(costmaps.size(), 1.F / costmaps.size());
293 costmaps.front().params());
296 grid, costmaps.front().params(), costmaps.front().getLocalSceneBounds());
305 ARMARX_CHECK(
false) <<
"This case should already be handled.";
318 ranges::for_each(costmaps,
319 [&mergedCostmap, &addMode](
const auto& costmap)
320 { mergedCostmap.
add(costmap, addMode); });
322 return mergedCostmap;
326 mergeAligned(
const std::vector<Costmap>& costmaps,
const std::vector<float>& weights)
334 costmaps.front().params());
337 grid, costmaps.front().params(), costmaps.front().getLocalSceneBounds());
340 ranges::for_each(ranges::views::zip(costmaps, weights),
341 [&mergedCostmap](
const auto& p)
343 const auto& [costmap, weight] = p;
344 mergedCostmap.
add(costmap, weight);
347 return mergedCostmap;
360 cv::resize(src, dst, cv::Size{0, 0}, scale, scale);
362 Eigen::MatrixXf scaledGrid;
366 std::optional<Costmap::Mask> scaledMask;
367 if (costmap.
getMask().has_value())
374 constexpr
int someInt = 100;
377 someInt * costmap.
getMask().value().cast<std::uint8_t>();
383 cv::resize(maskMat, maskScaled, cv::Size{0, 0}, scale, scale);
389 Eigen::MatrixXi scaledMaskInt;
398 << costmap.
getGrid().cols() <<
")";
399 ARMARX_VERBOSE <<
"Resized to (" << scaledGrid.rows() <<
", " << scaledGrid.cols() <<
")";
407 std::optional<core::Pose2D>
410 const auto sizeX = costmap.
getGrid().cols();
411 const auto sizeY = costmap.
getGrid().rows();
413 constexpr std::size_t maxIterations = 1000;
417 for (std::size_t iteration = 0; iteration < maxIterations; iteration++)
419 const float iX = VirtualRobot::RandomFloat() *
static_cast<float>(sizeX);
420 const float iY = VirtualRobot::RandomFloat() *
static_cast<float>(sizeY);
441 const std::size_t c_x = costmap.
getGrid().rows();
442 const std::size_t c_y = costmap.
getGrid().cols();
444 const auto isInsideRoom = [&
rooms](
const Eigen::Vector2f& pos) ->
bool
446 return std::any_of(
rooms.begin(),
448 [&pos](
const Room& room) ->
bool { return room.isInside(pos); });
455 for (
unsigned int x = 0; x < c_x; x++)
457 for (
unsigned int y = 0; y < c_y; y++)