VTK-计算两个模型的距离DistancePolyDataFilter

(一)DistancePolyDataFilter的使用
1、官网教程:https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/DistancePolyDataFilter/

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 

int main(int argc, char* argv[])
{
  vtkSmartPointer<vtkPolyData> input1;
  vtkSmartPointer<vtkPolyData> input2;
  if (argc == 3)
  {
//     std::cerr << "Usage: " << argv[0]
//               << " filename1.vtk"
//               << " filename2.vtk" << std::endl;
    vtkSmartPointer<vtkPolyDataReader> reader1 =
      vtkSmartPointer<vtkPolyDataReader>::New();
    reader1->SetFileName(argv[1]);
    reader1->Update();
    input1 = reader1->GetOutput();

    vtkSmartPointer<vtkPolyDataReader> reader2 =
      vtkSmartPointer<vtkPolyDataReader>::New();
    reader2->SetFileName(argv[2]);
    reader2->Update();
    input2 = reader2->GetOutput();
  }
  else
  {
    vtkSmartPointer<vtkSphereSource> sphereSource1 =
      vtkSmartPointer<vtkSphereSource>::New();
    sphereSource1->SetCenter(1, 0, 0);
    sphereSource1->SetPhiResolution(21);
    sphereSource1->SetThetaResolution(21);
    sphereSource1->Update();
    input1 = sphereSource1->GetOutput();

    vtkSmartPointer<vtkSphereSource> sphereSource2 =
      vtkSmartPointer<vtkSphereSource>::New();
    sphereSource2->SetPhiResolution(21);
    sphereSource2->SetThetaResolution(21);
    sphereSource2->Update();
    input2 = sphereSource2->GetOutput();
  }

  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  vtkSmartPointer<vtkCleanPolyData> clean1 =
    vtkSmartPointer<vtkCleanPolyData>::New();
  clean1->SetInputData( input1);

  vtkSmartPointer<vtkCleanPolyData> clean2 =
    vtkSmartPointer<vtkCleanPolyData>::New();
  clean2->SetInputData( input2);

  vtkSmartPointer<vtkDistancePolyDataFilter> distanceFilter =
    vtkSmartPointer<vtkDistancePolyDataFilter>::New();

  distanceFilter->SetInputConnection( 0, clean1->GetOutputPort() );
  distanceFilter->SetInputConnection( 1, clean2->GetOutputPort() );
  distanceFilter->Update();

  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection( distanceFilter->GetOutputPort() );
  mapper->SetScalarRange(
    distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[0],
    distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[1]);

  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper( mapper );

  vtkSmartPointer<vtkScalarBarActor> scalarBar =
    vtkSmartPointer<vtkScalarBarActor>::New();
  scalarBar->SetLookupTable(mapper->GetLookupTable());
  scalarBar->SetTitle("Distance");
  scalarBar->SetNumberOfLabels(4);
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->SetBackground(colors->GetColor3d("Silver").GetData());//背景色
  renderer->SetBackground2(colors->GetColor3d("Gold").GetData());//背景色
  renderer->GradientBackgroundOn();

  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->AddRenderer( renderer );

  vtkSmartPointer<vtkRenderWindowInteractor> renWinInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renWinInteractor->SetRenderWindow( renWin );

  renderer->AddActor( actor );
  renderer->AddActor2D(scalarBar);

  renWin->Render();
  renWinInteractor->Start();

  return EXIT_SUCCESS;
}

效果图:
VTK-计算两个模型的距离DistancePolyDataFilter_第1张图片
2、实际用到的:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[]) {
    vtkSmartPointer<vtkPolyData> input1;
    vtkSmartPointer<vtkPolyData> input2;
    
    vtkSmartPointer<vtkSTLReader> reader1 =
        vtkSmartPointer<vtkSTLReader>::New();
    reader1->SetFileName("../data/large.stl");
    reader1->Update();
    input1 = reader1->GetOutput();
    vtkSmartPointer<vtkSTLReader> reader2 =
        vtkSmartPointer<vtkSTLReader>::New();
    reader2->SetFileName("../data/small.stl");
    reader2->Update();
    input2 = reader2->GetOutput();

    vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

    vtkSmartPointer<vtkCleanPolyData> clean1 =
        vtkSmartPointer<vtkCleanPolyData>::New();
    clean1->SetInputData(input1);

    vtkSmartPointer<vtkCleanPolyData> clean2 =
        vtkSmartPointer<vtkCleanPolyData>::New();
    clean2->SetInputData(input2);

    vtkSmartPointer<vtkDistancePolyDataFilter> distanceFilter =
        vtkSmartPointer<vtkDistancePolyDataFilter>::New();
    distanceFilter->SetInputConnection(0, clean1->GetOutputPort());
    distanceFilter->SetInputConnection(1, clean2->GetOutputPort());
    distanceFilter->Update();
//修改色调颜色由蓝到红
    vtkSmartPointer<vtkLookupTable> lut =
        vtkSmartPointer<vtkLookupTable>::New();
    lut->SetHueRange(0.6667, 0.0);//大致走向(0.83, 0.0)色调从红色到蓝色,(0,0.67)蓝色到红色
//    lut->SetAlphaRange(1.0, 1.0);
//    lut->SetValueRange(1.0, 1.0);
//    lut->SetSaturationRange(1.0, 1.0);
//    lut->SetNumberOfTableValues(256);
//    lut->SetRange(scalarRange);
    lut->Build();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(distanceFilter->GetOutputPort());
    mapper->SetLookupTable(lut);
//    mapper->SetScalarRange(
//    distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[0],
//    distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[1]);//获取映射的范围
    mapper->SetScalarRange(0.2, 2);//设置映射的范围
    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    //vtkScalarBar用于绘制分级的颜色图例,一般与vtkLookupTable配套使用
    vtkSmartPointer<vtkScalarBarActor> scalarBar =
        vtkSmartPointer<vtkScalarBarActor>::New();
    scalarBar->SetLookupTable(mapper->GetLookupTable());
    scalarBar->SetTitle("Distance");
    scalarBar->SetNumberOfLabels(10);//设置要显示的刻度标签数。
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->SetBackground(colors->GetColor3d("White").GetData());//调整背景颜色
    renderer->SetBackground2(colors->GetColor3d("White").GetData());//调整背景颜色
    renderer->GradientBackgroundOn();

    vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renWinInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renWinInteractor->SetRenderWindow(renWin);

    renderer->AddActor(actor);
    renderer->AddActor2D(scalarBar);

    renWin->Render();
    renWinInteractor->Start();

    return EXIT_SUCCESS;
}

VTK-计算两个模型的距离DistancePolyDataFilter_第2张图片

(二)关于颜色映射方面的内容:
1、https://blog.csdn.net/qq_40734628/article/details/88095668
2、vTK颜色渲染-vtkLookupTable
https://blog.csdn.net/weixin_44679836/article/details/102579531

ScalarBar的用法,可以根据自己的需要来设置scalarBar的属性。
ScalarBar和lookupTable结合使用方法

你可能感兴趣的:(VTK)