The post shows a way to calculate unsigned distance Between Two 3D Models.
The models can’t have too low resolution because the result that vtkDistancePolyDataFilter computes will not be accurate.

#include <vtkActor.h>
#include <vtkCleanPolyData.h>
#include <vtkDistancePolyDataFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkScalarBarActor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkSTLReader.h>

#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();


int main(int argc, char* argv[])
{
    vtkSmartPointer<vtkPolyData> input1;
    vtkSmartPointer<vtkPolyData> input2;

    vtkNew<vtkSphereSource> sphereSource1;
    sphereSource1->SetCenter(0.5, 0, 0);
    sphereSource1->SetPhiResolution(21);
    sphereSource1->SetThetaResolution(21);
    sphereSource1->Update();
    input1 = sphereSource1->GetOutput();

    vtkNew<vtkSphereSource> sphereSource2;
    sphereSource2->SetPhiResolution(21);
    sphereSource2->SetThetaResolution(21);
    sphereSource2->Update();
    input2 = sphereSource2->GetOutput();

    vtkNew<vtkNamedColors> colors;
    vtkSPtrNew(distanceFilter, vtkDistancePolyDataFilter);
    distanceFilter->SignedDistanceOff();
    distanceFilter->SetInputData(0,input1);
    distanceFilter->SetInputData(1,input2);
    distanceFilter->ComputeSecondDistanceOn();
    distanceFilter->Update();

    auto disMesh = distanceFilter->GetOutput();
    auto ptr = disMesh->GetCenter();
    cout << "ptr: " << ptr[0] << ", " << ptr[1] << ", " << ptr[2] << endl;

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

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);

    vtkNew<vtkScalarBarActor> scalarBar;
    scalarBar->SetLookupTable(mapper->GetLookupTable());
    scalarBar->SetTitle("Distance");
    scalarBar->SetNumberOfLabels(4);
    scalarBar->UnconstrainedFontSizeOn();

    vtkNew<vtkRenderWindow> renWin;
    renWin->SetSize(1200, 500);
    renWin->SetWindowName("DistancePolyDataFilter");

    vtkNew<vtkRenderWindowInteractor> renWinInteractor;
    renWinInteractor->SetRenderWindow(renWin);

    double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
    double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

    vtkSPtrNew( mapper1, vtkPolyDataMapper );
    mapper1->SetInputData( input1 );
    vtkSPtrNew( actor1, vtkActor );
    actor1->SetMapper( mapper1 );

    vtkSPtrNew( mapper2, vtkPolyDataMapper );
    mapper2->SetInputData( input2 );
    vtkSPtrNew( actor2, vtkActor );
    actor2->SetMapper( mapper2 );

    // Setup renderers
    vtkSPtrNew( leftRenderer, vtkRenderer );
    leftRenderer->SetViewport( leftViewport );
    leftRenderer->AddActor( actor1 );
    leftRenderer->AddActor( actor2 );
    leftRenderer->SetBackground(.6, .5, .4);
    leftRenderer->ResetCamera();

    vtkSPtrNew( rightRenderer, vtkRenderer );
    rightRenderer->SetViewport(rightViewport);
    rightRenderer->AddActor( actor );
    rightRenderer->AddActor2D( scalarBar );
    rightRenderer->SetBackground(.4, .5, .6);
    rightRenderer->SetActiveCamera( leftRenderer->GetActiveCamera() );

    renWin->AddRenderer( leftRenderer );
    renWin->AddRenderer( rightRenderer );

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


    return EXIT_SUCCESS;
}

Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

E-book Converter
: help you to convert E-book.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x