├── CMakeLists.txt ├── README.md └── src ├── Array_initializer_list.h ├── Matrix_initializer_list.h └── eigen_initializer_list_demo.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Authors: hauptmech , Nov 2013 2 | 3 | SET(EXECUTABLE_NAME "eigen_initializer_list") #<-------Change! 4 | 5 | 6 | PROJECT(${EXECUTABLE_NAME}) 7 | cmake_minimum_required(VERSION 2.8) 8 | 9 | set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) 10 | 11 | 12 | # ./src holds our code and private *.h files 13 | include_directories(${CMAKE_SOURCE_DIR}/src) 14 | 15 | find_package(PkgConfig) 16 | pkg_check_modules(EIGEN3 eigen3) 17 | 18 | INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIRS}) 19 | 20 | #Add source files here 21 | set( LIBRARY1_SOURCES 22 | src/eigen_initializer_list_demo.cpp #<---- Change me! 23 | ) 24 | 25 | add_definitions(-std=c++11 -DEIGEN_MATRIX_PLUGIN="Matrix_initializer_list.h" -DEIGEN_ARRAY_PLUGIN="Array_initializer_list.h") 26 | 27 | add_executable(${EXECUTABLE_NAME} ${LIBRARY1_SOURCES} ) 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A plugin for Eigen3 to allow the user of C++11 initializer lists. 2 | ```c++ 3 | #include 4 | #include 5 | 6 | using namespace Eigen; 7 | 8 | int main() 9 | { 10 | // Init a fixed size 3x3 matrix 11 | Matrix3d m3 {{1.2,2.2,3.3},{4.2,2.5,6.3},{7.2,8.2,9.3}}; 12 | std::cout << "\nm3:\n" << m3 << std::endl; 13 | 14 | 15 | // Initialize a variable length Matrix to 2x2 16 | MatrixXd mY {{1.2,2.2},{3.2,4.5}}; 17 | std::cout << "\nmY:\n" << mY << std::endl; 18 | 19 | // Initialize a variable length Matrix (Vector) to 9x1 20 | // 2D Matrices are initialized column-wise 21 | MatrixXd mX {1.2,2.2,3.3, 4.2,2.5,6.3, 7.2,8.2,9.3}; 22 | std::cout << "\nmX:\n" << mX << std::endl; 23 | 24 | // Init a 3 element vector 25 | Vector3d v3 {1,3,3}; 26 | std::cout << "\nv3:\n" << v3 << std::endl; 27 | } 28 | ``` 29 | 30 | 31 | The key line to add to your CMakeLists.txt file is: 32 | 33 | add_definitions(-std=c++11 -DEIGEN_MATRIX_PLUGIN="Matrix_initializer_list.h" -DEIGEN_ARRAY_PLUGIN="Array_initializer_list.h") 34 | 35 | 36 | LICENSE 37 | ======= 38 | 39 | This is free and unencumbered software released into the public domain. 40 | 41 | Anyone is free to copy, modify, publish, use, compile, sell, or 42 | distribute this software, either in source code form or as a compiled 43 | binary, for any purpose, commercial or non-commercial, and by any 44 | means. 45 | 46 | In jurisdictions that recognize copyright laws, the author or authors 47 | of this software dedicate any and all copyright interest in the 48 | software to the public domain. We make this dedication for the benefit 49 | of the public at large and to the detriment of our heirs and 50 | successors. We intend this dedication to be an overt act of 51 | relinquishment in perpetuity of all present and future rights to this 52 | software under copyright law. 53 | 54 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 55 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 56 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 57 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 58 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 59 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 60 | OTHER DEALINGS IN THE SOFTWARE. 61 | 62 | For more information, please refer to 63 | 64 | -------------------------------------------------------------------------------- /src/Array_initializer_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | Author: hauptmech , Nov 2013 3 | 4 | This is free and unencumbered software released into the public domain. 5 | 6 | Anyone is free to copy, modify, publish, use, compile, sell, or 7 | distribute this software, either in source code form or as a compiled 8 | binary, for any purpose, commercial or non-commercial, and by any 9 | means. 10 | 11 | In jurisdictions that recognize copyright laws, the author or authors 12 | of this software dedicate any and all copyright interest in the 13 | software to the public domain. We make this dedication for the benefit 14 | of the public at large and to the detriment of our heirs and 15 | successors. We intend this dedication to be an overt act of 16 | relinquishment in perpetuity of all present and future rights to this 17 | software under copyright law. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 23 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 24 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | 27 | For more information, please refer to 28 | */ 29 | 30 | #include 31 | 32 | 33 | /** Constructs an array from a C++11 initializer_list 34 | 35 | ArrayXXd a {1,2,3,4} //4x1 array 36 | ArrayXd b {6,5,8,6} // 4 element array 37 | Array3d c {1,2,3} 38 | 39 | The constructor will have an assertion failure if a fixed size 40 | array given a initializer list of the wrong size. 41 | 42 | **/ 43 | EIGEN_STRONG_INLINE Array(std::initializer_list initlist) : Base() 44 | { 45 | 46 | Base::_check_template_params(); 47 | int size = initlist.size(); 48 | 49 | if (base().size() == 0) { //Empty array, gotta resize it 50 | this->resize(size,1 ); 51 | } 52 | 53 | int initializer_list_size = initlist.size(); 54 | int Array_size = base().size(); 55 | eigen_assert(initializer_list_size == Array_size); 56 | 57 | if (static_cast(initlist.size()) == base().size()){ 58 | int i = 0; 59 | for (auto x: initlist){ 60 | coeffRef(i)=Scalar(x); 61 | i++; 62 | } 63 | } 64 | } 65 | 66 | /** Constructs an array from a C++11 initializer_list 67 | 68 | ArrayXXd a {1,2,3,4} //4x1 array 69 | ArrayXd b {6,5,8,6} // 4 element array 70 | Array3d c {1,2,3} 71 | 72 | The constructor will have an assertion failure if a fixed size 73 | array given a initializer list of the wrong size. 74 | 75 | **/ 76 | EIGEN_STRONG_INLINE Array(std::initializer_list > initlist) : Base() 77 | { 78 | Base::_check_template_params(); 79 | int rows = initlist.size(); 80 | int cols = initlist.begin()->size(); 81 | 82 | if (base().size() == 0) { //Empty array, gotta resize it 83 | this->resize(rows, cols); 84 | } 85 | 86 | int initializer_list_size = rows*cols; 87 | int Array_size = base().size(); 88 | eigen_assert(initializer_list_size == Array_size); 89 | 90 | if (rows*cols == base().size()){ 91 | int i = 0; 92 | for (auto x: initlist){ 93 | int j=0; 94 | for (auto y: x){ 95 | coeffRef(rows*j+i)=Scalar(y); 96 | j++; 97 | } 98 | i++; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/Matrix_initializer_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | Author: hauptmech , Nov 2013 3 | 4 | This is free and unencumbered software released into the public domain. 5 | 6 | Anyone is free to copy, modify, publish, use, compile, sell, or 7 | distribute this software, either in source code form or as a compiled 8 | binary, for any purpose, commercial or non-commercial, and by any 9 | means. 10 | 11 | In jurisdictions that recognize copyright laws, the author or authors 12 | of this software dedicate any and all copyright interest in the 13 | software to the public domain. We make this dedication for the benefit 14 | of the public at large and to the detriment of our heirs and 15 | successors. We intend this dedication to be an overt act of 16 | relinquishment in perpetuity of all present and future rights to this 17 | software under copyright law. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 23 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 24 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | 27 | For more information, please refer to 28 | */ 29 | 30 | #include 31 | 32 | 33 | /** Constructs a matrix from a C++11 initializer_list 34 | 35 | MatrixXd a {1,2,3,4} //4x1 matrix 36 | VectorXd b {6,5,8,6} // 4 element vector 37 | Vector3d c {1,2,3} 38 | 39 | The constructor will have an assertion failure if a fixed size vector or 40 | matrix given a initializer list of the wrong size. 41 | 42 | **/ 43 | EIGEN_STRONG_INLINE Matrix(std::initializer_list initlist) : Base() 44 | { 45 | 46 | Base::_check_template_params(); 47 | int size = initlist.size(); 48 | 49 | if (base().size() == 0) { //Empty matrix, gotta resize it 50 | this->resize(size,1 ); 51 | } 52 | 53 | int initializer_list_size = initlist.size(); 54 | int Matrix_size = base().size(); 55 | eigen_assert(initializer_list_size == Matrix_size); 56 | 57 | if (static_cast(initlist.size()) == base().size()){ 58 | int i = 0; 59 | for (auto x: initlist){ 60 | coeffRef(i)=Scalar(x); 61 | i++; 62 | } 63 | } 64 | } 65 | 66 | /** Constructs a matrix from a C++11 initializer_list 67 | 68 | MatrixXd a {1,2,3,4} //4x1 matrix 69 | VectorXd b {6,5,8,6} // 4 element vector 70 | Vector3d c {1,2,3} 71 | 72 | The constructor will have an assertion failure if a fixed size vector or 73 | matrix given a initializer list of the wrong size. 74 | 75 | **/ 76 | EIGEN_STRONG_INLINE Matrix(std::initializer_list > initlist) : Base() 77 | { 78 | Base::_check_template_params(); 79 | int rows = initlist.size(); 80 | int cols = initlist.begin()->size(); 81 | 82 | if (base().size() == 0) { //Empty matrix, gotta resize it 83 | this->resize(rows, cols); 84 | } 85 | 86 | int initializer_list_size = rows*cols; 87 | int Matrix_size = base().size(); 88 | eigen_assert(initializer_list_size == Matrix_size); 89 | 90 | if (rows*cols == base().size()){ 91 | int i = 0; 92 | for (auto x: initlist){ 93 | int j=0; 94 | for (auto y: x){ 95 | coeffRef(rows*j+i)=Scalar(y); 96 | j++; 97 | } 98 | i++; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/eigen_initializer_list_demo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: hauptmech , Nov 2013 3 | 4 | This is free and unencumbered software released into the public domain. 5 | 6 | Anyone is free to copy, modify, publish, use, compile, sell, or 7 | distribute this software, either in source code form or as a compiled 8 | binary, for any purpose, commercial or non-commercial, and by any 9 | means. 10 | 11 | In jurisdictions that recognize copyright laws, the author or authors 12 | of this software dedicate any and all copyright interest in the 13 | software to the public domain. We make this dedication for the benefit 14 | of the public at large and to the detriment of our heirs and 15 | successors. We intend this dedication to be an overt act of 16 | relinquishment in perpetuity of all present and future rights to this 17 | software under copyright law. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 23 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 24 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | 27 | For more information, please refer to 28 | */ 29 | 30 | 31 | #include 32 | #include 33 | 34 | using namespace Eigen; 35 | 36 | int main() 37 | { 38 | // Init a fixed size 3x3 matrix 39 | Matrix3d m3 {{1.2,2.2,3.3},{4.2,2.5,6.3},{7.2,8.2,9.3}}; 40 | std::cout << "\nm3:\n" << m3 << std::endl; 41 | 42 | 43 | // Initialize a variable length Matrix to 2x2 44 | MatrixXd mY {{1.2,2.2},{3.2,4.5}}; 45 | std::cout << "\nmY:\n" << mY << std::endl; 46 | 47 | // Initialize a variable length Matrix (Vector) to 9x1 48 | // 2D Matrices are initialized column-wise 49 | MatrixXd mX {1.2,2.2,3.3, 4.2,2.5,6.3, 7.2,8.2,9.3}; 50 | std::cout << "\nmX:\n" << mX << std::endl; 51 | 52 | // Init a 3 element vector 53 | Vector3d v3 {1,3,3}; 54 | std::cout << "\nv3:\n" << v3 << std::endl; 55 | 56 | 57 | 58 | 59 | 60 | 61 | // Init a fixed size 3x3 array 62 | Array33d a3 {{1.2,2.2,3.3},{4.2,2.5,6.3},{7.2,8.2,9.3}}; 63 | std::cout << "\na3:\n" << a3 << std::endl; 64 | 65 | 66 | // Initialize a variable length Array to 2x2 67 | ArrayXXd aY {{1.2,2.2},{3.2,4.5}}; 68 | std::cout << "\naY:\n" << aY << std::endl; 69 | 70 | // Initialize a variable length Array to 9x1 71 | // 2D Arrays are initialized column-wise 72 | ArrayXXd aX {1.2,2.2,3.3, 4.2,2.5,6.3, 7.2,8.2,9.3}; 73 | std::cout << "\naX:\n" << aX << std::endl; 74 | 75 | // Init a 3 element array 76 | Array3d w3 {1,3,3}; 77 | std::cout << "\nw3:\n" << w3 << std::endl; 78 | } 79 | 80 | --------------------------------------------------------------------------------