Object.cpp
Go to the documentation of this file.
1 #include "Object.h"
2 #include "parameters.h"
3 #include <boost/lexical_cast.hpp>
4 
5 #include <Eigen/Geometry>
7 
8 namespace hector_object_tracker {
9 
10 std::map<std::string,unsigned int> Object::object_count;
11 std::string Object::object_namespace;
12 
13 Object::Object(const std::string class_id, const std::string object_id)
14 {
15  if (!class_id.empty()) {
16  this->info.class_id = class_id;
17  } else {
18  this->info.class_id = "object";
19  }
20 
21  if (!object_id.empty()) {
22  this->info.object_id = object_id;
23  } else {
24  this->info.object_id = this->info.class_id + "_" + boost::lexical_cast<std::string>(object_count[this->info.class_id]++);
25  }
26 
27  // if (this->info.object_id[0] != '/') this->info.object_id = object_namespace + "/" + this->info.object_id;
28 }
29 
30 Object::Object(const hector_worldmodel_msgs::Object& other) {
31  *this = other;
32 }
33 
35 {}
36 
37 void Object::reset() {
38  object_count.clear();
39 }
40 
41 void Object::getMessage(hector_worldmodel_msgs::Object& object) const {
42  object.header = this->header;
43  this->getPoseWithCovariance(object.pose);
44  object.info = this->info;
45  object.state = this->state;
46 }
47 
48 hector_worldmodel_msgs::Object Object::getMessage() const {
49  hector_worldmodel_msgs::Object object;
50  getMessage(object);
51  return object;
52 }
53 
54 void Object::getPoseWithCovariance(geometry_msgs::PoseWithCovariance& pose) const {
55  getPose(pose.pose);
56  getCovariance(pose.covariance);
57 }
58 
59 void Object::setPose(const geometry_msgs::PoseWithCovariance& pose) {
60  setPose(pose.pose);
61  setCovariance(pose.covariance);
62 }
63 
64 void Object::getPose(geometry_msgs::Pose& pose) const
65 {
66  pose.position.x = this->position.x();
67  pose.position.y = this->position.y();
68  pose.position.z = this->position.z();
69  pose.orientation.w = this->orientation.w();
70  pose.orientation.x = this->orientation.x();
71  pose.orientation.y = this->orientation.y();
72  pose.orientation.z = this->orientation.z();
73 }
74 
75 void Object::getPose(tf::Pose& pose) const
76 {
77  pose.getOrigin().setValue(this->position.x(), this->position.y(), this->position.z());
78  pose.getBasis().setRotation(tf::Quaternion(this->orientation.x(), this->orientation.y(), this->orientation.z(), this->orientation.w()));
79 }
80 
81 void Object::setPose(const geometry_msgs::Pose& pose)
82 {
83  setPosition(pose.position);
84  setOrientation(pose.orientation);
85 }
86 
87 void Object::setPose(const tf::Pose &pose)
88 {
89  setPosition(pose.getOrigin());
90  tf::Quaternion rot;
91  pose.getBasis().getRotation(rot);
92  setOrientation(rot);
93 }
94 
95 const Eigen::Vector3f& Object::getPosition() const {
96  return position;
97 }
98 
99 void Object::setPosition(const Eigen::Vector3f& position) {
100  this->position = position;
101 }
102 
103 void Object::setPosition(const geometry_msgs::Point& position) {
104  this->position << position.x, position.y, position.z;
105 }
106 
108 {
109  this->position << position.x(), position.y(), position.z();
110 }
111 
112 const Eigen::Quaternionf& Object::getOrientation() const
113 {
114  return this->orientation;
115 }
116 
117 void Object::setOrientation(const geometry_msgs::Quaternion& orientation)
118 {
119  this->orientation.coeffs() << orientation.x, orientation.y, orientation.z, orientation.w;
120 }
121 
123 {
124  this->orientation.coeffs() << orientation.x(), orientation.y(), orientation.z(), orientation.w();
125 }
126 
127 const Eigen::Matrix3f& Object::getCovariance() const {
128  return covariance;
129 }
130 
131 void Object::getCovariance(geometry_msgs::PoseWithCovariance::_covariance_type& covariance) const {
132  covariance[0] = this->covariance(0,0);
133  covariance[1] = this->covariance(0,1);
134  covariance[2] = this->covariance(0,2);
135  covariance[6] = this->covariance(1,0);
136  covariance[7] = this->covariance(1,1);
137  covariance[8] = this->covariance(1,2);
138  covariance[12] = this->covariance(2,0);
139  covariance[13] = this->covariance(2,1);
140  covariance[14] = this->covariance(2,2);
141 }
142 
143 void Object::setCovariance(const Eigen::Matrix3f& eigen) {
144  this->covariance = eigen;
145 }
146 
148  this->covariance << tf[0][0], tf[0][1], tf[0][2],
149  tf[1][0], tf[1][1], tf[1][2],
150  tf[2][0], tf[2][1], tf[2][2];
151 }
152 
153 void Object::setCovariance(const geometry_msgs::PoseWithCovariance::_covariance_type& covariance) {
154  this->covariance << covariance[0], covariance[1], covariance[2],
155  covariance[6], covariance[7], covariance[8],
156  covariance[12], covariance[13], covariance[14];
157 }
158 
160  if (this->state.state == state) return;
161  ROS_INFO("Setting object state for %s to %s", getObjectId().c_str(), hector_worldmodel_msgs::getObjectStateString(state));
162  this->state.state = state;
163 }
164 
165 void Object::intersect(const tf::Pose& poseB, const Eigen::Matrix3f& covarianceB, float support) {
166  Eigen::Vector3f positionB(poseB.getOrigin().x(), poseB.getOrigin().y(), poseB.getOrigin().z());
167  tf::Quaternion orientationB = poseB.getRotation();
168  // old cov/covariance is A , new cov/covIn is B
169  float omega = 0.5f;
170 
171  Eigen::Matrix3f A(covariance.inverse() * omega);
172  Eigen::Matrix3f B(covarianceB.inverse() * (1.0f - omega));
173  double infA = 1./covariance.trace();
174  double infB = 1./covarianceB.trace();
175 
176  covariance = (A + B).inverse();
177  position = covariance * (A * position + B * positionB);
178  updateOrientation(orientationB, infB / (infA + infB));
179 
180  setPosition(position);
182  addSupport(support);
183 }
184 
185 void Object::update(const tf::Pose& poseB, const Eigen::Matrix3f& covarianceB, float support) {
186  Eigen::Vector3f positionB(poseB.getOrigin().x(), poseB.getOrigin().y(), poseB.getOrigin().z());
187  tf::Quaternion orientationB = poseB.getRotation();
188  // old information is A , new information is B
189 
190  Eigen::Matrix3f A(covariance.inverse());
191  Eigen::Matrix3f B(covarianceB.inverse());
192  double infA = 1./covariance.trace();
193  double infB = 1./covarianceB.trace();
194 
195  covariance = (A + B).inverse();
196  position = covariance * (A * position + B * positionB);
197  updateOrientation(orientationB, infB / (infA + infB));
198 
199  setPosition(position);
201  addSupport(support);
202 }
203 
204 void Object::updateOrientation(const tf::Quaternion& orientationB, double slerp_factor) {
205  // update orientation of objects using low-pass filtering
208  q.slerp(orientationB, slerp_factor);
209  setOrientation(q);
210  }
211  // or simply set new orientation
212  else {
213  setOrientation(orientationB);
214  }
215 }
216 
217 void Object::getVisualization(visualization_msgs::MarkerArray &markers) const {
218  visualization_msgs::Marker marker;
219  std::string postfix;
220 
221  // default color
222  marker.color = parameter(_marker_color, this->info.class_id);
223 
224  switch(this->state.state) {
225  case hector_worldmodel_msgs::ObjectState::CONFIRMED:
226  marker.color.r = 0.0;
227  marker.color.g = 0.8;
228  marker.color.b = 0.0;
229  postfix = " (CONFIRMED)";
230  break;
231  case hector_worldmodel_msgs::ObjectState::DISCARDED:
232  marker.color.a = 0.5;
233  postfix = " (DISCARDED)";
234  break;
235  case hector_worldmodel_msgs::ObjectState::INACTIVE:
236  marker.color.a = 0.5;
237  postfix = " (INACTIVE)";
238  break;
239  case hector_worldmodel_msgs::ObjectState::UNKNOWN:
240  marker.color.a = 0.5;
241  break;
242  case hector_worldmodel_msgs::ObjectState::PENDING:
243  marker.color.a = 0.5;
244  postfix = " (PENDING)";
245  break;
246  default:
247  break;
248  }
249 
250  marker.header = this->header;
251  marker.action = visualization_msgs::Marker::ADD;
252  getPose(marker.pose);
253  marker.ns = this->info.class_id;
254 
255  marker.type = visualization_msgs::Marker::ARROW;
256  marker.scale.x = 1.0;
257  marker.scale.y = 1.0;
258  marker.scale.z = 1.0;
259  // markers.markers.push_back(marker);
260 
261  marker.type = visualization_msgs::Marker::SPHERE;
262  marker.scale.x = 0.1;
263  marker.scale.y = 0.1;
264  marker.scale.z = 0.1;
265  markers.markers.push_back(marker);
266 
267  marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
268  marker.text = (!this->info.name.empty() ? this->info.name : this->info.object_id) + postfix;
269  marker.scale.x = 0.0;
270  marker.scale.y = 0.0;
271  marker.scale.z = 0.1;
272  marker.pose.position.z += 1.5 * marker.scale.z;
273  markers.markers.push_back(marker);
274 }
275 
276 void Object::setNamespace(const std::string &ns) {
277  object_namespace = ns;
278 }
279 
280 Object& Object::operator =(const hector_worldmodel_msgs::Object& other) {
281  header = other.header;
282  info = other.info;
283  state = other.state;
284  setPose(other.pose);
285  return *this;
286 }
287 
288 ObjectPtr Object::transform(tf::Transformer& tf, const std::string& target_frame) const
289 {
290  return transform(tf, target_frame, this->header.stamp);
291 }
292 
293 ObjectPtr Object::transform(tf::Transformer& tf, const std::string& target_frame, const ros::Time& target_time) const
294 {
296  tf.lookupTransform(target_frame, this->header.frame_id, target_time, transform);
297 
298  ObjectPtr result(new Object(*this));
299 
300  tf::Pose pose;
301  this->getPose(pose);
302 
303  // transform pose
304  pose = transform * pose;
305  result->setPose(pose);
306 
307  // rotate covariance matrix
308  tf::Matrix3x3 rotation(transform.getBasis());
309  tf::Matrix3x3 cov(covariance(0,0), covariance(0,1), covariance(0,2),
310  covariance(1,0), covariance(1,1), covariance(1,2),
311  covariance(2,0), covariance(2,1), covariance(2,2));
312  result->setCovariance(rotation * cov * rotation.transpose());
313 
314  // set new frame_id
315  result->header.frame_id = target_frame;
316 
317  return result;
318 }
319 
320 double Object::getDistance(const Object &other)
321 {
322  return (this->getPosition() - other.getPosition()).norm();
323 }
324 
325 } // namespace hector_object_tracker
void getPose(geometry_msgs::Pose &pose) const
Definition: Object.cpp:64
std_msgs::Header header
Definition: Object.h:124
Eigen::Matrix3f covariance
Definition: Object.h:130
void setPosition(const Eigen::Vector3f &position)
Definition: Object.cpp:99
void update(const tf::Pose &poseB, const Eigen::Matrix3f &covarianceB, float support)
Definition: Object.cpp:185
ObjectPtr transform(tf::Transformer &tf, const std::string &target_frame) const
Definition: Object.cpp:288
Eigen::Vector3f position
Definition: Object.h:128
TFSIMD_FORCE_INLINE Matrix3x3 & getBasis()
void intersect(const tf::Pose &poseB, const Eigen::Matrix3f &covarianceB, float support)
Definition: Object.cpp:165
const Eigen::Matrix3f & getCovariance() const
Definition: Object.cpp:127
const Eigen::Quaternionf & getOrientation() const
Definition: Object.cpp:112
std::map< std::string, std_msgs::ColorRGBA > _marker_color
Definition: parameters.cpp:54
Object(const std::string class_id="", const std::string object_id="")
Definition: Object.cpp:13
std::map< std::string, bool > _with_orientation
Definition: parameters.cpp:41
static void setNamespace(const std::string &ns)
Definition: Object.cpp:276
Quaternion slerp(const Quaternion &q, const tfScalar &t) const
TFSIMD_FORCE_INLINE void setValue(const tfScalar &x, const tfScalar &y, const tfScalar &z)
void getRotation(Quaternion &q) const
void setRotation(const Quaternion &q)
const std::string & getClassId() const
Definition: Object.h:59
void setOrientation(const geometry_msgs::Quaternion &orientation)
Definition: Object.cpp:117
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setState(const StateType &state)
Definition: Object.cpp:159
hector_worldmodel_msgs::ObjectState::_state_type StateType
Definition: Object.h:24
TFSIMD_FORCE_INLINE const tfScalar & z() const
const Eigen::Vector3f & getPosition() const
Definition: Object.cpp:95
#define ROS_INFO(...)
TFSIMD_FORCE_INLINE Quaternion inverse(const Quaternion &q)
TFSIMD_FORCE_INLINE const tfScalar & y() const
hector_worldmodel_msgs::Object getMessage() const
Definition: Object.cpp:48
static const char * getObjectStateString(const ObjectState::_state_type &state)
Object & operator=(const hector_worldmodel_msgs::Object &other)
Definition: Object.cpp:280
Eigen::Quaternionf orientation
Definition: Object.h:129
static std::map< std::string, unsigned int > object_count
Definition: Object.h:132
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
void updateOrientation(const tf::Quaternion &orientationB, double slerp_factor)
Definition: Object.cpp:204
static T & parameter(std::map< std::string, T > &p, const std::string &class_id=std::string())
Definition: parameters.h:68
const std::string & getObjectId() const
Definition: Object.h:63
void getPoseWithCovariance(geometry_msgs::PoseWithCovariance &pose) const
Definition: Object.cpp:54
Quaternion getRotation() const
void getVisualization(visualization_msgs::MarkerArray &markers) const
Definition: Object.cpp:217
double getDistance(const Object &other)
Definition: Object.cpp:320
static std::string object_namespace
Definition: Object.h:133
void setPose(const geometry_msgs::PoseWithCovariance &pose)
Definition: Object.cpp:59
void addSupport(float support)
Definition: Object.h:79
void setCovariance(const Eigen::Matrix3f &covariance)
Definition: Object.cpp:143


hector_object_tracker
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 13:35:13