首页 > 代码库 > vtkQuadratic创建半球面

vtkQuadratic创建半球面

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

技术分享

 

 

 

 1 #ifndef INITIAL_OPENGL
 2 #define INITIAL_OPENGL
 3 #include <vtkAutoInit.h>
 4 VTK_MODULE_INIT(vtkRenderingOpenGL)
 5 VTK_MODULE_INIT(vtkInteractionStyle)
 6 #endif
 7 #include <iostream>
 8 using namespace std;
 9 #include <vtkQuadric.h>
10 #include <vtkSampleFunction.h>
11 #include <vtkContourFilter.h>
12 #include <vtkOutlineFilter.h>
13 #include <vtkPolyDataMapper.h>
14 #include <vtkActor.h>
15 #include <vtkProperty.h>
16 #include <vtkRenderWindow.h>
17 #include <vtkRenderer.h>
18 #include <vtkRenderWindowInteractor.h>
19 #include <vtkImageData.h>
20 #include <vtkSmartPointer.h>
21 
22 void Sphere();
23 void PlotFunction(vtkQuadric*quadric,double value);
24 int main()
25 {
26     Sphere();
27     return 0;
28 }
29 void Sphere()
30 {
31     vtkSmartPointer<vtkQuadric>quadric=vtkSmartPointer<vtkQuadric>::New();
32     quadric->SetCoefficients(0.5,0.5,1,0,0,0,0,0,0,0);
33 
34     // 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
35       // F(x,y,z) = 1*x^2 + 1*y^2 + 1*z^2
36     PlotFunction(quadric,1);
37 }
38 
39 void PlotFunction(vtkQuadric *quadric, double value){
40     //采样二次函数
41     vtkSmartPointer<vtkSampleFunction>sample=vtkSmartPointer<vtkSampleFunction>::New();
42     sample->SetSampleDimensions(150,150,150);
43     sample->SetImplicitFunction(quadric);
44     //x,y,z取值范围
45     double xmin=-2,xmax=2,ymin=-2,ymax=2,zmin=0,zmax=2;
46     double scale=0.2;
47     sample->SetModelBounds(xmin,xmax,ymin,ymax,zmin,zmax);
48     //创建5个曲面surfaces F(x,y,z) = constant
49 
50 /*vtkContourFilter*contours=vtkContourFilter::New();
51     contours->SetInputData(sample->GetOutput());
52     contours->GenerateValues(5,0,1.2);*/
53     //创建第0 isosurface
54     vtkSmartPointer<vtkContourFilter>contours=vtkSmartPointer<vtkContourFilter>::New();
55     contours->SetInputConnection(sample->GetOutputPort());
56     contours->GenerateValues(1,value,value);
57     //将contours映射到图形图元
58     vtkSmartPointer<vtkPolyDataMapper>contourMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
59     contourMapper->SetInputConnection(contours->GetOutputPort());
60     contourMapper->SetScalarRange(0,1.2);
61     //创建contour演员
62     vtkSmartPointer<vtkActor>contourActor=vtkSmartPointer<vtkActor>::New();
63     contourActor->SetMapper(contourMapper);
64 /*创建一个方盒,将函数曲面包围起来,标识抽样体*/
65     //创建方盒的边看线
66     vtkSmartPointer<vtkOutlineFilter>outline=vtkSmartPointer<vtkOutlineFilter>::New();
67     outline->SetInputConnection(sample->GetOutputPort());
68     //将线框映射为图形图元
69     vtkSmartPointer<vtkPolyDataMapper>outlineMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
70     outlineMapper->SetInputConnection(outline->GetOutputPort());
71     //创建outline演员
72     vtkSmartPointer<vtkActor> outlineActor=vtkSmartPointer<vtkActor>::New();
73     outlineActor->SetMapper(outlineMapper);
74     outlineActor->GetProperty()->SetColor(0,0,0);
75 
76     //创建显示窗口
77     vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer<vtkRenderer>::New();
78     vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
79     renWin->AddRenderer(ren1);
80     vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
81     iren->SetRenderWindow(renWin);
82     //在场景中添加演员
83     ren1->AddActor(contourActor);
84     ren1->AddActor(outlineActor);
85     ren1->SetBackground(1,1,1);
86     //渲染 互动
87     renWin->Render();
88     iren->Start();
89 }

 

#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();
return0;
}
voidSphere()
{
vtkSmartPointer<vtkQuadric>quadric=vtkSmartPointer<vtkQuadric>::New();
quadric->SetCoefficients(0.5,0.5,1,0,0,0,0,0,0,0);

//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,1);
}

voidPlotFunction(vtkQuadric*quadric,doublevalue){
//采样二次函数
vtkSmartPointer<vtkSampleFunction>sample=vtkSmartPointer<vtkSampleFunction>::New();
sample->SetSampleDimensions(150,150,150);
sample->SetImplicitFunction(quadric);
//x,y,z取值范围
doublexmin=-2,xmax=2,ymin=-2,ymax=2,zmin=0,zmax=2;
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(1,value,value);
//将contours映射到图形图元
vtkSmartPointer<vtkPolyDataMapper>contourMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
contourMapper->SetInputConnection(contours->GetOutputPort());
contourMapper->SetScalarRange(0,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(0,0,0);

//创建显示窗口
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(1,1,1);
//渲染互动
renWin->Render();
iren->Start();
}

vtkQuadratic创建半球面