The article shows how to combine two mesh objects to one based on VTK.
The code snippet is in the following part.



#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>

#define vtkPtr( var, className ) vtkSmartPointer<className> var = \
    vtkSmartPointer<className>::New()

vtkSmartPointer<vtkPolyData> AddTwoPolyData(vtkSmartPointer<vtkPolyData> data1, vtkSmartPointer<vtkPolyData> data2)
{
    vtkPtr( resultPts, vtkPoints );
    for( vtkIdType i = 0; i < data1->GetNumberOfPoints(); ++i )
    {
        double pointPos[3];
        data1->GetPoint(i, pointPos);
        resultPts->InsertNextPoint(pointPos);
    }
    for (vtkIdType i = 0; i < data2->GetNumberOfPoints(); ++i)
    {
        double pointPos[3];
        data2->GetPoint(i, pointPos);
        resultPts->InsertNextPoint(pointPos);
    }

    vtkPtr(resultCells, vtkCellArray);
    for (vtkIdType i = 0; i < data1->GetNumberOfCells(); i++)
    {
        resultCells->InsertNextCell(data1->GetCell(i));
    }
    vtkIdType input2StartId = data1->GetNumberOfPoints();
    for (vtkIdType i = 0; i < data2->GetNumberOfCells(); i++)
    {
        vtkIdType nPoints;
        const vtkIdType *pts;
        data2->GetCellPoints( i, nPoints, pts );
        resultCells->InsertNextCell( nPoints );
        for(int j = 0; j < nPoints; j++ )
            resultCells->InsertCellPoint(input2StartId+pts[j]);
    }
    vtkPtr( resultPd, vtkPolyData );
    resultPd->SetPoints( resultPts );
    resultPd->SetPolys( resultCells );

    return resultPd;
}

int main()
{
    setbuf( stdout, nullptr );

    vtkPtr( cone, vtkConeSource );
    cone->Update();
    vtkPtr( sphere, vtkSphereSource );
    sphere->Update();

    auto data = AddTwoPolyData( cone->GetOutput(), sphere->GetOutput() );
    vtkPtr( mapper, vtkPolyDataMapper );
    mapper->SetInputData( data );

    vtkPtr( actor, vtkActor );
    actor->SetMapper( mapper );

    vtkPtr( renderer, vtkRenderer );
    renderer->SetBackground( 0, 0, 0 );
    renderer->AddActor( actor );

    vtkPtr( renderWindow, vtkRenderWindow );
    renderWindow->AddRenderer( renderer );

    vtkPtr( renderWindowInteractor, vtkRenderWindowInteractor );
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

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

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