前言
最近项目使用到了3D Slicer,slicer使用的vtk的vtkAnnotatedCubeActor方位标不符合产品美工需求,需要添加边框,这里给出一种添加边框的方法
一、实现方法
从slicer创建的vtkAnnotatedCubeActor对象中获取vtkAssembly,然后向其中添加一个新的frameactor,负责绘制边框。
frameactor实现方法:
创建vtkCubeSource对象,获取其8个顶点,通过各顶点创建多个cell,用于绘制连线,使用vtkTubeFilter将连线提升为管道。
cube的渲染仍由slicer默认流程通过MarkerRenderer实现。
二、代码
// 创建CubeActor,设置属性
this->CubeActor = vtkSmartPointer<vtkAnnotatedCubeActor>::New();
this->CubeActor->SetZFaceTextRotation(90);
this->CubeActor->GetTextEdgesProperty()->SetColor(0.95,0.95,0.95);
this->CubeActor->GetTextEdgesProperty()->SetLineWidth(2);
this->CubeActor->SetTextEdgesVisibility(0);
this->CubeActor->GetCubeProperty()->SetColor(0.15,0.15,0.15);
// 创建cube frame
vtkSmartPointer<vtkCubeSource> CubeSource = vtkSmartPointer<vtkCubeSource>::New();
CubeSource->SetBounds(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
CubeSource->SetCenter(0, 0, 0);
CubeSource->Update();
vtkPolyData* polyDatao = CubeSource->GetOutput();
vtkPoints* points = polyDatao->GetPoints();
//connection
vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
std::vector<std::vector<vtkIdType>> allconnections;
allconnections.push_back({ 0, 1, 3, 2, 0 });
allconnections.push_back({ 4,5,7,6,4 });
allconnections.push_back({ 0,4 });
allconnections.push_back({ 1,5 });
allconnections.push_back({ 2,6 });
allconnections.push_back({ 3,7 });
for (int i=0;i<allconnections.size();i++){
vtkIdType numIndices = allconnections[i].size();
lines->InsertNextCell(numIndices);
for (vtkIdType j = 0; j < numIndices; ++j) {
lines->InsertCellPoint(allconnections[i][j]);
}
}
// create vtkPolyData
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(lines);
vtkSmartPointer<vtkTubeFilter> TubeFilter = vtkSmartPointer<vtkTubeFilter>::New();
TubeFilter->SetInputData(polyData);
TubeFilter->SetNumberOfSides(6);
TubeFilter->SetRadius(0.04);//设置边框粗细
vtkSmartPointer<vtkPolyDataMapper> framemapper = vtkSmartPointer<vtkPolyDataMapper>::New();
framemapper->SetInputConnection(TubeFilter->GetOutputPort());
vtkSmartPointer<vtkActor> frameactor = vtkSmartPointer<vtkActor>::New();
frameactor->SetMapper(framemapper);
frameactor->GetProperty()->SetColor(255/255.0,0/255.0,0/255.0);//边框颜色
frameactor->GetProperty()->SetLighting(false);
frameactor->ForceOpaqueOn();
// 加入到assembly
vtkAssembly* assembly = this->CubeActor->GetAssembly();
assembly->AddPart(frameactor);