• VTK中二维Delaunay三角剖分


    使用VTK过程中需要用到Delaunay三角剖分,搜了一下搜出了下面的代码。转自:http://gardenjoy.spaces.live.com/blog/cns!DDE596D749321E57!262.entry

    我运行并添加了注释,现发于博客,以便日后温故总结学习。

    代码
    1 #include "vtkActor.h"
    2 #include "vtkCellArray.h"
    3 #include "vtkPoints.h"
    4 #include "vtkPolyData.h"
    5 #include "vtkPolyDataMapper.h"
    6 #include "vtkRenderWindow.h"
    7 #include "vtkRenderWindowInteractor.h"
    8 #include "vtkRenderer.h"
    9 #include "vtkTubeFilter.h"
    10 #include "vtkDelaunay2D.h"
    11 #include "vtkExtractEdges.h"
    12 #include "vtkInteractorStyleTrackballCamera.h"
    13 #include "vtkProperty.h"
    14 #include "vtkCamera.h"
    15
    16  #pragma comment( lib, "vtkGraphics" )
    17  #pragma comment( lib, "vtkRendering" )
    18  #pragma comment( lib, "vtkFiltering" )
    19 #pragma comment( lib, "vtkCommon" )
    20 #pragma comment( lib, "vtkWidgets")
    21 #pragma comment( lib, "vtksys")
    22 #pragma comment( lib, "vtkIO")
    23 #pragma comment( lib, "vtkexpat")
    24 #pragma comment( lib, "vtkjpeg")
    25 #pragma comment( lib, "vtkpng")
    26 #pragma comment( lib, "vtkzlib")
    27 #pragma comment( lib, "vtktiff")
    28 #pragma comment( lib, "OpenGL32")
    29 #pragma comment( lib, "vtkImaging")
    30 #pragma comment( lib, "vtkHybrid")
    31
    32 #define NUM_INPUT 14
    33 #define NUM_CONSTRAIN1 16
    34 #define NUM_CONSTRAIN2 3
    35 #define NUM_TOTAL (NUM_INPUT + NUM_CONSTRAIN1 + NUM_CONSTRAIN2)
    36 int main( int argc, char *argv[] )
    37 {
    38 // Generate the input points and constrained edges/polygons.
    39 float input[NUM_TOTAL][3] = {{0, 100, 0}, {0, 50, 0}, {0, 0, 0}, {50, 0, 0}, {100, 0, 0}, {150, 0, 0},
    40 {200, 0, 0}, {200, 50, 0}, {200, 100, 0}, {200, 150, 0}, {150, 150, 0},
    41 {100, 150, 0}, {50, 150, 0}, {0, 150, 0}, {154, 29, 0}, {167, 29, 0},
    42 {167, 20, 0}, {120, 20, 0}, {120, 29, 0}, {138, 29, 0}, {121, 63, 0},
    43 {74, 63, 0}, {58, 29, 0}, {78, 29, 0}, {78, 20, 0}, {38, 20, 0},
    44 {38, 29, 0}, {50, 29, 0}, {95, 130, 0}, {110, 135, 0}, {76,70, 0},
    45 {120, 70, 0}, {99, 121, 0}};
    46 vtkPoints *points = vtkPoints::New();
    47
    48 //添加所有点
    49 for (int i = 0; i < NUM_TOTAL; ++i)
    50 {
    51 points->InsertPoint(i, input[i]);
    52 }
    53 //添加CELL
    54 vtkCellArray *polys = vtkCellArray::New();
    55 polys->InsertNextCell(NUM_INPUT);
    56 for (int i = 0; i < NUM_INPUT; ++i)
    57 {
    58 polys->InsertCellPoint(i ); //所有输入的点 组成一个CELL
    59 }
    60 polys->InsertNextCell(NUM_CONSTRAIN1);
    61 for (int i = 0; i < NUM_CONSTRAIN1; ++i) //约束1
    62 {
    63 polys->InsertCellPoint(i + NUM_INPUT);
    64 }
    65 polys->InsertNextCell(NUM_CONSTRAIN2);
    66 for (int i = 0; i < NUM_CONSTRAIN2; ++i)
    67 {
    68 polys->InsertCellPoint(i + NUM_INPUT + NUM_CONSTRAIN1); //约束2
    69 }
    70
    71 vtkPolyData *polyData = vtkPolyData::New();
    72 polyData->SetPoints(points); //添加点集
    73 polyData->SetPolys(polys); // 添加面集
    74
    75 vtkDelaunay2D *del = vtkDelaunay2D::New();
    76 del->SetInput(polyData); //对数据进行Delaunay剖分
    77 del->SetSource(polyData);
    78
    79 vtkPolyDataMapper *mapMesh = vtkPolyDataMapper::New();
    80 mapMesh->SetInput(del->GetOutput()); //映射到Mapper
    81
    82 vtkActor *meshActor = vtkActor::New();
    83 meshActor->SetMapper(mapMesh);
    84 meshActor->GetProperty()->SetColor(0.4, 1.0, 0.4);
    85 meshActor->GetProperty()->SetOpacity(0.4);
    86
    87 vtkExtractEdges *extract = vtkExtractEdges::New(); //提取Delaunay三角网边界 可以从任何类型的数据中提取边界
    88 extract->SetInput(del->GetOutput());
    89 vtkTubeFilter *tubes = vtkTubeFilter::New();
    90 tubes->SetInput(extract->GetOutput());
    91 tubes->SetRadius(1.4);
    92 tubes->SetNumberOfSides(5);
    93
    94 vtkPolyDataMapper *mapEdges = vtkPolyDataMapper::New(); //映射
    95 mapEdges->SetInput(tubes->GetOutput());
    96
    97 vtkActor *edgeActor = vtkActor::New();
    98 edgeActor->SetMapper(mapEdges);
    99 edgeActor->GetProperty()->SetColor(1, 1, 0);
    100
    101 // Create the rendering window, renderer, and interactive renderer
    102 vtkRenderer *ren1 = vtkRenderer::New();
    103 vtkRenderWindow *renWin = vtkRenderWindow::New();
    104 renWin->AddRenderer(ren1);
    105
    106 // Add the actors to the renderer, set the background and size
    107 ren1->AddActor(meshActor);
    108 ren1->AddActor(edgeActor);
    109 ren1->SetBackground(0.0, 0.0, 0.2);
    110 ren1->GetActiveCamera()->Zoom(1.8);
    111 renWin->SetSize(500, 400);
    112
    113 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    114 iren->SetRenderWindow(renWin);
    115
    116 vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
    117 iren->SetInteractorStyle(style);
    118 iren->Initialize();
    119 iren->Start();
    120
    121 // Free up any objects we created. All instances in VTK are deleted by
    122 // using the Delete() method.
    123 points->Delete();
    124 polys->Delete();
    125 polyData->Delete();
    126 del->Delete();
    127 mapMesh->Delete();
    128 meshActor->Delete();
    129 extract->Delete();
    130 tubes->Delete();
    131 mapEdges->Delete();
    132 edgeActor->Delete();
    133 ren1->Delete();
    134 renWin->Delete();
    135 iren->Delete();
    136 style->Delete();
    137 return 0;
    138 }
    139
    140

    运行后的结果如下图所示:

     

  • 相关阅读:
    [转]读取并修改App.config文件
    [转]线程和进程的概念
    实习日志(3)
    实习日志2
    实习小感,回学校啦~~~~
    请教LUA高手一段代码,希望帮忙谢谢!
    实习的日子
    vs显示 error LNK2019: 无法解析的外部符号 _main解决办法
    创建一个新窗口进程并返回进程ID号和进程的主线程ID号
    显示基本图形界面第一天
  • 原文地址:https://www.cnblogs.com/zuoan/p/1716329.html
Copyright © 2020-2023  润新知