27 #include <AffordanceKit/affordances/PlatformGraspAffordance.h>
28 #include <AffordanceKit/affordances/PrismaticGraspAffordance.h>
29 #include <AffordanceKit/affordances/GraspAffordance.h>
30 #include <AffordanceKit/affordances/SupportAffordance.h>
31 #include <AffordanceKit/affordances/LeanAffordance.h>
32 #include <AffordanceKit/affordances/HoldAffordance.h>
33 #include <AffordanceKit/affordances/LiftAffordance.h>
34 #include <AffordanceKit/affordances/TurnAffordance.h>
35 #include <AffordanceKit/affordances/PushAffordance.h>
36 #include <AffordanceKit/affordances/PullAffordance.h>
37 #include <AffordanceKit/affordances/BimanualPlatformGraspAffordance.h>
38 #include <AffordanceKit/affordances/BimanualPrismaticGraspAffordance.h>
39 #include <AffordanceKit/affordances/BimanualGraspAffordance.h>
40 #include <AffordanceKit/affordances/BimanualOpposedGraspAffordance.h>
41 #include <AffordanceKit/affordances/BimanualAlignedGraspAffordance.h>
42 #include <AffordanceKit/affordances/BimanualOpposedPlatformGraspAffordance.h>
43 #include <AffordanceKit/affordances/BimanualOpposedPrismaticGraspAffordance.h>
44 #include <AffordanceKit/affordances/BimanualAlignedPlatformGraspAffordance.h>
45 #include <AffordanceKit/affordances/BimanualAlignedPrismaticGraspAffordance.h>
46 #include <AffordanceKit/affordances/BimanualTurnAffordance.h>
47 #include <AffordanceKit/affordances/BimanualLiftAffordance.h>
52 #include <RobotAPI/interface/core/PoseBase.h>
64 SceneArmarX::SceneArmarX(
const AffordanceKit::PrimitiveSetPtr& primitives,
const std::vector<AffordanceKit::UnimanualAffordancePtr>& unimanualAffordances,
const std::vector<AffordanceKit::BimanualAffordancePtr>& bimanualAffordances) :
65 AffordanceKit::Scene(primitives, unimanualAffordances, bimanualAffordances)
70 SceneArmarX::SceneArmarX(memoryx::EnvironmentalPrimitiveSegmentBasePrx& primitiveSegment, memoryx::AffordanceSegmentBasePrx& affordanceSegment)
72 auto t = IceUtil::Time::now();
75 memoryx::AffordanceBaseList affordances = affordanceSegment->getAffordances();
77 if (affordances.size() == 0)
84 long timestamp = (*affordances.begin())->getTime()->timestamp;
89 std::cout <<
"Creating a primitive set from memory took: " << (IceUtil::Time::now() - t).toMilliSeconds() <<
" ms" << std::endl;
91 t = IceUtil::Time::now();
93 #pragma omp parallel for
94 for (
unsigned int i = 0; i < affordances.size(); i++)
96 AffordanceKit::UnimanualAffordancePtr uni;
97 AffordanceKit::BimanualAffordancePtr bi;
99 switch (affordances[i]->getType())
101 case memoryx::eAffordanceTypeGraspPlatform:
102 uni.reset(
new AffordanceKit::PlatformGraspAffordance());
105 case memoryx::eAffordanceTypeGraspPrismatic:
106 uni.reset(
new AffordanceKit::PrismaticGraspAffordance());
109 case memoryx::eAffordanceTypeGrasp:
110 uni.reset(
new AffordanceKit::GraspAffordance());
113 case memoryx::eAffordanceTypeSupport:
114 uni.reset(
new AffordanceKit::SupportAffordance());
117 case memoryx::eAffordanceTypeLean:
118 uni.reset(
new AffordanceKit::LeanAffordance());
121 case memoryx::eAffordanceTypeHold:
122 uni.reset(
new AffordanceKit::HoldAffordance());
125 case memoryx::eAffordanceTypeLift:
126 uni.reset(
new AffordanceKit::LiftAffordance());
129 case memoryx::eAffordanceTypeTurn:
130 uni.reset(
new AffordanceKit::TurnAffordance());
133 case memoryx::eAffordanceTypePush:
134 uni.reset(
new AffordanceKit::PushAffordance());
137 case memoryx::eAffordanceTypePull:
138 uni.reset(
new AffordanceKit::PullAffordance());
141 case memoryx::eAffordanceTypeBimanualGraspPlatform:
142 bi.reset(
new AffordanceKit::BimanualPlatformGraspAffordance());
145 case memoryx::eAffordanceTypeBimanualGraspPrismatic:
146 bi.reset(
new AffordanceKit::BimanualPrismaticGraspAffordance());
149 case memoryx::eAffordanceTypeBimanualGrasp:
150 bi.reset(
new AffordanceKit::BimanualGraspAffordance());
153 case memoryx::eAffordanceTypeBimanualGraspOpposed:
154 bi.reset(
new AffordanceKit::BimanualOpposedGraspAffordance());
157 case memoryx::eAffordanceTypeBimanualGraspAligned:
158 bi.reset(
new AffordanceKit::BimanualAlignedGraspAffordance());
161 case memoryx::eAffordanceTypeBimanualOpposedGraspPlatform:
162 bi.reset(
new AffordanceKit::BimanualOpposedPlatformGraspAffordance());
165 case memoryx::eAffordanceTypeBimanualOpposedGraspPrismatic:
166 bi.reset(
new AffordanceKit::BimanualOpposedPrismaticGraspAffordance());
169 case memoryx::eAffordanceTypeBimanualAlignedGraspPlatform:
170 bi.reset(
new AffordanceKit::BimanualAlignedPlatformGraspAffordance());
173 case memoryx::eAffordanceTypeBimanualAlignedGraspPrismatic:
174 bi.reset(
new AffordanceKit::BimanualAlignedPrismaticGraspAffordance());
177 case memoryx::eAffordanceTypeBimanualTurn:
178 bi.reset(
new AffordanceKit::BimanualTurnAffordance());
181 case memoryx::eAffordanceTypeBimanualLift:
182 bi.reset(
new AffordanceKit::BimanualLiftAffordance());
185 case memoryx::eAffordanceTypeBimanualSupport:
186 case memoryx::eAffordanceTypeBimanualLean:
187 case memoryx::eAffordanceTypeBimanualHold:
188 case memoryx::eAffordanceTypeBimanualPush:
189 case memoryx::eAffordanceTypeBimanualPull:
190 ARMARX_WARNING_S <<
"Affordance type " << affordances[i]->getType() <<
" not yet included in AffordanceKitArmarX";
194 AffordanceKit::PrimitivePtr p = primitives->getPrimitiveById(affordances[i]->getPrimitiveId());
197 std::cout <<
"Warning: Affordance relates to unknown primitive Id" << std::endl;
201 AffordanceKit::ThetaFunctionPtr theta(
new AffordanceKit::ThetaFunction());
202 (*theta)[AffordanceKit::PrimitivePair(p)] = armarx::MatrixFloatPtr::dynamicCast(affordances[i]->getCertaintyFunction())->toEigen();
204 for (
auto& observation : affordances[i]->getObservations())
206 AffordanceKit::ThetaFunctionPtr theta(
new AffordanceKit::ThetaFunction());
207 (*theta)[AffordanceKit::PrimitivePair(p)] = armarx::MatrixFloatPtr::dynamicCast(observation)->toEigen();
211 uni->addObservation(AffordanceKit::ObservationPtr(
new AffordanceKit::Observation(theta)));
215 bi->addObservation(AffordanceKit::ObservationPtr(
new AffordanceKit::Observation(theta)));
221 uni->setTheta(theta);
222 uni->setTimestamp(affordances[i]->getTime()->timestamp);
226 unimanualAffordances.push_back(uni);
227 AffordanceKit::Scene::affordances.push_back(uni);
233 bi->setTimestamp(affordances[i]->getTime()->timestamp);
237 bimanualAffordances.push_back(bi);
238 AffordanceKit::Scene::affordances.push_back(bi);
243 std::cout <<
"Creating a scene from memory took: " << (IceUtil::Time::now() - t).toMilliSeconds() <<
" ms (" << unimanualAffordances.size() <<
" unimanual affordances and " << bimanualAffordances.size() <<
" bimanual affordances)" << std::endl;
246 void SceneArmarX::writeToMemory(
const memoryx::EnvironmentalPrimitiveSegmentBasePrx& primitiveSegment,
const memoryx::AffordanceSegmentBasePrx& affordanceSegment)
const
253 std::vector<memoryx::EntityBasePtr> unimanualAffordanceList;
254 unimanualAffordanceList.reserve(unimanualAffordances.size());
256 for (
auto& primitive : *primitives)
259 for (std::vector<AffordanceKit::UnimanualAffordancePtr>::const_iterator
a = unimanualAffordances.begin();
a < unimanualAffordances.end();
a++)
261 AffordanceKit::UnimanualAffordancePtr affordance = *
a;
262 if (!affordance->existsForPrimitive(primitive))
267 AffordanceKit::Belief
value;
274 a_mx->setPosition(
new armarx::Vector3(Eigen::Vector3f(pose.block<3, 1>(0, 3))));
275 a_mx->setPrimitiveId(primitive->getId());
276 a_mx->setValidationStatus(memoryx::eAffordanceNotValidated);
278 a_mx->setCertaintyFunction(
new armarx::MatrixFloat(affordance->getTheta(AffordanceKit::PrimitivePair(primitive))));
280 memoryx::AffordanceObservationList observations;
281 for (
auto& observation : affordance->getObservations())
283 if (observation->existsForPrimitive(primitive))
288 a_mx->setObservations(observations);
290 unimanualAffordanceList.push_back(a_mx);
295 std::vector<memoryx::EntityBasePtr> bimanualAffordanceList;
296 bimanualAffordanceList.reserve(bimanualAffordances.size());
298 for (
auto& primitive : *primitives)
301 for (std::vector<AffordanceKit::BimanualAffordancePtr>::const_iterator
a = bimanualAffordances.begin();
a < bimanualAffordances.end();
a++)
303 AffordanceKit::BimanualAffordancePtr affordance = *
a;
304 if (!affordance->existsForPrimitive(primitive))
309 AffordanceKit::Belief
value;
310 std::pair<Eigen::Matrix4f, Eigen::Matrix4f> poses = affordance->getMostCertainPosesForPrimitive(primitive,
value);
311 if (poses.first.isZero())
319 std::cout << affordance->getName() << std::endl;
321 a_mx->setPosition(
new armarx::Vector3(Eigen::Vector3f(poses.first.block<3, 1>(0, 3))));
322 a_mx->setPrimitiveId(primitive->getId());
323 a_mx->setValidationStatus(memoryx::eAffordanceNotValidated);
325 a_mx->setCertaintyFunction(
new armarx::MatrixFloat(affordance->getTheta(AffordanceKit::PrimitivePair(primitive, primitive))));
327 memoryx::AffordanceObservationList observations;
328 for (
auto& observation : affordance->getObservations())
330 if (observation->existsForPrimitive(primitive))
335 a_mx->setObservations(observations);
337 bimanualAffordanceList.push_back(a_mx);
342 affordanceSegment->clear();
343 affordanceSegment->upsertEntityList(unimanualAffordanceList);
344 affordanceSegment->upsertEntityList(bimanualAffordanceList);
359 affordanceTypes[
"Bi-G-Pl"] = memoryx::eAffordanceTypeBimanualGraspPlatform;
360 affordanceTypes[
"Bi-G-Pr"] = memoryx::eAffordanceTypeBimanualGraspPrismatic;
361 affordanceTypes[
"Bi-Op-G-Pl"] = memoryx::eAffordanceTypeBimanualOpposedGraspPlatform;
362 affordanceTypes[
"Bi-Op-G-Pr"] = memoryx::eAffordanceTypeBimanualOpposedGraspPrismatic;