You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.9 KiB

  1. namespace StormEigen {
  2. /** \eigenManualPage TutorialMapClass Interfacing with raw buffers: the Map class
  3. This page explains how to work with "raw" C/C++ arrays.
  4. This can be useful in a variety of contexts, particularly when "importing" vectors and matrices from other libraries into %Eigen.
  5. \eigenAutoToc
  6. \section TutorialMapIntroduction Introduction
  7. Occasionally you may have a pre-defined array of numbers that you want to use within %Eigen as a vector or matrix. While one option is to make a copy of the data, most commonly you probably want to re-use this memory as an %Eigen type. Fortunately, this is very easy with the Map class.
  8. \section TutorialMapTypes Map types and declaring Map variables
  9. A Map object has a type defined by its %Eigen equivalent:
  10. \code
  11. Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> >
  12. \endcode
  13. Note that, in this default case, a Map requires just a single template parameter.
  14. To construct a Map variable, you need two other pieces of information: a pointer to the region of memory defining the array of coefficients, and the desired shape of the matrix or vector. For example, to define a matrix of \c float with sizes determined at compile time, you might do the following:
  15. \code
  16. Map<MatrixXf> mf(pf,rows,columns);
  17. \endcode
  18. where \c pf is a \c float \c * pointing to the array of memory. A fixed-size read-only vector of integers might be declared as
  19. \code
  20. Map<const Vector4i> mi(pi);
  21. \endcode
  22. where \c pi is an \c int \c *. In this case the size does not have to be passed to the constructor, because it is already specified by the Matrix/Array type.
  23. Note that Map does not have a default constructor; you \em must pass a pointer to intialize the object. However, you can work around this requirement (see \ref TutorialMapPlacementNew).
  24. Map is flexible enough to accomodate a variety of different data representations. There are two other (optional) template parameters:
  25. \code
  26. Map<typename MatrixType,
  27. int MapOptions,
  28. typename StrideType>
  29. \endcode
  30. \li \c MapOptions specifies whether the pointer is \c #Aligned, or \c #Unaligned. The default is \c #Unaligned.
  31. \li \c StrideType allows you to specify a custom layout for the memory array, using the Stride class. One example would be to specify that the data array is organized in row-major format:
  32. <table class="example">
  33. <tr><th>Example:</th><th>Output:</th></tr>
  34. <tr>
  35. <td>\include Tutorial_Map_rowmajor.cpp </td>
  36. <td>\verbinclude Tutorial_Map_rowmajor.out </td>
  37. </table>
  38. However, Stride is even more flexible than this; for details, see the documentation for the Map and Stride classes.
  39. \section TutorialMapUsing Using Map variables
  40. You can use a Map object just like any other %Eigen type:
  41. <table class="example">
  42. <tr><th>Example:</th><th>Output:</th></tr>
  43. <tr>
  44. <td>\include Tutorial_Map_using.cpp </td>
  45. <td>\verbinclude Tutorial_Map_using.out </td>
  46. </table>
  47. All %Eigen functions are written to accept Map objects just like other %Eigen types. However, when writing your own functions taking %Eigen types, this does \em not happen automatically: a Map type is not identical to its Dense equivalent. See \ref TopicFunctionTakingEigenTypes for details.
  48. \section TutorialMapPlacementNew Changing the mapped array
  49. It is possible to change the array of a Map object after declaration, using the C++ "placement new" syntax:
  50. <table class="example">
  51. <tr><th>Example:</th><th>Output:</th></tr>
  52. <tr>
  53. <td>\include Map_placement_new.cpp </td>
  54. <td>\verbinclude Map_placement_new.out </td>
  55. </table>
  56. Despite appearances, this does not invoke the memory allocator, because the syntax specifies the location for storing the result.
  57. This syntax makes it possible to declare a Map object without first knowing the mapped array's location in memory:
  58. \code
  59. Map<Matrix3f> A(NULL); // don't try to use this matrix yet!
  60. VectorXf b(n_matrices);
  61. for (int i = 0; i < n_matrices; i++)
  62. {
  63. new (&A) Map<Matrix3f>(get_matrix_pointer(i));
  64. b(i) = A.trace();
  65. }
  66. \endcode
  67. */
  68. }