vtkQuadratic创建半球面

时间:2022-05-22 14:53:22

用的关键类:vtkQuadric、vtkSampleFunction、vtkContourFilter;用于创建方框的类vtkOutlineFilter

vtkQuadratic创建半球面

 #ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
#include <vtkQuadric.h>
#include <vtkSampleFunction.h>
#include <vtkContourFilter.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageData.h>
#include <vtkSmartPointer.h> void Sphere();
void PlotFunction(vtkQuadric*quadric,double value);
int main()
{
Sphere();
return ;
}
void Sphere()
{
vtkSmartPointer<vtkQuadric>quadric=vtkSmartPointer<vtkQuadric>::New();
quadric->SetCoefficients(0.5,0.5,,,,,,,,); // F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
// F(x,y,z) = 1*x^2 + 1*y^2 + 1*z^2
PlotFunction(quadric,);
} void PlotFunction(vtkQuadric *quadric, double value){
//采样二次函数
vtkSmartPointer<vtkSampleFunction>sample=vtkSmartPointer<vtkSampleFunction>::New();
sample->SetSampleDimensions(,,);
sample->SetImplicitFunction(quadric);
//x,y,z取值范围
double xmin=-,xmax=,ymin=-,ymax=,zmin=,zmax=;
double scale=0.2;
sample->SetModelBounds(xmin,xmax,ymin,ymax,zmin,zmax);
//创建5个曲面surfaces F(x,y,z) = constant /*vtkContourFilter*contours=vtkContourFilter::New();
contours->SetInputData(sample->GetOutput());
contours->GenerateValues(5,0,1.2);*/
//创建第0 isosurface
vtkSmartPointer<vtkContourFilter>contours=vtkSmartPointer<vtkContourFilter>::New();
contours->SetInputConnection(sample->GetOutputPort());
contours->GenerateValues(,value,value);
//将contours映射到图形图元
vtkSmartPointer<vtkPolyDataMapper>contourMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
contourMapper->SetInputConnection(contours->GetOutputPort());
contourMapper->SetScalarRange(,1.2);
//创建contour演员
vtkSmartPointer<vtkActor>contourActor=vtkSmartPointer<vtkActor>::New();
contourActor->SetMapper(contourMapper);
/*创建一个方盒,将函数曲面包围起来,标识抽样体*/
//创建方盒的边看线
vtkSmartPointer<vtkOutlineFilter>outline=vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputConnection(sample->GetOutputPort());
//将线框映射为图形图元
vtkSmartPointer<vtkPolyDataMapper>outlineMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInputConnection(outline->GetOutputPort());
//创建outline演员
vtkSmartPointer<vtkActor> outlineActor=vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outlineMapper);
outlineActor->GetProperty()->SetColor(,,); //创建显示窗口
vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
//在场景中添加演员
ren1->AddActor(contourActor);
ren1->AddActor(outlineActor);
ren1->SetBackground(,,);
//渲染 互动
renWin->Render();
iren->Start();
}
#ifndefINITIAL_OPENGL
#defineINITIAL_OPENGL
#include<vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include<iostream>
usingnamespacestd;
#include<vtkQuadric.h>
#include<vtkSampleFunction.h>
#include<vtkContourFilter.h>
#include<vtkOutlineFilter.h>
#include<vtkPolyDataMapper.h>
#include<vtkActor.h>
#include<vtkProperty.h>
#include<vtkRenderWindow.h>
#include<vtkRenderer.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkImageData.h>
#include<vtkSmartPointer.h>

voidSphere();
voidPlotFunction(vtkQuadric*quadric,doublevalue);
intmain()
{
Sphere();
return;
}
voidSphere()
{
vtkSmartPointer<vtkQuadric>quadric=vtkSmartPointer<vtkQuadric>::New();
quadric->SetCoefficients(0.5,0.5,,,,,,,,);

//F(x,y,z)=a0*x^2+a1*y^2+a2*z^2+a3*x*y+a4*y*z+a5*x*z+a6*x+a7*y+a8*z+a9
//F(x,y,z)=1*x^2+1*y^2+1*z^2
PlotFunction(quadric,);
}

voidPlotFunction(vtkQuadric*quadric,doublevalue){
//采样二次函数
vtkSmartPointer<vtkSampleFunction>sample=vtkSmartPointer<vtkSampleFunction>::New();
sample->SetSampleDimensions(,,);
sample->SetImplicitFunction(quadric);
//x,y,z取值范围
doublexmin=-,xmax=,ymin=-,ymax=,zmin=,zmax=;
doublescale=0.2;
sample->SetModelBounds(xmin,xmax,ymin,ymax,zmin,zmax);
//创建5个曲面surfacesF(x,y,z)=constant

/*vtkContourFilter*contours=vtkContourFilter::New();
contours->SetInputData(sample->GetOutput());
contours->GenerateValues(5,0,1.2);*/
//创建第0isosurface
vtkSmartPointer<vtkContourFilter>contours=vtkSmartPointer<vtkContourFilter>::New();
contours->SetInputConnection(sample->GetOutputPort());
contours->GenerateValues(,value,value);
//将contours映射到图形图元
vtkSmartPointer<vtkPolyDataMapper>contourMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
contourMapper->SetInputConnection(contours->GetOutputPort());
contourMapper->SetScalarRange(,1.2);
//创建contour演员
vtkSmartPointer<vtkActor>contourActor=vtkSmartPointer<vtkActor>::New();
contourActor->SetMapper(contourMapper);
/*创建一个方盒,将函数曲面包围起来,标识抽样体*/
//创建方盒的边看线
vtkSmartPointer<vtkOutlineFilter>outline=vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputConnection(sample->GetOutputPort());
//将线框映射为图形图元
vtkSmartPointer<vtkPolyDataMapper>outlineMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInputConnection(outline->GetOutputPort());
//创建outline演员
vtkSmartPointer<vtkActor>outlineActor=vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outlineMapper);
outlineActor->GetProperty()->SetColor(,,);

//创建显示窗口
vtkSmartPointer<vtkRenderer>ren1=vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
//在场景中添加演员
ren1->AddActor(contourActor);
ren1->AddActor(outlineActor);
ren1->SetBackground(,,);
//渲染互动
renWin->Render();
iren->Start();
}