icub-client
object.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 WYSIWYD Consortium, European Commission FP7 Project ICT-612139
3  * Authors: Stéphane Lallée
4  * email: stephane.lallee@gmail.com
5  * website: https://github.com/robotology/icub-client/
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * icub-client/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 
21 #include <math.h>
22 
23 using namespace std;
24 using namespace yarp::os;
25 using namespace yarp::sig;
26 using namespace icubclient;
27 
28 Object::Object():Entity()
29 {
31  m_ego_position.resize(3,0.0);
32  m_ego_orientation.resize(3,0.0);
33  m_dimensions.resize(3,0.1);
34  m_color.resize(3,255.0);
35  m_saliency = 0.0;
36  m_value = 0.0;
37  m_present = 0.0;
39 
40  // by default, place object 25cm in front of the robot
41  // to avoid collisions when pointing to an object
42  m_ego_position[0] = -0.25;
43 }
44 
46 {
47  this->m_entity_type = b.m_entity_type;
48  this->m_color = b.m_color;
49  this->m_dimensions = b.m_dimensions;
52  this->m_present = b.m_present;
53  this->m_saliency = b.m_saliency;
54  this->m_objectarea = b.m_objectarea;
55  this->m_value = b.m_value;
56 }
57 double Object::value() const
58 {
59  return m_value;
60 }
61 
62 void Object::setValue(double value)
63 {
64  m_value = value;
65 }
66 
67 double Object::saliency() const
68 {
69  return m_saliency;
70 }
71 
73 {
75 }
76 
77 
78 Bottle Object::asBottle() const
79 {
80  //Get the entity bottle
81  Bottle b = this->Entity::asBottle();
82 
83  Bottle bSub;
84  //Add Object specific properties
85  //Position
87  bSub.addDouble(m_ego_position[0]);
88  b.addList() = bSub;
89  bSub.clear();
91  bSub.addDouble(m_ego_position[1]);
92  b.addList() = bSub;
93  bSub.clear();
95  bSub.addDouble(m_ego_position[2]);
96  b.addList() = bSub;
97  bSub.clear();
98  bSub.addString(ICUBCLIENT_OPC_OBJECT_ROBOTPOS_TAG);
99  Vector m_ego_copy(m_ego_position);
100  bSub.addList().read(m_ego_copy);
101  b.addList() = bSub;
102  bSub.clear();
103 
104  //Orientation
105  bSub.addString(ICUBCLIENT_OPC_OBJECT_ROBOTORX_TAG);
106  bSub.addDouble(m_ego_orientation[0]);
107  b.addList() = bSub;
108  bSub.clear();
109  bSub.addString(ICUBCLIENT_OPC_OBJECT_ROBOTORY_TAG);
110  bSub.addDouble(m_ego_orientation[1]);
111  b.addList() = bSub;
112  bSub.clear();
113  bSub.addString(ICUBCLIENT_OPC_OBJECT_ROBOTORZ_TAG);
114  bSub.addDouble(m_ego_orientation[2]);
115  b.addList() = bSub;
116  bSub.clear();
117 
118  //Dimension
119  bSub.addString(ICUBCLIENT_OPC_OBJECT_RTDIMX_TAG);
120  bSub.addDouble(m_dimensions[0]);
121  b.addList() = bSub;
122  bSub.clear();
123  bSub.addString(ICUBCLIENT_OPC_OBJECT_RTDIMY_TAG);
124  bSub.addDouble(m_dimensions[1]);
125  b.addList() = bSub;
126  bSub.clear();
127  bSub.addString(ICUBCLIENT_OPC_OBJECT_RTDIMZ_TAG);
128  bSub.addDouble(m_dimensions[2]);
129  b.addList() = bSub;
130  bSub.clear();
131 
132  //Color
133  bSub.addString(ICUBCLIENT_OPC_OBJECT_GUI_COLOR_R);
134  bSub.addDouble(m_color[0]);
135  b.addList() = bSub;
136  bSub.clear();
137  bSub.addString(ICUBCLIENT_OPC_OBJECT_GUI_COLOR_G);
138  bSub.addDouble(m_color[1]);
139  b.addList() = bSub;
140  bSub.clear();
141  bSub.addString(ICUBCLIENT_OPC_OBJECT_GUI_COLOR_B);
142  bSub.addDouble(m_color[2]);
143  b.addList() = bSub;
144  bSub.clear();
145  bSub.addString(ICUBCLIENT_OPC_OBJECT_SALIENCY);
146  bSub.addDouble(m_saliency);
147  b.addList() = bSub;
148  bSub.clear();
149  bSub.addString(ICUBCLIENT_OPC_OBJECT_VALUE);
150  bSub.addDouble(m_value);
151  b.addList() = bSub;
152  bSub.clear();
153 
154  //Present
155  bSub.addString(ICUBCLIENT_OPC_OBJECT_PRESENT_TAG);
156  bSub.addDouble(m_present);
157  b.addList() = bSub;
158  bSub.clear();
159 
160  //ObjectArea
161  bSub.addString("object_area");
162  bSub.addInt(static_cast<int>(m_objectarea));
163  b.addList() = bSub;
164  bSub.clear();
165 
166  return b;
167 }
168 
169 bool Object::fromBottle(const Bottle &b)
170 {
171  if (!this->Entity::fromBottle(b))
172  return false;
173 
174  if (!b.check(ICUBCLIENT_OPC_OBJECT_ROBOTPOSX_TAG) ||
187  !b.check("object_area") ||
188  !b.check(ICUBCLIENT_OPC_OBJECT_VALUE))
189  {
190  return false;
191  }
192 
193  m_ego_position[0] = b.find(ICUBCLIENT_OPC_OBJECT_ROBOTPOSX_TAG).asDouble();
194  m_ego_position[1] = b.find(ICUBCLIENT_OPC_OBJECT_ROBOTPOSY_TAG).asDouble();
195  m_ego_position[2] = b.find(ICUBCLIENT_OPC_OBJECT_ROBOTPOSZ_TAG).asDouble();
196  m_dimensions[0] = b.find(ICUBCLIENT_OPC_OBJECT_RTDIMX_TAG).asDouble();
197  m_dimensions[1] = b.find(ICUBCLIENT_OPC_OBJECT_RTDIMY_TAG).asDouble();
198  m_dimensions[2] = b.find(ICUBCLIENT_OPC_OBJECT_RTDIMZ_TAG).asDouble();
202  m_color[0] = b.find(ICUBCLIENT_OPC_OBJECT_GUI_COLOR_R).asDouble();
203  m_color[1] = b.find(ICUBCLIENT_OPC_OBJECT_GUI_COLOR_G).asDouble();
204  m_color[2] = b.find(ICUBCLIENT_OPC_OBJECT_GUI_COLOR_B).asDouble();
205  m_saliency = b.find(ICUBCLIENT_OPC_OBJECT_SALIENCY).asDouble();
206  m_value = b.find(ICUBCLIENT_OPC_OBJECT_VALUE).asDouble();
207  m_present = b.find(ICUBCLIENT_OPC_OBJECT_PRESENT_TAG).asDouble();
208  m_objectarea = static_cast<ObjectArea>(b.find("object_area").asInt());
209 
210  return true;
211 }
212 
213 string Object::toString() const
214 {
215  std::ostringstream oss;
216  oss<< this->Entity::toString();
217  oss<<"self xyz : \t";
218  oss<< m_ego_position.toString(3,3)<<endl;
219  oss<<"self rpy: \t";
220  oss<< m_ego_orientation.toString(3,3)<<endl;
221  oss<<"size : \t \t";
222  oss<< m_dimensions.toString(3,3)<<endl;
223  oss<<"color : \t";
224  oss<< m_color.toString(3,3)<<endl;
225  oss<<"saliency : \t";
226  oss<< m_saliency<<endl;
227  oss<<"value : \t";
228  oss<< m_value<<endl;
229  oss<<"present : \t";
230  oss<< m_present<<endl;
231  oss<<"object area : \t";
232  oss<< static_cast<int>(m_objectarea)<<endl;
233  return oss.str();
234 }
235 
236 Vector Object::getSelfRelativePosition(const Vector &vInitialRoot) const
237 {
238  Vector targetAbsolute(4,1.0);
239  targetAbsolute.setSubvector(0,vInitialRoot);
240  Vector targetRelative(4,1.0);
241 
242  //Translation
243  targetAbsolute[0] = targetAbsolute[0] - this->m_ego_position[0];
244  targetAbsolute[1] = targetAbsolute[1] - this->m_ego_position[1];
245 
246  //Rotation
247  double theta = this->m_ego_orientation[2] * M_PI/180.0;
248  targetRelative[0] = targetAbsolute[0] * cos(theta) + targetAbsolute[1]*sin(theta);
249  targetRelative[1] = -targetAbsolute[0]*sin(theta) + targetAbsolute[1]*cos(theta);
250  targetRelative[2] = targetAbsolute[2];
251  return targetRelative;
252 }
253 
254 std::string Object::objectAreaAsString(const ObjectArea &o) {
255  if(o==ObjectArea::HUMAN) {
256  return "HumanOnly";
257  } else if(o==ObjectArea::ROBOT) {
258  return "RobotOnly";
259  } else if(o==ObjectArea::SHARED) {
260  return "Shared";
261  } else if(o==ObjectArea::NOTREACHABLE) {
262  return "NotReachable";
263  } else {
264  yError() << "Something went wrong in objectAreaAsString()";
265  return "NULL";
266  }
267 }
268 
269 ObjectArea Object::stringToObjectArea(const std::string &o) {
270  if(o=="HumanOnly") {
271  return ObjectArea::HUMAN;
272  } else if(o=="RobotOnly") {
273  return ObjectArea::ROBOT;
274  } else if(o=="Shared") {
275  return ObjectArea::SHARED;
276  } else if(o=="NotReachable") {
278  } else {
279  yError() << "Something went wrong in stringToObjectArea()";
281  }
282 }
yarp::sig::VectorOf< double > m_color
Mean color of the object (r,g,b) used mainly for debugging/displaying purposes in the iCubGUI...
Definition: object.h:61
#define ICUBCLIENT_OPC_OBJECT_ROBOTORX_TAG
Definition: tags.h:54
virtual bool fromBottle(const yarp::os::Bottle &b)
Fill entity fields from a bottle representation.
Definition: entity.cpp:95
Represent any entity that can be stored within the OPC.
Definition: entity.h:40
std::string objectAreaAsString() const
Definition: object.h:106
yarp::sig::VectorOf< double > m_ego_orientation
Orientation of the Object, in the initial ego-centered reference frame of the agent mainting the OPC ...
Definition: object.h:51
#define ICUBCLIENT_OPC_OBJECT_ROBOTPOSY_TAG
Definition: tags.h:52
#define ICUBCLIENT_OPC_OBJECT_GUI_COLOR_G
Definition: tags.h:63
double value() const
Get value of object.
Definition: object.cpp:57
ObjectArea
Definition: object.h:28
#define ICUBCLIENT_OPC_OBJECT_GUI_COLOR_R
Definition: tags.h:62
static icubclient::ObjectArea stringToObjectArea(const std::string &o)
Definition: object.cpp:269
void setSaliency(double saliency)
Set saliency of object.
Definition: object.cpp:72
double m_present
Is the object present in the scene A value of 1.0 means that the object currently is in the scene A v...
Definition: object.h:69
#define ICUBCLIENT_OPC_OBJECT_ROBOTORZ_TAG
Definition: tags.h:56
#define ICUBCLIENT_OPC_OBJECT_RTDIMZ_TAG
Definition: tags.h:59
yarp::sig::VectorOf< double > getSelfRelativePosition(const yarp::sig::VectorOf< double > &vInitialRoot) const
Express a point given in the initial ego-centered reference frame in respect to the object reference ...
Definition: object.cpp:236
STL namespace.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
Definition: object.h:35
yarp::sig::VectorOf< double > m_ego_position
Position of the Object, in the initial ego-centered reference frame of the agent mainting the OPC (in...
Definition: object.h:46
virtual bool fromBottle(const yarp::os::Bottle &b)
Fill entity fields from a bottle representation.
Definition: object.cpp:169
yarp::sig::VectorOf< double > m_dimensions
Dimensions of the Object, in meters.
Definition: object.h:56
#define ICUBCLIENT_OPC_OBJECT_RTDIMX_TAG
Definition: tags.h:57
#define ICUBCLIENT_OPC_OBJECT_VALUE
Definition: tags.h:47
#define ICUBCLIENT_OPC_OBJECT_ROBOTPOSZ_TAG
Definition: tags.h:53
virtual std::string toString() const
Return a human readable description of the entity.
Definition: entity.cpp:116
#define ICUBCLIENT_OPC_OBJECT_PRESENT_TAG
Definition: tags.h:45
void setValue(double value)
Set value of object.
Definition: object.cpp:62
virtual std::string toString() const
Return a human readable description of the entity.
Definition: object.cpp:213
ObjectArea m_objectarea
Whether the object is accessible by only the robot, only the human, both or neither agent...
Definition: object.h:79
#define ICUBCLIENT_OPC_ENTITY_OBJECT
Definition: tags.h:38
virtual yarp::os::Bottle asBottle() const
Return the entity as a bottle.
Definition: entity.cpp:45
double m_saliency
A measurement of the object saliency [0,1].
Definition: object.h:74
virtual yarp::os::Bottle asBottle() const
Return the entity as a bottle.
Definition: object.cpp:78
#define ICUBCLIENT_OPC_OBJECT_SALIENCY
Definition: tags.h:46
#define ICUBCLIENT_OPC_OBJECT_RTDIMY_TAG
Definition: tags.h:58
double m_value
A measurement of the object subjective value [0,1].
Definition: object.h:84
double saliency() const
Get saliency of object.
Definition: object.cpp:67
#define ICUBCLIENT_OPC_OBJECT_ROBOTORY_TAG
Definition: tags.h:55
#define ICUBCLIENT_OPC_OBJECT_GUI_COLOR_B
Definition: tags.h:64
#define ICUBCLIENT_OPC_OBJECT_ROBOTPOSX_TAG
Definition: tags.h:51
#define ICUBCLIENT_OPC_OBJECT_ROBOTPOS_TAG
Definition: tags.h:50
std::string m_entity_type
Definition: entity.h:55