【VTK】vtkPolyData生成网格与平面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/theArcticOcean/article/details/83038644

生成网格

通过设置vtkPolyData的Points和lines来生成我们想要的网格。
为了不影响观看效果,例子中将交互性关闭了。
CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(twoDimensionGird)

find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )

add_executable(${PROJECT_NAME} "main.cpp")

target_link_libraries( ${PROJECT_NAME} ${VTK_LIBRARIES} )

main.cpp

#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 <vtkActor2D.h>
#include <vtkRendererCollection.h>

using namespace std;

int main()
{
    int mmSize = 1;
    vtkSmartPointer<vtkPolyDataMapper> v1mmMapper =
            vtkSmartPointer<vtkPolyDataMapper>::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<vtkActor> v1mmLinesActor =
            vtkSmartPointer<vtkActor>::New();
    v1mmLinesActor->SetMapper( v1mmMapper );
    v1mmLinesActor->GetProperty()->SetColor( 1, 0, 0 );
    v1mmLinesActor->GetProperty()->SetLineWidth( 1 );
    v1mmLinesActor->DragableOff();
    v1mmLinesActor->PickableOff();

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor( v1mmLinesActor );
    renderer->SetBackground( 1, 1, 1 );

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

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

    //--------------------start generate------------------
    v1mmPoints->Initialize();
    v1mmLines->Initialize();
    double width = renderWindow->GetScreenSize()[0]; //GetActualSize()[0];
    double height = renderWindow->GetScreenSize()[1];
    printf("w: %lf, h: %lf\n", width, height);

    double x, y;
    //--------------------vertical lines------------------
    for ( x = -width / 2; x <= width / 2; x += mmSize )
    {
        double linePoint1[3] = { x, -height/2, 0.0 };
        double linePoint2[3] = { x, height/2, 0.0 };
        vtkIdType pointId1 = v1mmPoints->InsertNextPoint(linePoint1);
        vtkIdType pointId2 = v1mmPoints->InsertNextPoint(linePoint2);
        vtkIdType lineIds[2] = { pointId1, pointId2 };
        v1mmLines->InsertNextCell(2, lineIds);
    }
    //--------------------horizontal lines----------------
    for ( y = -height / 2; y <= height / 2; y += mmSize )
    {
        double linePoint1[3] = { -width/2, y, 0.0 };
        double linePoint2[3] = { width/2, y, 0.0 };
        vtkIdType pointId1 = v1mmPoints->InsertNextPoint(linePoint1);
        vtkIdType pointId2 = v1mmPoints->InsertNextPoint(linePoint2);
        vtkIdType lineIds[2] = { pointId1, pointId2 };
        v1mmLines->InsertNextCell(2, lineIds);
    }

    v1mmPolyData->Modified();
    //-------------------- end ------------------

    fflush( stdout );
    renderer->GetActiveCamera()->ParallelProjectionOn();
    renderer->GetActiveCamera()->Zoom( 0.05 ); //zoom out
    renderWindow->Render();
    renderWindowInteractor->SetInteractorStyle( nullptr ); //make Interactivity off
    renderWindowInteractor->Start();
    return 0;
}

grid.png

生成平面

通过设置vtkPolyData的Points和Polygons来生成我们想要的平面。

    planePoints->InsertPoint(0, width, height, 0);
    planePoints->InsertPoint(1, -width, height, 0);
    planePoints->InsertPoint(2, width, -height, 0);
    planePoints->InsertPoint(3, -width, -height, 0);

对应着:

在这里插入图片描述

如下cellId排列会生成下图中的图案:

    vtkIdType cellId1[3] = { 0, 3, 2 };
    vtkIdType cellId2[3] = { 0, 1, 2 };

在这里插入图片描述

想要生成一个矩形图案的话就不能随意地构造cellID数组。
    vtkIdType cellId1[3] = { 1, 2, 3 };
    vtkIdType cellId2[3] = { 1, 2, 0 };

得到平面:

在这里插入图片描述

CMakeList.txt:

project(createPlane)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries( ${PROJECT_NAME} ${VTK_LIBRARIES} )

main.cpp:

#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 <vtkActor2D.h>
#include <vtkRendererCollection.h>

using namespace std;

int main(int argc, char *argv[])
{   
    vtkSmartPointer<vtkPolyData> planePld =
            vtkSmartPointer<vtkPolyData>::New();

    vtkSmartPointer<vtkPoints> planePoints =
            vtkSmartPointer<vtkPoints>::New();

    vtkSmartPointer<vtkCellArray> planeCells =
            vtkSmartPointer<vtkCellArray>::New();

    vtkSmartPointer<vtkPolyDataMapper> planeMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();

    vtkSmartPointer<vtkActor> planeActor =
            vtkSmartPointer<vtkActor>::New();

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->SetBackground( 1, 1, 1 );

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

    //-----------start create plane------------------
    planePoints->SetNumberOfPoints( 4 );
    double width = renderWindow->GetScreenSize()[0] / 2.0;
    double height = renderWindow->GetScreenSize()[1] / 2.0;
    planePoints->InsertPoint(0, width, height, 0);
    planePoints->InsertPoint(1, -width, height, 0);
    planePoints->InsertPoint(2, width, -height, 0);
    planePoints->InsertPoint(3, -width, -height, 0);
    planePld->SetPoints( planePoints );
    vtkIdType cellId1[3] = { 1, 2, 3 };
    vtkIdType cellId2[3] = { 1, 2, 0 };
    //Create a cell by specifying the number of points and an array of pointid's.  Return the cell id of the cell.
    planeCells->InsertNextCell( 3, cellId1 );
    planeCells->InsertNextCell( 3, cellId2 );
    planePld->SetPolys( planeCells );
    planeMapper->SetInputData( planePld );
    planeActor->SetMapper( planeMapper );
    planeActor->GetProperty()->SetOpacity( 0.5 );
    planeActor->GetProperty()->SetColor( 0.4, 0.2, 0.7 );

    renderer->AddActor( planeActor );
    //-----------create plane finished------------------

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

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

    return 0;
}

猜你喜欢

转载自blog.csdn.net/theArcticOcean/article/details/83038644