为什么 vtkClipPolyData 在 vtk 中的表面渲染输出中渲染得非常慢

Why is vtkClipPolyData is rendering very slowly in surface rendered output in vtk

我有以下代码来裁剪模型,但是当我更新平面以生成新的裁剪表面时,速度非常慢。有没有办法让它更快?还是有另一个 vtk class 执行得更快?

vtkSmartPointer<vtkPlane> clipPlane =  vtkSmartPointer<vtkPlane>::New();
clipPlane->SetNormal(1, 1, 0);
clipPlane->SetOrigin(0.0, -10.0, 0.0 );

vtkSmartPointer<vtkClipPolyData> clipper = 
vtkSmartPointer<vtkClipPolyData>::New();
clipper->SetInputData( reader->GetOutput() );
clipper->SetClipFunction( clipPlane );
clipper->Update();

但是我没有找到这个 question.Any 的答案 我发现我的问题与给定 below.For 的 link 中的问题相同,请使用下面的 link

http://vtk.1045678.n5.nabble.com/is-vtkClipPolyData-slow-td5727625.html1

取决于您希望实现的目标:vtkClipPolyData 计算一个新的几何形状,并使用提供的 vtkImplicitFunction 进行裁剪。我想你可以写一个 "StrictlyPlaneClipPolyData" 比当前过滤器更快的过滤器。

然而,如果渲染是这样的话,那么你可以(并且应该)让着色器来做。简单如下:

//vtkActor * actor;
actor->GetMapper()->AddClippingPlane( clipPlane );

交互之后,也许你只应用了一次几何滤镜。

* 编辑 *

演示性能的示例代码:

#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkClipPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include <Windows.h>
#include "vtkPlane.h"
#include "vtkTransform.h"
#include "vtkMath.h"
#include "vtkProperty.h"
// performance counter in ms units
double ticks(void)
{
    LARGE_INTEGER lg,f;
    if ( QueryPerformanceCounter( &lg ) && QueryPerformanceFrequency( &f ) )
        return 1000.0 * (double)lg.QuadPart/(double)f.QuadPart;
    else
        return (double)GetTickCount();
}
void ClipSample( void )
{
    vtkPolyData * monkey;
    // monkey is the Blender monkey courtesy of Blender.org 
    // @126 k vertices
    // make sure to transform the monkey into origin 

    // create render window
    vtkSmartPointer< vtkRenderer > ren = vtkSmartPointer< vtkRenderer >::New();
    vtkSmartPointer< vtkRenderWindow > rw = vtkSmartPointer< vtkRenderWindow >::New();
    rw->AddRenderer( ren );
    rw->SetSize( 1024,1024 );
    // create interactor used later
    vtkSmartPointer< vtkRenderWindowInteractor > ia = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    ia->SetRenderWindow( rw );
    ia->SetInteractorStyle(  vtkSmartPointer< vtkInteractorStyleTrackballCamera >::New() );
    ia->Initialize();

    // create clipper
    vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<vtkPlane> ::New();
    plane->SetNormal(0,0,1);
    plane->SetOrigin(0,0,0);

//#define SOFTWARE_CLIPPER

    // create mapper
    vtkSmartPointer< vtkPolyDataMapper > mapper = vtkSmartPointer< vtkPolyDataMapper >::New();
#ifdef SOFTWARE_CLIPPER
    vtkSmartPointer< vtkClipPolyData > clipper = vtkSmartPointer< vtkClipPolyData >::New();
    clipper->SetInputData( monkey );
    clipper->SetClipFunction( plane );
    mapper->SetInputConnection( clipper->GetOutputPort() );
#else
    mapper->SetInputData( monkey );
    mapper->AddClippingPlane( plane );
#endif

    // create actor
    vtkSmartPointer< vtkActor > actor = vtkSmartPointer< vtkActor >::New();
    actor->SetMapper( mapper );
    actor->GetProperty()->SetColor( 0.3, 1.0, 0.7 );

    // color back face to emphasize clipping
    vtkSmartPointer< vtkProperty > backface = vtkSmartPointer< vtkProperty > ::New();
    backface->SetColor(1,0,0);
    actor->SetBackfaceProperty( backface );

    ren->AddActor( actor );
    double delta = 20.0;

    double timeTotal=0.0;
    double count=0.0;
    for ( int i=0;i<10;++i)
    {
        // make rotation axis
        vtkSmartPointer<vtkTransform> trf0=vtkSmartPointer<vtkTransform>::New();
        trf0->RotateZ( vtkMath::Random(-90,90) );
        trf0->RotateX( vtkMath::Random(-90,90) );
        trf0->RotateY( vtkMath::Random(-90,90) );
        const double * axis = trf0->TransformNormal(0,0,1);

        for ( double z=-180; z<180; z+=delta ) 
        {
            double time = ticks();
            // rotate the clipper around its axis
            double normal[3];
            vtkSmartPointer<vtkTransform> trf=vtkSmartPointer<vtkTransform>::New();
            trf->RotateWXYZ( z, axis );
            plane->SetNormal( trf->TransformNormal(0,0,1) );
            ia->Render();
            time = ticks()-time;
            timeTotal+=time;
            count+=1.0;
            Sleep( 30 ); // too fast otherwise
        }
    }
#ifdef SOFTWARE_CLIPPER
    std::cout << "Software clipper: ";
#else
    std::cout << "Hardware clipper: ";
#endif
    std::cout << "time per iteration " << timeTotal/count << " ms" << std::endl;
    ia->Start(); // start interactor finally: use 'C','A' etc. for changing interaction modes
}

软硬件剪刀对比(Windows10、Intel i5 8th gen、VTK8.1):

Software clipper: time per iteration 76.5701 ms

Hardware clipper: time per iteration 1.63613 ms