The article shows a few kinds of widgets which are used to measure the distance between two points, angle between two rays, and the bidimensional length of an object.

Distance

Measure The Distance On 2D Model

We need vtkDistanceWidget, vtkDistanceRepresentation2D and vtkPointHandleRepresentation2D to help us to calculate the distance between two points on 3D model. Both points can be moved flexibly.

int main()
{
    vtkSmartPointer<vtkPNGReader> reader =
            vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName( "/Users/weiyang/Desktop/Untitled.png" );
    reader->Update();

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();

    vtkSmartPointer<vtkImageViewer2> imageViewer =
            vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputData( reader->GetOutput() );
    imageViewer->SetColorWindow( 256 );
    imageViewer->SetColorLevel( 127.5 );
    imageViewer->SetupInteractor( renderWindowInteractor );

    vtkPointHandleRepresentation2D *handle = vtkPointHandleRepresentation2D::New();
    vtkDistanceRepresentation2D *rep = vtkDistanceRepresentation2D::New();
    rep->SetHandleRepresentation( handle );

    vtkDistanceWidget *widget = vtkDistanceWidget::New();
    widget->SetInteractor( renderWindowInteractor );
    widget->SetRepresentation( rep );
    widget->SetWidgetStateToManipulate();
    widget->EnabledOn();
    widget->ProcessEventsOn();

    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->Render();
    renderWindowInteractor->Start();

    handle->Delete();
    rep->Delete();
    widget->Delete();
    return 0;
}


Measure The Distance On 3D Model

This scenario is similar to the above last example, but we put both points on the 3D model in the following code.
We have to convert vtkDistanceRepresentation2D and vtkPointHandleRepresentation2D to vtkDistanceRepresentation3D and vtkPointHandleRepresentation3D individually.

int main()
{
    vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData( sphereSource->GetOutput() );

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

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor( actor );
    renderer->SetBackground( 0, 0, 0 );

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

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    vtkSmartPointer<vtkPointHandleRepresentation3D> handle =
            vtkSmartPointer<vtkPointHandleRepresentation3D>::New();
    vtkSmartPointer<vtkDistanceRepresentation3D> rep =
            vtkSmartPointer<vtkDistanceRepresentation3D>::New();
    rep->SetHandleRepresentation( handle );

    vtkSmartPointer<vtkDistanceWidget> widget =
            vtkSmartPointer<vtkDistanceWidget>::New();
    widget->SetInteractor( renderWindowInteractor );
    widget->SetRepresentation( rep );
    widget->SetWidgetStateToManipulate();
    widget->EnabledOn();
    widget->ProcessEventsOn();

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();

    return 0;
}


Angle

The examples in the angle section also show different measuring situations in 2D and 3D.
Let the code and images to tell you the details of angle measuring.

Measure Angle For 2D Model

int main()
{
    vtkSmartPointer<vtkPNGReader> reader =
            vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName( "/Users/weiyang/Desktop/Untitled.png" );
    reader->Update();

    vtkSmartPointer<vtkImageActor> actor =
            vtkSmartPointer<vtkImageActor>::New();
    actor->GetMapper()->SetInputConnection( reader->GetOutputPort() );

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
    renderer->SetActiveCamera( camera );
    renderer->AddActor( actor );
    renderer->SetBackground( 0, 0, 0 );

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

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    vtkPointHandleRepresentation2D *handle = vtkPointHandleRepresentation2D::New();
    vtkAngleRepresentation2D *rep = vtkAngleRepresentation2D::New();
    rep->SetHandleRepresentation(handle);

    vtkAngleWidget *widget = vtkAngleWidget::New();
    widget->SetInteractor( renderWindowInteractor );
    widget->SetRepresentation(rep);
    widget->EnabledOn();

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();

    handle->Delete();
    rep->Delete();
    widget->Delete();
    return 0;
}


Measure Angle For 3D Model

    vtkSmartPointer<vtkPointHandleRepresentation3D> handle =
            vtkSmartPointer<vtkPointHandleRepresentation3D>::New();
    vtkSmartPointer<vtkAngleRepresentation3D> rep =
            vtkSmartPointer<vtkAngleRepresentation3D>::New();
    rep->SetHandleRepresentation(handle);

    vtkSmartPointer<vtkAngleWidget> widget =
            vtkSmartPointer<vtkAngleWidget>::New();
    widget->SetInteractor( renderWindowInteractor );
    widget->SetRepresentation(rep);
    widget->EnabledOn();


BiDimensional

We use two finite, orthogonal lines that intersect within the finite extent of both lines to do bi-dimensional measure .
There are four end points can be positioned in the measuring.

int main()
{
    vtkSmartPointer<vtkPNGReader> reader =
            vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName( "/Users/weiyang/Desktop/Untitled.png" );
    reader->Update();

    vtkSmartPointer<vtkImageActor> actor =
            vtkSmartPointer<vtkImageActor>::New();
    actor->GetMapper()->SetInputConnection( reader->GetOutputPort() );

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
    renderer->SetActiveCamera( camera );
    renderer->AddActor( actor );
    renderer->SetBackground( 0, 0, 0 );

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

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    vtkBiDimensionalRepresentation2D *rep = vtkBiDimensionalRepresentation2D::New();
    vtkBiDimensionalWidget *widget = vtkBiDimensionalWidget::New();
    widget->SetInteractor( renderWindowInteractor );
    widget->SetRepresentation( rep );
    widget->EnabledOn();

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();

    rep->Delete();
    widget->Delete();
    return 0;
}


 


0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
DuongLe
DuongLe
4 years ago

how to measure area of rectangle by using vtk?

trackback

[…] Im trying to somewhat create a ruler of some sort as seen in this resource which is made to measure models within vtk itself: https://www.weiy.city/2019/08/vtk-measurement-widgets/ […]

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
3
0
Would love your thoughts, please comment.x
()
x