source: pacpussensors/trunk/Vislab/lib3dv-1.2.0/lib3dv/eigen/doc/StlContainers.dox@ 140

Last change on this file since 140 was 136, checked in by ldecherf, 8 years ago

Doc

File size: 3.3 KB
Line 
1namespace Eigen {
2
3/** \eigenManualPage TopicStlContainers Using STL Containers with Eigen
4
5\eigenAutoToc
6
7\section summary Executive summary
8
9Using STL containers on \ref TopicFixedSizeVectorizable "fixed-size vectorizable Eigen types", or classes having members of such types, requires taking the following two steps:
10
11\li A 16-byte-aligned allocator must be used. Eigen does provide one ready for use: aligned_allocator.
12\li If you want to use the std::vector container, you need to \#include <Eigen/StdVector>.
13
14These issues arise only with \ref TopicFixedSizeVectorizable "fixed-size vectorizable Eigen types" and \ref TopicStructHavingEigenMembers "structures having such Eigen objects as member". For other Eigen types, such as Vector3f or MatrixXd, no special care is needed when using STL containers.
15
16\section allocator Using an aligned allocator
17
18STL containers take an optional template parameter, the allocator type. When using STL containers on \ref TopicFixedSizeVectorizable "fixed-size vectorizable Eigen types", you need tell the container to use an allocator that will always allocate memory at 16-byte-aligned locations. Fortunately, Eigen does provide such an allocator: Eigen::aligned_allocator.
19
20For example, instead of
21\code
22std::map<int, Eigen::Vector4f>
23\endcode
24you need to use
25\code
26std::map<int, Eigen::Vector4f, std::less<int>,
27 Eigen::aligned_allocator<std::pair<const int, Eigen::Vector4f> > >
28\endcode
29Note that the third parameter "std::less<int>" is just the default value, but we have to include it because we want to specify the fourth parameter, which is the allocator type.
30
31\section vector The case of std::vector
32
33The situation with std::vector was even worse (explanation below) so we had to specialize it for the Eigen::aligned_allocator type. In practice you \b must use the Eigen::aligned_allocator (not another aligned allocator), \b and \#include <Eigen/StdVector>.
34
35Here is an example:
36\code
37#include<Eigen/StdVector>
38/* ... */
39std::vector<Eigen::Vector4f,Eigen::aligned_allocator<Eigen::Vector4f> >
40\endcode
41
42\subsection vector_spec An alternative - specializing std::vector for Eigen types
43
44As an alternative to the recommended approach described above, you have the option to specialize std::vector for Eigen types requiring alignment.
45The advantage is that you won't need to declare std::vector all over with Eigen::allocator. One drawback on the other hand side is that
46the specialization needs to be defined before all code pieces in which e.g. std::vector<Vector2d> is used. Otherwise, without knowing the specialization
47the compiler will compile that particular instance with the default std::allocator and you program is most likely to crash.
48
49Here is an example:
50\code
51#include<Eigen/StdVector>
52/* ... */
53EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix2d)
54std::vector<Eigen::Vector2d>
55\endcode
56
57<span class="note">\b Explanation: The resize() method of std::vector takes a value_type argument (defaulting to value_type()). So with std::vector<Eigen::Vector4f>, some Eigen::Vector4f objects will be passed by value, which discards any alignment modifiers, so a Eigen::Vector4f can be created at an unaligned location. In order to avoid that, the only solution we saw was to specialize std::vector to make it work on a slight modification of, here, Eigen::Vector4f, that is able to deal properly with this situation.
58</span>
59
60*/
61
62}
Note: See TracBrowser for help on using the repository browser.