28#include <IceUtil/UUID.h>
31#include <Inventor/SbRotation.h>
32#include <Inventor/SbVec3f.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);
90std::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(
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),
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())
311 controller->getScene(),
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())
386 new controller::RotateTranslateOperation(memXController,
387 controller->getScene(),
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);
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));
483memoryxcontroller::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);
The FramedOrientation class.
The FramedPosition class.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
A Operation to create a new object in the scene and the WorkingMemory.
static const int UNDOABLE
A flag to save the executed operations to the history.
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
void onInitComponent() override
static SbRotation getSbRotationFromInstance(const memoryx::ObjectInstancePtr &objectInstance)
Returns the rotation of a object instance.
WorkingMemoryController(const memoryx::WorkingMemoryInterfacePrx &workingMemoryPrx, const std::string &workingMemoryUpdatesTopic, const std::string &objectInstancesSegmentName, const std::shared_ptr< MemoryXController > &memoryXController)
Constructor.
void reportSnapshotLoaded(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when a segment was loaded from a snapshot.
void addAllInstancesToLocalScene() const
Adds all object instances of the working memory to the local scene.
~WorkingMemoryController() override
Destructor.
std::string addObjectInstance(const std::string &objectName, const memoryx::ObjectClassPtr &objectClassPtr)
Adds a new object instance to the working memory.
void reportEntityRemoved(const std::string &segmentName, const ::memoryx::EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is removed from the working memory.
static SbVec3f getSbVec3fFromInstance(const memoryx::ObjectInstancePtr &objectInstance)
Returns the position of a object instance.
void reportEntityUpdated(const std::string &segmentName, const ::memoryx::EntityBasePtr &entityOld, const ::memoryx::EntityBasePtr &entityNew, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is updated in the working memory.
void removeObjectInstance(const std::string &id)
Removes the object instance with the given id.
void rotateTranslateObject(const std::string &id, const SbRotation &newOrientation, const SbVec3f &newPosition)
Applies the given orientation and position to the object instance with the given id.
bool saveObjectsInSnapshot(const std::string &snapshotName, const memoryx::LongtermMemoryInterfacePrx &longtermMemoryPrx, const std::vector< std::string > &objectIds) const
Saves the object instances with the given ids in a snapshot with the given name.
void reportEntityCreated(const std::string &segmentName, const ::memoryx::EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is created in the working memory.
void onConnectComponent() override
bool saveSceneInSnapshot(const std::string &snapshotName, const memoryx::LongtermMemoryInterfacePrx &longtermMemoryPrx) const
Saves the whole scene in a snapshot with the given name.
std::vector< std::string > addObjectInstances(const std::vector< memoryx::ObjectInstancePtr > &instancePtrs)
Adds all given object instances to the working memory.
void reportMemoryCleared(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is created on the working memory.
std::string getDefaultName() const override
Returns the default name of this managed ice object.
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
std::string const GlobalFrame
Variable of the global coordinate system.
Quaternion< float, 0 > Quaternionf
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< FramedPosition > FramedPositionPtr
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< Operation > OperationPtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
std::shared_ptr< MemoryXController > MemoryXControllerPtr