VTK实现Delaunay三角化

#include <vtkVersion.h>
#include <vtkCellArray.h>
#include <vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkFeatureEdges.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolygon.h>
#include <vtkSmartPointer.h>
#include <vtkDelaunay2D.h>
#include <vtkMath.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#define PI 3.1415926


int main(int, char *[])
{
        // Generate two circular polygons
        vtkSmartPointer<vtkPoints> points =
                vtkSmartPointer<vtkPoints>::New();
        vtkSmartPointer<vtkCellArray> aCellArray =
                vtkSmartPointer<vtkCellArray>::New();


        aCellArray->InsertNextCell(36);
        for (int theta = 0;theta<360;theta+=10)
        {
                double x = 2*cos(theta*PI/180);
                double y = 2*sin(theta*PI/180);
                vtkIdType pointId = points->InsertNextPoint(x,y,0);
                aCellArray->InsertCellPoint(pointId);
        }


        aCellArray->InsertNextCell(36);
        for (int theta = 350;theta>=0;theta-=10)
        {
                double x = cos(theta*PI/180);
                double y = sin(theta*PI/180);
                vtkIdType pointId = points->InsertNextPoint(x,y,0);
                aCellArray->InsertCellPoint(pointId);
        }


        // Create a polydata to store the polys.
        vtkSmartPointer<vtkPolyData> polys =
                vtkSmartPointer<vtkPolyData>::New();
        polys->SetPoints(points);
        polys->SetPolys(aCellArray);


        // Triangulate the grid points
        vtkSmartPointer<vtkDelaunay2D> triangulator =
                vtkSmartPointer<vtkDelaunay2D>::New();
#if VTK_MAJOR_VERSION <= 5
        triangulator->SetInput(polys);
        triangulator->SetSource(polys);
#else
        triangulator->SetInputData(polys);
        triangulator->SetSourceData(polys);
#endif
        triangulator->Update();


        // Visualize
        vtkSmartPointer<vtkPolyDataMapper> meshMapper =
                vtkSmartPointer<vtkPolyDataMapper>::New();
        meshMapper->SetInputConnection(triangulator->GetOutputPort());


        vtkSmartPointer<vtkActor> meshActor =
                vtkSmartPointer<vtkActor>::New();
        meshActor->SetMapper(meshMapper);
        meshActor->GetProperty()->SetRepresentationToWireframe();


        vtkSmartPointer<vtkFeatureEdges> edgeFilter =
                vtkSmartPointer<vtkFeatureEdges>::New();
#if VTK_MAJOR_VERSION <= 5
        edgeFilter->SetInputConnection(polys->GetProducerPort());
#else
        edgeFilter->SetInputData(polys);
#endif
        edgeFilter->Update();


        vtkSmartPointer<vtkPolyDataMapper> contourMapper =
                vtkSmartPointer<vtkPolyDataMapper>::New();
        contourMapper->SetInputConnection(edgeFilter->GetOutputPort());


        vtkSmartPointer<vtkActor> contourActor =
                vtkSmartPointer<vtkActor>::New();
        contourActor->SetMapper(contourMapper);
        contourActor->GetProperty()->SetColor(1,0,0);


        // Create a renderer, render window, and interactor
        vtkSmartPointer<vtkRenderer> renderer =
                vtkSmartPointer<vtkRenderer>::New();
        vtkSmartPointer<vtkRenderWindow> renderWindow =
                vtkSmartPointer<vtkRenderWindow>::New();
        renderWindow->AddRenderer(renderer);
        vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
                vtkSmartPointer<vtkRenderWindowInteractor>::New();
        renderWindowInteractor->SetRenderWindow(renderWindow);


        // Add the actor to the scene
        renderer->AddActor(meshActor);
        renderer->AddActor(contourActor);
        renderer->SetBackground(.3, .6, .3); // Background color green


        // Render and interact
        renderWindow->Render();
        renderWindowInteractor->Start();


        return EXIT_SUCCESS;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值