c++ - 将平移应用于特征顶点
问题描述
我正在尝试Eigen
使用从外部库以Vector3d
. 我不担心轮换。
我已经尝试了我有限的知识所Eigen
允许的一切。我当前的代码如下所示:
void Mesh::displace(const Vector3d& v)
{
Transform<double, 3, Affine> t = Transform<double, 3, Affine>::Identity();
t.translate(v);
}
现在我很难将此翻译应用于我的m_vertices
which is a MatrixXd
,3 by N
其中3
代表x, y, z
和N
代表顶点的列。
转换矩阵最终看起来像这样(X
s 表示转换的平移部分):
1, 0, 0, X,
0, 1, 0, X,
0, 0, 1, X,
0, 0, 0, 1
基于此,我很确定到目前为止我已经掌握了一切。
我已经多次尝试应用翻译,但编译的那些在运行时崩溃了。
解决方案
如果您只需要翻译,则不需要Transform
对象。您可以简单地将平移向量添加到网格中需要置换的每个位置向量。如果您需要旋转和平移,那么您需要分别应用它们
Output = Rotation*Input + Translation
下面的代码演示了这两种方法是等效的。
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Geometry>
using namespace Eigen;
int main(){
int N = 2;
// The translation vector
Vector3d translation;
translation << 1.0, 2.0, 3.0;
// The N points to be translated
Matrix3Xd points(3,N);
points.setRandom(3,N);
std::cout<< " The initial positions :" << std::endl
<< points << std::endl;
// ******************** Case 1: Pure Translation ******************** //
// Just add the translation vector to each point
Matrix3Xd displaced_points(3,N);
displaced_points = points.colwise() + translation;
std::cout<< " The displaced positions :" << std::endl
<< displaced_points << std::endl;
// **************** Case 2: Full Affine transformation **************** //
std::cout<< "******************** Method 2 ********************" << std::endl;
Transform<double_t, 3, Affine> t =
Transform<double_t, 3, Affine>::Identity();
t.translate(translation);
std::cout<< "The transformation: " << std::endl
<< t.translation() << std::endl;
// We need to apply the rotation and translation separately
for( auto i=0; i < N; ++i){
displaced_points.col(i) = t.linear()*points.col(i) + t.translation();
}
std::cout<< " The displaced positions :" << std::endl
<< displaced_points << std::endl;
return 0;
}
这会产生如下输出:
The initial positions :
0.680375 0.59688
-0.211234 0.823295
0.566198 -0.604897
The displaced positions :
1.68038 1.59688
1.78877 2.82329
3.5662 2.3951
******************** Method 2 ********************
The transformation:
1
2
3
The displaced positions :
1.68038 1.59688
1.78877 2.82329
3.5662 2.3951
推荐阅读
- reactjs - 如何从 React JS 中的 MYSQL 数据库中获取图像标签中的图像?
- c - 如何从C中的函数返回空字符串?
- kotlin - 如何同步这个包含函数的 kotlin 函数?
- python - 将文件移动到python中的文件夹
- python - I get and error when I try to do pip install
- java - 在 Spring Boot 项目中的多个数据库中执行搜索操作
- javascript - 如何仅显示最后的 Google Places 搜索结果
- azure-devops - Azure DevOps Extension 获取管道中执行任务的位置
- flutter - 使两个小部件与顶部中心对齐 Flutter
- python - 使用决策树分类器的自动决策