ObjectInstance.cpp
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 ALexey Kozlov ( kozlov at kit dot edu), Kai Welke (welke at kit got edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "ObjectInstance.h"
24 
27 #include <MemoryX/interface/observers/ObjectMemoryObserverInterface.h>
29 
30 namespace memoryx
31 {
32 
33  // ********************************************************************
34  // construction / destruction
35  // ********************************************************************
36  ObjectInstance::ObjectInstance(const std::string& name, const std::string& id)
37  : Entity()
38  {
39  // set entity properties (TODO: maybe move to entity constructor)
40  setName(name);
41  setId(id);
42 
43  // initialize attributes
47  putAttribute(new EntityAttribute("priority"));
48  setLocalizationPriority(armarx::DEFAULT_VIEWTARGET_PRIORITY);
49 
50  EntityAttributePtr classNamesAttr = new EntityAttribute("classes");
51  putAttribute(classNamesAttr);
52 
53  motionModel = NULL;
54  }
55 
56  ObjectInstance::ObjectInstance(const ObjectInstance& source) :
57  IceUtil::Shared(source),
58  ::armarx::Serializable(source),
59  EntityBase(),// dont copy
60  ObjectInstanceBase(source),
61  Entity(source)
62  {
63 
64  }
65 
66  // ********************************************************************
67  // attribute getters / setters
68  // ********************************************************************
69  float ObjectInstance::getExistenceCertainty(const ::Ice::Current&) const
70  {
71  return getAttributeValue("existenceCertainty")->getFloat();
72  }
73 
74 
75  void ObjectInstance::setExistenceCertainty(float existenceCertainty, const ::Ice::Current&)
76  {
77  putAttribute("existenceCertainty", existenceCertainty);
78  }
79 
80 
81  EntityAttributeBasePtr ObjectInstance::getPositionAttribute() const
82  {
83  return getAttribute("position");
84  }
85 
86 
88  {
89  return armarx::FramedPositionPtr::dynamicCast(getPositionBase());
90  }
91 
92 
93  void ObjectInstance::setPosition(const armarx::FramedPositionBasePtr& position, const ::Ice::Current&)
94  {
96  {
97  if (getPositionAttribute()->getUncertainty())
98  {
99  MultivariateNormalDistributionBasePtr uncertainty = MultivariateNormalDistributionBasePtr::dynamicCast(getPositionAttribute()->getUncertainty());
100  putAttribute("position", position);
101  getPositionAttribute()->setValueWithUncertainty(getPositionAttribute()->getValue(), uncertainty);
102  }
103  else
104  {
105  putAttribute("position", position);
106  }
107  }
108  else
109  {
110  putAttribute("position", position);
111  }
112  }
113 
114 
116  {
118  }
119 
120  void ObjectInstance::setLocalizationTimestamp(const armarx::TimestampBasePtr& timestamp)
121  {
122  putAttribute("localizationTimestamp", timestamp);
123  }
124 
126  {
127  return hasAttribute("localizationTimestamp");
128  }
129 
131  {
132  auto variant = getAttributeValue("localizationTimestamp");
133  ARMARX_CHECK_EXPRESSION(variant) << "no timestamp was set for this object pose";
134  auto timestamp = variant->get<armarx::TimestampVariant>();
135  return timestamp->toTime();
136  }
137 
138  int ObjectInstance::getLocalizationPriority(const ::Ice::Current&) const
139  {
140  if (!hasAttribute("priority"))
141  {
142  return armarx::DEFAULT_VIEWTARGET_PRIORITY;
143  }
144  return getAttributeValue("priority")->getInt();
145  }
146 
147  void ObjectInstance::setLocalizationPriority(Ice::Int priority, const ::Ice::Current&)
148  {
149  getAttribute("priority")->setValue(new armarx::Variant(priority));
150  }
151 
152 
153  MultivariateNormalDistributionBasePtr ObjectInstance::getPositionUncertainty(const ::Ice::Current&) const
154  {
155  return MultivariateNormalDistributionBasePtr::dynamicCast(getPositionAttribute()->getUncertainty());
156  }
157 
158 
159  void ObjectInstance::setPositionUncertainty(const MultivariateNormalDistributionBasePtr& uncertainty, const ::Ice::Current&)
160  {
161  getPositionAttribute()->setValueWithUncertainty(getPositionAttribute()->getValue(), uncertainty);
162  ARMARX_DEBUG << "New position uncertainty mean: " << getPositionUncertainty()->getMean();
163  }
164 
165 
166  EntityAttributeBasePtr ObjectInstance::getOrientationAttribute() const
167  {
168  return getAttribute("orientation");
169  }
170 
171 
173  {
174  return armarx::FramedOrientationPtr::dynamicCast(getOrientationBase());
175  }
176 
177 
178  void ObjectInstance::setOrientation(const armarx::FramedOrientationBasePtr& orientation, const ::Ice::Current&)
179  {
180  putAttribute("orientation", orientation);
181  }
182 
184  {
185  auto pos = getPosition();
186  armarx::FramedPosePtr pose = new armarx::FramedPose(pos, getOrientation(), pos->frame, pos->agent);
187  return pose;
188  }
189 
190  void ObjectInstance::setPose(const armarx::FramedPoseBasePtr& newPose)
191  {
192  armarx::FramedPosePtr pose = armarx::FramedPosePtr::dynamicCast(newPose);
193  setPosition(pose->getPosition());
194  setOrientation(pose->getOrientation());
195  }
196 
197 
198  ClassProbabilityMap ObjectInstance::getClasses(const ::Ice::Current&) const
199  {
200  ClassProbabilityMap classes;
201 
202  EntityAttributeBasePtr attr = getAttribute("classes");
203  int numberValues = attr->size();
204 
205  std::pair<std::string, float> element;
206 
207  for (int v = 0 ; v < numberValues ; v++)
208  {
209  element.first = attr->getValueAt(v)->getString();
210  element.second = DiscreteProbabilityBasePtr::dynamicCast(attr->getUncertaintyAt(v))->getProbability();
211 
212  classes.insert(element);
213  }
214 
215  return classes;
216  }
217 
218 
219  void ObjectInstance::setClass(const std::string& className, float probability,
220  const Ice::Current&)
221  {
222  if (getClasses().size() == 1)
223  {
224  // Workaround: Don't change container size in case of container size one.
225  // Provents segfaults when other threads loop through the container
226  EntityAttributeBasePtr attr = getAttribute("classes");
227  attr->setValueWithUncertainty(armarx::VariantPtr(new armarx::Variant(className)),
228  ProbabilityMeasureBasePtr(new DiscreteProbability(probability)));
229  }
230  else
231  {
232  clearClasses();
233  addClass(className, probability);
234  }
235  }
236 
237 
238  void ObjectInstance::addClass(const std::string& className, float probability,
239  const Ice::Current&)
240  {
241  EntityAttributeBasePtr attr = getAttribute("classes");
242  attr->addValueWithUncertainty(armarx::VariantPtr(new armarx::Variant(className)),
243  ProbabilityMeasureBasePtr(new DiscreteProbability(probability)));
244  }
245 
246 
247  void ObjectInstance::clearClasses(const Ice::Current&)
248  {
249  EntityAttributeBasePtr attr = getAttribute("classes");
250  attr->clear();
251  }
252 
253 
254  // ********************************************************************
255  // special ObjectClass handling
256  // ********************************************************************
257  std::string ObjectInstance::getMostProbableClass(const Ice::Current&) const
258  {
259  ClassProbabilityMap classes = getClasses();
260 
261  ClassProbabilityMap::const_iterator itMax = classes.end();
262 
263  for (ClassProbabilityMap::const_iterator it = classes.begin(); it != classes.end(); ++it)
264  if (itMax == classes.end() || it->second > itMax->second)
265  {
266  itMax = it;
267  }
268 
269  return (itMax != classes.end()) ? itMax->first : "";
270  }
271 
272 
273  float ObjectInstance::getClassProbability(const std::string& className,
274  const Ice::Current&) const
275  {
276  ClassProbabilityMap classes = getClasses();
277  ClassProbabilityMap::const_iterator it = classes.find(className);
278 
279  if (it != classes.end())
280  {
281  return it->second;
282  }
283  else
284  {
285  return 0.;
286  }
287  }
288 
290  {
291  std::scoped_lock lock(wrappersMutex);
292  ARMARX_DEBUG_S << "New motion model is being set for " << this->getName() << ": " << motionModel->getMotionModelName();
293  this->motionModel = motionModel;
294  }
295 
297  {
298  std::scoped_lock lock(wrappersMutex);
299  return motionModel;
300  }
301 
303  {
304  return this->clone();
305  }
306 
307  ObjectInstancePtr ObjectInstance::clone(const Ice::Current& c) const
308  {
310  {
311  std::shared_lock lock1(entityMutex);
312  std::scoped_lock lock2(attributesMutex);
313  std::scoped_lock lock3(wrappersMutex);
314  ret = new ObjectInstance(*this);
315  ret->motionModel = this->motionModel;
316  }
317  // ret->deepCopy(*this);
318  return ret;
319  }
320 
321 
322  // ********************************************************************
323  // i / o
324  // ********************************************************************
325  void ObjectInstance::output(std::ostream& stream) const
326  {
327  Entity::output(stream);
328  }
329 
330 
331  // ********************************************************************
332  // protected members (Ice interface methods overwritten by c++ implementation)
333  // ********************************************************************
334  armarx::FramedPositionBasePtr ObjectInstance::getPositionBase(const ::Ice::Current&) const
335  {
336  return armarx::VariantPtr::dynamicCast(getPositionAttribute()->getValue())->getClass<armarx::FramedPositionBase>();
337  }
338 
339  armarx::FramedOrientationBasePtr ObjectInstance::getOrientationBase(const ::Ice::Current&) const
340  {
341  return armarx::VariantPtr::dynamicCast(getOrientationAttribute()->getValue())->getClass<armarx::FramedOrientationBase>();
342  }
343 
344  ObjectInstance::ObjectInstance() {}
345 
346 }
memoryx::ObjectInstance::hasLocalizationTimestamp
bool hasLocalizationTimestamp() const
Definition: ObjectInstance.cpp:125
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
memoryx::ObjectInstance::setLocalizationPriority
void setLocalizationPriority(Ice::Int priority, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: ObjectInstance.cpp:147
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
memoryx::ObjectInstance::getOrientation
armarx::FramedOrientationPtr getOrientation() const
Retrieve orientation of the instance.
Definition: ObjectInstance.cpp:172
memoryx::Entity::putAttribute
void putAttribute(const ::memoryx::EntityAttributeBasePtr &attr, const ::Ice::Current &=Ice::emptyCurrent) override
Store attribute in entity.
Definition: Entity.cpp:327
memoryx::Entity::getName
::std::string getName(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve name of this entity.
Definition: Entity.cpp:173
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
memoryx::Entity::getAttribute
EntityAttributeBasePtr getAttribute(const ::std::string &attrName, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve attribute from entity.
Definition: Entity.cpp:293
memoryx::ObjectInstance::getExistenceCertainty
float getExistenceCertainty(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve existence certainty for this instance.
Definition: ObjectInstance.cpp:69
armarx::TimestampVariant
Definition: TimestampVariant.h:54
AbstractObjectSerializer.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::ObjectInstance::getPositionAttribute
EntityAttributeBasePtr getPositionAttribute() const
Retrieve attribute which holds instance position.
Definition: ObjectInstance.cpp:81
memoryx::ObjectInstance
Definition: ObjectInstance.h:47
memoryx::ObjectInstance::setLocalizationTimestamp
void setLocalizationTimestamp(const IceUtil::Time &timestamp)
Definition: ObjectInstance.cpp:115
memoryx::ObjectInstance::setPose
void setPose(const armarx::FramedPoseBasePtr &newPose)
Convenience function to set position and orientation attributes at once.
Definition: ObjectInstance.cpp:190
IceUtil
Definition: Instance.h:21
memoryx::ObjectInstance::setMotionModel
void setMotionModel(AbstractMotionModelPtr motionModel)
Set the motion model for this object.
Definition: ObjectInstance.cpp:289
memoryx::ObjectInstance::getPositionUncertainty
MultivariateNormalDistributionBasePtr getPositionUncertainty(const ::Ice::Current &=Ice::emptyCurrent) const override
Get uncertainty of position estimate.
Definition: ObjectInstance.cpp:153
memoryx::Entity::output
void output(std::ostream &stream) const
Definition: Entity.cpp:409
memoryx::Entity::getAttributeValue
virtual armarx::VariantPtr getAttributeValue(const ::std::string &attrName) const
Retrieve value of an attribute from entity.
Definition: Entity.cpp:308
ViewSelection.h
memoryx::ObjectInstance::getClasses
ClassProbabilityMap getClasses(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve classes and associated class membership probability for this instance.
Definition: ObjectInstance.cpp:198
memoryx::ObjectInstance::getMotionModel
AbstractMotionModelPtr getMotionModel() const
Definition: ObjectInstance.cpp:296
memoryx::ObjectInstance::getLocalizationPriority
int getLocalizationPriority(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: ObjectInstance.cpp:138
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::ObjectInstance::addClass
void addClass(const std::string &className, float probability, const Ice::Current &=Ice::emptyCurrent) override
Adds new class membership to the class attribute.
Definition: ObjectInstance.cpp:238
memoryx::ObjectInstance::setPositionUncertainty
void setPositionUncertainty(const MultivariateNormalDistributionBasePtr &uncertainty, const ::Ice::Current &=Ice::emptyCurrent) override
Set uncertainty of position estimate.
Definition: ObjectInstance.cpp:159
memoryx::ObjectInstance::getPosition
armarx::FramedPositionPtr getPosition() const
Retrieve position of the instance.
Definition: ObjectInstance.cpp:87
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
memoryx::Entity::attributesMutex
std::mutex attributesMutex
Definition: Entity.h:504
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
memoryx::ObjectInstance::setClass
void setClass(const std::string &className, float probability, const Ice::Current &=Ice::emptyCurrent) override
Set the class attribute of the ObjectInstance.
Definition: ObjectInstance.cpp:219
memoryx::ObjectInstance::getLocalizationTimestamp
IceUtil::Time getLocalizationTimestamp() const
Definition: ObjectInstance.cpp:130
armarx::TimestampVariant::toTime
IceUtil::Time toTime()
Definition: TimestampVariant.h:116
memoryx::ObjectInstance::clearClasses
void clearClasses(const Ice::Current &=Ice::emptyCurrent) override
Clear class membership.
Definition: ObjectInstance.cpp:247
memoryx::ObjectInstance::motionModel
AbstractMotionModelPtr motionModel
Definition: ObjectInstance.h:220
armarx::FramedOrientation
The FramedOrientation class.
Definition: FramedPose.h:199
memoryx::ObjectInstance::setPosition
void setPosition(const armarx::FramedPositionBasePtr &position, const ::Ice::Current &=Ice::emptyCurrent) override
Set position of the instance.
Definition: ObjectInstance.cpp:93
memoryx::ObjectInstance::getMostProbableClass
::std::string getMostProbableClass(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve the class with highest membership probability.
Definition: ObjectInstance.cpp:257
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
memoryx::Entity::setId
void setId(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) override
Set id of this entity.
Definition: Entity.cpp:167
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
memoryx::ObjectInstance::getPose
armarx::FramedPosePtr getPose() const
Convenience function to get position and orientation attributes at once.
Definition: ObjectInstance.cpp:183
memoryx::ObjectInstance::getOrientationBase
armarx::FramedOrientationBasePtr getOrientationBase(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: ObjectInstance.cpp:339
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
ObjectInstance.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
AbstractMotionModel.h
memoryx::ObjectInstance::getOrientationAttribute
EntityAttributeBasePtr getOrientationAttribute() const
Retrieve attribute which holds instance position.
Definition: ObjectInstance.cpp:166
memoryx::Entity::entityMutex
std::shared_mutex entityMutex
Definition: Entity.h:505
memoryx::Entity::hasAttribute
bool hasAttribute(const ::std::string &attrName, const ::Ice::Current &=Ice::emptyCurrent) const override
Check whether this entity has an attribute with the given name.
Definition: Entity.cpp:339
memoryx::ObjectInstance::setExistenceCertainty
void setExistenceCertainty(float existenceCertainty, const ::Ice::Current &=Ice::emptyCurrent) override
Set existence certainty for this instance.
Definition: ObjectInstance.cpp:75
memoryx::ObjectInstance::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: ObjectInstance.cpp:302
memoryx::EntityAttribute
Attribute of MemoryX entities.
Definition: EntityAttribute.h:48
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
memoryx::ObjectInstance::clone
ObjectInstancePtr clone(const Ice::Current &c=Ice::emptyCurrent) const
Definition: ObjectInstance.cpp:307
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
memoryx::ObjectInstance::getPositionBase
armarx::FramedPositionBasePtr getPositionBase(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: ObjectInstance.cpp:334
memoryx::Entity
Definition: Entity.h:246
armarx::VariantType::DiscreteProbability
const armarx::VariantTypeId DiscreteProbability
Definition: ProbabilityMeasures.h:33
memoryx::ObjectInstance::setOrientation
void setOrientation(const armarx::FramedOrientationBasePtr &orientation, const ::Ice::Current &=Ice::emptyCurrent) override
Set orientation of the instance.
Definition: ObjectInstance.cpp:178
memoryx::Entity::setName
void setName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Set name of this entity.
Definition: Entity.cpp:179
memoryx::Entity::wrappersMutex
std::recursive_mutex wrappersMutex
Definition: Entity.h:506
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
memoryx::ObjectInstance::getClassProbability
float getClassProbability(const ::std::string &className, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve membership probability for a given class.
Definition: ObjectInstance.cpp:273