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
25#include <vector>
26
28
30#include <MemoryX/interface/core/EntityBase.h>
31#include <MemoryX/interface/memorytypes/MemoryEntities.h>
32#include <MemoryX/interface/memorytypes/MemorySegments.h>
35
36namespace memoryx
37{
39 virtual public PersistentEntitySegment,
40 virtual public PersistentObjectInstanceSegmentBase
41 {
42 public:
43 PersistentObjectInstanceSegment(CollectionInterfacePrx entityCollection,
45 bool useMongoIds = true) :
46 PersistentEntitySegment(entityCollection, ic, useMongoIds),
47 PersistentObjectInstanceSegmentBase()
48 {
49 }
50
51 ObjectInstanceBasePtr
52 getObjectInstanceById(const ::std::string& id,
53 const ::Ice::Current& = Ice::emptyCurrent) const override
54 {
55 return ObjectInstanceBasePtr::dynamicCast(getEntityById(id));
56 }
57
58 ObjectInstanceBasePtr
59 getObjectInstanceByName(const ::std::string& name,
60 const ::Ice::Current& = Ice::emptyCurrent) const override
61 {
62 return ObjectInstanceBasePtr::dynamicCast(getEntityByName(name));
63 }
64
65 ObjectInstanceList
66 getObjectInstancesByClass(const ::std::string& className,
67 const ::Ice::Current& c = Ice::emptyCurrent) const override
68 {
69 ObjectInstanceList result;
70 NameList classList;
71 classList.push_back(className);
72 return getObjectInstancesByClassList(classList, c);
73 }
74
75 ObjectInstanceList
76 getObjectInstancesByClassList(const NameList& classList,
77 const ::Ice::Current& = Ice::emptyCurrent) const override
78 {
79 ObjectInstanceList result;
80 const EntityBaseList entityList = getEntitiesByAttrValueList("classes", classList);
81
82 for (EntityBaseList::const_iterator it = entityList.begin(); it != entityList.end();
83 ++it)
84 {
85 const ObjectInstanceBasePtr instance = ObjectInstanceBasePtr::dynamicCast(*it);
86
87 if (instance)
88 {
89 result.push_back(instance);
90 }
91 }
92
93 return result;
94 }
95
96 void
97 setNewMotionModel(const ::std::string& entityId,
98 const ::memoryx::MotionModelInterfacePtr& newMotionModel,
99 const ::Ice::Current& = Ice::emptyCurrent) override
100 {
101 throw armarx::LocalException()
102 << "This function does not work on the persistent instance segment since the "
103 "motion model is only temporary!";
104
105
106 ObjectInstancePtr object =
107 ObjectInstancePtr::dynamicCast(getObjectInstanceById(entityId));
108 MotionModelInterfacePtr oldMotionModel = object->getMotionModel();
109
110 if (oldMotionModel)
111 {
112 armarx::LinkedPoseBasePtr oldPose = oldMotionModel->getPoseAtLastLocalisation();
113
114 if (oldPose)
115 {
116 newMotionModel->setPoseAtLastLocalisation(oldPose, NULL, NULL);
117 }
118 else
119 {
121 << "Object has an old motion model, but that motion model has no pose.";
122 }
123 }
124 else
125 {
127 << "Object didn't have a motion model before, this may cause problems";
128 }
129
130 object->setMotionModel(AbstractMotionModelPtr::dynamicCast(newMotionModel));
131 }
132
133 void
134 setObjectPose(const std::string& entityId,
135 const armarx::LinkedPoseBasePtr& objectPose,
136 const ::Ice::Current& = Ice::emptyCurrent) override
137 {
138 ObjectInstancePtr object =
139 ObjectInstancePtr::dynamicCast(getObjectInstanceById(entityId));
140
142 armarx::Vector3Ptr::dynamicCast(objectPose->position)->toEigen(),
143 objectPose->frame,
144 objectPose->agent);
145 object->setPosition(position);
147 armarx::QuaternionPtr::dynamicCast(objectPose->orientation)->toEigen(),
148 objectPose->frame,
149 objectPose->agent);
150 object->setOrientation(orientation);
151
152 //armarx::LinkedPosePtr newPose = armarx::LinkedPosePtr::dynamicCast(object->getMotionModel()->getPoseAtLastLocalisation());
153 //newPose->position = objectPose->position;
154 //newPose->orientation = objectPose->orientation;
155 //newPose->frame = objectPose->frame;
156 object->getMotionModel()->setPoseAtLastLocalisation(objectPose, NULL, NULL);
157 }
158
159 void
160 setObjectPoseWithoutMotionModel(const std::string& entityId,
161 const armarx::FramedPoseBasePtr& objectPose,
162 const Ice::Current&) override
163 {
164 ObjectInstancePtr object =
165 ObjectInstancePtr::dynamicCast(getObjectInstanceById(entityId));
166
168 armarx::Vector3Ptr::dynamicCast(objectPose->position)->toEigen(),
169 objectPose->frame,
170 objectPose->agent);
171 object->setPosition(position);
172 object->setPositionUncertainty(
175 armarx::QuaternionPtr::dynamicCast(objectPose->orientation)->toEigen(),
176 objectPose->frame,
177 objectPose->agent);
178 object->setOrientation(orientation);
179
180
181 if (object->getMotionModel())
182 {
183 ARMARX_WARNING_S << object->getName()
184 << " has a motion model - You should not call "
185 "setObjectPoseWithoutMotionModel() when a motion model is set. "
186 "Use set setObjectPose() instead!";
187 }
188 }
189
190 std::string
191 addObjectInstance(const std::string& instanceName,
192 const std::string& className,
193 const armarx::LinkedPoseBasePtr& objectPose,
194 const ::memoryx::MotionModelInterfacePtr& motionModel,
195 const ::Ice::Current& = Ice::emptyCurrent) override
196 {
197 ObjectInstancePtr object = new ObjectInstance(instanceName);
199 armarx::Vector3Ptr::dynamicCast(objectPose->position)->toEigen(),
200 objectPose->frame,
201 objectPose->agent);
202 object->setPosition(position);
204 armarx::QuaternionPtr::dynamicCast(objectPose->orientation)->toEigen(),
205 objectPose->frame,
206 objectPose->agent);
207 object->setOrientation(orientation);
208 FloatVector mean = {
209 objectPose->position->x, objectPose->position->y, objectPose->position->z};
210 FloatVector variances = {10, 10, 10};
212 new MultivariateNormalDistribution(mean, variances);
213 object->setPositionUncertainty(uncertainty);
214 object->addClass(className, 1.0);
215 object->setExistenceCertainty(1.0);
216
217 motionModel->setPoseAtLastLocalisation(objectPose, NULL, NULL);
218 object->setMotionModel(AbstractMotionModelPtr::dynamicCast(motionModel));
219
220 return addEntity(object);
221 }
222 };
223
225} // namespace memoryx
#define ARMARXCOMPONENT_IMPORT_EXPORT
constexpr T c
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPosition class.
Definition FramedPose.h:158
The MultivariateNormalDistribution class.
static MultivariateNormalDistributionPtr CreateDefaultDistribution(float variance=10000)
Create a distribution with uncertainty of variance in all directions (default: variance = 10000,...
PersistentEntitySegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds=true)
std::string addEntity(const EntityBasePtr &entity, const ::Ice::Current &c=Ice::emptyCurrent) override
addEntity add new entity and return the newly generated entity ID
EntityBasePtr getEntityById(const ::std::string &entityId, const ::Ice::Current &=Ice::emptyCurrent) const override
EntityBaseList getEntitiesByAttrValueList(const ::std::string &attrName, const NameList &attrValueList, const ::Ice::Current &=Ice::emptyCurrent) const override
EntityBasePtr getEntityByName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
void setObjectPose(const std::string &entityId, const armarx::LinkedPoseBasePtr &objectPose, const ::Ice::Current &=Ice::emptyCurrent) override
ObjectInstanceList getObjectInstancesByClass(const ::std::string &className, const ::Ice::Current &c=Ice::emptyCurrent) const override
ObjectInstanceBasePtr getObjectInstanceById(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) const override
void setObjectPoseWithoutMotionModel(const std::string &entityId, const armarx::FramedPoseBasePtr &objectPose, const Ice::Current &) override
ObjectInstanceBasePtr getObjectInstanceByName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
ObjectInstanceList getObjectInstancesByClassList(const NameList &classList, const ::Ice::Current &=Ice::emptyCurrent) const override
PersistentObjectInstanceSegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds=true)
void setNewMotionModel(const ::std::string &entityId, const ::memoryx::MotionModelInterfacePtr &newMotionModel, const ::Ice::Current &=Ice::emptyCurrent) override
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
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
Definition FramedPose.h:207
VirtualRobot headers.
IceInternal::Handle< PersistentObjectInstanceSegment > PersistentObjectInstanceSegmentPtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
IceInternal::Handle< MultivariateNormalDistribution > MultivariateNormalDistributionPtr