思路:首先分别读取几个STL文件,创建Actor,设置位置、平移及旋转,创建每一个Actor对应的Mapper,绑定,最后创建render将多个Actor绑定render即可,同时显示多个STL模型。
std::string filename ="./data/mesh1.stl";//E:\\VTKDemo\\build\\data\\data.stl
vtkSmartPointer<vtkSTLReader> reader=vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
std::string filename1 ="./data/data.stl";//E:\\VTKDemo\\build\\data\\data.stl
vtkSmartPointer<vtkSTLReader> reader1=vtkSmartPointer<vtkSTLReader>::New();
reader1->SetFileName(filename1.c_str());
reader1->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper1 = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
mapper1->SetInputConnection(reader1->GetOutputPort());
vtkSmartPointer<vtkTransform> pTransform = vtkSmartPointer<vtkTransform>::New();
pTransform->PostMultiply();
//pTransform->RotateX(90);//x轴旋转90度
pTransform->Translate(50,0,180);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
//actor->SetUserTransform(pTransform);
actor->SetPosition(50,0,180);
vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New();
actor1->SetMapper(mapper1);
actor1->SetPosition(0,0,0);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->AddActor(actor1);
renderer->SetBackground(.3, .6, .3); // Background color green
renderWindow->Render();
vtkSmartPointer<vtkSTLWriter> partW= vtkSmartPointer<vtkSTLWriter>::New();
vtkSmartPointer<vtkAppendPolyData> apd= vtkSmartPointer<vtkAppendPolyData>::New();
vtkSmartPointer<vtkActorCollection> actors=vtkSmartPointer<vtkActorCollection>::New();
actors=renderer->GetActors();//获取当前RENDER的ACTORS
int iSum = actors->GetNumberOfItems();//获得这个渲染器内拥有的actor的数量
actors->InitTraversal();//初始化访问便利链表,这两句话不可颠倒,初始化后获得的actor总数就不对了
cout<<iSum<<endl;
for (int i = 0;i < iSum;i++)
{
vtkSmartPointer<vtkActor> pA = vtkSmartPointer<vtkActor>::New();
pA = actors->GetNextActor();//遍历每一个actor
double* pPos = pA->GetPosition();
pA->SetPosition(pPos[0],pPos[1],pPos[2]);
cout<<pPos[0]<<" "<<pPos[1]<<" "<<pPos[2]<<endl;
/*
if (abs(pPos[0]) < ESP && abs(pPos[1]) < ESP && abs(pPos[2]) <ESP)//有没有平移运动
{
vtkSmartPointer<vtkPolyDataMapper> pMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
pMapper= (vtkPolyDataMapper*)pA->GetMapper();
apd->AddInputData(pMapper->GetInput());
continue;
}
*/
vtkSmartPointer<vtkPolyDataMapper> pMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
pMapper= (vtkPolyDataMapper*)pA->GetMapper();
vtkSmartPointer<vtkPolyData> pData = vtkSmartPointer<vtkPolyData>::New();
pData=pMapper->GetInput();
vtkSmartPointer<vtkPoints> pPoints = vtkSmartPointer<vtkPoints>::New();
pPoints=pData->GetPoints();
vtkIdType iSumPts=pPoints->GetNumberOfPoints();//获得三角片点集
cout<<iSumPts<<endl;
for (int j = 0;j < iSumPts;j++)
{
double tmp[3];
pPoints->GetPoint(j,tmp);
for(int k=0;k<3;k++)
{
tmp[k]+=pPos[k];
}
pPoints->SetPoint(j,tmp);
}
apd->AddInputData(pData);
apd->Update();
}
//vtkSmartPointer<vtkTriangleFilter> triangleFilter = vtkSmartPointer<vtkTriangleFilter>::New();//三角片过滤器
//triangleFilter->SetInputConnection(apd->GetOutputPort());//导入合并后的polydata
partW->SetInputData(apd->GetOutput());//将三角化后的模型数据导入STL输出器中
std::string filename2 ="./data/BInd.stl";//E:\\VTKDemo\\build\\data\\data.stl
partW->SetFileName(filename2.c_str());//设置输出文件名称
partW->SetFileTypeToBinary(); //设置输出的STL文件为二进制类型,二进制占用更小的内存
partW->Write();//写入文件
renderWindowInteractor->Start();
return 0;