CollisionModelHelper.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 RobotAPI
17  * @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18  * @date 17.02.23
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "CollisionModelHelper.h"
24 
25 #include <VirtualRobot/ManipulationObject.h>
26 #include <VirtualRobot/SceneObjectSet.h>
27 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
28 #include <VirtualRobot/Visualization/TriMeshModel.h>
29 
31 
33 
34 
36 {
37 
38  VirtualRobot::ManipulationObjectPtr
40  {
41  const ObjectFinder finder;
42 
43  const VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
44  if (auto obstacle = finder.loadManipulationObject(objectPose))
45  {
46  obstacle->setGlobalPose(objectPose.objectPoseGlobal);
47  return obstacle;
48  }
49 
50  ARMARX_WARNING << "Failed to load scene object `" << objectPose.objectID << "`";
51  return nullptr;
52  }
53 
54  VirtualRobot::SceneObjectSetPtr
56  {
57  const ObjectFinder finder;
58 
59  VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
60  for (const auto& objectPose : objectPoses)
61  {
62  if (auto obstacle = finder.loadManipulationObject(objectPose))
63  {
64  obstacle->setGlobalPose(objectPose.objectPoseGlobal);
65  obstacle->setName(objectPose.objectID.instanceName());
66  sceneObjects->addSceneObject(obstacle);
67  }
68  }
69 
70  return sceneObjects;
71  }
72 
73  VirtualRobot::SceneObjectSetPtr
75  const OccupancyGridHelper::Params& params)
76  {
77  const OccupancyGridHelper ocHelper(occupancyGrid, params);
78  const auto obstacles = ocHelper.obstacles();
79 
80  const float boxSize = occupancyGrid.resolution;
81  const float resolution = occupancyGrid.resolution;
82 
83  VirtualRobot::SceneObjectSetPtr sceneObjects(new VirtualRobot::SceneObjectSet);
84 
85  ARMARX_CHECK_EQUAL(occupancyGrid.frame, GlobalFrame)
86  << "Only occupancy grid in global frame supported.";
87 
88  VirtualRobot::CoinVisualizationFactory factory;
89 
90  const auto& world_T_map = occupancyGrid.pose;
91 
92  for (int x = 0; x < obstacles.rows(); x++)
93  {
94  for (int y = 0; y < obstacles.cols(); y++)
95  {
96  if (obstacles(x, y))
97  {
98  const Eigen::Vector3f pos{
99  static_cast<float>(x) * resolution, static_cast<float>(y) * resolution, 0};
100 
101  // FIXME: change to Isometry3f
102  Eigen::Affine3f map_T_obj = Eigen::Affine3f::Identity();
103  map_T_obj.translation() = pos;
104 
105  Eigen::Affine3f world_T_obj = world_T_map * map_T_obj;
106 
107  // ARMARX_INFO << world_T_obj.translation();
108 
109  auto cube = factory.createBox(boxSize, boxSize, boxSize);
110 
111  const VirtualRobot::CollisionModelPtr collisionModel(
112  new VirtualRobot::CollisionModel(cube));
113 
114  const VirtualRobot::SceneObjectPtr sceneObject(new VirtualRobot::SceneObject(
115  "box_" + std::to_string(sceneObjects->getSize()), cube, collisionModel));
116  sceneObject->setGlobalPose(world_T_obj.matrix());
117 
118  sceneObjects->addSceneObject(sceneObject);
119  }
120  }
121  }
122 
123  return sceneObjects;
124  }
125 
127  objectPoseClient_(client)
128  {
129  }
130 
131  VirtualRobot::SceneObjectSetPtr
133  {
134  const objpose::ObjectPoseSeq objectPoses = objectPoseClient_.fetchObjectPoses();
135  return asSceneObjects(objectPoses);
136  }
137 
140  {
141  const objpose::ObjectPoseSeq objectPoses = objectPoseClient_.fetchObjectPoses();
142  return asManipulationObjects(objectPoses);
143  }
144 
147  {
149 
150  for (const auto& pose : objectPoses)
151  {
152  set.emplace_back(*asManipulationObject(pose));
153  }
154 
155  return std::make_shared<ManipulationObjectSet>(set);
156  }
157 
158  void
159  CollisionModelHelper::visualizeCollisionModel(const VirtualRobot::CollisionModelPtr& model,
160  viz::Client& arviz)
161  {
162  armarx::viz::Mesh mesh(model->getName());
163  auto faces = model->getTriMeshModel()->faces;
164  std::vector<armarx::viz::data::Face> viz_faces;
166  faces.begin(),
167  faces.end(),
168  std::back_inserter(viz_faces),
169  [](const auto& face)
170  {
171  return armarx::viz::data::Face(
172  face.id1, face.id2, face.id3, face.idColor1, face.idColor2, face.idColor3);
173  });
174  mesh.vertices(model->getTriMeshModel()->vertices)
175  .faces(viz_faces)
176  .pose(model->getGlobalPose());
177  arviz.commitLayerContaining("CollisionModel", mesh);
178  }
179 
180 } // namespace armarx::obstacle_avoidance
armarx::obstacle_avoidance::CollisionModelHelper::visualizeCollisionModel
static void visualizeCollisionModel(const VirtualRobot::CollisionModelPtr &model, viz::Client &arviz)
Definition: CollisionModelHelper.cpp:159
armarx::objpose::ObjectPoseClient::fetchObjectPoses
ObjectPoseSeq fetchObjectPoses() const
Fetch all known object poses.
Definition: ObjectPoseClient.cpp:39
armarx::obstacle_avoidance::CollisionModelHelper::ManipulationObjectSetPtr
std::shared_ptr< ManipulationObjectSet > ManipulationObjectSetPtr
Definition: CollisionModelHelper.h:40
armarx::armem::vision::OccupancyGrid
Definition: types.h:36
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::obstacle_avoidance
Definition: CollisionModelHelper.cpp:35
armarx::obstacle_avoidance::CollisionModelHelper::asSceneObjects
static VirtualRobot::SceneObjectSetPtr asSceneObjects(const objpose::ObjectPoseSeq &objectPoses)
Definition: CollisionModelHelper.cpp:55
CollisionModelHelper.h
armarx::obstacle_avoidance::CollisionModelHelper::asManipulationObject
static VirtualRobot::ManipulationObjectPtr asManipulationObject(const objpose::ObjectPose &objectPose)
Definition: CollisionModelHelper.cpp:39
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::viz::Mesh
Definition: Mesh.h:28
armarx::obstacle_avoidance::CollisionModelHelper::fetchSceneObjects
VirtualRobot::SceneObjectSetPtr fetchSceneObjects()
Definition: CollisionModelHelper.cpp:132
armarx::obstacle_avoidance::CollisionModelHelper::CollisionModelHelper
CollisionModelHelper(const objpose::ObjectPoseClient &client)
Definition: CollisionModelHelper.cpp:126
armarx::OccupancyGridHelper
Definition: OccupancyGridHelper.h:23
armarx::obstacle_avoidance::CollisionModelHelper::fetchManipulationObjects
ManipulationObjectSetPtr fetchManipulationObjects()
Definition: CollisionModelHelper.cpp:139
armarx::ObjectFinder::loadManipulationObject
static VirtualRobot::ManipulationObjectPtr loadManipulationObject(const std::optional< ObjectInfo > &ts)
Definition: ObjectFinder.cpp:300
armarx::objpose::ObjectPoseClient
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
Definition: ObjectPoseClient.h:18
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::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
armarx::viz::Mesh::faces
Mesh & faces(const data::Face *fs, std::size_t size)
Definition: Mesh.h:73
armarx::obstacle_avoidance::CollisionModelHelper::ManipulationObjectSet
std::vector< VirtualRobot::ManipulationObject > ManipulationObjectSet
Definition: CollisionModelHelper.h:39
armarx::detail::OccupancyGridHelperParams
Definition: OccupancyGridHelper.h:16
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::obstacle_avoidance::CollisionModelHelper::asManipulationObjects
static ManipulationObjectSetPtr asManipulationObjects(const objpose::ObjectPoseSeq &objectPoses)
Definition: CollisionModelHelper.cpp:146
armarx::armem::vision::OccupancyGrid::resolution
float resolution
Definition: types.h:38
ExpressionException.h
Mesh.h
armarx::objpose::ObjectPose::objectID
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
Definition: ObjectPose.h:58
armarx::viz::Mesh::vertices
Mesh & vertices(const Eigen::Vector3f *vs, std::size_t size)
Definition: Mesh.h:33
armarx::objpose::ObjectPose::objectPoseGlobal
Eigen::Matrix4f objectPoseGlobal
The object pose in the global frame.
Definition: ObjectPose.h:73
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:159
set
set(LIBS ArmarXCoreInterfaces ${CMAKE_THREAD_LIBS_INIT} ${dl_LIBRARIES} ${rt_LIBRARIES} ${QT_LIBRARIES} ${Boost_LIBRARIES} BoostAssertionHandler ArmarXCPPUtility SimoxUtility) set(LIB_FILES ArmarXManager.cpp ArmarXMultipleObjectsScheduler.cpp ArmarXObjectScheduler.cpp ManagedIceObject.cpp ManagedIceObjectPlugin.cpp Component.cpp ComponentPlugin.cpp IceGridAdmin.cpp ArmarXObjectObserver.cpp IceManager.cpp PackagePath.cpp RemoteReferenceCount.cpp logging/LoggingUtil.cpp logging/Logging.cpp logging/LogSender.cpp logging/ArmarXLogBuf.cpp system/ArmarXDataPath.cpp system/DynamicLibrary.cpp system/ProcessWatcher.cpp system/FactoryCollectionBase.cpp system/cmake/CMakePackageFinder.cpp system/cmake/CMakePackageFinderCache.cpp system/cmake/ArmarXPackageToolInterface.cpp system/RemoteObjectNode.cpp services/sharedmemory/HardwareId.cpp services/tasks/RunningTask.cpp services/tasks/ThreadList.cpp services/tasks/ThreadPool.cpp services/profiler/Profiler.cpp services/profiler/FileLoggingStrategy.cpp services/profiler/IceLoggingStrategy.cpp application/Application.cpp application/ApplicationOptions.cpp application/ApplicationProcessFacet.cpp application/ApplicationNetworkStats.cpp application/properties/PropertyUser.cpp application/properties/Property.cpp application/properties/PropertyDefinition.cpp application/properties/PropertyDefinitionContainer.cpp application/properties/PropertyDefinitionHelpFormatter.cpp application/properties/PropertyDefinitionConfigFormatter.cpp application/properties/PropertyDefinitionBriefHelpFormatter.cpp application/properties/PropertyDefinitionXmlFormatter.cpp application/properties/PropertyDefinitionDoxygenFormatter.cpp application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.cpp application/properties/PropertyDefinitionContainerBriefHelpFormatter.cpp application/properties/IceProperties.cpp exceptions/Exception.cpp exceptions/local/UnexpectedEnumValueException.cpp util/FileSystemPathBuilder.cpp util/StringHelpers.cpp util/IceReportSkipper.cpp util/Throttler.cpp util/distributed/AMDCallbackCollection.cpp util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.cpp util/distributed/RemoteHandle/RemoteHandle.cpp util/distributed/RemoteHandle/RemoteHandleControlBlock.cpp time/ice_conversions.cpp time/json_conversions.cpp time/CallbackWaitLock.cpp time/Clock.cpp time/ClockType.cpp time/ClockTypeNames.cpp time/CycleUtil.cpp time/DateTime.cpp time/Duration.cpp time/Frequency.cpp time/LocalTimeServer.cpp time/Metronome.cpp time/ScopedStopWatch.cpp time/StopWatch.cpp time/Timer.cpp time/TimeKeeper.cpp time/TimeUtil.cpp csv/CsvWriter.cpp csv/CsvReader.cpp eigen/conversions.cpp eigen/ice_conversions.cpp) set(LIB_HEADERS ArmarXManager.h ArmarXDummyManager.h ArmarXMultipleObjectsScheduler.h ArmarXObjectObserver.h ArmarXObjectScheduler.h ArmarXFwd.h Component.h ComponentPlugin.h ComponentFactories.h CoreObjectFactories.h IceGridAdmin.h IceManager.h IceManagerImpl.h json_conversions.h ManagedIceObject.h ManagedIceObjectPlugin.h ManagedIceObjectImpl.h ManagedIceObjectDependency.h ManagedIceObjectRegistryInterface.h PackagePath.h RemoteReferenceCount.h system/ImportExport.h system/ImportExportComponent.h system/AbstractFactoryMethod.h system/FactoryCollectionBase.h system/Synchronization.h system/ArmarXDataPath.h system/DynamicLibrary.h system/ProcessWatcher.h system/ConditionSynchronization.h system/cmake/CMakePackageFinder.h system/cmake/CMakePackageFinderCache.h system/cmake/FindPackageX.cmake system/cmake/ArmarXPackageToolInterface.h system/RemoteObjectNode.h logging/LoggingUtil.h logging/LogSender.h logging/Logging.h logging/ArmarXLogBuf.h logging/SpamFilterData.h services/tasks/RunningTask.h services/tasks/PeriodicTask.h services/tasks/ThreadList.h services/tasks/TaskUtil.h services/tasks/ThreadPool.h services/sharedmemory/SharedMemoryProvider.h services/sharedmemory/SharedMemoryConsumer.h services/sharedmemory/IceSharedMemoryProvider.h services/sharedmemory/IceSharedMemoryConsumer.h services/sharedmemory/HardwareIdentifierProvider.h services/sharedmemory/HardwareId.h services/sharedmemory/exceptions/SharedMemoryExceptions.h services/profiler/Profiler.h services/profiler/LoggingStrategy.h services/profiler/FileLoggingStrategy.h services/profiler/IceLoggingStrategy.h application/Application.h application/ApplicationOptions.h application/ApplicationProcessFacet.h application/ApplicationNetworkStats.h application/properties/forward_declarations.h application/properties/Properties.h application/properties/Property.h application/properties/PluginEigen.h application/properties/PluginEnumNames.h application/properties/PluginCfgStruct.h application/properties/PluginAll.h application/properties/PropertyUser.h application/properties/PropertyDefinition.h application/properties/PropertyDefinition.hpp application/properties/PropertyDefinitionInterface.h application/properties/PropertyDefinitionContainer.h application/properties/PropertyDefinitionFormatter.h application/properties/PropertyDefinitionContainerFormatter.h application/properties/PropertyDefinitionConfigFormatter.h application/properties/PropertyDefinitionHelpFormatter.h application/properties/PropertyDefinitionBriefHelpFormatter.h application/properties/PropertyDefinitionXmlFormatter.h application/properties/PropertyDefinitionDoxygenFormatter.h application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.h application/properties/PropertyDefinitionContainerBriefHelpFormatter.h application/properties/ProxyPropertyDefinition.h application/properties/IceProperties.h exceptions/Exception.h exceptions/LocalException.h exceptions/local/DynamicLibraryException.h exceptions/local/ExpressionException.h exceptions/local/FileIOException.h exceptions/local/InvalidPropertyValueException.h exceptions/local/MissingRequiredPropertyException.h exceptions/local/PropertyInheritanceCycleException.h exceptions/local/ProxyNotInitializedException.h exceptions/local/UnexpectedEnumValueException.h exceptions/local/UnmappedValueException.h exceptions/local/ValueRangeExceededException.h exceptions/user/NotImplementedYetException.h rapidxml/rapidxml.hpp rapidxml/rapidxml_print.hpp rapidxml/rapidxml_iterators.hpp rapidxml/rapidxml_utils.hpp rapidxml/wrapper/RapidXmlReader.h rapidxml/wrapper/RapidXmlWriter.h rapidxml/wrapper/DefaultRapidXmlReader.h rapidxml/wrapper/MultiNodeRapidXMLReader.h util/IceBlobToObject.h util/ObjectToIceBlob.h util/FileSystemPathBuilder.h util/FiniteStateMachine.h util/StringHelpers.h util/StringHelperTemplates.h util/algorithm.h util/OnScopeExit.h util/Predicates.h util/Preprocessor.h util/PropagateConst.h util/Registrar.h util/TemplateMetaProgramming.h util/TripleBuffer.h util/IceReportSkipper.h util/Throttler.h util/distributed/AMDCallbackCollection.h util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.h util/distributed/RemoteHandle/RemoteHandle.h util/distributed/RemoteHandle/RemoteHandleControlBlock.h util/SimpleStatemachine.h time.h time_minimal.h time/forward_declarations.h time/ice_conversions.h time/json_conversions.h time/CallbackWaitLock.h time/Clock.h time/ClockType.h time/ClockTypeNames.h time/CycleUtil.h time/DateTime.h time/Duration.h time/Frequency.h time/LocalTimeServer.h time/Metronome.h time/ScopedStopWatch.h time/StopWatch.h time/Timer.h time/TimeUtil.h time/TimeKeeper.h csv/CsvWriter.h csv/CsvReader.h eigen/conversions.h eigen/ice_conversions.h ice_conversions.h ice_conversions/ice_conversions_boost_templates.h ice_conversions/ice_conversions_templates.h ice_conversions/ice_conversions_templates.tpp $
Definition: CMakeLists.txt:12
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
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::viz::Client
Definition: Client.h:109
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
armarx::viz::Client::commitLayerContaining
void commitLayerContaining(std::string const &name)
Definition: Client.h:153