c++ vtk创建100*100网格
时间: 2023-11-26 18:03:13 浏览: 123
使用VTK库创建一个100*100网格的步骤如下:
1. 包含VTK库的头文件:
```c++
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
```
2. 创建点集:
```c++
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
for(int i=0; i<100; i++){
for(int j=0; j<100; j++){
points->InsertNextPoint(i, j, 0);
}
}
```
3. 创建网格单元:
```c++
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
for(int i=0; i<99; i++){
for(int j=0; j<99; j++){
vtkSmartPointer<vtkQuad> quad = vtkSmartPointer<vtkQuad>::New();
quad->GetPointIds()->SetId(0, i*100+j);
quad->GetPointIds()->SetId(1, i*100+j+1);
quad->GetPointIds()->SetId(2, (i+1)*100+j+1);
quad->GetPointIds()->SetId(3, (i+1)*100+j);
cells->InsertNextCell(quad);
}
}
```
4. 使用点集和网格单元创建多边形数据:
```c++
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->SetPolys(cells);
```
5. 创建渲染器、映射器和演员:
```c++
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.1, 0.2, 0.4);
```
6. 创建窗口交互器并运行:
```c++
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(800, 800);
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
```
这样就创建了一个100*100的网格并显示在窗口中。
阅读全文