-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobject_filter.h
270 lines (211 loc) · 8.46 KB
/
object_filter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef _OBJECT_FILTER_H_
#define _OBJECT_FILTER_H_
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <memory>
#include <Eigen/Eigen>
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <geometry_msgs/PoseWithCovariance.h>
#include <geometry_msgs/PoseWithCovarianceStamped.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/filters/filter_indices.h>
#include <pcl/filters/crop_box.h>
#include <pcl/segmentation/min_cut_segmentation.h>
typedef geometry_msgs::PoseWithCovarianceStamped ObjectPoseType;
typedef sensor_msgs::PointCloud2 PointCloudType;
/**************************
DynamicObject class to
set object BBox and filtering
**************************/
class DynamicObject
{
private:
Eigen::Vector4f max_vec_;
Eigen::Vector4f min_vec_;
public:
DynamicObject();
~DynamicObject();
Eigen::Vector4f GetMaxVec() const;
Eigen::Vector4f GetMinVec() const;
void SetMinMaxVec(double& min_x, double& min_y, double& min_z,
double& max_x, double& max_y, double& max_z);
void FilterObject(pcl::PCLPointCloud2Ptr point_cloud,
pcl::PCLPointCloud2& filtered_cloud,
const Eigen::Vector3f& sync_pose);
};
DynamicObject::DynamicObject(){};
DynamicObject::~DynamicObject(){};
Eigen::Vector4f DynamicObject::GetMaxVec() const
{
return max_vec_;
}
Eigen::Vector4f DynamicObject::GetMinVec() const
{
return min_vec_;
}
void DynamicObject::SetMinMaxVec(double& min_x, double& min_y, double& min_z,
double& max_x, double& max_y, double& max_z)
{
min_vec_ << min_x, min_y, min_z, 1.0;
max_vec_ << max_x, max_y, max_z, 1.0;
}
void DynamicObject::FilterObject(pcl::PCLPointCloud2Ptr point_cloud,
pcl::PCLPointCloud2& filtered_cloud,
const Eigen::Vector3f& sync_pose)
{
pcl::CropBox<pcl::PCLPointCloud2> box_filter;
box_filter.setInputCloud(point_cloud);
box_filter.setMax(max_vec_);
box_filter.setMin(min_vec_);
box_filter.setTranslation(sync_pose);
box_filter.setNegative(true);
box_filter.filter(filtered_cloud);
std::cout << "::" << filtered_cloud.width << std::endl;
}
/******************************
Setting node parameters for
node and DynamicObject objects
*****************************/
void SetNodeParameters(const ros::NodeHandle& node,
Eigen::Vector4f& raw_cloud_max_vec,
Eigen::Vector4f& raw_cloud_min_vec,
DynamicObject& self,
DynamicObject& objA,
DynamicObject& objB,
DynamicObject& objC);
/**************************
ObjectsFiltering class to
encapsulate and process
objects and filtering.
**************************/
class ObjectsFiltering
{
private:
DynamicObject self_;
DynamicObject objA_;
DynamicObject objB_;
DynamicObject objC_;
Eigen::Vector4f raw_cloud_max_vec_;
Eigen::Vector4f raw_cloud_min_vec_;
std::queue<ObjectPoseType>objA_queue_;
std::queue<ObjectPoseType>objB_queue_;
std::queue<ObjectPoseType>objC_queue_;
ros::Publisher filtered_cloud_pub_;
ros::Subscriber point_cloud_sub_;
ros::Subscriber objA_pose_sub_;
ros::Subscriber objB_pose_sub_;
ros::Subscriber objC_pose_sub_;
public:
ObjectsFiltering(ros::NodeHandle node);
~ObjectsFiltering();
void CropCloud(pcl::PCLPointCloud2Ptr input_cloud, pcl::PCLPointCloud2& output_cloud);
void RemoveSelf(pcl::PCLPointCloud2Ptr input_cloud, pcl::PCLPointCloud2& output_cloud);
void FilterObjects(pcl::PCLPointCloud2Ptr input_cloud, pcl::PCLPointCloud2& filtered_cloud);
void CloudCallBack(const PointCloudType::ConstPtr& point_cloud_msg);
void ObjAPoseCallBack(const ObjectPoseType::ConstPtr& objA_pose_msg);
void ObjBPoseCallBack(const ObjectPoseType::ConstPtr& objB_pose_msg);
void ObjCPoseCallBack(const ObjectPoseType::ConstPtr& objC_pose_msg);
};
ObjectsFiltering::ObjectsFiltering(ros::NodeHandle node)
{
SetNodeParameters(node, raw_cloud_max_vec_, raw_cloud_min_vec_,
self_, objA_, objB_, objC_);
point_cloud_sub_ = node.subscribe("raw_point_cloud", 10, &ObjectsFiltering::CloudCallBack, this);
objA_pose_sub_ = node.subscribe("objA_pose", 20, &ObjectsFiltering::ObjAPoseCallBack, this);
objB_pose_sub_ = node.subscribe("objB_pose", 20, &ObjectsFiltering::ObjBPoseCallBack, this);
objC_pose_sub_ = node.subscribe("objC_pose", 20, &ObjectsFiltering::ObjCPoseCallBack, this);
filtered_cloud_pub_ = node.advertise<PointCloudType>("filtered_point_cloud", 10);
}
ObjectsFiltering::~ObjectsFiltering(){}
void ObjectsFiltering::CropCloud(pcl::PCLPointCloud2Ptr input_cloud, pcl::PCLPointCloud2& output_cloud)
{
pcl::CropBox<pcl::PCLPointCloud2> box_filter;
box_filter.setInputCloud(input_cloud);
box_filter.setMax(raw_cloud_max_vec_);
box_filter.setMin(raw_cloud_min_vec_);
box_filter.filter(output_cloud);
std::cout << "Cropped raw point cloud size: " << output_cloud.width << std::endl;
}
void ObjectsFiltering::RemoveSelf(pcl::PCLPointCloud2Ptr input_cloud, pcl::PCLPointCloud2& output_cloud)
{
//TODO
Eigen::Vector3f self_pos;
self_pos << 0.0, 0.0, 0.0;
self_.FilterObject(input_cloud, output_cloud, self_pos);
}
void ObjectsFiltering::FilterObjects(pcl::PCLPointCloud2Ptr input_cloud, pcl::PCLPointCloud2& filtered_cloud)
{
ros::Duration time_tolerance(1e-3); // 1ms
//ros::Time point_cloud_time = pcl_conversions::fromPCL(point_cloud.header.stamp);
ros::Time point_cloud_time = ros::Time::now();
ROS_INFO("Point cloud msg time: %lf", point_cloud_time.toSec());
Eigen::Vector3f sync_objA_pos;
Eigen::Vector3f sync_objB_pos;
Eigen::Vector3f sync_objC_pos;
RemoveSelf(input_cloud, filtered_cloud);
while(objA_queue_.size() > 1 &&
objA_queue_.front().header.stamp <= point_cloud_time + time_tolerance)
{
sync_objA_pos << objA_queue_.front().pose.pose.position.x,
objA_queue_.front().pose.pose.position.y,
objA_queue_.front().pose.pose.position.z;
objA_queue_.pop();
}
while(objB_queue_.size() > 1 &&
objB_queue_.front().header.stamp <= point_cloud_time + time_tolerance)
{
sync_objB_pos << objB_queue_.front().pose.pose.position.x,
objB_queue_.front().pose.pose.position.y,
objB_queue_.front().pose.pose.position.z;
objB_queue_.pop();
}
while(objC_queue_.size() > 1 &&
objC_queue_.front().header.stamp <= point_cloud_time + time_tolerance)
{
sync_objC_pos << objC_queue_.front().pose.pose.position.x,
objC_queue_.front().pose.pose.position.y,
objC_queue_.front().pose.pose.position.z;
objC_queue_.pop();
}
// objA_.FilterObject(input_cloud, filtered_cloud, sync_objA_pos);
// *point_cloud = filtered_cloud;
// filtered_cloud.data.clear();
// objB_.FilterObject(point_cloud, filtered_cloud, sync_objB_pos);
}
void ObjectsFiltering::CloudCallBack(const PointCloudType::ConstPtr& point_cloud_msg)
{
pcl::PCLPointCloud2* pcl_point_cloud = new pcl::PCLPointCloud2;
pcl::PCLPointCloud2Ptr pcl_point_cloud_ptr(pcl_point_cloud);
pcl::PCLPointCloud2 filtered_pcl_point_cloud;
pcl_conversions::toPCL(*point_cloud_msg, *pcl_point_cloud);
FilterObjects(pcl_point_cloud_ptr, filtered_pcl_point_cloud);
sensor_msgs::PointCloud2 filtered_ros_point_cloud;
pcl_conversions::moveFromPCL(filtered_pcl_point_cloud, filtered_ros_point_cloud);
filtered_cloud_pub_.publish(filtered_ros_point_cloud);
}
void ObjectsFiltering::ObjAPoseCallBack(const ObjectPoseType::ConstPtr& objA_pose_msg)
{
ObjectPoseType objA_pose;
objA_pose.header = objA_pose_msg->header;
objA_pose.pose = objA_pose_msg->pose;
objA_queue_.push(objA_pose);
}
void ObjectsFiltering::ObjBPoseCallBack(const ObjectPoseType::ConstPtr& objB_pose_msg)
{
ObjectPoseType objB_pose;
objB_pose.header = objB_pose_msg->header;
objB_pose.pose = objB_pose_msg->pose;
objA_queue_.push(objB_pose);
}
void ObjectsFiltering::ObjCPoseCallBack(const ObjectPoseType::ConstPtr& objC_pose_msg)
{
ObjectPoseType objC_pose;
objC_pose.header = objC_pose_msg->header;
objC_pose.pose = objC_pose_msg->pose;
objC_queue_.push(objC_pose);
}
#endif //_OBJECT_FILTER_H_