-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstd_vector_conversion.cpp
47 lines (37 loc) · 1.28 KB
/
std_vector_conversion.cpp
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
#include <vector>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Core> //Eigen::Map
//https://stackoverflow.com/questions/26094379/typecasting-eigenvectorxd-to-stdvector
//https://stackoverflow.com/questions/17036818/initialise-eigenvector-with-stdvector
using namespace std;
Eigen::VectorXf stdToEigenVector(const std::vector<float>& vec) {
return Eigen::Map<Eigen::VectorXf, Eigen::Unaligned>(
const_cast<float*>(vec.data()), vec.size());
}
Eigen::VectorXd stdToEigenVector(const std::vector<double>& vec) {
return Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(
const_cast<double*>(vec.data()), vec.size());
}
std::vector<float> eigenToStdVector(const Eigen::VectorXf& vec) {
return std::vector<float>(vec.data(), vec.data() + vec.size());
}
std::vector<double> eigenToStdVector(const Eigen::VectorXd& vec) {
return std::vector<double>(vec.data(), vec.data() + vec.size());
}
int main(int argc, char** argv) {
Eigen::Vector3f mat(1.2, 3.4, 5.6);
vector<float> vec = eigenToStdVector(mat);
for (float& e : vec) {
cout << e << " ";
e += 1;
}
cout << endl;
//1.2 3.4 5.6
mat = stdToEigenVector(vec);
for (size_t i = 0; i < 3; ++i){
cout << mat(i) << " ";
}
cout << endl;
return 0;
}