The article shows how to create a subclass which inherits vtkPolyDataAlgorithm and use it in vtk pipeline mechanism.
We create a star source object which is like vtkSphereSource and display it by a vtkActor object.
The star source class is very simple, it has just five points and five lines.

class StarSource: public vtkPolyDataAlgorithm
{
public:
    vtkTypeMacro(StarSource, vtkPolyDataAlgorithm);
    static StarSource *New();
protected:
    StarSource()
    {
        this->SetNumberOfInputPorts(0);
    }
    ~StarSource()override {}
    int RequestData
        (
        vtkInformation *vtkNotUsed(request),
        vtkInformationVector **vtkNotUsed(inputVector),
        vtkInformationVector *outputVector
        ) override
    {
        // get the info object
        vtkInformation *outInfo = outputVector->GetInformationObject(0);

        // get the ouptut
        vtkPolyData *output = vtkPolyData::SafeDownCast(
          outInfo->Get(vtkDataObject::DATA_OBJECT()));

        vtkPoints *newPoints = vtkPoints::New();
/*
A(0, 1)
B(cos18°,sin18°)
C(cos54°,-sin54°)
D(-cos54°,-sin54°)
E(-cos18°,sin18°)
*/
        double PI = vtkMath::Pi();
        newPoints->InsertNextPoint( 0, 1, 0 );
        newPoints->InsertNextPoint( cos(PI * 18 / 180.0), sin( PI * 18 / 180 ), 0 );
        newPoints->InsertNextPoint( cos(PI * 54 / 180.0), -sin( PI * 54 / 180 ), 0 );
        newPoints->InsertNextPoint( -cos(PI * 54 / 180.0), -sin( PI * 54 / 180 ), 0 );
        newPoints->InsertNextPoint( -cos(PI * 18 / 180.0), sin( PI * 18 / 180 ), 0 );
        output->SetPoints( newPoints );

        vtkCellArray *newLines = vtkCellArray::New();
        vtkIdType line[2] = { 0 };
        line[0] = 0; line[1] = 1;
        newLines->InsertNextCell( 2, line );
        line[0] = 1; line[1] = 2;
        newLines->InsertNextCell( 2, line );
        line[0] = 2; line[1] = 3;
        newLines->InsertNextCell( 2, line );
        line[0] = 3; line[1] = 4;
        newLines->InsertNextCell( 2, line );
        line[0] = 4; line[1] = 0;
        newLines->InsertNextCell( 2, line );
        output->SetLines( newLines );

        newPoints->Delete();
        newLines->Delete();

        cout << "works: " << output->GetNumberOfCells() << endl;

        return 1;
    }
private:
    StarSource(const StarSource&) = delete;
    void operator=(const StarSource&) = delete;
};

vtkStandardNewMacro(StarSource);

int main(int, char *[])
{
    vtkSmartPointer<StarSource> star = vtkSmartPointer<StarSource>::New();

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

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

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

    return EXIT_SUCCESS;
}




0 0 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] debug information by the custom class which inherits vtkPolyDataAlgorithm, relevant article: 【VTK】Create source class which is derived from vtkPolyDataAlgorithm We just need to Star->DebugOn() and use vtkDebugMacro as std::cout. […]

trackback

[…] debug information by the custom class which inherits vtkPolyDataAlgorithm, relevant article: 【VTK】Create source class which is derived from vtkPolyDataAlgorithm We just need to Star->DebugOn() and use vtkDebugMacro as std::cout. […]

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