Skip to content

Commit

Permalink
add test eigen
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwenzhi committed Nov 28, 2019
1 parent dcd6c84 commit c9ee767
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test_eigen_map/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 2.8)
project(test_eigen)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "-std=c++14 -O3")

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# # OpenCV
# find_package(OpenCV REQUIRED)
# include_directories(${OpenCV_INCLUDE_DIRS})

# # Ceres
# find_package(Ceres REQUIRED)
# include_directories(${CERES_INCLUDE_DIRS})

# # g2o
# find_package(G2O REQUIRED)
# include_directories(${G2O_INCLUDE_DIRS})

# Eigen
include_directories("/usr/include/eigen3")

add_executable(main main.cpp)
# target_link_libraries(gaussNewton ${OpenCV_LIBS})

# add_executable(ceresCurveFitting ceresCurveFitting.cpp)
# target_link_libraries(ceresCurveFitting ${OpenCV_LIBS} ${CERES_LIBRARIES})

# add_executable(g2oCurveFitting g2oCurveFitting.cpp)
# target_link_libraries(g2oCurveFitting ${OpenCV_LIBS} g2o_core g2o_stuff)
91 changes: 91 additions & 0 deletions test_eigen_map/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>

using namespace Eigen;
using namespace std;

// map 和 vector 的配合怎样的改变 矩阵的形状。
// 使用map 来从已有内存获得数据 形成一个vector
typedef Eigen::Map<Eigen::VectorXi> VectorRef;
// 使用map 来从已有的内存获得数据形成一个vector (const)
typedef Eigen::Map<const Eigen::VectorXi> ConstVectorRef;

int main(){
int num = 11;

int array[num];
// 首先定义一个数组
for(int i = 0; i < num; ++i) array[i] = i;
// 从array的第一个内存开始把数据映射为一个vector
cout << "VectorRef:\n" << VectorRef(array,7) << endl;
/* output
VectorRef:
0
1
2
3
4
5
6
*/
// 从array的第二个内存开始把数据映射为一个vector
cout << "VectorRef:\n" << VectorRef(array+1,7) << endl;
/* VectorRef:
1
2
3
4
5
6
7 */
// 从array的第一个内存开始把数据映射为一个const vector
// cout << "ConstVectorRef :\n" << ConstVectorRef(array,4) << endl;

VectorRef testvector(array,3);
cout << "testvector :\n" << testvector << endl;
/* testvector :
0
1
2 */

ConstVectorRef testconstvector(array,4);
cout << "testconstvector :\n" << testconstvector << "\n";
cout << "testconstvector.size(): " << testconstvector.size() << endl;
/* testconstvector :
0
1
2
3
testconstvector.size(): 4
*/

// testconstvector[2] = 4;
// error: lvalue required as left operand of assignment


cout << "Column-major-12:\n" << Map<Matrix<int,2,6> >(array) << endl;
/*
Column-major-12:
0 2 4 6 8 10
1 3 5 7 9 0 */

cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array) << endl;
/* Column-major:
0 2 4 6
1 3 5 7
*/

cout << "Row-major:\n" << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
/* Row-major:
0 1 2 3
4 5 6 7 */

cout << "Row-major using stride:\n" <<

Map<Matrix<int,2,4>, Unaligned, Stride<1,5> >(array) << endl;
/* Row-major using stride:
0 1 2 3
5 6 7 8 */
}

0 comments on commit c9ee767

Please sign in to comment.