The post shows a way to create 2D grid on screen by VTK.

Show it by vtkActor2D because we regard it as a 2D object.
Its default coordinate system is viewport which is rendering. So we will have to know the window’s size.
Let’s make the process easier. Change the TransformCoordinate of mapper to view system.
The view system has x, y, and z in the range (-1, 1).

Then we can draw vertical lines one by one from left to right position. The process for drawing horizontal lines is same with the vertical’s.

Code snippet:

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkActor2D.h>
#include <vtkProperty2D.h>

#include "point.hpp"

using namespace std;

int main()
{
    vtkSmartPointer<vtkConeSource> cone =
            vtkSmartPointer<vtkConeSource>::New();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection( cone->GetOutputPort() );

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper( mapper );
    actor->GetProperty()->SetColor( 1, 0, 0 );

    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 );

    // ------------- start to add lines ----------------
    vtkSmartPointer<vtkPolyDataMapper2D> v1mmMapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
    vtkSmartPointer<vtkPolyData> v1mmPolyData = vtkSmartPointer<vtkPolyData>::New();
    vtkSmartPointer<vtkCellArray> v1mmLines = vtkSmartPointer<vtkCellArray>::New();
    vtkSmartPointer<vtkPoints> v1mmPoints = vtkSmartPointer<vtkPoints>::New();

    v1mmPolyData->SetPoints( v1mmPoints );
    v1mmPolyData->SetLines( v1mmLines );
    v1mmMapper->SetInputData( v1mmPolyData );

    vtkSmartPointer<vtkActor2D> v1mmLinesActor = vtkSmartPointer<vtkActor2D>::New();
    v1mmLinesActor->SetMapper( v1mmMapper );
    v1mmLinesActor->GetProperty()->SetColor( 1, 1, 1 );
    v1mmLinesActor->GetProperty()->SetLineWidth( 1 );

    vtkSmartPointer<vtkCoordinate> normCoords = vtkSmartPointer<vtkCoordinate>::New();
    normCoords->SetCoordinateSystemToView();
    v1mmMapper->SetTransformCoordinate( normCoords );

    Point tmpPt0( 0, -1, 0.0 );
    Point tmpPt1( 0, 1, 0.0 );
    int linesCount = 50;
    // draw vertical lines
    for( int i = -linesCount; i < linesCount; ++i )
    {
        Point pt0 = tmpPt0;
        pt0[0] = i*1.0/linesCount;
        vtkIdType pointId0 = v1mmPoints->InsertNextPoint(pt0.point);
        Point pt1 = tmpPt1;
        pt1[0] = i*1.0/linesCount;
        vtkIdType pointId1 = v1mmPoints->InsertNextPoint(pt1.point);
        vtkIdType lineIds[2] = { pointId0, pointId1 };
        v1mmLines->InsertNextCell(2, lineIds);
    }
    tmpPt0 = Point( -1, 0, 0 );
    tmpPt1 = Point( 1, 0, 0 );
    // draw horizontal lines
    for( int i = -linesCount; i < linesCount; ++i )
    {
        Point pt0 = tmpPt0;
        pt0[1] = i*1.0/linesCount;
        vtkIdType pointId0 = v1mmPoints->InsertNextPoint(pt0.point);
        Point pt1 = tmpPt1;
        pt1[1] = i*1.0/linesCount;
        vtkIdType pointId1 = v1mmPoints->InsertNextPoint(pt1.point);
        vtkIdType lineIds[2] = { pointId0, pointId1 };
        v1mmLines->InsertNextCell(2, lineIds);
    }

    v1mmPolyData->Modified();
    // ------------- adding red line finished----------------

    renderer->ResetCamera();

    renderer->AddActor2D( v1mmLinesActor );

    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}
Categories: VTK

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

3D Model Viewer: add grid plane and convex hull.
Add google translate tool at right-bottom position.

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