VTK初学一,e_Triangle三角形的绘制

时间:2024-07-26 08:05:01
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
#include "vtkPolyDataMapper.h"
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPoints.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkFloatArray.h"
#include "vtkPolyData.h"
#include "vtkDataSetMapper.h"
#include "vtkActor2D.h"
#include "vtkContourFilter.h"
#include "vtkContourValues.h"
#include "vtkUnstructuredGrid.h"
#include "vtkPointData.h"
#include "vtkTriangle.h"
#include <vtkInteractorStyleTrackballCamera.h>
void myShow(vtkUnstructuredGrid* aGrid)
{
    vtkSmartPointer<vtkDataSetMapper> aMapper=vtkSmartPointer<vtkDataSetMapper>::New();
    aMapper->SetInputData(aGrid);
    aMapper->ScalarVisibilityOn();

    vtkSmartPointer<vtkActor> anActor=vtkSmartPointer<vtkActor>::New();
    anActor->SetMapper(aMapper);
    anActor->GetProperty()->SetRepresentationToWireframe();
    anActor->GetProperty()->SetDiffuseColor(1,1,1);
    anActor->GetProperty()->SetLineWidth(10);

    vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin=vtkSmartPointer<vtkRenderWindow>::New();
    ren1->AddActor(anActor);
    ren1->ResetCamera();
    renWin->AddRenderer(ren1);
    renWin->SetSize(512,512);

    vtkSmartPointer<vtkRenderWindowInteractor> iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style=vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetRenderWindow(renWin);
    iren->SetInteractorStyle(style);
    iren->Start();
}

int main()
{
    //几何数据;
    vtkSmartPointer<vtkPoints> trianglePoints=vtkSmartPointer<vtkPoints>::New();
    trianglePoints->SetNumberOfPoints(3);
    trianglePoints->InsertPoint(0,0,0,0);
    trianglePoints->InsertPoint(1,1,0,0);
    trianglePoints->InsertPoint(2,1,1,1);
    //属性数据
    vtkSmartPointer<vtkFloatArray> triangleScalars=vtkSmartPointer<vtkFloatArray>::New();
    triangleScalars->SetNumberOfTuples(3);
    triangleScalars->InsertValue(0,0);
    triangleScalars->InsertValue(1,0);
    triangleScalars->InsertValue(2,1);
    //拓扑结构
    vtkSmartPointer<vtkTriangle> aTriangle=vtkSmartPointer<vtkTriangle>::New();
    aTriangle->GetPointIds()->SetNumberOfIds(3);
    aTriangle->GetPointIds()->SetId(0,0);
    aTriangle->GetPointIds()->SetId(1,1);
    aTriangle->GetPointIds()->SetId(2,2);
    //将以上三部分组装成一个结构
    vtkSmartPointer<vtkUnstructuredGrid> aTriangleGrid=vtkSmartPointer<vtkUnstructuredGrid>::New();
    aTriangleGrid->Allocate(1,1);
    aTriangleGrid->SetPoints(trianglePoints);
    aTriangleGrid->GetPointData()->SetScalars(triangleScalars);
    aTriangleGrid->InsertNextCell(aTriangle->GetCellType(),aTriangle->GetPointIds());
    //显示triangle
    myShow(aTriangleGrid);
    return 0;
}

VTK初学一,e_Triangle三角形的绘制