#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkIntArray.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkContourFilter.h>
#include <vtkOutputWindow.h>
#include <vtkObject.h>
int main(int, char* [])
{
vtkObject::SetGlobalWarningDisplay(1);
// 设置自定义输出窗口
vtkSmartPointer<vtkOutputWindow> outputWindow = vtkSmartPointer<vtkOutputWindow>::New();
vtkOutputWindow::SetInstance(outputWindow);
// 创建点
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0.0, 0.0, 0.0); // 点0
points->InsertNextPoint(1.0, 0.0, 0.0); // 点1
points->InsertNextPoint(1.0, 1.0, 0.0); // 点2
points->InsertNextPoint(0.0, 0.0, 0.0); // 点3
points->InsertNextPoint(1.0, 1.0, 0.0); // 点4
points->InsertNextPoint(0.0, 1.0, 0.0); // 点5
// 创建三角形
vtkSmartPointer<vtkTriangle> triangle1 = vtkSmartPointer<vtkTriangle>::New();
triangle1->GetPointIds()->SetId(0, 0);
triangle1->GetPointIds()->SetId(1, 1);
triangle1->GetPointIds()->SetId(2, 2);
vtkSmartPointer<vtkTriangle> triangle2 = vtkSmartPointer<vtkTriangle>::New();
triangle2->GetPointIds()->SetId(0, 3);
triangle2->GetPointIds()->SetId(1, 4);
triangle2->GetPointIds()->SetId(2, 5);
// 创建单元数组
vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
triangles->InsertNextCell(triangle1);
triangles->InsertNextCell(triangle2);
// 创建PolyData
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetPolys(triangles);
// 创建标量数组
vtkSmartPointer<vtkIntArray> scalars = vtkSmartPointer<vtkIntArray>::New();
scalars->SetNumberOfComponents(1);
scalars->SetName("Scalars");
// 设置每个点的标量值
scalars->InsertNextValue(0);
scalars->InsertNextValue(0);
scalars->InsertNextValue(0); // 三角形1
scalars->InsertNextValue(1);
scalars->InsertNextValue(1);
scalars->InsertNextValue(1); // 三角形2
polyData->GetPointData()->SetScalars(scalars);
// 创建mapper和actor用于原始数据
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
mapper->ScalarVisibilityOn();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// 创建renderer和renderWindow
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// 创建renderWindowInteractor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// 创建等值线滤波器
vtkSmartPointer<vtkContourFilter> contourFilter = vtkSmartPointer<vtkContourFilter>::New();
contourFilter->SetInputData(polyData);
contourFilter->GenerateValues(2, 0, 1); // 生成两个等值线值
for (int i = 0; i < contourFilter->GetNumberOfContours(); i++) {
std::cout << "Contour value " << i << ": " << contourFilter->GetValue(i) << std::endl;
}
contourFilter->Update();
// 在这里添加调试输出
std::cout << "ContourFilter Output: " << contourFilter->GetOutput() << std::endl;
// 创建mapper和actor用于等值线
vtkSmartPointer<vtkPolyDataMapper> contourMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
contourMapper->SetInputConnection(contourFilter->GetOutputPort());
contourMapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> contourActor = vtkSmartPointer<vtkActor>::New();
contourActor->SetMapper(contourMapper);
contourActor->GetProperty()->SetColor(0.0, 0.0, 0.0); // 设置等值线颜色为黑色
// 添加actor到renderer
renderer->AddActor(actor);
// 添加actor到renderer
renderer->AddActor(contourActor);
renderer->SetBackground(1, 1, 1); // 设置背景颜色为白色
// 开始渲染循环
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include <vtkPoints.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkIntArray.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkContourFilter.h>
#include <vtkOutputWindow.h>
#include <vtkObject.h>
int main(int, char* [])
{
vtkObject::SetGlobalWarningDisplay(1);
// 设置自定义输出窗口
vtkSmartPointer<vtkOutputWindow> outputWindow = vtkSmartPointer<vtkOutputWindow>::New();
vtkOutputWindow::SetInstance(outputWindow);
// 创建点
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0.0, 0.0, 0.0); // 点0
points->InsertNextPoint(1.0, 0.0, 0.0); // 点1
points->InsertNextPoint(1.0, 1.0, 0.0); // 点2
points->InsertNextPoint(0.0, 0.0, 0.0); // 点3
points->InsertNextPoint(1.0, 1.0, 0.0); // 点4
points->InsertNextPoint(0.0, 1.0, 0.0); // 点5
// 创建三角形
vtkSmartPointer<vtkTriangle> triangle1 = vtkSmartPointer<vtkTriangle>::New();
triangle1->GetPointIds()->SetId(0, 0);
triangle1->GetPointIds()->SetId(1, 1);
triangle1->GetPointIds()->SetId(2, 2);
vtkSmartPointer<vtkTriangle> triangle2 = vtkSmartPointer<vtkTriangle>::New();
triangle2->GetPointIds()->SetId(0, 3);
triangle2->GetPointIds()->SetId(1, 4);
triangle2->GetPointIds()->SetId(2, 5);
// 创建单元数组
vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
triangles->InsertNextCell(triangle1);
triangles->InsertNextCell(triangle2);
// 创建PolyData
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetPolys(triangles);
// 创建标量数组
vtkSmartPointer<vtkIntArray> scalars = vtkSmartPointer<vtkIntArray>::New();
scalars->SetNumberOfComponents(1);
scalars->SetName("Scalars");
// 设置每个点的标量值
scalars->InsertNextValue(0);
scalars->InsertNextValue(0);
scalars->InsertNextValue(0); // 三角形1
scalars->InsertNextValue(1);
scalars->InsertNextValue(1);
scalars->InsertNextValue(1); // 三角形2
polyData->GetPointData()->SetScalars(scalars);
// 创建mapper和actor用于原始数据
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
mapper->ScalarVisibilityOn();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// 创建renderer和renderWindow
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// 创建renderWindowInteractor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// 创建等值线滤波器
vtkSmartPointer<vtkContourFilter> contourFilter = vtkSmartPointer<vtkContourFilter>::New();
contourFilter->SetInputData(polyData);
contourFilter->GenerateValues(2, 0, 1); // 生成两个等值线值
for (int i = 0; i < contourFilter->GetNumberOfContours(); i++) {
std::cout << "Contour value " << i << ": " << contourFilter->GetValue(i) << std::endl;
}
contourFilter->Update();
// 在这里添加调试输出
std::cout << "ContourFilter Output: " << contourFilter->GetOutput() << std::endl;
// 创建mapper和actor用于等值线
vtkSmartPointer<vtkPolyDataMapper> contourMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
contourMapper->SetInputConnection(contourFilter->GetOutputPort());
contourMapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> contourActor = vtkSmartPointer<vtkActor>::New();
contourActor->SetMapper(contourMapper);
contourActor->GetProperty()->SetColor(0.0, 0.0, 0.0); // 设置等值线颜色为黑色
// 添加actor到renderer
renderer->AddActor(actor);
// 添加actor到renderer
renderer->AddActor(contourActor);
renderer->SetBackground(1, 1, 1); // 设置背景颜色为白色
// 开始渲染循环
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}