-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredeal.hpp
executable file
·189 lines (165 loc) · 5.59 KB
/
predeal.hpp
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
#ifndef _PREDEAL_H_
#define _PREDEAL_H_
#include<cassert>
#include<cmath>
#include<iostream>
#include<sensor_msgs/LaserScan.h>
#include<sensor_msgs/PointCloud.h>
#include<Eigen/Dense>
#include<fstream>
#include<pcl/common/common.h>
using namespace std;
namespace preDeal
{
/**
* \brief Transform the point in matrix frame into real world frame. The default origin of world frame is in the centriod of matrix
*/
Eigen::Vector2f gird2world(const float &map_resolution, Eigen::Vector2i map_origin , const Eigen::Vector2i& position_grid)
{
Eigen::Vector2f pos;
//x_in_map_frame=map_origin_y_in_img_frame-y_in_img_frame
//y_in_map_frame=x_img_frame-map_origin_x_in_img_frame
//x_in_world=map_resolution*x_in_map_frame
//y_in_world=map_resolution*y_in_map_frame
pos<<map_resolution*(position_grid(1)-map_origin(1)),map_resolution*(map_origin(0)-position_grid(0));
return pos;
}
/**
* \brief Transform the point in real world frame into matrix frame.
* \param[in] map_origin 地图原点在矩阵坐标系下的坐标
* \param[in] map_resolution 地图的分辨率,单位是米
* \param[in] position 地图坐标系下点的位置
* \return 矩阵坐标系下点的位置
*/
Eigen::Vector2i world2grid(const float &map_resolution, Eigen::Vector2i map_origin , Eigen::Vector2f& position)
{
Eigen::Vector2i x;
//通过上面反推
x<<map_origin(0)-floor(position(1)/map_resolution),floor(position(0)/map_resolution)+map_origin(1);
return x;
}
/**
* \brief Transform scan into point cloud
* \param[in] scan the scan data
* \param[out] pcout the point cloud in scan frame
*/
void scan2pc(const sensor_msgs::LaserScan &scan, sensor_msgs::PointCloud &pcout)
{
if(scan.ranges.empty())
{
cout<<"failed to Transform scan into point cloud because the scan is empty"<<endl;
assert(scan.ranges.size()>0);
}
pcout.points.reserve(scan.ranges.size());
for(int i=0;i<scan.ranges.size();i++)
{
geometry_msgs::Point32 point;
if(scan.ranges[i]<scan.range_max)
{
point.x=scan.ranges[i]*cos(scan.angle_min+i*scan.angle_increment);
point.y=scan.ranges[i]*sin(scan.angle_min+i*scan.angle_increment);
point.z=0;
pcout.points.push_back(point);
}
}
pcout.header.frame_id=scan.header.frame_id;
pcout.header.stamp=scan.header.stamp;
}
/**
* \brief transform point cloud into target_frame using pose3d, it is noted that the transform only is suitable for 2D situation
* \param[in] pcin the point cloud to transform
* \param[out] pcout the point cloud transformed
* \param[in] pose3d the pose of pcin in target_frame, it is noted that rotation represent the transform from pc_frame into target_frame
* \param[in] target_frame it is used to label the pcout
*/
void transformPointCloud(const sensor_msgs::PointCloud& pcin, sensor_msgs::PointCloud &pcout, const Eigen::Vector3f &pose3d,const string & target_frame)
{
if(!pcout.points.empty())
{
pcout.points.clear();
}
Eigen::Quaternion<float> q(cos(pose3d(2)/2), 0,0,sin(pose3d(2)/2));
Eigen::Vector3f trans(pose3d(0),pose3d(1),0), transformedPoint, initPoint;
for(int i=0;i<pcin.points.size();i++)
{
initPoint<<pcin.points[i].x, pcin.points[i].y,0;
geometry_msgs::Point32 tempoint;
transformedPoint=q.toRotationMatrix()*initPoint+trans;
// transformedPoint=q.toRotationMatrix().transpose()*initPoint+trans;
tempoint.x=transformedPoint(0); tempoint.y=transformedPoint(1);tempoint.z=0;
pcout.points.push_back(tempoint);
}
pcout.header.frame_id=target_frame;
}
void vector3ftoRationTtrans(const Eigen::Vector3f& pose, Eigen::Matrix3f& R, Eigen::Vector3f& t)
{
Eigen::Quaternion<float> q_w_target(cos(pose(2)/2),0,0,sin(pose(2)/2));
t(0)=pose(0);
t(1)=pose(1);
t(2)=0.0f;
R=q_w_target.toRotationMatrix();
}
void rotateTransToVector3f( Eigen::Vector3f& pose, const Eigen::Matrix3f& R, const Eigen::Vector3f& t)
{
Eigen::Quaternion<float> q(R);
Eigen::Vector3f v;
//以下的处理非常重要,只适用于只存在偏航角的情况
float a1=acos(q.w());
float a2=-a1;
if(abs(sin(a1)-q.z())<0.001f)
{
pose(2)=2*a1;
}
else if(abs(sin(a2)-q.z())<0.001f)
{
pose(2)=2*a2;
//cout<<v(2)<<endl;
}
else{
cout<<"四元数解析错误,该旋转轴不为z轴"<<endl;
exit(0);
}
pose(1)=t(1);
pose(0)=t(0);
}
void readpcdfile(const string& dir, pcl::PointCloud< pcl::PointXYZ >& pcd)
{
ifstream file;
file.open(dir.c_str(), ios_base::in);
if(file.good())
{
while(file.good())
{
pcl::PointXYZ tempoint;
file>>tempoint.x>>tempoint.y>>tempoint.z;
pcd.push_back(tempoint);
}
}
else
{
cout<<"can't open the file in the specified dir"<<endl;
assert(file.good());
}
}
/*
* 有问题,不能这样做,因为laser frame到odom frame不仅仅只有俯仰角的变化,所以不能简单的用pose3d来代表transform
void transformPC(const sensor_msgs::LaserScan &scan, sensor_msgs::PointCloud &pcout, const Eigen::Vector3f &pose3d,const string & target_frame)
{
sensor_msgs::PointCloud pcin;
scan2pc(scan,pcin);
pcout.points.clear();
Eigen::Quaternion<float> q(cos(pose3d(2)/2), 0,0,sin(pose3d(2)/2));
Eigen::Vector3f trans(pose3d(0),pose3d(1),0), transformedPoint, initPoint;
for(int i=0;i<pcin.points.size();i++)
{
initPoint<<pcin.points[i].x, pcin.points[i].y,0;
geometry_msgs::Point32 tempoint;
transformedPoint=q.toRotationMatrix().transpose()*initPoint+trans;
tempoint.x=transformedPoint(0); tempoint.y=-transformedPoint(1);tempoint.z=0;
pcout.points.push_back(tempoint);
}
pcout.header.frame_id=target_frame;
}
*/
}
#endif