PersistentObjectInstanceSegment.h
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 MemoryX::WorkingMemory
17 * @author Kai Welke ( welke at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
26 
30 
31 #include <MemoryX/interface/core/EntityBase.h>
32 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
33 #include <MemoryX/interface/memorytypes/MemorySegments.h>
34 
35 #include <vector>
36 
37 namespace memoryx
38 {
40  virtual public PersistentEntitySegment,
41  virtual public PersistentObjectInstanceSegmentBase
42  {
43  public:
44  PersistentObjectInstanceSegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds = true) :
45  PersistentEntitySegment(entityCollection, ic, useMongoIds), PersistentObjectInstanceSegmentBase()
46  {
47  }
48 
49  ObjectInstanceBasePtr getObjectInstanceById(const ::std::string& id, const ::Ice::Current& = Ice::emptyCurrent) const override
50  {
51  return ObjectInstanceBasePtr::dynamicCast(getEntityById(id));
52  }
53 
54  ObjectInstanceBasePtr getObjectInstanceByName(const ::std::string& name, const ::Ice::Current& = Ice::emptyCurrent) const override
55  {
56  return ObjectInstanceBasePtr::dynamicCast(getEntityByName(name));
57  }
58 
59  ObjectInstanceList getObjectInstancesByClass(const ::std::string& className, const ::Ice::Current& c = Ice::emptyCurrent) const override
60  {
61  ObjectInstanceList result;
62  NameList classList;
63  classList.push_back(className);
64  return getObjectInstancesByClassList(classList, c);
65  }
66 
67  ObjectInstanceList getObjectInstancesByClassList(const NameList& classList, const ::Ice::Current& = Ice::emptyCurrent) const override
68  {
69  ObjectInstanceList result;
70  const EntityBaseList entityList = getEntitiesByAttrValueList("classes", classList);
71 
72  for (EntityBaseList::const_iterator it = entityList.begin(); it != entityList.end(); ++it)
73  {
74  const ObjectInstanceBasePtr instance = ObjectInstanceBasePtr::dynamicCast(*it);
75 
76  if (instance)
77  {
78  result.push_back(instance);
79  }
80  }
81 
82  return result;
83  }
84 
85  void setNewMotionModel(const ::std::string& entityId, const ::memoryx::MotionModelInterfacePtr& newMotionModel, const ::Ice::Current& = Ice::emptyCurrent) override
86  {
87  throw armarx::LocalException() << "This function does not work on the persistent instance segment since the motion model is only temporary!";
88 
89 
90 
91  ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(getObjectInstanceById(entityId));
92  MotionModelInterfacePtr oldMotionModel = object->getMotionModel();
93 
94  if (oldMotionModel)
95  {
96  armarx::LinkedPoseBasePtr oldPose = oldMotionModel->getPoseAtLastLocalisation();
97 
98  if (oldPose)
99  {
100  newMotionModel->setPoseAtLastLocalisation(oldPose, NULL, NULL);
101  }
102  else
103  {
104  ARMARX_ERROR_S << "Object has an old motion model, but that motion model has no pose.";
105  }
106  }
107  else
108  {
109  ARMARX_WARNING_S << "Object didn't have a motion model before, this may cause problems";
110  }
111 
112  object->setMotionModel(AbstractMotionModelPtr::dynamicCast(newMotionModel));
113  }
114 
115 
116  void setObjectPose(const std::string& entityId, const armarx::LinkedPoseBasePtr& objectPose, const ::Ice::Current& = Ice::emptyCurrent) override
117  {
118  ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(getObjectInstanceById(entityId));
119 
120  armarx::FramedPositionPtr position = new armarx::FramedPosition(armarx::Vector3Ptr::dynamicCast(objectPose->position)->toEigen(), objectPose->frame, objectPose->agent);
121  object->setPosition(position);
122  armarx::FramedOrientationPtr orientation = new armarx::FramedOrientation(armarx::QuaternionPtr::dynamicCast(objectPose->orientation)->toEigen(), objectPose->frame, objectPose->agent);
123  object->setOrientation(orientation);
124 
125  //armarx::LinkedPosePtr newPose = armarx::LinkedPosePtr::dynamicCast(object->getMotionModel()->getPoseAtLastLocalisation());
126  //newPose->position = objectPose->position;
127  //newPose->orientation = objectPose->orientation;
128  //newPose->frame = objectPose->frame;
129  object->getMotionModel()->setPoseAtLastLocalisation(objectPose, NULL, NULL);
130  }
131 
132  void setObjectPoseWithoutMotionModel(const std::string& entityId, const armarx::FramedPoseBasePtr& objectPose, const Ice::Current&) override
133  {
134  ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(getObjectInstanceById(entityId));
135 
136  armarx::FramedPositionPtr position = new armarx::FramedPosition(armarx::Vector3Ptr::dynamicCast(objectPose->position)->toEigen(), objectPose->frame, objectPose->agent);
137  object->setPosition(position);
138  object->setPositionUncertainty(MultivariateNormalDistribution::CreateDefaultDistribution());
139  armarx::FramedOrientationPtr orientation = new armarx::FramedOrientation(armarx::QuaternionPtr::dynamicCast(objectPose->orientation)->toEigen(), objectPose->frame, objectPose->agent);
140  object->setOrientation(orientation);
141 
142 
143  if (object->getMotionModel())
144  {
145  ARMARX_WARNING_S << object->getName() << " has a motion model - You should not call setObjectPoseWithoutMotionModel() when a motion model is set. Use set setObjectPose() instead!";
146  }
147  }
148 
149 
150  std::string addObjectInstance(const std::string& instanceName, const std::string& className, const armarx::LinkedPoseBasePtr& objectPose,
151  const ::memoryx::MotionModelInterfacePtr& motionModel, const ::Ice::Current& = Ice::emptyCurrent) override
152  {
153  ObjectInstancePtr object = new ObjectInstance(instanceName);
154  armarx::FramedPositionPtr position = new armarx::FramedPosition(armarx::Vector3Ptr::dynamicCast(objectPose->position)->toEigen(), objectPose->frame, objectPose->agent);
155  object->setPosition(position);
156  armarx::FramedOrientationPtr orientation = new armarx::FramedOrientation(armarx::QuaternionPtr::dynamicCast(objectPose->orientation)->toEigen(), objectPose->frame, objectPose->agent);
157  object->setOrientation(orientation);
158  FloatVector mean = {objectPose->position->x, objectPose->position->y, objectPose->position->z};
159  FloatVector variances = {10, 10, 10};
161  object->setPositionUncertainty(uncertainty);
162  object->addClass(className, 1.0);
163  object->setExistenceCertainty(1.0);
164 
165  motionModel->setPoseAtLastLocalisation(objectPose, NULL, NULL);
166  object->setMotionModel(AbstractMotionModelPtr::dynamicCast(motionModel));
167 
168  return addEntity(object);
169  }
170 
171  };
172 
174 }
175 
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
memoryx::PersistentObjectInstanceSegment::setObjectPoseWithoutMotionModel
void setObjectPoseWithoutMotionModel(const std::string &entityId, const armarx::FramedPoseBasePtr &objectPose, const Ice::Current &) override
Definition: PersistentObjectInstanceSegment.h:132
memoryx::PersistentObjectInstanceSegment::getObjectInstancesByClassList
ObjectInstanceList getObjectInstancesByClassList(const NameList &classList, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: PersistentObjectInstanceSegment.h:67
PersistentEntitySegment.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::ObjectInstance
Definition: ObjectInstance.h:47
MotionModelObjectFactories.h
memoryx::PersistentObjectInstanceSegment::getObjectInstanceById
ObjectInstanceBasePtr getObjectInstanceById(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: PersistentObjectInstanceSegment.h:49
IceInternal::Handle< ::Ice::Communicator >
memoryx::MultivariateNormalDistribution::CreateDefaultDistribution
static MultivariateNormalDistributionPtr CreateDefaultDistribution(float variance=10000)
Create a distribution with uncertainty of variance in all directions (default: variance = 10000,...
Definition: ProbabilityMeasures.h:251
memoryx::PersistentObjectInstanceSegment::setObjectPose
void setObjectPose(const std::string &entityId, const armarx::LinkedPoseBasePtr &objectPose, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PersistentObjectInstanceSegment.h:116
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
armarx::FloatVector
::std::vector< ::Ice::Float > FloatVector
Definition: KinematicUnitGuiPlugin.h:327
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
memoryx::PersistentObjectInstanceSegment::getObjectInstancesByClass
ObjectInstanceList getObjectInstancesByClass(const ::std::string &className, const ::Ice::Current &c=Ice::emptyCurrent) const override
Definition: PersistentObjectInstanceSegment.h:59
memoryx::PersistentObjectInstanceSegment::setNewMotionModel
void setNewMotionModel(const ::std::string &entityId, const ::memoryx::MotionModelInterfacePtr &newMotionModel, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PersistentObjectInstanceSegment.h:85
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
ObjectInstance.h
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
memoryx::PersistentObjectInstanceSegment::PersistentObjectInstanceSegment
PersistentObjectInstanceSegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds=true)
Definition: PersistentObjectInstanceSegment.h:44
memoryx::PersistentObjectInstanceSegment::addObjectInstance
std::string addObjectInstance(const std::string &instanceName, const std::string &className, const armarx::LinkedPoseBasePtr &objectPose, const ::memoryx::MotionModelInterfacePtr &motionModel, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PersistentObjectInstanceSegment.h:150
memoryx::PersistentEntitySegment
The PersistentEntitySegment class is the base class for all memory segments containing memoryx::Entit...
Definition: PersistentEntitySegment.h:107
memoryx::PersistentObjectInstanceSegment
Definition: PersistentObjectInstanceSegment.h:39
memoryx::PersistentObjectInstanceSegment::getObjectInstanceByName
ObjectInstanceBasePtr getObjectInstanceByName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: PersistentObjectInstanceSegment.h:54
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
armarx::VariantType::MultivariateNormalDistribution
const armarx::VariantTypeId MultivariateNormalDistribution
Definition: ProbabilityMeasures.h:36
ImportExportComponent.h