[VTK] 四元素实现旋转平移

发布于:2025-04-13 ⋅ 阅读:(98) ⋅ 点赞:(0)

VTK 实现旋转,有四元数的方案,也有 vtkTransform 的方案;主要示例代码如下:

    //构造旋转四元数
    vtkQuaterniond rotation;
    rotation.SetRotationAngleAndAxis(vtkMath::RadiansFromDegrees(90.0),0.0, 1.0, 0.0);
    
    //构造旋转点四元数
    vtkQuaterniond p;
    p.Set(0.0,1.0,0.0,0.0);
    
    //转置
    vtkQuaterniond inverse = rotation.Inverse();
    
    //计算旋转结果
    vtkQuaterniond res = rotation*p*inverse;
    double resdata[4] = {0};
    res.Get(resdata);
vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
trans->PostMultiply();
trans->Translate(1, 0, 0);
trans->RotateZ(45);
import math
import numpy as np
import vtk


def radians_to_degrees(radians):
    return radians * (180.0 / math.pi)


def degrees_to_radians(degree):
    return degree / 180.0 * math.pi

# 示例使用
radians = 1.0
degrees = radians_to_degrees(radians)

polyline_pts = [[math.sin(degrees_to_radians(60)), math.cos(degrees_to_radians(60)), 0],
                [math.sin(degrees_to_radians(30)), math.cos(degrees_to_radians(30)), 0.0]]

t = np.cross(np.array(polyline_pts[0]), np.array(polyline_pts[1]))
print(t)

print(polyline_pts[0])

# 创建点
points = vtk.vtkPoints()
points.InsertNextPoint(0, 0, 0)
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(1, 1, 0)
points.InsertNextPoint(0, 1, 0)

# 创建单元数组
lines = vtk.vtkCellArray()
lines.InsertNextCell(4)
lines.InsertCellPoint(0)
lines.InsertCellPoint(1)
lines.InsertCellPoint(2)
lines.InsertCellPoint(3)

# 创建多边形数据
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetLines(lines)

# 创建映射器
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(polydata)

# 创建actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# 创建渲染器、渲染窗口和渲染窗口交互器
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# 添加actor到渲染器并进行渲染
renderer.AddActor(actor)
renderer.SetBackground(0, 0, 0)
renderWindow.SetSize(640, 480)
renderWindow.Render()
renderWindowInteractor.Start()

网站公告

今日签到

点亮在社区的每一天
去签到