我具有以下代码来修剪模型,但是当我更新“平面”以生成新的修剪表面时,它非常慢。有没有办法使它更快?还是有另一个vtk类可以更快地执行?
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();
但是我没有找到这个问题的答案,任何帮助都将得到帮助 我发现我的问题与下面的链接相同。要参考,请使用下面的链接
http://vtk.1045678.n5.nabble.com/is-vtkClipPolyData-slow-td5727625.html 1
答案 0 :(得分:0)
取决于您要实现的目标: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
}
硬件和软件裁剪器的比较(Windows 10,Intel i5 8th gen,VTK8.1):
Software clipper: time per iteration 76.5701 ms
Hardware clipper: time per iteration 1.63613 ms