VTK Learning Three
引言
生成规则格网vtkStructuredGrid
,并用平面vtkPlane
或者多段面vtkPolyPlane
进行切割vtkCutter
。
切割获得的平面或多段面黄色显示。规则格网以线框模式显示RepresentationToWireframe
。
代码
#include <vtkActor.h>
#include <vtkIntersectionPolyDataFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkPolyLine.h>
#include <vtkPolyPlane.h>
#include <vtkPlaneSource.h>
#include <vtkCutter.h>
#include <vtkDataSetMapper.h>
#include <vtkPlane.h>
int main(int, char *[])
{
vtkSmartPointer<vtkStructuredGrid> structuredGrid =
vtkSmartPointer<vtkStructuredGrid>::New();
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
const int Size=20;
int i, j, k;
for(k = 0; k < Size; k++)
for(j = 0; j < Size; j++)
for(i = 0; i < Size; i++)
points->InsertNextPoint(i, j, k);
// Specify the dimensions of the grid
structuredGrid->SetDimensions(Size,Size,Size);
structuredGrid->SetPoints(points);
// Cutter
#if 0
vtkSmartPointer<vtkPlane> plane =
vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(Size/2,0,0);
plane->SetNormal(1,0,0);
#else
const int N=3;
const double Step10=(Size-1)/10.0;
vtkPoints *cutPts = vtkPoints::New();
cutPts->SetNumberOfPoints(N);
{
i=0;
cutPts->SetPoint(i++, 1*Step10, 1*Step10, 100);
cutPts->SetPoint(i++, 5*Step10, 5*Step10, 100);
cutPts->SetPoint(i++, 5*Step10, 8*Step10, 100);
}
int num = cutPts->GetNumberOfPoints();
vtkIdType* idCutPts = new vtkIdType[N];
for (int i = 0; i < N; ++i)
idCutPts[i] = i;
vtkSmartPointer<vtkPolyLine> PolyLine =
vtkSmartPointer<vtkPolyLine>::New();
//调用父类vtkCell的Initialize方法直接生成多段线
PolyLine->Initialize(N, idCutPts, cutPts);
vtkSmartPointer<vtkPolyPlane> plane =
vtkSmartPointer<vtkPolyPlane>::New();
plane->SetPolyLine(PolyLine);
#endif
vtkSmartPointer<vtkCutter> cutter =
vtkSmartPointer<vtkCutter>::New();
cutter->SetCutFunction(plane);
cutter->SetInputData(structuredGrid);
cutter->Update();
vtkSmartPointer<vtkPolyDataMapper> cutterMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cutterMapper->SetInputConnection( cutter->GetOutputPort());
// Create plane actor
vtkSmartPointer<vtkActor> planeActor =
vtkSmartPointer<vtkActor>::New();
planeActor->GetProperty()->SetColor(1.0,1.0,0);
planeActor->GetProperty()->SetLineWidth(2);
planeActor->SetMapper(cutterMapper);
// Create a mapper and actor
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper->SetInputConnection(structuredGrid->GetProducerPort());
#else
mapper->SetInputData(structuredGrid);
#endif
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetRepresentationToWireframe();
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderer->AddActor(planeActor);
renderer->SetBackground(.3, .6, .3); // Background color
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
构建CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(PolyPlane)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(PolyPlane PolyPlane.cxx )
target_link_libraries(PolyPlane ${VTK_LIBRARIES})