Eigen slerp 导致读取访问冲突 (C++/OpenGL)

Eigen slerp resulting in a Read Access Violation (C++/OpenGL)

我从 Eigen libary 呼叫 slerp() 如下:

Eigen::MatrixXf Rtime = (Eigen::Quaternionf::Identity().slerp(timer, quarts[i])).toRotationMatrix();

其中 timer 是一个浮点数,夸脱被声明为

std::vector<Eigen::Quaternionf> quarts;

对 slerp 的调用有时只会导致 读取访问冲突(大约 50% 的时间),这让我很困惑。

查看栈帧, 我可以看到代码达到 Eigen::internal::pload 直到它中断。

通常我会认为我的索引不正确,但即使在 i = 0quarts.size() = 1。我声明向量中唯一的四元数:

Eigen::Matrix3f rotMatrix;
    rotMatrix = U * V;
    Eigen::Quaternionf temp;
    temp = rotMatrix;
    quarts.push_back(temp);

其中 UV 来自奇异值分解的计算,所以我声明四元数的方式可能有问题?或者以某种方式将它存储在向量中会影响它?我不确定。

问题是 Quaternionf 需要 std::vector 不能保证的 16 字节对齐。更多详细信息 there。解决方案是使用对齐的分配器,例如:

std::vector<Quaternionf,Eigen::aligned_allocator<Quaternionf>> quats;

或在向量中使用非对齐四元数:

std::vector<Quaternion<float,Eigen::DontAlign>> quats;