VTK例子16-体绘制
体绘制#include "stdafx.h"/*========================================================================= Program: Visualization ToolkitModule: $RCSfile: volProt.cxx,v $ Copyright (c) Ken Martin, Will Schroeder, Bill LorensenAll rights reserved.See Copyright.txt or http://www.kitware.com/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" // Create an 8x7 grid of render windows in a renderer and render a volume// using various techniques for testing purposesint main( 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 = vtkTestUtilities::ExpandDataFileName(argc, argv, "ironProt.vtk");vtkStructuredPointsReader *reader = vtkStructuredPointsReader::New();reader->SetFileName("ironProt.vtk");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 *prop;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 < 2; i++ ) { prop = vtkVolumeProperty::New(); prop->SetShade(k); prop->SetAmbient(0.3); prop->SetDiffuse(1.0); prop->SetSpecular(0.2); prop->SetSpecularPower(50.0); prop->SetScalarOpacity(oTFun); if ( l ) { prop->SetGradientOpacity( goTFun ); } if ( j ) { prop->SetColor( cTFun ); } else { prop->SetColor( gTFun ); } if ( i ) { prop->SetInterpolationTypeToNearest(); } else { prop->SetInterpolationTypeToLinear(); } index++; } } } } // Create a set of properties for mipvtkVolumeProperty *mipprop;index = 0;for ( j = 0; j < 2; j++ ) { for ( i = 0; i < 2; i++ ) { mipprop = vtkVolumeProperty::New(); mipprop->SetScalarOpacity(oTFun2); if ( j ) { mipprop->SetColor( cTFun ); } else { mipprop->SetColor( gTFun ); } if ( i ) { mipprop->SetInterpolationTypeToNearest(); } else { mipprop->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 *volume;index = 0;for ( j = 0; j < 7; j++ ) { for ( i = 0; i < 8; i++ ) { volume = vtkVolume::New(); volume->AddPosition( i*70, j*70, 0 ); ren->AddViewProp(volume); index++; } }// Create 48 ray cast mappers - 32 composite, 8 mip, 8 isosurfacevtkVolumeRayCastMapper *raycastMapper;for ( i = 0; i < 48; i++ ) { raycastMapper = vtkVolumeRayCastMapper::New(); raycastMapper->SetInputConnection(reader->GetOutputPort()); raycastMapper->SetGradientEstimator(gradest); volume->SetMapper( raycastMapper ); if ( i < 16 ) { volume->SetProperty( prop ); raycastMapper->SetVolumeRayCastFunction( compositeFunction1 ); } else if ( i < 32 ) { volume->SetProperty( prop ); raycastMapper->SetVolumeRayCastFunction( compositeFunction2 ); } else { if ( i < 36 ) { raycastMapper->SetVolumeRayCastFunction( MIPFunction1 ); volume->SetProperty( mipprop ); } else if ( i < 40 ) { raycastMapper->SetVolumeRayCastFunction( MIPFunction2 ); volume->SetProperty( mipprop ); } else { raycastMapper->SetVolumeRayCastFunction( isosurfaceFunction ); volume->SetProperty( prop ); } } } // Create 8 texture mappersvtkVolumeTextureMapper2D *textureMapper;for ( i = 0; i < 8; i++ ) { textureMapper = vtkVolumeTextureMapper2D::New(); textureMapper->SetInputConnection( reader->GetOutputPort() ); volume->SetMapper( textureMapper ); volume->SetProperty( prop ); } 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); iren->Start(); // Clean upreader->Delete();oTFun->Delete();oTFun2->Delete();gTFun->Delete();cTFun->Delete();goTFun->Delete();for ( i = 0; i < 16; i++ ) { prop->Delete(); }for ( i = 0; i < 4; i++ ) { mipprop->Delete(); }compositeFunction1->Delete();compositeFunction2->Delete();isosurfaceFunction->Delete();MIPFunction1->Delete();MIPFunction2->Delete();for ( i = 0; i < 56; i++ ) { volume->Delete(); }gradest->Delete();for ( i = 0; i < 48; i++ ) { raycastMapper->Delete(); }for ( i = 0; i < 8; i++ ) { textureMapper->Delete(); }ren->Delete();iren->Delete();renWin->Delete();return !retVal;} 这个东西一定要看看 学习学习。。。 先看看在说
页:
[1]