记录Eigen3的一个坑

首先Eigen3这个牛逼的库,我在两年前就接触了,但是到现在也没有用过,这次的事件也一样。

最近在使用公司提供的一个摄像头标定工具,他依赖一堆的库,比如opencv,eigen3,pcl等等一堆牛逼的库

然后我就一关一关的过,整整折腾了三天,终于把这个工具编译过了。

然后今天开始试着使用标定一下,结果出了一个段错误。

内心os,这时哪个大婶写的代码呀,咋还段错误了呢。

于是祭起gdb大旗,找一下在哪里出错了。结果如下

(gdb) core-file core 
[New LWP 21126]
[New LWP 21128]
[New LWP 21127]
[New LWP 21129]
[New LWP 21130]
[New LWP 21141]
[New LWP 21131]
[New LWP 21132]
[New LWP 21152]
[New LWP 21143]
[New LWP 21133]
[New LWP 21151]
[New LWP 21144]
[New LWP 21134]
[New LWP 21145]
[New LWP 21142]
[New LWP 21148]
[New LWP 21149]
[New LWP 21135]
[New LWP 21146]
[New LWP 21154]
[New LWP 21140]
[New LWP 21153]
[New LWP 21150]
[New LWP 21136]
[New LWP 21147]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./intrinsic_calib'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __GI___libc_free (mem=0x21) at malloc.c:2951
2951	malloc.c: 没有那个文件或目录.
[Current thread is 1 (Thread 0x7f1a2c0af7c0 (LWP 21126))]

这一看把我吓一跳,咋还malloc出错了呢。

赶紧找度娘帮忙,看到一个大婶的帖子,https://www.cnblogs.com/gatsby123/p/11755320.html

说是可能是某个析构函数的问题,并建议查看函数调用栈,

(gdb) where
#0  __GI___libc_free (mem=0x21) at malloc.c:2951
#1  0x00007f1a2b47b9a8 in Eigen::internal::handmade_aligned_free (ptr=0x5c15ca0) at /usr/local/include/eigen3/Eigen/src/Core/util/Memory.h:98
#2  0x00007f1a2b47ba04 in Eigen::internal::aligned_free (ptr=0x5c15ca0) at /usr/local/include/eigen3/Eigen/src/Core/util/Memory.h:179
#3  0x00007f1a2b47e544 in Eigen::internal::conditional_aligned_free<true> (ptr=0x5c15ca0) at /usr/local/include/eigen3/Eigen/src/Core/util/Memory.h:230
#4  0x00007f1a2b48075c in Eigen::internal::conditional_aligned_delete_auto<double, true> (ptr=0x5c15ca0, size=3) at /usr/local/include/eigen3/Eigen/src/Core/util/Memory.h:416
#5  0x00007f1a2b47f013 in Eigen::DenseStorage<double, -1, -1, 1, 0>::~DenseStorage (this=0x7ffe7ca9cd18, __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/DenseStorage.h:542
#6  0x00007f1a2b47ded2 in Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::~PlainObjectBase (this=0x7ffe7ca9cd18, __in_chrg=<optimized out>)
    at /usr/local/include/eigen3/Eigen/src/Core/PlainObjectBase.h:98
#7  0x00007f1a2b47deee in Eigen::Matrix<double, -1, 1, 0, -1, 1>::~Matrix (this=0x7ffe7ca9cd18, __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/Matrix.h:178
#8  0x00007f1a21de503e in Eigen::internal::product_evaluator<Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0>, 3, Eigen::DenseShape, Eigen::DenseShape, double, double>::~product_evaluator (this=0x7ffe7ca9cd08, __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/ProductEvaluators.h:96
#9  0x00007f1a21de5066 in Eigen::internal::evaluator<Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> >::~evaluator (this=0x7ffe7ca9cd08, 
    __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/ProductEvaluators.h:29
#10 0x00007f1a21de5082 in Eigen::internal::evaluator<Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const>::~evaluator (this=0x7ffe7ca9cd08, 
    __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/CoreEvaluators.h:99
#11 0x00007f1a21de50b2 in Eigen::internal::binary_evaluator<Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const>, Eigen::internal::IndexBased, Eigen::internal::IndexBased, double, double>::~binary_evaluator (this=0x7ffe7ca9cd00, 
    __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/CoreEvaluators.h:675
#12 0x00007f1a21de50da in Eigen::internal::evaluator<Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const> >::~evaluator (this=0x7ffe7ca9cd00, __in_chrg=<optimized out>) at /usr/local/include/eigen3/Eigen/src/Core/CoreEvaluators.h:665
#13 0x00007f1a21de51ad in Eigen::internal::call_dense_assignment_loop<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const>, Eigen::internal::assign_op<double, double> > (dst=..., src=..., func=...)
    at /usr/local/include/eigen3/Eigen/src/Core/AssignEvaluator.h:730
#14 0x00007f1a21de4c1a in Eigen::internal::Assignment<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const>, Eigen::internal::assign_op<double, double>, Eigen::internal::Dense2Dense, void>::run (dst=..., 
    src=..., func=...) at /usr/local/include/eigen3/Eigen/src/Core/AssignEvaluator.h:879
#15 0x00007f1a21de4572 in Eigen::internal::call_assignment_no_alias<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const>, Eigen::internal::assign_op<double, double> > (dst=..., src=..., func=...)
    at /usr/local/include/eigen3/Eigen/src/Core/AssignEvaluator.h:836
#16 0x00007f1a21de3d50 in Eigen::internal::call_assignment<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const>, Eigen::internal::assign_op<double, double> >(Eigen::Matrix<double, 3, 1, 0, 3, 1>&, Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const> const&, Eigen::internal::assign_op<double, double> const&, Eigen::internal::enable_if<!Eigen::internal::evaluator_assume_aliasing<Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const>, Eigen::internal::evaluator_traits<Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const> >::Shape>::value, void*>::type) (dst=..., src=..., func=...) at /usr/local/include/eigen3/Eigen/src/Core/AssignEvaluator.h:804
#17 0x00007f1a21de2fe2 in Eigen::internal::call_assignment<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const> > (dst=..., src=...) at /usr/local/include/eigen3/Eigen/src/Core/AssignEvaluator.h:782
#18 0x00007f1a21de2411 in Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >::_set<Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const> > (this=0x7ffe7ca9cf90, other=...)
    at /usr/local/include/eigen3/Eigen/src/Core/PlainObjectBase.h:714
#19 0x00007f1a21de10e1 in Eigen::Matrix<double, 3, 1, 0, 3, 1>::operator=<Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double, double>, Eigen::Product<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0> const, Eigen::Matrix<double, 3, 1, 0, 3, 1> const> > (this=0x7ffe7ca9cf90, other=...) at /usr/local/include/eigen3/Eigen/src/Core/Matrix.h:225

于是我发现了这个坑是Eigen3的一个bug。下巴都掉了。

继续找度娘呗,这次找到一个国外大婶的帖子,http://www.pcl-users.org/Segfaults-in-eigen-s-handmade-aligned-free-on-ubuntu-artful-td4045237.html

大致翻译了一下这个帖子,

照着里边说的几个方法试了一下:

1、编译的时候去掉-march=native,

     我查了一下,我的编译选项里都没有这个参数

2、编译的时候去掉-msse4.1 -mfpmath=sse

    我的编译参数里有这个,去掉了没有起作用,大婶那里好像也没有起作用

3、编译pcl时,使用这个参数,cmake <path> -DPCL_ENABLE_SSE=OFF

    我想起我当时pcl是使用的apt安装的,于是换用源码安装试试。

以上办法都没有起作用

最后又重新读了这个帖子,发现里边有一句话,

“Heads up, once the PR is merged, only the versions onwards will have the fix. We don't backport fixes.”

我终于意识到是剎问题了。我需要安装最新版的pcl,大婶已经回复了,这个bug已经在最新版上修复了。

于是我装了pcl-1.9,然后问题搞定了。虽然在装最新pcl的时候反反复复安装和卸载了很多库,但都不重要了。

 

另外,eigen3也有一堆版本,在找问题的时候,发现一个好用的版本,在这里记录一下

https://blog.youkuaiyun.com/sunyoop/article/details/79032174

<think>根据用户当前查询“如何使用Eigen3个点转换为一个矩阵”,结合历史引用内容(尤其是引用[1]涉及3D点操作),推断用户需求是将三个3D点组合成矩阵。Eigen库中常用MatrixXd存储点云数据。 用户未明确矩阵结构,但通常三点可组成3x3矩阵(每行一个点)。Eigen创建矩阵的方法如下: ```cpp #include <Eigen/Dense> using namespace Eigen; Matrix3d createMatrixFromPoints(Vector3d p1, Vector3d p2, Vector3d p3) { Matrix3d mat; mat << p1.transpose(), p2.transpose(), p3.transpose(); return mat; } ``` 若需列向量形式(每列一个点),则用`.colwise()`或直接赋值: ```cpp Matrix3d mat; mat.col(0) = p1; mat.col(1) = p2; mat.col(2) = p3; ``` 引用[1]提到3D点处理误差,暗示用户可能在处理投影或重建问题。因此补充说明:若点坐标以行向量存储需转置(如第一段代码),列向量则直接填充。 相关问题设计围绕Eigen矩阵操作和3D点应用: 1. 不同存储方式(行/列优先)的影响 2. 点云数据的大规模矩阵构造 3. 与引用[1]相关的3D重建误差计算 </think> 根据您的查询,我将结合Eigen库(C++线性代数库)和点云处理知识,详细说明如何将三个3D点转换为矩阵: ### 解决方案 在Eigen中,三个3D点可存储为 $3 \times 3$ 矩阵,**每列代表一个点的坐标**。假设有三个点: $$P_1 = \begin{pmatrix} x_1 \\ y_1 \\ z_1 \end{pmatrix}, \ P_2 = \begin{pmatrix} x_2 \\ y_2 \\ z_2 \end{pmatrix}, \ P_3 = \begin{pmatrix} x_3 \\ y_3 \\ z_3 \end{pmatrix}$$ #### 步骤: 1. **包含头文件**: ```cpp #include <Eigen/Dense> ``` 2. **创建点向量**: ```cpp Eigen::Vector3d p1(x1, y1, z1); // 点1 Eigen::Vector3d p2(x2, y2, z2); // 点2 Eigen::Vector3d p3(x3, y3, z3); // 点3 ``` 3. **组合成矩阵**: ```cpp Eigen::Matrix3d pointMatrix; pointMatrix.col(0) = p1; // 第一列为p1 pointMatrix.col(1) = p2; // 第二列为p2 pointMatrix.col(2) = p3; // 第三列为p3 ``` 最终矩阵形式: $$ \text{pointMatrix} = \begin{pmatrix} x_1 & x_2 & x_3 \\ y_1 & y_2 & y_3 \\ z_1 & z_2 & z_3 \end{pmatrix} $$ #### 完整示例 ```cpp #include <iostream> #include <Eigen/Dense> int main() { // 定义三个3D点 Eigen::Vector3d p1(1.0, 2.0, 3.0); Eigen::Vector3d p2(4.0, 5.0, 6.0); Eigen::Vector3d p3(7.0, 8.0, 9.0); // 组合为3x3矩阵 Eigen::Matrix3d mat; mat.col(0) = p1; mat.col(1) = p2; mat.col(2) = p3; std::cout << "Point Matrix:\n" << mat << std::endl; return 0; } ``` 输出: ``` Point Matrix: 1 4 7 2 5 8 3 6 9 ``` ### 关键说明 1. **数据结构选择**:`Eigen::Matrix3d` 是 $3 \times 3$ 双精度浮点矩阵,适合存储三个3D点。 2. **应用场景**:此类转换常用于点云处理、3D重建(如引用[1]提到的投影误差计算)[^1]或几何变换。 3. **转置需求**:若需每行存储一个点,使用 `mat.row(i) = p_i` 或最终转置 `mat.transpose()`。
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值