2021-06-16 17:01

如何在vtk中旋转平面？或者更一般的问题，如何在三维中旋转一个平面？

1. 按照StackOverflow上的答案，构造了答案中提到的矩阵，但是结果不对。结果如下图所示。

``````{
//目标法向量方向
vtkVector3d vec_normal(x - x_before, y - y_before, z - z_before);
vec_normal = vec_normal.Normalized();
//z_direction 为原始法向量方向
vtkVector3d cross_direction = vec_normal.Cross(z_direction).Normalized();

double theta = vtkMath::AngleBetweenVectors(vec_normal.GetData(), z_direction.GetData());
double c = theta;
double s = std::sqrt(1 - theta * theta);
double C = 1 - theta;

vtkSmartPointer<vtkMatrix4x4> m = vtkSmartPointer<vtkMatrix4x4>::New();
double ma[4][4] = { {cross_direction.GetX()*cross_direction.GetX()*C + c,cross_direction.GetX()*cross_direction.GetY()*C - cross_direction.GetZ()*s,cross_direction.GetX()*cross_direction.GetZ()*C + cross_direction.GetY()*s,0}
,{cross_direction.GetY()*cross_direction.GetX()*C + cross_direction.GetZ()*s,cross_direction.GetY()*cross_direction.GetY()*C + c,cross_direction.GetY()*cross_direction.GetZ()*C - cross_direction.GetX()*s,0}
,{cross_direction.GetZ()*cross_direction.GetX()*C - cross_direction.GetY()*s,cross_direction.GetZ()*cross_direction.GetY()*C + cross_direction.GetX()*s,cross_direction.GetZ()*cross_direction.GetZ()*C + c,0}
,{0,0,0,1} };
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
//void SetElement(int i, int j, double value),i行、j列的值为value
m->SetElement(i, j, ma[i][j]);
}
}

vtkNew<vtkTransform> trans;
trans->Translate(x, y, z);
trans->SetMatrix(m);

//trans->RotateWXYZ(theta, cross_direction[0], cross_direction[1], cross_direction[2]);
vtkNew<vtkTransformPolyDataFilter> pTransformPolyDataFilter;
pTransformPolyDataFilter->SetInputData(circle);
pTransformPolyDataFilter->SetTransform(trans);
pTransformPolyDataFilter->Update();

circle = pTransformPolyDataFilter->GetOutput();
}``````

``````vtkNew<vtkTransform> trans;
trans->Translate(x, y, z);