用vtk自己创建立体图形的方法.doc_第1页
用vtk自己创建立体图形的方法.doc_第2页
用vtk自己创建立体图形的方法.doc_第3页
用vtk自己创建立体图形的方法.doc_第4页
用vtk自己创建立体图形的方法.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用vtk自己创建立体图形的方法/*=Program: Visualization ToolkitModule: $RCSfile: Cone.cxx,v $Copyright (c) Ken Martin, Will Schroeder, Bill LorensenAll rights reserved.See Copyright.txt or /Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information.=*/ This example creates a polygonal model of a cone, and then renders it to/ the screen. It will rotate the cone 360 degrees and then exit. The basic/ setup of source - mapper - actor - renderer - renderwindow is / typical of most VTK programs./ First include the required header files for the VTK classes we are using.#include vtkConeSource.h#include vtkPolyDataMapper.h#include vtkRenderWindow.h#include vtkCamera.h#include vtkActor.h#include vtkRenderer.h#include vtkRenderer.h#include vtkRenderWindow.h#include vtkRenderWindowInteractor.h#include vtkProperty.h#include vtkCamera.h#include vtkStructuredPointsReader.h#include vtkPiecewiseFunction.h#include vtkColorTransferFunction.h#include vtkVolumeProperty.h#include vtkColorTransferFunction.h#include vtkVolumeRayCastCompositeFunction.h#include vtkVolumeRayCastMapper.h#pragma comment (lib, vtkFiltering.lib)#pragma comment (lib, vtkRendering.lib)#pragma comment (lib, vtkIO.lib)#pragma comment (lib, vtkVolumeRendering.lib)/*=Program: Visualization ToolkitModule: $RCSfile: volProt.cxx,v $Copyright (c) Ken Martin, Will Schroeder, Bill LorensenAll rights reserved.See Copyright.txt or /Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information.=*/#include vtkCamera.h#include vtkFiniteDifferenceGradientEstimator.h#include vtkPiecewiseFunction.h#include vtkRenderWindow.h#include vtkRenderWindowInteractor.h#include vtkRenderer.h#include vtkStructuredPoints.h#include vtkStructuredPointsReader.h#include vtkVolume.h#include vtkVolumeProperty.h#include vtkVolumeRayCastCompositeFunction.h#include vtkVolumeRayCastIsosurfaceFunction.h#include vtkVolumeRayCastMIPFunction.h#include vtkVolumeRayCastMapper.h#include vtkVolumeTextureMapper2D.h#include vtkColorTransferFunction.h#include vtkTestUtilities.h#include vtkRegressionTestImage.h#include vtkDebugLeaks.h#include vtkRenderer.h#include vtkRenderWindow.h#include vtkRenderWindowInteractor.h#include vtkProperty.h#include vtkCamera.h#include vtkPolyDataMapper.h#include vtkPolyDataNormals.h#include vtkContourFilter.h#include vtkPLOT3DReader.h#include vtkSLCReader.h#include vtkActor.h#include vtkCellArray.h#include vtkDoubleArray.h#include vtkFloatArray.h#include vtkIntArray.h#include vtkPointData.h#include vtkPoints.h#include vtkPolyData.h#include vtkPolyDataMapper.h#include vtkRenderWindow.h#include vtkRenderWindowInteractor.h#include vtkRenderer.h#pragma comment (lib, vtkGraphics.lib)#pragma comment (lib, vtkFiltering.lib)#pragma comment (lib, vtkRendering.lib)#pragma comment (lib, vtkIO.lib)int main (int argc, char *argv) vtkRenderer *aRenderer = vtkRenderer:New(); vtkRenderWindow *renWin = vtkRenderWindow:New(); renWin-AddRenderer(aRenderer); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor:New(); iren-SetRenderWindow(renWin); / Create Points for a cube vtkPoints *pt = vtkPoints:New(); pt-SetNumberOfPoints( 8); pt-SetPoint ( 0, 0 ,0, 0); pt-SetPoint( 1 ,1 ,0 ,0); pt-SetPoint ( 2 ,1 ,1 ,0); pt-SetPoint( 3 ,0 ,1 ,0 ); pt-SetPoint( 4 ,0 ,0 ,1); pt-SetPoint( 5 ,1 ,0 ,1); pt-SetPoint( 6 ,1 ,1 ,1); pt-SetPoint( 7 ,0, 1 ,1); /Create Polygons vtkCellArray* cl = vtkCellArray:New();/ Insert a Square cl-InsertNextCell( 4); cl-InsertCellPoint(0); cl-InsertCellPoint(1); cl-InsertCellPoint (2); cl-InsertCellPoint(3); /Insert a Triangle cl-InsertNextCell(3); cl-InsertCellPoint(2); cl-InsertCellPoint(3); cl-InsertCellPoint(6); / Create the Surface vtkPolyData *sur = vtkPolyData:New(); / Set the points and cleanup sur-SetPoints( pt); pt-Delete(); / Set the polygons and cleanup sur-SetPolys (cl); cl-Delete(); /Create a Mapper and set its input vtkPolyDataMapper *map = vtkPolyDataMapper:New(); map-SetInput(sur); /Create the actor and set it to display wireframes vtkActor *Actor = vtkActor:New(); Actor-SetMapper(map); Actor-GetProperty()-SetDiffuse(.8); Actor-GetProperty()-SetSpecular(.5); Actor-GetProperty()-SetSpecularPower(30); aRenderer-AddActor(Actor); aRenderer-SetBackground(1,1,1); renWin-SetSize(320,240); aRenderer-ResetCameraClippingRange(); iren-Initialize(); iren-Start(); aRenderer-Delete(); renWin-Delete(); iren-Delete(); return 0;/ Create an 8x7 grid of render windows in a renderer and render a volume/ using various techniques for testing purposesint mainc( int argc, char *argv )int i, j, k, l;/ Create the renderers, render window, and interactorvtkRenderWindow *renWin = vtkRenderWindow:New();vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor:New();iren-SetRenderWindow(renWin);vtkRenderer *ren = vtkRenderer:New();renWin-AddRenderer(ren);/ Read the data from a vtk filechar* fname = F:/vtkdata-5.0.1/Data/ironProt.vtk;vtkStructuredPointsReader *reader = vtkStructuredPointsReader:New();reader-SetFileName(fname);reader-Update();delete fname;/ Create a transfer function mapping scalar value to opacityvtkPiecewiseFunction *oTFun = vtkPiecewiseFunction:New();oTFun-AddSegment(10, 0.0, 255, 0.3);vtkPiecewiseFunction *oTFun2 = vtkPiecewiseFunction:New();oTFun2-AddSegment( 0, 0.0, 128, 1.0);oTFun2-AddSegment(128, 1.0, 255, 0.0);/ Create a transfer function mapping scalar value to color (grey)vtkPiecewiseFunction *gTFun = vtkPiecewiseFunction:New();gTFun-AddSegment(0, 1.0, 255, 1.0); / Create a transfer function mapping scalar value to color (color)vtkColorTransferFunction *cTFun = vtkColorTransferFunction:New();cTFun-AddRGBPoint( 0, 1.0, 0.0, 0.0 );cTFun-AddRGBPoint( 64, 1.0, 1.0, 0.0 );cTFun-AddRGBPoint( 128, 0.0, 1.0, 0.0 );cTFun-AddRGBPoint( 192, 0.0, 1.0, 1.0 );cTFun-AddRGBPoint( 255, 0.0, 0.0, 1.0 );/ Create a transfer function mapping magnitude of gradient to opacityvtkPiecewiseFunction *goTFun = vtkPiecewiseFunction:New();goTFun-AddPoint( 0, 0.0 );goTFun-AddPoint( 30, 0.0 );goTFun-AddPoint( 40, 1.0 );goTFun-AddPoint( 255, 1.0 );/ Create a set of properties with varying optionsvtkVolumeProperty *prop16;int index = 0;for ( l = 0; l 2; l+ ) for ( k = 0; k 2; k+ ) for ( j = 0; j 2; j+ ) for ( i = 0; i SetShade(k); propindex-SetAmbient(0.3); propindex-SetDiffuse(1.0); propindex-SetSpecular(0.2); propindex-SetSpecularPower(50.0); propindex-SetScalarOpacity(oTFun); if ( l ) propindex-SetGradientOpacity( goTFun ); if ( j ) propindex-SetColor( cTFun ); else propindex-SetColor( gTFun ); if ( i ) propindex-SetInterpolationTypeToNearest(); else propindex-SetInterpolationTypeToLinear(); index+; / Create a set of properties for mipvtkVolumeProperty *mipprop4;index = 0;for ( j = 0; j 2; j+ ) for ( i = 0; i SetScalarOpacity(oTFun2); if ( j ) mippropindex-SetColor( cTFun ); else mippropindex-SetColor( gTFun ); if ( i ) mippropindex-SetInterpolationTypeToNearest(); else mippropindex-SetInterpolationTypeToLinear(); index+; / Create compositing ray functionsvtkVolumeRayCastCompositeFunction *compositeFunction1 = vtkVolumeRayCastCompositeFunction:New();compositeFunction1-SetCompositeMethodToInterpolateFirst();vtkVolumeRayCastCompositeFunction *compositeFunction2 = vtkVolumeRayCastCompositeFunction:New();compositeFunction2-SetCompositeMethodToClassifyFirst();/ Create mip ray functionsvtkVolumeRayCastMIPFunction *MIPFunction1 = vtkVolumeRayCastMIPFunction:New();MIPFunction1-SetMaximizeMethodToScalarValue();vtkVolumeRayCastMIPFunction *MIPFunction2 = vtkVolumeRayCastMIPFunction:New();MIPFunction2-SetMaximizeMethodToOpacity();/ Create an isosurface ray functionvtkVolumeRayCastIsosurfaceFunction *isosurfaceFunction = vtkVolumeRayCastIsosurfaceFunction:New();isosurfaceFunction-SetIsoValue(80);vtkFiniteDifferenceGradientEstimator *gradest = vtkFiniteDifferenceGradientEstimator:New();/ Create 56 volumesvtkVolume *volume56;index = 0;for ( j = 0; j 7; j+ ) for ( i = 0; i AddPosition( i*70, j*70, 0 ); ren-AddViewProp(volumeindex); index+; / Create 48 ray cast mappers - 32 composite, 8 mip, 8 isosurfacevtkVolumeRayCastMapper *raycastMapper48;for ( i = 0; i SetInputConnection(reader-GetOutputPort(); raycastMapper-SetGradientEstimator(gradest); volume-SetMapper( raycastMapper ); if ( i SetProperty( prop ); raycastMapper-SetVolumeRayCastFunction( compositeFunction1 ); else if ( i SetProperty( propi-16 ); raycastMapper-SetVolumeRayCastFunction( compositeFunction2 ); else if ( i SetVolumeRayCastFunction( MIPFunction1 ); volume-SetProperty( mippropi-32 ); else if ( i SetVolumeRayCastFunction( MIPFunction2 ); volume-SetProperty( mippropi-36 ); else raycastMapper-SetVolumeRayCastFunction( isosurfaceFunction ); volume-SetProperty( propi-40 ); / Create 8 texture mappersvtkVolumeTextureMapper2D *textureMapper8;for ( i = 0; i SetInputConnection( reader-GetOutputPort() ); volumei+48-SetMapper( textureMapper ); volumei+48-SetProperty( propi*2 ); renWin-SetSize(400,350);ren-ResetCamera();ren-GetActiveCamera()-Zoom(1.5);renWin-Render();int retVal = vtkRegressionTestImageThreshold( renWin, 70 );/ Interact with the data at 3 frames per secondiren-SetDesiredUpdateRate(3.0);iren-SetStillUpdateRate(0.001);if ( retVal = vtkRegressionTester:DO_INTERACTOR) iren-Start(); / Clean upreader-Delete();oTFun-Delete();oTFun2-Delete();gTFun-Delete();cTFun-Delete();goTFun-Delete();for ( i = 0; i Delete(); for ( i = 0; i Delete(); compositeFunction1-Delete();compositeFunction2-Delete();isosurfaceFunction-Delete();MIPFunction1-Delete();MIPFunction2-Delete();for ( i = 0; i Delete(); gradest-Delete();for ( i = 0; i Delete(); for ( i = 0; i Delete(); ren-Delete();iren-Delete();renWin-Delete();return !retVal;int mainM (int argc, char *argv) vtkRenderer *aRenderer = vtkRenderer:New(); vtkRenderWindow *renWin = vtkRenderWindow:New(); renWin-AddRenderer(aRenderer); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor:New(); iren-SetRenderWindow(renWin); vtkStructuredPointsReader *reader = vtkStructuredPointsReader:New(); reader-SetFileName(F:/vtkdata-5.0.1/Data/tensors.vtk); vtkVolumeRayCastCompositeFunction *ComositeFun = vtkVolumeRayCastCompositeFunction:New(); vtkVolumeRayCastMapper *VolMapper = vtkVolumeRayCastMapper:New(); VolMapper-SetInputConnection(reader-GetOutputPort(); VolMapper-SetVolumeRayCastFunction(ComositeFun); vtkVolume *Volume = vtkVolume:New(); Volume-SetMapper(VolMapper); vtkColorTransferFunction *gTFun = vtkColorTransferFunction:New(); gTFun-AddRGBSegment(128.0,126/255.0,16/255.0,16/255.0, 255.0,64/255.0,255/255.0,16/255.0); vtkVolumeProperty *VolProperty = vtkVolumeProperty:New(); VolProperty-SetColor(gTFun); vtkPiecewiseFunction *oTFun = vtkPiecewiseFunction:New(); oTFun-AddSegment(48,0.0,256,1.0);/改变第一个数据,可以得到不同的体透明度 VolProperty-SetScalarOpacity(oTFun); VolProperty-SetInterpolationTypeToLinear(); VolProperty-ShadeOn(); Volume-SetProperty(VolProperty); aRenderer-AddActor(Volume); aRenderer-SetBackground(1,1,1); renWin-SetSize(640,480); aRenderer-ResetCameraClippingRange(); iren-Initialize(); iren-Start(); aRenderer-Delete(); renWin-Delete(); iren-Delete(); return 0;int mainc()/ / Next we create an instance of vtkConeSource and set some of its/ properties. The instance of vtkConeSource cone is part of a/ 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( 20.0 );cone-SetRadius( 2.0 );cone-SetResolution( 7 );/ / 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 a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论