VTK入门程序样例分析-vtk与MFc混编

更新时间:2024-05-29 16:11:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

VTk与MFC混编

前面几个例子是熟悉vtk的,练习时用控制台程序编写,vtk与MFC混编的过程中遇到了很多问题,为了方便大家,我把这一过程中的遇到的错误和解决办法都记录下来了,希望能够对大家有所帮助

VTK入门程序样例分析

一、圆锥圆柱类

1、简单圆锥【圆锥鼠标拖动】

#include \ #include \ #include \ #include \ #include \ #include \

int main() {

vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 );

vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper->SetInputConnection( cone->GetOutputPort() );

vtkActor *coneActor = vtkActor::New(); coneActor->SetMapper( coneMapper );

vtkRenderer *ren1= vtkRenderer::New(); ren1->AddActor( coneActor );

ren1->SetBackground( 0.1, 0.2, 0.4 );

vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer( ren1 ); renWin->SetSize( 300, 300 );

int i;

for (i = 0; i < 360; ++i) {

// render the image renWin->Render();

// rotate the active camera by one degree ren1->GetActiveCamera()->Azimuth( 1 ); }

cone->Delete(); coneMapper->Delete(); coneActor->Delete(); ren1->Delete(); renWin->Delete(); return 0; }

1、新建工程 2、输入代码 3、添加包含文件 4、编译出错 cone1.obj : error LNK2019: unresolved external symbol \public: void __thiscall vtkCamera::Azimuth(double)\(__imp_?Azimuth@vtkCamera@@QAEXN@Z) referenced in function _wmain

错误解决办法 error LNK2019: 无法解析的外部符号 \称找不到vtkIO.dll 对应解决办法:在每个项目属性的链接器->输入->附加依赖项中添加入vtkIO.lib库,目录为:..\\..\\..\\..\\bin\\Debug\\vtkIO.lib

F:\\VTK58\\bin\\bin\\Debug\\vtkMFC.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkRendering.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkIO.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkFiltering.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkCommon.lib;DelayImp.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkGraphics.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkImaging.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkFiltering.lib;F:\\VTK58\\bin\\bin\\Debug\\vtkCommon.lib;F:\\VTK58\\bin\\bin\\Debug\\vtksys.lib;

错误没了!

2、Callback命令

#include \

#include \ #include \ #include \ #include \ #include \ #include \

// Callback for the interaction

class vtkMyCallback : public vtkCommand { public:

static vtkMyCallback *New() { return new vtkMyCallback; }

virtual void Execute(vtkObject *caller, unsigned long, void*) {

vtkRenderer *renderer = reinterpret_cast(caller); cout << renderer->GetActiveCamera()->GetPosition()[0] << \ << renderer->GetActiveCamera()->GetPosition()[1] << \ << renderer->GetActiveCamera()->GetPosition()[2] << \; } };

int main() { //

// The pipeline creation is documented in Step1 //

vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 );

vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper->SetInputConnection( cone->GetOutputPort() ); vtkActor *coneActor = vtkActor::New(); coneActor->SetMapper( coneMapper );

vtkRenderer *ren1= vtkRenderer::New(); ren1->AddActor( coneActor );

ren1->SetBackground( 0.1, 0.2, 0.4 ); ren1->ResetCamera();

vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer( ren1 );

renWin->SetSize( 300, 300 );

// Here is where we setup the observer, we do a new and ren1 will // eventually free the observer

vtkMyCallback *mo1 = vtkMyCallback::New(); ren1->AddObserver(vtkCommand::StartEvent,mo1); mo1->Delete(); //

// now we loop over 360 degrees and render the cone each time // int i;

for (i = 0; i < 360; ++i) {

// render the image renWin->Render();

// rotate the active camera by one degree ren1->GetActiveCamera()->Azimuth( 1 ); } //

// Free up any objects we created //

cone->Delete(); coneMapper->Delete(); coneActor->Delete(); ren1->Delete(); renWin->Delete();

return 0; }

3、一个圆锥两个renderer在同一个窗口下显示

#include \ #include \ #include \ #include \ #include \ #include \

int main() { //

// Next we create an instance of vtkConeSource and set some of its // properties. The instance of vtkConeSource \

// visualization pipeline (it is a source process object); it produces data // (output type is vtkPolyData) which other filters may process. //

vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 ); //

// In this example we terminate the pipeline with a mapper process object. // (Intermediate filters such as vtkShrinkPolyData could be inserted in // between the source and the mapper.) We create an instance of

// vtkPolyDataMapper to map the polygonal data into graphics primitives. We // connect the output of the cone souece to the input of this mapper. //

vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper->SetInputConnection( cone->GetOutputPort() ); //

// Create an actor to represent the cone. The actor orchestrates rendering // of the mapper's graphics primitives. An actor also refers to properties // via a vtkProperty instance, and includes an internal transformation // matrix. We set this actor's mapper to be coneMapper which we created // above. //

vtkActor *coneActor = vtkActor::New(); coneActor->SetMapper( coneMapper ); //

// Create two renderers and assign actors to them. A renderer renders into // a viewport within the vtkRenderWindow. It is part or all of a window on // the screen and it is responsible for drawing the actors it has. We also // set the background color here. In this example we are adding the same // actor to two different renderers; it is okay to add different actors to // different renderers as well. //

vtkRenderer *ren1= vtkRenderer::New(); ren1->AddActor( coneActor );

ren1->SetBackground( 0.1, 0.2, 0.4 ); ren1->SetViewport(0.0, 0.0, 0.5, 1.0);

vtkRenderer *ren2= vtkRenderer::New(); ren2->AddActor( coneActor );

ren2->SetBackground( 0.2, 0.3, 0.5 ); ren2->SetViewport(0.5, 0.0, 1.0, 1.0); //

// Finally we create the render window which will show up on the screen. // We put our renderer into the render window using AddRenderer. We also // set the size to be 300 pixels by 300. //

vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer( ren1 ); renWin->AddRenderer( ren2 ); renWin->SetSize( 600, 300 ); //

// Make one view 90 degrees from other. //

ren1->ResetCamera();

ren1->GetActiveCamera()->Azimuth(90); //

// Now we loop over 360 degreeees and render the cone each time. // int i;

for (i = 0; i < 360; ++i) {

// render the image renWin->Render();

// rotate the active camera by one degree ren1->GetActiveCamera()->Azimuth( 1 ); ren2->GetActiveCamera()->Azimuth( 1 ); } //

// Free up any objects we created. All instances in VTK are deleted by // using the Delete() method. //

cone->Delete(); coneMapper->Delete(); coneActor->Delete(); ren1->Delete(); ren2->Delete(); renWin->Delete();

return 0; }

4、一个圆锥两个renderer在同一个窗口下显示

Cone4

#include \ #include \ #include \ #include \ #include \ #include \ #include \

int main() { //

// Next we create an instance of vtkConeSource and set some of its // properties. The instance of vtkConeSource \

// visualization pipeline (it is a source process object); it produces data // (output type is vtkPolyData) which other filters may process. //

vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 ); //

// In this example we terminate the pipeline with a mapper process object. // (Intermediate filters such as vtkShrinkPolyData could be inserted in // between the source and the mapper.) We create an instance of

// vtkPolyDataMapper to map the polygonal data into graphics primitives. We

二、Filter 1、ImplicitSum

#include \

#include \#include \

#include \#include \#include \#include \#include \#include \#include \#include \#include \

#include #include #include #include

#include

#include

void main() {

// rand()/RAND_MAX;// 0 - RAND_MAX vtkRenderer *ren = vtkRenderer::New();

vtkRenderWindow *renWindow = vtkRenderWindow::New(); renWindow->AddRenderer(ren); renWindow->SetSize( 600, 600 );

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWindow);

vtkCone *geomObject1=vtkCone::New();

vtkSphere *geomObject2=vtkSphere::New(); geomObject2-> SetRadius( 0.5); geomObject2-> SetCenter( 0.5, 0 ,0);

vtkImplicitSum *sum=vtkImplicitSum::New(); sum-> SetNormalizeByWeight( 1); sum-> AddFunction (geomObject1 ,2); sum-> AddFunction (geomObject2 ,1);

vtkSampleFunction *sample=vtkSampleFunction::New(); sample-> SetImplicitFunction( sum);

sample-> SetSampleDimensions( 60, 60 ,60); sample-> ComputeNormalsOn();

vtkContourFilter *surface=vtkContourFilter::New();

surface-> SetInputConnection (sample->GetOutputPort()); surface-> SetValue (0 ,0.0);

vtkPolyDataMapper *mapper=vtkPolyDataMapper::New(); mapper-> SetInputConnection(surface-> GetOutputPort()); mapper-> ScalarVisibilityOff();

vtkActor *actor=vtkActor::New(); actor-> SetMapper (mapper);

actor-> GetProperty()-> SetColor( 0.2 ,0.4 ,0.6); actor-> GetProperty()-> SetSpecular ( 0.4); actor-> GetProperty()-> SetDiffuse ( 0.7);

actor-> GetProperty()-> SetSpecularPower ( 40); //////////////

ren->AddActor(actor);

iren->Initialize();

renWindow->Render();

iren->Start(); }

2、PerlinNoise

#include \ #include \ #include \

#include \ #include \ #include \ #include \ #include \ #include \

#include \ #include \ #include \ #include #include #include #include #include #include

void main() {

srand(time(NULL)) ;

// rand()/RAND_MAX;//

0 - RAND_MAX

vtkRenderer *ren = vtkRenderer::New();

vtkRenderWindow *renWindow = vtkRenderWindow::New(); renWindow->AddRenderer(ren); renWindow->SetSize( 600, 600 );

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWindow);

vtkPerlinNoise *perlin=vtkPerlinNoise::New(); perlin-> SetFrequency (2 ,6.25, 5.5); perlin-> SetPhase (0 ,0, 0);

vtkSampleFunction *sample=vtkSampleFunction::New(); sample-> SetImplicitFunction (perlin); sample-> SetSampleDimensions( 65,65 ,20); sample-> ComputeNormalsOff();

vtkContourFilter *surface=vtkContourFilter::New();

surface-> SetInputConnection(sample-> GetOutputPort()); surface-> SetValue (0, 0.0);

vtkPolyDataMapper *mapper=vtkPolyDataMapper::New();

mapper-> SetInputConnection(surface-> GetOutputPort()); mapper-> ScalarVisibilityOff();

vtkActor *actor=vtkActor::New(); actor-> SetMapper (mapper);

actor-> GetProperty()-> SetColor( 0.2 ,0.4 ,0.6);

////////////// ren->AddActor(actor);

iren->Initialize(); renWindow->Render();

iren->Start(); }

3、样条曲线CSpline

.

#include \ #include \ #include \

#include \ #include \ #include \ #include \ #include \ #include \

#include \ #include \ #include \ #include #include #include #include #include #include

void main() {

double x,y,z; int i;

for(i=0;i

x = (float)rand()/(float)RAND_MAX; y = (float)rand()/(float)RAND_MAX; z = (float)rand()/(float)RAND_MAX; printf(\,x,y,z); aSplineX->AddPoint(i, x); aSplineY->AddPoint(i, y); aSplineZ->AddPoint(i, z);

inputPoints->InsertPoint(i, x, y, z); vtkPoints *inputPoints = vtkPoints::New();

vtkCardinalSpline *aSplineX = vtkCardinalSpline::New(); vtkCardinalSpline *aSplineY = vtkCardinalSpline::New(); vtkCardinalSpline *aSplineZ = vtkCardinalSpline::New(); int numberOfInputPoints = 30;

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWindow); // rand()/RAND_MAX;//

0 - RAND_MAX

vtkRenderer *ren = vtkRenderer::New();

vtkRenderWindow *renWindow = vtkRenderWindow::New(); renWindow->AddRenderer(ren); renWindow->SetSize( 600, 600 ); srand(time(NULL)) ;

}

vtkPolyData *inputData =vtkPolyData::New(); inputData->SetPoints(inputPoints);

vtkSphereSource *balls =vtkSphereSource::New(); balls->SetRadius(.01); balls->SetPhiResolution(10); balls->SetThetaResolution(10);

vtkGlyph3D *glyphPoints =vtkGlyph3D::New(); glyphPoints->SetInput(inputData);

glyphPoints->SetSource(balls->GetOutput());

vtkPolyDataMapper *glyphMapper = vtkPolyDataMapper::New(); glyphMapper->SetInputConnection(glyphPoints->GetOutputPort()); vtkActor *glyph = vtkActor::New(); glyph->SetMapper(glyphMapper);

glyph->GetProperty()->SetDiffuseColor(1,0,0); glyph->GetProperty()->SetSpecular(.3); glyph->GetProperty()->SetSpecularPower(30); ///////////////

vtkPoints *points = vtkPoints::New(); // Number of points on the spline int numberOfOutputPoints = 400; float t;

for(i=0;i

vtkCellArray *lines = vtkCellArray::New();

int a=lines->InsertNextCell(numberOfOutputPoints); for(i=0;i

t = (numberOfInputPoints-1.0)/(numberOfOutputPoints-1.0)*i;

points->InsertPoint(i, aSplineX->Evaluate(t), aSplineY->Evaluate(t),

aSplineZ->Evaluate(t));

points->GetPoint(i)[1],points->GetPoint(i)[2]); printf(\,points->GetPoint(i)[0],

}

}

lines->InsertCellPoint(i);

vtkPolyData *profileData = vtkPolyData::New(); profileData->SetPoints(points); profileData->SetLines(lines);

// Add thickness to the resulting line.

vtkTubeFilter *profileTubes = vtkTubeFilter::New(); profileTubes->SetNumberOfSides(8); profileTubes->SetInput(profileData); profileTubes->SetRadius(0.005);

vtkPolyDataMapper *profileMapper = vtkPolyDataMapper::New(); profileMapper->SetInputConnection(profileTubes->GetOutputPort()); vtkActor *profile = vtkActor::New(); profile->SetMapper(profileMapper);

profile->GetProperty()->SetDiffuseColor(1,1,0); profile->GetProperty()->SetSpecular(0.3); profile->GetProperty()->SetSpecularPower(30); ////////////// ren->AddActor(glyph); ren->AddActor(profile); iren->Initialize(); renWindow->Render(); iren->Start();

4、图形切割

#include \

#include \ #include \ #include \ #include \ #include \ #include \

#include \ #include \ #include \

#include \ #include \

int main() {

vtkQuadric *quadric = vtkQuadric::New();

vtkSampleFunction *sample = vtkSampleFunction::New();

vtkExtractVOI *extract = vtkExtractVOI::New();

sample->SetSampleDimensions(30, 30, 30); sample->SetImplicitFunction(quadric); sample->ComputeNormalsOff();

quadric->SetCoefficients(.5, 1, .2, 0, .1, 0, 0, .2, 0, 0);

extract->SetInputConnection(sample->GetOutputPort());

extract->SetVOI(0, 29, 0, 29, 5, 25); //**********调整三维切割

// extract->SetSampleRate(1, 2, 3);

vtkContourFilter *contours = vtkContourFilter::New();

vtkPolyDataMapper *contMapper = vtkPolyDataMapper::New();

vtkActor *contActor = vtkActor::New();

vtkOutlineFilter *outline = vtkOutlineFilter::New();

vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New();

vtkActor *outlineActor = vtkActor::New();

vtkRenderer *ren1 = vtkRenderer::New();

vtkRenderWindow *renWin = vtkRenderWindow::New(); }

return 0;

ren1->ResetCamera();

ren1->GetActiveCamera()->Zoom( 1.5 ); renWin->Render(); iren->Initialize(); iren->Start();

ren1->SetBackground(1, 1, 1); ren1->AddActor(contActor); ren1->AddActor(outlineActor); renWin->AddRenderer(ren1); iren->SetRenderWindow(renWin);

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

outlineActor->SetMapper(outlineMapper);

outlineActor->GetProperty()->SetColor(0, 0, 0);

outlineMapper->SetInputConnection(outline->GetOutputPort()); outline->SetInputConnection(sample->GetOutputPort()); contActor->SetMapper(contMapper);

contMapper->SetInputConnection(contours->GetOutputPort()); contMapper->SetScalarRange(0.0, 1.2);

contours->SetInputConnection(extract->GetOutputPort()); contours->GenerateValues(13, 0.0, 1.2);

本文来源:https://www.bwwdw.com/article/y2y6.html

Top