vtkContourFilter
是 VTK(Visualization Toolkit)中的一个关键类,用于从输入数据生成等值线或等值面。它是基于阈值的过滤器,可以从标量字段中提取等值线或等值面。vtkContourFilter
的核心功能是根据用户指定的值生成等值线或等值面,并将其表示为多边形网格。
SetValue(int index, double value)
:设置第index
个等值线的值为value
。GenerateValues(int numContours, double range[2])
:自动生成numContours
个等值线,范围在range
之间。SetInputData(vtkDataObject input)
:设置输入数据。Update()
:更新过滤器并生成输出数据。vtkContourFilter
是 VTK 中用于生成等值线/等值面的基础类。根据不同的应用场景和需求,VTK 提供了一些专门化的衍生类,这些类继承自 vtkContourFilter
,并在其基础上进行了功能扩展或优化。以下是一些常见的 vtkContourFilter
衍生类:
vtkMarchingCubes
vtkDiscreteMarchingCubes
vtkFlyingEdges3D
vtkBandedPolyDataContourFilter
vtkSynchronizedTemplates3D
vtkSynchronizedTemplatesCutter3D
vtkSynchronizedTemplates3D
,但支持通过其他几何体进行切割。vtkContourFilter
及其衍生类是 VTK 中用于生成等值线/等值面的核心工具。根据不同的应用场景和需求,用户可以选择合适的类来生成高质量的等值面。例如,vtkMarchingCubes
适用于医学图像中的三维重建,而 vtkFlyingEdges3D
则更适合实时渲染和体绘制。
vtkContourFilter
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphere = vtkSmartPointer::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(1.0);
vtkSmartPointer sample = vtkSmartPointer::New();
sample->SetImplicitFunction(sphere);
sample->SetModelBounds(-2.5, 2.5, -2.5, 2.5, -2.5, 2.5);
sample->SetSampleDimensions(50, 50, 50);
sample->SetCapping(true);
sample->SetCapValue(100);
sample->Update();
vtkSmartPointer contourFilter = vtkSmartPointer::New();
contourFilter->SetInputConnection(sample->GetOutputPort());
contourFilter->GenerateValues(5, 0.0, 1.0); // 生成5个等值面
contourFilter->Update();
vtkSmartPointer colors = vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(contourFilter->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
vtkSphere
生成一个球体。vtkSampleFunction
对球体进行采样,生成一个标量场。vtkContourFilter
从采样结果中提取多个等值面。vtkMarchingCubes
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphere = vtkSmartPointer::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(1.0);
vtkSmartPointer sample = vtkSmartPointer::New();
sample->SetImplicitFunction(sphere);
sample->SetModelBounds(-2.5, 2.5, -2.5, 2.5, -2.5, 2.5);
sample->SetSampleDimensions(50, 50, 50);
sample->SetCapping(true);
sample->SetCapValue(100);
sample->Update();
vtkSmartPointer marchingCubes = vtkSmartPointer::New();
marchingCubes->SetInputConnection(sample->GetOutputPort());
marchingCubes->SetValue(0, 0.0); // 生成单个等值面
marchingCubes->Update();
vtkSmartPointer colors = vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(marchingCubes->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
vtkSphere
生成一个球体。vtkSampleFunction
对球体进行采样,生成一个标量场。vtkMarchingCubes
从采样结果中提取单个等值面。vtkDiscreteMarchingCubes
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphere = vtkSmartPointer::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(1.0);
vtkSmartPointer sample = vtkSmartPointer::New();
sample->SetImplicitFunction(sphere);
sample->SetModelBounds(-2.5, 2.5, -2.5, 2.5, -2.5, 2.5);
sample->SetSampleDimensions(50, 50, 50);
sample->SetCapping(true);
sample->SetCapValue(100);
sample->Update();
vtkSmartPointer discreteMarchingCubes = vtkSmartPointer::New();
discreteMarchingCubes->SetInputConnection(sample->GetOutputPort());
discreteMarchingCubes->SetValue(0, 100); // 生成单个等值面
discreteMarchingCubes->Update();
vtkSmartPointer colors = vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(discreteMarchingCubes->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
vtkSphere
生成一个球体。vtkSampleFunction
对球体进行采样,生成一个标量场。vtkDiscreteMarchingCubes
从采样结果中提取单个离散等值面。vtkFlyingEdges3D
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphere = vtkSmartPointer::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(1.0);
vtkSmartPointer sample = vtkSmartPointer::New();
sample->SetImplicitFunction(sphere);
sample->SetModelBounds(-2.5, 2.5, -2.5, 2.5, -2.5, 2.5);
sample->SetSampleDimensions(50, 50, 50);
sample->SetCapping(true);
sample->SetCapValue(100);
sample->Update();
vtkSmartPointer flyingEdges = vtkSmartPointer::New();
flyingEdges->SetInputConnection(sample->GetOutputPort());
flyingEdges->GenerateValues(5, 0.0, 1.0); // 生成5个等值面
flyingEdges->Update();
vtkSmartPointer colors = vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(flyingEdges->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
vtkSphere
生成一个球体。vtkSampleFunction
对球体进行采样,生成一个标量场。vtkFlyingEdges3D
从采样结果中提取多个等值面。vtkBandedPolyDataContourFilter
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphere = vtkSmartPointer::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(1.0);
vtkSmartPointer sample = vtkSmartPointer::New();
sample->SetImplicitFunction(sphere);
sample->SetModelBounds(-2.5, 2.5, -2.5, 2.5, -2.5, 2.5);
sample->SetSampleDimensions(50, 50, 50);
sample->SetCapping(true);
sample->SetCapValue(100);
sample->Update();
vtkSmartPointer bandedContourFilter = vtkSmartPointer::New();
bandedContourFilter->SetInputConnection(sample->GetOutputPort());
bandedContourFilter->GenerateValues(5, 0.0, 1.0); // 生成5个带状等值线
bandedContourFilter->Update();
vtkSmartPointer colors = vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(bandedContourFilter->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
vtkSphere
生成一个球体。vtkSampleFunction
对球体进行采样,生成一个标量场。vtkBandedPolyDataContourFilter
从采样结果中提取多个带状等值线。vtkSynchronizedTemplates3D
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphere = vtkSmartPointer::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(1.0);
vtkSmartPointer sample = vtkSmartPointer::New();
sample->SetImplicitFunction(sphere);
sample->SetModelBounds(-2.5, 2.5, -2.5, 2.5, -2.5, 2.5);
sample->SetSampleDimensions(50, 50, 50);
sample->SetCapping(true);
sample->SetCapValue(100);
sample->Update();
vtkSmartPointer syncTemplates = vtkSmartPointer::New();
syncTemplates->SetInputConnection(sample->GetOutputPort());
syncTemplates->SetValue(0, 0.0); // 生成单个等值面
syncTemplates->Update();
vtkSmartPointer colors = vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(syncTemplates->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkSmartPointer renderer = vtkSmartPointer::New();
vtkSmartPointer renderWindow = vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
vtkSphere
生成一个球体。vtkSampleFunction
对球体进行采样,生成一个标量场。vtkSynchronizedTemplates3D
从采样结果中提取单个等值面。这些示例代码展示了如何使用 vtkContourFilter
及其衍生类来生成等值线或等值面,并将其渲染出来。每个类都有其特定的应用场景和优势,可以根据具体需求选择合适的类。