Third, create a set of display points

I. Description 

       vtk definition data in the time necessary to define the geometry and topology data.

       Coordinate geometry is the point of

       Connection topology is the point

Second, the program description

       Create and display a triangle

Third, the program code

#include <vtkPoints.h>
#include <vtkLine.h>
#include <vtkCellArray.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int argc,four* The argv []) 
{ 
    // create three coordinate points 
    vtkSmartPointer <vtkPoints> Points = vtkSmartPointer <vtkPoints> :: New (); 
    Points -> InsertNextPoint ( 1.0 , 0.0 , 0.0 ); // Returns the ID of the first point : 0 
    points-> InsertNextPoint ( 0.0 , 0.0 , 4.0 ); // returns the second point ID:. 1 
    points-> InsertNextPoint ( 0.0 , 0.0 , 0.0 ); // returns the third point ID: 2 

    // create a line between each two coordinate points are
     // SetId () the first argument is the endpoint of a segment ID, and the second parameter is the connection point of the ID
    vtkSmartPointer<vtkLine> line0 = vtkSmartPointer<vtkLine>::New();
    line0->GetPointIds()->SetId ( 0,0 ); 
    line0->GetPointIds()->SetId ( 1,1 );

    vtkSmartPointer<vtkLine> line1 = vtkSmartPointer<vtkLine>::New();
    line1->GetPointIds()->SetId ( 0,1 );
    line1->GetPointIds()->SetId ( 1,2 );

    vtkSmartPointer<vtkLine> line2 = vtkSmartPointer<vtkLine>::New();
    line2->GetPointIds()->SetId ( 0 , 2 ); 
    line2 -> GetPointIds () -> SetId ( . 1 , 0 ); 

    // Create Cell array, for storing a line segment created above 
    vtkSmartPointer <vtkCellArray> = vtkSmartPointer Lines <vtkCellArray> :: New (); 
    Lines -> InsertNextCell (Line0); 
    lines -> InsertNextCell (line1); 
    lines -> InsertNextCell (line2); 

    vtkSmartPointer <vtkPolyData> = vtkSmartPointer POLYDATA <vtkPolyData> :: New (); 

    // to point and line added to the data set, the former specifies the geometry data set, which specifies its topology 
    polydata-> SETPOINTS (Points); 
    POLYDATA ->SetLines ( lines );
    
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polydata);//读取数据用SetInputData
    
    vtkSmartPointer<vtkActor>actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

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

    vtkSmartPointer <vtkRenderWindow> Ruins vtkSmartPointer = <vtkRenderWindow> :: New (); 
    Ruins -> AddRenderer (renderer); 
    Ruins -> SetSize ( 600 , 600 ); 
    
    vtkSmartPointer <vtkRenderWindowInteractor> = rwinInter 
        vtkSmartPointer <vtkRenderWindowInteractor> :: New (); 
    rwinInter -> SetRenderWindow (Ruins); 
    rwinInter -> Initialize (); 
    rwinInter -> Start (); 

    return EXIT_SUCCESS; 
}
View Code

      Show results:

     

 

 Fourth, program analysis

        Program data object ploydata, 

       Which specify geometry (Points) and topology (Lines)

       Unit type using vtkLine.

Fifth, modify

      1- cube is displayed using ploydata:

#include <vtkPoints.h>
#include <vtkLine.h>
#include <vtkCellArray.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int argc,four *argv[])
{
       
    //显示一个立体形状
    vtkSmartPointer<vtkConeSource>coneSource = vtkSmartPointer<vtkConeSource>::New();
    coneSource->Update();
        vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata = coneSource->GetOutput();
    
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polydata);//读取数据用SetInputData
    
    vtkSmartPointer<vtkActor>actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

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

    vtkSmartPointer<vtkRenderWindow>rwin = vtkSmartPointer<vtkRenderWindow>::New();
    rwin->AddRenderer(renderer);
    rwin->SetSize(600, 600);
    
    vtkSmartPointer<vtkRenderWindowInteractor>rwinInter =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    rwinInter->SetRenderWindow(rwin);
    rwinInter->Initialize();
    rwinInter->Start();

    return EXIT_SUCCESS;
}
View Code

       

      2- display line tetrahedron

#include <vtkPoints.h>
#include <vtkLine.h>
#include <vtkCellArray.h>
#include <vtkCellType.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkConeSource.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL
VTK_MODULE_INIT (vtkInteractionStyle);
 int main ( int argc, char * the argv []) 
{ 
    // create three coordinate points 
    vtkSmartPointer <vtkPoints> Points = vtkSmartPointer <vtkPoints> :: New (); 
    Points -> InsertNextPoint ( 1.0 , 0.0 , 0.0 ); // returns the ID of the first point: 0 
    points-> InsertNextPoint ( 0.0 , 0.0 , 4.0 ); // returns the ID of the second point:. 1 
    points-> InsertNextPoint ( 0.0 , 0.0 , 0.0 ); / /Returns the third point ID: 2 
    points-> InsertNextPoint ( 0.0 , 2.0 , 0.0 ); // returns the third point ID:. 3 
    /// are created between / two coordinate points for each line
     /// the first parameter / SetId () is the endpoint of the segment ID, and the second parameter is the ID of the connection point 
    vtkSmartPointer <vtkLine> Line0 = vtkSmartPointer <vtkLine> :: New (); 
    
    Line0 -> GetPointIds () -> SetId ( 0 , 0 ); 
    Line0 -> GetPointIds () -> SetId ( . 1 , . 1 ); 

    vtkSmartPointer <vtkLine> line1 = vtkSmartPointer <vtkLine> :: New (); 
    line1 -> GetPointIds () -> SetId ( 0 ,1 );
    line1->GetPointIds()->SetId ( 1,2 );

    vtkSmartPointer<vtkLine> line2 = vtkSmartPointer<vtkLine>::New();
    line2->GetPointIds()->SetId ( 0,2 );
    line2->GetPointIds()->SetId ( 1,0 );

    vtkSmartPointer<vtkLine> line3 = vtkSmartPointer<vtkLine>::New();
    line3->GetPointIds()->SetId(0, 0);
    line3->GetPointIds()->SetId(1, 3);
    vtkSmartPointer :: New ();<vtkLine> line4 = vtkSmartPointer <vtkLine> :: New (); 
    line4 -> GetPointIds () -> SetId ( 0 , . 3 ); 
    line4 -> GetPointIds () -> SetId ( . 1 , . 1 ); 
    vtkSmartPointer <vtkLine> LINE5 vtkSmartPointer = <vtkLine> :: New (); 
    LINE5 -> GetPointIds () -> SetId ( 0 , . 3 ); 
    LINE5 -> GetPointIds () -> SetId ( . 1 , 2 );
     /// / Cell array created with segment for storing created above 
    vtkSmartPointer <vtkCellArray> = vtkSmartPointer Lines <vtkCellArray> 
    
    Lines ->InsertNextCell (Line0); 
    Lines -> InsertNextCell (line1); 
    Lines -> InsertNextCell (line2); 
    Lines -> InsertNextCell (Iine3); 
    Lines -> InsertNextCell (line4); 
    Lines -> InsertNextCell (LINE5); 
    vtkSmartPointer <vtkPolyData> POLYDATA vtkSmartPointer = <vtkPolyData> :: New (); 

    // to point and line added to the data set, the data set specified geometry former, which specifies its topology 
    polydata-> SETPOINTS (points); 
    POLYDATA -> SetLines (lines); 

    // display a stereoscopic shape 
    / *vtkSmartPointer<vtkConeSource>coneSource = vtkSmartPointer<vtkConeSource>::New();
    coneSource->Update();
    polydata = coneSource->GetOutput();*/
    
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polydata);
    
    vtkSmartPointer<vtkActor>actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

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

    vtkSmartPointer <vtkRenderWindow> Ruins vtkSmartPointer = <vtkRenderWindow> :: New (); 
    Ruins -> AddRenderer (renderer); 
    Ruins -> SetSize ( 600 , 600 ); 
    vtkSmartPointer <vtkRenderWindowInteractor> = rwinInter 
        vtkSmartPointer <vtkRenderWindowInteractor> :: New (); 
    rwinInter -> SetRenderWindow (Ruins); 
    rwinInter -> Initialize (); 
    rwinInter -> Start (); 

    return EXIT_SUCCESS;
}
View Code

       

Guess you like

Origin www.cnblogs.com/fantianliang/p/12498509.html