ed
entity.cpp
Go to the documentation of this file.
1 #include "ed/entity.h"
2 
3 #include "ed/measurement.h"
4 
5 #include <geolib/Shape.h>
6 #include <geolib/Mesh.h>
7 
8 #include "ed/convex_hull_calc.h"
9 
10 // ----------------------------------------------------------------------------------------------------
11 
12 namespace ed
13 {
14 
15 // ----------------------------------------------------------------------------------------------------
16 
17 Entity::Entity(const UUID& id, const TYPE& type, const unsigned int& measurement_buffer_size) :
18  id_(id),
19  revision_(0),
20  type_(type),
21  existence_prob_(1.0),
22  last_update_timestamp_(0),
23  measurements_(measurement_buffer_size),
24  measurements_seq_(0),
25  visual_revision_(0),
26  collision_revision_(0),
27  volumes_revision_(0),
28 // creation_time_(creation_time),
29  has_pose_(false),
30  pose_(geo::Pose3D::identity())
31 {
32 }
33 
34 // ----------------------------------------------------------------------------------------------------
35 
37 {
38 // std::cout << "Removing entity with ID: " << id_ << std::endl;
39 }
40 
41 // ----------------------------------------------------------------------------------------------------
42 
44 {
45  if (convex_hull_map_.empty())
46  {
47  convex_hull_new_.points.clear();
48  return;
49  }
50 
52  const MeasurementConvexHull& m = it->second;
53 
54  if (convex_hull_map_.size() == 1)
55  {
57  pose_ = m.pose;
58  has_pose_ = true;
59 
60  return;
61  }
62 
63  float z_min = m.convex_hull.z_min + m.pose.t.z;
64  float z_max = m.convex_hull.z_max + m.pose.t.z;
65 
66  ++it;
67 
69  for(; it != convex_hull_map_.end(); ++it)
70  {
71  const MeasurementConvexHull& m = it->second;
72  z_min = std::min<float>(z_min, m.convex_hull.z_min + m.pose.t.z);
73  z_max = std::max<float>(z_max, m.convex_hull.z_max + m.pose.t.z);
74 
75  geo::Vec2f offset(m.pose.t.x, m.pose.t.y);
76 
77  for(unsigned int i = 0; i < m.convex_hull.points.size(); ++i)
78  points.push_back(m.convex_hull.points[i] + offset);
79  }
80 
81  ed::convex_hull::create(points, z_min, z_max, convex_hull_new_, pose_);
82 
83  has_pose_ = true;
84 }
85 
86 // ----------------------------------------------------------------------------------------------------
87 
89 {
90  const std::vector<geo::Vector3>& vertices = visual_->getMesh().getPoints();
91 
92  if (vertices.empty())
93  return;
94 
95  float z_min = 1e9;
96  float z_max = -1e9;
97 
98  std::vector<geo::Vec2f> points(vertices.size());
99  for(unsigned int i = 0; i < vertices.size(); ++i)
100  {
101 // geo::Vector3 p_MAP = pose_ * vertices[i];
102  // old implementation, this is correct, but gives the wrong result with the rest of the code
103  // Because it is too much work for now to change that. So therefore ignoring rotation.
104  geo::Vector3 p_MAP = pose_.t + vertices[i];
105  // new implementation, not correct either. Because this creates the wrong output in case of other rotation,
106  // than arround z-axis. But solves the main issue, rotation of convex hull is in the wrong frame.
107  // ToDo: Make sure everything in stamped correctly. Then conversion are much easier.
108  z_min = std::min<float>(z_min, p_MAP.z - pose_.t.z);
109  z_max = std::max<float>(z_max, p_MAP.z - pose_.t.z);
110 
111  points[i] = geo::Vec2f(p_MAP.x - pose_.t.x, p_MAP.y - pose_.t.y);
112  }
113 
114  convex_hull::createAbsolute(points, z_min, z_max, convex_hull_new_);
115 }
116 
117 // ----------------------------------------------------------------------------------------------------
118 
120 {
121  if (visual_ != visual)
122  {
124  visual_ = visual;
125 
127  }
128 }
129 
130 // ----------------------------------------------------------------------------------------------------
131 
133 {
134  if (collision_ != collision)
135  {
138  }
139 }
140 
141 
142 // ----------------------------------------------------------------------------------------------------
143 
145 {
146  // Push back the measurement
147  measurements_.push_front(measurement);
149 
150  // Update beste measurement
151  if (best_measurement_)
152  {
153  if (measurement->imageMask().getSize() > best_measurement_->imageMask().getSize()
154  || (measurement->mask() && best_measurement_->mask() && measurement->mask()->size() > best_measurement_->mask()->size()))
155  best_measurement_ = measurement;
156  }
157  else
158  {
159  best_measurement_ = measurement;
160  }
161 }
162 
163 // ----------------------------------------------------------------------------------------------------
164 
165 void Entity::measurements(std::vector<MeasurementConstPtr>& measurements, double min_timestamp) const
166 {
167  for(boost::circular_buffer<MeasurementConstPtr>::const_iterator it = measurements_.begin(); it != measurements_.end(); ++it)
168  {
169  const MeasurementConstPtr& m = *it;
170  if (m->timestamp() > min_timestamp)
171  measurements.push_back(m);
172  }
173 }
174 
175 
176 // ----------------------------------------------------------------------------------------------------
177 
178 void Entity::measurements(std::vector<MeasurementConstPtr>& measurements, unsigned int num) const
179 {
180  for(unsigned int i = 0; i < num && i < measurements_.size(); ++i)
181  {
182  measurements.push_back(measurements_[i]);
183  }
184 }
185 
186 // ----------------------------------------------------------------------------------------------------
187 
189 {
190  if (measurements_.empty())
191  return MeasurementConstPtr();
192 
193  return measurements_.front();
194 }
195 
196 // ----------------------------------------------------------------------------------------------------
197 
199  static const char alphanum[] =
200  "0123456789"
201  "abcdef";
202 
203  std::string s;
204  for (int i = 0; i < 32; ++i) {
205  int n = rand() / (RAND_MAX / (sizeof(alphanum) - 1) + 1);
206  s += alphanum[n];
207  }
208 
209  return UUID(s);
210 }
211 
212 }
geo::Vector3::y
const real & y() const
convex_hull_calc.h
ed::ConvexHull::z_min
float z_min
Definition: convex_hull.h:16
std::string
std::shared_ptr
ed::Entity::setCollision
void setCollision(const geo::ShapeConstPtr &collision)
Definition: entity.cpp:132
geo
entity.h
ed::convex_hull::create
void create(const std::vector< geo::Vec2f > &points, float z_min, float z_max, ConvexHull &chull, geo::Pose3D &pose)
create fill a ConvexHull and put its origin in the middle of the Convexhull
Definition: convex_hull_calc.cpp:21
std::vector
std::vector::size
T size(T... args)
Pose3D
Transform3 Pose3D
Shape.h
ed::Entity::pose_
geo::Pose3D pose_
Definition: entity.h:259
ed::Entity::measurements_seq_
unsigned int measurements_seq_
Definition: entity.h:246
geo::Vec2f
Vec2T< float > Vec2f
std::vector::push_back
T push_back(T... args)
ed::Entity::updateConvexHull
void updateConvexHull()
Definition: entity.cpp:43
geo::Vec3T::y
T y
ed::MeasurementConvexHull::convex_hull
ConvexHull convex_hull
Definition: measurement_convex_hull.h:12
measurement.h
ed::Entity::convex_hull_map_
std::map< std::string, MeasurementConvexHull > convex_hull_map_
Definition: entity.h:255
ed::Entity::collision_
geo::ShapeConstPtr collision_
Definition: entity.h:249
ed::MeasurementConvexHull
Definition: measurement_convex_hull.h:10
geo::Transform3T::t
Vec3T< T > t
ed::UUID
Definition: uuid.h:10
geo::Vector3
std::map
ed::Entity::measurements
void measurements(std::vector< MeasurementConstPtr > &measurements, double min_timestamp=0) const
Definition: entity.cpp:165
ed::Entity::measurements_
boost::circular_buffer< MeasurementConstPtr > measurements_
Definition: entity.h:244
geo::Vector3::z
const real & z() const
ed::MeasurementConstPtr
shared_ptr< const Measurement > MeasurementConstPtr
Definition: types.h:33
ed::Entity::visual_
geo::ShapeConstPtr visual_
Definition: entity.h:248
Mesh.h
ed::Entity::generateID
static UUID generateID()
Definition: entity.cpp:198
ed::Entity::~Entity
~Entity()
Definition: entity.cpp:36
ed::Entity::addMeasurement
void addMeasurement(MeasurementConstPtr measurement)
Definition: entity.cpp:144
ed::Entity::has_pose_
bool has_pose_
Definition: entity.h:258
ed::Entity::visual_revision_
unsigned long visual_revision_
Definition: entity.h:251
ed::MeasurementConvexHull::pose
geo::Pose3D pose
Definition: measurement_convex_hull.h:13
geo::Vector3::x
const real & x() const
ed::Entity::convex_hull_new_
ConvexHull convex_hull_new_
Definition: entity.h:256
geo::Vec3T::z
T z
std::vector::empty
T empty(T... args)
ed::Entity::updateConvexHullFromVisual
void updateConvexHullFromVisual()
Definition: entity.cpp:88
ed::Entity::Entity
Entity(const UUID &id=generateID(), const TYPE &type="", const unsigned int &measurement_buffer_size=5)
Definition: entity.cpp:17
ed
Definition: convex_hull.h:8
ed::Entity::collision
geo::ShapeConstPtr collision() const
Definition: entity.h:59
ed::convex_hull::createAbsolute
void createAbsolute(const std::vector< geo::Vec2f > &points, float z_min, float z_max, ConvexHull &chull)
createAbsolute fill a ConvexHull with its origin in map frame.
Definition: convex_hull_calc.cpp:83
ed::Entity::best_measurement_
MeasurementConstPtr best_measurement_
Definition: entity.h:245
ed::ConvexHull::z_max
float z_max
Definition: convex_hull.h:16
ed::Entity::setVisual
void setVisual(const geo::ShapeConstPtr &visual)
Definition: entity.cpp:119
ed::Entity::visual
geo::ShapeConstPtr visual() const
Definition: entity.h:58
geo::Vec3T::x
T x
geo::Vec2T
ed::Entity::lastMeasurement
MeasurementConstPtr lastMeasurement() const
Definition: entity.cpp:188
ed::Entity::collision_revision_
unsigned long collision_revision_
Definition: entity.h:252
ed::ConvexHull::points
std::vector< geo::Vec2f > points
Definition: convex_hull.h:13