28 #include <IceUtil/UUID.h>
31 #include <Inventor/SbRotation.h>
32 #include <Inventor/SbVec3f.h>
35 #include "../controller/AddOperation.h"
36 #include "../controller/RemoveOperation.h"
37 #include "../controller/RotateTranslateOperation.h"
39 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
40 #include <MemoryX/interface/memorytypes/MemorySegments.h>
44 const memoryx::WorkingMemoryInterfacePrx& workingMemoryPrx,
45 const std::string& workingMemoryUpdatesTopic,
46 const std::string& objectInstancesSegmentName,
48 workingMemoryPrx(workingMemoryPrx), workingMemoryUpdatesTopic(workingMemoryUpdatesTopic)
51 this->objectInstancesPrx = memoryx::ObjectInstanceMemorySegmentBasePrx::uncheckedCast(
52 workingMemoryPrx->getSegment(objectInstancesSegmentName));
53 this->memoryXController = memoryXController;
55 std::string s1 =
"SceneEditor.WorkingMemoryController";
56 std::string s2 = IceUtil::generateUUID();
58 this->setName(s1 + s2);
67 const std::string& objectName,
71 memoryx::NameList attributeNames = objectClassPtr->getAttributeNames();
73 for (memoryx::NameList::const_iterator it = attributeNames.begin(); it != attributeNames.end();
76 newObject->putAttribute(objectClassPtr->getAttribute(*it));
79 newObject->setClass(objectClassPtr->getName(), 1.0f);
80 newObject->setExistenceCertainty(1.0f);
82 this->currentChangedInstances.push_back(newObject);
84 std::string objectID = this->objectInstancesPrx->addEntity(newObject);
85 newObject->setId(objectID);
90 std::vector<std::string>
92 const std::vector<memoryx::ObjectInstancePtr>& objectInstances)
94 std::vector<std::string> addedIds;
96 if (!objectInstances.empty())
98 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
99 new std::vector<controller::OperationPtr>());
101 if (std::shared_ptr<memoryxcontroller::MemoryXController> memXController =
102 this->memoryXController.lock())
106 for (std::vector<memoryx::ObjectInstancePtr>::const_iterator it =
107 objectInstances.begin();
108 it != objectInstances.end();
113 auto pose = objectInstance->getPose();
120 workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(
125 objectInstance->setPose(newPose);
134 this->currentChangedInstances.push_back(objectInstance);
135 std::string objectID = this->objectInstancesPrx->addEntity(objectInstance);
136 objectInstance->setId(objectID);
138 addedIds.push_back(objectID);
143 objectInstance->getName(),
144 memXController->getPriorKnowlegdeController()->getCollection(
146 getSbVec3fFromInstance(objectInstance),
147 getSbRotationFromInstance(objectInstance),
148 objectInstance->getId()));
150 operations->push_back(operation);
166 if (this->objectInstancesPrx->getEntityById(
id))
169 this->currentChangedInstances.push_back(
170 memoryx::ObjectInstancePtr::dynamicCast(this->objectInstancesPrx->getEntityById(
id)));
172 this->objectInstancesPrx->removeEntity(
id);
176 throw std::invalid_argument(
"There is no objectInstance with given ID.");
182 const SbRotation& newOrientation,
183 const SbVec3f& newPosition)
185 memoryx::ObjectInstanceBasePtr objectBase = this->objectInstancesPrx->getObjectInstanceById(
id);
193 newPosition.getValue(x, y, z);
196 const Eigen::Vector3f vec(x * 1000.0f, y * 1000.0f, z * 1000.0f);
198 object->setPosition(newPos);
200 float pw, px, py, pz;
201 newOrientation.getValue(px, py, pz, pw);
205 object->setOrientation(newOrient);
207 this->currentChangedInstances.push_back(
object);
209 this->objectInstancesPrx->setObjectPoseWithoutMotionModel(
id, object->getPose());
213 throw std::invalid_argument(
"There is no objectInstance with given ID.");
220 if (std::shared_ptr<memoryxcontroller::MemoryXController> memXController =
221 this->memoryXController.lock())
225 std::unique_lock lock(mutexEntities);
227 memoryx::EntityIdList ids = this->objectInstancesPrx->getAllEntityIds();
228 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
229 new std::vector<controller::OperationPtr>());
231 for (memoryx::EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
234 this->objectInstancesPrx->getObjectInstanceById(*it));
236 auto pose =
object->getPose();
240 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(
245 object->setPose(newPose);
258 memXController->getPriorKnowlegdeController()->getCollection(
object),
259 getSbVec3fFromInstance(
object),
260 getSbRotationFromInstance(
object),
262 operations->push_back(operation);
272 const std::string& segmentName,
273 const memoryx::EntityBasePtr& entity,
276 if (0 == objectInstancesPrx->getIceId().name.compare(
277 objectInstancesPrx->getIceId().name.length() - segmentName.length(),
278 segmentName.length(),
283 auto pose = ptr->getPose();
288 workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
292 ptr->setPose(newPose);
296 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
300 if (!(this->findAndRemoveInstanceFromList(this->currentChangedInstances, ptr)))
302 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
303 new std::vector<controller::OperationPtr>());
305 if (std::shared_ptr<MemoryXController> memXController = this->memoryXController.lock())
313 memXController->getPriorKnowlegdeController()->getCollection(ptr),
314 getSbVec3fFromInstance(ptr),
315 getSbRotationFromInstance(ptr),
317 operations->push_back(operation);
327 const std::string& segmentName,
328 const memoryx::EntityBasePtr& entityOld,
329 const memoryx::EntityBasePtr& entityNew,
334 if (0 == objectInstancesPrx->getIceId().name.compare(
335 objectInstancesPrx->getIceId().name.length() - segmentName.length(),
336 segmentName.length(),
340 auto pose = ptr_new->getPose();
345 workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
349 ptr_new->setPose(newPose);
353 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
357 if (!(this->findAndRemoveInstanceFromList(this->currentChangedInstances, ptr_new)))
361 auto pose = ptr_old->getPose();
365 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(
370 ptr_old->setPose(newPose);
374 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
378 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
379 new std::vector<controller::OperationPtr>());
381 if (std::shared_ptr<MemoryXController> memXController = this->memoryXController.lock())
389 getSbRotationFromInstance(ptr_old),
390 getSbRotationFromInstance(ptr_new),
391 getSbVec3fFromInstance(ptr_old),
392 getSbVec3fFromInstance(ptr_new)));
393 operations->push_back(operation);
403 const std::string& segmentName,
404 const memoryx::EntityBasePtr& entity,
407 if (0 == objectInstancesPrx->getIceId().name.compare(
408 objectInstancesPrx->getIceId().name.length() - segmentName.length(),
409 segmentName.length(),
414 if (!(this->findAndRemoveInstanceFromList(this->currentChangedInstances, ptr)))
416 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
417 new std::vector<controller::OperationPtr>());
419 if (std::shared_ptr<MemoryXController> memXController = this->memoryXController.lock())
424 memXController,
controller->getScene(), ptr->getId()));
425 operations->push_back(operation);
448 usingTopic(workingMemoryUpdatesTopic);
459 return "SceneEditor.WorkingMemoryController";
467 SbRotation orientation(SbRotation(ptr->qx, ptr->qy, ptr->qz, ptr->qw));
478 SbVec3f pos(SbVec3f(ptr->x * 0.001f, ptr->y * 0.001f, ptr->z * 0.001f));
483 memoryxcontroller::WorkingMemoryController::findAndRemoveInstanceFromList(
484 std::list<memoryx::ObjectInstancePtr>& instanceList,
487 for (std::list<memoryx::ObjectInstancePtr>::const_iterator it = instanceList.begin();
488 it != instanceList.end();
493 if (temp->getId() == instance->getId() &&
494 temp->getPosition()->toEigen() == instance->getPosition()->toEigen())
496 instanceList.remove(temp);
506 const std::string& snapshotName,
507 const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx)
const
509 return longtermMemoryPrx->saveWorkingMemorySnapshot(snapshotName, this->workingMemoryPrx);
514 const std::string& snapshotName,
515 const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx,
516 const std::vector<std::string>& objectIds)
const
518 return longtermMemoryPrx->getWorkingMemorySnapshotListSegment()->createSubsetSnapshot(
519 snapshotName, this->workingMemoryPrx, objectIds);