util.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::util
17  * @author Fabian Reister ( fabian dot reister at kit dot edu )
18  * @author Christian R. G. Dreher ( c dot dreher at kit dot edu )
19  * @date 2021
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "util.h"
25 
26 #include <algorithm>
27 #include <string>
28 
29 #include <Eigen/Geometry>
30 
31 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
32 #include <VirtualRobot/ManipulationObject.h>
33 #include <VirtualRobot/Primitive.h>
34 #include <VirtualRobot/SceneObjectSet.h>
35 #include <VirtualRobot/VirtualRobot.h>
36 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
37 #include <VirtualRobot/Visualization/VisualizationNode.h>
38 
40 
45 
47 {
48 
51  const std::vector<std::string>& datasetDisableList)
52  {
53  const auto isInDisableList = [&datasetDisableList](const objpose::ObjectPose& objectPose)
54  {
55  const auto dataset = objectPose.objectID.dataset();
56 
57  return std::find(datasetDisableList.begin(), datasetDisableList.end(), dataset) !=
58  datasetDisableList.end();
59  };
60 
61  objects.erase(std::remove_if(objects.begin(), objects.end(), isInDisableList),
62  objects.end());
63  return objects;
64  }
65 
68  {
69  const auto isDynamic = [](const objpose::ObjectPose& objectPose)
70  { return not objectPose.isStatic; };
71 
72  objects.erase(std::remove_if(objects.begin(), objects.end(), isDynamic), objects.end());
73  return objects;
74  }
75 
78  {
79  ObjectFinder finder;
80  finder.setLogObjectDiscoveryError(false);
81 
82  const auto isArticulated = [&finder](const objpose::ObjectPose& objectPose)
83  {
84  const auto objInfo = finder.findObject(objectPose.objectID);
85  ARMARX_CHECK_NOT_NULL(objInfo) << objectPose.objectID;
86  const bool hasArticulatedModel = objInfo->getArticulatedModel().has_value();
87  return hasArticulatedModel;
88  };
89 
90  objects.erase(std::remove_if(objects.begin(), objects.end(), isArticulated), objects.end());
91  return objects;
92  }
93 
96  {
97  ObjectFinder finder;
98  finder.setLogObjectDiscoveryError(false);
99 
100  const auto isNonArticulated = [&finder](const objpose::ObjectPose& objectPose)
101  {
102  const auto objInfo = finder.findObject(objectPose.objectID);
103  ARMARX_CHECK_NOT_NULL(objInfo) << objectPose.objectID;
104  const bool hasArticulatedModel = objInfo->getArticulatedModel().has_value();
105  return not hasArticulatedModel;
106  };
107 
108  objects.erase(std::remove_if(objects.begin(), objects.end(), isNonArticulated),
109  objects.end());
110  return objects;
111  }
112 
113  std::optional<objpose::ObjectPose>
114  findObject(const objpose::ObjectPoseSeq& objectPoses, const armarx::ObjectID& objectID)
115  {
116  const auto matchesId = [&objectID](const objpose::ObjectPose& objectPose) -> bool
117  { return objectPose.objectID == objectID; };
118 
119  const auto it = std::find_if(objectPoses.begin(), objectPoses.end(), matchesId);
120  if (it != objectPoses.end())
121  {
122  return *it;
123  }
124 
125  return std::nullopt;
126  }
127 
128  VirtualRobot::ManipulationObjectPtr
130  {
131  ObjectFinder finder;
132  finder.setLogObjectDiscoveryError(false);
133 
134  VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
135  if (auto obstacle = finder.loadManipulationObject(objectPose))
136  {
137  obstacle->setGlobalPose(objectPose.objectPoseGlobal);
138  return obstacle;
139  }
140 
141  ARMARX_WARNING << "Failed to load scene object `" << objectPose.objectID << "`";
142  return nullptr;
143  }
144 
145  VirtualRobot::SceneObjectSetPtr
147  {
148  ObjectFinder finder;
149  finder.setLogObjectDiscoveryError(false);
150 
151  VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
152  for (const auto& objectPose : objectPoses)
153  {
154  if (auto obstacle = finder.loadManipulationObject(objectPose))
155  {
156  obstacle->setGlobalPose(objectPose.objectPoseGlobal);
157  sceneObjects->addSceneObject(obstacle);
158  }
159  }
160 
161  return sceneObjects;
162  }
163 
166  {
167  ObjectFinder finder;
168  finder.setLogObjectDiscoveryError(false);
169 
170  VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
171  if (auto obstacle = finder.loadManipulationObject(objectPose))
172  {
173  obstacle->setGlobalPose(objectPose.objectPoseGlobal);
174  return obstacle;
175  }
176  return nullptr;
177  }
178 
179  VirtualRobot::SceneObjectSetPtr
181  const OccupancyGridHelper::Params& params)
182  {
183  const OccupancyGridHelper ocHelper(occupancyGrid, params);
184  const auto obstacles = ocHelper.obstacles();
185 
186  const float boxSize = occupancyGrid.resolution;
187  const float resolution = occupancyGrid.resolution;
188 
189  VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
190 
191  ARMARX_CHECK_EQUAL(occupancyGrid.frame, GlobalFrame)
192  << "Only occupancy grid in global frame supported.";
193 
194  VirtualRobot::CoinVisualizationFactory factory;
195 
196  const auto& world_T_map = occupancyGrid.pose;
197 
198  for (int x = 0; x < obstacles.rows(); x++)
199  {
200  for (int y = 0; y < obstacles.cols(); y++)
201  {
202  if (obstacles(x, y))
203  {
204  const Eigen::Vector3f pos{
205  static_cast<float>(x * resolution), static_cast<float>(y * resolution), 0};
206 
207  // FIXME: change to Isometry3f
208  Eigen::Affine3f map_T_obj = Eigen::Affine3f::Identity();
209  map_T_obj.translation() = pos;
210 
211  Eigen::Affine3f world_T_obj = world_T_map * map_T_obj;
212 
213  // ARMARX_INFO << world_T_obj.translation();
214 
215  auto cube = factory.createBox(boxSize, boxSize, boxSize);
216 
217  VirtualRobot::CollisionModelPtr collisionModel(
218  new VirtualRobot::CollisionModel(cube));
219 
220  VirtualRobot::SceneObjectPtr sceneObject(new VirtualRobot::SceneObject(
221  "box_" + std::to_string(sceneObjects->getSize()), cube, collisionModel));
222  sceneObject->setGlobalPose(world_T_obj.matrix());
223 
224  sceneObjects->addSceneObject(sceneObject);
225  }
226  }
227  }
228 
229  return sceneObjects;
230  }
231 } // namespace armarx::navigation::util
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:11
armarx::armem::vision::OccupancyGrid
Definition: types.h:36
armarx::navigation::util
This file is part of ArmarX.
Definition: geometry.cpp:8
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::OccupancyGridHelper
Definition: OccupancyGridHelper.h:23
armarx::ObjectFinder::loadManipulationObject
static VirtualRobot::ManipulationObjectPtr loadManipulationObject(const std::optional< ObjectInfo > &ts)
Definition: ObjectFinder.cpp:300
armarx::ObjectFinder::setLogObjectDiscoveryError
void setLogObjectDiscoveryError(bool logEnabled)
Definition: ObjectFinder.cpp:379
armarx::armem::vision::OccupancyGrid::frame
std::string frame
Definition: types.h:40
armarx::armem::vision::OccupancyGrid::pose
Eigen::Affine3f pose
Definition: types.h:41
armarx::navigation::util::asSceneObject
VirtualRobot::SceneObjectPtr asSceneObject(const objpose::ObjectPose &objectPose)
Definition: util.cpp:165
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
util.h
FramedPose.h
armarx::navigation::util::findObject
std::optional< objpose::ObjectPose > findObject(const objpose::ObjectPoseSeq &objectPoses, const armarx::ObjectID &objectID)
Definition: util.cpp:114
armarx::navigation::util::nonArticulatedObjects
objpose::ObjectPoseSeq nonArticulatedObjects(objpose::ObjectPoseSeq objects)
Definition: util.cpp:77
armarx::navigation::util::articulatedObjects
objpose::ObjectPoseSeq articulatedObjects(objpose::ObjectPoseSeq objects)
Definition: util.cpp:95
armarx::detail::OccupancyGridHelperParams
Definition: OccupancyGridHelper.h:16
armarx::navigation::util::asSceneObjects
VirtualRobot::SceneObjectSetPtr asSceneObjects(const objpose::ObjectPoseSeq &objectPoses)
Definition: util.cpp:146
armarx::OccupancyGridHelper::obstacles
BinaryArray obstacles() const
Definition: OccupancyGridHelper.cpp:31
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::navigation::util::asManipulationObject
VirtualRobot::ManipulationObjectPtr asManipulationObject(const objpose::ObjectPose &objectPose)
Definition: util.cpp:129
armarx::armem::vision::OccupancyGrid::resolution
float resolution
Definition: types.h:38
ExpressionException.h
forward_declarations.h
armarx::objpose::ObjectPose::objectID
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
Definition: ObjectPose.h:58
armarx::objpose::ObjectPose::objectPoseGlobal
Eigen::Matrix4f objectPoseGlobal
The object pose in the global frame.
Definition: ObjectPose.h:73
OccupancyGridHelper.h
armarx::navigation::util::staticObjects
objpose::ObjectPoseSeq staticObjects(objpose::ObjectPoseSeq objects)
Definition: util.cpp:67
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:65
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
ObjectFinder.h
armarx::navigation::util::filterObjects
objpose::ObjectPoseSeq filterObjects(objpose::ObjectPoseSeq objects, const std::vector< std::string > &datasetDisableList)
Definition: util.cpp:50