医站点医维基

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1377|回复: 3

[VTK] VTK例子16-体绘制

[复制链接]

336

主题

410

回帖

2806

积分

管理员

积分
2806

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2015-12-5 17:57:11 | 显示全部楼层 |阅读模式
体绘制
#include "stdafx.h"
/*=========================================================================
  Program:   Visualization Toolkit
  Module:    $RCSfile: volProt.cxx,v $
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All 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 purposes
int main( int argc, char *argv[] )
{
  int i, j, k, l;
  
  // Create the renderers, render window, and interactor
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);
  vtkRenderer *ren = vtkRenderer::New();
  renWin->AddRenderer(ren);
  
  // Read the data from a vtk file
  char* 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 opacity
  vtkPiecewiseFunction *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 opacity
  vtkPiecewiseFunction *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 options
  vtkVolumeProperty *prop[16];
  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[index] = vtkVolumeProperty::New();
          prop[index]->SetShade(k);
          prop[index]->SetAmbient(0.3);
          prop[index]->SetDiffuse(1.0);
          prop[index]->SetSpecular(0.2);
          prop[index]->SetSpecularPower(50.0);
          prop[index]->SetScalarOpacity(oTFun);
         
          if ( l )
            {
            prop[index]->SetGradientOpacity( goTFun );
            }
         
          if ( j )
            {
            prop[index]->SetColor( cTFun );
            }
          else
            {
            prop[index]->SetColor( gTFun );
            }
         
          if ( i )
            {
            prop[index]->SetInterpolationTypeToNearest();
            }
          else
            {
            prop[index]->SetInterpolationTypeToLinear();
            }
         
          index++;
          }
        }
      }
    }
  // Create a set of properties for mip
  vtkVolumeProperty *mipprop[4];
  index = 0;
  for ( j = 0; j < 2; j++ )
    {
    for ( i = 0; i < 2; i++ )
      {
      mipprop[index] = vtkVolumeProperty::New();
      mipprop[index]->SetScalarOpacity(oTFun2);         
         
      if ( j )
        {
        mipprop[index]->SetColor( cTFun );
        }
      else
        {
        mipprop[index]->SetColor( gTFun );
        }
      
      if ( i )
        {
        mipprop[index]->SetInterpolationTypeToNearest();
        }
      else
        {
        mipprop[index]->SetInterpolationTypeToLinear();
        }
         
      index++;
      }
    }
  
  // Create compositing ray functions
  vtkVolumeRayCastCompositeFunction *compositeFunction1 =
    vtkVolumeRayCastCompositeFunction::New();
  compositeFunction1->SetCompositeMethodToInterpolateFirst();
  vtkVolumeRayCastCompositeFunction *compositeFunction2 =
    vtkVolumeRayCastCompositeFunction::New();
  compositeFunction2->SetCompositeMethodToClassifyFirst();
  
  // Create mip ray functions
  vtkVolumeRayCastMIPFunction *MIPFunction1 =
    vtkVolumeRayCastMIPFunction::New();
  MIPFunction1->SetMaximizeMethodToScalarValue();
  vtkVolumeRayCastMIPFunction *MIPFunction2 =
    vtkVolumeRayCastMIPFunction::New();
  MIPFunction2->SetMaximizeMethodToOpacity();
  // Create an isosurface ray function
  vtkVolumeRayCastIsosurfaceFunction *isosurfaceFunction =
    vtkVolumeRayCastIsosurfaceFunction::New();
  isosurfaceFunction->SetIsoValue(80);
  vtkFiniteDifferenceGradientEstimator *gradest =
    vtkFiniteDifferenceGradientEstimator::New();
  
  // Create 56 volumes
  vtkVolume *volume[56];
  index = 0;
  for ( j = 0; j < 7; j++ )
    {
    for ( i = 0; i < 8; i++ )
      {
      volume[index] = vtkVolume::New();
      volume[index]->AddPosition( i*70, j*70, 0 );
      ren->AddViewProp(volume[index]);
      index++;
      }
    }
  
  
  // Create 48 ray cast mappers - 32 composite, 8 mip, 8 isosurface
  vtkVolumeRayCastMapper *raycastMapper[48];
  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[i-16] );
      raycastMapper->SetVolumeRayCastFunction( compositeFunction2 );
      }
    else
      {
      if ( i < 36 )
        {
        raycastMapper->SetVolumeRayCastFunction( MIPFunction1 );  
        volume->SetProperty( mipprop[i-32] );
        }
      else if ( i < 40 )
        {
        raycastMapper->SetVolumeRayCastFunction( MIPFunction2 );  
        volume->SetProperty( mipprop[i-36] );
        }
      else
        {
        raycastMapper->SetVolumeRayCastFunction( isosurfaceFunction );  
        volume->SetProperty( prop[i-40] );
        }
      }
    }
  // Create 8 texture mappers
  vtkVolumeTextureMapper2D *textureMapper[8];
  for ( i = 0; i < 8; i++ )
    {
    textureMapper = vtkVolumeTextureMapper2D::New();
    textureMapper->SetInputConnection( reader->GetOutputPort() );   
    volume[i+48]->SetMapper( textureMapper );
    volume[i+48]->SetProperty( prop[i*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 second
  iren->SetDesiredUpdateRate(3.0);
  iren->SetStillUpdateRate(0.001);
    iren->Start();
  
  // Clean up
  reader->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;
}
16.png
回复

使用道具 举报

178

主题

438

回帖

1713

积分

金牌会员

积分
1713

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-18 04:25:09 | 显示全部楼层
这个东西一定要看看
回复

使用道具 举报

13

主题

398

回帖

1051

积分

版主

积分
1051

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-21 05:24:35 | 显示全部楼层
学习学习。。。
回复

使用道具 举报

13

主题

398

回帖

1051

积分

版主

积分
1051

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-22 23:31:11 | 显示全部楼层
先看看在说
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|关于我们|医维基|网站地图|Archiver|手机版|医疗之家 ( 沪ICP备2023001278号-1 )  

GMT+8, 2024-5-2 09:53 , Processed in 0.153734 second(s), 31 queries .

Designed by Medical BBS

快速回复 返回顶部 返回列表