OnTopPredicateProvider.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 ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "OnTopPredicateProvider.h"
24 
28 
29 
30 using namespace memoryx;
31 
33 {
34 }
35 
36 void
38 {
39  usingProxy("WorkingMemory");
40 }
41 
42 void
44 {
45  workingMemoryProxy = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
46  objectInstancesProxy = workingMemoryProxy->getObjectInstancesSegment();
47 }
48 
49 std::string
51 {
52  return "OnTopPredicateProvider";
53 }
54 
55 PredicateInfo
57 {
58  return PredicateInfo{"ontop", 2};
59 }
60 
61 PredicateInstanceList
63 {
64  PredicateInstanceList returnPIList;
65 
66  const float maxDistanceXY = 50;
67  const float minDistanceZ = 40;
68  const float maxDistanceZ = 200;
69 
70  // get all currently localized objects and their positions from the working memory
71  std::map<std::string, armarx::FramedPositionPtr> objectPositions;
72  EntityIdList objectIds = objectInstancesProxy->getAllEntityIds();
73 
74  for (EntityIdList::const_iterator it = objectIds.begin(); it != objectIds.end(); it++)
75  {
76  const EntityBasePtr entityBase = objectInstancesProxy->getEntityById(*it);
77  const ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(entityBase);
78 
79  if (object)
80  {
81  // TODO: assert that positions are in base coordinate frame
82  objectPositions[object->getName()] = object->getPosition();
83  }
84  else
85  {
86  ARMARX_WARNING << "Something went wrong when getting the object with id " << *it
87  << " from working memory";
88  }
89  }
90 
91  // determine which object pairs are standing on one another
92  for (std::map<std::string, armarx::FramedPositionPtr>::iterator i = objectPositions.begin();
93  i != objectPositions.end();
94  i++)
95  {
96  Eigen::Vector3f pos1 = i->second->toEigen();
97  ARMARX_IMPORTANT << "Object " << i->first << " at position " << i->second->toEigen();
98  std::map<std::string, armarx::FramedPositionPtr>::iterator j = i;
99  j++;
100 
101  for (; j != objectPositions.end(); j++)
102  {
103  Eigen::Vector3f pos2 = j->second->toEigen();
104 
105  // objects have to be close to each other in the x-y-plane
106  if (sqrtf((pos1(0) - pos2(0)) * (pos1(0) - pos2(0)) +
107  (pos1(1) - pos2(1)) * (pos1(1) - pos2(1))) < maxDistanceXY)
108  {
109  // difference in z coordinate must be between min and max value
110  if (fabs(pos1(2) - pos2(2)) > minDistanceZ &&
111  fabs(pos1(2) - pos2(2)) < maxDistanceZ)
112  {
113  if (pos1(2) > pos2(2))
114  {
115  //@TODO
116  // returnPIList.push_back(PredicateInstance{"on", Ice::StringSeq{i->first, j->first}, true});
117  }
118  else
119  {
120  // returnPIList.push_back(PredicateInstance{"on", Ice::StringSeq{j->first, i->first}, true});
121  }
122  }
123  }
124  }
125  }
126 
127  return returnPIList;
128 }
memoryx::OnTopPredicateProvider::onInitWorldStateUpdater
void onInitWorldStateUpdater() override
Definition: OnTopPredicateProvider.cpp:37
OnTopPredicateProvider.h
memoryx::OnTopPredicateProvider::calcPredicates
PredicateInstanceList calcPredicates(const Ice::Current &) override
Definition: OnTopPredicateProvider.cpp:62
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
memoryx::OnTopPredicateProvider::OnTopPredicateProvider
OnTopPredicateProvider()
Definition: OnTopPredicateProvider.cpp:32
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
IceInternal::Handle< ObjectInstance >
memoryx::OnTopPredicateProvider::onConnectWorldStateUpdater
void onConnectWorldStateUpdater() override
Definition: OnTopPredicateProvider.cpp:43
MemoryXCoreObjectFactories.h
memoryx::OnTopPredicateProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: OnTopPredicateProvider.cpp:50
ObjectInstance.h
memoryx::OnTopPredicateProvider::getPredicateInfo
PredicateInfo getPredicateInfo(const Ice::Current &c=Ice::emptyCurrent)
Definition: OnTopPredicateProvider.cpp:56
MemoryXTypesObjectFactories.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193