官网有一个例子,看完之后仍然不太明白。经过一点点的探索,记录一下收获。
1.建立一个六边形的点集
Create(vtkPoints, points);
points->InsertNextPoint(-5, 0, 0);
points->InsertNextPoint(-2.5, -4.33, 0);
points->InsertNextPoint(2.5, -4.33, 0);
points->InsertNextPoint(5, 0, 0);
points->InsertNextPoint(2.5, 4.33, 0);
points->InsertNextPoint(-2.5, 4.33, 0);
2.创建单元结构
Create(vtkCellArray, cells);
cells->InsertNextCell(6);
cells->InsertCellPoint(0);
cells->InsertCellPoint(1);
cells->InsertCellPoint(2);
cells->InsertCellPoint(3);
cells->InsertCellPoint(4);
cells->InsertCellPoint(5);
3.分配一个标量数据
Create(vtkFloatArray, scalars);
scalars->InsertNextValue(50);
scalars->InsertNextValue(50);
scalars->InsertNextValue(150);
scalars->InsertNextValue(200);
scalars->InsertNextValue(200);
scalars->InsertNextValue(150);
顶点上的标量数据如图所示。为了帮助理解,所以数据分配的比较整齐。
4.用上面的数据创建Polydata
Create(vtkPolyData, polydata);
polydata->SetPoints(points);
polydata->SetPolys(cells);
polydata->GetPointData()->SetScalars(scalars);
5.vtkBandedPolyDataContourFilter等值线
Create(vtkBandedPolyDataContourFilter, bandeds);
bandeds->SetInputData(polydata);
bandeds->GenerateValues(5, 75, 175);
bandeds->Update();
这里的作用是将点标量数据重新划分,生成等值线。
GenerateValues(5, 75, 175)这句话将75-175这个范围划分成4段(5点),会生成下面的等值:
75,100,125,150,175
这样一看非常清晰,这个过滤器从点标量数据生成单元标量数据。
6.显示条带
Create(vtkPolyDataMapper, mapper);
mapper->SetInputConnection(bandeds->GetOutputPort());
mapper->SetScalarModeToUseCellData();
mapper->SetScalarRange(bandeds->GetOutput()->GetCellData()->GetScalars()->GetRange());
标量模式一定要是cellData,而且标量范围是上面的过滤生成的单元标量范围,也可以手动输入范围,范围外的标量数据就自动取极值了。
7.完整代码
#include<vtkBandedPolyDataContourFilter.h>
#include"vtkFloatArray.h"
#include"vtkModelInit.h"
#include"vtkGloble.h"
int main()
{
Create(vtkPoints, points);
points->InsertNextPoint(-5, 0, 0);
points->InsertNextPoint(-2.5, -4.33, 0);
points->InsertNextPoint(2.5, -4.33, 0);
points->InsertNextPoint(5, 0, 0);
points->InsertNextPoint(2.5, 4.33, 0);
points->InsertNextPoint(-2.5, 4.33, 0);
Create(vtkCellArray, cells);
cells->InsertNextCell(6);
cells->InsertCellPoint(0);
cells->InsertCellPoint(1);
cells->InsertCellPoint(2);
cells->InsertCellPoint(3);
cells->InsertCellPoint(4);
cells->InsertCellPoint(5);
Create(vtkFloatArray, scalars);
scalars->InsertNextValue(50);
scalars->InsertNextValue(50);
scalars->InsertNextValue(150);
scalars->InsertNextValue(200);
scalars->InsertNextValue(200);
scalars->InsertNextValue(150);
Create(vtkPolyData, polydata);
polydata->SetPoints(points);
polydata->SetPolys(cells);
polydata->GetPointData()->SetScalars(scalars);
Create(vtkBandedPolyDataContourFilter, bandeds);
bandeds->SetInputData(polydata);
bandeds->GenerateValues(5, 75, 175);
bandeds->Update();
Create(vtkPolyDataMapper, mapper);
mapper->SetInputConnection(bandeds->GetOutputPort());
mapper->SetScalarModeToUseCellData();
mapper->SetScalarRange(bandeds->GetOutput()->GetCellData()->GetScalars()->GetRange());
Create(vtkActor, actor);
actor->SetMapper(mapper);
Create(vtkRenderer, render);
render->AddActor(actor);
Create(vtkRenderWindow, window);
window->AddRenderer(render);
Create(vtkRenderWindowInteractor, inter);
inter->SetRenderWindow(window);
inter->Start();
return 0;
}