实践课
- useGeometry.cpp
- 多种变换演示Eigen
#include <iostream>
#include <cmath>
using namespace std;
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace Eigen;
int main(int argc,char **argv){
Matrix3d rotation_matrix=Matrix3d::Identity();
AngleAxisd rotation_vector(M_PI/4,Vector3d(0,0,1));
cout.precision(3);
cout<<"rotation matrix=\n"<< rotation_vector.matrix()<<endl;
rotation_matrix=rotation_vector.toRotationMatrix();
Vector3d v(1,0,0);
Vector3d v_rotated=rotation_vector * v;
cout<<"(1,0,0) after rotation (by angle axis) = "<<v_rotated.transpose()<<endl;
v_rotated=rotation_matrix * v;
cout<<"(1,0,0) after rotation (by matrix) = "<<v_rotated.transpose()<<endl;
Vector3d euler_angles = rotation_matrix.eulerAngles(2, 1, 0);
cout << "yaw pitch roll = " << euler_angles.transpose() << endl;
Isometry3d T=Isometry3d::Identity();
T.rotate(rotation_vector);
T.pretranslate(Vector3d(1,3,4));
cout<<"Transform matrix=\n"<<T.matrix()<<endl;
Vector3d v_transformed=T*v;
cout<<"v transformed = "<<v_transformed.transpose()<<endl;
Quaterniond q=Quaterniond(rotation_vector);
cout<<"quaternion from rotation vector ="<<q.coeffs().transpose()<<endl;
q=Quaterniond(rotation_matrix);
cout<<"Quaternion from rotation matrix ="<<q.coeffs().transpose()<<endl;
v_rotated=q*v;
cout<<"(1,0,0) after rotation ="<<v_rotated.transpose()<<endl;
cout<<"should be equal to "<<(q*Quaterniond(0,1,0,0)*q.inverse()).coeffs().transpose()<<endl;
return 0;
}
- 小萝卜的例子
- coordinateTransform.cpp
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace std;
using namespace Eigen;
int main(int argc,char** argv){
Quaterniond q1(1,0,0,0),q2(1,0,0,0);
q1.normalize();
q2.normalize();
Vector3d t1(0.5,0.5,0.0),t2(0,0,0);
Vector3d p1(0.5,0.5,0.0);
Isometry3d T1w(q1),T2w(q2);
T1w.pretranslate(t1);
T2w.pretranslate(t2);
cout<<T2w.matrix()<<endl;
Vector3d p2=T2w*T1w.inverse()*p1;
cout<<endl<<p2.transpose()<<endl;
Vector3d p3=T2w.inverse()*T1w*p1;
cout<<endl<<p3.transpose()<<endl;
return 0;
}
#include <pangolin/pangolin.h>
#include <Eigen/Core>
#include <unistd.h>
#include <Eigen/Geometry>
using namespace std;
using namespace Eigen;
string trajectory_file ="trajectory.txt";
void DrawTrajectory(vector<Isometry3d,Eigen::aligned_allocator<Isometry3d>>);
int main(int argc,char **argv)
{
vector<Isometry3d,Eigen::aligned_allocator<Isometry3d>> poses;
ifstream fin(trajectory_file);
if(!fin){
cout<<"can not find trajectory file at "<<trajectory_file<<endl;
return -1;
}
while(!fin.eof()){
double time,tx,ty,tz,qx,qy,qz,qw;
fin>>time>>tx>>ty>>tz>>qx>>qy>>qz>>qw;
Isometry3d Twr(Quaterniond(qw,qx,qy,qz));
Twr.pretranslate(Vector3d(tx,ty,tz));
poses.push_back(Twr);
}
cout<<"read total"<<poses.size()<<"pose entries"<<endl;
DrawTrajectory(poses);
return 0;
}
void DrawTrajectory(vector<Isometry3d,Eigen::aligned_allocator<Isometry3d>> poses)
{
pangolin::CreateWindowAndBind("Trajectory Viewer",1024,768);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC1_ALPHA);
pangolin::OpenGlRenderState s_cam(
pangolin::ProjectionMatrix(1024,768,500,500,512,389,0.1,1000),
pangolin::ModelViewLookAt(0,-0.1,-1.8,0,0,0,0.0,-1.0,0.0)
);
pangolin::View &d_cam=pangolin::CreateDisplay()
.SetBounds(0.0,1.0,0.0,1.0,-1024.0f/768.0f)
.SetHandler(new pangolin::Handler3D(s_cam));
while(pangolin::ShouldQuit()==false){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glLineWidth(2);
for(size_t i=0;i<poses.size();i++)
{
Vector3d Ow=poses[i].translation();
Vector3d Xw=poses[i]*(0.1*Vector3d(1,0,0));
Vector3d Yw=poses[i]*(0.1*Vector3d(0,1,0));
Vector3d Zw=poses[i]*(0,1*Vector3d(0,0,1));
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);
glVertex3d(Ow[0],Ow[1],Ow[2]);
glVertex3d(Xw[0],Xw[1],Xw[2]);
glColor3f(0.0,1.0,0.0);
glVertex3d(Ow[0],Ow[1],Ow[2]);
glVertex3d(Yw[0],Yw[1],Yw[2]);
glColor3f(0.0,0.0,1.0);
glVertex3d(Ow[0],Ow[1],Ow[2]);
glVertex3d(Zw[0],Zw[1],Zw[2]);
glEnd();
}
for(size_t i=0;i<poses.size();i++)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE);
auto p1=poses[i],p2=poses[i+1];
glVertex3d(p1.translation()[0],p1.translation()[1],p1.translation()[2]);
glVertex3d(p2.translation()[0],p2.translation()[1],p2.translation()[2]);
glEnd();
}
pangolin::FinishFrame();
usleep(5000);
}
}