在本实例中,我们将用到vtkQuadric、vtkSampleFunction、vtkContourFilter三个类,分别是二次曲面函数、函数曲面抽样和等高滤波。
vtkQuadric负责二次曲面基本参数的设置,vtkSampleFunction则是对二次曲面进行等间隔逐点采样,在本例中采样点数为50*50*50。vtkContourFilter负责将采集到空间点,转成vtkPolyData型对象。
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRenderingOpenGL) VTK_MODULE_INIT(vtkInteractionStyle) VTK_MODULE_INIT(vtkRenderingFreeType) #endif #include <iostream> using namespace std; #include <vtkSmartPointer.h> #include <vtkQuadric.h> #include <vtkSampleFunction.h> #include <vtkContourFilter.h> #include <vtkActor.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtkPolyDataMapper.h> #include <vtkCellArray.h> #include <vtkImageDataGeometryFilter.h> #include <vtkImageData.h> #include <vtkImageActor.h> int main() { //创建二次函数 vtkSmartPointer<vtkQuadric>quadric=vtkSmartPointer<vtkQuadric>::New(); quadric->SetCoefficients(0.5,1,0.2,0,0.1,0,0,0.2,0,0); //二次函数采样分辨率 vtkSmartPointer<vtkSampleFunction>sample=vtkSmartPointer<vtkSampleFunction>::New(); sample->SetSampleDimensions(50,50,50); sample->SetImplicitFunction(quadric); double xmin=0,xmax=1,ymin=0,ymax=1,zmin=0,zmax=1; sample->SetModelBounds(xmin,xmax,ymin,ymax,zmin,zmax); vtkSmartPointer<vtkContourFilter> contourFilter=vtkSmartPointer<vtkContourFilter>::New(); contourFilter->SetInputConnection(sample->GetOutputPort()); contourFilter->GenerateValues(1,1,1); contourFilter->Update();/**在此处可以有两种方法,使得contourFilter得到的vtkPolyData型对象与mapper对接1.用contourFilter->GetOutput();得到PolyData,然后输入mapper;2.再有就是,直接调用contourFilter->GetOutputPort()输入到 mapper->SetInputConnection()******************************************************************************************/
vtkSmartPointer<vtkPolyData> polyData= contourFilter->GetOutput();
//可视化,设置mapper、actor vtkSmartPointer<vtkPolyDataMapper>mapper=vtkSmartPointer<vtkPolyDataMapper>::New(); // mapper->SetInputConnection(contourFilter->GetOutputPort()); mapper->SetInputData(polyData); vtkSmartPointer<vtkActor>actor=vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); actor->SetOrigin(0,0,0); //设置renderer、window和interactor vtkSmartPointer<vtkRenderer> renderer=vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renWin=vtkSmartPointer<vtkRenderWindow>::New(); vtkSmartPointer<vtkRenderWindowInteractor> iren=vtkSmartPointer<vtkRenderWindowInteractor>::New(); renWin->AddRenderer(renderer); iren->SetRenderWindow(renWin); renderer->AddActor(actor); // renderer->AddActor(imageActor); renderer->SetBackground(0.3,0.6,0.3); renWin->Render(); iren->Start(); return 0; }
#ifndefINITIAL_OPENGL
#defineINITIAL_OPENGL
#include<vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
#endif
#include<iostream>
usingnamespacestd;
#include<vtkSmartPointer.h>
#include<vtkQuadric.h>
#include<vtkSampleFunction.h>
#include<vtkContourFilter.h>
#include<vtkActor.h>
#include<vtkProperty.h>
#include<vtkRenderWindow.h>
#include<vtkRenderer.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkPolyDataMapper.h>
#include<vtkCellArray.h>
#include<vtkImageDataGeometryFilter.h>
#include<vtkImageData.h>
#include<vtkImageActor.h>
intmain()
{
//创建二次函数
vtkSmartPointer<vtkQuadric>quadric=vtkSmartPointer<vtkQuadric>::New();
quadric->SetCoefficients(0.5,1,0.2,0,0.1,0,0,0.2,0,0);
//二次函数采样分辨率
vtkSmartPointer<vtkSampleFunction>sample=vtkSmartPointer<vtkSampleFunction>::New();
sample->SetSampleDimensions(50,50,50);
sample->SetImplicitFunction(quadric);
doublexmin=0,xmax=1,ymin=0,ymax=1,zmin=0,zmax=1;
sample->SetModelBounds(xmin,xmax,ymin,ymax,zmin,zmax);
vtkSmartPointer<vtkContourFilter>contourFilter=vtkSmartPointer<vtkContourFilter>::New();
contourFilter->SetInputConnection(sample->GetOutputPort());
contourFilter->GenerateValues(1,1,1);
contourFilter->Update();
vtkSmartPointer<vtkPolyData>polyData=contourFilter->GetOutput();
//可视化,设置mapper、actor
vtkSmartPointer<vtkPolyDataMapper>mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
//mapper->SetInputConnection(contourFilter->GetOutputPort());
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor>actor=vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->SetOrigin(0,0,0);
//设置renderer、window和interactor
vtkSmartPointer<vtkRenderer>renderer=vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
renWin->AddRenderer(renderer);
iren->SetRenderWindow(renWin);
renderer->AddActor(actor);
//renderer->AddActor(imageActor);
renderer->SetBackground(0.3,0.6,0.3);
renWin->Render();
iren->Start();
return0;
}
时间: 2024-11-09 05:32:09