I take notes about how to rotate vector to a special direction.
#include "./tool.h"
#include <vtkTransform.h>
#include <vtkSmartPointer.h>
#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();
vtkSPtr<vtkTransform> RotateVecToSpecialDir(Point vec0, Point vec1)
{
vtkSPtrNew(trans, vtkTransform);
vec0.Unit();
vec1.Unit();
double radian = vtkMath::AngleBetweenVectors(vec0.point, vec1.point);
double degree = vtkMath::DegreesFromRadians(radian);
if (fabs(degree) < 0.0001)
{
return trans;
}
if (fabs(degree - 180.0) < 0.0001)
{
Point rotationAxis;
if (fabs(vec0[0]) < fabs(vec0[1]) - 1e-6)
{
rotationAxis = Point(1, 0, 0) ^ vec0;
}
else
{
rotationAxis = Point(0, 1, 0) ^ vec0;
}
rotationAxis.Unit();
trans->RotateWXYZ(180.0, rotationAxis[0], rotationAxis[1], rotationAxis[2]);
}
else
{
Point rotationAxis = vec0 ^ vec1;
rotationAxis.Unit();
trans->RotateWXYZ(degree, rotationAxis[0], rotationAxis[1], rotationAxis[2]);
}
trans->Update();
return trans;
}
int main(int, char *[])
{
PointStruct vec0( 1, 1, 0 );
PointStruct vec1( -1, 1, 0 );
PointStruct vecZ( 0, 0, 1 );
auto trans = RotateVecToSpecialDir( vec0, vec1 );
PointStruct tester = vec0;
trans->TransformVector( tester.point, tester.point );
cout << tester << endl;
return EXIT_SUCCESS;
}
[…] The post is based on https://www.weiy.city/2021/11/vtk-rotate-vector-to-special-direction/. […]