28 #include <IceUtil/UUID.h>
31 #include <Inventor/SbVec3f.h>
32 #include <Inventor/SbRotation.h>
35 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
36 #include <MemoryX/interface/memorytypes/MemorySegments.h>
40 #include "../controller/AddOperation.h"
41 #include "../controller/RemoveOperation.h"
42 #include "../controller/RotateTranslateOperation.h"
45 workingMemoryPrx(workingMemoryPrx),
46 workingMemoryUpdatesTopic(workingMemoryUpdatesTopic)
49 this->objectInstancesPrx = memoryx::ObjectInstanceMemorySegmentBasePrx::uncheckedCast(workingMemoryPrx->getSegment(objectInstancesSegmentName));
50 this->memoryXController = memoryXController;
52 std::string s1 =
"SceneEditor.WorkingMemoryController";
53 std::string s2 = IceUtil::generateUUID();
55 this->setName(s1 + s2);
65 memoryx::NameList attributeNames = objectClassPtr->getAttributeNames();
67 for (memoryx::NameList::const_iterator it = attributeNames.begin(); it != attributeNames.end(); ++it)
69 newObject->putAttribute(objectClassPtr->getAttribute(*it));
72 newObject->setClass(objectClassPtr->getName(), 1.0f);
73 newObject->setExistenceCertainty(1.0f);
75 this->currentChangedInstances.push_back(newObject);
77 std::string objectID = this->objectInstancesPrx->addEntity(newObject);
78 newObject->setId(objectID);
85 std::vector<std::string> addedIds;
87 if (!objectInstances.empty())
89 std::shared_ptr<std::vector<controller::OperationPtr> > operations(
new std::vector<controller::OperationPtr>());
91 if (std::shared_ptr<memoryxcontroller::MemoryXController> memXController = this->memoryXController.lock())
95 for (std::vector<memoryx::ObjectInstancePtr>::const_iterator it = objectInstances.begin(); it != objectInstances.end(); ++it)
99 auto pose = objectInstance->getPose();
105 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
109 objectInstance->setPose(newPose);
113 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
117 this->currentChangedInstances.push_back(objectInstance);
118 std::string objectID = this->objectInstancesPrx->addEntity(objectInstance);
119 objectInstance->setId(objectID);
121 addedIds.push_back(objectID);
125 objectInstance->getName(),
126 memXController->getPriorKnowlegdeController()->getCollection(objectInstance),
127 getSbVec3fFromInstance(objectInstance),
128 getSbRotationFromInstance(objectInstance),
129 objectInstance->getId()));
131 operations->push_back(operation);
144 if (this->objectInstancesPrx->getEntityById(
id))
147 this->currentChangedInstances.push_back(memoryx::ObjectInstancePtr::dynamicCast(this->objectInstancesPrx->getEntityById(
id)));
149 this->objectInstancesPrx->removeEntity(
id);
153 throw std::invalid_argument(
"There is no objectInstance with given ID.");
159 memoryx::ObjectInstanceBasePtr objectBase = this->objectInstancesPrx->getObjectInstanceById(
id);
167 newPosition.getValue(x, y, z);
170 const Eigen::Vector3f vec(x * 1000.0f, y * 1000.0f, z * 1000.0f);
172 object->setPosition(newPos);
174 float pw, px, py, pz;
175 newOrientation.getValue(px, py, pz, pw);
178 object->setOrientation(newOrient);
180 this->currentChangedInstances.push_back(
object);
182 this->objectInstancesPrx->setObjectPoseWithoutMotionModel(
id, object->getPose());
186 throw std::invalid_argument(
"There is no objectInstance with given ID.");
192 if (std::shared_ptr<memoryxcontroller::MemoryXController> memXController = this->memoryXController.lock())
196 std::unique_lock lock(mutexEntities);
198 memoryx::EntityIdList ids = this->objectInstancesPrx->getAllEntityIds();
199 std::shared_ptr<std::vector<controller::OperationPtr> > operations(
new std::vector<controller::OperationPtr>());
201 for (memoryx::EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
203 memoryx::ObjectInstancePtr object = memoryx::ObjectInstancePtr::dynamicCast(this->objectInstancesPrx->getObjectInstanceById(*it));
205 auto pose =
object->getPose();
209 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
213 object->setPose(newPose);
217 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
224 memXController->getPriorKnowlegdeController()->getCollection(
object),
225 getSbVec3fFromInstance(
object),
226 getSbRotationFromInstance(
object),
228 operations->push_back(operation);
239 if (0 == objectInstancesPrx->getIceId().name.compare(objectInstancesPrx->getIceId().name.length() - segmentName.length(), segmentName.length(), segmentName))
243 auto pose = ptr->getPose();
247 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
251 ptr->setPose(newPose);
255 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
259 if (!(this->findAndRemoveInstanceFromList(this->currentChangedInstances, ptr)))
261 std::shared_ptr<std::vector<controller::OperationPtr> > operations(
new std::vector<controller::OperationPtr>());
263 if (std::shared_ptr<MemoryXController> memXController = this->memoryXController.lock())
270 memXController->getPriorKnowlegdeController()->getCollection(ptr),
271 getSbVec3fFromInstance(ptr),
272 getSbRotationFromInstance(ptr),
274 operations->push_back(operation);
283 const memoryx::EntityBasePtr& entityOld,
284 const memoryx::EntityBasePtr& entityNew,
const Ice::Current&)
288 if (0 == objectInstancesPrx->getIceId().name.compare(objectInstancesPrx->getIceId().name.length() - segmentName.length(), segmentName.length(), segmentName))
291 auto pose = ptr_new->getPose();
295 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
299 ptr_new->setPose(newPose);
303 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
307 if (!(this->findAndRemoveInstanceFromList(this->currentChangedInstances, ptr_new)))
311 auto pose = ptr_old->getPose();
315 auto newPose = workingMemoryPrx->getAgentInstancesSegment()->convertToWorldPose(pose->agent, pose);
319 ptr_old->setPose(newPose);
323 ARMARX_ERROR_S <<
"could not convert local pose to global pose" << std::endl;
327 std::shared_ptr<std::vector<controller::OperationPtr> > operations(
new std::vector<controller::OperationPtr>());
329 if (std::shared_ptr<MemoryXController> memXController = this->memoryXController.lock())
336 getSbRotationFromInstance(ptr_old),
337 getSbRotationFromInstance(ptr_new),
338 getSbVec3fFromInstance(ptr_old),
339 getSbVec3fFromInstance(ptr_new)));
340 operations->push_back(operation);
351 if (0 == objectInstancesPrx->getIceId().name.compare(objectInstancesPrx->getIceId().name.length() - segmentName.length(), segmentName.length(), segmentName))
355 if (!(this->findAndRemoveInstanceFromList(this->currentChangedInstances, ptr)))
357 std::shared_ptr<std::vector<controller::OperationPtr> > operations(
new std::vector<controller::OperationPtr>());
359 if (std::shared_ptr<MemoryXController> memXController = this->memoryXController.lock())
366 operations->push_back(operation);
386 usingTopic(workingMemoryUpdatesTopic);
396 return "SceneEditor.WorkingMemoryController";
403 SbRotation orientation(SbRotation(ptr->qx, ptr->qy, ptr->qz, ptr->qw));
412 SbVec3f pos(SbVec3f(ptr->x * 0.001f, ptr->y * 0.001f, ptr->z * 0.001f));
416 bool memoryxcontroller::WorkingMemoryController::findAndRemoveInstanceFromList(std::list<memoryx::ObjectInstancePtr>& instanceList,
const memoryx::ObjectInstancePtr& instance)
const
418 for (std::list<memoryx::ObjectInstancePtr>::const_iterator it = instanceList.begin(); it != instanceList.end(); ++it)
422 if (temp->getId() == instance->getId() && temp->getPosition()->toEigen() == instance->getPosition()->toEigen())
424 instanceList.remove(temp);
434 return longtermMemoryPrx->saveWorkingMemorySnapshot(snapshotName, this->workingMemoryPrx);
439 return longtermMemoryPrx->getWorkingMemorySnapshotListSegment()->createSubsetSnapshot(snapshotName, this->workingMemoryPrx, objectIds);